unit U_ZdyAttInputX;

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, cxDropDownEdit, cxTimeEdit;

type
  TfrmZdyAttInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBsave: TToolButton;
    ADOQueryKH: TADOQuery;
    DSKH: TDataSource;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    Panel1: TPanel;
    cxGrid3: TcxGrid;
    TV1: TcxGridDBTableView;
    cxLXFS_LXR: TcxGridDBColumn;
    cxGridDBLXFS_CZ: TcxGridDBColumn;
    cxGridDLXFS_DH: TcxGridDBColumn;
    cxGridDBLXFS_SJ: TcxGridDBColumn;
    cxGridDBCLXFS_YX: TcxGridDBColumn;
    cxGridDBLXFS_Other: TcxGridDBColumn;
    cxGridDBLXFS_WX: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar3: TToolBar;
    ToolButton1: TToolButton;
    ToolButton2: TToolButton;
    Panel2: TPanel;
    cxGrid1: TcxGrid;
    TV3: TcxGridDBTableView;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    cxGridDBColumn14: TcxGridDBColumn;
    cxGridDBColumn15: TcxGridDBColumn;
    cxGridDBColumn16: TcxGridDBColumn;
    cxGridDBColumn17: TcxGridDBColumn;
    cxGridDBColumn20: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    ToolBar2: TToolBar;
    ToolButton3: TToolButton;
    ToolButton4: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DS_HZ2: TDataSource;
    CDS_HZ2: TClientDataSet;
    ADOQueryMain2: TADOQuery;
    V1LXFS_BH: TcxGridDBColumn;
    V1LXFS_ZW: TcxGridDBColumn;
    V1CLXFS_DZ: TcxGridDBColumn;
    V1LXFS_BZ: TcxGridDBColumn;
    Panel3: TPanel;
    Label1: TLabel;
    Panel4: TPanel;
    Label2: TLabel;
    Panel5: TPanel;
    Label3: TLabel;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    v2GYSName: TcxGridDBColumn;
    v2GYSNameZC: TcxGridDBColumn;
    v2GYSFZRen: TcxGridDBColumn;
    v2GYSAdd: TcxGridDBColumn;
    v2GYSTel: TcxGridDBColumn;
    v2KHCZ: TcxGridDBColumn;
    v2KHLXR: TcxGridDBColumn;
    v2KHPhone: TcxGridDBColumn;
    v2Note: TcxGridDBColumn;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu3: TcxGridPopupMenu;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TBsaveClick(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;

  public
        fkhType:string;
    { Public declarations }
    RKFlag,FCYID:String;

  end;

var
  frmZdyAttInput: TfrmZdyAttInput;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}



procedure TfrmZdyAttInput.InitGrid();
begin
  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_AttachmentX where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);
  //表2
    with ADOQueryMain2 do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from GYS_ZHXX where ATID='''+Trim(FCYID)+'''');
      sql.Add(' order by XHNo');
      Open;
    end;
    SCreateCDS20(ADOQueryMain2,CDS_HZ2);
    SInitCDSData20(ADOQueryMain2,CDS_HZ2);
  //表3
    ///联系人
    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from GYS_LXFS where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_LXR);
    SInitCDSData20(ADOQueryMain,CDS_LXR);
  finally
    ADOQueryMain.EnableControls;
  end;
end;


procedure TfrmZdyAttInput.FormDestroy(Sender: TObject);
begin
  //frmZdyAttInput:=nil;
end;

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

procedure TfrmZdyAttInput.TBCloseClick(Sender: TObject);
begin
  WriteCxGrid('客户资料1',Tv1,'客户自定义');
  WriteCxGrid('客户资料2',Tv2,'客户自定义');
  WriteCxGrid('客户资料3',Tv3,'客户自定义');
  Close;
end;
procedure TfrmZdyAttInput.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户资料1',Tv1,'客户自定义');
  ReadCxGrid('客户资料2',Tv2,'客户自定义');
  ReadCxGrid('客户资料3',Tv3,'客户自定义');
  InitGrid();
  if Trim(FCYID)='' then
  begin
    CDS_HZ.Append;
    CDS_HZ.FieldByName('GYSFZRen').Value:=trim(Dname);
    CDS_HZ.Post;
  end;
end;

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

function TfrmZdyAttInput.SaveData():Boolean;
var
  maxId,CRID:String;
  fzdyCode,FYSSubId,maxno:string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxId,'AT','KH_Zdy_AttachmentX',4,1)=False then
      begin
        Result:=False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！','提示',0);
        Exit;
      end;
      IF trim(fkhType)='广东客户' then
      begin
        if GetLSNo(ADOQueryCmd,fzdyCode,'KH','KH_Zdy_AttachmentX',4,0)=False then
        begin
          Result:=False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end
      else
      IF trim(fkhType)='湖州客户' then
      begin
        if GetLSNo(ADOQueryCmd,fzdyCode,'HZ','KH_Zdy_AttachmentX',4,0)=False then
        begin
          Result:=False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end
      else
      begin
        if GetLSNo(ADOQueryCmd,fzdyCode,'KH','KH_Zdy_AttachmentX',4,0)=False then
        begin
          Result:=False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！','提示',0);
          Exit;
        end;
      end
    end
    else
    begin
      maxId:=Trim(FCYID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Zdy_AttachmentX where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('GYSNo').Value:=Trim(fzdyCode);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('ATID').Value:=Trim(maxId);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'KH_Zdy_AttachmentX',0);
      FieldByName('GYSType').Value:='客户';
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_AttachmentX where GYSName='''+Trim(CDS_HZ.fieldbyname('GYSName').AsString)+'''');
      sql.Add(' and GYSType=''客户'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('客户名称重复!','提示',0);
      Exit;
    end;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_AttachmentX where GYSNameZC='''+Trim(CDS_HZ.fieldbyname('GYSNameZC').AsString)+'''');
      sql.Add(' and GYSType=''客户'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('客户简称重复!','提示',0);
      Exit;
    end;
    with CDS_HZ do
    begin
      Edit;
      FieldByName('ATID').Value:=Trim(maxId);
      Post;
    end;
    if Trim(FCYID)='' then
    begin
      with CDS_HZ2 do
      begin
        Append;
        FieldByName('ZdyCode').Value:=Trim(fzdyCode);
        FieldByName('ZdyName').Value:=Trim(CDS_HZ.fieldbyname('GYSName').AsString);
        FieldByName('ZHXX_JC').Value:=Trim(CDS_HZ.fieldbyname('GYSNameZC').AsString);
        fieldbyname('ZHXX_CWFZR').value:=Trim(CDS_HZ.fieldbyname('GYSFZRen').AsString);
        fieldbyname('ZHXX_DH').value:=Trim(CDS_HZ.fieldbyname('GYSTel').AsString);
        fieldbyname('ZHXX_SJ').value:=Trim(CDS_HZ.fieldbyname('KHPhone').AsString);
        FieldByName('XHNo').Value:=1;
        Post;
      end;
    end;
    ///表2
    with CDS_HZ2 do
    begin
      CDS_HZ2.First;
      while not Eof do
      begin
        if Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'ZH','GYS_ZHXX',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取系人最大ID失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxno:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from GYS_ZHXX where ZHXX_ZJ='''+Trim(maxno)+'''');
          Open;
          If IsEmpty then
          begin
            append;
            FieldByName('XHNo').Value:=CDS_HZ2.fieldbyname('XHNo').asinteger;
          end
          else
          begin
            Edit;
          end;
          RTSetSaveDataCDS(ADOQueryCmd,Tv3,CDS_HZ2,'GYS_ZHXX',0);
          FieldByName('ZHXX_ZJ').Value:=Trim(maxno);
          FieldByName('ATID').Value:=Trim(maxId);
          post;
        end;
        CDS_HZ2.Next;
      end;
    end;

    ///联系人
    IF not CDS_LXR.IsEmpty then
    begin
      with CDS_LXR do
      begin
        CDS_LXR.First;
        while not Eof do
        begin
          FYSSubId:=Trim(CDS_LXR.fieldbyname('LXFS_ZJ').AsString);
          if Trim(FYSSubId)='' then
          begin
            if GetLSNo(ADOQueryCmd,maxno,'AS','GYS_LXFS',4,1)=False then
            begin
              ADOQueryCmd.Connection.RollbackTrans;
              Application.MessageBox('取系人最大ID失败!','提示',0);
              Exit;
            end;
          end else
          begin
            maxno:=Trim(FYSSubId);
          end;
          with ADOQueryCmd do
          begin
            Close;
            sql.Clear;
            sql.Add('select * from GYS_LXFS where LXFS_ZJ='''+Trim(maxno)+'''');
            Open;
          end;
          with ADOQueryCmd do
          begin
            if Trim(FYSSubId)='' then
            begin
              Append;
            end else
            begin
              Edit;
            end;
            FieldByName('LXFS_ZJ').Value:=Trim(maxno);
            FieldByName('ATID').Value:=Trim(CDS_HZ.fieldbyname('ATID').AsString);
            FieldByName('ZHXX_ZJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
            RTSetSaveDataCDS(ADOQueryCmd,Tv1,CDS_LXR,'GYS_LXFS',0);
            Post;
          end;
          CDS_LXR.Next;
        end;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result:=True;
  except
    Result:=True;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!','提示',0);
  end;
end;

procedure TfrmZdyAttInput.TBsaveClick(Sender: TObject);
begin
  cxGrid2.SetFocus;
  if Trim(CDS_HZ.FieldByName('GYSName').AsString)='' then
  begin
    Application.MessageBox('客户名称不能为空!','提示',0);
    Exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;  

  if SaveData() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;


procedure TfrmZdyAttInput.ToolButton4Click(Sender: TObject);
begin
  if CDS_HZ2.IsEmpty then Exit;
  if Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from GYS_ZHXX where ZHXX_ZJ='''+Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_HZ2.Delete;
end;

procedure TfrmZdyAttInput.ToolButton5Click(Sender: TObject);
begin
  if CDS_LXR.IsEmpty then Exit;
  if Trim(CDS_LXR.fieldbyname('LXFS_ZJ').AsString)<>'' then
  begin
    if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('delete from GYS_LXFS where LXFS_ZJ='''+Trim(CDS_LXR.fieldbyname('LXFS_ZJ').AsString)+'''');
      ExecSQL;
    end;
  end;
  CDS_LXR.Delete;
end;

procedure TfrmZdyAttInput.ToolButton1Click(Sender: TObject);
begin
  with CDS_LXR do
  begin
    Append;
    Post;
  end;
end;

procedure TfrmZdyAttInput.ToolButton3Click(Sender: TObject);
begin
  with CDS_HZ2 do
  begin
    Append;
    FieldByName('XHNo').Value:=CDS_HZ2.RecordCount+2;
    Post;
  end;
end;

end.
