unit U_YSMXlistTaiTou;

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

type
  TfrmYSMXListTaiTou = 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;
    v2Column1: TcxGridDBColumn;
    v2note: TcxGridDBColumn;
    Panetime: TPanel;
    v2Column2: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    factoryName: TBtnEditA;
    v2Column3: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v2Column5: TcxGridDBColumn;
    Label9: TLabel;
    ComTaiTou: TBtnEditA;
    v2Column6: 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 ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure factoryNameBtnClick(Sender: TObject);
    procedure v2Column5CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure v2Column5PropertiesEditValueChanged(Sender: TObject);
    procedure ComTaiTouBtnClick(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

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

var
  frmYSMXListTaiTou: TfrmYSMXListTaiTou;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderNewList,U_RTFun,
  U_ZdyAttachment;

{$R *.dfm}




procedure TfrmYSMXListTaiTou.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;}
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' exec  P_Qry_Ysk_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 TfrmYSMXListTaiTou.FormDestroy(Sender: TObject);
begin
  frmYSMXListTaiTou:=nil;
end;

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

procedure TfrmYSMXListTaiTou.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption,Tv2,'财务管理');
  Close;
end;
procedure TfrmYSMXListTaiTou.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 TfrmYSMXListTaiTou.TBRafreshClick(Sender: TObject);
begin
  begdate.SetFocus;
  InitGrid();
end;

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

procedure TfrmYSMXListTaiTou.FormCreate(Sender: TObject);
begin

  {with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select distinct(ComTaiTou) from YF_Money_CR where CRFlag=''应收收'' and ISNULL(ComTaiTou,'''')<>'''' ');
    Open;
  end;
  cxTabControl1.Tabs.Clear;
  with ADOQueryTemp do
  begin
    First;
    while not Eof do
    begin
      cxTabControl1.Tabs.Add(ADOQueryTemp.fieldbyname('ComTaiTou').AsString);
      Next;
    end;
  end;}
end;

procedure TfrmYSMXListTaiTou.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('YSMoney').AsFloat + CDS_HZ.fieldbyname('SKMoney').AsFloat ;
    CDS_HZ.Last;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    //RmVariables['ComTaiTou'] :=CDS_HZ.fieldbyname('ComTaiTou').AsString;
    //RmVariables['ConNo'] :=CDS_HZ.fieldbyname('ConNo').AsString;
    //RmVariables['KHConNo'] :=CDS_HZ.fieldbyname('KHConNo').AsString;
    RM1.LoadFromFile(filepath);
    RM1.ShowReport;

  finally

  end;

end;

procedure TfrmYSMXListTaiTou.cxTabControl1Change(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmYSMXListTaiTou.factoryNameBtnClick(Sender: TObject);
begin
  try
    frmZdyAttachment:=TfrmZdyAttachment.Create(Application);
    with frmZdyAttachment do
    begin
      if ShowModal=1 then
      begin
        Self.factoryName.Text:=Trim(frmZdyAttachment.CDS_HZ.fieldbyname('ZdyNameZ').AsString);
      end;
    end;
  finally
    frmZdyAttachment.Free;
  end;
end;

procedure TfrmYSMXListTaiTou.v2Column5CustomDrawCell(
  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 TfrmYSMXListTaiTou.v2Column5PropertiesEditValueChanged(
  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 TfrmYSMXListTaiTou.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.
