unit U_YWYHXMX;

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

type
  TfrmYWYHXMX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    ORDERNO: TEdit;
    EndDate: TDateTimePicker;
    Label4: TLabel;
    Label5: TLabel;
    Begdate: TDateTimePicker;
    ToolButton3: TToolButton;
    CDSPRTHZ: TClientDataSet;
    CDSPRTMX: TClientDataSet;
    RMDBMX: TRMDBDataSet;
    ADOQueryMain: TADOQuery;
    DataSource1: TDataSource;
    CDS_HZ: TClientDataSet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column12: TcxGridDBColumn;
    cxGrid1Level1: TcxGridLevel;
    Tv1Column1: TcxGridDBColumn;
    Tv1Column2: TcxGridDBColumn;
    Tv1Column3: TcxGridDBColumn;
    Tv1Column4: TcxGridDBColumn;
    Tv1Column5: TcxGridDBColumn;
    Tv1Column6: TcxGridDBColumn;
    Tv1Column7: TcxGridDBColumn;
    Label2: TLabel;
    M_HXCODE: TEdit;
    Tv1Column8: TcxGridDBColumn;
    Tv1Column9: TcxGridDBColumn;
    Tv1Column10: 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 COAREAChange(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
    procedure COAREAKeyPress(Sender: TObject; var Key: Char);
  private

    { Private declarations }
    canshu1: string;
    procedure InitGrid();
  public
    fkhType: string;
    Fmanage: string;
    FKHNAME, FYWY: string;
    FKK: string;
  end;

var
  frmYWYHXMX: TfrmYWYHXMX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}

procedure TfrmYWYHXMX.InitGrid();
var
  STRwhere, Pwhere: string;
begin
  STRwhere := '';
  //if Trim(canshu1) = '录入' then
  Pwhere := SGetFilters(Panel1, 1, 2);

  begin
    if trim(Pwhere) <> '' then
      STRwhere := STRwhere + ' and ' + trim(Pwhere);

  end;
  try
    ADOQueryMain.DisableControls;
    if FKK = '0' then
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;

        sql.Add(' exec P_View_KHXDTJ_MX @Begdate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' ,@enddate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
        SQL.Add(',@KHNAME=' + QuotedStr(Trim(FKHNAME)));
        SQL.Add(',@YWY=' + QuotedStr(Trim(FYWY)));
        SQL.Add(',@WSql=' + QuotedStr(Trim(STRwhere)));
//     ShowMessage(SQL.Text);
        Open;
      end;
    end
    else
    begin
      with ADOQueryMain do
      begin
        Close;
        SQL.Clear;

        sql.Add(' exec P_View_KHXDTJ_DD_MX @Begdate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
        sql.Add(' ,@enddate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
        SQL.Add(',@KHNAME=' + QuotedStr(Trim(FKHNAME)));
        SQL.Add(',@YWY=' + QuotedStr(Trim(FYWY)));
        SQL.Add(',@WSql=' + QuotedStr(Trim(STRwhere)));
  // ShowMessage(SQL.Text);
        Open;
      end;
    end;

    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
//    CDS_HZ.Last;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYWYHXMX.FormDestroy(Sender: TObject);
begin
  frmYWYHXMX := nil;
end;

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

procedure TfrmYWYHXMX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid(trim(self.caption), Tv1, '业务员花型4');
  Close;
end;

procedure TfrmYWYHXMX.FormShow(Sender: TObject);
begin
  ReadCxGrid(trim(self.caption), Tv1, '业务员花型4');
  InitGrid();
end;

procedure TfrmYWYHXMX.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

procedure TfrmYWYHXMX.ToolButton2Click(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 TfrmYWYHXMX.COAREAChange(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmYWYHXMX.ToolButton1Click(Sender: TObject);
begin
  if ADOQueryMain.IsEmpty then
    Exit;
  TcxGridToExcel(self.Caption, cxgrid1);
end;

procedure TfrmYWYHXMX.FormCreate(Sender: TObject);
begin
  canshu1 := Trim(DParameters1);
//  EndDate.DateTime := SGetServerDate10(ADOQueryTemp);
//  BegDate.DateTime := EndDate.DateTime - 90;
end;

procedure TfrmYWYHXMX.ToolButton3Click(Sender: TObject);
var
  fPrintFile, fPrintFile10, FMainID, FKHName, FFPNo: string;
  i: Integer;
begin
  ExportFtErpFile('跟进表.rmf', ADOQueryTemp);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD_HZ ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDSPRTHZ);
  SInitCDSData20(ADOQueryTemp, CDSPRTHZ);

  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD_CD ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    Open;
  end;
  RMVariables['HZCD'] := ADOQueryTemp.fieldbyname('HZCD').Value;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.add('exec P_Print_CRJD ');
    sql.add('@MainId=' + quotedstr(Trim(CDS_HZ.fieldbyname('MainId').AsString)));
    sql.add(',@Falg=''明细''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDSPRTMX);
  SInitCDSData20(ADOQueryTemp, CDSPRTMX);

  fPrintFile := ExtractFilePath(Application.ExeName) + 'Report\跟进表.rmf';
  if FileExists(fPrintFile) then
  begin
    RM1.LoadFromFile(fPrintFile);
    RM1.ShowReport;
  end
  else
  begin
    Application.MessageBox(PChar('没有找到' + fPrintFile), '提示', 0);
  end;
end;

procedure TfrmYWYHXMX.Tv1Column1CompareRowValuesForCellMerging(Sender: TcxGridColumn; ARow1: TcxGridDataRow; AProperties1: TcxCustomEditProperties; const AValue1: Variant; ARow2: TcxGridDataRow; AProperties2: TcxCustomEditProperties; const AValue2: Variant; var AAreEqual: Boolean);
begin
  if ARow1.Values[v1Column12.Index] = ARow2.Values[v1Column12.Index] then
    AAreEqual := True
  else
    AAreEqual := False;
end;

procedure TfrmYWYHXMX.COAREAKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

end.

