unit U_KHAdd;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, BtnEdit, ComCtrls, cxStyles, cxCustomData, cxGraphics,
  cxFilter, cxData, cxDataStorage, cxEdit, DB, cxDBData, ToolWin,
  cxGridLevel, cxGridCustomTableView, cxGridTableView, cxGridDBTableView,
  cxClasses, cxControls, cxGridCustomView, cxGrid, cxCalendar, DBClient,
  ADODB, cxButtonEdit, cxDropDownEdit, cxGridCustomPopupMenu,
  cxGridPopupMenu, cxTextEdit;

type
  TfrmKHAdd = class(TForm)
    ToolBar1: TToolBar;
    ToolButton6: TToolButton;
    TBClose: TToolButton;
    DS_BJ: TDataSource;
    CDS_SQ: TClientDataSet;
    ADOQueryMain: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    cxGridPopupMenu1: TcxGridPopupMenu;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2Column2: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    v2Column14: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column6: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    v2Column8: TcxGridDBColumn;
    v2Column17: TcxGridDBColumn;
    procedure FormDestroy(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure v2Column5PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column12PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column13PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
    procedure v2Column15PropertiesButtonClick(Sender: TObject;
      AButtonIndex: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    FCYID,FYSID,FPrice:String;
  end;

var
  frmKHAdd: TfrmKHAdd;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmKHAdd.FormDestroy(Sender: TObject);
begin
  frmKHAdd:=nil;
end;

procedure TfrmKHAdd.FormShow(Sender: TObject);
var
  FBZType,fsj:string;
begin
  ReadCxGrid('客户Add',Tv2,'基础数据');
  fsj:='select UserId code,UserName Name from SY_User where UserId not in(''Admin'',''Test'')';
  SInitCxGridComboBoxBySql(ADOQueryTemp,v2Column14,fsj,1,False,'');
  fsj:='select UserId code,UserName Name from SY_User where UserId not in(''Admin'',''Test'')';
  SInitCxGridComboBoxBySql(ADOQueryTemp,v2Column10,fsj,1,False,'');
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ATID='''+Trim(FYSID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_SQ);
    SInitCDSData20(ADOQueryMain,CDS_SQ);
    if CDS_SQ.IsEmpty then
    begin
      with CDS_SQ do
      begin
        Append;
        Post;
      end;
    end;
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmKHAdd.TBCloseClick(Sender: TObject);
begin
  Close;
  WriteCxGrid('客户Add',Tv2,'基础数据');
end;

procedure TfrmKHAdd.ToolButton6Click(Sender: TObject);
var
  maxno:String;
begin
  if Trim(CDS_SQ.fieldbyname('ZdyCode').AsString)='' then
  begin
    Application.MessageBox('客户编号不能为空!','提示',0);
    Exit;
  end;
  ToolBar1.SetFocus;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FYSID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'AT','KH_Zdy_Attachment',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取客户最大ID失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FYSID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ATID='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FYSID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('ATID').Value:=Trim(maxno);
      FieldByName('Type').Value:='KHName';
      FieldByName('ZdyName').Value:=CDS_SQ.fieldbyname('ZdyName').Value;
      FieldByName('MainType').Value:=CDS_SQ.fieldbyname('MainType').Value;
      FieldByName('ZdyCode').Value:=CDS_SQ.fieldbyname('ZdyCode').Value;
      FieldByName('DEFstr1').Value:=CDS_SQ.fieldbyname('DEFstr1').Value;
      FieldByName('DEFstr2').Value:=CDS_SQ.fieldbyname('DEFstr2').Value;
      FieldByName('DEFstr3').Value:=CDS_SQ.fieldbyname('DEFstr3').Value;
      FieldByName('DEFstr4').Value:=CDS_SQ.fieldbyname('DEFstr4').Value;
      FieldByName('DEFstr5').Value:=CDS_SQ.fieldbyname('DEFstr5').Value;
      FieldByName('DEFstr6').Value:=CDS_SQ.fieldbyname('DEFstr6').Value;
      FieldByName('DEFstr7').Value:=CDS_SQ.fieldbyname('DEFstr7').Value;
      FieldByName('DEFstr8').Value:=CDS_SQ.fieldbyname('DEFstr8').Value;
      FieldByName('DEFstr9').Value:=CDS_SQ.fieldbyname('DEFstr9').Value;
      FieldByName('DEFstr10').Value:=CDS_SQ.fieldbyname('DEFstr10').Value;
      FieldByName('DEFNote1').Value:=CDS_SQ.fieldbyname('DEFNote1').Value;
      FieldByName('DEFNote2').Value:=CDS_SQ.fieldbyname('DEFNote2').Value;
      FieldByName('DEFNote3').Value:=CDS_SQ.fieldbyname('DEFNote3').Value;
      FieldByName('DEFNote4').Value:=CDS_SQ.fieldbyname('DEFNote4').Value;
      FieldByName('DEFNote5').Value:=CDS_SQ.fieldbyname('DEFNote5').Value;
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select Count(*) FFCount from KH_Zdy_Attachment where ZdyCode='''+Trim(CDS_SQ.fieldbyname('ZdyCode').AsString)+'''');
      Open;
    end;
    if ADOQueryCmd.FieldByName('FFCount').AsInteger>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      //FYSID:='';
      Application.MessageBox('编号重复!','提示',0);
      Exit;
    end;
    FYSID:=Trim(maxno);
    ADOQueryCmd.Connection.CommitTrans;
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmKHAdd.v2Column5PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DEFstr4GSLX';
      flagname:='公司类型';
      //MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with Self.CDS_SQ do
        begin
          Edit;
          FieldByName('DEFstr4').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHAdd.v2Column12PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DEFstr6ZYSC';
      flagname:='主营市场';
      //MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with Self.CDS_SQ do
        begin
          Edit;
          FieldByName('DEFstr6').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHAdd.v2Column13PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DEFstr7ZYCP';
      flagname:='主营产品';
      //MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with Self.CDS_SQ do
        begin
          Edit;
          FieldByName('DEFstr7').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmKHAdd.v2Column15PropertiesButtonClick(Sender: TObject;
  AButtonIndex: Integer);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='DEFstr8KHQY';
      flagname:='客户区域';
      //MainType:=Trim(DName);
      if ShowModal=1 then
      begin
        with Self.CDS_SQ do
        begin
          Edit;
          FieldByName('DEFstr8').Value:=Trim(ClientDataSet1.fieldbyname('ZdyName').AsString);
        end;
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
