unit U_YWYHX;

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
  TfrmYWYHX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ToolButton1: TToolButton;
    RM1: TRMGridReport;
    RMDBHZ: TRMDBDataSet;
    Label3: TLabel;
    COAREA: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    Label1: TLabel;
    YWY: 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_KHNAME: TEdit;
    Tv1Column8: TcxGridDBColumn;
    ToolButton4: TToolButton;
    Tv1Column9: TcxGridDBColumn;
    ToolButton5: TToolButton;
    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);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
  private

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

var
  frmYWYHX: TfrmYWYHX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp, U_YWYHXMX;

{$R *.dfm}

procedure TfrmYWYHX.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);

    if COAREA.Text <> '' then
    begin
      STRwhere := STRwhere + ' and ((SELECT TOP 1 COAREA FROM Company B WHERE B.CONAME=A.M_KHNAME)) like ''' + '%' + Trim(COAREA.Text) + '%' + '''';
    end;
  end;
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;

      sql.Add(' exec P_View_KHXDTJ @Begdate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', Begdate.DateTime))));
      sql.Add(' ,@enddate=' + quotedstr(trim(FormatDateTime('yyyy-MM-dd', EndDate.DateTime + 1))));
      SQL.Add(',@WSql=' + QuotedStr(Trim(STRwhere)));
//      ShowMessage(SQL.Text);
      Open;
    end;
    SCreateCDS20(ADOQueryMain, CDS_HZ);
    SInitCDSData20(ADOQueryMain, CDS_HZ);
//    CDS_HZ.Last;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmYWYHX.FormDestroy(Sender: TObject);
begin
  frmYWYHX := nil;
end;

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

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

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

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

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

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

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

procedure TfrmYWYHX.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 TfrmYWYHX.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 TfrmYWYHX.COAREAKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    InitGrid();
  end;
end;

procedure TfrmYWYHX.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty = true then
    Exit;
  if CDS_HZ.fieldbyname('XDGS').AsString = '0' then
    Exit;
  try
    frmYWYHXMX := TfrmYWYHXMX.Create(Application);
    with frmYWYHXMX do
    begin
      Begdate.DateTime := Self.Begdate.DateTime;
      EndDate.DateTime := Self.EndDate.DateTime;
      FKHName := SELF.CDS_HZ.fieldbyname('M_KHNAME').AsString;
      FYWY := Self.CDS_HZ.fieldbyname('YWY').AsString;
      FKK := '0';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYWYHXMX.Free;
  end;
end;

procedure TfrmYWYHX.ToolButton5Click(Sender: TObject);
begin
  if CDS_HZ.IsEmpty = true then
    Exit;
  if CDS_HZ.fieldbyname('PRTOrderQty').AsString = '0' then
    Exit;
  try
    frmYWYHXMX := TfrmYWYHXMX.Create(Application);
    with frmYWYHXMX do
    begin
      Begdate.DateTime := Self.Begdate.DateTime;
      EndDate.DateTime := Self.EndDate.DateTime;
      FKHName := SELF.CDS_HZ.fieldbyname('M_KHNAME').AsString;
      FYWY := Self.CDS_HZ.fieldbyname('YWY').AsString;
      FKK := '1';
      if ShowModal = 1 then
      begin
        InitGrid();
      end;
    end;
  finally
    frmYWYHXMX.Free;
  end;
end;

end.

