unit U_YFMXlist;

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
  TfrmYFMXlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxCRTime: TcxGridDBColumn;
    cxMoneyYe: TcxGridDBColumn;
    cxQty: TcxGridDBColumn;
    cxQtyUnit: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    Label2: TLabel;
    begdate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    v2MRPTspec: TcxGridDBColumn;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2gangNo: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column2: TcxGridDBColumn;
    factoryName: TBtnEditA;
    v2Column3: TcxGridDBColumn;
    cxTabControl1: 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 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);
  private
    { Private declarations }

    procedure InitGrid();

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

  end;

var
  frmYFMXlist: TfrmYFMXlist;

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

{$R *.dfm}

procedure TfrmYFMXlist.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 trim(factoryName.Text)='' then
  begin
    //application.MessageBox('请选择供应商！','提示信息',MB_ICONERROR);
    exit;
  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;
      sql.Add(' exec  P_Qry_Yfk_Month :FDate,:factoryName');
      Parameters.ParamByName('FDate').Value:=Trim(FDate);
      Parameters.ParamByName('factoryName').Value:=Trim(factoryName.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
    Panetime.Visible:=false;
    screen.Cursor:=crdefault;
  end;
end;


procedure TfrmYFMXlist.FormDestroy(Sender: TObject);
begin
  frmYFMXlist:=nil;
end;

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

procedure TfrmYFMXlist.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理');
  Close;
end;
procedure TfrmYFMXlist.FormShow(Sender: TObject);
var
  fstr:string;
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理');
  if Trim(FMX)='' then
  begin
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
    fstr:=Trim(FormatDateTime('MM',begdate.Date));
    cxTabControl1.TabIndex:=StrToInt(fstr)-1;
  end else
  begin
    InitGrid();
  end;
end;

procedure TfrmYFMXlist.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

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

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

procedure TfrmYFMXlist.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\应付款对账单.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 TfrmYFMXlist.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachGYS:=TfrmZdyAttachGYS.Create(Application);
    with frmZdyAttachGYS do
    begin
      if ShowModal=1 then
      begin
        Self.factoryName.Text:=Trim(CDS_HZ.fieldbyname('ZdyNameZ').AsString);
      end;
    end;
  finally
    frmZdyAttachGYS.Free;
  end;
end;

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

end.
