unit U_MYSC_CKTJ_MB;

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,DateUtils,
  cxButtonEdit, cxTextEdit, cxDropDownEdit, RM_e_Xls, BtnEdit, cxPC;

type
  TfrmMYSC_CKTJ_MB = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    Panetime: TPanel;
    cxTabControl1: TcxTabControl;
    Panel2: TPanel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Panel3: TPanel;
    Label1: TLabel;
    FBegDate: TDateTimePicker;
    FEndDate: TDateTimePicker;
    Panel4: TPanel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v1Column9: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column10: TcxGridDBColumn;
    v1Column4: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column1: TcxGridDBColumn;
    v1Column17: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    cxGrid2Level1: TcxGridLevel;
    v1LJQty: TcxGridDBColumn;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    RadioGroup1: TRadioGroup;
    Label34: TLabel;
    Label3: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    MYCode: TEdit;
    MYName: TEdit;
    Note: TEdit;
    GYLXName: TEdit;
    Label7: TLabel;
    MYColor: TEdit;
    Label8: TLabel;
    Edit1: TEdit;
    Label9: TLabel;
    Edit2: TEdit;
    cxTabControl2: TcxTabControl;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure v2Column3PropertiesEditValueChanged(Sender: TObject);
    procedure v2Column3CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure ToFactoryNameChange(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure RadioGroup1Click(Sender: TObject);
    procedure MYNameChange(Sender: TObject);
    procedure cxTabControl2Change(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();
    procedure DateSel(FMonth:string);

  public
    { Public declarations }
    RKFlag,FCYID:String;
    fType,FMX:string;
    PState:Integer;

  end;

var
  frmMYSC_CKTJ_MB: TfrmMYSC_CKTJ_MB;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_RTFun,U_ZdyAttachGYS, U_KHListSelJJ;

{$R *.dfm}
procedure TfrmMYSC_CKTJ_MB.DateSel(FMonth:string);
var FYMD:Tdatetime;
    FRQ:string;
    i:Integer;
begin
  FYMD:=StrToDate(Trim(FormatDateTime('yyyy',begdate.Date))+'-'+FMonth+'-01');
  FRQ:=FormatDateTime('dd',EndOfTheMonth(FYMD));
  cxTabControl2.Tabs.Clear;
  for i:=0 to StrToInt(FRQ)-1 do
  begin
    cxTabControl2.Tabs.Add(' '+IntToStr(i+1)+'日');
  end;
end;

procedure TfrmMYSC_CKTJ_MB.InitGrid();
var
  FYue,FRQ,FDate,Fdate1:string;
  FInt,Fint1:Integer;
begin
  FInt:=cxTabControl1.TabIndex+1;
  Fint1:=cxTabControl2.TabIndex+1;
  FYue:=Trim(IntToStr(FInt));
  FRQ:=Trim(IntToStr(Fint1));
  if FInt<10 then
  begin
    FYue:='0'+Trim(IntToStr(FInt));
  end;
  if FInt1<10 then
  begin
    FRQ:='0'+Trim(IntToStr(FInt1));
  end;
  FDate:=Trim(FormatDateTime('yyyy',begdate.Date))+'-'+FYue;
  Fdate1:=Trim(FormatDateTime('yyyy',begdate.Date))+'-'+FYue+'-'+FRQ;
  FBegDate.Date:=StrToDate(Fdate1);
  {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;}
  FEndDate.Date:=StrToDate(Fdate1);
  screen.Cursor:=crsqlwait;
  Panetime.Visible:=true;
  Panetime.Left:=(Width-Panetime.Width) div 2;
  Panetime.top:=(Height-Panetime.Height-200) div 2;
  application.ProcessMessages;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      if RadioGroup1.ItemIndex=0 then
      begin
        sql.Add(' exec  P_Qry_Ysk_CKMonth :FDate,:MYName,:GYLXName,:MYCode,:Note,:MYColor');
        Parameters.ParamByName('FDate').Value:=Trim(Fdate1);
        if Trim(MYName.Text)='' then
        Parameters.ParamByName('MYName').Value:=NULL
        else
        Parameters.ParamByName('MYName').Value:=Trim(MYName.Text);
        if Trim(GYLXName.Text)='' then
        Parameters.ParamByName('GYLXName').Value:=NULL
        else
        Parameters.ParamByName('GYLXName').Value:=Trim(GYLXName.Text);
        if Trim(MYCode.Text)='' then
        Parameters.ParamByName('MYCode').Value:=NULL
        else
        Parameters.ParamByName('MYCode').Value:=Trim(MYCode.Text);
        if Trim(Note.Text)='' then
        Parameters.ParamByName('Note').Value:=NULL
        else
        Parameters.ParamByName('Note').Value:=Trim(Note.Text);
        if Trim(MYColor.Text)='' then
        Parameters.ParamByName('MYColor').Value:=NULL
        else
        Parameters.ParamByName('MYColor').Value:=Trim(MYColor.Text);
      end else
      if RadioGroup1.ItemIndex=1 then
      begin
        sql.Add(' exec  P_Qry_Ysk_CK :FBegDate,:FEndDate,:MYName,:GYLXName,:MYCode,:Note,:MYColor');
        Parameters.ParamByName('FBegdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date));
        Parameters.ParamByName('FEndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1));
        if Trim(MYName.Text)='' then
        Parameters.ParamByName('MYName').Value:=NULL
        else
        Parameters.ParamByName('MYName').Value:=Trim(MYName.Text);
        if Trim(GYLXName.Text)='' then
        Parameters.ParamByName('GYLXName').Value:=NULL
        else
        Parameters.ParamByName('GYLXName').Value:=Trim(GYLXName.Text);
        if Trim(MYCode.Text)='' then
        Parameters.ParamByName('MYCode').Value:=NULL
        else
        Parameters.ParamByName('MYCode').Value:=Trim(MYCode.Text);
        if Trim(Note.Text)='' then
        Parameters.ParamByName('Note').Value:=NULL
        else
        Parameters.ParamByName('Note').Value:=Trim(Note.Text);
        if Trim(MYColor.Text)='' then
        Parameters.ParamByName('MYColor').Value:=NULL
        else
        Parameters.ParamByName('MYColor').Value:=Trim(MYColor.Text);
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;


procedure TfrmMYSC_CKTJ_MB.FormDestroy(Sender: TObject);
begin
  frmMYSC_CKTJ:=nil;
end;

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

procedure TfrmMYSC_CKTJ_MB.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款对账单YX1',Tv2,'财务管理');
  Close;
end;

procedure TfrmMYSC_CKTJ_MB.FormShow(Sender: TObject);
var
  fstr,FRQ:string;
begin
  ReadCxGrid('应收款对账单YX1',Tv2,'财务管理');
  if Trim(FMX)='' then
  begin
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
    fstr:=Trim(FormatDateTime('MM',begdate.Date));
    cxTabControl1.TabIndex:=StrToInt(fstr)-1;
    //DateSel(fstr);
    FRQ:=Trim(FormatDateTime('dd',now));
    cxTabControl2.TabIndex:=StrToInt(FRQ)-1;
    Panel3.Visible:=False;
  end
  else
  begin
    RadioGroup1.ItemIndex:=PState;
    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;
    FBegDate.Date:=SGetServerDate(ADOQueryCmd)-7;
    FEndDate.Date:=SGetServerDate(ADOQueryCmd);
    InitGrid();
  end;
end;

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

procedure TfrmMYSC_CKTJ_MB.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then Exit;
  SelExportData(Tv2,ADOQueryMain,self.Caption);
end;

procedure TfrmMYSC_CKTJ_MB.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=AlClient;
end;

procedure TfrmMYSC_CKTJ_MB.ToolButton3Click(Sender: TObject);
var
  filepath,fsj:string;
  mBegdate,mEnddate:string;
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['dtxz'] :=FormatDateTime('yyyy-mm-dd',date());
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=mbegdate;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;
  finally
  end;
end;

procedure TfrmMYSC_CKTJ_MB.v2Column3PropertiesEditValueChanged(
  Sender: TObject);
var
  mvalue:Boolean;
begin
  mvalue:=TcxCheckBox(Sender).EditValue;
  with CDS_HZ do
  begin
    Edit;
    FieldByName('DZFlag').Value:=mvalue;
  end;
  if mvalue=True then
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=1,DZTime=getdate(),DZPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end else
  begin
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('Update YF_Money_CR Set DZFlag=0,DZTime=getdate(),DZPerson='''+Trim(DName)+'''');
      sql.Add(' where YFID='''+Trim(CDS_HZ.fieldbyname('YFID').AsString)+'''');
      ExecSQL;
    end;
  end; 
end;

procedure TfrmMYSC_CKTJ_MB.v2Column3CustomDrawCell(
  Sender: TcxCustomGridTableView; ACanvas: TcxCanvas;
  AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id:Integer;
begin
  Id:=TV2.GetColumnByFieldName('DZFlag').Index;//;-TV1.GroupedItemCount;
  if Id<0 then Exit;
  if AViewInfo.GridRecord.Values[Id]=True then
  begin
    ACanvas.Brush.Color:=clRed;
  end;
end;

procedure TfrmMYSC_CKTJ_MB.ToFactoryNameChange(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 TfrmMYSC_CKTJ_MB.cxTabControl1Change(Sender: TObject);
var fstr:string;
begin
  fstr:=IntToStr(cxTabControl1.TabIndex+1);
  DateSel(fstr);
  cxTabControl2.TabIndex:=0;
end;

procedure TfrmMYSC_CKTJ_MB.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 TfrmMYSC_CKTJ_MB.MYNameChange(Sender: TObject);
begin
  InitGrid();  
end;

procedure TfrmMYSC_CKTJ_MB.cxTabControl2Change(Sender: TObject);
begin
  InitGrid();
end;

end.
