unit U_YSMXlist;

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, cxLookAndFeels,
  cxLookAndFeelPainters, cxNavigator;

type
  TfrmYSMXlist = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    Label1: TLabel;
    MPRTCodeName: TEdit;
    ADOQueryMain: TADOQuery;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    Label3: TLabel;
    inoutNO: TEdit;
    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;
    VOrderNo: TcxGridDBColumn;
    v2BBMoney: TcxGridDBColumn;
    v2MPRTCodeName: TcxGridDBColumn;
    Label4: TLabel;
    OrdDefNote3: TComboBox;
    v2factoryName: TcxGridDBColumn;
    v2MRPTspec: TcxGridDBColumn;
    v2YFName: TcxGridDBColumn;
    Label5: TLabel;
    factoryName: TEdit;
    v2Column1: TcxGridDBColumn;
    v2SMoney: TcxGridDBColumn;
    v2FHAddress: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2RollNum: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    ToolButton3: TToolButton;
    v2Column5: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    CheckBox1: TCheckBox;
    v2Column7: TcxGridDBColumn;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    CheckBox2: TCheckBox;
    ToolButton6: TToolButton;
    RMDBDataSet1: TRMDBDataSet;
    RMXLSExport1: TRMXLSExport;
    RMGridReport1: TRMGridReport;
    ADOPrint: TADOQuery;
    v2Column8: TcxGridDBColumn;
    v2Column9: 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 ToolButton2Click(Sender: TObject);
    procedure P_OrderNo_YSK(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure inoutNOChange(Sender: TObject);
    procedure MPRTCodeNameChange(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure v2SMoneyCompareRowValuesForCellMerging(Sender: TcxGridColumn;
      ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties;
      const AValue1: Variant; ARow2: TcxGridDataRow;
      AProperties2: TcxCustomEditProperties; const AValue2: Variant;
      var AAreEqual: Boolean);
    procedure Tv2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure VOrderNoCompareRowValuesForCellMerging(Sender: TcxGridColumn;
      ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties;
      const AValue1: Variant; ARow2: TcxGridDataRow;
      AProperties2: TcxCustomEditProperties; const AValue2: Variant;
      var AAreEqual: Boolean);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure CheckBox2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
  private
    { Private declarations }

    procedure InitGrid();

  public
    { Public declarations }
    RKFlag,FCYID:String;

  end;
                                     
var
  frmYSMXlist: TfrmYSMXlist;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp, U_ProductOrderNewList;

{$R *.dfm}

procedure TfrmYSMXlist.InitGrid();
begin
  IF trim(factoryName.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 :begdate,:enddate,:factoryName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Parameters.ParamByName('factoryName').Value:=Trim(factoryName.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  finally
    ADOQueryMain.EnableControls;
    ToolButton2.Click;
  end;
end;


procedure TfrmYSMXlist.FormDestroy(Sender: TObject);
begin
  frmYSMXlist:=nil;
end;

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

procedure TfrmYSMXlist.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(self.Caption+'2',Tv2,'财务管理');
  Close;
end;
procedure TfrmYSMXlist.FormShow(Sender: TObject);
begin
  ReadCxGrid(self.Caption+'2',Tv2,'财务管理');

 // InitGrid();
end;

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

procedure TfrmYSMXlist.ToolButton2Click(Sender: TObject);
var
  sqlwhere:string;
begin
  if ADOQueryMain.Active then
  begin
    sqlwhere:=SGetFilters(Panel1,1,2);
    if sqlwhere='' then
    begin
      IF not CheckBox1.Checked then
      begin
        sqlwhere:=' YFDefFlag1=0 ';
      end;
    end
    else
    begin
      IF not CheckBox1.Checked then
      begin
        sqlwhere:=sqlwhere+' and YFDefFlag1=0 ';
      end;
    end;
    SDofilter(ADOQueryMain,sqlwhere);
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  end;
end;

procedure TfrmYSMXlist.P_OrderNo_YSK(Sender: TObject);
begin
  ToolButton2.Click;
end;

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

procedure TfrmYSMXlist.inoutNOChange(Sender: TObject);
begin

  ToolButton2.Click;
end;

procedure TfrmYSMXlist.MPRTCodeNameChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYSMXlist.FormCreate(Sender: TObject);
begin
  cxGrid2.Align:=AlClient;
  Enddate.DateTime:=SGetServerDate(ADOQueryTemp);
  begdate.DateTime:=Enddate.DateTime-30;
end;

procedure TfrmYSMXlist.v2SMoneyCompareRowValuesForCellMerging(
  Sender: TcxGridColumn; ARow1: TcxGridDataRow;
  AProperties1: TcxCustomEditProperties; const AValue1: Variant;
  ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties;
  const AValue2: Variant; var AAreEqual: Boolean);
var
   colIdx0,colIdx1:integer;
begin
  colIdx0:= tv2.GetColumnByFieldName('inoutNo').Index;
  colIdx1:= tv2.GetColumnByFieldName('CRType').Index;
  if (ARow1.Values[colIdx0] = ARow2.Values[colIdx0])
    and (ARow1.Values[colIdx1] = ARow2.Values[colIdx1])
    and (ARow1.Values[colIdx1] = '应收款登记') then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmYSMXlist.Tv2CustomDrawCell(Sender: TcxCustomGridTableView;
  ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
  var ADone: Boolean);
begin
  IF AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('CRType').Index]='收款登记'then
  ACanvas.Brush.Color:=clFuchsia;
  IF AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('CRType').Index]<>'收款登记'then
  begin
    IF AViewInfo.GridRecord.Values[tv2.GetColumnByFieldName('hxNum').Index]<>0 then
    ACanvas.Brush.Color:=clMoneyGreen;
  end;
end;

procedure TfrmYSMXlist.VOrderNoCompareRowValuesForCellMerging(
  Sender: TcxGridColumn; ARow1: TcxGridDataRow;
  AProperties1: TcxCustomEditProperties; const AValue1: Variant;
  ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties;
  const AValue2: Variant; var AAreEqual: Boolean);
var
   colIdx0,colIdx1:integer;
begin

  colIdx0:= tv2.GetColumnByFieldName('inoutNO').Index;
  colIdx1:= tv2.GetColumnByFieldName('CRType').Index;
  if (ARow1.Values[colIdx0] = ARow2.Values[colIdx0])then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmYSMXlist.ToolButton3Click(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  begdate.SetFocus;
  try
    with ADOQueryCmd do
    begin
    close;
    sql.Clear;
    sql.Add('update YF_Money_CR SET otherNote='+quotedstr(trim(CDS_HZ.fieldbyname('otherNote').AsString)));
    sql.Add('where YFID='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
    execsql;
    end;
  except
    application.MessageBox('保存失败！','提示信息',0);
  end;
end;

procedure TfrmYSMXlist.ToolButton4Click(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      IF fieldbyname('YFDefFlag1').AsBoolean then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update YF_Money_CR SET YFDefFlag1=1 ');
          sql.Add('where YFID='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
          execsql;
        end;
      end;
      next;
    end;
    First;
    EnableControls;
  end;
  TBRafresh.Click;
end;

procedure TfrmYSMXlist.ToolButton5Click(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  IF Application.MessageBox('确定要取消隐藏吗？','提示',32+4)<>IDYES then Exit;
  with CDS_HZ do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      IF fieldbyname('YFDefFlag1').AsBoolean then
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update YF_Money_CR SET YFDefFlag1=0 ');
          sql.Add('where YFID='+quotedstr(trim(CDS_HZ.fieldbyname('YFID').AsString)));
          execsql;
        end;
      end;
      next;
    end;
    First;
    EnableControls;
  end;
  TBRafresh.Click;
end;

procedure TfrmYSMXlist.CheckBox1Click(Sender: TObject);
begin
  TBRafresh.Click;
end;

procedure TfrmYSMXlist.CheckBox2Click(Sender: TObject);
begin
  IF CDS_HZ.IsEmpty then exit;
  with CDS_HZ do
  begin
    DisableControls;
    First;
    while not eof do
    begin
      edit;
      fieldbyname('YFDefFlag1').Value:=checkbox2.Checked;
      post;
      next;
    end;
    First;
    EnableControls;
  end;
end;

procedure TfrmYSMXlist.ToolButton6Click(Sender: TObject);
var
  filepath:string;
begin
  IF CDS_HZ.IsEmpty then exit;
  try
    with ADOPrint do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' exec  P_Qry_Ysk_mx_DZ :begdate,:enddate,:factoryName');
      Parameters.ParamByName('begdate').Value:=Trim(FormatDateTime('yyyy-MM-dd',begdate.DateTime));
      Parameters.ParamByName('Enddate').Value:=Trim(FormatDateTime('yyyy-MM-dd',Enddate.DateTime+1));
      Parameters.ParamByName('factoryName').Value:=Trim(factoryName.Text);
      Open;
    end;
    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['begDate'] :=FormatDateTime('yyyy-mm-dd',begDate.Date);
    RmVariables['enddate'] :=FormatDateTime('yyyy-mm-dd',enddate.Date);
    RMVariables['zdr'] :=trim(DName);
    RmVariables['begDate'] :=FormatDateTime('yyyy-mm-dd',begDate.Date)+' 至 '+FormatDateTime('yyyy-mm-dd',enddate.Date);
    CDS_HZ.First;
    RmVariables['QCNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsFloat - CDS_HZ.fieldbyname('Money').AsFloat + CDS_HZ.fieldbyname('BBMoney').AsFloat  ;
    CDS_HZ.Last;
    RmVariables['endNum'] :=CDS_HZ.fieldbyname('MoneyYe').AsString;
    RMGridReport1.LoadFromFile(filepath);
   // RMGridReport1.PrintReport;
    RMGridReport1.ShowReport;

  finally

  end;
end;

end.
