unit U_CYCPCKList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridCustomTableView,
  cxGridTableView, cxGridBandedTableView, cxGridDBBandedTableView,
  cxGridLevel, cxClasses, cxControls, cxGridCustomView, cxGridDBTableView,
  cxGrid, StdCtrls, ComCtrls, ExtCtrls, ToolWin, cxGridCustomPopupMenu,
  cxGridPopupMenu, ADODB, DBClient, cxDropDownEdit, cxCheckBox, RM_Common,
  RM_Class, RM_e_Xls, RM_Dataset, RM_System, RM_GridReport, Menus,
  cxCalendar, cxButtonEdit, cxTextEdit, cxPC;

type
  TfrmCYCPCKList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    TBDel: TToolButton;
    TBAdd: TToolButton;
    TBEdit: TToolButton;
    Label3: TLabel;
    Label8: TLabel;
    Label12: TLabel;
    SPName: TEdit;
    SPSpec: TEdit;
    CRType: TComboBox;
    Label6: TLabel;
    SPCF: TEdit;
    Label7: TLabel;
    OrderNo: TEdit;
    Label10: TLabel;
    SPCode: TEdit;
    TBOut: TToolButton;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    Panel2: TPanel;
    Label4: TLabel;
    ComboBox1: TComboBox;
    cxPageControl2: TcxPageControl;
    Panel3: TPanel;
    Label5: TLabel;
    ComboBox2: TComboBox;
    cxPageControl3: TcxPageControl;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column18: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1CRType: TcxGridDBColumn;
    v1FtyGYS: TcxGridDBColumn;
    v1FtyJGC: TcxGridDBColumn;
    v1FtyKH: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    v1FromOrderNo: TcxGridDBColumn;
    v1FromOrderNoMX: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column14: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    cxTabControl1: TcxTabControl;
    cxTabControl2: TcxTabControl;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    vC1: TcxGridDBColumn;
    vC2: TcxGridDBColumn;
    vC3: TcxGridDBColumn;
    vC4: TcxGridDBColumn;
    vC5: TcxGridDBColumn;
    vC6: TcxGridDBColumn;
    vC7: TcxGridDBColumn;
    vC8: TcxGridDBColumn;
    vC9: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    vC10: TcxGridDBColumn;
    vC11: TcxGridDBColumn;
    vC12: TcxGridDBColumn;
    vC13: TcxGridDBColumn;
    vC14: TcxGridDBColumn;
    vCKOrderNo: TcxGridDBColumn;
    vOrderNoMX: TcxGridDBColumn;
    vToFactoryName: TcxGridDBColumn;
    vToPSName: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v2Column2: TcxGridDBColumn;
    vToFactoryNameGYS: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    vCRType: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    vFactoryName: TcxGridDBColumn;
    vFactoryNameJY: TcxGridDBColumn;
    vFactoryNameKH: TcxGridDBColumn;
    vFromOrderNo: TcxGridDBColumn;
    vFromOrderNoMX: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    cxGridDBColumn8: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    cxGridDBColumn10: TcxGridDBColumn;
    cxGridDBColumn11: TcxGridDBColumn;
    cxGridDBColumn12: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxTabControl3: TcxTabControl;
    v2Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn29: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn36: TcxGridDBColumn;
    cxGridDBColumn37: TcxGridDBColumn;
    cxGridDBColumn38: TcxGridDBColumn;
    cxGridDBColumn39: TcxGridDBColumn;
    cxGridDBColumn40: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v4Column1: TcxGridDBColumn;
    v4Column2: TcxGridDBColumn;
    v4Column3: TcxGridDBColumn;
    v4Column4: TcxGridDBColumn;
    v4Column5: TcxGridDBColumn;
    v4Column6: TcxGridDBColumn;
    v4Column7: TcxGridDBColumn;
    Label9: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure TBEditClick(Sender: TObject);
    procedure SPIDKeyPress(Sender: TObject; var Key: Char);
    procedure SPSpecChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure cxPageControl2Change(Sender: TObject);
    procedure TBOutClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure cxPageControl1Change(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
  private
    canshu1,canshu2,canshu3:string;
    procedure InitGrid();
    procedure InitGridStatus();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmCYCPCKList: TfrmCYCPCKList;

implementation
uses
  U_DataLink,U_RTFun,U_CPCKInPut,U_CPCKOutPut;

{$R *.dfm}

procedure TfrmCYCPCKList.FormDestroy(Sender: TObject);
begin
  frmCYCPCKList:=nil;
end;

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

procedure TfrmCYCPCKList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  EndDate.DateTime:=SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime:=EndDate.DateTime-30;
  canshu1:=Trim(DParameters1);
  canshu2:=Trim(DParameters2);
  canshu3:=Trim(DParameters3);
end;

procedure TfrmCYCPCKList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      if cxPageControl1.ActivePageIndex<2 then
      begin
        sql.Add(' select A.* ');
        if cxPageControl1.ActivePageIndex=0 then
        begin
          sql.Add(',case when isnull(CRType,'''')=''采购入库'' then A.FactoryName else Null end as FactoryNameGYS ');
          sql.Add(',case when isnull(CRType,'''')=''他检入库'' then A.FactoryName else Null end as FactoryNameJY ');
          sql.Add(',case when isnull(CRType,'''')=''客户退回'' then A.FactoryName else Null end as FactoryNameKH ');
        end else
        if cxPageControl1.ActivePageIndex=1 then
        begin
          sql.Add(',case when isnull(CRType,'''')=''再加工出库'' then A.ToFactoryName else Null end as ToFactoryNameJGC ');
          sql.Add(',case when isnull(CRType,'''')=''退货出库'' then A.ToFactoryName else Null end as ToFactoryNameGYS ');
        end;
        sql.Add(' from CK_SXPB_CR A');
        sql.add(' where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
        sql.Add(' and isnull(CKName,'''')=''成品仓库''');
        if cxPageControl1.ActivePageIndex=0 then
        begin
          SQL.Add(' and CRFlag=''入库''  ');
          if cxTabControl1.TabIndex<>0 then
            sql.Add(' and isnull(A.CRType,'''')='''+Trim(cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption)+'''');
        end else
        if cxPageControl1.ActivePageIndex=1 then
        begin
          SQL.Add(' and CRFlag=''出库''  ');
          if cxTabControl2.TabIndex<>0 then
            sql.Add(' and isnull(A.CRType,'''')='''+Trim(cxTabControl2.Tabs[cxTabControl2.TabIndex].Caption)+'''');
        end;
        if Trim(canshu1)<>'高权限' then
        begin
          sql.Add(' and A.Filler='''+Trim(DName)+'''');
        end;
        SQL.Add(' order by A.CRTime desc');
      end else
      if cxPageControl1.ActivePageIndex=2 then
      begin
        sql.Add(' select A.* ');
        sql.Add(',case when isnull(CRType,'''')=''采购入库'' then A.FactoryName else Null end as FactoryNameGYS ');
        sql.Add(',case when isnull(CRType,'''')=''他检入库'' then A.FactoryName else Null end as FactoryNameJY ');
        sql.Add(',case when isnull(CRType,'''')=''客户退回'' then A.FactoryName else Null end as FactoryNameKH ');
        sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''成品仓库'' ');
        SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0');
        if cxTabControl3.TabIndex<>0 then
          sql.Add(' and isnull(A.CRType,'''')='''+Trim(cxTabControl3.Tabs[cxTabControl3.TabIndex].Caption)+'''');
        SQL.Add(' order by A.CRTime desc');
      end else
      if cxPageControl1.ActivePageIndex=3 then
      begin
        sql.Add(' select A.* ');
        sql.Add(',case when isnull(CRType,'''')=''采购入库'' then A.FactoryName else Null end as FactoryNameGYS ');
        sql.Add(',case when isnull(CRType,'''')=''他检入库'' then A.FactoryName else Null end as FactoryNameJY ');
        sql.Add(',case when isnull(CRType,'''')=''客户退回'' then A.FactoryName else Null end as FactoryNameKH ');
        sql.Add(',case when isnull(CRType,'''')=''再加工出库'' then A.ToFactoryName else Null end as ToFactoryNameJGC ');
        sql.Add(',case when isnull(CRType,'''')=''退货出库'' then A.ToFactoryName else Null end as ToFactoryNameGYS ');
        sql.Add(',Case when isnull(CRFlag,'''')=''入库'' then PiQty else Null end as PiQtyRK ');
        sql.Add(',Case when isnull(CRFlag,'''')=''出库'' then PiQty else Null end as PiQtyCK ');
        sql.Add(',Case when isnull(CRFlag,'''')=''入库'' then Qty else Null end as QtyRK ');
        sql.Add(',Case when isnull(CRFlag,'''')=''出库'' then Qty else Null end as QtyCK ');
        sql.add(' from CK_SXPB_CR A where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
        sql.Add(' and A.CRTime<='''+Trim(FormatDateTime('yyyy-MM-dd',enddate.DateTime+1))+'''');
        sql.Add(' and isnull(CKName,'''')=''成品仓库''');
        sql.Add(' order by A.CRTime,A.FillTime');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCYCPCKList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

procedure TfrmCYCPCKList.ConNoMChange(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  end;
end;

procedure TfrmCYCPCKList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('入库'+Tv1.Name,Tv1,Self.Caption);
  WriteCxGrid('出库'+Tv2.Name,Tv2,Self.Caption);
  WriteCxGrid('库存'+Tv3.Name,Tv3,Self.Caption);
  WriteCxGrid('流水'+Tv4.Name,Tv4,Self.Caption);
  Close;
end;

procedure TfrmCYCPCKList.FormShow(Sender: TObject);
begin
  ReadcxGrid('入库'+Tv1.Name,Tv1,Self.Caption);
  ReadcxGrid('出库'+Tv2.Name,Tv2,Self.Caption);
  ReadcxGrid('库存'+Tv3.Name,Tv3,Self.Caption);
  ReadcxGrid('流水'+Tv4.Name,Tv4,Self.Caption);
  if Trim(canshu2)='查看' then
  begin
    TBAdd.Visible:=False;
    TBDel.Visible:=False;
    TBEdit.Visible:=False;
    TBOut.Visible:=False;
    Panel2.Visible:=False;
    Panel3.Visible:=False;
  end else
  begin
    TBAdd.Visible:=True;
    TBDel.Visible:=True;
    TBEdit.Visible:=True;
    TBOut.Visible:=True;
    Panel2.Visible:=True;
    Panel3.Visible:=True;
  end;
  cxPageControl1.ActivePageIndex:=0;
  //InitGrid();
end;

procedure TfrmCYCPCKList.TBExportClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if cxPageControl1.ActivePageIndex=0 then
  begin
    TcxGridToExcel(Self.Caption+'入库列表',cxGrid1);
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    TcxGridToExcel(Self.Caption+'出库列表',cxGrid2);
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    TcxGridToExcel(Self.Caption+'库存列表',cxGrid3);
  end else
  if cxPageControl1.ActivePageIndex=3 then
  begin
    TcxGridToExcel(Self.Caption+'出库流水列表',cxGrid4);
  end;
end;

procedure TfrmCYCPCKList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_Main);
    SInitCDSData20(ADOQueryMain,CDS_Main);
  end;
end;

procedure TfrmCYCPCKList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

procedure TfrmCYCPCKList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,False);
end;

procedure TfrmCYCPCKList.TBDelClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;
  if Trim(CDS_Main.fieldbyname('SPID').AsString)<>'' then
  begin
    if cxPageControl1.ActivePageIndex=0 then
    begin
      if Trim(CDS_Main.fieldbyname('CRType').AsString)='转单入库' then
      begin
        Application.MessageBox('转单入库的数据为自动生成，不能删除!','提示',0);
        Exit;
      end;
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from CK_SXPB_CR where FZSPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
        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_Main.fieldbyname('SPID').AsString)+'''');
          ExecSQL;
        end;
        ADOQueryCmd.Connection.CommitTrans;
        CDS_Main.Delete;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('删除异常!','提示',0);
      end;
    end else
    if cxPageControl1.ActivePageIndex=1 then
    begin
      if Trim(CDS_Main.fieldbyname('CRType').AsString)='转单出库' then
      begin
        with ADOQueryTemp do
        begin
          Close;
          sql.Clear;
          sql.Add('select * from CK_SXPB_CR where ZDSPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
          sql.Add(' and isnull(ZDSPID,'''')<>isnull(SPID,'''')');
          sql.Add(' and CRQtyFlag=-1');
          Open;
        end;
        if ADOQueryTemp.IsEmpty=False then
        begin
          Application.MessageBox('转单出库对应的转单入库记录已有出库记录，不能删除!','提示',0);
          Exit;
        end;
      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_Main.fieldbyname('SPID').AsString)+'''');
          if Trim(CDS_Main.fieldbyname('CRType').AsString)='转单出库' then
          begin
            sql.Add(' delete CK_SXPB_CR where ZDSPID='''+Trim(CDS_Main.fieldbyname('SPID').AsString)+'''');
          end;
          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_Main.fieldbyname('FZSPID').AsString)+'''');
          ExecSQL;
        end;
        ADOQueryCmd.Connection.CommitTrans;
        CDS_Main.Delete;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('删除异常!','提示',0);
      end;
    end;


  end else
  begin
    CDS_Main.Delete;
  end;
end;
procedure TfrmCYCPCKList.InitGridStatus();
var
  FFCRType:String;
begin
  if cxPageControl1.ActivePageIndex=0 then
  begin
    FFCRType:=cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    FFCRType:=cxTabControl2.Tabs[cxTabControl2.TabIndex].Caption;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    FFCRType:=cxTabControl3.Tabs[cxTabControl3.TabIndex].Caption;
  end else
  begin
    Exit;
  end;
  if cxPageControl1.ActivePageIndex<2 then
  begin
    if Trim(FFCRType)='自检入库' then
    begin
      v1FtyGYS.Visible:=False;
      v1FtyGYS.Hidden:=True;
      v1FtyJGC.Visible:=False;
      v1FtyJGC.Hidden:=True;
      v1FtyKH.Visible:=False;
      v1FtyKH.Hidden:=True;
      v1FromOrderNo.Visible:=False;
      v1FromOrderNo.Hidden:=True;
      v1FromOrderNoMX.Visible:=False;
      v1FromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='他检入库' then
    begin
      v1FtyGYS.Visible:=False;
      v1FtyGYS.Hidden:=True;
      v1FtyJGC.Visible:=True;
      v1FtyJGC.Hidden:=False;
      v1FtyKH.Visible:=False;
      v1FtyKH.Hidden:=True;
      v1FromOrderNo.Visible:=False;
      v1FromOrderNo.Hidden:=True;
      v1FromOrderNoMX.Visible:=False;
      v1FromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='采购入库' then
    begin
      v1FtyGYS.Visible:=True;
      v1FtyGYS.Hidden:=False;
      v1FtyJGC.Visible:=False;
      v1FtyJGC.Hidden:=True;
      v1FtyKH.Visible:=False;
      v1FtyKH.Hidden:=True;
      v1FromOrderNo.Visible:=False;
      v1FromOrderNo.Hidden:=True;
      v1FromOrderNoMX.Visible:=False;
      v1FromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='客户退回' then
    begin
      v1FtyGYS.Visible:=False;
      v1FtyGYS.Hidden:=True;
      v1FtyJGC.Visible:=False;
      v1FtyJGC.Hidden:=True;
      v1FtyKH.Visible:=True;
      v1FtyKH.Hidden:=False;
      v1FromOrderNo.Visible:=False;
      v1FromOrderNo.Hidden:=True;
      v1FromOrderNoMX.Visible:=False;
      v1FromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='转单入库' then
    begin
      v1FtyGYS.Visible:=False;
      v1FtyGYS.Hidden:=True;
      v1FtyJGC.Visible:=False;
      v1FtyJGC.Hidden:=True;
      v1FtyKH.Visible:=False;
      v1FtyKH.Hidden:=True;
      v1FromOrderNo.Visible:=True;
      v1FromOrderNo.Hidden:=False;
      v1FromOrderNoMX.Visible:=True;
      v1FromOrderNoMX.Hidden:=False;
    end else
    if Trim(FFCRType)='销售出库' 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;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='转单出库' 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;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='再加工出库' then
    begin
      vCKOrderNo.Visible:=False;
      vCKOrderNo.Hidden:=True;
      vToFactoryName.Visible:=True;
      vToFactoryName.Hidden:=False;
      vOrderNoMX.Visible:=False;
      vOrderNoMX.Hidden:=True;
      vToPSName.Visible:=True;
      vToPSName.Hidden:=false;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='退货出库' then
    begin
      vCKOrderNo.Visible:=False;
      vCKOrderNo.Hidden:=True;
      vToFactoryName.Visible:=false;
      vToFactoryName.Hidden:=True;
      vOrderNoMX.Visible:=False;
      vOrderNoMX.Hidden:=True;
      vToPSName.Visible:=True;
      vToPSName.Hidden:=False;
      vToFactoryNameGYS.Visible:=True;
      vToFactoryNameGYS.Hidden:=false;
    end else
    if Trim(FFCRType)='全部' then
    begin
      if cxPageControl1.ActivePageIndex=0 then
      begin
        v1FtyGYS.Visible:=True;
        v1FtyGYS.Hidden:=False;
        v1FtyJGC.Visible:=True;
        v1FtyJGC.Hidden:=False;
        v1FtyKH.Visible:=True;
        v1FtyKH.Hidden:=False;
        v1FromOrderNo.Visible:=True;
        v1FromOrderNo.Hidden:=False;
        v1FromOrderNoMX.Visible:=True;
        v1FromOrderNoMX.Hidden:=False;
      end else
      if cxPageControl1.ActivePageIndex=1 then
      begin
        vCKOrderNo.Visible:=True;
        vCKOrderNo.Hidden:=false;
        vToFactoryName.Visible:=True;
        vToFactoryName.Hidden:=False;
        vOrderNoMX.Visible:=True;
        vOrderNoMX.Hidden:=False;
        vToPSName.Visible:=True;
        vToPSName.Hidden:=False;
        vToFactoryNameGYS.Visible:=True;
        vToFactoryNameGYS.Hidden:=false;
      end;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if Trim(FFCRType)='自检入库' then
    begin
      vFactoryName.Visible:=False;
      vFactoryName.Hidden:=True;
      vFactoryNameJY.Visible:=False;
      vFactoryNameJY.Hidden:=True;
      vFactoryNameKH.Visible:=False;
      vFactoryNameKH.Hidden:=True;
      vFromOrderNo.Visible:=False;
      vFromOrderNo.Hidden:=True;
      vFromOrderNoMX.Visible:=False;
      vFromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='他检入库' then
    begin
      vFactoryName.Visible:=False;
      vFactoryName.Hidden:=True;
      vFactoryNameJY.Visible:=True;
      vFactoryNameJY.Hidden:=False;
      vFactoryNameKH.Visible:=False;
      vFactoryNameKH.Hidden:=True;
      vFromOrderNo.Visible:=False;
      vFromOrderNo.Hidden:=True;
      vFromOrderNoMX.Visible:=False;
      vFromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='采购入库' then
    begin
      vFactoryName.Visible:=True;
      vFactoryName.Hidden:=False;
      vFactoryNameJY.Visible:=False;
      vFactoryNameJY.Hidden:=True;
      vFactoryNameKH.Visible:=False;
      vFactoryNameKH.Hidden:=True;
      vFromOrderNo.Visible:=False;
      vFromOrderNo.Hidden:=True;
      vFromOrderNoMX.Visible:=False;
      vFromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='客户退回' then
    begin
      vFactoryName.Visible:=False;
      vFactoryName.Hidden:=True;
      vFactoryNameJY.Visible:=False;
      vFactoryNameJY.Hidden:=True;
      vFactoryNameKH.Visible:=True;
      vFactoryNameKH.Hidden:=False;
      vFromOrderNo.Visible:=False;
      vFromOrderNo.Hidden:=True;
      vFromOrderNoMX.Visible:=False;
      vFromOrderNoMX.Hidden:=True;
    end else
    if Trim(FFCRType)='转单入库' then
    begin
      vFactoryName.Visible:=False;
      vFactoryName.Hidden:=True;
      vFactoryNameJY.Visible:=False;
      vFactoryNameJY.Hidden:=True;
      vFactoryNameKH.Visible:=False;
      vFactoryNameKH.Hidden:=True;
      vFromOrderNo.Visible:=True;
      vFromOrderNo.Hidden:=False;
      vFromOrderNoMX.Visible:=True;
      vFromOrderNoMX.Hidden:=False;
    end else
    if Trim(FFCRType)='全部' then
    begin
      vFactoryName.Visible:=True;
      vFactoryName.Hidden:=False;
      vFactoryNameJY.Visible:=True;
      vFactoryNameJY.Hidden:=False;
      vFactoryNameKH.Visible:=True;
      vFactoryNameKH.Hidden:=False;
      vFromOrderNo.Visible:=True;
      vFromOrderNo.Hidden:=False;
      vFromOrderNoMX.Visible:=True;
      vFromOrderNoMX.Hidden:=False;
    end;
  end;

end;
procedure TfrmCYCPCKList.TBAddClick(Sender: TObject);
begin
  if Trim(ComboBox1.Text)='' then
  begin
    Application.MessageBox('入库类型不能为空!','提示',0);
    Exit;
  end;
  try
    frmCPCKInPut:=TfrmCPCKInPut.Create(Application);
    with frmCPCKInPut do
    begin
      FBCId:='';
      FCRType:=Trim(Self.ComboBox1.Text);
      frmCPCKInPut.Caption:='成品仓库→'+Trim(Self.ComboBox1.Text);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPCKInPut.Free;
  end;
end;

procedure TfrmCYCPCKList.TBEditClick(Sender: TObject);
begin
  if CDS_Main.IsEmpty then Exit;

  if cxPageControl1.ActivePageIndex=0 then
  begin
    if Trim(CDS_Main.fieldbyname('CRType').AsString)='转单入库' then
    begin
      Application.MessageBox('转单入库的数据为自动生成，不能修改!','提示',0);
      Exit;
    end;
    try
      frmCPCKInPut:=TfrmCPCKInPut.Create(Application);
      with frmCPCKInPut do
      begin
        FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
        TBDel.Visible:=False;
        ToolButton1.Visible:=False;
        FCRType:=Trim(Self.CDS_Main.fieldbyname('CRType').AsString);
        frmCPCKInPut.Caption:='成品仓库→'+FCRType;
        if ShowModal=1 then
        begin
          Self.InitGrid();
        end;
      end;
    finally
      frmCPCKInPut.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      frmCPCKOutPut:=TfrmCPCKOutPut.Create(Application);
      with frmCPCKOutPut do
      begin
        FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
        TBDel.Visible:=False;
        TBAdd.Visible:=False;
        FCRType:=Trim(Self.CDS_Main.fieldbyname('CRType').AsString);
        frmCPCKOutPut.Caption:='成品仓库→'+FCRType;
        if ShowModal=1 then
        begin
          Self.InitGrid();
        end;
      end;
    finally
      frmCPCKOutPut.Free;
    end;
  end;

end;

procedure TfrmCYCPCKList.SPIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    {if Length(Trim(SPID.Text))<4 then Exit;
    try
      ADOQueryMain.DisableControls;
      with ADOQueryMain do
      begin
        Filtered:=False;
        Close;
        sql.Clear;
        sql.Add(' select A.* ');
        sql.Add(',ConNo=(select ConNo from Contract_Main CM where CM.MainId=A.CGMainId)');
        //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 like :SXID');
        Parameters.ParamByName('SXID').Value:='%'+Trim(SXID.Text)+'%';
        Open;
        //ShowMessage(SQL.Text);
      end;
      SCreateCDS20(ADOQueryMain,CDS_Main);
      SInitCDSData20(ADOQueryMain,CDS_Main);
    finally
      ADOQueryMain.EnableControls;
    end;  }
  end;
end;

procedure TfrmCYCPCKList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCYCPCKList.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCYCPCKList.cxPageControl2Change(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmCYCPCKList.TBOutClick(Sender: TObject);
begin
  if Trim(ComboBox2.Text)='' then
  begin
    Application.MessageBox('出库类型不能为空!','提示',0);
    Exit;
  end;
  try
    frmCPCKOutPut:=TfrmCPCKOutPut.Create(Application);
    with frmCPCKOutPut do
    begin
      FBCId:='';
      FCRType:=Trim(Self.ComboBox2.Text);
      frmCPCKOutPut.Caption:='成品仓库→'+Trim(Self.ComboBox2.Text);
      if ShowModal=1 then
      begin
        Self.InitGrid();
      end;
    end;
  finally
    frmCPCKOutPut.Free;
  end;
end;

procedure TfrmCYCPCKList.cxTabControl1Change(Sender: TObject);
begin
  InitGridStatus();
  InitGrid();
end;

procedure TfrmCYCPCKList.cxPageControl1Change(Sender: TObject);
begin
  InitGridStatus();
  InitGrid();
end;

procedure TfrmCYCPCKList.cxTabControl2Change(Sender: TObject);
begin
  InitGridStatus();
  InitGrid();
end;

end.
