unit U_RankingList;

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, cxPC, cxDropDownEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxBarBuiltInMenu,  
  dxDateRanges, U_BaseList, Vcl.Menus, Vcl.Clipbrd;

type
  TfrmRankingList = class(TfrmBaseList)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    GPM_1: TcxGridPopupMenu;
    DS_1: TDataSource;
    CDS_1: TClientDataSet;
    PM_1: TPopupMenu;
    N1: TMenuItem;
    ToolButton5: TToolButton;
    N2: TMenuItem;
    N3: TMenuItem;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    Pnl_F2: TPanel;
    Label1: TLabel;
    Label18: TLabel;
    BegDate2: TDateTimePicker;
    EndDate2: TDateTimePicker;
    DateTimePicker3: TDateTimePicker;
    DateTimePicker4: TDateTimePicker;
    Edit10: TEdit;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    cxTabSheet2: TcxTabSheet;
    Pnl_F1: TPanel;
    Label14: TLabel;
    Label20: TLabel;
    begdate: TDateTimePicker;
    EndDate: TDateTimePicker;
    begtime: TDateTimePicker;
    endTime: TDateTimePicker;
    F_Name: TEdit;
    GPM_2: TcxGridPopupMenu;
    CDS_2: TClientDataSet;
    DS_2: TDataSource;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    Tv1Column28: TcxGridDBColumn;
    Tv1Column29: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    TV2Column1: TcxGridDBColumn;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    TV3Column1: TcxGridDBColumn;
    TV3Column2: TcxGridDBColumn;
    cxGrid4: TcxGrid;
    TV4: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    TV4Column1: TcxGridDBColumn;
    Label3: TLabel;
    GPM_3: TcxGridPopupMenu;
    CDS_3: TClientDataSet;
    DS_3: TDataSource;
    DS_4: TDataSource;
    CDS_4: TClientDataSet;
    GPM_4: TcxGridPopupMenu;
    ADO_4: TADOQuery;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
    procedure OppCoNameKeyPress(Sender: TObject; var Key: Char);
    procedure N1Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure TV4FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
    procedure cxPageControl1Change(Sender: TObject);
    procedure Edit10Change(Sender: TObject);
    procedure F_NameChange(Sender: TObject);
  private
    procedure InitGrid();
    procedure SetStatus();
  public
    FAuthority: string;
  end;

implementation

uses
  U_DataLink, U_RTFun, U_ReceivableInput, U_ReceivableEdit, U_ZDYHelp;

{$R *.dfm}
procedure TfrmRankingList.SetStatus();
begin

end;

procedure TfrmRankingList.InitGrid();
begin

  case cxPageControl1.ActivePageIndex of
    0:
      begin
        with ADOQueryMain do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select OppCoName,Amount=sum(A.Amount) from Finance_Flow A ');
          sql.Add(' where A.FFType=''应收款登记''  and isnull(status,''0'')=''9'' ');
          sql.Add(' and A.FFTime>=''' + FormatDateTime('yyyy-MM-dd', begdate2.DateTime) + ''' ');
          sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate2.DateTime + 1) + ''' ');
          sql.Add(' group by OppCoName       ');
          Open;
        end;
        SCreateCDS(ADOQueryMain, CDS_2);
        SInitCDSData(ADOQueryMain, CDS_2);
        TV2.DataController.Filter.Clear;
      end;
    1:
      begin
        with ADO_4 do
        begin
          Close;
          SQL.Clear;
          sql.Add(' select F_Name,Amount=sum(A.Amount),Qty=sum(A.Qty) from Finance_Flow A ');
          sql.Add(' where A.FFType=''应收款登记'' and isnull(status,''0'')=''9'' ');
          sql.Add(' and A.FFTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
          sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
          sql.Add(' group by F_Name ');
          Open;
        end;
        SCreateCDS(ADO_4, CDS_4);
        SInitCDSData(ADO_4, CDS_4);
        TV4.DataController.Filter.Clear;
      end;
  end;

end;

procedure TfrmRankingList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_1, true)
end;

procedure TfrmRankingList.N2Click(Sender: TObject);
begin
  Clipboard.SetTextBuf(PChar(Trim(CDS_1.fieldbyname(TV1.Controller.FocusedColumn.DataBinding.FilterFieldName).AsString)));
end;

procedure TfrmRankingList.N3Click(Sender: TObject);
begin
  SelOKNo(CDS_1, false)

end;

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

procedure TfrmRankingList.TBCloseClick(Sender: TObject);
begin

  Close;
end;

procedure TfrmRankingList.FormShow(Sender: TObject);
begin
  inherited;

  ReadCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '财务管理');
  ReadCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '财务管理');
  ReadCxGrid(trim(Self.Caption) + 'Tv3', Tv3, '财务管理');
  ReadCxGrid(trim(Self.Caption) + 'Tv4', Tv4, '财务管理');
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := strtodate(FormatDateTime('yyyy', Enddate.DateTime) + '-01-01');
  begdate2.DateTime := begdate.DateTime;
  Enddate2.DateTime := Enddate.DateTime;
  SetStatus();
  InitGrid();
end;

procedure TfrmRankingList.TBRafreshClick(Sender: TObject);
begin
//  begdate.SetFocus;

  InitGrid();

end;

procedure TfrmRankingList.cxGridDBColumn5PropertiesEditValueChanged(Sender: TObject);
var
  mvalue, FFieldName: string;
  FPS, FQty, FPrice, FQTFee, FMoney, FZKMoney: Double;
begin
  FFieldName := Trim(Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName);
  mvalue := TcxTextEdit(Sender).EditingText;
  if Trim(mvalue) = '' then
  begin
    mvalue := '0';
  end;

  with CDS_1 do
  begin
    Edit;
    FieldByName(FFieldName).Value := Trim(mvalue);
    Post;
    FPS := CDS_1.fieldbyname('PS').AsFloat;
    FQty := CDS_1.fieldbyname('Qty').AsFloat;
    FQTFee := CDS_1.fieldbyname('QTFee').AsFloat;
    FPrice := CDS_1.fieldbyname('Price').AsFloat;
    FMoney := CDS_1.fieldbyname('Money').AsFloat;

    FZKMoney := CDS_1.fieldbyname('ZKMoney').AsFloat;
  end;

  if Trim(FFieldName) <> 'Money' then
  begin
    FMoney := FQty * FPrice + FQTFee - FZKMoney;
  end;
  with CDS_1 do
  begin
    Edit;
    FieldByName('Money').Value := FMoney;
    Post;
  end;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('update Finance_Flow set ' + FFieldName + '=' + Trim(mvalue));
    if Trim(FFieldName) <> 'Money' then
      sql.Add(' ,Money=' + FloatToStr(FMoney));
    sql.Add('where FFID=' + quotedstr(Trim(CDS_1.fieldbyname('FFID').AsString)));
    ExecSQL;
  end;

  Tv1.Controller.EditingController.ShowEdit();
end;

procedure TfrmRankingList.cxPageControl1Change(Sender: TObject);
begin

  InitGrid();
end;

procedure TfrmRankingList.F_NameChange(Sender: TObject);
begin
  if ADO_4.Active then
  begin
    SDofilter(ADO_4, SGetHintFilters(Pnl_F1, 1, 2));
    SCreateCDS(ADO_4, CDS_4);
    SInitCDSData(ADO_4, CDS_4);
  end;

end;

procedure TfrmRankingList.Edit10Change(Sender: TObject);
begin

  if ADOQueryMain.Active then
  begin
    SDofilter(ADOQueryMain, SGetHintFilters(Pnl_F2, 1, 2));
    SCreateCDS(ADOQueryMain, CDS_2);
    SInitCDSData(ADOQueryMain, CDS_2);
  end;

end;

procedure TfrmRankingList.ToolButton5Click(Sender: TObject);
begin
  WriteCxGrid(trim(Self.Caption) + 'Tv1', Tv1, '财务管理');
  WriteCxGrid(trim(Self.Caption) + 'Tv2', Tv2, '财务管理');
  WriteCxGrid(trim(Self.Caption) + 'Tv3', Tv3, '财务管理');
  WriteCxGrid(trim(Self.Caption) + 'Tv4', Tv4, '财务管理');
end;

procedure TfrmRankingList.TV2FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select OppCoName,F_Name,Amount=sum(A.Amount),Qty=sum(A.Qty) from Finance_Flow A ');
    sql.Add(' where A.FFType=''应收款登记''  and isnull(status,''0'')=''9'' ');
    sql.Add(' and A.FFTime>=''' + FormatDateTime('yyyy-MM-dd', begdate2.DateTime) + ''' ');
    sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate2.DateTime + 1) + ''' ');
    sql.Add(' and OppCoName=''' + CDS_2.FieldByName('OppCoName').AsString + ''' ');
    sql.Add(' group by OppCoName,F_Name ');
    Open;
  end;
  SCreateCDS(ADOQueryTemp, CDS_3);
  SInitCDSData(ADOQueryTemp, CDS_3);

end;

procedure TfrmRankingList.TV4FocusedRecordChanged(Sender: TcxCustomGridTableView; APrevFocusedRecord, AFocusedRecord: TcxCustomGridRecord; ANewItemRecordFocusingChanged: Boolean);
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add(' select OppCoName,Amount=sum(A.Amount),Qty=sum(A.Qty) from Finance_Flow A ');
    sql.Add(' where A.FFType=''应收款登记''  and isnull(status,''0'')=''9'' ');
    sql.Add(' and A.FFTime>=''' + FormatDateTime('yyyy-MM-dd', begdate2.DateTime) + ''' ');
    sql.Add(' and FFTime<''' + FormatDateTime('yyyy-MM-dd', Enddate2.DateTime + 1) + ''' ');
    sql.Add(' and F_Name=''' + CDS_4.FieldByName('F_Name').AsString + ''' ');
    sql.Add(' group by OppCoName ');
//    ShowMessage(SQL.Text);
    Open;

  end;
  SCreateCDS(ADOQueryTemp, CDS_1);
  SInitCDSData(ADOQueryTemp, CDS_1);
end;

procedure TfrmRankingList.OppCoNameKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

end.

