unit U_ZHKHInPutTabNew;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu,IdGlobal;

type
  TfrmZHKHInPutTabNew = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    cxPageControl1: TcxPageControl;
    cxTabSheet1: TcxTabSheet;
    cxTabSheet2: TcxTabSheet;
    cxTabSheet3: TcxTabSheet;
    cxTabSheet4: TcxTabSheet;
    cxGrid1: TcxGrid;
    Tv1: TcxGridDBTableView;
    v1Column1: TcxGridDBColumn;
    v1Column2: TcxGridDBColumn;
    v1Column3: TcxGridDBColumn;
    v1Column5: TcxGridDBColumn;
    v1Column6: TcxGridDBColumn;
    v1Column7: TcxGridDBColumn;
    v1Column8: TcxGridDBColumn;
    v1Column9: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGrid2: TcxGrid;
    TV2: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    Panel5: TPanel;
    cxTabSheet7: TcxTabSheet;
    v1Column10: TcxGridDBColumn;
    Label14: TLabel;
    Label15: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label21: TLabel;
    Label22: TLabel;
    CWPhone: TEdit;
    CWPerson: TEdit;
    CWEmail: TEdit;
    CWQQ: TEdit;
    CWMSN: TEdit;
    CWSKYPE: TEdit;
    Label23: TLabel;
    CWKJAddress: TEdit;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    ToolBar3: TToolBar;
    ToolButton2: TToolButton;
    ToolButton6: TToolButton;
    Panel3: TPanel;
    Label1: TLabel;
    Label4: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label9: TLabel;
    Label13: TLabel;
    KHName: TEdit;
    YWY: TEdit;
    ZKAddress: TEdit;
    ZKTelNo: TEdit;
    ZKFax: TEdit;
    KHNameEng: TEdit;
    KHNameJC: TEdit;
    Label3: TLabel;
    ZKDate: TDateTimePicker;
    Panel4: TPanel;
    Label2: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Label16: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    ZKPhone: TEdit;
    ZKPerson: TEdit;
    ZKEmail: TEdit;
    BossQQ: TEdit;
    BossMSN: TEdit;
    BossSKYPE: TEdit;
    Label12: TLabel;
    BossKJAddress: TEdit;
    Label33: TLabel;
    KHCode: TEdit;
    Label34: TLabel;
    TBSave: TToolButton;
    V2Column1: TcxGridDBColumn;
    ClientDataSet1: TClientDataSet;
    DataSource1: TDataSource;
    ClientDataSet2: TClientDataSet;
    DataSource2: TDataSource;
    ToolBar5: TToolBar;
    ToolButton1: TToolButton;
    ToolButton3: TToolButton;
    cxGrid4: TcxGrid;
    Tv4: TcxGridDBTableView;
    cxGridDBColumn13: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridLevel4: TcxGridLevel;
    ClientDataSet4: TClientDataSet;
    DataSource4: TDataSource;
    RichEdit1: TRichEdit;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGridPopupMenu2: TcxGridPopupMenu;
    cxGridPopupMenu4: TcxGridPopupMenu;
    Label25: TLabel;
    Label32: TLabel;
    NetAddress: TEdit;
    v1Column4: TcxGridDBColumn;
    Label35: TLabel;
    KHType: TBtnEditA;
    KHLaiYuan: TBtnEditA;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure SKBankBtnDnClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBSaveClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure Tv4CellClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure KHTypeBtnClick(Sender: TObject);
    procedure KHLaiYuanBtnClick(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    procedure SaveCDS(ClientDataSet20:TClientDataSet;Tv20:TcxGridDBTableView;maxno10:string;ZSType:String);

    { Private declarations }
  public
    PState,CopyInt:Integer;
    FMainId,FFMainId,FTFID:String;
    FXS,TPInt,SaveInt,TPIntOther,FWH:Integer;
    { Public declarations }
  end;

var
  frmZHKHInPutTabNew: TfrmZHKHInPutTabNew;
  newh:hwnd;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmZHKHInPutTabNew.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('KH1',Tv1,'客户信息');
  WriteCxGrid('KH2',Tv2,'客户信息');
  WriteCxGrid('KH4',Tv4,'客户信息');
  Close;
end;

procedure TfrmZHKHInPutTabNew.InitData();
var
  fsj:string;
begin
  cxPageControl1.ActivePageIndex:=0;
  ReadCxGrid('KH1',Tv1,'客户信息');
  ReadCxGrid('KH2',Tv2,'客户信息');
  ReadCxGrid('KH4',Tv4,'客户信息');

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_Info where ZKId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel3,2);
  SCSHDataNew(ADOQuery1,Panel4,2);
  SCSHDataNew(ADOQuery1,Panel5,2);
  KHType.Text:=Trim(ADOQuery1.fieldbyname('KHType').AsString);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_Info_Sub where ZKId='''+Trim(FMainId)+'''');
    sql.Add(' and ZSType=''YW'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet1);
  SInitCDSData20(ADOQuery1,ClientDataSet1);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_Info_Sub where ZKId='''+Trim(FMainId)+'''');
    sql.Add(' and ZSType=''CW'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet2);
  SInitCDSData20(ADOQuery1,ClientDataSet2);
  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_Info_Sub where ZKId='''+Trim(FMainId)+'''');
    sql.Add(' and ZSType=''QK'' ');
    Open;
  end;
  SCreateCDS20(ADOQuery1,ClientDataSet4);
  SInitCDSData20(ADOQuery1,ClientDataSet4);
  if PState=0 then
  begin
    ZKDate.DateTime:=SGetServerDateTime(ADOTemp);
    YWY.Text:=Trim(DName);
  end else
  begin
  end;
  if CopyInt=99 then
  begin
    PState:=0;
    FMainId:='';
  end;

end;



procedure TfrmZHKHInPutTabNew.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmZHKHInPutTabNew.SaveData():Boolean;
var
  maxno,maxsubno:String;
begin
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'ZK','ZH_KH_Info',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ZH_KH_Info where ZKId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('ZKId').Value:=Trim(maxno);
      SSetsaveSqlNew(ADOCmd,'ZH_KH_Info',Panel3,2);
      SSetsaveSqlNew(ADOCmd,'ZH_KH_Info',Panel4,2);
      SSetsaveSqlNew(ADOCmd,'ZH_KH_Info',Panel5,2);
      FieldByName('Type').Value:='KH';
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      FieldByName('Valid').Value:='Y';

      Post;
    end;
    if Trim(KHName.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHName='''+Trim(KHName.Text)+'''');
        SQL.Add(' and Type=''KH'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('客户名称重复!','提示',0);
        Exit;
      end;
    end;

    if Trim(KHCode.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHCode='''+Trim(KHCode.Text)+'''');
        SQL.Add(' and Type=''KH'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('客户编号重复!','提示',0);
        Exit;
      end;
    end;

    if Trim(KHNameJC.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHNameJC='''+Trim(KHNameJC.Text)+'''');
        SQL.Add(' and Type=''KH'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('客户简称重复!','提示',0);
        Exit;
      end;
    end;
    if Trim(KHNameEng.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHNameEng='''+Trim(KHNameEng.Text)+'''');
        SQL.Add(' and Type=''KH'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('重复!','提示',0);
        Exit;
      end;
    end;
    SaveCDS(ClientDataSet1,Tv1,maxno,'YW');
    SaveCDS(ClientDataSet2,TV2,maxno,'CW');
    SaveCDS(ClientDataSet4,Tv4,maxno,'QK');

    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;
procedure TfrmZHKHInPutTabNew.SaveCDS(ClientDataSet20:TClientDataSet;Tv20:TcxGridDBTableView;maxno10:string;ZSType:String);
var
  maxsubno:string;
begin
  with ClientDataSet20 do
  begin
    First;
    while not Eof do
    begin
      if Trim(ClientDataSet20.fieldbyname('ZSID').AsString)='' then
      begin
        if GetLSNo(ADOCmd,maxsubno,'ZS','ZH_KH_Info_Sub',3,1)=False then
        begin
          ADOCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败!','提示',0);
          Exit;
        end;
      end else
      begin
        maxsubno:=Trim(ClientDataSet20.fieldbyname('ZSID').AsString);
      end;
      with ADOCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from ZH_KH_Info_Sub where ZSID='''+Trim(maxsubno)+'''');
        Open;
      end;
      with ADOCmd do
      begin
        if Trim(ClientDataSet20.fieldbyname('ZSID').AsString)='' then
        begin
          Append;
          FieldByName('Filler').Value:=Trim(DName);
        end else
        begin
          Edit;
          FieldByName('Editer').Value:=Trim(DName);
          FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
        end;
        FieldByName('ZKID').Value:=Trim(maxno10);
        FieldByName('ZSID').Value:=Trim(maxsubno);
        FieldByName('ZSType').Value:=Trim(ZSType);
        RTSetSaveDataCDS(ADOCmd,Tv20,ClientDataSet20,'ZH_KH_Info_Sub',2);
        Post;
      end;
      Edit;
      FieldByName('ZSID').Value:=Trim(maxsubno);
      post;
      Next;
    end;
  end;
end;
procedure TfrmZHKHInPutTabNew.SYRNameBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZHKHInPutTabNew.SKBankBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;



procedure TfrmZHKHInPutTabNew.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  if SaveInt=99 then
  begin
    ModalResult:=1;
  end;
end;

procedure TfrmZHKHInPutTabNew.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(KHName.Text)+Trim(KHNameEng.Text)='' then
  begin
    Application.MessageBox('客户名称不能为空！','提示',0);
    Exit;
  end;
  if Trim(KHType.Text)='' then
  begin
    Application.MessageBox('客户类型不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
    SaveInt:=99;
  end;
end;

procedure TfrmZHKHInPutTabNew.ToolButton4Click(Sender: TObject);
begin
  ClientDataSet1.Append;
  ClientDataSet1.Post;
end;

procedure TfrmZHKHInPutTabNew.ToolButton5Click(Sender: TObject);
begin
  DelCDS(ClientDataSet1,ADOCmd,'delete ZH_KH_Info_Sub where ZSID='''+Trim(ClientDataSet1.fieldbyname('ZSID').AsString)+'''');
end;

procedure TfrmZHKHInPutTabNew.ToolButton2Click(Sender: TObject);
begin
  ClientDataSet2.Append;
  ClientDataSet2.Post;
end;

procedure TfrmZHKHInPutTabNew.ToolButton6Click(Sender: TObject);
begin
  DelCDS(ClientDataSet2,ADOCmd,'delete ZH_KH_Info_Sub where ZSID='''+Trim(ClientDataSet2.fieldbyname('ZSID').AsString)+'''');
end;

procedure TfrmZHKHInPutTabNew.ToolButton1Click(Sender: TObject);
begin
  ClientDataSet4.Append;
  ClientDataSet4.Post;
end;

procedure TfrmZHKHInPutTabNew.ToolButton3Click(Sender: TObject);
begin
  DelCDS(ClientDataSet4,ADOCmd,'delete ZH_KH_Info_Sub where ZSID='''+Trim(ClientDataSet4.fieldbyname('ZSID').AsString)+'''');
end;

procedure TfrmZHKHInPutTabNew.Tv4CellClick(Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  RichEdit1.Text:=Trim(ClientDataSet4.fieldbyname('QKNote').AsString);
end;

procedure TfrmZHKHInPutTabNew.KHTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ZHKHType';
      flagname:='客户类型';
      if ShowModal=1 then
      begin
        KHType.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmZHKHInPutTabNew.KHLaiYuanBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='ZHKHLaiYuan';
      flagname:='客户来源';
      if ShowModal=1 then
      begin
        KHLaiYuan.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
