unit U_SQKCHZMXSel;

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, cxCheckBox, cxPC, Math, cxCalendar,
  Menus;

type
  TfrmSQKCHZMXSel = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    MovePanel2: TMovePanel;
    ToolButton1: TToolButton;
    Panel1: TPanel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Label12: TLabel;
    Label14: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    MPRTCodeName: TEdit;
    MPRTMF: TEdit;
    ConNo: TEdit;
    MPRTCode: TEdit;
    orderNo: TEdit;
    MPRTKZ: TEdit;
    PRTColor: TEdit;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label1: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    cxgrdSSel: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxgrdMJGW: TcxGridDBColumn;
    cxgrdMJNW: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    cxgrdMJLen: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    cxgrdPS: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    Label2: TLabel;
    Label6: TLabel;
    MJID: TEdit;
    ganghao: TEdit;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBRafreshClick(Sender: TObject);
    procedure ConNoMChange(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv1DblClick(Sender: TObject);
    procedure ConNoKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    { Public declarations }
    FCode, FClothType, FSeBie, FZZType: string;
  end;

var
  frmSQKCHZMXSel: TfrmSQKCHZMXSel;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp,U_RTFun;

{$R *.dfm}
procedure TfrmSQKCHZMXSel.SetStatus();
var
  i: Integer;
begin

  Panel6.Visible := False;
  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);
        cxgrdPS.Visible := True;
        cxgrdMJGW.Visible := True;
        cxgrdMJNW.Visible := True;
        cxgrdMJLen.Visible := True;
        cxgrdSSel.Visible := True;
      end;
    1:
      begin
        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        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);
        cxgrdPS.Visible := True;
        cxgrdMJGW.Visible := True;
        cxgrdMJNW.Visible := True;
        cxgrdMJLen.Visible := True;
        cxgrdSSel.Visible := True;
      end;
    2:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        cxgrdPS.Visible := false;

        ClearOrHideControls(Panel1, '0', False, True);
        ClearOrHideControls(Panel1, '1', False, True);
        ClearOrHideControls(Panel1, '2', False, True);
        Panel6.Visible := True;
      end;
  end;
end;

procedure TfrmSQKCHZMXSel.FormDestroy(Sender: TObject);
begin
  frmSQKCHZMXSel := nil;
end;

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

procedure TfrmSQKCHZMXSel.InitGrid();
var
  fwhere, Pwhere: string;
begin
  Pwhere := SGetFilters(Panel1, 1, 2);
  if trim(Pwhere) <> '' then
    fwhere := fwhere + ' where ' + trim(Pwhere);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;

      case cxTabControl1.TabIndex of
        0:   /////////////////////////订单汇总//////////////////////////////
          begin
            SQL.Add('select AA.*,BB.*,CC.*,SelType=''订单'' from ( ');
            sql.add(' select A.MainId,A.SubId    ');
            sql.add(' ,SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty  ');
            sql.add('  from CK_BanCP_CR A');
            Sql.add(' inner join CK_BanCP_KC B on A.MJID=B.MJID where B.KCValid=''Y'' and A.CRFlag=''入库'' ');
            SQL.Add(' group by A.MainId,A.SubId  ');
            SQL.Add(' ) AA inner join JYOrder_Main BB on AA.Mainid=BB.MainId');
            SQL.Add(' inner join JYOrder_Sub CC on AA.SubId=CC.SubId  ');
            sql.Add(fwhere);
          end;
        1:   /////////////////////////缸汇总//////////////////////////////
          begin
            SQL.Add('select AA.*,BB.*,CC.*,SelType=''缸'' from ( ');
            sql.add(' select A.MainId,A.SubId,A.ganghao    ');
            sql.add(' ,SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty  ');
            sql.add('  from CK_BanCP_CR A');
            Sql.add(' inner join CK_BanCP_KC B on A.MJID=B.MJID where B.KCValid=''Y'' and A.CRFlag=''入库'' ');
            SQL.Add(' group by A.MainId,A.SubId,A.ganghao  ');
            SQL.Add(' ) AA inner join JYOrder_Main BB on AA.Mainid=BB.MainId');
            SQL.Add(' inner join JYOrder_Sub CC on AA.SubId=CC.SubId  ');
            sql.Add(fwhere);
          end;
        2:   /////////////////////////明细//////////////////////////////
          begin
            sql.Add('exec P_Page_FinishClothKC ');
            sql.Add('@pageIndex=' + inttostr(CurrentPage));
            sql.Add(',@pageSize=' + inttostr(RecordsNumber));
            sql.Add(',@criteria= ' + quotedstr(fwhere));
          end;
      end;
//      showmessage(sql.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);

    if cxTabControl1.TabIndex = 2 then
      LBCPAP.Caption := inttostr(CurrentPage) + '/' + inttostr(ceil(CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber));

  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmSQKCHZMXSel.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('发货申请专用', Tv1, '坯布仓库');
  Close;
end;

procedure TfrmSQKCHZMXSel.FormShow(Sender: TObject);
begin
  ReadCxGrid('发货申请专用', Tv1, '坯布仓库');
  CurrentPage := 1;
  RecordsNumber := 500;
  SetStatus();
//  InitGrid();
end;

procedure TfrmSQKCHZMXSel.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

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

procedure TfrmSQKCHZMXSel.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 TfrmSQKCHZMXSel.ConNoKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

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

procedure TfrmSQKCHZMXSel.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_Main, false);
end;

end.

