unit U_CYBCPCKOutPut;

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
  TfrmCYBCPCKOutPut = 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;
    vC9: TcxGridDBColumn;
    vC7: TcxGridDBColumn;
    vC8: TcxGridDBColumn;
    vC4: TcxGridDBColumn;
    Label1: TLabel;
    vOrderNoMX: TcxGridDBColumn;
    vToPSName: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column2: 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
  frmCYBCPCKOutPut: TfrmCYBCPCKOutPut;

implementation
uses
  U_DataLink,U_RTFun,U_ProductOrderListOneSel,U_ZDYHelp,U_CPCKKCSelList;

{$R *.dfm}

procedure TfrmCYBCPCKOutPut.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  Action:=caFree;
end;
procedure TfrmCYBCPCKOutPut.InitGridStatus();
begin
  if Trim(FCRType)='销售出库' then
  begin
    vCKOrderNo.Visible:=False;
    vCKOrderNo.Hidden:=True;
    vToFactoryName.Visible:=False;
    vToFactoryName.Hidden:=True;
    vOrderNoMX.Visible:=False;
    vOrderNoMX.Hidden:=True;
    vToPSName.Visible:=False;
    vToPSName.Hidden:=True;
  end else
  if Trim(FCRType)='转单出库' then
  begin
    vCKOrderNo.Visible:=True;
    vCKOrderNo.Hidden:=false;
    vToFactoryName.Visible:=False;
    vToFactoryName.Hidden:=True;
    vOrderNoMX.Visible:=True;
    vOrderNoMX.Hidden:=False;
    vToPSName.Visible:=True;
    vToPSName.Hidden:=false;
    vToPSName.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:=False;
    vToFactoryName.Visible:=True;
    vCKOrderNo.Hidden:=True;
    vToFactoryName.Hidden:=False;
    vOrderNoMX.Visible:=False;
    vOrderNoMX.Hidden:=True;
    vToPSName.Visible:=True;
    vToPSName.Hidden:=false;
    vToPSName.Caption:='出到工序';
  end else
  if Trim(FCRType)='退货出库' then
  begin
    vCKOrderNo.Visible:=False;
    vCKOrderNo.Hidden:=True;
    vToFactoryName.Visible:=True;
    vToFactoryName.Hidden:=False;
    vToFactoryName.Caption:='供应商';
    vOrderNoMX.Visible:=False;
    vOrderNoMX.Hidden:=True;
    vToPSName.Visible:=True;
    vToPSName.Hidden:=False;
  end;
end;

procedure TfrmCYBCPCKOutPut.TBAddClick(Sender: TObject);
begin
  try
    frmCPCKKCSelList:=TfrmCPCKKCSelList.Create(Application);
    with frmCPCKKCSelList do
    begin
      ToolButton1.Visible:=True;
      if ShowModal=1 then
      begin
        frmCPCKKCSelList.CDS_Main.DisableControls;
        with frmCPCKKCSelList.CDS_Main do
        begin
          First;
          while frmCPCKKCSelList.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(frmCPCKKCSelList.CDS_Main.fieldbyname('MainId').AsString);
              FieldByName('SubId').Value:=Trim(frmCPCKKCSelList.CDS_Main.fieldbyname('SubId').AsString);
              FieldByName('FZSPID').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPID').Value;
              FieldByName('ZDSPID').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('ZDSPID').Value;
              FieldByName('OrderNo').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('OrderNo').Value;
              FieldByName('FactoryName').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('FactoryName').Value;
              FieldByName('FactoryNo').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('FactoryNo').Value;
              FieldByName('SSType').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SSType').Value;
              FieldByName('SPName').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPName').Value;
              FieldByName('SPCode').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPCode').Value;
              FieldByName('CPID').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('CPID').Value;
              FieldByName('SPSpec').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPSpec').Value;
              FieldByName('SPCF').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPCF').Value;
              FieldByName('SPMF').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPMF').Value;
              FieldByName('SPKZ').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPKZ').Value;
              FieldByName('SPStyleNo').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPStyleNo').Value;
              FieldByName('SPColor').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPColor').Value;
              FieldByName('SPColorNo').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPColorNo').Value;
              FieldByName('SPColorEng').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPColorEng').Value;
              FieldByName('SPHX').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('SPHX').Value;
              FieldByName('QtyUnit').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('QtyUnit').Value;
              FieldByName('PiQty').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('MXKCPiQty').Value;
              FieldByName('Qty').Value:=frmCPCKKCSelList.CDS_Main.fieldbyname('MXKCQty').Value;

            end;
            frmCPCKKCSelList.CDS_Main.Delete;
          end;
        end;
        frmCPCKKCSelList.CDS_Main.EnableControls;

      end;
    end;
  finally
    frmCPCKKCSelList.Free;
  end;
end;
function TfrmCYBCPCKOutPut.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:='CJ';
    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:='CC'+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:='CR'+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('FZSPID').Value:=Trim(MaxPYNo);
            FieldByName('FactoryNo').Value:=Trim(CDS_Sub.fieldbyname('FactoryNo').AsString);
            FieldByName('FactoryName').Value:=Trim(CDS_Sub.fieldbyname('FactoryName').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:='长阳总仓';
            with ADOQueryTemp do
            begin
              Close;
              sql.Clear;
              sql.Add('select * from JYOrder_Main A inner join JYOrder_Sub B on A.Mainid=B.Mainid');
              sql.Add(' where B.SubId='''+Trim(CDS_Sub.fieldbyname('ORDSubIdCK').AsString)+'''');
              Open;
            end;
            FieldByName('OrderNo').Value:=Trim(ADOQueryTemp.fieldbyname('OrderNo').AsString);
            FieldByName('SPName').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTCodeName').AsString);
            FieldByName('SPCode').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTCode').AsString);
            FieldByName('SPSpec').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTSpec').AsString);
            FieldByName('SPCF').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTCF').AsString);
            FieldByName('SPColorNo').Value:=Trim(ADOQueryTemp.fieldbyname('PRTColorNo').AsString);
            FieldByName('SPColor').Value:=Trim(ADOQueryTemp.fieldbyname('PRTColor').AsString);
            FieldByName('SPHX').Value:=Trim(ADOQueryTemp.fieldbyname('PRTHX').AsString);
            FieldByName('SPStyleNo').Value:=Trim(ADOQueryTemp.fieldbyname('StyleNo').AsString);
            FieldByName('SPMF').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTMF').AsString);
            FieldByName('SPKZ').Value:=Trim(ADOQueryTemp.fieldbyname('MPRTKZ').AsString);
            FieldByName('FromOrderNo').Value:=Trim(CDS_Sub.fieldbyname('OrderNo').AsString);
            FieldByName('FromOrderNoMX').Value:='款号:'+Trim(CDS_Sub.fieldbyname('SPStyleNo').AsString)
                                           +'→花型:'+Trim(CDS_Sub.fieldbyname('SPHX').AsString)
                                           +'→颜色:'+Trim(CDS_Sub.fieldbyname('SPColor').AsString)
                                           +' '+Trim(CDS_Sub.fieldbyname('SPColorNo').AsString)
                                           +' '+Trim(CDS_Sub.fieldbyname('SPColorEng').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 TfrmCYBCPCKOutPut.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('成品出库登记',Tv1,'成品仓库');
  Close;
end;

procedure TfrmCYBCPCKOutPut.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 TfrmCYBCPCKOutPut.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 TfrmCYBCPCKOutPut.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('ToFactoryName',null,[])=True then
    begin
      Application.MessageBox('出到加工厂不能为空!','提示',0);
      Exit;
    end;
    if CDS_Sub.Locate('ToPSName',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;
    if CDS_Sub.Locate('ToPSName',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;
    if CDS_Sub.Locate('ToOrderNo',null,[])=True then
    begin
      Application.MessageBox('转到订单号明细不能为空!','提示',0);
      Exit;
    end;
    if CDS_Sub.Locate('ToPSName',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 TfrmCYBCPCKOutPut.v1Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
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;

procedure TfrmCYBCPCKOutPut.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(FCRType)='加工出库' then
  begin
    if Trim(FName)='ToFactoryName' then
    begin
      fsj:='select Code='''',name=FactoryName from JYOrder_PCS_Sub A '
         +' inner join JYOrder_Process B on A.PSID=B.PSID '
         +' where A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').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
    if Trim(FName)='ToPSName' then
    begin
      fsj:='select Code='''',name=PSName from JYOrder_Process A '
         +' where A.MainId='''+Trim(CDS_Sub.fieldbyname('MainId').AsString)+''''
         +' and isnull(A.PSName,'''') not in(''检验包装'',''成品'',''坯布'')'
         +' Group by A.PSName';
      SInitCxGridComboBoxBySql(ADOQueryTemp,vToPSName,fsj,0,True,'');
      if ADOQueryTemp.IsEmpty then
      begin
        Application.MessageBox('工序没有找到加工工序!','提示',0);
        Exit;
      end;
    end;
  end else
  if Trim(FCRType)='退货出库' then
  begin
    if Trim(FName)='ToFactoryName' then
    begin
      fsj:='select Code='''',name=FactoryName from JYOrder_PCS_Sub A '
         +' inner join JYOrder_Process B on A.PSID=B.PSID '
         +' where A.SubId='''+Trim(CDS_Sub.fieldbyname('SubId').AsString)+''''
         +' and isnull(B.PSName,'''')  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
    if Trim(FName)='ToPSName' then
    begin
      fsj:='select Code='''',name=PSName from JYOrder_Process A '
         +' where A.MainId='''+Trim(CDS_Sub.fieldbyname('MainId').AsString)+''''
         +' and isnull(A.PSName,'''')  in(''成品'',''坯布'')'
         +' Group by A.PSName';
      SInitCxGridComboBoxBySql(ADOQueryTemp,vToPSName,fsj,0,True,'');
      if ADOQueryTemp.IsEmpty then
      begin
        Application.MessageBox('工序没有找到采购工序!','提示',0);
        Exit;
      end;
    end;
  end else
  if Trim(FCRType)='转单出库' then
  begin
    if Trim(FName)='ToPSName' then
    begin
      fsj:='select Code='''',name=PSName from JYOrder_Process A '
         +' where A.MainId='''+Trim(CDS_Sub.fieldbyname('ORDMainIDCK').AsString)+''''
         +' Group by A.PSName';
      SInitCxGridComboBoxBySql(ADOQueryTemp,vToPSName,fsj,0,True,'');
      if ADOQueryTemp.IsEmpty then
      begin
        Application.MessageBox('出到订单号没有找到工序!','提示',0);
        Exit;
      end;
    end;
  end;
end;

end.
