unit U_YFMXlistTaiTou;

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;

type
  TfrmYFMXlistTaiTou = 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;
    Label6: TLabel;
    begdate: TDateTimePicker;
    Enddate: TDateTimePicker;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    v2Price: TcxGridDBColumn;
    VMoney: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    v2factoryName: TcxGridDBColumn;
    v2MRPTspec: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    Label5: TLabel;
    ToolButton3: TToolButton;
    RMXLSExport1: TRMXLSExport;
    v2gangNo: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column5: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    factoryName: TBtnEditA;
    CheckBox1: TCheckBox;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    Label9: TLabel;
    v2Column6: TcxGridDBColumn;
    ComTaiTou: TBtnEditA;
    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 ComTaiTouChange(Sender: TObject);
    procedure ComTaiTouBtnClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

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

  end;

var
  frmYFMXlistTaiTou: TfrmYFMXlistTaiTou;

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

{$R *.dfm}

procedure TfrmYFMXlistTaiTou.InitGrid();
begin
  IF trim(factoryName.Text)='' then
  begin
    application.MessageBox('请选择供应商！','提示信息',MB_ICONERROR);
    exit;
  end;
  {IF trim(ComTaiTou.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_mx_TaiTou :begdate,:enddate,:factoryName,:ComTaiTou');
      if CheckBox1.Checked=True then
      begin
        Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
        Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      end else
      begin
        Parameters.ParamByName('begdate').Value:='2000-01-01';
        Parameters.ParamByName('Enddate').Value:='2500-01-01';
      end;
      Parameters.ParamByName('factoryName').Value:=Trim(factoryName.Text);
      Parameters.ParamByName('ComTaiTou').Value:=Trim(ComTaiTou.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 TfrmYFMXlistTaiTou.FormDestroy(Sender: TObject);
begin
  frmYFMXlistTaiTou:=nil;
end;

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

procedure TfrmYFMXlistTaiTou.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理');
  Close;
end;
procedure TfrmYFMXlistTaiTou.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption,Tv2,'财务管理');
  if Trim(FMX)='' then
  begin
    Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
    begdate.DateTime:=SGetServerDateMBeg(ADOQueryTemp);
  end else
  begin
    InitGrid();
  end;
end;

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

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

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

procedure TfrmYFMXlistTaiTou.ToolButton3Click(Sender: TObject);
var
  filepath:string;
  mBegdate,mEnddate:string;
begin
  mbegdate:=FormatDateTime('yyyy-MM-dd',begdate.Date);
  menddate:=FormatDateTime('yyyy-MM-dd',enddate.Date);
  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['ComTaiTou'] :=ComTaiTou.Text;
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=FormatDateTime('yymmdd',begDate.Date)+'-'+FormatDateTime('yymmdd',enddate.Date);
    CDS_HZ.First;
    RmVariables['QCNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsFloat - CDS_HZ.fieldbyname('YFMoney').AsFloat + CDS_HZ.fieldbyname('FKMoney').AsFloat ;
    CDS_HZ.Last;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYFMXlistTaiTou.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 TfrmYFMXlistTaiTou.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 TfrmYFMXlistTaiTou.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 TfrmYFMXlistTaiTou.ComTaiTouChange(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 TfrmYFMXlistTaiTou.ComTaiTouBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='FHTaiTou';
      flagname:='公司抬头';
      if ShowModal=1 then
      begin
        Self.ComTaiTou.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
