unit U_YarnOutEdit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, cxCalendar, cxDropDownEdit, ComCtrls, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxButtonEdit, cxTextEdit, cxSpinEdit,
  StdCtrls, ExtCtrls, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator,
  dxDateRanges, dxBarBuiltInMenu, U_BaseInput, System.ImageList, Vcl.ImgList,
  dxScrollbarAnnotations, dxSkinsCore, dxSkinsDefaultPainters, cxImageList,
  dxSkinOffice2013White, dxSkinSharpPlus, dxSkinWXI;

type
  TfrmYarnOutEdit = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1BYIOID: TcxGridDBColumn;
    Tv1IOTime: TcxGridDBColumn;
    Tv1IOType: TcxGridDBColumn;
    Tv1Y_name: TcxGridDBColumn;
    Tv1Qty: TcxGridDBColumn;
    Tv1QtyUnit: TcxGridDBColumn;
    Tv1Note: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    Tv1BatchNo: TcxGridDBColumn;
    ToolButton1: TToolButton;
    Tv1ToCoName: TcxGridDBColumn;
    Tv1Piece: TcxGridDBColumn;
    Tv1Y_Composition: TcxGridDBColumn;
    Tv1Y_Color: TcxGridDBColumn;
    Tv1Y_Code: TcxGridDBColumn;
    Tv1FromCoName: TcxGridDBColumn;
    Tv1FF: TcxGridDBColumn;
    Tv1ToOrdNo: TcxGridDBColumn;
    Tv1ToConNo: TcxGridDBColumn;
    Tv1SXCBH: TcxGridDBColumn;
    Tv1SXCPM: TcxGridDBColumn;
    Tv1Denier: TcxGridDBColumn;
    Tv1ZS: TcxGridDBColumn;
    Tv1FromPurNo: TcxGridDBColumn;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBAddClick(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId, FStkName, FKHName: string;
  end;

var
  frmYarnOutEdit: TfrmYarnOutEdit;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_YarnStkSel, U_CompanySel, U_TatPlanSel;

{$R *.dfm}

procedure TfrmYarnOutEdit.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  inherited;
  Action := caFree;
end;

procedure TfrmYarnOutEdit.TBAddClick(Sender: TObject);
begin
  try
    frmYarnStkSel := TfrmYarnStkSel.Create(Application);
    with frmYarnStkSel do
    begin
      FstkName := Self.FstkName;
      if ShowModal = 1 then
      begin
        frmYarnStkSel.CDS_Main.DisableControls;
        with frmYarnStkSel.CDS_Main do
        begin
          First;
          while frmYarnStkSel.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('IOTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('IOType').Value := '车间领用';
              FieldByName('Y_Code').Value := frmYarnStkSel.CDS_Main.fieldbyname('Y_Code').Value;
              FieldByName('SXCBH').Value := frmYarnStkSel.CDS_Main.fieldbyname('SXCBH').Value;
              FieldByName('Y_Name').Value := frmYarnStkSel.CDS_Main.fieldbyname('Y_Name').Value;
              FieldByName('SXCPM').Value := frmYarnStkSel.CDS_Main.fieldbyname('SXCPM').Value;
              FieldByName('Y_Composition').Value := frmYarnStkSel.CDS_Main.fieldbyname('Y_Composition').Value;
              FieldByName('Y_Color').Value := frmYarnStkSel.CDS_Main.fieldbyname('Y_Color').Value;
              FieldByName('Denier').Value := frmYarnStkSel.CDS_Main.fieldbyname('Denier').Value;
              FieldByName('ZS').Value := frmYarnStkSel.CDS_Main.fieldbyname('ZS').Value;
              FieldByName('ff').Value := frmYarnStkSel.CDS_Main.fieldbyname('ff').Value;
              FieldByName('BatchNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('Piece').Value := frmYarnStkSel.CDS_Main.fieldbyname('STKPiece').Value;
              FieldByName('Qty').Value := frmYarnStkSel.CDS_Main.fieldbyname('STKQTY').Value;
              FieldByName('QtyUnit').Value := frmYarnStkSel.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('note').Value := frmYarnStkSel.CDS_Main.fieldbyname('note').Value;
              FieldByName('FromPurNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromPurNo').Value;   //采购单号
              FieldByName('FromCoName').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromCoName').Value; //来自单位 ，供应商
              FieldByName('ToCoNo').Value := '本厂仓库';   //出到单位
              FieldByName('ToCoName').Value := '本厂仓库'; //出到单位

              //存放单位
              FieldByName('StkCoNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('StkCoNo').Value;
              FieldByName('StkCoName').Value := frmYarnStkSel.CDS_Main.fieldbyname('StkCoName').Value;

              //来自单位 ，供应商
              FieldByName('FromCoNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromCoNo').Value;
              //入库织造单
              FieldByName('FromOrdNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromOrdNo').value;
              FieldByName('FromMainId').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromMainId').value;
              FieldByName('FromSubId').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromSubId').value;
              //入库坯布合同号
              FieldByName('FromConNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromConNo').value;
              FieldByName('fromConMId').Value := frmYarnStkSel.CDS_Main.fieldbyname('fromConMId').value;
              FieldByName('fromConSId').Value := frmYarnStkSel.CDS_Main.fieldbyname('fromConSId').value;
                //入库采购计划单号
//                FieldByName('FromPurNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('PurNo').value;
              FieldByName('FromPurMId').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromPurMId').value;
              FieldByName('FromPurSId').Value := frmYarnStkSel.CDS_Main.fieldbyname('FromPurSId').value;

              FieldByName('STKID').Value := frmYarnStkSel.CDS_Main.fieldbyname('BYIOID').Value;

            end;
            frmYarnStkSel.CDS_Main.Delete;
          end;
        end;
        frmYarnStkSel.CDS_Main.EnableControls;
      end;
    end;
  finally
    frmYarnStkSel.Free;
  end;

end;

function TfrmYarnOutEdit.SaveCKData(): Boolean;
var
  MBYIOID, Maxno: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(CDS_Sub.fieldbyname('BYIOID').AsString) + '''');
          Open;
        end;
        MBYIOID := Trim(ADOQueryTemp.fieldbyname('BYIOID').AsString);
        if Trim(MBYIOID) = '' then
        begin
          if not GetLSNo(ADOQueryCmd, Maxno, 'YC', 'BS_Yarn_IO', 3, 1) then
            raise Exception.Create('取染色坯布出库编号失败！');
        end
        else
        begin
          Maxno := Trim(MBYIOID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(MBYIOID) = '' then
          begin
            Append;
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillId').Value := Trim(DName);
          end
          else
          begin
            Edit;
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'BS_Yarn_IO', 0);

          //供应商
          FieldByName('FromCoNo').Value := CDS_Sub.fieldbyname('FromCoNo').Value;
                        //入库织造单
          FieldByName('FromOrdNo').Value := CDS_Sub.fieldbyname('FromOrdNo').value;
          FieldByName('FromMainId').Value := CDS_Sub.fieldbyname('FromMainId').value;
          FieldByName('FromSubId').Value := CDS_Sub.fieldbyname('FromSubId').value;
                //入库坯布合同号
          FieldByName('FromConNo').Value := CDS_Sub.fieldbyname('FromConNo').value;
          FieldByName('fromConMId').Value := CDS_Sub.fieldbyname('fromConMId').value;
          FieldByName('fromConSId').Value := CDS_Sub.fieldbyname('fromConSId').value;
                //入库采购计划单号
//                FieldByName('FromPurNo').Value := frmYarnStkSel.CDS_Main.fieldbyname('PurNo').value;
          FieldByName('FromPurMId').Value := CDS_Sub.fieldbyname('FromPurMId').value;
          FieldByName('FromPurSId').Value := CDS_Sub.fieldbyname('FromPurSId').value;
            //       出库织造单号
          FieldByName('ToMainId').Value := CDS_Sub.fieldbyname('ToMainId').value;
          FieldByName('ToSubId').Value := CDS_Sub.fieldbyname('ToSubId').value;
   //       出库坯布合同号
          FieldByName('ToConMId').Value := CDS_Sub.fieldbyname('ToConMId').value;
          FieldByName('ToConSId').Value := CDS_Sub.fieldbyname('ToConSId').value;
          //出到单位
          FieldByName('ToCoNo').Value := CDS_Sub.fieldbyname('ToCoNo').Value;
          //存放单位
          FieldByName('StkCoNo').Value := CDS_Sub.fieldbyname('StkCoNo').Value;
          FieldByName('StkCoName').Value := CDS_Sub.fieldbyname('StkCoName').Value;

          FieldByName('STKName').Value := FSTKName;
          FieldByName('BYIOID').Value := Trim(Maxno);
          FieldByName('IOFlag').Value := '出库';
          FieldByName('IOQtyFlag').Value := -1;
          FieldByName('STKID').Value := CDS_Sub.fieldbyname('STKID').Value;

          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update BS_Yarn_IO Set StkPiece=(select Sum(isnull(Piece,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID)');
          sql.Add(',STKQty=(select Sum(isnull(Qty,0)*IOQtyFlag) from BS_Yarn_IO A where A.STKID=BS_Yarn_IO.BYIOID )');
          sql.Add('where BYIOID=''' + Trim(CDS_Sub.fieldbyname('STKID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(CDS_Sub.fieldbyname('STKID').AsString) + '''');
          Open;
        end;

        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('出库数量大于入库数量不能保存！');

        if ADOQueryTemp.FieldByName('StkPiece').Value < 0 then
          raise Exception.Create('出库箱/件数大于入库箱/件数不能保存！');

        with ADOQueryCmd do
        begin
          Close;
          Sql.Clear;
          Sql.Add('exec P_BS_Yarn_Move ');
          Sql.Add(' @BYIOID=' + quotedstr(Trim(Maxno)));
          Open;
        end;
        if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
          raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
          sql.Add('values(''纱线出库''    ');
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(',''保存''   ');
          sql.Add(',' + quotedstr(DName));
          sql.Add(',' + quotedstr(trim(self.Caption)));
          sql.Add(',' + quotedstr(Maxno));
          sql.Add(')  ');
          ExecSQL;
        end;
        Edit;
        FieldByName('BYIOID').Value := Trim(Maxno);
        Post;

        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
  end;

end;

procedure TfrmYarnOutEdit.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmYarnOutEdit.FormShow(Sender: TObject);
var
  fsj: string;
begin
  inherited;
  ReadCxGrid(FSTKName + '出库登记', Tv1, '纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from BS_Yarn_IO A');
    sql.Add(' where BYIOID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_Sub);
  SInitCDSData(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmYarnOutEdit.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('BYIOID').AsString) <> '' then
  begin
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_Fin_Flow_Judge ');
      Sql.Add(' @FFIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').AsString)));
      Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
      Sql.Add(',@DName=' + quotedstr(Trim(DName)));
      Open;
    end;
    if ADOQueryTemp.FieldByName('intReturn').AsInteger = -1 then
    begin
      Application.MessageBox(PChar(ADOQueryTemp.fieldbyname('ShowMsg').AsString), '提示', 0);
      exit;
    end;
    try
      ADOQueryCmd.Connection.BeginTrans;
      with ADOQueryCmd do
      begin
        Close;
        Sql.Clear;
        sql.Add('insert into Finance_Need_Up(UType,UDataId,UOperation,UOperator,UModule,UDetails)       ');
        sql.Add('values(''纱线出库''    ');
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').AsString)));
        sql.Add(',''删除''   ');
        sql.Add(',' + quotedstr(DName));
        sql.Add(',' + quotedstr(trim(self.Caption)));
        sql.Add(',' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').AsString)));
        sql.Add(')  ');

        Sql.Add('exec P_BS_Yarn_Out_Del ');
        Sql.Add(' @BYIOIDS=' + quotedstr(Trim(CDS_Sub.fieldbyname('BYIOID').AsString)));
        Sql.Add(',@DCode=' + quotedstr(Trim(DCode)));
        Sql.Add(',@DName=' + quotedstr(Trim(DName)));
        Open;
      end;
      if ADOQueryCmd.FieldByName('intReturn').AsInteger = -1 then
        raise Exception.Create(pchar(trim(ADOQueryCmd.FieldByName('ShowMsg').AsString)));
      ADOQueryCmd.Connection.CommitTrans;
      CDS_Sub.Delete;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      application.MessageBox(PChar(Exception(ExceptObject).Message), '提示信息', 0);
    end;
  end
  else
    CDS_Sub.Delete;

end;

procedure TfrmYarnOutEdit.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('IOTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('IOType', null, []) = True then
  begin
    Application.MessageBox('出库类型不能为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;

  CDS_Sub.First;
  while not CDS_Sub.Eof do
  begin
  // 同时判断两个条件
    if (CDS_Sub.FieldByName('IOType').AsString = '车间领用') and (CDS_Sub.FieldByName('ToOrdNo').IsNull or (Trim(CDS_Sub.FieldByName('ToOrdNo').AsString) = '')) then
    begin
      Application.MessageBox('请选择车间领用 出到织造单!', '提示', 0);
      Exit;
    end;
    CDS_Sub.Next;
  end;

  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmYarnOutEdit.ToolButton1Click(Sender: TObject);
begin
  WriteCxGrid(fSTKName + '出库登记', Tv1, '纱线仓库');

end;

procedure TfrmYarnOutEdit.Tv1Column2PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmCompanySel := TfrmCompanySel.Create(Application);
    with frmCompanySel do
    begin
      FCoType := '供应商';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToCoNo').Value := Trim(CDS_1.fieldbyname('CoCode').AsString);
          FieldByName('ToCoName').Value := Trim(CDS_1.fieldbyname('CoAbbrName').AsString);
          post;
        end;
      end;
    end;
  finally
    frmCompanySel.Free;
  end;

end;

procedure TfrmYarnOutEdit.Tv1Column7PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
  try
    frmTatPlanSel := TfrmTatPlanSel.Create(Application);
    with frmTatPlanSel do
    begin
      FCoType := '';
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;

   //        出库织造单
          FieldByName('ToOrdNo').Value := CDS_1.fieldbyname('OrderNo').value;
          FieldByName('ToMainId').Value := CDS_1.fieldbyname('MainId').value;
          FieldByName('ToSubId').Value := CDS_1.fieldbyname('SubId').value;

   //       出库坯布合同号
          FieldByName('ToConNo').Value := CDS_1.fieldbyname('ConNo').value;
          FieldByName('ToConMId').Value := CDS_1.fieldbyname('ConMId').value;
          FieldByName('ToConSId').Value := CDS_1.fieldbyname('ConSId').value;

        end;
      end;
    end;
  finally
    frmTatPlanSel.Free;
  end;

end;

procedure TfrmYarnOutEdit.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  FFSPID: string;
begin
//  if CDS_Sub.IsEmpty then
//    Exit;
//  if Trim(CDS_Sub.fieldbyname('BYIOID').AsString) <> '' then
//  begin
//    with ADOQueryTemp do
//    begin
//      Close;
//      sql.Clear;
//      sql.Add('select * from BS_Yarn_IO where BYIOID=''' + Trim(CDS_Sub.fieldbyname('BYIOID').AsString) + '''');
//      Open;
//    end;
//    FFSPID := Trim(ADOQueryTemp.fieldbyname('BYIOID').AsString);
//    if Trim(FFSPID) <> '' then
//    begin
//      v1CRType.Options.Editing := False;
//    end
//    else
//    begin
//      v1CRType.Options.Editing := True;
//    end;
//  end
//  else
//  begin
//    v1CRType.Options.Editing := True;
//  end;
end;

end.

