unit U_CPKCHZList;

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, MovePanel, cxButtonEdit, cxPC, RM_Common, RM_Class, RM_e_Xls,
  RM_Dataset, RM_System, RM_GridReport, cxCheckBox, cxTextEdit,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu,StrUtils;

type
  TfrmCPKCHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label3: TLabel;
    Label4: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    v2Column1: TcxGridDBColumn;
    cxgrdPRTColor: TcxGridDBColumn;
    VC_KGQty: TcxGridDBColumn;
    VC_Qty: TcxGridDBColumn;
    v1orderNo: TcxGridDBColumn;
    Label6: TLabel;
    MJID: TEdit;
    VC_JQty: TcxGridDBColumn;
    v1MPRTMF: TcxGridDBColumn;
    v1MPRTKZ: TcxGridDBColumn;
    Label8: TLabel;
    MPRTKZ: TEdit;
    Label9: TLabel;
    MPRTMF: TEdit;
    Label7: TLabel;
    CPType10: TComboBox;
    v1Column3: TcxGridDBColumn;
    VC_NetKGQty: TcxGridDBColumn;
    Label10: TLabel;
    v1ConNo: TcxGridDBColumn;
    v1lidan: TcxGridDBColumn;
    v1ywy: TcxGridDBColumn;
    Label11: TLabel;
    Label12: TLabel;
    lidan: TEdit;
    ywy: TEdit;
    VC_ganghao: TcxGridDBColumn;
    Label14: TLabel;
    ganghao: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label16: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    PRTColor: TEdit;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDBPRT: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    VC_TJDate: TcxGridDBColumn;
    VC_ISHj: TcxGridDBColumn;
    chkwhj: TCheckBox;
    chkyhj: TCheckBox;
    CheckBox1: TCheckBox;
    VC_JYF: TcxGridDBColumn;
    MovePanel2: TPanel;
    Tv1Column1: TcxGridDBColumn;
    CPType: TEdit;
    ConNo: TEdit;
    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 PRTColorChange(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure chkwhjClick(Sender: TObject);
    procedure chkyhjClick(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CPType10Change(Sender: TObject);
    procedure ganghaoChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
    function  SGetFiltersZH(TMPanel:TPanel;EquTag,LikeTag:Integer):string;
    { Private declarations }
  public
    canshu1: string;
    { Public declarations }
  end;

var
  frmCPKCHZList: TfrmCPKCHZList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp,U_RTFun;

{$R *.dfm}
procedure TfrmCPKCHZList.SetStatus();
var
  i: Integer;
begin
  case cxTabControl1.TabIndex of
    0:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);
        ClearOrHideControls(Panel1, '2', True, False);
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        v1ywy.Visible:=True;
        v1lidan.Visible:=True;
      end;

    1:
      begin

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', True, True);
        ClearOrHideControls(Panel1, '2', True, False);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        ClearOrHideControls(Panel1, '2', True, True);

      end;

    2:
      begin

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        ClearOrHideControls(Panel1, '2', False, True);

        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
      end;
    3:
      begin
        ClearOrHideControls(Panel1, '0', True, True);
        ClearOrHideControls(Panel1, '1', True, False);
        ClearOrHideControls(Panel1, '2', True, False);
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        v1ConNo.Visible:=False;
        v1orderNo.Visible:=False;
        v1MPRTMF.Visible:=False;
        v1MPRTKZ.Visible:=False;
        v1lidan.Visible:=False;
        v1ywy.Visible:=False;
      end;
  end;

  VC_JQty.Visible := True;
  VC_KGQty.Visible := True;
  VC_NetKGQty.Visible := True;
  VC_Qty.Visible := True;
  v2Column1.Visible:=True;
  VC_ISHj.Visible := True;
  if cxTabControl1.TabIndex<>3 then
  begin
    VC_JYF.Visible:=True;
    VC_TJDate.Visible := True;
    v1ConNo.Visible:=True;
  end;

end;

procedure TfrmCPKCHZList.FormDestroy(Sender: TObject);
begin
  frmCPKCHZList := nil;
end;

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

procedure TfrmCPKCHZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCPKCHZList.InitGrid();
var
  Pwhere, fwhere: string;
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            Pwhere := SGetFiltersZH(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;

            sql.Add('exec P_View_FinshHZKCWKL  @HZType=''订单'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;
        1:      /////////////////////////颜色汇总//////////////////////////////
          begin
            Pwhere := SGetFiltersZH(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;
            sql.Add('exec P_View_FinshHZKCWKL  @HZType=''颜色'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;

        2:      /////////////////////////缸号汇总//////////////////////////////
          begin
            Pwhere := SGetFiltersZH(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;
            sql.Add('exec P_View_FinshHZKCWKL  @HZType=''缸号'' ');
            sql.Add(', @criteria =' + quotedstr(Trim(fwhere)));
          end;
        3:   /////////////////////////产品汇总//////////////////////////////
          begin
            Pwhere := SGetFiltersZH(Panel1, 1, 2);
            if trim(Pwhere) <> '' then
              fwhere := fwhere + ' and ' + trim(Pwhere);
            if trim(Pwhere) <> '' then
            begin
              if chkyhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=1 ';
              end;
              if chkwhj.Checked then
              begin
                fwhere := fwhere + ' and ISHj=0 ';
              end;
            end;

            sql.Add('exec P_View_FinshHZKCWKL  @HZType=''产品'' ');
            sql.Add(', @criteria=' + quotedstr(Trim(fwhere)));
          end;

      end;
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCPKCHZList.TBRafreshClick(Sender: TObject);
begin
  SetStatus();
  //BegDate.SetFocus;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  InitGrid();
  MovePanel2.Visible := False;
end;

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

procedure TfrmCPKCHZList.TBCloseClick(Sender: TObject);
begin
  if Trim(DName)='ADMIN' then
  begin
    WriteCxGridAll(trim(self.Caption)+'1', Tv1,ADOQueryCmd,ADOQueryTemp);
  end;
  Close;
end;

procedure TfrmCPKCHZList.FormShow(Sender: TObject);
begin
  ReadCxGridAll(trim(self.Caption)+'1', Tv1);
end;

procedure TfrmCPKCHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel('库存汇总列表', cxGrid2);
end;

procedure TfrmCPKCHZList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 3, 4));
    //SDofilter(ADOQueryMain, ' and '+SGetFiltersZH(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;
function TfrmCPKCHZList.SGetFiltersZH(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 Controls[i].Tag=EquTag then
            Result:=Result+' and '+Controls[i].Hint+'='+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 ('+Controls[i].Hint+' 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+' or '+Controls[i].Hint+' like '+QuotedStr('%'+Copy(Trim(fsj1),1,j-1 )+'%');
                  fsj1:=Copy(Trim(fsj1),j+1,Length(Trim(fsj1)));
                end else
                begin
                  Result:=Result+' or '+Controls[i].Hint+' like '+QuotedStr('%'+Trim(fsj1)+'%');
                  fsj1:='';
                end;
              end;
              Result:=Result+')'
            end else
              Result:=Result+' and '+Controls[i].Hint+' like '+QuotedStr('%'+Trim(TEdit(Controls[i]).Text)+'%');
          end;
        end;
      end;
    end;
  end;
  if Trim(Result)<>'' then
    Result:=Trim(RightBStr(Result,Length(Result)-4));
end;
procedure TfrmCPKCHZList.PRTColorChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmCPKCHZList.Tv1DblClick(Sender: TObject);
var
  Tabint: integer;
begin
  Tabint := cxTabControl1.TabIndex;
  case Tabint of
    0:
      begin
        AssignmentControls(Panel1, CDS_Main, '0');
        cxTabControl1.TabIndex := 1;
      end;
    1:
      begin
        AssignmentControls(Panel1, CDS_Main, '1');
        cxTabControl1.TabIndex := 2;
      end;
  end;

end;

procedure TfrmCPKCHZList.cxTabControl1Change(Sender: TObject);
begin

  SetStatus();
  InitGrid();
end;

procedure TfrmCPKCHZList.ToolButton1Click(Sender: TObject);
var
  fPrintFile: string;
begin
  if CDS_Main.IsEmpty then
    EXIT;

  case cxTabControl1.TabIndex of
    0:
      begin
        ExportFtErpFile('成品汇总订单码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总订单码单.rmf';
      end;

    1:
      begin
        ExportFtErpFile('成品汇总颜色码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总颜色码单.rmf';
      end;

    2:
      begin
        ExportFtErpFile('成品汇总缸号码单.rmf', ADOQueryTemp);
        fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\成品汇总缸号码单.rmf';
      end;
  end;

  if FileExists(fPrintFile) then
  begin
//    RM1.DefaultCopies := 2;
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmCPKCHZList.chkwhjClick(Sender: TObject);
begin

  if chkwhj.Checked then
  begin
    chkyhj.Checked := False;
  end;
  InitGrid();
end;

procedure TfrmCPKCHZList.chkyhjClick(Sender: TObject);
begin
  if chkyhj.Checked then
  begin
    chkwhj.Checked := False;
  end;
  InitGrid();
end;

procedure TfrmCPKCHZList.CheckBox1Click(Sender: TObject);
var
  i:Integer;
begin
  with Panel1 do
  begin
    for i:=0 to ControlCount-1 do
    begin
      if CheckBox1.Checked=True then
        Controls[i].Tag:=1
      else
        Controls[i].Tag:=2;
    end;
  end;
  TBFind.Click;
end;


procedure TfrmCPKCHZList.CPType10Change(Sender: TObject);
begin
  CPType.Text:=Trim(CPType10.Text);
  InitGrid();
end;

procedure TfrmCPKCHZList.ganghaoChange(Sender: TObject);
begin
  TBFind.Click;
end;

end.

