unit U_CYPBCKOutPutW;

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;

type
  TfrmCYPBCKOutPutW = 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;
    vC1: TcxGridDBColumn;
    vC2: TcxGridDBColumn;
    vC3: TcxGridDBColumn;
    vC5: TcxGridDBColumn;
    vC6: TcxGridDBColumn;
    vC10: TcxGridDBColumn;
    vC11: TcxGridDBColumn;
    vToFactoryName: TcxGridDBColumn;
    vC12: TcxGridDBColumn;
    vC13: TcxGridDBColumn;
    vC14: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    vCKOrderNo: TcxGridDBColumn;
    vC4: TcxGridDBColumn;
    Label1: TLabel;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: 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 v1Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    function SaveCKData():Boolean;
    procedure InitGridStatus();
  public
    { Public declarations }
    FBCId,FCRType:String;
  end;

var
  frmCYPBCKOutPutW: TfrmCYPBCKOutPutW;

implementation
uses
  U_DataLink,U_RTFun,U_ProductOrderListOneSel,U_ProductOrderMainListOneSel,U_ZDYHelp,U_CYPBCKKCSelList;

{$R *.dfm}

procedure TfrmCYPBCKOutPutW.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;
procedure TfrmCYPBCKOutPutW.InitGridStatus();
begin
  if Trim(FCRType)='销售出库' then
  begin
    vCKOrderNo.Visible:=True;
    vCKOrderNo.Hidden:=false;
    vToFactoryName.Visible:=false;
    vToFactoryName.Hidden:=True;
    vCKOrderNo.Caption:='出到订单号';
  end else
  if Trim(FCRType)='转单出库' then
  begin
    vCKOrderNo.Visible:=True;
    vCKOrderNo.Hidden:=false;
    vToFactoryName.Visible:=False;
    vToFactoryName.Hidden:=True;
    vCKOrderNo.Caption:='转到订单号';
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_SXPB_CR where ZDSPID='''+Trim(CDS_Sub.fieldbyname('SPID').AsString)+'''');
      sql.Add(' and CRQtyFlag=-1');
      Open;
    end;
    if ADOQueryTemp.IsEmpty=False then
    begin
      Tv1.OptionsSelection.CellSelect:=False;
    end;
  end else
  if Trim(FCRType)='半加工出库' then
  begin
    vCKOrderNo.Visible:=True;
    vCKOrderNo.Hidden:=false;
    vToFactoryName.Visible:=True;
    vToFactoryName.Hidden:=False;
    vCKOrderNo.Caption:='出到订单号';
  end else
  if Trim(FCRType)='再加工出库' then
  begin
    vCKOrderNo.Visible:=True;
    vCKOrderNo.Hidden:=false;
    vToFactoryName.Visible:=True;
    vToFactoryName.Hidden:=False;
    vCKOrderNo.Caption:='出到订单号';
  end else
  if Trim(FCRType)='退货出库' then
  begin
    vCKOrderNo.Visible:=false;
    vCKOrderNo.Hidden:=True;
    vToFactoryName.Visible:=True;
    vToFactoryName.Hidden:=False;
    vToFactoryName.Caption:='退给供应商';
    //vToFactoryName.Options.Focusing:=False;
  end;
end;

procedure TfrmCYPBCKOutPutW.TBAddClick(Sender: TObject);
begin
  try
    frmCYPBCKKCSelList:=TfrmCYPBCKKCSelList.Create(Application);
    with frmCYPBCKKCSelList do
    begin
      ToolButton1.Visible:=True;
      if ShowModal=1 then
      begin
        frmCYPBCKKCSelList.CDS_Main.DisableControls;
        with frmCYPBCKKCSelList.CDS_Main do
        begin
          First;
          while frmCYPBCKKCSelList.CDS_Main.Locate('SSel',True,[]) do
          begin
            with Self.CDS_Sub do
            begin
              Append;
              FieldByName('CRTime').Value:=Trim(FormatDateTime('yyyy-MM-dd',Now));
              FieldByName('MainId').Value:=Trim(frmCYPBCKKCSelList.CDS_Main.fieldbyname('MainId').AsString);
              FieldByName('SubId').Value:=Trim(frmCYPBCKKCSelList.CDS_Main.fieldbyname('SubId').AsString);
              FieldByName('FZSPID').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('ZDSPID').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('ZDSPID').Value;
              FieldByName('OrderNo').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('OrderNo').Value;
              FieldByName('PBNo').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('PBNo').Value;
              FieldByName('KHName').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('KHName').Value;
              FieldByName('FromKHName').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('FromKHName').Value;
              FieldByName('FactoryName').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FromFactoryName').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('FromFactoryName').Value;
              FieldByName('FactoryNo').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPCode').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('SPSpec').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SPMF').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('SPStyleNo').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('SPStyleNo').Value;
              FieldByName('QtyUnit').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
              FieldByName('Qty').Value:=frmCYPBCKKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;

            end;
            frmCYPBCKKCSelList.CDS_Main.Delete;
          end;
        end;
        frmCYPBCKKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmCYPBCKKCSelList.Free;
  end;
end;
function TfrmCYPBCKOutPutW.SaveCKData():Boolean;
var
  FCRID,Maxno,FSPID,MaxPYNo,FPYID,FSPNO,FFlag,FPYNO:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCRType)='销售出库' then
    begin
      FFlag:='XS';
    end else
    if Trim(FCRType)='转单出库' then
    begin
      FFlag:='ZC';
    end else
    if Trim(FCRType)='再加工出库' then
    begin
      FFlag:='ZJ';
    end else
    if Trim(FCRType)='半加工出库' then
    begin
      FFlag:='BJ';
    end else
    if Trim(FCRType)='退货出库' then
    begin
      FFlag:='TH';
    end;
    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);
        FSPNO:=Trim(ADOQueryTemp.fieldbyname('SPNO').AsString);
        if Trim(FSPID)='' then
        begin
          if GetLSNo(ADOQueryCmd,Maxno,FFlag,'CK_SXPB_CR',3,1)=False then
          begin
            Result:=False;
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取坯布出库编号失败!','提示',0);
            Exit;
          end;
          FSPNO:='PC'+Trim(Maxno);
        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('SPNO').Value:=Trim(FSPNO);
          FieldByName('FZSPID').Value:=Trim(CDS_Sub.fieldbyname('FZSPID').AsString);
          FieldByName('ZDSPID').Value:=Trim(CDS_Sub.fieldbyname('ZDSPID').AsString);
          FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('MainId').AsString);
          FieldByName('SubId').Value:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
          FieldByName('ORDMainIdCK').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
          FieldByName('ORDSubIdCK').Value:=Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);
          FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
          FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
          FieldByName('ToFactoryNo').Value:=Trim(CDS_Sub.fieldbyname('ToFactoryNo').AsString);
          FieldByName('DeptName').Value:='长阳总仓';
          FieldByName('CRFlag').Value:='出库';
          FieldByName('CRQtyFlag').Value:=-1;
          FieldByName('CRType').Value:=FCRType;
          RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_Sub,'CK_SXPB_CR',2);
          FieldByName('CKName').Value:='坯布仓库';
          if Trim(FCRType)='转单出库' then
          begin
            FieldByName('ZDSPID').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(FCRType)='转单出库' then
        begin
          with ADOQueryTemp do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from CK_SXPB_CR where ZDSPID='''+Trim(Maxno)+'''');
            sql.Add(' and isnull(CRType,'''')=''转单入库'' ');
            Open;
          end;
          FPYID:=Trim(ADOQueryTemp.fieldbyname('SPID').AsString);
          FPYNO:=Trim(ADOQueryTemp.fieldbyname('SPNO').AsString);
          if Trim(FPYID)='' then
          begin
            if GetLSNo(ADOQueryCmd,MaxPYNo,'ZR','CK_SXPB_CR',3,1)=False then
            begin
              Result:=False;
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取成品入库编号失败!','提示',0);
              Exit;
            end;
            FPYNO:='PR'+Trim(MaxPYNo);
          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('ZDSPID').Value:=Trim(Maxno);
            FieldByName('SPNO').Value:=Trim(FPYNO);
            FieldByName('ORDMainIdRK').Value:=Trim(CDS_Sub.fieldbyname('MainId').AsString);
            FieldByName('ORDSubIdRK').Value:=Trim(CDS_Sub.fieldbyname('SubId').AsString);
            FieldByName('MainId').Value:=Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString);
            FieldByName('SubId').Value:=Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString);
            FieldByName('OrderNo').Value:=Trim(CDS_Sub.fieldbyname('ToOrderNo').AsString);
            FieldByName('FromOrderNo').Value:=Trim(CDS_Sub.fieldbyname('OrderNo').AsString);
            FieldByName('PBNO').Value:=Trim(CDS_Sub.fieldbyname('PBNO').AsString);
            FieldByName('FZSPID').Value:=Trim(MaxPYNo);
            FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
            FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('FactoryName').AsString);
            FieldByName('FromFactoryName').Value:=Trim(CDS_Sub.fieldbyname('FromFactoryName').AsString);
            FieldByName('KHName').Value:=Trim(CDS_Sub.fieldbyname('KHName').AsString);
            FieldByName('FromKHName').Value:=Trim(CDS_Sub.fieldbyname('FromKHName').AsString);
            FieldByName('CRTime').Value:=CDS_Sub.fieldbyname('CRTime').Value;
            FieldByName('PiQty').Value:=CDS_Sub.fieldbyname('PiQty').Value;
            FieldByName('Qty').Value:=CDS_Sub.fieldbyname('Qty').Value;
            FieldByName('QtyUnit').Value:=CDS_Sub.fieldbyname('QtyUnit').Value;
            FieldByName('Note').Value:=CDS_Sub.fieldbyname('Note').Value;
            FieldByName('CRFlag').Value:='入库';
            FieldByName('CRQtyFlag').Value:=1;
            FieldByName('CKName').Value:='坯布仓库';
            FieldByName('CRType').Value:='转单入库';
            FieldByName('DeptName').Value:='长阳总仓';
            FieldByName('SPName').Value:=Trim(CDS_Sub.fieldbyname('SPName').AsString);
            FieldByName('SPCode').Value:=Trim(CDS_Sub.fieldbyname('SPCode').AsString);
            FieldByName('SPSpec').Value:=Trim(CDS_Sub.fieldbyname('SPSpec').AsString);
            FieldByName('SPCF').Value:=Trim(CDS_Sub.fieldbyname('SPCF').AsString);
            FieldByName('SPMF').Value:=Trim(CDS_Sub.fieldbyname('SPMF').AsString);
            FieldByName('SPKZ').Value:=Trim(CDS_Sub.fieldbyname('SPKZ').AsString);
            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('SPNo').Value:=Trim(FSPNO);
        Post;
        Next;
      end;
    end;
    CDS_Sub.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;

end;
procedure TfrmCYPBCKOutPutW.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品出库登记',Tv1,'成品仓库');
  Close;
end;

procedure TfrmCYPBCKOutPutW.FormShow(Sender: TObject);
begin
  ReadCxGrid('成品出库登记',Tv1,'成品仓库');
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add(' select A.* ');
    sql.Add(' from CK_SXPB_CR A');
    sql.Add(' where SPID='''+Trim(FBCId)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_Sub);
  SInitCDSData20(ADOQueryTemp,CDS_Sub);
  InitGridStatus();
end;

procedure TfrmCYPBCKOutPutW.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(',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 TfrmCYPBCKOutPutW.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 Trim(FCRType)='销售出库' then
  begin
    if CDS_Sub.Locate('ToOrderNo',null,[])=True then
    begin
      Application.MessageBox('出到订单号不能为空!','提示',0);
      Exit;
    end;
  end;
  if (Trim(FCRType)='再加工出库') or (Trim(FCRType)='半加工出库') then
  begin
    if CDS_Sub.Locate('ToOrderNo',null,[])=True then
    begin
      Application.MessageBox('出到订单号不能为空!','提示',0);
      Exit;
    end;
    if CDS_Sub.Locate('ToFactoryName',null,[])=True then
    begin
      Application.MessageBox('出到加工厂不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(FCRType)='转单出库' then
  begin
    if CDS_Sub.Locate('ToOrderNo',null,[])=True then
    begin
      Application.MessageBox('转到订单号不能为空!','提示',0);
      Exit;
    end;
  end;
  if Trim(FCRType)='退货出库' then
  begin
    if CDS_Sub.Locate('ToFactoryName',null,[])=True then
    begin
      Application.MessageBox('退给供应商不能为空!','提示',0);
      Exit;
    end;
  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 TfrmCYPBCKOutPutW.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  if Trim(FCRType)='销售出库' then
  begin
    try
      frmProductOrderListOneSel:=TfrmProductOrderListOneSel.Create(Application);
      with frmProductOrderListOneSel do
      begin
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ORDMainIdCK').Value:=Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('MainId').AsString);
            FieldByName('ORDSubIdCK').Value:=Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('SubId').AsString);
            FieldByName('ToorderNo').Value:=Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('OrderNo').AsString);
            {FieldByName('ToOrderNoMX').Value:='款号:'+Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('StyleNo').AsString)
                                             +'→花型:'+Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('PRTHX').AsString)
                                             +'→颜色:'+Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('PRTColor').AsString)
                                             +' '+Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('PRTColorNo').AsString)
                                             +' '+Trim(frmProductOrderListOneSel.Order_Main.fieldbyname('PRTColorEng').AsString); }
          end;
        end;
      end;
    finally
      frmProductOrderListOneSel.Free;
    end;
  end else
  begin
    try
      frmProductOrderMainListOneSel:=TfrmProductOrderMainListOneSel.Create(Application);
      with frmProductOrderMainListOneSel do
      begin
        if ShowModal=1 then
        begin
          with Self.CDS_Sub do
          begin
            Edit;
            FieldByName('ORDMainIdCK').Value:=Trim(frmProductOrderMainListOneSel.Order_Main.fieldbyname('MainId').AsString);
            FieldByName('ToOrderNo').Value:=Trim(frmProductOrderMainListOneSel.Order_Main.fieldbyname('OrderNo').AsString);
          end;
        end;
      end;
    finally
      frmProductOrderMainListOneSel.Free;
    end;
  end;

end;

procedure TfrmCYPBCKOutPutW.Tv1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  fsj,FName:string;
begin
  if CDS_Sub.IsEmpty then Exit;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='ToFactoryName' then
  begin
    if Trim(CDS_Sub.fieldbyname('ToOrderNo').AsString)<>'' then
    begin
      fsj:='select Code='''',name=FactoryName from JYOrder_PCS_Sub A '
         +' inner join JYOrder_Process B on A.PSID=B.PSID '
         +' where A.MainId='''+Trim(CDS_Sub.fieldbyname('ORDMainIdCK').AsString)+''''
         +' and isnull(B.PSName,'''') not in(''检验包装'',''成品'',''坯布'') and isnull(A.FactoryName,'''')<>'''''
         +' Group by A.FactoryName';
      SInitCxGridComboBoxBySql(ADOQueryTemp,vToFactoryName,fsj,0,True,'');
      if ADOQueryTemp.IsEmpty then
      begin
        Application.MessageBox('工序没有安排加工厂!','提示',0);
        Exit;
      end;
    end else
    begin
      if Trim(FCRType)<>'退货出库' then Exit;
      (vToFactoryName.Properties as TcxComboBoxProperties).Items.Add(trim(CDS_Sub.fieldbyname('FactoryName').AsString));
      (vToFactoryName.Properties as TcxComboBoxProperties).Items.Add(trim(CDS_Sub.fieldbyname('FromFactoryName').AsString));
    end;
  end;
end;

end.
