unit U_CKProductBCPKCList;

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, cxCheckBox, Menus, cxCalendar, RM_Common, RM_Class,
  RM_GridReport, RM_System, RM_Dataset, BtnEdit, Math, Clipbrd, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmCKProductBCPKCList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    Tv1: TcxGridDBTableView;
    cxGrid2Level1: TcxGridLevel;
    cxGrid2: TcxGrid;
    cxgrdMPRTCodeName: TcxGridDBColumn;
    cxgrdPRTColor: TcxGridDBColumn;
    cxgrdKGQty: TcxGridDBColumn;
    cxgrdQty: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    cxgrdQtyUnit: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    cxgrdMPRTMF: TcxGridDBColumn;
    cxgrdMPRTKZ: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    cxgrdMPRTSpec: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    MovePanel2: TMovePanel;
    v1Column11: TcxGridDBColumn;
    v1Column12: TcxGridDBColumn;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    ThreeColorBase: TcxStyleRepository;
    SHuangSe: TcxStyle;
    SkyBlue: TcxStyle;
    Default: TcxStyle;
    QHuangSe: TcxStyle;
    Red: TcxStyle;
    FontBlue: TcxStyle;
    TextSHuangSe: TcxStyle;
    FonePurple: TcxStyle;
    FoneClMaroon: TcxStyle;
    FoneRed: TcxStyle;
    RowColor: TcxStyle;
    handBlack: TcxStyle;
    cxBlue: TcxStyle;
    RMDB_Main: TRMDBDataSet;
    ADOQueryPrint: TADOQuery;
    RM2: TRMGridReport;
    v1Column16: TcxGridDBColumn;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    RM3: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    RMDBPRT: TRMDBDataSet;
    cxgrdNETKGQty: TcxGridDBColumn;
    Panel1: TPanel;
    lblMPRTCodeName: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    lblorderNo: TLabel;
    lblMJID: TLabel;
    lblMPRTKZ: TLabel;
    lblMPRTMF: TLabel;
    lblCPType: TLabel;
    lblPRTColor: TLabel;
    lblMPRTSpec: TLabel;
    lblRKplace: TLabel;
    MPRTCodeName: TEdit;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    orderNo: TEdit;
    MJID: TEdit;
    MJKZ: TEdit;
    MJMF: TEdit;
    CPType: TComboBox;
    RKplace: TEdit;
    PRTColor: TEdit;
    v1Column5: TcxGridDBColumn;
    MPRTSpec: TEdit;
    v1Column10: TcxGridDBColumn;
    lblConNo: TLabel;
    ConNo: TEdit;
    lblRKOrdID: TLabel;
    RKOrdID: TEdit;
    lblKuangHao: TLabel;
    v1Column13: TcxGridDBColumn;
    cxgrdCustomerNoName: TcxGridDBColumn;
    Label14: TLabel;
    CustomerNoName: TEdit;
    N3: TMenuItem;
    cxgrdJQty: TcxGridDBColumn;
    Panel2: TPanel;
    Panel3: TPanel;
    cxTabControl1: TcxTabControl;
    Panel6: TPanel;
    Label31: TLabel;
    LBCPAP: TLabel;
    BTLP: TButton;
    BTNP: TButton;
    TCBNOR: TComboBox;
    v1Column2: TcxGridDBColumn;
    Label3: TLabel;
    APID: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    JYXH: 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 Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure BTNPClick(Sender: TObject);
    procedure BTLPClick(Sender: TObject);
    procedure TCBNORChange(Sender: TObject);
    procedure MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
    procedure CPTypeKeyPress(Sender: TObject; var Key: Char);
    procedure cxTabControl1Change(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    FLeft, FTop: Integer;
    CurrentPage, RecordsNumber: Integer;
    procedure InitGrid();
    procedure SetStatus();
    { Private declarations }
  public
    issel: integer;
    { Public declarations }
  end;

var
  frmCKProductBCPKCList: TfrmCKProductBCPKCList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

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

  case cxTabControl1.TabIndex of
    0:
      begin
        Panel6.Visible := False;
        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);
        cxgrdJQty.Visible := True;
        cxgrdKGQty.Visible := True;
        cxgrdNETKGQty.Visible := True;
        cxgrdQty.Visible := True;
        cxgrdQtyUnit.Visible := True;
      end;
    1:
      begin
        Panel6.Visible := False;
        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 := false;
        end;
        IsVisibleTV(Panel1, TV1, '0', True);
        IsVisibleTV(Panel1, TV1, '1', True);
        cxgrdJQty.Visible := True;
        cxgrdKGQty.Visible := True;
        cxgrdNETKGQty.Visible := True;
        cxgrdQty.Visible := True;
        cxgrdQtyUnit.Visible := True;
      end;
    2:
      begin
        for i := 0 to tv1.ColumnCount - 1 do
        begin
          tv1.Columns[i].Visible := True;
        end;
        cxgrdJQty.Visible := false;

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

        Panel6.Visible := True;

      end;
  end;
end;

procedure TfrmCKProductBCPKCList.FormDestroy(Sender: TObject);
begin
  frmCKProductBCPKCList := nil;
end;

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

procedure TfrmCKProductBCPKCList.FormCreate(Sender: TObject);
begin
  BegDate.DateTime := SGetServerDateTime(ADOQueryTemp);
  EndDate.DateTime := SGetServerDateTime(ADOQueryTemp)
end;

procedure TfrmCKProductBCPKCList.InitGrid();
var
  fwhere, Pwhere: string;
begin
  MovePanel2.Left := (self.Width - MovePanel2.Width) div 2;
  MovePanel2.Visible := True;
  MovePanel2.Refresh;
  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 * from (   ');
            sql.Add('select SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.NetKGQty) NETKGQty,count(A.MJID) JQty,A.QtyUnit ');
            sql.Add(',B.CustomerNoName');
            sql.Add('from  CK_BanCP_CR A   inner join CK_BanCP_KC D on A.MJID=D.MJID inner join JYOrder_Main B on A.MainId=B.MainId  ');
            sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId   where A.CRFlag=''入库'' and D.KCValid=''Y'' ');
            sql.Add(' group by  B.CustomerNoName,A.QtyUnit');
            sql.Add(' ) AA ' + fwhere);
          end;
        1:
          begin
            sql.Add('select * from (   ');
            sql.Add('select SUM(A.KGQty) KGQty,SUM(A.Qty) Qty,SUM(A.NetKGQty) NetKGQty,count(A.MJID) JQty,A.QtyUnit,A.ganghao ');
            sql.Add(' ,B.CustomerNoName,B.ConNo,B.OrderNo,B.MPRTCode,B.MPRTCodeName,B.MPRTSpec,C.PRTColor  ');
            sql.Add(' ,A.MJMF,A.MJKZ,A.APID   ');
            sql.Add('from  CK_BanCP_CR A   inner join CK_BanCP_KC D on A.MJID=D.MJID inner join JYOrder_Main B on A.MainId=B.MainId  ');
            sql.Add('inner join JYOrder_Sub C on A.SubId=C.SubId   ');
            sql.Add('inner join JYOrder_Car E on E.CarNo=A.APID ');
            sql.Add('where A.CRFlag=''入库'' and D.KCValid=''Y'' ');
            sql.Add(' group by  B.CustomerNoName,A.QtyUnit,A.ganghao,B.ConNo,B.OrderNo,B.MPRTCode,B.MPRTCodeName,A.MJMF,A.MJKZ,B.MPRTSpec ,C.PRTColor,A.APID  ');
            sql.Add(' ) AA ' + 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));
    MovePanel2.Visible := false;
  finally
    ADOQueryMain.EnableControls;
    MovePanel2.Visible := false;
  end;
end;

procedure TfrmCKProductBCPKCList.TBRafreshClick(Sender: TObject);
begin
  CurrentPage := 1;
  SetStatus();
  InitGrid();

end;

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

procedure TfrmCKProductBCPKCList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.Caption) + trim(IntToStr(cxTabControl1.TabIndex)), Tv1, '三水成品仓库');
  Close;
end;

procedure TfrmCKProductBCPKCList.FormShow(Sender: TObject);
begin
  CurrentPage := 1;
  RecordsNumber := 500;
  ReadCxGrid(trim(self.Caption) + trim(IntToStr(cxTabControl1.TabIndex)), Tv1, '三水成品仓库');
  SetStatus();
end;

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

procedure TfrmCKProductBCPKCList.Tv1MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  FLeft := X;
  FTop := Y;
end;

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

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

procedure TfrmCKProductBCPKCList.BTNPClick(Sender: TObject);
begin
  if CurrentPage < CDS_Main.FieldByName('TotalCount').AsInteger / RecordsNumber then
    CurrentPage := CurrentPage + 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.BTLPClick(Sender: TObject);
begin
  if CurrentPage > 1 then
    CurrentPage := CurrentPage - 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.TCBNORChange(Sender: TObject);
begin
  RecordsNumber := StrToInt(TCBNOR.Text);
  CurrentPage := 1;
  InitGrid();
end;

procedure TfrmCKProductBCPKCList.MPRTCodeNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    CurrentPage := 1;
    InitGrid();
  end;
end;

procedure TfrmCKProductBCPKCList.CPTypeKeyPress(Sender: TObject; var Key: Char);
begin
  CurrentPage := 1;
  InitGrid();
end;

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

end;

procedure TfrmCKProductBCPKCList.N3Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_Main.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmCKProductBCPKCList.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
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;

end.

