unit U_ShaKCHZListJWL;

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;
    v1Column8: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Label3: TLabel;
    Label8: TLabel;
    SPName: TEdit;
    SPSpec: TEdit;
    ToolButton2: TToolButton;
    v1Column17: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    Label2: TLabel;
    BatchNo: TEdit;
    v1Column20: TcxGridDBColumn;
    v1Column21: TcxGridDBColumn;
    v1Column23: TcxGridDBColumn;
    Label9: TLabel;
    SXPinPai: TEdit;
    v1Column1: TcxGridDBColumn;
    Label1: TLabel;
    QtyUnit: TEdit;
    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 SPName,SPSpec,BatchNo,SXPinPai,QtyUnit');
      sql.Add(' ,RKPiQty=Sum(isnull(PiQty,0)),RKQty=Sum(isnull(Qty,0))');
      sql.Add(' ,KCPiQty=Sum(isnull(MXKCPiQty,0)),KCQty=Sum(isnull(MXKCQty,0))');
      sql.Add(' ,CKPiQty=Sum(isnull(PiQty,0))-Sum(isnull(MXKCPiQty,0)),CKQty=Sum(isnull(Qty,0))-Sum(isnull(MXKCQty,0))');
      sql.Add(' from CK_SXPB_CR A where isnull(CKName,'''')=''机物料'' ');
      SQL.Add(' and A.CRFlag=''入库'' and MXKCQty>0 and ZFFlag=0 ');
      SQL.Add(' Group by SPName,SPSpec,BatchNo,SXPinPai,QtyUnit');
      Open;
      //ShowMessage(SQL.Text);
    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('机物料库存汇总列表',Tv1,'机物料仓库');
  Close;
end;

procedure TfrmShaKCHZListJWL.FormShow(Sender: TObject);
begin
  ReadCxGrid('机物料库存汇总列表',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.BatchNo.Text:=Trim(Self.CDS_Main.fieldbyname('BatchNo').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.
