unit U_YSMXlistMonDay;

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

type
  TfrmYSMXlistMonDay = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxCRTime: TcxGridDBColumn;
    cxMoneyYe: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2gangNo: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column2: TcxGridDBColumn;
    cxTabControl1: TcxTabControl;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    Panel2: TPanel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    Panel3: TPanel;
    Label1: TLabel;
    Label3: TLabel;
    FBegDate: TDateTimePicker;
    FEndDate: TDateTimePicker;
    Panel4: TPanel;
    Label5: TLabel;
    factoryName: TBtnEditA;
    RadioGroup1: TRadioGroup;
    LabBZ: TLabel;
    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 factoryNameBtnClick(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);
  private
    { Private declarations }

    procedure InitGrid();

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

  end;

var
  frmYSMXlistMonDay: TfrmYSMXlistMonDay;

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

{$R *.dfm}

procedure TfrmYSMXlistMonDay.InitGrid();
var
  FYue,FDate:string;
  FInt:Integer;
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;
  if FMX='' then
  begin
    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;
    IF trim(factoryName.Text)='' then
    begin
      //application.MessageBox('请选择供应商！','提示信息',MB_ICONERROR);
      exit;
    end;
  end;

  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_Month :FDate,:factoryNo,:BZ');
        Parameters.ParamByName('FDate').Value:=Trim(FDate);
        Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
        Parameters.ParamByName('BZ').Value:=Trim(FBZ);
      end else
      if RadioGroup1.ItemIndex=1 then
      begin
        sql.Add(' exec  P_Qry_YSk_Day :FBegDate,:FEndDate,:factoryNo,:BZ');
        Parameters.ParamByName('FBegdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FBegDate.Date));
        Parameters.ParamByName('FEndDate').Value:=Trim(FormatDateTime('yyyy-MM-dd',FEndDate.Date+1));
        Parameters.ParamByName('factoryNo').Value:=Trim(factoryName.TxtCode);
        Parameters.ParamByName('BZ').Value:=Trim(FBZ);
      end;
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;


procedure TfrmYSMXlistMonDay.FormDestroy(Sender: TObject);
begin
  frmYSMXlistMonDay:=nil;
end;

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

procedure TfrmYSMXlistMonDay.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('应收款对账单YX1',Tv2,'财务管理');
  Close;
end;
procedure TfrmYSMXlistMonDay.FormShow(Sender: TObject);
var
  fstr:string;
begin
  ReadCxGrid('应收款对账单YX1',Tv2,'财务管理');
  LabBZ.Caption:=Trim(FBZ);
  if Trim(FMX)='' then
  begin
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
    fstr:=Trim(FormatDateTime('MM',begdate.Date));
    cxTabControl1.TabIndex:=StrToInt(fstr)-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;
    InitGrid();
  end;
end;

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

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

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

procedure TfrmYSMXlistMonDay.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 TfrmYSMXlistMonDay.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmKHListSelJJ:=TfrmKHListSelJJ.Create(Application);
    with frmKHListSelJJ do
    begin
      frmKHListSelJJ.canshu2:='高权限';
      if ShowModal=1 then
      begin
        Self.factoryName.Text:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNameJC').AsString);
        Self.factoryName.TxtCode:=Trim(frmKHListSelJJ.CDS_HZ.fieldbyname('KHNO').AsString);
      end;
    end;
  finally
    frmKHListSelJJ.Free;
  end;
  begdate.SetFocus;
  InitGrid();
end;

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

procedure TfrmYSMXlistMonDay.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;

end.
