unit U_CYPBCKList;

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
  TfrmCYPBCKList = 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;
    PBNO: 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;
    cxTabControl1: TcxTabControl;
    cxTabControl2: TcxTabControl;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn18: TcxGridDBColumn;
    cxGridDBColumn19: TcxGridDBColumn;
    cxGridDBColumn23: TcxGridDBColumn;
    cxGridDBColumn24: TcxGridDBColumn;
    cxGridDBColumn25: TcxGridDBColumn;
    cxGridDBColumn26: TcxGridDBColumn;
    cxGridDBColumn27: TcxGridDBColumn;
    cxGridDBColumn28: TcxGridDBColumn;
    cxGridDBColumn30: TcxGridDBColumn;
    cxGridDBColumn31: TcxGridDBColumn;
    cxGridDBColumn32: TcxGridDBColumn;
    cxGridDBColumn33: TcxGridDBColumn;
    cxGridDBColumn34: TcxGridDBColumn;
    cxGridDBColumn35: TcxGridDBColumn;
    cxGridDBColumn41: TcxGridDBColumn;
    cxGridDBColumn42: TcxGridDBColumn;
    cxGridDBColumn43: TcxGridDBColumn;
    cxGridDBColumn44: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    v4Column1: TcxGridDBColumn;
    v4Column2: TcxGridDBColumn;
    v4Column3: TcxGridDBColumn;
    v4Column4: TcxGridDBColumn;
    v4Column6: TcxGridDBColumn;
    v4Column7: TcxGridDBColumn;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1C1: TcxGridDBColumn;
    v1C2: TcxGridDBColumn;
    v1PBNO: TcxGridDBColumn;
    v1FtyName: TcxGridDBColumn;
    v1FromFtyName: TcxGridDBColumn;
    v1FromKHName: TcxGridDBColumn;
    v1C3: TcxGridDBColumn;
    v1C4: TcxGridDBColumn;
    v1C5: TcxGridDBColumn;
    v1C6: TcxGridDBColumn;
    v1C7: TcxGridDBColumn;
    v1C8: TcxGridDBColumn;
    v1C12: TcxGridDBColumn;
    v1C13: TcxGridDBColumn;
    v1C14: TcxGridDBColumn;
    v1C15: TcxGridDBColumn;
    v1C16: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    v1FromOrderNo: TcxGridDBColumn;
    cxTabControl3: TcxTabControl;
    cxGrid3: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn6: TcxGridDBColumn;
    cxGridDBColumn7: TcxGridDBColumn;
    vFromOrderNo: TcxGridDBColumn;
    cxGridDBColumn9: TcxGridDBColumn;
    vFtyName: TcxGridDBColumn;
    vFromFtyName: TcxGridDBColumn;
    vFromKHName: TcxGridDBColumn;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridDBColumn21: TcxGridDBColumn;
    cxGridDBColumn22: TcxGridDBColumn;
    cxGridDBColumn45: TcxGridDBColumn;
    cxGridDBColumn46: TcxGridDBColumn;
    cxGridDBColumn47: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu3: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Label9: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    vC1: TcxGridDBColumn;
    vC2: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    vC3: TcxGridDBColumn;
    vC4: TcxGridDBColumn;
    vC5: TcxGridDBColumn;
    vC6: TcxGridDBColumn;
    vC10: TcxGridDBColumn;
    vC11: TcxGridDBColumn;
    vC12: TcxGridDBColumn;
    vC13: TcxGridDBColumn;
    vC14: TcxGridDBColumn;
    vToOrderNoZD: TcxGridDBColumn;
    vToFactoryNameJGC: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v2Column1: TcxGridDBColumn;
    v2ToOrderNoCD: TcxGridDBColumn;
    vToFactoryNameGYS: TcxGridDBColumn;
    v4Column8: TcxGridDBColumn;
    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 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
  frmCYPBCKList: TfrmCYPBCKList;

implementation
uses
  U_DataLink,U_RTFun,U_CYPBCKInPut,U_CYPBCKOutPut;

{$R *.dfm}

procedure TfrmCYPBCKList.FormDestroy(Sender: TObject);
begin
  frmCYPBCKList:=nil;
end;

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

procedure TfrmCYPBCKList.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 TfrmCYPBCKList.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=1 then
        begin
          sql.Add(',Case when isnull(A.CRType,'''') in(''销售出库'',''半加工出库'',''再加工出库'') then A.ToOrderNo else Null end as ToOrderNoCD ');
          sql.Add(',Case when isnull(A.CRType,'''')=''转单出库'' then A.ToOrderNo else Null end as ToOrderNoZD ');
          sql.Add(',Case when isnull(A.CRType,'''') in(''半加工出库'',''再加工出库'') then A.ToFactoryName else Null end as ToFactoryNameJGC ');
          sql.Add(',Case when isnull(A.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,'''')=''坯布仓库''');
        sql.Add(' and isnull(DeptName,'''')=''长阳总仓'' ');
        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(' from CK_SXPB_CR A where isnull(CKName,'''')=''坯布仓库'' ');
        SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0');
        sql.Add(' and isnull(DeptName,'''')=''长阳总仓'' ');
        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(A.CRType,'''') in(''销售出库'',''半加工出库'',''再加工出库'') then A.ToOrderNo else Null end as ToOrderNoCD ');
        sql.Add(',Case when isnull(A.CRType,'''')=''转单出库'' then A.ToOrderNo else Null end as ToOrderNoZD ');
        sql.Add(',Case when isnull(A.CRType,'''') in(''半加工出库'',''再加工出库'') then A.ToFactoryName else Null end as ToFactoryNameJGC ');
        sql.Add(',Case when isnull(A.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(' and isnull(DeptName,'''')=''长阳总仓'' ');
        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 TfrmCYPBCKList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

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

procedure TfrmCYPBCKList.FormShow(Sender: TObject);
begin
  ReadcxGrid(Self.Caption+'入库'+Tv1.Name,Tv1,Self.Caption);
  ReadcxGrid(Self.Caption+'出库'+Tv2.Name,Tv2,Self.Caption);
  ReadcxGrid(Self.Caption+'库存'+Tv3.Name,Tv3,Self.Caption);
  ReadcxGrid(Self.Caption+'流水'+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;
end;

procedure TfrmCYPBCKList.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 TfrmCYPBCKList.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 TfrmCYPBCKList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_Main,True);
end;

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

procedure TfrmCYPBCKList.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 TfrmCYPBCKList.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
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=False;
      v1FromFtyName.Hidden:=True;
      v1FromKHName.Visible:=False;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='织造入库' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=False;
      v1FromFtyName.Hidden:=True;
      v1FromKHName.Visible:=False;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='半加工完成入库' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=True;
      v1FromFtyName.Hidden:=false;
      v1FromFtyName.Caption:='加工厂';
      v1FromKHName.Visible:=False;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='半加工未做回仓' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=True;
      v1FromFtyName.Hidden:=false;
      v1FromFtyName.Caption:='加工厂';
      v1FromKHName.Visible:=False;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='再加工未做回仓' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=True;
      v1FromFtyName.Hidden:=false;
      v1FromFtyName.Caption:='加工厂';
      v1FromKHName.Visible:=False;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='客户退回' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=False;
      v1FromFtyName.Hidden:=True;
      v1FromKHName.Visible:=True;
      v1FromKHName.Hidden:=False;
      v1FromOrderNo.Visible:=false;
      v1FromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='转单入库' then
    begin
      v1FtyName.Visible:=True;
      v1FtyName.Hidden:=false;
      v1FromFtyName.Visible:=True;
      v1FromFtyName.Hidden:=false;
      v1FromKHName.Visible:=false;
      v1FromKHName.Hidden:=True;
      v1FromOrderNo.Visible:=True;
      v1FromOrderNo.Hidden:=False;
    end else
    if Trim(FFCRType)='销售出库' then
    begin
      v2ToOrderNoCD.Visible:=True;
      v2ToOrderNoCD.Hidden:=false;
      vToOrderNoZD.Visible:=False;
      vToOrderNoZD.Hidden:=True;
      vToFactoryNameJGC.Visible:=False;
      vToFactoryNameJGC.Hidden:=True;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='转单出库' then
    begin
      v2ToOrderNoCD.Visible:=false;
      v2ToOrderNoCD.Hidden:=True;
      vToOrderNoZD.Visible:=True;
      vToOrderNoZD.Hidden:=False;
      vToFactoryNameJGC.Visible:=False;
      vToFactoryNameJGC.Hidden:=True;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='半加工出库' then
    begin
      v2ToOrderNoCD.Visible:=True;
      v2ToOrderNoCD.Hidden:=false;
      vToOrderNoZD.Visible:=false;
      vToOrderNoZD.Hidden:=True;
      vToFactoryNameJGC.Visible:=True;
      vToFactoryNameJGC.Hidden:=false;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='再加工出库' then
    begin
      v2ToOrderNoCD.Visible:=True;
      v2ToOrderNoCD.Hidden:=false;
      vToOrderNoZD.Visible:=false;
      vToOrderNoZD.Hidden:=True;
      vToFactoryNameJGC.Visible:=True;
      vToFactoryNameJGC.Hidden:=false;
      vToFactoryNameGYS.Visible:=False;
      vToFactoryNameGYS.Hidden:=True;
    end else
    if Trim(FFCRType)='退货出库' then
    begin
      v2ToOrderNoCD.Visible:=false;
      v2ToOrderNoCD.Hidden:=True;
      vToOrderNoZD.Visible:=false;
      vToOrderNoZD.Hidden:=True;
      vToFactoryNameJGC.Visible:=false;
      vToFactoryNameJGC.Hidden:=True;
      vToFactoryNameGYS.Visible:=True;
      vToFactoryNameGYS.Hidden:=false;
    end else
    if Trim(FFCRType)='全部' then
    begin
      if cxPageControl1.ActivePageIndex=0 then
      begin
        v1FtyName.Visible:=True;
        v1FtyName.Hidden:=false;
        v1FromFtyName.Visible:=True;
        v1FromFtyName.Hidden:=false;
        v1FromKHName.Visible:=True;
        v1FromKHName.Hidden:=false;
        v1FromOrderNo.Visible:=True;
        v1FromOrderNo.Hidden:=False;
      end else
      if cxPageControl1.ActivePageIndex=1 then
      begin
        v2ToOrderNoCD.Visible:=True;
        v2ToOrderNoCD.Hidden:=false;
        vToOrderNoZD.Visible:=True;
        vToOrderNoZD.Hidden:=false;
        vToFactoryNameJGC.Visible:=True;
        vToFactoryNameJGC.Hidden:=false;
        vToFactoryNameGYS.Visible:=True;
        vToFactoryNameGYS.Hidden:=false;
      end;
    end;
  end else
  if cxPageControl1.ActivePageIndex=2 then
  begin
    if Trim(FFCRType)='采购入库' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=False;
      vFromFtyName.Hidden:=True;
      vFromKHName.Visible:=False;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='织造入库' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=False;
      vFromFtyName.Hidden:=True;
      vFromKHName.Visible:=False;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='半加工完成入库' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=True;
      vFromFtyName.Hidden:=false;
      vFromFtyName.Caption:='加工厂';
      vFromKHName.Visible:=False;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='半加工未做回仓' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=True;
      vFromFtyName.Hidden:=false;
      vFromFtyName.Caption:='加工厂';
      vFromKHName.Visible:=False;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='再加工未做回仓' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=True;
      vFromFtyName.Hidden:=false;
      vFromFtyName.Caption:='加工厂';
      vFromKHName.Visible:=False;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='客户退回' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=False;
      vFromFtyName.Hidden:=True;
      vFromKHName.Visible:=True;
      vFromKHName.Hidden:=False;
      vFromOrderNo.Visible:=false;
      vFromOrderNo.Hidden:=True;
    end else
    if Trim(FFCRType)='转单入库' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=True;
      vFromFtyName.Hidden:=false;
      vFromKHName.Visible:=false;
      vFromKHName.Hidden:=True;
      vFromOrderNo.Visible:=True;
      vFromOrderNo.Hidden:=False;
    end else
    if Trim(FFCRType)='全部' then
    begin
      vFtyName.Visible:=True;
      vFtyName.Hidden:=false;
      vFromFtyName.Visible:=True;
      vFromFtyName.Hidden:=false;
      vFromKHName.Visible:=True;
      vFromKHName.Hidden:=false;
      vFromOrderNo.Visible:=True;
      vFromOrderNo.Hidden:=False;
    end;
  end;

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

procedure TfrmCYPBCKList.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
      frmCYPBCKInPut:=TfrmCYPBCKInPut.Create(Application);
      with frmCYPBCKInPut do
      begin
        FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
        TBDel.Visible:=False;
        TBAdd.Visible:=False;
        TBCopyAdd.Visible:=False;
        FCRType:=Trim(Self.CDS_Main.fieldbyname('CRType').AsString);
        frmCYPBCKInPut.Caption:='坯布仓库→'+FCRType;
        if ShowModal=1 then
        begin
          Self.InitGrid();
        end;
      end;
    finally
      frmCYPBCKInPut.Free;
    end;
  end else
  if cxPageControl1.ActivePageIndex=1 then
  begin
    try
      frmCYPBCKOutPut:=TfrmCYPBCKOutPut.Create(Application);
      with frmCYPBCKOutPut do
      begin
        FBCId:=Trim(CDS_Main.fieldbyname('SPID').AsString);
        TBDel.Visible:=False;
        TBAdd.Visible:=False;
        FCRType:=Trim(Self.CDS_Main.fieldbyname('CRType').AsString);
        frmCYPBCKOutPut.Caption:='坯布仓库→'+FCRType;
        if ShowModal=1 then
        begin
          Self.InitGrid();
        end;
      end;
    finally
      frmCYPBCKOutPut.Free;
    end;
  end;

end;

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

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

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

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

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

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

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

end.
