unit U_ShaOutPut;

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, cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaOutPut = class(TForm)
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1ComTaiTou: TcxGridDBColumn;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    Tv1Column1: 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 v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ToolButton1Click(Sender: TObject);
  private
    { Private declarations }
    function SaveCKData(): Boolean;
  public
    { Public declarations }
    FBCId: string;
  end;

var
  frmShaOutPut: TfrmShaOutPut;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_ShaKCSelList, U_ZdyAttachGYS;

{$R *.dfm}

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

procedure TfrmShaOutPut.TBAddClick(Sender: TObject);
begin
  try
    frmShaKCSelList := TfrmShaKCSelList.Create(Application);
    with frmShaKCSelList do
    begin
      ToolButton1.Visible := True;
      if ShowModal = 1 then
      begin
        frmShaKCSelList.CDS_Main.DisableControls;
        with frmShaKCSelList.CDS_Main do
        begin
          First;
          while frmShaKCSelList.CDS_Main.Locate('SSel', True, []) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value := Trim(FormatDateTime('yyyy-MM-dd', Now));
              FieldByName('FactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('SyrName').Value := frmShaKCSelList.CDS_Main.fieldbyname('SyrName').Value;
              FieldByName('FactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('Y_CodeName').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_CodeName').Value;
              FieldByName('Y_Code').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_Code').Value;
              FieldByName('Y_Spec').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_Spec').Value;
              FieldByName('Y_CF').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_CF').Value;
              FieldByName('Y_MF').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_MF').Value;
              FieldByName('Y_KZ').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_KZ').Value;
              FieldByName('FromFactoryName').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('FromFactoryNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('QtyUnit').Value := frmShaKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value := frmShaKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
              FieldByName('Qty').Value := frmShaKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;
              FieldByName('KCID').Value := frmShaKCSelList.CDS_Main.fieldbyname('Y_ID').Value;
              FieldByName('BatchNo').Value := frmShaKCSelList.CDS_Main.fieldbyname('BatchNo').Value;
              FieldByName('danbao').Value := frmShaKCSelList.CDS_Main.fieldbyname('danbao').Value;
            end;
            frmShaKCSelList.CDS_Main.Delete;
          end;
        end;
        frmShaKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmShaKCSelList.Free;
  end;
end;

function TfrmShaOutPut.SaveCKData(): Boolean;
var
  FCRID, Maxno, FY_ID, MaxPYNo, FPYID, FFString: 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 CK_Yarn_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
          Open;
        end;
        FY_ID := Trim(ADOQueryTemp.fieldbyname('Y_ID').AsString);
        if Trim(FY_ID) = '' then
        begin
          if GetLSNo(ADOQueryCmd, Maxno, 'YC', 'CK_Yarn_CR', 3, 1) = False then
          begin
            CDS_Sub.EnableControls;
            Result := False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线出库编号失败!', '提示', 0);
            Exit;
          end;
        end
        else
        begin
          Maxno := Trim(FY_ID);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(Maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FY_ID) = '' then
          begin
            Append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('Y_ID').Value := Trim(Maxno);
          FieldByName('KCID').Value := Trim(CDS_Sub.fieldbyname('KCID').AsString);
          FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value := Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CRFlag').Value := '出库';
          FieldByName('CRQtyFlag').Value := -1;
          RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_Yarn_CR', 2);
          FieldByName('CKName').Value := '纱线';
          if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
          begin
            FieldByName('PYID').Value := Trim(Maxno);
          end;
          if Trim(FY_ID) = '' then
          begin
            FieldByName('Filler').Value := Trim(DName);
            FieldByName('FillCode').Value := Trim(DCode);
            FieldByName('FillTime').Value := SGetServerDate(ADOQueryTemp);
          end
          else
          begin
            FieldByName('Editer').Value := Trim(DName);
            FieldByName('EditCode').Value := Trim(DCode);
            FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_Yarn_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
          sql.Add(' where Y_ID=''' + Trim(CDS_Sub.fieldbyname('KCID').AsString) + '''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(CDS_Sub.fieldbyname('KCID').AsString) + '''');
          //ShowMessage(sql.text);
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
        begin
          FFString := Trim(CDS_Sub.fieldbyname('KCID').AsString);
          CDS_Sub.EnableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
          CDS_Sub.Locate('KCID', FFString, []);

          Exit;
        end;
        if Trim(CDS_Sub.fieldbyname('CRType').AsString) = '平移出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_Yarn_CR where PYID=''' + Trim(Maxno) + '''');
            sql.Add(' and isnull(CRType,'''')=''平移入库'' ');
            Open;
          end;
          FPYID := Trim(ADOQueryTemp.fieldbyname('Y_ID').AsString);
          if Trim(FPYID) = '' then
          begin
            if GetLSNo(ADOQueryCmd, MaxPYNo, 'YR', 'CK_Yarn_CR', 3, 1) = False then
            begin
              Result := False;
              CDS_Sub.EnableControls;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取纱线平移入库编号失败!', '提示', 0);
              Exit;
            end;
          end
          else
          begin
            MaxPYNo := Trim(FPYID);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(MaxPYNo) + '''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FPYID) = '' then
            begin
              Append;
            end
            else
            begin
              Edit;
            end;
            FieldByName('Y_ID').Value := Trim(MaxPYNo);
            FieldByName('KCID').Value := Trim(MaxPYNo);
            FieldByName('FactoryNo').Value := Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
            FieldByName('FromFactoryNo').Value := Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
            FieldByName('ToFactoryNo').Value := Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
            FieldByName('CRFlag').Value := '入库';
            FieldByName('CRQtyFlag').Value := 1;
            RTSetSaveDataCDS(ADOQueryCmd, Tv1, CDS_Sub, 'CK_SXPB_CR', 2);
            FieldByName('CKName').Value := '纱线';
            FieldByName('CRType').Value := '平移入库';
            FieldByName('PYID').Value := Trim(Maxno);
            if Trim(FY_ID) = '' then
            begin
              FieldByName('Filler').Value := Trim(DName);
              FieldByName('FillCode').Value := Trim(DCode);
              FieldByName('FillTime').Value := SGetServerDate(ADOQueryTemp);
            end
            else
            begin
              FieldByName('Editer').Value := Trim(DName);
              FieldByName('EditCode').Value := Trim(DCode);
              FieldByName('EditTime').Value := SGetServerDate(ADOQueryTemp);
            end;
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_Yarn_CR Set MXKCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID)');
            sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID)');
            sql.Add(' where Y_ID=''' + Trim(MaxPYNo) + '''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_Yarn_CR where Y_ID=''' + Trim(MaxPYNo) + '''');
            Open;
          end;
          if ADOQueryTemp.FieldByName('MXKCQty').Value < 0 then
          begin
            FFString := Trim(CDS_Sub.fieldbyname('KCID').AsString);
            CDS_Sub.EnableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('出库数大于库存数不能保存!', '提示', 0);
            CDS_Sub.Locate('KCID', FFString, []);
            Exit;
          end;
        end;
        Edit;
        FieldByName('Y_ID').Value := Trim(Maxno);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmShaOutPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线出库登记2', Tv1, '纱线仓库');
  Close;
end;

procedure TfrmShaOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线出库登记2', Tv1, '纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_Yarn_CR A');
    sql.Add(' where Y_ID=''' + Trim(FBCId) + '''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_Sub);
  SInitCDSData20(ADOQueryTemp, CDS_Sub);
end;

procedure TfrmShaOutPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if Trim(CDS_Sub.fieldbyname('Y_ID').AsString) <> '' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_Yarn_CR where KCID=');
      sql.Add('(select Y_ID from CK_Yarn_CR where PYID=''' + Trim(CDS_Sub.fieldbyname('y_ID').AsString) + '''');
      sql.Add(' and isnull(CRType,'''')=''平移入库'')');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty = False then
    begin
      Application.MessageBox('已有出库记录，不能删除!', '提示', 0);
      Exit;
    end;
    if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
      Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;

      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_Yarn_CR where y_ID=''' + Trim(CDS_Sub.fieldbyname('Y_ID').AsString) + '''');
        sql.Add('Update CK_Yarn_CR Set MXKCQty=(select sum(isnullQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_Yarn_CR A where A.KCID=CK_Yarn_CR.Y_ID )');
        sql.Add(' where Y_ID=''' + Trim(CDS_Sub.fieldbyname('KCID').AsString) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!', '提示', 0);
    end;

  end;
  CDS_Sub.Delete;
end;

procedure TfrmShaOutPut.TBSaveClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then
    Exit;
  if CDS_Sub.Locate('CRTime', null, []) = True then
  begin
    Application.MessageBox('出库时间不能为空!', '提示', 0);
    Exit;
  end;
  if (CDS_Sub.Locate('CRType', null, []) = True) and (CDS_Sub.Locate('ToFactoryName', null, []) = True) then
  begin
    Application.MessageBox('出库类型或出到加工厂不能都为空!', '提示', 0);
    Exit;
  end;

  if CDS_Sub.Locate('Qty', null, []) = True then
  begin
    Application.MessageBox('数量不能为空!', '提示', 0);
    Exit;
  end;
  if CDS_Sub.Locate('PiQty', null, []) = True then
  begin
    Application.MessageBox('箱/件数不能为空!', '提示', 0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaOutPut.v1Column11PropertiesButtonClick(Sender: TObject; AButtonIndex: Integer);
begin
 // if Trim(CDS_Sub.fieldbyname('ConNo').AsString)<>'' then Exit;
  try
    frmZdyAttachGYS := TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal = 1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ToFactoryNo').Value := Trim(CDS_HZ.fieldbyname('CoID').AsString);
          FieldByName('ToFactoryName').Value := Trim(CDS_HZ.fieldbyname('Name').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

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

procedure TfrmShaOutPut.ToolButton1Click(Sender: TObject);
begin
  OneKeyPost(Tv1, CDS_Sub);
end;

end.

