unit U_CKCR_TJCX;

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, cxGridBandedTableView,
  cxGridDBBandedTableView, cxLookAndFeels, cxLookAndFeelPainters,
  dxBarBuiltInMenu, cxNavigator,dxCore,ActiveX;

type
  TfrmCKCR_TJCX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    ADOQueryMain: TADOQuery;
    CDS_HZ: TClientDataSet;
    ToolButton3: TToolButton;
    cxTabControl1: TcxTabControl;
    Panel2: TPanel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Panel4: TPanel;
    RadioGroup1: TRadioGroup;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1CRTime: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Qty: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    Panel3: TPanel;
    Label5: TLabel;
    FBegDate: TDateTimePicker;
    FEndDate: TDateTimePicker;
    CheckBox1: TCheckBox;
    ADOQuery1: TADOQuery;
    v2CRType: TcxGridDBColumn;
    v2MYName: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure FactoryNameGYSChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure Tv2CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column6HeaderClick(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
  public
    fDParameters1:string;
    { Public declarations }
    RKFlag,FCYID:String;
    FDate1:TDateTime;
  end;

var
  frmCKCR_TJCX: TfrmCKCR_TJCX;

implementation
uses
  U_DataLink,U_RTFun,U_Fun,U_ZDYHelp,U_MYSC_CKTJ;

{$R *.dfm}

procedure TfrmCKCR_TJCX.InitGrid();
var
  FYue,FDate:string;
  FInt:Integer;
  FHuiLv:Double;
begin
  FInt:=cxTabControl1.TabIndex+1;
  FYue:=Trim(IntToStr(FInt));
  if FInt<10 then
  begin
    FYue:='0'+Trim(IntToStr(FInt));
  end;
  FDate:=Trim(FormatDateTime('yyyy',begdate.Date))+'-'+FYue;
  FBegDate.Date:=StrToDate(FDate+'-01');
  if FInt=12 then
  FEndDate.Date:=StrToDate(Trim(FormatDateTime('yyyy',begdate.Date))+'-12-31')
  else
  FEndDate.Date:=StrToDate(Trim(FormatDateTime('yyyy',begdate.Date))+'-'+Trim(IntToStr(FInt+1))+'-01')-1;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      if RadioGroup1.ItemIndex=0 then
      begin
        sql.Add('select A.CRTime,A.MYName,A.CRType,Sum(GangQty) GangQty,Sum(PS) PS');
        sql.Add(',Sum(KgQty) KgQty,Sum(Qty) Qty');
        sql.Add(' from CK_MYSC_CR A');
        sql.Add(' inner join ML_Manage B on A.MLID=B.MLID ');
        sql.Add(' where convert(varchar(7),A.CRTime,120)='''+Trim(FDate)+'''');
        sql.Add(' group by A.CRTime,A.MYName,A.CRType order by A.CRTime');
      end
      else
      begin
        sql.Add('select A.CRTime,A.MYName,A.CRType,Sum(GangQty) GangQty,Sum(PS) PS');
        sql.Add(',Sum(KgQty) KgQty,Sum(Qty) Qty');
        sql.Add(' from CK_MYSC_CR A');
        sql.Add(' inner join ML_Manage B on A.MLID=B.MLID ');
        if CheckBox1.Checked=false then
        begin
          sql.Add('where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',FDate1))+'''');
          sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',FDate1+1))+'''');
        end
        else
        begin
          sql.Add(' where A.CRTime>='''+Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date))+'''');
          sql.Add(' and A.CRTime<'''+Trim(FormatDateTime('yyyy-MM-dd',FendDate.Date+1))+'''');
        end;
        sql.Add(' group by A.CRTime,A.MYName,A.CRType order by A.CRTime');
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmCKCR_TJCX.FormDestroy(Sender: TObject);
begin
  frmCKCR_TJCX:=nil;
end;

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

procedure TfrmCKCR_TJCX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款统计报表YX',Tv2,'财务管理');
  Close;
end;
procedure TfrmCKCR_TJCX.FormShow(Sender: TObject);
var
  fstr:string;
begin
  ReadCxGrid('应收款统计报表YX',Tv2,'财务管理');
  begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  Fbegdate.DateTime:=SGetServerDate(ADOQueryTemp);
  FEndDate.Date:=Fbegdate.DateTime;
  FDate1:=SGetServerDate(ADOQuery1);
  fstr:=Trim(FormatDateTime('MM',begdate.Date));
  cxTabControl1.TabIndex:=StrToInt(fstr)-1;
  Panel3.Visible:=False;
 // InitGrid();
end;

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

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

procedure TfrmCKCR_TJCX.FactoryNameGYSChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmCKCR_TJCX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  //SelExportData(Tv2,ADOQueryMain,'应收款收款统计列表');

  TcxGridToExcel('应收款收款统计列表',cxGrid2);
end;

procedure TfrmCKCR_TJCX.Tv2CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  try
    frmMYSC_CKTJ:=TfrmMYSC_CKTJ.Create(Application);
    with frmMYSC_CKTJ do
    begin
      FMX:='99';
      PState:=Self.RadioGroup1.ItemIndex;
      if Self.RadioGroup1.ItemIndex=0 then
      begin
        frmMYSC_CKTJ.begdate.DateTime:=Self.begdate.DateTime;
        frmMYSC_CKTJ.cxTabControl1.TabIndex:=Self.cxTabControl1.TabIndex;
        //frmMYSC_CKTJ.factoryName.Text:=Self.CDS_HZ.fieldbyname('factoryName').AsString;
        frmMYSC_CKTJ.Fbegdate.DateTime:=Self.Fbegdate.DateTime;
        frmMYSC_CKTJ.FEnddate.DateTime:=Self.FEnddate.DateTime;
      end else
      if Self.RadioGroup1.ItemIndex=1 then
      begin
        frmMYSC_CKTJ.begdate.DateTime:=Self.begdate.DateTime;
        frmMYSC_CKTJ.Fbegdate.DateTime:=Self.Fbegdate.DateTime;
        frmMYSC_CKTJ.FEnddate.DateTime:=Self.FEnddate.DateTime;
        //frmMYSC_CKTJ.factoryName.Text:=Self.CDS_HZ.fieldbyname('factoryName').AsString;
      end;
      //frmMYSC_CKTJ.factoryName.TxtCode:=Self.CDS_HZ.fieldbyname('factoryNo').AsString;
      frmMYSC_CKTJ.Align:=alClient;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmMYSC_CKTJ.Free;
  end;
end;

procedure TfrmCKCR_TJCX.ToolButton3Click(Sender: TObject);
var
  filepath,fsj:string;
  mBegdate,mEnddate:string;
  FName,FNameSX,FNameJX:String;
  i:Integer;
begin
  fsj:=cxTabControl1.Tabs[cxTabControl1.TabIndex].Caption;
  mbegdate:=FormatDateTime('yyyy',begdate.Date)+'年'+Trim(Copy(fsj,3,Length(fsj)-2))+'份';
  if CDS_HZ.IsEmpty then exit;
  try
    filepath:=ExtractFilePath(Application.ExeName) + 'report\应收款统计报表YX.rmf';
    if not FileExists(Pchar(filepath))  then
    begin
      application.MessageBox(pchar('文件['+filepath+']不存在！'),'提示信息',MB_IConError);
      exit;
    end;
    RmVariables['begDate'] :=mbegdate;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally

  end;

end;

procedure TfrmCKCR_TJCX.v2Column6HeaderClick(Sender: TObject);
var
  FName,FNameSX,FNameJX:String;
begin
  FName:=TcxGridDBColumn(Sender).DataBinding.FilterFieldName;
  FNameSX:=Trim(FName)+'-1';
  FNameJX:=Trim(FName)+'-2';
  CDS_HZ.AddIndex(FNameSX, FName, []);             { 正序 }
  CDS_HZ.AddIndex(FNameJX, FName, [ixDescending]); { 倒序 }
  if TcxGridDBColumn(Sender).SortOrder=soDescending then
  begin
    CDS_HZ.IndexName:=FNameSX;
  end else
  if TcxGridDBColumn(Sender).SortOrder=soAscending then
  begin
    CDS_HZ.IndexName:=FNameJX;
  end;

end;

procedure TfrmCKCR_TJCX.RadioGroup1Click(Sender: TObject);
begin
  if RadioGroup1.ItemIndex=0 then
  begin
    Panel2.Visible:=True;
    Panel3.Visible:=False;
    cxTabControl1.Visible:=True;
  end else
  if RadioGroup1.ItemIndex=1 then
  begin
    Panel2.Visible:=False;
    Panel3.Visible:=True;
    cxTabControl1.Visible:=False;
  end;
  InitGrid();
end;

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

end.
