unit U_KHList;

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

type
  TfrmKHList = class(TForm)
    ToolBar1: TToolBar;
    TBRafresh: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Panel1: TPanel;
    ToolButton2: TToolButton;
    ADOQueryMain: TADOQuery;
    TBExport: TToolButton;
    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;
    Tchk: TToolButton;
    TNochk: TToolButton;
    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;
    TBOwner: TToolButton;
    V1Column2: TcxGridDBColumn;
    LabKHType: TLabel;
    KHType: TComboBox;
    V1Column3: TcxGridDBColumn;
    TBLiDan: TToolButton;
    V1Column5: TcxGridDBColumn;
    Panel2: TPanel;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    Image2: TImage;
    Label6: TLabel;
    Label7: TLabel;
    Label3: TLabel;
    YWY: TComboBox;
    Label8: TLabel;
    LiDan: TComboBox;
    TBAddBank: TMenuItem;
    V3Column1: TcxGridDBColumn;
    Label1: TLabel;
    KHQuYu: TEdit;
    cxStyleRepository1: TcxStyleRepository;
    cxStyle1: TcxStyle;
    BtnEditC1: TBtnEditA;
    BtnEditC2: TBtnEditA;
    TBFJUp: TMenuItem;
    TBFJView: TMenuItem;
    V1Column6: TcxGridDBColumn;
    V3Column4: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure TBDelClick(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 TchkClick(Sender: TObject);
    procedure TNochkClick(Sender: TObject);
    procedure MainEditClick(Sender: TObject);
    procedure MainViewClick(Sender: TObject);
    procedure BankEditClick(Sender: TObject);
    procedure BankDelClick(Sender: TObject);
    procedure BankViewClick(Sender: TObject);
    procedure TV3DblClick(Sender: TObject);
    procedure TBOwnerClick(Sender: TObject);
    procedure KHTypeClick(Sender: TObject);
    procedure TBLiDanClick(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure TaddClick(Sender: TObject);
    procedure TBAddBankClick(Sender: TObject);
    procedure Image2Click(Sender: TObject);
    procedure V1Column2CustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure BtnEditC1BtnClick(Sender: TObject);
    procedure BtnEditC2BtnClick(Sender: TObject);
    procedure V1SHStatusCustomDrawCell(Sender: TcxCustomGridTableView;
      ACanvas: TcxCanvas; AViewInfo: TcxGridTableDataCellViewInfo;
      var ADone: Boolean);
    procedure TBFJUpClick(Sender: TObject);
    procedure TBFJViewClick(Sender: TObject);
  private
    { Private declarations }
    ColorInt:Integer;
    procedure InitGrid();
    procedure setstatus();
    procedure InitGridBankMain();
    procedure InitGridBankMainID(FID:String);
  public
    canshu1,canshu2,FGYSType:string;
  end;

//var
  //frmGYSListPB: TfrmGYSListPB;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_KHInPutMainNX,U_KHInPutMainWX,U_PBGYSInPutMainBank,U_UserMoreSel,U_InfoBtn,U_FjList;

{$R *.dfm}
procedure TfrmKHList.setstatus();
begin
  tchk.Visible:=false;
  tnochk.Visible:=false;
  Tadd.Visible:=false;
  TBDel.Visible:=false;
  canshu2:='KH';
  IF trim(Self.Caption)='客户信息录入(高)*'then
  begin
    Tadd.Visible:=True;
    TBAddBank.Visible:=True;
    TBLiDan.Visible:=True;
    TBDel.Visible:=True;
    tchk.Visible:=False;
    tNochk.Visible:=False;
    MainEdit.Visible:=True;
    MainView.Visible:=True;
    TBExport.Visible:=True;
    BankEdit.Visible:=True;
    BankDel.Visible:=True;
    BankView.Visible:=True;
    TBOwner.Visible:=True;
    canshu1:='高权限';
    V1Column4.Visible:=False;
    V1Column4.Hidden:=True;
    TBFJUp.Visible:=True;
    TBFJView.Visible:=True;
  end else
  IF trim(Self.Caption)='客户信息审核*'then
  begin
    Tadd.Visible:=false;
    TBAddBank.Visible:=false;
    TBDel.Visible:=false;
    tchk.Visible:=True;
    tNochk.Visible:=True;
    MainEdit.Visible:=False;
    MainView.Visible:=True;
    TBExport.Visible:=True;
    BankEdit.Visible:=false;
    BankDel.Visible:=false;
    BankView.Visible:=True;
    TBOwner.Visible:=False;
    V1Column4.Visible:=True;
    V1Column4.Hidden:=False;
    TBFJUp.Visible:=True;
    TBFJView.Visible:=True;
  end else
  IF trim(Self.Caption)='客户信息录入*'then
  begin
    Tadd.Visible:=True;
    TBAddBank.Visible:=True;
    TBDel.Visible:=False;
    tchk.Visible:=False;
    tNochk.Visible:=False;
    MainEdit.Visible:=True;
    MainView.Visible:=True;
    TBExport.Visible:=True;
    BankEdit.Visible:=True;
    BankDel.Visible:=True;
    BankView.Visible:=True;
    TBOwner.Visible:=False;
    canshu1:='录入';
    V1Column4.Visible:=False;
    V1Column4.Hidden:=True;
    TBFJUp.Visible:=True;
    TBFJView.Visible:=True;
  end else
  IF trim(Self.Caption)='客户信息查询*'then
  begin
    Tadd.Visible:=False;
    TBAddBank.Visible:=False;
    TBDel.Visible:=False;
    tchk.Visible:=False;
    tNochk.Visible:=False;
    MainEdit.Visible:=False;
    MainView.Visible:=True;
    TBExport.Visible:=False;
    BankEdit.Visible:=False;
    BankDel.Visible:=False;
    BankView.Visible:=True;
    TBOwner.Visible:=False;
    V1Column4.Visible:=False;
    V1Column4.Hidden:=True;
    TBFJView.Visible:=True;
  end;

end;

procedure TfrmKHList.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select *,zjm=dbo.getpinyin(KHName)+KHNo ');
    sql.add(',SHStatus=(case when isnull(status,''0'')=''0'' then ''未审核'' else ''已审核'' end)');
    sql.Add(',SDQty=(select Count(*) from TP_File TF where TF.WBID=A.KHMainId and TF.TFType=''客户资料''  )');
    sql.Add(' from KH_Main A ');
    sql.add(' where isnull(KHFlag,'''')='''+Trim(canshu2)+''' 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.FormDestroy(Sender: TObject);
begin
  //frmGYSListPB:=nil;
end;

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

procedure TfrmKHList.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('PBGYS11YX',Tv1,'客户管理');
  WriteCxGrid('PBGYS31YX',Tv3,'客户管理');
  Close;
end;
procedure TfrmKHList.TBDelClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  {if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;}
  if Trim(canshu1)<>'高权限' then
  begin
    if Trim(CDS_HZ.fieldbyname('FillerCode').AsString)<>Trim(DCode) then
    begin
      Application.MessageBox('不能操作他人录入的数据!','提示',0);
      Exit;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Main where KHMainId='''+Trim(CDS_HZ.fieldbyname('KHMainId').AsString)+'''');
      Open;
    end;
    if Trim(ADOQueryTemp.fieldbyname('SHStatus').AsString)='已审核' then
    begin
      Application.MessageBox('已审核不能操作数据!','提示',0);
      Exit;
    end;
  end;
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('exec P_ISNO_KHInfo_Del :KHNO');
    Parameters.ParamByName('KHNO').Value:=Trim(CDS_HZ.fieldbyname('KHNo').AsString);
    Open;
  end;
  if ADOQueryTemp.IsEmpty=False then
  begin
    Application.MessageBox('此客户已经产生数据，不能删除!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  try
    ADOQueryCmd.Connection.BeginTrans;
    CDS_HZ.DisableControls;
    with CDS_HZ do
    begin
     // while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('UPdate  KH_Main  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
          sql.Add(' where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          
          sql.Add('UPdate  KH_Sub_Bank  Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
          sql.Add(' where KHMainID='''+Trim(CDS_HZ.fieldbyname('KHMainID').AsString)+'''');
          ExecSQL;
        end;
        CDS_HZ.Delete;
      end;
    end;
    CDS_HZ.EnableControls;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('操作失败!','提示',0);
    CDS_HZ.EnableControls;
  end;
end;

procedure TfrmKHList.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.TBRafreshClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  InitGrid();
end;

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


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

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

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

procedure TfrmKHList.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.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.TV1CellDblClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
var
  FName:string;
begin
  if CDS_HZ.IsEmpty then Exit;
  FName:=Tv1.Controller.FocusedColumn.DataBinding.FilterFieldName;
  if Trim(FName)='SDQty' then
  begin
    if CDS_HZ.IsEmpty then Exit;
    try
      frmFjList:=TfrmFjList.Create(Application);
      with frmFjList do
      begin
        fkeyNO:=Trim(CDS_HZ.fieldbyname('KHMainid').AsString);
        fType:='客户资料';
        if Self.Caption='客户信息查询*' then
        begin
          fstatus:=1;
        end else
        begin
          fstatus:=0;
        end;

        if ShowModal=1 then
        begin

        end;
      end;
    finally
      frmFjList.Free;
    end;
  end;
end;

procedure TfrmKHList.TchkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
    with CDS_HZ do
    begin
      DisableControls;
      First;
      while Locate('SSel',True,[]) do
      begin
        with ADOQueryCmd do
        begin
          close;
          sql.Clear;
          sql.Add('update KH_Main SET status=1,chker='''+trim(DName)+''',chkTime=getdate() ');
          sql.Add('where KHMainid='+quotedstr(trim(CDS_HZ.fieldbyname('KHMainid').AsString)));
          execsql;
        end;
        Edit;
        FieldByName('SSel').Value:=false;
        FieldByName('SHStatus').Value:='已审核';
      end;
      EnableControls;
    end;
    ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
    application.MessageBox('审核失败！','提示信息',0);
  end;
end;

procedure TfrmKHList.TNochkClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  if CDS_HZ.Locate('ssel',true,[])=false then
  begin
    Application.MessageBox('没有选择数据!','提示',0);
    Exit;
  end;
  try
    ADOQueryCmd.Connection.BeginTrans;
     with CDS_HZ do
     begin
       DisableControls;
       First;
       while Locate('SSel',True,[]) do
       begin
         with ADOQueryCmd do
         begin
           close;
           sql.Clear;
           sql.Add('update KH_Main SET status=0,chker='''',chkTime=null');
           sql.Add('where KHMainid='+quotedstr(trim(CDS_HZ.fieldbyname('KHMainid').AsString)));
           execsql;
         end;
         Edit;
         FieldByName('SSel').Value:=false;
         FieldByName('SHStatus').Value:='未审核';
       end;
       EnableControls;
     end;
     ADOQueryCmd.Connection.CommitTrans;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    CDS_HZ.EnableControls;
    application.MessageBox('撤销审核失败！','提示信息',0);
  end;
end;

procedure TfrmKHList.MainEditClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Trim(canshu1)<>'高权限' then
  begin
    {if Trim(CDS_HZ.fieldbyname('FillerCode').AsString)<>Trim(DCode) then
    begin
      Application.MessageBox('不能操作他人录入的数据!','提示',0);
      Exit;
    end;  }
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Main where KHMainId='''+Trim(CDS_HZ.fieldbyname('KHMainId').AsString)+'''');
      Open;
    end;
    if Trim(ADOQueryTemp.fieldbyname('Status').AsString)='1' then
    begin
      Application.MessageBox('已审核不能操作数据!','提示',0);
      Exit;
    end;
  end;
  if Trim(CDS_HZ.fieldbyname('KHType').AsString)='内销' then
  begin
    try
      frmKHInPutMainNX:=TfrmKHInPutMainNX.Create(Application);
      with frmKHInPutMainNX do
      begin
        FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
        FKHFlag:=Trim(Self.canshu2);
        if ShowModal=1 then
        begin
          TBRafresh.Click;
          CDS_HZ.Locate('KHMainId',FKHMainid,[]);
          InitGridBankMainID(FKHMainid);
        end;
      end;
    finally
      frmKHInPutMainNX.Free;
    end;
  end else
  if Trim(CDS_HZ.fieldbyname('KHType').AsString)='外销' then
  begin
    try
      frmKHInPutMainWX:=TfrmKHInPutMainWX.Create(Application);
      with frmKHInPutMainWX do
      begin
        FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
        FKHFlag:=Trim(Self.canshu2);
        if ShowModal=1 then
        begin
          TBRafresh.Click;
          CDS_HZ.Locate('KHMainId',FKHMainid,[]);
          InitGridBankMainID(FKHMainid);
        end;
      end;
    finally
      frmKHInPutMainWX.Free;
    end;
  end;

end;

procedure TfrmKHList.MainViewClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  if Trim(CDS_HZ.fieldbyname('KHType').AsString)='内销' then
  begin
    try
      frmKHInPutMainNX:=TfrmKHInPutMainNX.Create(Application);
      with frmKHInPutMainNX do
      begin
        FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
        frmKHInPutMainNX.TBSave.Visible:=False;
        if ShowModal=1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmKHInPutMainNX.Free;
    end;
  end else
  if Trim(CDS_HZ.fieldbyname('KHType').AsString)='外销' then
  begin
    try
      frmKHInPutMainWX:=TfrmKHInPutMainWX.Create(Application);
      with frmKHInPutMainWX do
      begin
        FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
        frmKHInPutMainWX.TBSave.Visible:=False;
        if ShowModal=1 then
        begin
          TBRafresh.Click;
        end;
      end;
    finally
      frmKHInPutMainWX.Free;
    end;
  end;

end;

procedure TfrmKHList.BankEditClick(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if Trim(CDS_HZ3.fieldbyname('FillerCode').AsString)<>Trim(DCode) then
    begin
      Application.MessageBox('不能操作他人录入的数据!','提示',0);
      Exit;
    end;
  end;
  try
    frmPBGYSInPutMainBank:=TfrmPBGYSInPutMainBank.Create(Application);
    with frmPBGYSInPutMainBank do
    begin
      Label19.Caption:='客  户';
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FBKID:=Trim(Self.CDS_HZ3.fieldbyname('BKID').AsString);
      PState:=1;
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmPBGYSInPutMainBank.Free;
  end;

end;

procedure TfrmKHList.BankDelClick(Sender: TObject);
begin
  IF CDS_HZ3.IsEmpty then exit;
  if Trim(canshu1)<>'高权限' then
  begin
    if Trim(CDS_HZ3.fieldbyname('FillerCode').AsString)<>Trim(DCode) then
    begin
      Application.MessageBox('不能操作他人录入的数据!','提示',0);
      Exit;
    end;
  end;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Sub_Bank Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
    sql.add('where BKID='''+Trim(CDS_HZ3.fieldbyname('BKID').AsString)+'''');
    ExecSQL;
  end;
  CDS_HZ3.Delete;
end;

procedure TfrmKHList.BankViewClick(Sender: TObject);
begin
  try
    frmPBGYSInPutMainBank:=TfrmPBGYSInPutMainBank.Create(Application);
    with frmPBGYSInPutMainBank do
    begin
      Label19.Caption:='客  户';
      FKHMainid:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FBKID:=Trim(Self.CDS_HZ3.fieldbyname('BKID').AsString);
      PState:=2;
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmPBGYSInPutMainBank.Free;
  end;
end;

procedure TfrmKHList.TV3DblClick(Sender: TObject);
begin
  BankView.Click;
end;

procedure TfrmKHList.TBOwnerClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  try
    frmUserMoreSel:=TfrmUserMoreSel.Create(Application);
    with frmUserMoreSel do
    begin
      KHMainId:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FYWY:=Trim(Self.CDS_HZ.fieldbyname('YWY').AsString);
      FKHType:='KH';
      Panel2.Caption:='可指定业务员';
      Panel4.Caption:='已指定业务员';
      FKOType:='业务员';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('YWY').Value:=frmUserMoreSel.FYWY;
        end;
      end;
    end;
  finally
    frmUserMoreSel.Free;
  end;
end;

procedure TfrmKHList.KHTypeClick(Sender: TObject);
begin
  ToolButton2.Click;
end;

procedure TfrmKHList.TBLiDanClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then Exit;
  try
    frmUserMoreSel:=TfrmUserMoreSel.Create(Application);
    with frmUserMoreSel do
    begin
      KHMainId:=Trim(Self.CDS_HZ.fieldbyname('KHMainId').AsString);
      FYWY:=Trim(Self.CDS_HZ.fieldbyname('LiDan').AsString);
      FKHType:='KH';
      Panel2.Caption:='可指定理单员';
      Panel4.Caption:='已指定理单员';
      FKOType:='理单员';
      if ShowModal=1 then
      begin
        with Self.CDS_HZ do
        begin
          Edit;
          FieldByName('LiDan').Value:=frmUserMoreSel.FYWY;
        end;
      end;
    end;
  finally
    frmUserMoreSel.Free;
  end;
end;

procedure TfrmKHList.SpeedButton1Click(Sender: TObject);
begin
  if Trim(BtnEditC1.Text)='' then
  begin
    Application.MessageBox('业务员不能为空！','提示',0);
    Exit;
  end;
  if Trim(BtnEditC2.Text)='' then
  begin
    Application.MessageBox('理单员不能为空！','提示',0);
    Exit;
  end;
  Panel2.Visible:=False;
  try
    frmKHInPutMainWX:=TfrmKHInPutMainWX.Create(Application);
    with frmKHInPutMainWX do
    begin
      FKHMainid:='';
      FKHFlag:=Trim(Self.canshu2);
      KHType.ItemIndex:=0;
      KHType.Enabled:=False;
      KHType.Color:=clMenu;
      FYWY:=Trim(BtnEditC1.Text);
      FLiDan:=Trim(BtnEditC2.Text);
      if ShowModal=1 then
      begin
        TBRafresh.Click;
        CDS_HZ.Locate('KHMainId',FKHMainid,[]);
        InitGridBankMainID(FKHMainid);
      end;
    end;
  finally
    frmKHInPutMainWX.Free;
  end;
end;

procedure TfrmKHList.SpeedButton2Click(Sender: TObject);
begin
  if Trim(BtnEditC1.Text)='' then
  begin
    Application.MessageBox('业务员不能为空！','提示',0);
    Exit;
  end;
  if Trim(BtnEditC2.Text)='' then
  begin
    Application.MessageBox('理单员不能为空！','提示',0);
    Exit;
  end;
  Panel2.Visible:=False;
  try
    frmKHInPutMainNX:=TfrmKHInPutMainNX.Create(Application);
    with frmKHInPutMainNX do
    begin
      FKHMainid:='';
      FKHFlag:=Trim(Self.canshu2);
      KHType.ItemIndex:=1;
      KHType.Enabled:=False;
      KHType.Color:=clMenu;
      FYWY:=Trim(BtnEditC1.Text);
      FLiDan:=Trim(BtnEditC2.Text);
      if ShowModal=1 then
      begin
        TBRafresh.Click;
        CDS_HZ.Locate('KHMainId',FKHMainid,[]);
        InitGridBankMainID(FKHMainid);
      end;
    end;
  finally
    frmKHInPutMainNX.Free;
  end;
end;

procedure TfrmKHList.TaddClick(Sender: TObject);
begin
  Panel2.Visible:=True;
end;

procedure TfrmKHList.TBAddBankClick(Sender: TObject);
begin
  if CDS_HZ.IsEmpty then exit;
  try
    frmPBGYSInPutMainBank:=TfrmPBGYSInPutMainBank.Create(Application);
    with frmPBGYSInPutMainBank do
    begin
      FKHMainid:=Trim(CDS_HZ.fieldbyname('KHMainId').AsString);
      PState:=0;
      Label19.Caption:='客  户';
      if ShowModal=1 then
      begin
        Self.InitGridBankMain();
      end;
    end;
  finally
    frmPBGYSInPutMainBank.Free;
  end;
end;

procedure TfrmKHList.Image2Click(Sender: TObject);
begin
  Panel2.Visible:=False;
end;

procedure TfrmKHList.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.BtnEditC1BtnClick(Sender: TObject);
begin
  try
    frmInfoBtn:=TfrmInfoBtn.Create(Application);
    with frmInfoBtn do
    begin
      FSql:='select XSInfo=UserName from SY_User where USERID<>''ADMIN'' ';
      if ShowModal=1 then
      begin
        BtnEditC1.Text:=Trim(FXSInfo);
      end;
    end;
  finally
    frmInfoBtn.Free;
  end;
end;

procedure TfrmKHList.BtnEditC2BtnClick(Sender: TObject);
begin
  try
    frmInfoBtn:=TfrmInfoBtn.Create(Application);
    with frmInfoBtn do
    begin
      FSql:='select XSInfo=UserName from SY_User where USERID<>''ADMIN'' ';
      if ShowModal=1 then
      begin
        BtnEditC2.Text:=Trim(FXSInfo);
      end;
    end;
  finally
    frmInfoBtn.Free;
  end;
end;

procedure TfrmKHList.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.TBFJUpClick(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then Exit;
  try
    frmFjList:=TfrmFjList.Create(Application);
    with frmFjList do
    begin
      fkeyNO:=Trim(CDS_HZ3.fieldbyname('BKID').AsString);
      fType:='开票资料';
      fstatus:=0;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList.Free;
  end;
end;

procedure TfrmKHList.TBFJViewClick(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then Exit;
  try
    frmFjList:=TfrmFjList.Create(Application);
    with frmFjList do
    begin
      fkeyNO:=Trim(CDS_HZ3.fieldbyname('BKID').AsString);
      fType:='开票资料';
      fstatus:=1;
      if ShowModal=1 then
      begin

      end;
    end;
  finally
    frmFjList.Free;
  end;
end;

end.


