unit U_KHList_CX;

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, cxPC, cxDropDownEdit, Menus, BtnEdit, Buttons,
  cxLookAndFeels, cxLookAndFeelPainters, dxSkinsCore, 
      dxSkinDarkRoom,
    
     
     
    
    
    
    
   dxSkinOffice2013White, 
     dxSkinSharpPlus,
   dxSkinSpringTime,  
   dxSkinsDefaultPainters, 
     dxSkinscxPCPainter,
  cxNavigator;

type
  TfrmKHList_CX = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    zjm: TEdit;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    Tadd: TToolButton;
    Label5: TLabel;
    KHNameJC: TEdit;
    DS_HZ3: TDataSource;
    CDS_HZ3: TClientDataSet;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Label2: TLabel;
    Label4: TLabel;
    SHStatus: TComboBox;
    cxGrid1: TcxGrid;
    TV1: TcxGridDBTableView;
    V1Column4: TcxGridDBColumn;
    V1Column1: TcxGridDBColumn;
    cxGYSName: TcxGridDBColumn;
    cxGridDBGYSNameZC: TcxGridDBColumn;
    B1GYSFZRen: TcxGridDBColumn;
    cxGridDBGYSTel: TcxGridDBColumn;
    V1SHStatus: TcxGridDBColumn;
    V1FZR: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    PopupMenu1: TPopupMenu;
    MainEdit: TMenuItem;
    MainView: TMenuItem;
    PopupMenu3: TPopupMenu;
    BankEdit: TMenuItem;
    BankDel: TMenuItem;
    BankView: TMenuItem;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V3Column2: TcxGridDBColumn;
    V3BankName: TcxGridDBColumn;
    V3BankNo: TcxGridDBColumn;
    V3Column3: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    V1Column2: TcxGridDBColumn;
    LabKHType: TLabel;
    KHType: TComboBox;
    V1Column3: TcxGridDBColumn;
    V1Column5: TcxGridDBColumn;
    Label3: TLabel;
    YWY: TComboBox;
    Label8: TLabel;
    LiDan: TComboBox;
    TBAddBank: TMenuItem;
    V3Column1: TcxGridDBColumn;
    Label1: TLabel;
    KHQuYu: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    TBFJUp: TMenuItem;
    TBFJView: TMenuItem;
    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 zjmChange(Sender: TObject);
    procedure TBExportClick(Sender: TObject);
    procedure cxTabControl1Change(Sender: TObject);
    procedure TV1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
    procedure V1Column2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure V1SHStatusCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
    procedure TaddClick(Sender: TObject);
    procedure TV3CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
  private
    { Private declarations }
    ColorInt: Integer;
    procedure InitGrid();
    procedure setstatus();
    procedure InitGridBankMain();
    procedure InitGridBankMainID(FID: string);
  public
    canshu1, canshu2, FGYSType: string;
  end;

var
  frmKHList_CX: TfrmKHList_CX;

implementation

uses
  U_DataLink, U_Fun, U_ZDYHelp;

{$R *.dfm}
procedure TfrmKHList_CX.setstatus();
begin

end;

procedure TfrmKHList_CX.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered := False;
    Close;
    SQL.Clear;
    sql.Add(' select *,zjm=dbo.getpinyin(KHName) ');
    sql.add(',SHStatus=(case when isnull(status,''0'')=''0'' then ''未审核'' else ''已审核'' end)');
    sql.Add(' from KH_Main A ');
    sql.add(' where isnull(KHFlag,'''')=''KH'' and isnull(Valid,'''')=''Y''  ');
    if Trim(canshu1) = '录入' then
    begin
      sql.Add(' and exists(select * from KH_Owner B where B.KHMainId=A.KHMainId and B.UserName=''' + Trim(DName) + ''')');
    end;
    Open;
  end;
  SCreateCDS20(ADOQueryMain, CDS_HZ);
  SInitCDSData20(ADOQueryMain, CDS_HZ);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid=''' + Trim(CDS_HZ.fieldbyname('KHMainid').asstring) + ''' and isnull(Valid,'''')=''Y''');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_HZ3);
  SInitCDSData20(ADOQueryTemp, CDS_HZ3);
end;

procedure TfrmKHList_CX.FormDestroy(Sender: TObject);
begin
  frmKHList_CX := nil;
end;

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

procedure TfrmKHList_CX.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('PBGYS11YX', Tv1, '客户管理');
  WriteCxGrid('PBGYS31YX', Tv3, '客户管理');
  Close;
end;

procedure TfrmKHList_CX.FormShow(Sender: TObject);
var
  fsj: string;
begin
  fsj := 'select Name=YWY from KH_Main where isnull(YWY,'''')<>'''' and Valid=''Y'' and isnull(KHFlag,'''')=''KH''  Group by YWY';
  SInitComBoxBySql(ADOQueryTemp, YWY, False, fsj);
  fsj := 'select Name=LiDan from KH_Main where isnull(LiDan,'''')<>''''  and Valid=''Y'' and isnull(KHFlag,'''')=''KH'' Group by LiDan';
  SInitComBoxBySql(ADOQueryTemp, LiDan, False, fsj);
  ReadCxGrid('PBGYS11YX', Tv1, '客户管理');
  ReadCxGrid('PBGYS31YX', Tv3, '客户管理');
  setstatus();
  InitGrid();
end;

procedure TfrmKHList_CX.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

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

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

procedure TfrmKHList_CX.cxTabControl1Change(Sender: TObject);
begin
  setstatus();
  InitGrid();
end;

procedure TfrmKHList_CX.TV1MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  InitGridBankMain();
end;

procedure TfrmKHList_CX.InitGridBankMain();
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid=''' + Trim(CDS_HZ.fieldbyname('KHMainid').asstring) + '''');
    if CDS_HZ.IsEmpty then
    begin
      sql.Add(' and 1=2');
    end
    else
    begin
      sql.Add(' and valid=''Y'' ');
    end;
    sql.Add(' order by XH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_HZ3);
  SInitCDSData20(ADOQueryTemp, CDS_HZ3);
end;

procedure TfrmKHList_CX.InitGridBankMainID(FID: string);
begin
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHMainid=''' + Trim(FID) + '''');
    if CDS_HZ.IsEmpty then
    begin
      sql.Add(' and 1=2');
    end
    else
    begin
      sql.Add(' and valid=''Y'' ');
    end;
    sql.Add(' order by XH');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp, CDS_HZ3);
  SInitCDSData20(ADOQueryTemp, CDS_HZ3);
end;

procedure TfrmKHList_CX.TV1CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ.IsEmpty then
    Exit;
  if CDS_HZ3.IsEmpty then
  begin
    Tadd.Click;
  end;
end;

procedure TfrmKHList_CX.V1Column2CustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id: Integer;
begin
  id := TV1.GetColumnByFieldName('KHType').Index;
  if AViewInfo.GridRecord.Values[id] = '外销' then
  begin
    ACanvas.Font.Color := clBlue;
  end;
end;

procedure TfrmKHList_CX.V1SHStatusCustomDrawCell(Sender: TcxCustomGridTableView; ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo; var ADone: Boolean);
var
  id: Integer;
begin
  id := TV1.GetColumnByFieldName('SHStatus').Index;
  if AViewInfo.GridRecord.Values[id] = '未审核' then
  begin
    ACanvas.Font.Color := clRed;
  end;
end;

procedure TfrmKHList_CX.TaddClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then
    exit;
  ModalResult := 1;
end;

procedure TfrmKHList_CX.TV3CellDblClick(Sender: TcxCustomGridTableView; ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton; AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ3.IsEmpty = false then
    TADD.Click;
end;

end.

