unit U_ZdyAttInput;

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;

type
  TfrmZdyAttInput = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGrid2: TcxGrid;
    Tv2: TcxGridDBTableView;
    cxGridLevel1: TcxGridLevel;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    TBsave: TToolButton;
    v2Column6: TcxGridDBColumn;
    v2Column1: TcxGridDBColumn;
    v2Column3: TcxGridDBColumn;
    v2Column4: TcxGridDBColumn;
    v2Column5: TcxGridDBColumn;
    v2Column7: TcxGridDBColumn;
    v2Column8: TcxGridDBColumn;
    v2Column9: TcxGridDBColumn;
    v2Column10: TcxGridDBColumn;
    v2Column11: TcxGridDBColumn;
    v2Column12: TcxGridDBColumn;
    v2DefFlt1: TcxGridDBColumn;
    ADOQueryKH: TADOQuery;
    DSKH: TDataSource;
    v2Column14: TcxGridDBColumn;
    v2Column2: TcxGridDBColumn;
    v2Column13: TcxGridDBColumn;
    v2Column15: TcxGridDBColumn;
    v2Column16: TcxGridDBColumn;
    ToolBar2: TToolBar;
    ToolButton4: TToolButton;
    ToolButton5: TToolButton;
    cxGrid1: TcxGrid;
    Tv3: TcxGridDBTableView;
    cxGridDBColumn1: TcxGridDBColumn;
    cxGridDBColumn2: TcxGridDBColumn;
    cxGridDBColumn3: TcxGridDBColumn;
    cxGridDBColumn4: TcxGridDBColumn;
    cxGridDBColumn5: TcxGridDBColumn;
    Tv3Column1: TcxGridDBColumn;
    v3Column1: TcxGridDBColumn;
    v3Column2: TcxGridDBColumn;
    cxGridLevel2: TcxGridLevel;
    CDS_LXR: TClientDataSet;
    DSLXR: TDataSource;
    v2Column17: TcxGridDBColumn;
    v2Column18: TcxGridDBColumn;
    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);
  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_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    SCreateCDS20(ADOQueryMain,CDS_HZ);
    SInitCDSData20(ADOQueryMain,CDS_HZ);

    with ADOQueryMain do
    begin
      Filtered:=False;
      Close;
      SQL.Clear;
      sql.Add(' select * from KH_Zdy_Att_Sub where ATID='''+Trim(FCYID)+''' and Type=''KHName''');
      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',Tv2,'客户自定义');
  Close;
end;
procedure TfrmZdyAttInput.FormShow(Sender: TObject);
begin
  ReadCxGrid('客户资料录入1',Tv2,'客户自定义');
  InitGrid();
  if Trim(FCYID)='' then
  begin
    CDS_HZ.Append;
    CDS_HZ.FieldByName('DEFstr5').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_Attachment',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_Attachment',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_Attachment',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_Attachment',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_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID)='' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('zdyCode').Value:=Trim(fzdyCode);
      end else
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('Edittime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('ATID').Value:=Trim(maxId);
      FieldByName('ZdyName').Value:=Trim(CDS_HZ.fieldbyname('ZdyName').AsString);
      RTSetSaveDataCDS(ADOQueryCmd,Tv2,CDS_HZ,'',0);
      FieldByName('Type').Value:='KHName';
      //FieldByName('khType').Value:=trim(fkhType);
      FieldByName('DefFlt1').Value:=CDS_HZ.fieldbyname('DefFlt1').AsFloat;
   //   FieldByName('Filler').Value:=Trim(DName);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ZdyName='''+Trim(CDS_HZ.fieldbyname('ZdyName').AsString)+'''');
      sql.Add(' and Type=''KHName'' ');
      sql.Add(' and isnull(khType,'''')='+quotedstr(trim(fkhType)));
      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_Attachment where ZdyCode='''+Trim(CDS_HZ.fieldbyname('ZdyCode').AsString)+'''');
      sql.Add(' and Type=''KHName'' ');
      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 not CDS_LXR.IsEmpty then
    begin
      with CDS_LXR do
      begin
      CDS_LXR.First;
      while not Eof do
      begin
        FYSSubId:=Trim(CDS_LXR.fieldbyname('ASID').AsString);
        if Trim(FYSSubId)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxno,'AS','KH_Zdy_Att_Sub',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 KH_Zdy_Att_Sub where ASID='''+Trim(maxno)+'''');
          Open;
        end;
        with ADOQueryCmd do
        begin
          if Trim(FYSSubId)='' then
          begin
            Append;
            FieldByName('Filler').Value:=Trim(DName);
          end else
          begin
            Edit;
            FieldByName('Editer').Value:=Trim(DName);
            FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
          end;
          FieldByName('ASID').Value:=Trim(maxno);
          FieldByName('ATID').Value:=Trim(CDS_HZ.fieldbyname('ATID').AsString);
          FieldByName('SDEFstr1').Value:=CDS_LXR.fieldbyname('SDEFstr1').Value;
          FieldByName('SDEFstr2').Value:=CDS_LXR.fieldbyname('SDEFstr2').Value;
          FieldByName('SDEFstr3').Value:=CDS_LXR.fieldbyname('SDEFstr3').Value;
          FieldByName('SDEFstr4').Value:=CDS_LXR.fieldbyname('SDEFstr4').Value;
          FieldByName('SDEFstr5').Value:=CDS_LXR.fieldbyname('SDEFstr5').Value;
          FieldByName('SDEFstr6').Value:=CDS_LXR.fieldbyname('SDEFstr6').Value;
          FieldByName('SDEFstr7').Value:=CDS_LXR.fieldbyname('SDEFstr7').Value;
          FieldByName('SDEFstr8').Value:=CDS_LXR.fieldbyname('SDEFstr8').Value;
          FieldByName('SDEFstr9').Value:=CDS_LXR.fieldbyname('SDEFstr9').Value;
          FieldByName('SDEFstr10').Value:=CDS_LXR.fieldbyname('SDEFstr10').Value;
          FieldByName('OtherLXFS').Value:=CDS_LXR.fieldbyname('OtherLXFS').Value;
          FieldByName('Type').Value:='KHName';
          Post;
        end;
        with CDS_LXR do
        begin
          Edit;
          FieldByName('ASID').Value:=Trim(maxno);
          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('ZdyName').AsString)='' then
  begin
    Application.MessageBox('客户名称不能为空!','提示',0);
    Exit;
  end;
  IF not CDS_LXR.IsEmpty then
  begin
    if CDS_LXR.Locate('SDEFstr6',null,[])=True then
    begin
    Application.MessageBox('Email不能为空！','提示',0);
    Exit;
    end;
  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
  with CDS_LXR do
  begin
    Append;
    Post;
  end;
end;

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

end.
