unit U_ShaKCHZListJWL_Sel;

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, cxCheckBox, RM_Common, RM_Class, RM_e_Xls, RM_Dataset,
  RM_System, RM_GridReport, Menus, cxCalendar, cxButtonEdit, cxTextEdit, Clipbrd,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmShaKCHZListJWL = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    CDS_Main: TClientDataSet;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v2Column1: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    CPCode: TEdit;
    ToolButton2: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    Label9: TLabel;
    CPName: TEdit;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    cxStyle2: TcxStyle;
    cxStyle3: TcxStyle;
    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 TBFindClick(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SXBatchNOChange(Sender: TObject);
    procedure FactoryNameChange(Sender: TObject);
    procedure Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton2Click(Sender: TObject);
  private
    procedure InitGrid();
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmShaKCHZListJWL: TfrmShaKCHZListJWL;

implementation

uses
  U_DataLink, U_RTFun, U_ShaKCListJWL;

{$R *.dfm}

procedure TfrmShaKCHZListJWL.FormDestroy(Sender: TObject);
begin
  frmShaKCHZListJWL := nil;
end;

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

procedure TfrmShaKCHZListJWL.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select A.* from (  ');
      sql.Add(' SELECT CPCode,CPName  ');
      sql.Add(' ,KCQty=(select sum(isnull(Qty,0)*CRQtyFlag) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName)   ');
      sql.Add(' ,KCPS=(select sum(isnull(piqty,0)*CRQtyFlag) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName)   ');
      sql.Add(' ,CKQty=(select sum(isnull(Qty,0)) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName and X.CRFlag= ''出库'' and CONVERT(VARCHAR(10),X.CRTime,120) =CONVERT(VARCHAR(10),GETDATE(),120))   ');
      sql.Add(' ,CKPS=(select sum(isnull(piqty,0)) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName and X.CRFlag= ''出库'' and CONVERT(VARCHAR(10),X.CRTime,120) =CONVERT(VARCHAR(10),GETDATE(),120))   ');
      sql.Add(' ,RKQty=(select sum(isnull(Qty,0)) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName and X.CRFlag= ''入库'' and CONVERT(VARCHAR(10),X.CRTime,120) =CONVERT(VARCHAR(10),GETDATE(),120))   ');
      sql.Add(' ,RKPS=(select sum(isnull(piqty,0)) from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName and X.CRFlag= ''入库'' and CONVERT(VARCHAR(10),X.CRTime,120) =CONVERT(VARCHAR(10),GETDATE(),120))   ');
      sql.Add(' ,RKJT=(select COUNT (DISTINCT SJCarNo)  from CK_SXPB_CR X    ');
      sql.Add(' where A.CPCode=X.CPCode and A.CPName = X.CPName and X.CRFlag= ''入库'' and CONVERT(VARCHAR(10),X.CRTime,120) =CONVERT(VARCHAR(10),GETDATE(),120))   ');
      sql.Add(' FROM CK_SXPB_CR A ');
      sql.Add(' WHERE  A.CRFlag= ''入库''  ');
      sql.Add(' GROUP BY CPCode,CPName ');
      sql.Add(' )A where KCQty > 0   ');
      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

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

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

procedure TfrmShaKCHZListJWL.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('机物料库存汇总列表11', Tv1, '机物料仓库');
  Close;
end;

procedure TfrmShaKCHZListJWL.FormShow(Sender: TObject);
begin
  ReadCxGrid('机物料库存汇总列表11', Tv1, '机物料仓库');
  InitGrid();
end;

procedure TfrmShaKCHZListJWL.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  end;
end;

procedure TfrmShaKCHZListJWL.MPRTCodeNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmShaKCHZListJWL.SXBatchNOChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCHZListJWL.FactoryNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmShaKCHZListJWL.Tv1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmShaKCListJWL := TfrmShaKCListJWL.Create(Application);
    with frmShaKCListJWL do
    begin
      frmShaKCListJWL.Align := alClient;
      FDYFlag := '99';
      frmShaKCListJWL.SPName.Text := Trim(Self.CDS_Main.fieldbyname('SPName').AsString);
      frmShaKCListJWL.SPSpec.Text := Trim(Self.CDS_Main.fieldbyname('SPSpec').AsString);
      frmShaKCListJWL.BatchNo.Text := Trim(Self.CDS_Main.fieldbyname('BatchNo').AsString);
      frmShaKCListJWL.SXPinPai.Text := Trim(Self.CDS_Main.fieldbyname('SXPinPai').AsString);
      frmShaKCListJWL.QtyUnit.Text := Trim(Self.CDS_Main.fieldbyname('QtyUnit').AsString);
      if ShowModal = 1 then
      begin

      end;
    end;
  finally
    frmShaKCListJWL.Free;
  end;
end;

procedure TfrmShaKCHZListJWL.ToolButton2Click(Sender: TObject);
begin
  if CDS_Main.IsEmpty then
    exit;
  TcxGridToExcel('机物料库存汇总列表', cxGrid2);
end;

end.

