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;

type
  TfrmShaOutPut = 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;
    v1CRType: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column16: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column5: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: 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 v1Column1PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column11PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    function YFData():Boolean;
  public
    { Public declarations }
    FBCId:String;
  end;

var
  frmShaOutPut: TfrmShaOutPut;

implementation
uses
  U_DataLink,U_RTFun,U_ClothContractListHZCX,U_ZDYHelp,U_ShaKCSelList,
  U_GYSSelList, U_ZdyAttachGYS, U_ProductOrderListSel,
  U_BPShengChanListSel, U_ContractCGJGSelList;

{$R *.dfm}

function TfrmShaOutPut.YFData():Boolean;
var
  CRID,YFID,Price,PriceUnit,OrderUnit,FComTaiTou:String;
begin
  Result:=False;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select ComTaiTou from Cloth_Main where Mainid='''+Trim(CDS_Sub.fieldbyname('SCMainIdCK').AsString)+'''');
        Open;
      end;
      FComTaiTou:=Trim(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from YF_Money_KC where FactoryName='''+Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty=False then
      begin
        CRID:=ADOQueryTemp.fieldbyname('CRID').AsString;
      end else
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update YF_Money_CRID set CRID=CRID+1');
          sql.Add('select * from YF_Money_CRID ');
          Open;
        end;
        CRID:=ADOQueryCmd.fieldbyname('CRID').AsString;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from YF_Money_KC where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
          FieldByName('ZdyStr1').Value:='应收收';
          Post;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where FactoryName='''+Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString)+'''');
        sql.Add(' and YFTypeId='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        if GetLSNo(ADOQueryCmd,YFID,'CS','YF_Money_CR',3,1)=False then
        begin
          Application.MessageBox('取成品应收最大号失败!','提示',0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from YF_Money_CR where 1<>1');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('YFID').Value:=Trim(YFID);
          FieldByName('YFTypeId').Value:=Trim(CDS_Sub.fieldbyname('SPID').AsString);
          FieldByName('CRID').Value:=StrToInt(CRID);
          FieldByName('Filler').Value:=Trim(DName);
          FieldByName('status').Value:='0';
          FieldByName('CRType').Value:='应收款登记';
          FieldByName('CRFlag').Value:='应收收';
          FieldByName('QtyFlag').Value:=1;
          FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('ToFactoryName').AsString);
          FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',now));
          FieldByName('YFType').Value:='自动生成';
          FieldByName('Huilv').Value:=1;
          FieldByName('BZType').Value:='￥';
          FieldByName('QtyUnit').Value:=Trim(CDS_Sub.FieldByName('QtyUnit').asstring);
          FieldByName('ComTaiTou').Value:=Trim(FComTaiTou);
          FieldByName('YFName').Value:='纱线销售金额';
          FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('SCMainidCK').AsString);
          Post;
        end;
      end  else
      begin
        YFID:=Trim(ADOQueryTemp.fieldbyname('YFID').AsString);
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('update YF_Money_CR Set Qty=(select isnull(Sum(Qty),0) from CK_SXPB_CR ');
        sql.Add(' where SPID=YF_Money_CR.YFTypeId)');
        sql.Add(' where YFTypeId='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
        ExecSQL;
      end;
      {with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_CR Set Money=Price*Qty,BBMoney=Price*Qty*HuiLv');
        sql.Add(' where YFID='''+Trim(YFID)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update YF_Money_KC Set KCMoney=(select isnull(Sum(Money*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(',KCBBMoney=(select isnull(Sum(BBMoney*QtyFlag),0) from YF_Money_CR A where A.CRID=YF_Money_KC.CRID)');
        sql.Add(' where CRID='+CRID);
        ExecSQL;
      end;}
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where YFId='''+Trim(YFID)+'''');
        Open;
      end;
      if ADOQueryTemp.FieldByName('Qty').Value=0 then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_CR where YFID='''+Trim(YFID)+'''');
          ExecSQL;
        end;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from YF_Money_CR where CRId='+CRID);
        Open;
      end;
      if ADOQueryTemp.IsEmpty then
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('delete from YF_Money_KC where CRId='+CRID);
          ExecSQL;
        end;
      end;
  Result:=True;
end;

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('QCSCOrderNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SCOrderNo').Value;
              FieldByName('FactoryName').Value:=frmShaKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPCode').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value:=frmShaKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('SPSpec').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SPMF').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('FromFactoryName').Value:=frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('FromFactoryNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('ToFactoryName').Value:=frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryName').Value;
              FieldByName('ToFactoryNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('ToFactoryNo').Value;
              FieldByName('CKSCOrderNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SCOrderNo').Value;
              FieldByName('SCMainIdCK').Value:=Trim(frmShaKCSelList.CDS_Main.fieldbyname('SCMainIdRK').AsString);
              FieldByName('SCSubIdCK').Value:=Trim(frmShaKCSelList.CDS_Main.fieldbyname('SCSubIdRK').AsString);
              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('FZSPID').Value:=frmShaKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('CGMainIdFZ').Value:=frmShaKCSelList.CDS_Main.fieldbyname('CGMainId').Value;
              FieldByName('CGSubIdFZ').Value:=frmShaKCSelList.CDS_Main.fieldbyname('CGSubId').Value;
              FieldByName('BatchNo').Value:=frmShaKCSelList.CDS_Main.fieldbyname('BatchNo').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,FSPID,MaxPYNo,FPYID: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_SXPB_CR where SPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
          Open;
        end;
        FSPID:=Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
        if Trim(FSPID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,'SC','CK_SXPB_CR',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取纱线出库编号失败!','提示',0);
            Exit;
          end;
        end else
        begin
          Maxno:=Trim(FSPID);
        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(FSPID)='' then
          begin
            Append;
          end else
          begin
            Edit;
          end;
          FieldByName('SPID').Value:=Trim(Maxno);
          FieldByName('SCMainIdRK').Value:=Trim(CDS_Sub.fieldbyname('SCMainIdRK').AsString);
          FieldByName('SCSubIdRK').Value:=Trim(CDS_Sub.fieldbyname('SCSubIdRK').AsString);
          FieldByName('SCMainIdCK').Value:=Trim(CDS_Sub.fieldbyname('SCMainIdCK').AsString);
          FieldByName('SCSubIdCK').Value:=Trim(CDS_Sub.fieldbyname('SCSubIdCK').AsString);
          FieldByName('FZSPID').Value:=Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FromFactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FromFactoryNo').AsString);
          fieldbyname('FactoryName').Value:=trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('CPID').Value:=Trim(CDS_Sub.fieldbyname('CPID').AsString);
          FieldByName('QCSCOrderNo').Value:=Trim(CDS_Sub.fieldbyname('QCSCOrderNo').AsString);
          FieldByName('CKSCOrderNo').Value:=Trim(CDS_Sub.fieldbyname('CKSCOrderNo').AsString);
          //FieldByName('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRQtyFlag').Value:=-1;
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_SXPB_CR',2);
          FieldByName('CKName').Value:='纱线';
          if Trim(CDS_Sub.fieldbyname('CRType').AsString)='平移出库' then
          begin
            FieldByName('PYID').Value:=Trim(Maxno);
          end;
          if Trim(FSPID)='' then
            FieldByName('Filler').Value:=Trim(DName)
          else
          begin
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
          end;
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,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('MXKCQty').Value<0 then
        begin
          CDS_Sub.DisableControls;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('出库数大于库存数不能保存!','提示',0);
          Exit;
        end;

        {if Trim(CDS_Sub.fieldbyname('CRType').AsString)='平移出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where PYID='''+Trim(Maxno)+'''');
            sql.Add(' and isnull(CRType,'''')=''平移入库'' ');
            Open;
          end;
          FPYID:=Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
          if Trim(FPYID)='' then
          begin
            if GetLSNo(ADOQueryCmd,MaxPYNo,'YR','CK_SXPB_CR',3,1)=False then
            begin
              Result:=False;
              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_SXPB_CR where SPID='''+Trim(MaxPYNo)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FPYID)='' then
            begin
              Append;
            end else
            begin
              Edit;
            end;
            FieldByName('SPID').Value:=Trim(MaxPYNo);
            FieldByName('ORDMainIdRK').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
            FieldByName('ORDSubIdRK').Value:=Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);
            fieldbyname('FactoryName').Value:=trim(CDS_Sub.fieldbyname('FactoryName').AsString);
            FieldByName('FZSPID').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('CRID').Value:=CDS_Sub.fieldbyname('CRID').Value;
            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(FPYID)='' then
              FieldByName('Filler').Value:=Trim(DName)
            else
            begin
              FieldByName('Editer').Value:=Trim(DName);
              FieldByName('EditTime').Value:=SGetServerDate(ADOQueryTemp);
            end;
            Post;
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('Update CK_SXPB_CR Set MXKCQty=(select sum(isnull(Qty,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(MaxPYNo)+'''');
            ExecSQL;
          end;
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where SPID='''+Trim(MaxPYNo)+'''');
            Open;
          end;
          if ADOQueryTemp.FieldByName('MXKCQty').Value<0 then
          begin
            CDS_Sub.DisableControls;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('出库数大于库存数不能保存!','提示',0);
            Exit;
          end;
        end;}
        Edit;
        FieldByName('SPID').Value:=Trim(Maxno);
        Post;
        {if Trim(CDS_Sub.fieldbyname('CRType').AsString)='销售出库' then
        begin
          YFData();
        end;}
        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('纱线出库登记1',Tv1,'纱线仓库');
  Close;
end;

procedure TfrmShaOutPut.FormShow(Sender: TObject);
begin
  ReadCxGrid('纱线出库登记1',Tv1,'纱线仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' ,OrderNo=(select OrderNo from JYOrder_Main JM where JM.MainId=A.OrdMainIdRK )');
    sql.Add(' ,OrderNoCK=(select OrderNo from JYOrder_Main JM where JM.MainId=A.OrdMainIdCK )');
    sql.Add(' ,SOrddefstr1=(select SOrddefstr1 from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' ,PRTColor=(select PRTColor from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    sql.Add(' ,PRTHX=(select PRTHX from JYOrder_Sub JM where JM.SubId=A.OrdSubIdCK )');
    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 TfrmShaOutPut.TBDelClick(Sender: TObject);
begin
  if CDS_Sub.IsEmpty then Exit;
  if Trim(CDS_Sub.fieldbyname('SPID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where FZSPID=');
      sql.Add('(select SPID from CK_SXPB_CR where PYID='''+Trim(CDS_Sub.fieldbyname('SPID').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_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(',MXKCQty=(select sum(isnull(Qty,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;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('删除异常!','提示',0);
    end;
    {if CDS_Sub.fieldbyname('CRType').AsString='销售出库' then
    begin
      YFData();
    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 then
  begin
    Application.MessageBox('出库类型不能为空!','提示',0);
    Exit;
  end;
  if 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 CDS_Sub.Locate('CRType;CKSCOrderNo',VarArrayOf(['染纱加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('染纱加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;CKSCOrderNo',VarArrayOf(['网络加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('网络加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if CDS_Sub.Locate('CRType;CKSCOrderNo',VarArrayOf(['织造加工出库',Null]),[loPartialKey])=True then
  begin
    Application.MessageBox('织造加工出库出到白坯单号不能为空!','提示',0);
    Exit;
  end;
  if SaveCKData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    //ModalResult:=1;
    Exit;
  end;
end;

procedure TfrmShaOutPut.v1Column1PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmContractCGJGSelList:=TfrmContractCGJGSelList.Create(Application);
    with frmContractCGJGSelList do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('ConNo').Value:=frmContractCGJGSelList.Order_Main.fieldbyname('ConNo').Value;
          FieldByName('CGMainId').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('CGSubId').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('ToFactoryName').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNoName').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('FactoryNo').AsString);
          FieldByName('SPName').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CodeName').AsString);
          FieldByName('SPSpec').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_Spec').AsString);
          FieldByName('SPCF').Value:=Trim(frmContractCGJGSelList.Order_Main.fieldbyname('C_CF').AsString);
        end;
      end;
    end;
  finally
    frmContractCGJGSelList.Free;
  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(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyCode').AsString) ;
          FieldByName('ToFactoryName').Value:=Trim(frmZdyAttachGYS.CDS_HZ.fieldbyname('ZdyName').AsString);
          //Post;
        end;
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

procedure TfrmShaOutPut.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmBPShengChanListSel:=TfrmBPShengChanListSel.Create(Application);
    with frmBPShengChanListSel do
    begin
      if ShowModal=1 then
      begin
        with Self.CDS_Sub do
        begin
          Edit;
          FieldByName('SCMainIdCK').Value:=Trim(frmBPShengChanListSel.Order_Main.fieldbyname('MainId').AsString);
          FieldByName('SCSubIdCK').Value:=Trim(frmBPShengChanListSel.Order_Main.fieldbyname('SubId').AsString);
          FieldByName('CKSCorderNo').Value:=Trim(frmBPShengChanListSel.Order_Main.fieldbyname('ConNo').AsString);
        end;
      end;
    end;
  finally
    frmBPShengChanListSel.Free;
  end;
end;

procedure TfrmShaOutPut.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('SPID').AsString)<>'' then
  begin
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from YF_Money_CR where Mainid='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
      Open;
    end;
    FFSPID:=Trim(ADOQueryTemp.fieldbyname('Mainid').AsString);
    if Trim(FFSPID)<>'' then
    begin
      v1CRType.Options.Editing:=False;
    end;
  end;
end;

end.
