unit U_ZdyAttInputGYS;

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
  TfrmZdyAttInputGYS = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    RM1: TRMGridReport;
    RMDB_Main: TRMDBDataSet;
    cxGridPopupMenu2: TcxGridPopupMenu;
    DS_HZ: TDataSource;
    CDS_HZ: TClientDataSet;
    ToolBar1: TToolBar;
    ToolButton3: TToolButton;
    TBClose: TToolButton;
    cxGridPopupMenu1: TcxGridPopupMenu;
    DS_HZ2: TDataSource;
    CDS_HZ2: TClientDataSet;
    ADOQueryMain2: TADOQuery;
    cxGridPopupMenu3: TcxGridPopupMenu;
    DS_HZ3: TDataSource;
    ADOQueryMain3: TADOQuery;
    CDS_HZ3: TClientDataSet;
    Panel4: TPanel;
    Label3: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    ZdyName: TEdit;
    ZdyCode: TEdit;
    DEFStr4: TEdit;
    ZdyNameZ: TEdit;
    ZL_PBGYS: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    Label8: TLabel;
    defstr2: TEdit;
    DEFNote1: TEdit;
    Label9: TLabel;
    Label10: TLabel;
    ZL_CPFW: TEdit;
    Note: TMemo;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure ToolButton3Click(Sender: TObject);
    procedure ToolButton2Click(Sender: TObject);
    procedure ToolButton4Click(Sender: TObject);
    procedure ToolButton5Click(Sender: TObject);
    procedure ToolButton6Click(Sender: TObject);
    procedure ToolButton8Click(Sender: TObject);
    procedure TV2MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    function  SaveData2():Boolean;
  public
    { Public declarations }
    RKFlag,FCYID,FCYID2:String;
    fDEFstr5:string;
  end;

var
  frmZdyAttInputGYS: TfrmZdyAttInputGYS;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

procedure TfrmZdyAttInputGYS.InitGrid();
begin
  with ADOQueryMain do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
    Open;
  end;
  //主表
  SCSHDataNew(ADOQueryMain,Panel4,2);
  //表2
  with ADOQueryMain2 do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select * from GYS_ZHXX where ATID='''+Trim(FCYID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain2,CDS_HZ2);
  SInitCDSData20(ADOQueryMain2,CDS_HZ2);
  //表3
  with ADOQueryMain3 do
  begin
    Filtered:=False;
    Close;
    SQL.Clear;
    sql.Add(' select * from GYS_LXFS where ATID='''+Trim(FCYID)+'''');
    Open;
  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);     

end;


procedure TfrmZdyAttInputGYS.FormDestroy(Sender: TObject);
begin
  frmZdyAttInputGYS:=nil;
end;

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

procedure TfrmZdyAttInputGYS.TBCloseClick(Sender: TObject);
begin
  Close;
end;
procedure TfrmZdyAttInputGYS.FormShow(Sender: TObject);
begin
  InitGrid();
end;

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

function TfrmZdyAttInputGYS.SaveData():Boolean;
var
  maxId,CRID,fzdyCode,FYSSubId,maxno,maxnum:String;
begin
///取maxId，得出ATID
  result:=false;
  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 GetLSNo(ADOQueryCmd,fzdyCode,'PB','KH_Zdy_Attachment',4,0)=False then
    begin
      Result:=False;
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('取最大号失败！','提示',0);
      Exit;
    end;
  end else
  begin
    maxId:=Trim(FCYID);
  end;
///
  try
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Zdy_Attachment where ATID='''+Trim(FCYID)+'''');
      Open;
      If IsEmpty 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(ADOQueryTmp);
      end;
      FieldByName('ATID').Value:=Trim(maxId);
      FieldByName('Type').Value:='GYS';
//      FieldByName('ZdyCode').Value:=Trim(maxId);
      FieldByName('ZdyName').Value:=trim(ZdyName.Text);
      FieldByName('ZdyNameZ').Value:=trim(ZdyNameZ.Text);
      FieldByName('ZL_PBGYS').Value:=trim(ZL_PBGYS.Text);
      FieldByName('DEFStr4').Value:=trim(DEFStr4.Text);
      FieldByName('defstr2').Value:=trim(defstr2.Text);
      FieldByName('DEFNote1').Value:=trim(DEFNote1.Text);
      FieldByName('Note').Value:=trim(Note.Text);
      FieldByName('ZL_CPFW').Value:=trim(ZL_CPFW.Text);
      post;
    end;
    if Trim(FCYID)='' then
    begin
      with CDS_HZ2 do
      begin
        Append;
        FieldByName('ZdyCode').Value:=Trim(fzdyCode);
        FieldByName('ZdyName').Value:=Trim(ZdyName.Text);
        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;
          end
          else
          begin
            Edit;
//          FieldByName('ZHXX_BH').Value:=Trim(maxno);

          end;
          FieldByName('ZDYCode').Value:=Trim(CDS_HZ2.fieldbyname('ZDYCode').AsString);
          FieldByName('ZdyName').Value:=Trim(CDS_HZ2.fieldbyname('ZdyName').AsString);
          FieldByName('ZHXX_ZH').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZH').AsString);
          FieldByName('ZHXX_SH').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_SH').AsString);
          FieldByName('ZHXX_CWFZR').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_CWFZR').AsString);
          FieldByName('ZHXX_DH').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_DH').AsString);
          FieldByName('ZHXX_SJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_SJ').AsString);
          FieldByName('ZHXX_BZ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_BZ').AsString);
          FieldByName('ZHXX_ZJ').Value:=Trim(maxno);
          FieldByName('ATID').Value:=Trim(maxId);
          post;
        end;
        CDS_HZ2.Next;
      end;
    end;
    //表3
    with CDS_HZ3 do
    begin
      CDS_HZ3.First;
      while not Eof do
      begin
        if Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString)='' then
        begin
          if GetLSNo(ADOQueryCmd,maxnum,'LX','LXFS_ZJ',4,1)=False then
          begin
            ADOQueryCmd.Connection.RollbackTrans;
            Application.MessageBox('取系人最大ID失败!','提示',0);
            Exit;
          end;
        end else
        begin
          maxnum:=Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString);
        end;
        with ADOQueryCmd do
        begin
          Close;
          SQL.Clear;
          sql.Add('select * from GYS_LXFS where LXFS_ZJ='''+Trim(maxnum)+'''');
          Open;
          If Trim(FYSSubId)='' then
          begin
            append;
          end
          else
          begin
            Edit;
          end;
          FieldByName('LXFS_BH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BH').AsString);
          FieldByName('LXFS_LXR').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_LXR').AsString);
          FieldByName('LXFS_SJ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_SJ').AsString);
          FieldByName('LXFS_DZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DZ').AsString);
          FieldByName('LXFS_DH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DH').AsString);
          FieldByName('LXFS_CZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_CZ').AsString);
          FieldByName('LXFS_BZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BZ').AsString);
          FieldByName('LXFS_ZJ').Value:=Trim(maxnum);
          FieldByName('ATID').Value:=Trim(maxId);
          FieldByName('ZHXX_ZJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
          post;
        end;
        CDS_HZ3.Next;
      end;
    end;

    ADOQueryCmd.Connection.CommitTrans;  //代表完结
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据保存错误!','提示',0);
  end;
end;

procedure TfrmZdyAttInputGYS.ToolButton3Click(Sender: TObject);
begin
 Panel4.SetFocus;
 IF trim(zdyName.Text)='' then
  begin
    application.MessageBox('供应商不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;

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

procedure TfrmZdyAttInputGYS.ToolButton2Click(Sender: TObject);
var
   maxId:String;
begin
  with CDS_HZ2 do
  begin
    Append;

    post;
  end;
end;

procedure TfrmZdyAttInputGYS.ToolButton4Click(Sender: TObject);
begin
  IF CDS_HZ2.IsEmpty then exit;
  if Application.MessageBox('确定要删除数据吗？','提示',32+4)<>IDYES then Exit;
  //子表删除
{  IF CDS_HZ3.IsEmpty then exit;
  CDS_HZ3.DisableControls;
  with CDS_HZ3 do
  begin
    if Trim(CDS_HZ3.fieldbyname('ZHXX_ZJ').AsString)<>'' then
    begin
      with ADOQueryMain3 do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete from GYS_LXFS where ZHXX_ZJ ='''+Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)+'''');
        ExecSQL;
      end;
    end;
    Delete;
  end;      }
  //主表删除
  CDS_HZ2.DisableControls;
  with CDS_HZ2 do
  begin
    if Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)<>'' then
    begin

      with ADOQueryMain2 do
      begin
        Close;
        SQL.Clear;
        sql.Add('delete from GYS_ZHXX where ZHXX_ZJ ='''+Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString)+'''');
        ExecSQL;
      end;
    end;
    Delete;
  end;
  CDS_HZ2.EnableControls;
  CDS_HZ3.EnableControls;
end;

procedure TfrmZdyAttInputGYS.ToolButton5Click(Sender: TObject);
begin
  with CDS_HZ3 do
  begin
    Append;

    post;
  end;
end;

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

procedure TfrmZdyAttInputGYS.ToolButton8Click(Sender: TObject);
begin
  if CDS_HZ3.IsEmpty then exit;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;

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

function TfrmZdyAttInputGYS.SaveData2():Boolean;
var
  maxId,CRID,fzdyCode,FYSSubId,maxno:String;
begin
///判断、生成主键
  with CDS_HZ3 do
  begin
    CDS_HZ3.First;
    while not Eof do
    begin
      FYSSubId:=Trim(CDS_HZ3.fieldbyname('LXFS_ZJ').AsString);
      if Trim(FYSSubId)='' then
      begin
        if GetLSNo(ADOQueryMain3,maxno,'LX','LXFS_ZJ',4,1)=False then
        begin
//          ADOQueryCmd.Connection.RollbackTrans;    //回滚事务
          Application.MessageBox('取系人最大ID失败!','提示',0);
          Exit;
        end;
      end else
      begin
        maxno:=Trim(FYSSubId);
      end;
      ADOQueryCmd.Connection.BeginTrans;  //代表开始事务
      with ADOQueryCmd do
      begin
        Close;
        SQL.Clear;
        sql.Add('select * from GYS_LXFS where LXFS_ZJ='''+Trim(maxno)+'''');
        Open;
        If Trim(FYSSubId)='' then
        begin
          append;
        end
        else
        begin
          Edit;
        end;
      //  FieldByName('ATID').Value:=Trim(maxId); //ZhuG
        FieldByName('LXFS_BH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BH').AsString);
        FieldByName('LXFS_LXR').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_LXR').AsString);
        FieldByName('LXFS_SJ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_SJ').AsString);
        FieldByName('LXFS_DZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DZ').AsString);
        FieldByName('LXFS_DH').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_DH').AsString);
        FieldByName('LXFS_CZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_CZ').AsString);
        FieldByName('LXFS_BZ').Value:=Trim(CDS_HZ3.fieldbyname('LXFS_BZ').AsString);
        FieldByName('LXFS_ZJ').Value:=Trim(maxno);
        FieldByName('ATID').Value:=Trim(CDS_HZ2.fieldbyname('ATID').AsString);
        FieldByName('ZHXX_ZJ').Value:=Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').AsString);
        post;
      end;
      CDS_HZ3.Next;
    end;
  end;
  ADOQueryCmd.Connection.CommitTrans;  //代表完结事务
  result:=true;
end;

procedure TfrmZdyAttInputGYS.TV2MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  with ADOQueryMain3 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from GYS_LXFS where ZHXX_ZJ='''+Trim(CDS_HZ2.fieldbyname('ZHXX_ZJ').asstring)+'''');
    Open;

  end;
  SCreateCDS20(ADOQueryMain3,CDS_HZ3);
  SInitCDSData20(ADOQueryMain3,CDS_HZ3);
end;

end.
