unit U_ZHCPViewList;

interface

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

type
  TfrmZHCPViewList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    ToolButton1: TToolButton;
    Panel11: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    Panel2: TPanel;
    cxTabControl2: TcxTabControl;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    v1Column13: TcxGridDBColumn;
    Panel4: TPanel;
    Label9: TLabel;
    YeHZ: TEdit;
    Label10: TLabel;
    CPHZ: TEdit;
    Label11: TLabel;
    GGHZ: TEdit;
    Label12: TLabel;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label1: TLabel;
    Label5: TLabel;
    ZIID: TEdit;
    ZIName: TEdit;
    ZIGY: TEdit;
    Editer: TEdit;
    Filler: TEdit;
    Note: TEdit;
    PopupMenu1: TPopupMenu;
    KWView: TMenuItem;
    LabKWName: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure ZIIDKeyPress(Sender: TObject; var Key: Char);
    procedure KWViewClick(Sender: TObject);
  private
    canshu1:string;
    DQdate:TDateTime;
    procedure InitGrid();
    procedure InitForm();
    procedure IntTab();
    function GetFiltersGY(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
    function StrFenHang(FStr:string;var FHStr:String):Boolean;
    { Private declarations }
  public
    FFInt,FCloth:Integer;
    FKWID:String;

    { Public declarations }
  end;

var
  frmZHCPViewList:TfrmZHCPViewList;

implementation
uses
  U_DataLink,U_RTFun,U_ZHCPInPut,U_KWView,U_CPInPutDML;

{$R *.dfm}

procedure TfrmZHCPViewList.FormDestroy(Sender: TObject);
begin
  frmZHCPViewList:=nil;
end;

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

procedure TfrmZHCPViewList.FormCreate(Sender: TObject);
begin
  cxgrid1.Align:=alClient;
  canshu1:=Trim(DParameters1);
end;

procedure TfrmZHCPViewList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('挂钩库存查询MX',Tv1,'展会产品管理');
  Close;
end;
function TfrmZHCPViewList.GetFiltersGY(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
var
  i,j,k:Integer;
  fsj,fsj1:String;
begin
  Result:='';
  with TMPanel do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if Controls[i] is TLabel then Continue;
      if Controls[i] is TEdit then
      begin
        if Trim(TEdit(Controls[i]).Text)<>'' then
        begin
          if Trim(TEdit(Controls[i]).Name)='ZIID' then
          begin
            if Copy(Trim(TEdit(Controls[i]).Text),1,2)='DY' then
            begin
              with ADOQueryTemp do
              begin
                Close;
                sql.Clear;
                sql.Add('select * from ZH_CP_Info_KC where DYID='''+Trim(TEdit(Controls[i]).Text)+'''');
                Open;
              end;
              TEdit(Controls[i]).Text:=Trim(ADOQueryTemp.fieldbyname('ZIID').AsString);
              Controls[i].Tag:=1;
            end else
            begin
              Controls[i].Tag:=2;
            end;
          end;
          if Controls[i].Tag=EquTag then
            Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+'='+QuotedStr(Trim(TEdit(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
          begin
            j:=Pos(' ',Trim(TEdit(Controls[i]).Text));
            if j>0 then
            begin
              Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+' like '+QuotedStr('%'+Copy(Trim(TEdit(Controls[i]).Text),1,j-1 )+'%');
              fsj1:=Copy(Trim(TEdit(Controls[i]).Text),j+1,Length(Trim(TEdit(Controls[i]).Text)));
              while Trim(fsj1)<>'' do
              begin
                j:=Pos(' ',Trim(fsj1));
                if j>0 then
                begin
                  Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+' like '+QuotedStr('%'+Copy(Trim(fsj1),1,j-1 )+'%');
                  fsj1:=Copy(Trim(fsj1),j+1,Length(Trim(fsj1)));
                end else
                begin
                  Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+' like '+QuotedStr('%'+Trim(fsj1)+'%');
                  fsj1:='';
                end;
              end;
            end else
              Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+' like '+QuotedStr('%'+Trim(TEdit(Controls[i]).Text)+'%');
          end;
        end;
      end else
      if Controls[i] is TComboBox then
      begin
        if Trim(TComboBox(Controls[i]).Text)<>'' then
          if Controls[i].Tag=EquTag then
            Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+'='+QuotedStr(Trim(TComboBox(Controls[i]).Text))
          else if Controls[i].Tag=LikeTag then
            Result:=Result+'and A.'+Controls[i].Hint+Controls[i].Name+' like '+QuotedStr('%'+Trim(TComboBox(Controls[i]).Text)+'%');
      end;
    end;
  end;
  if Trim(Result)<>'' then
    Result:=Trim(RightBStr(Result,Length(Result)-4));
end;
procedure TfrmZHCPViewList.InitGrid();
var
  fsj10:String;
begin
  fsj10:=GetFiltersGY(Panel1,1,2);
  if Trim(fsj10)<>'' then
  begin
    fsj10:=' and '+fsj10;
  end else
  begin
    fsj10:='';
  end;
  Panel2.Visible:=True;
  Panel2.Refresh;
  cxTabControl2.Enabled:=False;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      sql.Clear;
      sql.Add('Select AA.* From(');
      sql.Add(' select A.*,Cast(ROW_NUMBER() OVER(order by A.ZIID) as int) as RowNum ');
      sql.Add(' ,ImageFileTP=(select ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
      sql.Add('             where B.WBID=A.ZIID and B.IMNO=''图片'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
      sql.Add(' ,ImageFileNR=(select ImageFile from Image_Info B inner join Image_File C on B.IMID=C.IMID ');
      sql.Add('             where B.WBID=A.ZIID and B.IMNO=''内容'' and B.Valid=''Y'' and C.Valid=''Y'' and C.IFType=''小'' )');
      sql.Add(',GS=KC.GGQty');
      sql.Add('From(');
      sql.Add(' select ZIID,GGQty=sum(GGQty)  from ZH_CP_Info_KC where isnull(GGQty,0)>0 and KWID='''+Trim(FKWID)+'''');
      sql.Add(' and Valid=''Y'' Group by ZIID) KC');
      sql.Add(' inner join ZH_CP_Info A  on KC.ZIID=A.ZIID');
      sql.Add(' where A.Valid=''Y''  ');
      sql.Add(fsj10);
      sql.Add(')AA');
      sql.Add(' where RowNum>='+inttostr(cxTabControl2.TabIndex*30+1));
      sql.Add(' and RowNum<='+inttostr((cxTabControl2.TabIndex+1)*30));

      Open;
    end;
    SCreateCDS20(ADOQueryMain,Order_Main);
    SInitCDSData20(ADOQueryMain,Order_Main);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select AA=count(*) ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and KWID='''+Trim(FKWID)+'''');
      SQL.Add(' and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
      sql.Add(fsj10);
      Open;
    end;
    CPHZ.Text:=Trim(ADOQueryTemp.fieldbyname('AA').AsString);
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add(' select AA=sum(GGQty) ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' inner join ZH_CP_Info_KC KC on A.ZIID=KC.ZIID');
      sql.Add(' where KC.KWID='''+Trim(FKWID)+''' and A.Valid=''Y'' and KC.Valid=''Y''   ');
      sql.Add(fsj10);
      Open;
    end;
    GGHZ.Text:=Trim(ADOQueryTemp.fieldbyname('AA').AsString);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible:=False;
  cxTabControl2.Enabled:=True;
end;
procedure TfrmZHCPViewList.InitForm();
begin
  ReadCxGrid('挂钩库存查询MX',Tv1,'展会产品管理');
  TBRafresh.Click;
end;

procedure TfrmZHCPViewList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;


function TfrmZHCPViewList.StrFenHang(FStr:string;var FHStr:String):Boolean;
var
  i,j:Integer;
  LSStr:String;
begin
  Result:=False;
  FStr:=Trim(FStr);
  FHStr:='';
  j:=1;
  i:=Pos(' ',FStr);
  while i>0 do
  begin
    if j=1 then
    begin
      LSStr:=Copy(FStr,1,i-1);  //'1234  5678 456'
      FHStr:='1:'+LSStr;
    end else
    begin
      LSStr:=Copy(FStr,1,i-1);
      if Trim(LSStr)='' then
      begin
        Fstr:=Copy(Fstr,i+1,Length(Fstr)-i);
      end else
        FHStr:=FHStr+#13+#10+inttostr(j)+':'+LSStr;
    end;
    if Trim(LSStr)<>'' then
    begin
      j:=j+1;
      Fstr:=Copy(Fstr,i+1,Length(Fstr)-i);
    end;
    i:=Pos(' ',FStr);
  end;
  if j=1 then
  begin
    FHStr:=FStr;
  end else
  begin
    if Trim(FStr)<>'' then
    begin
      FHStr:=FHStr+#13+#10+inttostr(j)+':'+FStr;
    end;
  end;
  Result:=True;
end;
procedure TfrmZHCPViewList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  TcxGridToExcel(Self.Caption,cxGrid1);
end;

procedure TfrmZHCPViewList.TBRafreshClick(Sender: TObject);
begin
  IntTab();
  InitGrid();
end;
procedure TfrmZHCPViewList.IntTab();
var
  JLInt,YeInt,i:Integer;
  fsj,fsj10:String;
begin
  fsj10:=GetFiltersGY(Panel1,1,2);
  if Trim(fsj10)<>'' then
  begin
    fsj10:=' and '+fsj10;
  end else
  begin
    fsj10:='';
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select JLInt=isnull(Count(*),0)  ');
    sql.Add(' from ZH_CP_Info A  ');
    sql.Add(' where Valid=''Y''  ');
    sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 ');
    sql.Add(' and KWID='''+Trim(FKWID)+''' and KC.Valid=''Y'' )');
    sql.Add(fsj10);
    Open;
  end;
  JLInt:=ADOQueryTemp.fieldbyname('JLInt').AsInteger;
  YeInt:=JLInt div 30;
  YeInt:=YeInt+1;
  cxTabControl2.Tabs.Clear;
  for i:=1 to YeInt do
  begin
    fsj:='第'+IntToStr(i)+'页';
    cxTabControl2.Tabs.Add(fsj);
  end;
  YeHZ.Text:=IntToStr(YeInt);
end;

procedure TfrmZHCPViewList.FormShow(Sender: TObject);
begin
  InitForm();
end;

procedure TfrmZHCPViewList.CheckBox1Click(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmZHCPViewList.CheckBox2Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmZHCPViewList.ToolButton1Click(Sender: TObject);

begin
  if Order_Main.IsEmpty then Exit;
  if Trim(Order_Main.fieldbyname('ZIType').AsString)='成品' then
  begin
    try
      frmZHCPInPut:=TfrmZHCPInPut.Create(Application);
      with frmZHCPInPut do
      begin
        PState:=1;
        FMainId:=Trim(Order_Main.fieldbyname('ZIID').AsString);
        TBSave.Visible:=False;
        if ShowModal=1 then
        begin
          //InitGrid();
        end;
      end;
    finally
      frmZHCPInPut.Free;
    end;
  end else
  if Trim(Order_Main.fieldbyname('ZIType').AsString)='单面料' then
  begin
    try
      frmCPInPutDML:=TfrmCPInPutDML.Create(Application);
      with frmCPInPutDML do
      begin
        PState:=1;
        FMainId:=Trim(Order_Main.fieldbyname('ZIID').AsString);
        TBSave.Visible:=False;
        if ShowModal=1 then
        begin
          //InitGrid();
        end;
      end;
    finally
      frmCPInPutDML.Free;
    end;
  end;

end;

procedure TfrmZHCPViewList.CustomerNoNameChange(Sender: TObject);
begin
  if ADOQueryMain.Active=False then Exit;
  SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
  SCreateCDS20(ADOQueryMain,Order_Main);
  SInitCDSData20(ADOQueryMain,Order_Main);
end;

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

procedure TfrmZHCPViewList.ZJStatusChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmZHCPViewList.Tv1DblClick(Sender: TObject);
begin
  ToolButton1.Click;
end;

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

procedure TfrmZHCPViewList.ZIIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key=#13 then
  begin
    IntTab();
    InitGrid();
    if Trim(TEdit(Sender).Name)='ZIID' then
    begin
      ZIID.Text:='';
    end;
  end;
end;

procedure TfrmZHCPViewList.KWViewClick(Sender: TObject);
begin
  try
    frmKWView:=TfrmKWView.Create(Application);
    with frmKWView do
    begin
      FZIID:=Trim(Self.Order_Main.fieldbyname('ZIID').AsString);
      FFKWID:=Self.FKWID;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmKWView.Free;
  end;
end;

end.
