unit U_ZHCPList;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu, dxSkinsCore,
  dxSkinDarkRoom, dxSkinOffice2013White, dxSkinSharpPlus, dxSkinSpringTime,
  dxSkinsDefaultPainters, dxSkinscxPCPainter, dxSkinBlack, dxSkinBlue,
  dxSkinBlueprint, dxSkinCaramel, dxSkinCoffee, dxSkinDarkSide,
  dxSkinDevExpressDarkStyle, dxSkinDevExpressStyle, dxSkinFoggy,
  dxSkinGlassOceans, dxSkinHighContrast, dxSkiniMaginary, dxSkinLilian,
  dxSkinLiquidSky, dxSkinLondonLiquidSky, dxSkinMcSkin, dxSkinMetropolis,
  dxSkinMetropolisDark, dxSkinMoneyTwins, dxSkinOffice2007Black,
  dxSkinOffice2007Blue, dxSkinOffice2007Green, dxSkinOffice2007Pink,
  dxSkinOffice2007Silver, dxSkinOffice2010Black, dxSkinOffice2010Blue,
  dxSkinOffice2010Silver, dxSkinOffice2013DarkGray, dxSkinOffice2013LightGray,
  dxSkinPumpkin, dxSkinSeven, dxSkinSevenClassic, dxSkinSharp, dxSkinSilver,
  dxSkinStardust, dxSkinSummer2008, dxSkinTheAsphaltWorld, dxSkinValentine,
  dxSkinVS2010, dxSkinWhiteprint, dxSkinXmas2008Blue;

type
  TfrmZHCPList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Tv1: TcxGridDBTableView;
    cxGrid1Level1: TcxGridLevel;
    cxGrid1: TcxGrid;
    cxGridPopupMenu1: TcxGridPopupMenu;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    TBExport: TToolButton;
    Order_Main: TClientDataSet;
    TbLook: TToolButton;
    Panel11: TPanel;
    v1Column1: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    ToolButton4: TToolButton;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport2: TRMXLSExport;
    CDS_PRT: TClientDataSet;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1ZICost: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    TBEdit: TMenuItem;
    TBDel: TMenuItem;
    Panel2: TPanel;
    cxTabControl2: TcxTabControl;
    v1Column10: TcxGridDBColumn;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    Panel3: TPanel;
    CheckBox1: TCheckBox;
    v1Column13: TcxGridDBColumn;
    Panel4: TPanel;
    Label9: TLabel;
    YeHZ: TEdit;
    Label10: TLabel;
    CPHZ: TEdit;
    Label11: TLabel;
    GGHZ: TEdit;
    Label12: TLabel;
    KWView: TMenuItem;
    Panel5: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    Panel1: TPanel;
    Label2: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    Label1: TLabel;
    Label5: TLabel;
    ZIName: TEdit;
    ZIGY: TEdit;
    Editer: TEdit;
    Filler: TEdit;
    Note: TEdit;
    v1Column14: TcxGridDBColumn;
    Label13: TLabel;
    KHNameHZ: TEdit;
    Panel6: TPanel;
    SpeedButton1: TSpeedButton;
    Image2: TImage;
    SpeedButton2: TSpeedButton;
    Label14: TLabel;
    ZIType: TComboBox;
    v1Column15: TcxGridDBColumn;
    Panel7: TPanel;
    SpeedButton3: TSpeedButton;
    Image1: TImage;
    SpeedButton4: TSpeedButton;
    RM2: TRMGridReport;
    SpeedButton5: TSpeedButton;
    Panel8: TPanel;
    Label15: TLabel;
    ComboBox1: TComboBox;
    v1Column16: TcxGridDBColumn;
    SpeedButton6: TSpeedButton;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Label16: TLabel;
    HXNO: TEdit;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Tv1Column8: TcxGridDBColumn;
    ZIID: TEdit;
    Label17: TLabel;
    Label18: TLabel;
    MianBuName: TEdit;
    MianBu: TEdit;
    Label19: TLabel;
    Label20: TLabel;
    ZhongJianCName: TEdit;
    ZhongJianC: TEdit;
    Label21: TLabel;
    Label22: TLabel;
    DiBuName: TEdit;
    DiBu: TEdit;
    SpeedButton7: TSpeedButton;
    Tv1Column9: TcxGridDBColumn;
    TbCopy: TToolButton;
    LabelType: TComboBox;
    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 TBAddClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure TbLookClick(Sender: TObject);
    procedure CustomerNoNameChange(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ZJStatusChange(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure v1Column2PropertiesEditValueChanged(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
    procedure ZIIDKeyPress(Sender: TObject; var Key: Char);
    procedure Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TBEditClick(Sender: TObject);
    procedure KWViewClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure ZITypeChange(Sender: TObject);
    procedure Image1Click(Sender: TObject);
    procedure SpeedButton4Click(Sender: TObject);
    procedure SpeedButton3Click(Sender: TObject);
    procedure SpeedButton5Click(Sender: TObject);
    procedure SpeedButton7Click(Sender: TObject);
    procedure TbCopyClick(Sender: TObject);
  private
    canshu1: string;
    DQdate: TDateTime;
    procedure InitGrid();
    procedure InitForm();
    function DelData(): Boolean;
    procedure IntTab();
    procedure IntTab1();
    function GetFiltersGY(TMPanel: TPanel; EquTag, LikeTag: Integer): string;
    function StrFenHang(FStr: string; var FHStr: string): Boolean;
    { Private declarations }
  public
    FFInt, FCloth: Integer;

    { Public declarations }
  end;

//var
  //frmZHCPList: TfrmZHCPList;

implementation

uses
  U_DataLink, U_RTFun, U_ZHCPInPut, U_KWView;

{$R *.dfm}
procedure TfrmZHCPList.IntTab1();
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;
    if Trim(canshu1) = '库存' then
    begin
      sql.Add('select JLInt=isnull(Count(*),0)  ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      //sql.Add(' and ZIType=''成品'' ');
      sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
    end
    else
    begin
      sql.Add(' select JLInt=isnull(Count(*),0) ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      //sql.Add(' and ZIType=''成品'' ');
//      sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//      sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
    end;
    sql.Add(fsj10);
//    ShowMessage(SQL.text);
    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 TfrmZHCPList.FormDestroy(Sender: TObject);
begin
  //frmZHCPList:=nil;
end;

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

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

procedure TfrmZHCPList.TBCloseClick(Sender: TObject);
begin
  if Trim(Self.Caption) = '公司产品录入(高)*' then
  begin
    WriteCxGrid('公司产品录入(高)', Tv1, '公司产品管理');
  end
  else if Trim(Self.Caption) = '公司产品录入*' then
  begin
    WriteCxGrid('公司产品录入', Tv1, '公司产品管理');
  end
  else if Trim(Self.Caption) = '公司产品查询*' then
  begin
    WriteCxGrid('公司产品查询', Tv1, '公司产品管理');
  end
  else if Trim(Self.Caption) = '挂钩库存查询*' then
  begin
    WriteCxGrid('挂钩库存查询', Tv1, '公司产品管理');
  end;
  Close;
end;

function TfrmZHCPList.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 TfrmZHCPList.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;
      if Self.canshu1 = '库存' then
      begin
        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 Top 1 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 Top 1 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 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''  ');
        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
        sql.Add(')AA');
        sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
        sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
      end
      else
      begin
        sql.Add('Select AA.* From(');
        sql.Add(' select A.*,Cast(ROW_NUMBER() OVER(order by ZIID) as int) as RowNum ');
        sql.Add(' ,ImageFileTP=(select top 1 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 top 1 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=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
        sql.Add(' from ZH_CP_Info A  ');
        sql.Add(' where Valid=''Y''  ');
        sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
        sql.Add(')AA');
        sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
        sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
      end;
//      ShowMessage(SQL.text);
      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''  ');
      if Trim(canshu1) <> '库存' then
      begin
        sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end
      else
      begin
        sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
      end;
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
      sql.Add(fsj10);
//      ShowMessage(sql.text);
      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 A.Valid=''Y'' and KC.Valid=''Y''  ');
      if Trim(canshu1) <> '库存' then
      begin
        sql.Add(' and A.ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
        sql.Add(' and A.ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
      sql.Add(fsj10);
      Open;
    end;
    GGHZ.Text := Trim(ADOQueryTemp.fieldbyname('AA').AsString);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
  cxTabControl2.Enabled := True;

  /////////查询框////////////
//  with ADOQueryCmd do
//  begin
//    Close;
//    sql.Clear;
//    sql.Add('select distinct(ZIID) AS name from ZH_CP_Info  ');
//    sql.Add(' where ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//    sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
//    Open;
//  end;
//  ZIID.Items.Clear; //清空
//  ZIID.Items.Add(Trim(''));
//  while not ADOQueryCmd.eof do
//  begin
//    ZIID.Items.Add(trim(ADOQueryCmd.fieldbyname('name').AsString));
//    ADOQueryCmd.next;
//  end;
    /////////查询框////////////
end;

procedure TfrmZHCPList.InitForm();
begin
  BegDate.Date := SGetServerDateMBeg(ADOQueryTemp);
  EndDate.Date := SGetServerDate(ADOQueryTemp);
  if Trim(Self.Caption) = '公司产品录入(高)*' then
  begin
    ReadCxGrid('公司产品录入(高)', Tv1, '公司产品管理');
    TBAdd.Visible := True;
    TBEdit.Visible := True;
    TBDel.Visible := True;
  end
  else if Trim(Self.Caption) = '公司产品录入*' then
  begin
    ReadCxGrid('公司产品录入', Tv1, '公司产品管理');
    TBAdd.Visible := True;
    TBEdit.Visible := True;
    v1ZICost.Visible := False;
    v1ZICost.Hidden := True;
    labelType.Items.Clear; // 清空现有选项
    labelType.Items.Add('客户标签');
    labelType.ItemIndex := 0;
  end
  else if Trim(Self.Caption) = '公司产品查询*' then
  begin
    ReadCxGrid('公司产品查询', Tv1, '公司产品管理');
    v1ZICost.Visible := False;
    v1ZICost.Hidden := True;
    TbCopy.Visible := False;
  end
  else if Trim(Self.Caption) = '挂钩库存查询*' then
  begin
    ReadCxGrid('挂钩库存查询', Tv1, '公司产品管理');
    v1ZICost.Visible := False;
    v1ZICost.Hidden := True;
    canshu1 := '库存';
    Panel5.Visible := False;
    TbCopy.Visible := False;
  end;
  ZIID.SetFocus;
  //InitGrid();
end;

procedure TfrmZHCPList.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 TfrmZHCPList.DelData(): Boolean;
begin
  try
    Result := false;
    ADOQueryCmd.Connection.BeginTrans;
    Order_Main.DisableControls;
    with Order_Main do
    begin
      //First;
     // while Order_Main.Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add(' UPdate ZH_CP_Info Set DelTime=getdate(),DelerCode=''' + Trim(DCode) + ''',Deler=''' + Trim(DName) + ''', Valid=''N'' ');
          sql.Add(' where ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
          sql.Add(' delete Image_File where IMID in(select IMID from Image_Info where WBID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + ''')');
          sql.Add(' delete Image_Info where  WBID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
          ExecSQL;
        end;
        Order_Main.Delete;
      end;
    end;
    Order_Main.EnableControls;

    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result := False;
    Application.MessageBox('数据删除异常！', '提示', 0);
  end;
end;

function TfrmZHCPList.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 TfrmZHCPList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(Self.Caption, cxGrid1);
end;

procedure TfrmZHCPList.TBRafreshClick(Sender: TObject);
begin
  IntTab();
  InitGrid();
end;

procedure TfrmZHCPList.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;
    if Trim(canshu1) = '库存' then
    begin
      sql.Add('select JLInt=isnull(Count(*),0)  ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      //sql.Add(' and ZIType=''成品'' ');
      sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
    end
    else
    begin
      sql.Add(' select JLInt=isnull(Count(*),0) ');
      sql.Add(' from ZH_CP_Info A  ');
      sql.Add(' where Valid=''Y''  ');
      //sql.Add(' and ZIType=''成品'' ');
      sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
      sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
    end;
    sql.Add(fsj10);
//    ShowMessage(SQL.text);
    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 TfrmZHCPList.TBAddClick(Sender: TObject);
begin
  Panel6.Visible := True;
end;

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

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

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

procedure TfrmZHCPList.TbLookClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  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;

procedure TfrmZHCPList.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 TfrmZHCPList.TBDelClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_CP_Info where ZIID like ''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '-%' + ''' and valid=''Y'' ');
    Open;
  end;
  if ADOQueryTemp.IsEmpty = False then
  begin
    Application.MessageBox('已存在关联产品，不能删除数据！', '提示', 0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？', '提示', 32 + 4) <> IDYES then
    Exit;
  if DelData() then
  begin
    //Order_Main.Delete;
  end;
end;

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

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

procedure TfrmZHCPList.ToolButton4Click(Sender: TObject);
begin
  if LabelType.Text = '大标签' then
  begin
     SpeedButton3.Click;
  end;
  if LabelType.Text = '小标签' then
  begin
     SpeedButton4.Click;
  end;
  if LabelType.Text = '客户标签' then
  begin
     SpeedButton5.Click;
  end;
  if LabelType.Text = '公司标签' then
  begin
     SpeedButton7.Click;
  end;
end;

procedure TfrmZHCPList.Tv1DblClick(Sender: TObject);
begin
  TbLook.Click;
end;

procedure TfrmZHCPList.v1Column2PropertiesEditValueChanged(Sender: TObject);
var
  mvalue: string;
begin
  mvalue := TcxTextEdit(Sender).EditingText;
  with Order_Main do
  begin
    Edit;
    if Trim(mvalue) = '' then
    begin
      FieldByName('ZICost').Value := Null;
    end
    else
    begin
      FieldByName('ZICost').Value := mvalue;
    end;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    sql.Clear;
    if Trim(mvalue) <> '' then
    begin
      sql.Add('Update ZH_CP_Info Set ZICost=' + mvalue);
    end
    else
    begin
      sql.Add('Update ZH_CP_Info Set ZICost=Null');
    end;
    sql.Add(' where ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
    ExecSQL;
  end;
end;

procedure TfrmZHCPList.cxTabControl2Change(Sender: TObject);
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;
      if Self.canshu1 = '库存' then
      begin
        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 Top 1 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 Top 1 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 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''  ');
        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
        sql.Add(')AA');
        sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
        sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
      end
      else
      begin
        sql.Add('Select AA.* From(');
        sql.Add(' select A.*,Cast(ROW_NUMBER() OVER(order by ZIID) as int) as RowNum ');
        sql.Add(' ,ImageFileTP=(select top 1 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 top 1 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=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
        sql.Add(' from ZH_CP_Info A  ');
        sql.Add(' where Valid=''Y''  ');
        if (Trim(fsj10) = '') and (Trim(HXNO.text) = '') then
        begin
          sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
          sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end;

        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
        sql.Add(')AA');
        sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
        sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
      end;
//      ShowMessage(SQL.text);
      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''  ');
      if Trim(canshu1) <> '库存' then
      begin
        if (Trim(fsj10) = '') and (Trim(HXNO.text) = '') then
        begin
          sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
          sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end;
//        sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//        sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end
      else
      begin
        sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
      end;
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
      sql.Add(fsj10);
//      ShowMessage(sql.text);
      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 A.Valid=''Y'' and KC.Valid=''Y''  ');
      if Trim(canshu1) <> '库存' then
      begin
        if (Trim(fsj10) = '') and (Trim(HXNO.text) = '') then
        begin
          sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
          sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end;
//        sql.Add(' and A.ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//        sql.Add(' and A.ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
      end;
      if trim(HXNO.text) <> '' then
        sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
      sql.Add(fsj10);
      Open;
    end;
    GGHZ.Text := Trim(ADOQueryTemp.fieldbyname('AA').AsString);
  finally
    ADOQueryMain.EnableControls;
  end;
  Panel2.Visible := False;
  cxTabControl2.Enabled := True;
//  InitGrid();
end;

procedure TfrmZHCPList.ZIIDKeyPress(Sender: TObject; var Key: Char);
var
  fsj, fsj10: string;
  JLInt, YeInt, i: Integer;
begin
  if Key = #13 then
  begin
    IntTab1();
    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;
        if Self.canshu1 = '库存' then
        begin
          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 Top 1 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 Top 1 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 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''  ');
          if trim(HXNO.text) <> '' then
            sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
          sql.Add(fsj10);
          sql.Add(')AA');
          sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
          sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
        end
        else
        begin
          sql.Add('Select AA.* From(');
          sql.Add(' select A.*,Cast(ROW_NUMBER() OVER(order by ZIID) as int) as RowNum ');
          sql.Add(' ,ImageFileTP=(select top 1 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 top 1 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=(select Sum(GGQty) from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID )');
          sql.Add(' from ZH_CP_Info A  ');
          sql.Add(' where Valid=''Y''  ');
//          sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//          sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
          if trim(HXNO.text) <> '' then
            sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
          sql.Add(fsj10);
          sql.Add(')AA');
          sql.Add(' where RowNum>=' + inttostr(cxTabControl2.TabIndex * 30 + 1));
          sql.Add(' and RowNum<=' + inttostr((cxTabControl2.TabIndex + 1) * 30));
        end;
//        ShowMessage(SQL.text);
        Open;
      end;
      SCreateCDS20(ADOQueryMain, Order_Main);
      SInitCDSData20(ADOQueryMain, Order_Main);
      with ADOQueryTemp do
      begin
        Close;
        sql.Clear;
        sql.Add(' select AA=isnull(Count(*),0) ');
        sql.Add(' from ZH_CP_Info A  ');
        sql.Add(' where Valid=''Y''  ');
        if Trim(canshu1) <> '库存' then
        begin
//          sql.Add(' and ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//          sql.Add(' and ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end
        else
        begin
          sql.Add(' and exists(select ZIID from ZH_CP_Info_KC KC where KC.ZIID=A.ZIID and isnull(KC.GGQty,0)>0 and KC.Valid=''Y'' )');
        end;
        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
//        ShowMessage(sql.text);
        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 A.Valid=''Y'' and KC.Valid=''Y''  ');
        if Trim(canshu1) <> '库存' then
        begin
//          sql.Add(' and A.ZIDate>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.Date)) + '''');
//          sql.Add(' and A.ZIDate<''' + Trim(FormatDateTime('yyyy-MM-dd', EndDate.Date + 1)) + '''');
        end;
        if trim(HXNO.text) <> '' then
          sql.Add('and (isnull(A.HXNO,'''') like ''%' + trim(HXNO.text) + '%'' or isnull(A.HXNM,'''') like ''%' + trim(HXNO.text) + '%'')');
        sql.Add(fsj10);
        Open;
      end;
      GGHZ.Text := Trim(ADOQueryTemp.fieldbyname('AA').AsString);
    finally
      ADOQueryMain.EnableControls;
    end;
//    IntTab();
    Panel2.Visible := False;
    cxTabControl2.Enabled := True;

//    BegDate.Date := StrToDate('2000-01-01');
//    EndDate.Date := StrToDate('2500-01-01');
//    IntTab();
//    InitGrid();
//    if Trim(TEdit(Sender).Name) = 'ZIID' then
//    begin
//      ZIID.Text := '';
//    end;
//    if CheckBox1.Checked = True then
//    begin
//      ToolButton4.Click;
//    end;
  end;
end;

procedure TfrmZHCPList.Tv1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
//var
  //fsj:String;
begin
 { StrFenHang(Order_Main.fieldbyname('ZIGY').AsString,fsj);
  if Trim(Order_Main.fieldbyname('ZIGY').AsString)<>fsj then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update ZH_CP_Info Set ZIGY='''+fsj+'''');
      sql.Add(' where ZIID='''+Trim(Order_Main.fieldbyname('ZIID').AsString)+'''');
      ExecSQL;
    end;
  end;
  with Order_Main do
  begin
    Edit;
    FieldByName('ZIGY').Value:=fsj;
    //Post;
  end; }

end;

procedure TfrmZHCPList.TBEditClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;
  try
    frmZHCPInPut := TfrmZHCPInPut.Create(Application);
    with frmZHCPInPut do
    begin
      PState := 1;
      FMainId := Trim(Order_Main.fieldbyname('ZIID').AsString);
      FZIType := Trim(Order_Main.fieldbyname('ZIType').AsString);
      if ShowModal = 1 then
      begin
        Self.ZIID.Text := Trim(frmZHCPInPut.FMainId);
        IntTab();
        InitGrid();
        Self.ZIID.Text := '';
      end;
    end;
  finally
    frmZHCPInPut.Free;
  end;
end;

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

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

procedure TfrmZHCPList.Image2Click(Sender: TObject);
begin
  Panel6.Visible := False;
end;

procedure TfrmZHCPList.SpeedButton2Click(Sender: TObject);
begin
  try
    frmZHCPInPut := TfrmZHCPInPut.Create(Application);
    with frmZHCPInPut do
    begin
      PState := 0;
      FMainId := '';
      FZIType := TSpeedButton(Sender).Caption;
      if ShowModal = 1 then
      begin
        Self.ZIID.Text := Trim(frmZHCPInPut.FMainId);
        IntTab();
        InitGrid();
        Self.ZIID.Text := '';
      end;
    end;
  finally
    frmZHCPInPut.Free;
  end;
end;

procedure TfrmZHCPList.ZITypeChange(Sender: TObject);
begin
//  BegDate.Date := StrToDate('2000-01-01');
//  EndDate.Date := StrToDate('2500-01-01');
//  IntTab();
//  InitGrid();
  TBFind.Click;
end;

procedure TfrmZHCPList.Image1Click(Sender: TObject);
begin
  panel7.Visible := false;
end;

procedure TfrmZHCPList.SpeedButton4Click(Sender: TObject);
var
  fPrintFile, Txt, fImagePath, maxno: string;
  i, j: Integer;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\展会产品标签85.rmf';
  if FileExists(fPrintFile) = False then
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\展会产品标签85.rmf'), '提示', 0);
    exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    //First;
 //   for i:=1 to j do
  //  begin
    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ,C.ImageFile');
      sql.Add(' ,defstr2=(select defstr2 from KH_Zdy_Attachment where zdyCode=''' + Trim(Order_Main.fieldbyname('HXNO').AsString) + ''' and Type=''CPHX'')');
      sql.Add(' from ZH_CP_Info A');
      sql.Add(' inner join Image_Info B on A.ZIID=B.WBID');
      sql.Add(' inner join Image_File C on B.IMID=C.IMID');
      sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
      sql.Add(' and B.IMNO=''图片'' and A.Valid=''Y'' and B.Valid=''Y'' and C.Valid=''Y'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PRT);
    SInitCDSData20(ADOQueryTemp, CDS_PRT);
    try
      ADOQueryCmd.Connection.BeginTrans;
      if GetLSNo(ADOQueryCmd, maxno, 'DYKC', 'ZH_CP_Info_KC', 4, 1, ) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取样品打印最大号失败!', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from ZH_CP_Info_KC where DYId=''' + Trim(maxno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('ZIId').Value := Trim(Order_Main.fieldbyname('ZIID').AsString);
        FieldByName('DYId').Value := Trim(maxno);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
        FieldByName('Valid').Value := 'Y';
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ZH_CP_Info_KC Set DYXH=isnull((select max(DYXH) from ZH_CP_Info_KC A ');
        sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''),0)+1 ');
        sql.Add(' where DYID=''' + Trim(maxno) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('保存打印流水号异常！', '提示', 0);
      Exit;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(maxno);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('二维码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['DYID'] := maxno;
    RM2.LoadFromFile(fPrintFile);
    RM2.DefaultCopies := StrToIntDef(trim(ComboBox1.Text), 1);
     // RM1.ShowReport;
    RM2.PrintReport;
  //  end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmZHCPList.SpeedButton3Click(Sender: TObject);
var
  fPrintFile, Txt, fImagePath, maxno: string;
  i, j: Integer;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\展会大标签.rmf';
  if FileExists(fPrintFile) = False then
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\展会大标签.rmf'), '提示', 0);
    exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin
    j := StrToInt(ComboBox1.Text);
    for i := 1 to j do
    begin
      with ADOQueryTemp do
      begin
        Close;
        SQL.Clear;
        sql.Add(' select A.* ,C.ImageFile');
        sql.Add(' ,defstr2=(select defstr2 from KH_Zdy_Attachment where zdyCode=''' + Trim(Order_Main.fieldbyname('HXNO').AsString) + ''' and Type=''CPHX'')');
        sql.Add(' ,ZJC=(select BPKZ+''G'' from M_ZDY X where X.BPID=A.ZhongJianCID )');
        sql.Add(' ,CASE WHEN( isnull(MianBu,'''')='''') THEN MianBuName ELSE MianBu END AS MB  ');
        sql.Add(' from ZH_CP_Info A');
        sql.Add(' inner join Image_Info B on A.ZIID=B.WBID');
        sql.Add(' inner join Image_File C on B.IMID=C.IMID');
        sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
        sql.Add(' and B.IMNO=''图片'' and A.Valid=''Y'' and B.Valid=''Y'' and C.Valid=''Y'' ');
        Open;
      end;
      SCreateCDS20(ADOQueryTemp, CDS_PRT);
      SInitCDSData20(ADOQueryTemp, CDS_PRT);
      try
        ADOQueryCmd.Connection.BeginTrans;
        if GetLSNo(ADOQueryCmd, maxno, 'DYKC', 'ZH_CP_Info_KC', 4, 1, ) = False then
        begin
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取样品打印最大号失败!', '提示', 0);
          Exit;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          SQL.Add('select * from ZH_CP_Info_KC where DYId=''' + Trim(maxno) + '''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          Append;
          FieldByName('ZIId').Value := Trim(Order_Main.fieldbyname('ZIID').AsString);
          FieldByName('DYId').Value := Trim(maxno);
          FieldByName('Filler').Value := Trim(DName);
          FieldByName('FillerCode').Value := Trim(DCode);
          FieldByName('Valid').Value := 'Y';
          Post;
        end;
        with ADOQueryCmd do
        begin
          Close;
          sql.Clear;
          sql.Add('Update ZH_CP_Info_KC Set DYXH=isnull((select max(DYXH) from ZH_CP_Info_KC A ');
          sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''),0)+1 ');
          sql.Add(' where DYID=''' + Trim(maxno) + '''');
          ExecSQL;
        end;
        ADOQueryCmd.Connection.CommitTrans;
      except
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('保存打印流水号异常！', '提示', 0);
        Exit;
      end;
      try
        Moudle := LoadLibrary('MakeQRBarcode.dll');
        @Makebar := GetProcAddress(Moudle, 'Make');
        @Mixtext := GetProcAddress(Moudle, 'MixText');
        Txt := Trim(maxno);
        fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
        if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
          CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
        if FileExists(fImagePath) then
          DeleteFile(fImagePath);
        Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
      except
        application.MessageBox('二维码生成失败！', '提示信息', MB_ICONERROR);
        exit;
      end;
      RMVariables['QRBARCODE'] := fImagePath;
      RMVariables['DYID'] := maxno;
      RM1.LoadFromFile(fPrintFile);
     // RM1.ShowReport;
      RM1.PrintReport;
    end;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmZHCPList.SpeedButton5Click(Sender: TObject);
var
  fPrintFile, Txt, fImagePath, maxno: string;
  i, j: Integer;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\客户标签.rmf';
  if FileExists(fPrintFile) = False then
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\客户标签.rmf'), '提示', 0);
    exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin

    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ,C.ImageFile');
      sql.Add(' ,defstr2=(select defstr2 from KH_Zdy_Attachment where zdyCode=''' + Trim(Order_Main.fieldbyname('HXNO').AsString) + ''' and Type=''CPHX'')');
      sql.Add(' ,CASE WHEN( isnull(MianBu,'''')='''') THEN MianBuName ELSE MianBu END AS MB  ');
      sql.Add(' from ZH_CP_Info A');
      sql.Add(' inner join Image_Info B on A.ZIID=B.WBID');
      sql.Add(' inner join Image_File C on B.IMID=C.IMID');
      sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
      sql.Add(' and B.IMNO=''图片'' and A.Valid=''Y'' and B.Valid=''Y'' and C.Valid=''Y'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PRT);
    SInitCDSData20(ADOQueryTemp, CDS_PRT);
    try
      ADOQueryCmd.Connection.BeginTrans;
      if GetLSNo(ADOQueryCmd, maxno, 'DYKC', 'ZH_CP_Info_KC', 4, 1, ) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取样品打印最大号失败!', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from ZH_CP_Info_KC where DYId=''' + Trim(maxno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('ZIId').Value := Trim(Order_Main.fieldbyname('ZIID').AsString);
        FieldByName('DYId').Value := Trim(maxno);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
        FieldByName('Valid').Value := 'Y';
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ZH_CP_Info_KC Set DYXH=isnull((select max(DYXH) from ZH_CP_Info_KC A ');
        sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''),0)+1 ');
        sql.Add(' where DYID=''' + Trim(maxno) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('保存打印流水号异常！', '提示', 0);
      Exit;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(maxno);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('二维码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['DYID'] := maxno;
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := StrToIntDef(trim(ComboBox1.Text), 1);
    RM1.PrintReport;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmZHCPList.SpeedButton7Click(Sender: TObject);
var
  fPrintFile, Txt, fImagePath, maxno: string;
  i, j: Integer;
  Moudle: THandle;
  Makebar: TMakebar;
  Mixtext: TMixtext;
begin
  if Order_Main.IsEmpty then
    Exit;
  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\公司标签.rmf';
  if FileExists(fPrintFile) = False then
  begin
    Application.MessageBox(PChar('没有找' + ExtractFilePath(Application.ExeName) + 'Report\公司标签.rmf'), '提示', 0);
    exit;
  end;
  Order_Main.DisableControls;
  with Order_Main do
  begin

    with ADOQueryTemp do
    begin
      Close;
      SQL.Clear;
      sql.Add(' select A.* ,C.ImageFile');
      sql.Add(' ,defstr2=(select defstr2 from KH_Zdy_Attachment where zdyCode=''' + Trim(Order_Main.fieldbyname('HXNO').AsString) + ''' and Type=''CPHX'')');
      sql.Add(' ,CASE WHEN( isnull(MianBu,'''')='''') THEN MianBuName ELSE MianBu END AS MB  ');
      sql.Add(' from ZH_CP_Info A');
      sql.Add(' inner join Image_Info B on A.ZIID=B.WBID');
      sql.Add(' inner join Image_File C on B.IMID=C.IMID');
      sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''');
      sql.Add(' and B.IMNO=''图片'' and A.Valid=''Y'' and B.Valid=''Y'' and C.Valid=''Y'' ');
      Open;
    end;
    SCreateCDS20(ADOQueryTemp, CDS_PRT);
    SInitCDSData20(ADOQueryTemp, CDS_PRT);
    try
      ADOQueryCmd.Connection.BeginTrans;
      if GetLSNo(ADOQueryCmd, maxno, 'DYKC', 'ZH_CP_Info_KC', 4, 1, ) = False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取样品打印最大号失败!', '提示', 0);
        Exit;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        SQL.Add('select * from ZH_CP_Info_KC where DYId=''' + Trim(maxno) + '''');
        Open;
      end;
      with ADOQueryCmd do
      begin
        Append;
        FieldByName('ZIId').Value := Trim(Order_Main.fieldbyname('ZIID').AsString);
        FieldByName('DYId').Value := Trim(maxno);
        FieldByName('Filler').Value := Trim(DName);
        FieldByName('FillerCode').Value := Trim(DCode);
        FieldByName('Valid').Value := 'Y';
        Post;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('Update ZH_CP_Info_KC Set DYXH=isnull((select max(DYXH) from ZH_CP_Info_KC A ');
        sql.Add(' where A.ZIID=''' + Trim(Order_Main.fieldbyname('ZIID').AsString) + '''),0)+1 ');
        sql.Add(' where DYID=''' + Trim(maxno) + '''');
        ExecSQL;
      end;
      ADOQueryCmd.Connection.CommitTrans;
    except
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('保存打印流水号异常！', '提示', 0);
      Exit;
    end;
    try
      Moudle := LoadLibrary('MakeQRBarcode.dll');
      @Makebar := GetProcAddress(Moudle, 'Make');
      @Mixtext := GetProcAddress(Moudle, 'MixText');
      Txt := Trim(maxno);
      fImagePath := ExtractFilePath(Application.ExeName) + 'image\temp.bmp';
      if not DirectoryExists(pchar(ExtractFilePath(Application.ExeName) + 'image')) then
        CreateDirectory(pchar(ExtractFilePath(Application.ExeName) + 'image'), nil);
      if FileExists(fImagePath) then
        DeleteFile(fImagePath);
      Makebar(pchar(Txt), Length(Txt), 3, 3, 0, PChar(fImagePath), 3);
    except
      application.MessageBox('二维码生成失败！', '提示信息', MB_ICONERROR);
      exit;
    end;
    RMVariables['QRBARCODE'] := fImagePath;
    RMVariables['DYID'] := maxno;
    RM1.LoadFromFile(fPrintFile);
    RM1.DefaultCopies := StrToIntDef(trim(ComboBox1.Text), 1);
    RM1.PrintReport;
  end;
  Order_Main.EnableControls;
end;

procedure TfrmZHCPList.TbCopyClick(Sender: TObject);
begin
  if Order_Main.IsEmpty then
    Exit;

//  if not assigned(frmZHCPInPut) then
//  begin
    try
      frmZHCPInPut := TfrmZHCPInPut.Create(Application);
      with frmZHCPInPut do
      begin
        PState := 1;
        CopyInt := 99;
        FZIType := Trim(Self.Order_Main.fieldbyname('ZIType').AsString);
        FMainId := Trim(Self.Order_Main.fieldbyname('ZIID').AsString);
//        Show;
        if ShowModal = 1 then
        begin
          InitGrid();
        end;
      end;
    finally
      frmZHCPInPut.Free;
    end;
//  end
//  else
//  begin
//    frmZHCPInPut.WindowState := wsMaximized;
//    frmZHCPInPut.Show;
//  end;
end;

end.

