unit U_SXCKTHPut;

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;

type
  TfrmSXCKTHPut = class(TForm)
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBSave: TToolButton;
    TBClose: TToolButton;
    DataSource3: TDataSource;
    CDS_Sub: TClientDataSet;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    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 v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
  public
    { Public declarations }
    FBCId:String;
  end;

var
  frmSXCKTHPut: TfrmSXCKTHPut;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractListHZCX,U_ZDYHelp,U_SXCKKCSelList;

{$R *.dfm}

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

procedure TfrmSXCKTHPut.TBAddClick(Sender: TObject);
begin
  try
    frmSXCKKCSelList:=TfrmSXCKKCSelList.Create(Application);
    with frmSXCKKCSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Append;
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
          FieldByName('CGMainId').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('CGMainId').Value;
          FieldByName('CGSubId').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('CGSubId').Value;
          FieldByName('FactoryName').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
          FieldByName('FactoryNo').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
          FieldByName('SPName').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('SPName').Value;
          FieldByName('BatchNo').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('BatchNo').Value;
          FieldByName('KuWei').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('KuWei').Value;
          FieldByName('SPCF').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('SPCF').Value;
          FieldByName('ShaZhi').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('ShaZhi').Value;
          FieldByName('SeBie').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('SeBie').Value;
          FieldByName('PiQty').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
          FieldByName('KgQty').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('MXKCKgQty').Value;
          FieldByName('CRID').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('CRID').Value;
          FieldByName('FZSPID').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
          FieldByName('Price').Value:=frmSXCKKCSelList.CDS_Main.fieldbyname('Price').Value;
        end;
      end;
    end;
  finally
    frmSXCKKCSelList.Free;
  end;
end;
function TfrmSXCKTHPut.SaveCKData():Boolean;
var
  FCRID,Maxno:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_Sub.DisableControls;
    with CDS_Sub do
    begin
      First;
      while not eof do
      begin
        if Trim(CDS_Sub.fieldbyname('SPID').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'ST','CK_SXPB_CR',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线退货编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(CDS_Sub.fieldbyname('SPID').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(Maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(CDS_Sub.fieldbyname('SPID').AsString)='' then
          begin
            Append;
          end else
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty=False  then Edit else Append;
          end;
          FieldByName('SPID').Value:=Trim(Maxno);
          FieldByName('CGMainId').Value:=Trim(CDS_Sub.fieldbyname('CGMainId').AsString);
          FieldByName('CGSubId').Value:=Trim(CDS_Sub.fieldbyname('CGSubId').AsString);
          FieldByName('FZSPID').Value:=Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('Price').Value:=CDS_Sub.fieldbyname('Price').Value;
          FieldByName('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRQtyFlag').Value:=-1;
          FieldByName('CRType').Value:='退货';
          FieldByName('CKName').Value:='纱线';
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_SXPB_CR',2);
          if Trim(CDS_Sub.fieldbyname('SPID').AsString)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
              Open;
            end;
            if ADOQueryTemp.IsEmpty=False then
            begin
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
            end else
            begin
              FieldByName('Filler').Value:=Trim(DName);
            end;
          end;
          Post;
        end;
         with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_KC Set KCKGQty=(select sum(KGQty*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
          sql.Add(',KCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
          sql.Add(' where CRID='+CDS_Sub.fieldbyname('CRID').AsString);
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_KC where CRID='+Trim(CDS_Sub.fieldbyname('CRID').AsString));
          Open;
        end;
        if ADOQueryTemp.FieldByName('KCKGQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('退货数大于库存公斤数不能保存!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCKgQty=(select sum(isnull(KGQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
          sql.Add(' where SPID='''+Trim(CDS_Sub.fieldbyname('FZSPID').AsString)+'''');
          ExecSQL;
        end;
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('FZSPID').AsString)+'''');
          Open;
        end;
        if ADOQueryTemp.FieldByName('MXKCKGQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('退货数大于库存数不能保存!','提示',0);
          Exit;
        end;
        Edit;
        FieldByName('SPID').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 TfrmSXCKTHPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('纱线退货登记',Tv1,'纱线仓库');
  Close;
end;

procedure TfrmSXCKTHPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线退货登记',Tv1,'纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    //sql.Add(' ,ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId )');

    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
end;

procedure TfrmSXCKTHPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    try
      ADOQueryCmd.Connection.BeginTrans;
     
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' delete CK_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        sql.Add('Update CK_SXPB_CR Set MXKCKgQty=(select sum(isnull(KGQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(',MXKCPiQty=(select sum(isnull(PiQty,0)*CRQtyFlag) from CK_SXPB_CR A where A.FZSPID=CK_SXPB_CR.SPID)');
        sql.Add(' where SPID='''+Trim(CDS_Sub.fieldbyname('FZSPID').AsString)+'''');
        sql.Add('Update CK_SXPB_KC Set KCKGQty=(select sum(KGQty*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
        sql.Add(',KCPiQty=(select sum(PiQty*CRQtyFlag) from CK_SXPB_CR A where A.CRID=CK_SXPB_KC.CRID)');
        sql.Add(' where CRID='+Trim(CDS_Sub.fieldbyname('CRID').AsString));
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;

  end;
  CDS_Sub.Delete;
end;

procedure TfrmSXCKTHPut.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('KGQty',null,[])=True then
  begin
    Application.MessageBox('净重不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmSXCKTHPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmClothContractListHZCX:=TfrmClothContractListHZCX.Create(Application);
    with frmClothContractListHZCX do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('CGConNo').Value:=Trim(frmClothContractListHZCX.Order_Main.fieldbyname('ConNo').AsString);
          FieldByName('CGConMainId').Value:=Trim(frmClothContractListHZCX.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('CGConSubId').Value:=Trim(frmClothContractListHZCX.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('FactoryName').Value:=Trim(frmClothContractListHZCX.Order_Main.fieldbyname('FactoryNoName').AsString);
          FieldByName('SXName').Value:=Trim(frmClothContractListHZCX.Order_Main.fieldbyname('C_CodeName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmClothContractListHZCX.Free;
  end;
end;

end.
