unit U_ZZJWLOutHZList;

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, cxPC,
  cxLookAndFeels, cxLookAndFeelPainters, cxNavigator;

type
  TfrmZZJWLOutHZList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBFind: TToolButton;
    TBExport: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    DataSource1: TDataSource;
    cxGridPopupMenu1: TcxGridPopupMenu;
    Label1: TLabel;
    Label2: TLabel;
    BegDate: TDateTimePicker;
    EndDate: TDateTimePicker;
    CDS_Main: TClientDataSet;
    RM1: TRMGridReport;
    RMDBMain: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMDBHZ: TRMDBDataSet;
    CDS_HZ: TClientDataSet;
    CDS_PRT: TClientDataSet;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    cxGrid2: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column6: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column15: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1Column1: TcxGridDBColumn;
    Label7: TLabel;
    llDepartment: 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 TBExportClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure SPNameChange(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure SPSpecChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    canshu1, canshu2: string;
    procedure InitGrid();
    { Private declarations }
  public
    FFSPID: string;
    { Public declarations }
  end;

var
  frmZZJWLOutHZList: TfrmZZJWLOutHZList;

implementation

uses
  U_DataLink, U_RTFun;

{$R *.dfm}

procedure TfrmZZJWLOutHZList.FormDestroy(Sender: TObject);
begin
  frmZZJWLOutHZList := nil;
end;

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

procedure TfrmZZJWLOutHZList.FormCreate(Sender: TObject);
begin
  //cxGrid1.Align:=alClient;
  canshu1 := Trim(DParameters1);
  canshu2 := Trim(DParameters2);
  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
  BegDate.DateTime := EndDate.DateTime - 90;
end;

procedure TfrmZZJWLOutHZList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      sql.Clear;
      sql.Add(' select AA.llDepartment ,sum(AA.RKMoney) RKMoney,sum(AA.ZRKMoney) ZRKMoney ');
      sql.Add(' from ( select A.llDepartment ');
      sql.Add(' ,RKMoney= A.qty*(select  price from CK_SXPB_CR X where X.spid=A.FZSPID)');
      sql.Add(' ,ZRKMoney= A.qty*( case when A.QtyUnit=''枚'' then (select  price from CK_SXPB_CR X where X.spid=A.FZSPID) else 0 end )');
      sql.Add(' from CK_SXPB_CR A');
      sql.add(' where A.CRTime>=''' + Trim(FormatDateTime('yyyy-MM-dd', BegDate.DateTime)) + '''');
      sql.Add(' and A.CRTime<''' + Trim(FormatDateTime('yyyy-MM-dd', enddate.DateTime + 1)) + '''');
      sql.Add(' and isnull(CKName,'''')=''机物料'' and CRFlag=''出库''  and ZFFlag=0  ) AA ');
      sql.Add(' group by AA.llDepartment');
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_Main);
    SInitCDSData20(ADOQueryMain, CDS_Main);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmZZJWLOutHZList.TBRafreshClick(Sender: TObject);
begin
  BegDate.SetFocus;
  InitGrid();
end;

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

procedure TfrmZZJWLOutHZList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption), Tv1, '机物料仓库');
  Close;
end;

procedure TfrmZZJWLOutHZList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    exit;
  TcxGridToExcel(trim(Self.Caption), cxGrid2);
end;

procedure TfrmZZJWLOutHZList.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 TfrmZZJWLOutHZList.SPNameChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

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

procedure TfrmZZJWLOutHZList.SPSpecChange(Sender: TObject);
begin
  TBFind.Click;
end;

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

end.

