unit U_NengHaoList;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxGraphics, cxCustomData, cxStyles, cxTL, cxMaskEdit, DB, ADODB,
  cxInplaceContainer, cxDBTL, cxControls, cxTLData, ComCtrls, ToolWin,
  StdCtrls, cxFilter, cxData, cxDataStorage, cxEdit, cxDBData, DBClient,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, ExtCtrls,
  cxSplitter, cxGridLevel, cxClasses, cxGridCustomView, cxGrid,
  cxGridCustomPopupMenu, cxGridPopupMenu, RM_Dataset, RM_System, RM_Common,
  RM_Class, RM_GridReport, IdBaseComponent, IdComponent, IdTCPConnection,
  IdTCPClient, IdFTP,ShellAPI,IniFiles, cxCheckBox, cxCalendar,
  cxButtonEdit, cxTextEdit;

type
  TfrmNengHaoList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    NHBiaoName: TEdit;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Label1: TLabel;
    Chejian: TEdit;
    ToolButton4: TToolButton;
    v2Column8: TcxGridDBColumn;
    Label2: TLabel;
    NHType: TComboBox;
    BegDate: TDateTimePicker;
    Label4: TLabel;
    EndDate: TDateTimePicker;
    Label5: TLabel;
    v2Column1: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    ToolButton5: TToolButton;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure NHBiaoNameChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();

  public

  end;

var
  frmNengHaoList: TfrmNengHaoList;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_NengHaoInput;

{$R *.dfm}



procedure TfrmNengHaoList.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from NengHao ');
      sql.Add(' where NHDate>='''+Trim(FormatDateTime('yyyy-MM-dd',BegDate.DateTime))+'''');
      sql.Add(' and NHDate<'''+Trim(FormatDateTime('yyyy-MM-dd',EndDate.DateTime+1))+'''');
      sql.Add(' order by Chejian,NHTYpe,NHDate');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmNengHaoList.FormDestroy(Sender: TObject);
begin
  frmNengHaoList:=nil;
end;

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

procedure TfrmNengHaoList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('能耗查询',Tv2,'客户自定义');
  Close;
end;
procedure TfrmNengHaoList.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  if Trim(CDS_HZ.fieldbyname('NHID').AsString)<>'' then
  begin
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('delete NengHao where NHID='''+Trim(CDS_HZ.fieldbyname('NHID').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ.Delete;
end;

procedure TfrmNengHaoList.FormShow(Sender: TObject);
begin
  ReadCxGrid('能耗查询',Tv2,'客户自定义');
  EndDate.Date:=SGetServerDate(ADOQueryTemp);
  BegDate.Date:=EndDate.Date;
  InitGrid();
end;

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

procedure TfrmNengHaoList.ToolButton2Click(Sender: TObject);
begin
  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain,SGetFilters(Panel1,1,2));
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmNengHaoList.NHBiaoNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;


procedure TfrmNengHaoList.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,'能耗列表');
end;

procedure TfrmNengHaoList.ToolButton4Click(Sender: TObject);
begin
  try
    frmNengHaoInput:=TfrmNengHaoInput.Create(Application);
    with frmNengHaoInput do
    begin
      FCYID:=Trim(Self.CDS_HZ.fieldbyname('NHID').AsString);
      if ShowModal=1 then
      begin
        Self.CDS_HZ.Locate('NHID',FCYID,[]);
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('NHDate').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHDate').Value;
          FieldByName('chejian').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('chejian').Value;
          FieldByName('NHType').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHType').Value;
          FieldByName('NHBiaoName').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHBiaoName').Value;
          FieldByName('Price').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Price').Value;
          FieldByName('Qty').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Qty').Value;
          FieldByName('Note').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Note').Value;
          //FieldByName('NHID').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHID').Value;
          Post;
        end;
      end;
    end;
  finally
    frmNengHaoInput.Free;
  end;
end;

procedure TfrmNengHaoList.ToolButton3Click(Sender: TObject);
begin
  try
    frmNengHaoInput:=TfrmNengHaoInput.Create(Application);
    with frmNengHaoInput do
    begin
      FCYID:='';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Append;
          FieldByName('NHDate').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHDate').Value;
          FieldByName('chejian').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('chejian').Value;
          FieldByName('NHType').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHType').Value;
          FieldByName('NHBiaoName').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHBiaoName').Value;
          FieldByName('Price').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Price').Value;
          FieldByName('Qty').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Qty').Value;
          FieldByName('Note').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('Note').Value;
          FieldByName('NHID').Value:=frmNengHaoInput.CDS_HZ.fieldbyname('NHID').Value;
          Post;
        end;
      end;
    end;
  finally
    frmNengHaoInput.Free;
  end;
end;

procedure TfrmNengHaoList.ToolButton5Click(Sender: TObject);
var
   fPrintFile,FConNoM,RPTName:string;

begin
  if CDS_HZ.IsEmpty then Exit;
  fPrintFile:= ExtractFilePath(Application.ExeName) + 'Report\能耗报表.rmf' ;
  if FileExists(fPrintFile) then
  begin
    RMVariables['begdate']:=BegDate.DateTime;
    RMVariables['enddate']:=enddate.DateTime;
    //RMVariables['printtime']:=Now;
    //RMVariables['printer']:=Trim(gUserName);
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end else
  begin
    Application.MessageBox(PChar('没有找'+ExtractFilePath(Application.ExeName)+RPTName),'提示',0);
  end;

end;

end.


