unit U_KHFeeList;

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, BtnEdit, Menus, Buttons, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator, dxBarBuiltInMenu;

type
  TfrmKHFeeList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    TBFind: TToolButton;
    ADOQueryMain: TADOQuery;
    TBExport: TToolButton;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Column1: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    Label8: TLabel;
    FactoryNo: TEdit;
    Label9: TLabel;
    FactoryName: TEdit;
    v2Column3: TcxGridDBColumn;
    Label1: TLabel;
    FKType: TComboBox;
    v2Column2: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    v2Column4: TcxGridDBColumn;
    Label3: TLabel;
    ConNo: TEdit;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    ToolButton1: TToolButton;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    Label4: TLabel;
    YFName: TEdit;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBFindClick(Sender: TObject);
    procedure FactoryName10Change(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure FKTypeChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N2Click(Sender: TObject);
  private
    { Private declarations }
    canshu1: string;
    procedure InitGrid();

  public
    { Public declarations }
    FBZType: string;
  end;

var
  frmKHFeeList: TfrmKHFeeList;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_RTFun, U_BankMoneyKHInPut, U_FjList;

{$R *.dfm}

procedure TfrmKHFeeList.InitGrid();
var
  fsj: string;
begin
  fsj := SGetWhereFinds(Panel1, 1, 2);
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add('SELECT AA.* FROM(');
      sql.Add(' select CRTime,A.Note,A.YFID,A.JSMoney,A.FKType ');
      sql.Add(',A.FactoryNo,A.CRType,A.CRFlag,A.YFName,A.ConNo,A.ChkStatusP');
      sql.Add(',FactoryName=(select KHNameJC from KH_Main KM where KM.KHNO=A.FactoryNo and Valid=''Y'' )');
      sql.Add(',SDQty=(select Count(*) from TP_File TF where TF.WBID=A.YFID and TF.TFType=''客户费用管理''  )');
      sql.Add(',Case when isnull(A.BZType,'''')=''￥'' then JSMoney else Null end as MoneyRMB ');
      sql.Add(',Case when isnull(A.BZType,'''')=''$'' then JSMoney else Null end as MoneyMJ ');
      sql.Add('  from YS_Money_CR A');
      sql.Add(' left join KH_Sub_Bank B on A.BKNO=B.BKNO');
      sql.Add(' left join KH_Main C on B.KHMainId=C.KHMainId');
      sql.Add(' where 1=1 ');
      sql.Add(' and isnull(A.BZType,'''')=''' + Trim(FBZType) + '''');
      if cxTabControl1.TabIndex = 1 then
      begin
        sql.Add(' and isnull(FKStatus,'''')=''已付款'' ');
        sql.Add('and A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
      end
      else
      begin
//        sql.Add('and A.CRTime>=''2000-01-01'' ');
//        sql.Add(' and CRTime<''2500-01-01'' ');
        sql.Add('and A.CRTime>=''' + FormatDateTime('yyyy-MM-dd', begdate.DateTime) + ''' ');
        sql.Add(' and CRTime<''' + FormatDateTime('yyyy-MM-dd', Enddate.DateTime + 1) + ''' ');
        sql.Add(' and isnull(FKStatus,'''')='''' ');
      end;
      sql.Add(' and A.CRType=''客户费用管理'' and FKType in(''自付'',''本公司代付'') )AA where 1=1 ');

      SQL.Add(fsj);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    TBFind.Click;
  end;
end;

procedure TfrmKHFeeList.FormDestroy(Sender: TObject);
begin
  frmKHFeeList := nil;
end;

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

procedure TfrmKHFeeList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(Copy(Self.Caption, 1, Length(Self.Caption) - 1) + 'list', Tv2, '财务管理');
  Close;
end;

procedure TfrmKHFeeList.FormShow(Sender: TObject);
begin
  Enddate.DateTime := SGetServerDate(ADOQueryTemp);
  begdate.DateTime := StrToDate(FormatDateTime('yyyy', Enddate.Date) + '-01-01');
  ReadCxGrid(Copy(Self.Caption, 1, Length(Self.Caption) - 1) + 'list', Tv2, '财务管理');
  InitGrid();
end;

procedure TfrmKHFeeList.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();

end;

procedure TfrmKHFeeList.TBFindClick(Sender: TObject);
begin
  if ADOQueryMain.Active = False then
    Exit;
  SDofilter(ADOQueryMain, SGetFilters(Panel1, 1, 2));
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
end;

procedure TfrmKHFeeList.FactoryName10Change(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmKHFeeList.TBExportClick(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  SelExportData(Tv2, ADOQueryMain, '客户费用列表');
end;

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

procedure TfrmKHFeeList.Tv2CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
var
  FName: string;
begin
  FName := Tv2.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName) = 'SDQty' then
  begin
    if CDS_HZ.IsEmpty then
      Exit;
    try
      frmFjList := TfrmFjList.Create(Application);
      with frmFjList do
      begin
        fkeyNO := Trim(CDS_HZ.fieldbyname('YFID').AsString);
        fType := '客户费用管理';
        fstatus := 0;
        if ShowModal = 1 then
        begin

        end;
      end;
    finally
      frmFjList.Free;
    end;
  end;
end;

procedure TfrmKHFeeList.FKTypeChange(Sender: TObject);
begin
  TBFind.Click;
end;

procedure TfrmKHFeeList.ToolButton1Click(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmKHFeeList.N1Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, True);
end;

procedure TfrmKHFeeList.N2Click(Sender: TObject);
begin
  SelOKNo(CDS_HZ, False);
end;

end.

