unit U_PBGYSInPutGR;

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, cxPC;

type
  TfrmPBGYSInPutGR = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel2: TPanel;
    Label15: TLabel;
    Label13: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    KHSJAddMX: TMemo;
    KHLXRMX: TEdit;
    KHTelMX: TEdit;
    KHNameMX: TEdit;
    NoteMX: TEdit;
    cxGrid3: TcxGrid;
    TV3: TcxGridDBTableView;
    V3Column1: TcxGridDBColumn;
    V3Column2: TcxGridDBColumn;
    cxGridDBBankName: TcxGridDBColumn;
    cxGridLevel3: TcxGridLevel;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBAdd: TToolButton;
    TBDel: TToolButton;
    TBClose: TToolButton;
    LabGYSName: TLabel;
    BankNo: TEdit;
    BankName: TEdit;
    HangNo: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    Panel1: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Button3: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Button1: TButton;
    Label11: TLabel;
    DS_HZ3: TDataSource;
    CDS_HZ3: TClientDataSet;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure TBDelClick(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure TV3CellDblClick(Sender: TcxCustomGridTableView;
      ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
      AShift: TShiftState; var AHandled: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    function SaveDataBank():Boolean;
  public
    { Public declarations }
    FKHMainid,FTSFlag,FKHSubId:String;
  end;
var
  frmPBGYSInPutGR: TfrmPBGYSInPutGR;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FjList_RZ;

{$R *.dfm}

procedure TfrmPBGYSInPutGR.InitGrid();
begin
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Sub where KHSubID='''+Trim(FKHSubId)+'''');
    Open;
  end;
  //主表
  if ADOQueryTemp.IsEmpty=false then
    SCSHDataNew(ADOQueryTemp,Panel2,2);
  with ADOQueryTemp do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from KH_Sub_Bank where KHSubId='''+Trim(FKHSubId)+''' and Valid=''Y'' ');
    Open;
  end;
  SCreateCDS20(ADOQueryTemp,CDS_HZ3);
  SInitCDSData20(ADOQueryTemp,CDS_HZ3);
end;

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

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

function TfrmPBGYSInPutGR.SaveData():Boolean;
var
  maxno,FGYSNO:String;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FKHSubId)='' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'KS','KH_Sub',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取个人编号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FKHSubId);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Sub where KHSubId='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      If IsEmpty then
      begin
        append;
        FieldByName('DataType').Value:='个人';
      end
      else
      begin
        Edit;
      end;
      FieldByName('KHSubid').Value:=Trim(maxno);
      FieldByName('KHMainID').Value:=Trim(FKHMainid);
      RTSetsavedata(ADOQueryCmd,'KH_Sub',Panel2,2);
      FieldByName('Valid').Value:='Y';
      FieldByName('KHNameJCMX').Value:=Trim(KHNameMX.Text);
      post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Sub where KHNameMX='''+Trim(KHNameMX.Text)+'''');
      sql.Add(' and KHMainId='''+Trim(FKHMainid)+'''');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('个人重复!','提示',0);
      Exit;
    end;
    if Trim(FKHSubId)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate KH_Sub Set XHNo=isnull((select Max(XHNo) from KH_Sub A where A.KHMainId=KH_Sub.KHMainId),0)+1');
        sql.Add(' where KHSubId='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FKHSubId:=maxno;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Result:=false;
    Application.MessageBox('数据保存失败!','提示',0);
    Exit;
  end;
end;

procedure TfrmPBGYSInPutGR.TBSaveClick(Sender: TObject);
begin
  Panel2.SetFocus;
  IF trim(KHNameMX.Text)='' then
  begin
    application.MessageBox('个人不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;
  if Application.MessageBox('确定要保存数据吗？','提示',32+4)<>IDYES then Exit;
  if SaveData() then
  begin
    if Trim(BankNo.Hint)<>'' then
    begin
      SaveDataBank();
    end;
    if FTSFlag<>'99' then
    begin
      Application.MessageBox('保存成功!','提示',0);
      ModalResult:=1;
    end;

  end;
end;
procedure TfrmPBGYSInPutGR.TBDelClick(Sender: TObject);
begin
  IF CDS_HZ3.IsEmpty then exit;
  if Application.MessageBox('确定要删除开户行数据吗？','提示',32+4)<>IDYES then Exit;
  with ADOQueryCmd do
  begin
    Close;
    SQL.Clear;
    sql.Add('Update KH_Sub_Bank Set Valid=''N'',DelTime=getdate(),Deler='''+Trim(DName)+''',DelerCode='''+Trim(DCode)+'''');
    sql.add('where BKID='''+Trim(CDS_HZ3.fieldbyname('BKID').AsString)+'''');
    ExecSQL;
  end;
  CDS_HZ3.Delete;
end;

procedure TfrmPBGYSInPutGR.TBAddClick(Sender: TObject);
begin
  Panel1.Visible:=True;
  SClearData(Panel1,0);
  BankNo.Hint:='';
end;

procedure TfrmPBGYSInPutGR.Button3Click(Sender: TObject);
begin
  FTSFlag:='99';
  if Trim(FKHSubId)='' then
  begin
    TBSave.Click;
  end;
  if Trim(Edit2.Text)='' then
  begin
    application.MessageBox('账号不能为空','提示');
    exit;
  end;
  if Trim(Edit3.Text)='' then
  begin
    application.MessageBox('行址不能为空','提示');
    exit;
  end;
  HangNo.Text:=Trim(Edit1.Text);
  BankNo.Text:=Trim(Edit2.Text);
  BankName.Text:=Trim(Edit3.Text);
  if SaveDataBank() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;
function TfrmPBGYSInPutGR.SaveDataBank():Boolean;
var
  maxno:String;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FTSFlag)='99' then
    begin
      if GetLSNo(ADOQueryCmd,maxno,'BK','KH_Sub_Bank',4,1)=False then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('开户行最大ID失败!','提示',0);
        Exit;
      end;
    end else
    if Trim(FTSFlag)='' then
    begin
      maxno:=Trim(CDS_HZ3.FieldByName('BKID').AsString);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from KH_Sub_Bank where BKID='''+Trim(maxno)+'''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FTSFlag)='99' then
      begin
        Append;
        FieldByName('Filler').Value:=Trim(DName);
        FieldByName('FillerCode').Value:=Trim(DCode);
        FieldByName('FillTime').Value:=SGetServerDateTime(ADOQueryTemp);
        FieldByName('Valid').Value:='Y';
      end else
      if Trim(FTSFlag)='' then
      begin
        Edit;
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditerCode').Value:=Trim(DCode);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('KHMainId').Value:=Trim(FKHMainid);
      FieldByName('KHSubId').Value:=Trim(FKHSubId);
      FieldByName('BKID').Value:=Trim(maxno);
      RTSetsavedata(ADOQueryCmd,'KH_Sub_Bank',Panel2,3);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from KH_Sub_Bank where BankName='''+Trim(BankName.Text)+'''');
      sql.Add(' and KHSubId='''+Trim(FKHSubId)+''' and valid=''Y'' ');
      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_Sub_Bank where BankNo='''+Trim(BankNo.Text)+'''');
      sql.Add(' and KHSubId='''+Trim(FKHSubId)+''' and valid=''Y'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('账号重复!','提示',0);
      Exit;
    end;
    if Trim(BankNo.Hint)='' then
    begin
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add('UPdate KH_Sub_Bank Set XH=isnull((select Max(XH) from KH_Sub_Bank A where A.KHSubId=KH_Sub_Bank.KHSubId),0)+1');
        sql.Add(' where BKID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据保存失败!','提示',0);
  end;
end;

procedure TfrmPBGYSInPutGR.TV3CellDblClick(
  Sender: TcxCustomGridTableView;
  ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
  AShift: TShiftState; var AHandled: Boolean);
begin
  if CDS_HZ3.IsEmpty then
  begin
    BankName.Text:='';
    BankNo.Text:='';
    BankNo.Hint:='';
    Exit;
  end;
  BankName.Text:=Trim(CDS_HZ3.fieldbyname('BankName').AsString);
  BankNo.Text:=Trim(CDS_HZ3.fieldbyname('BankNo').AsString);
  HangNo.Text:=Trim(CDS_HZ3.fieldbyname('HangNo').AsString);
  BankNo.Hint:=Trim(CDS_HZ3.fieldbyname('BKID').AsString);
end;

procedure TfrmPBGYSInPutGR.Button1Click(Sender: TObject);
begin
  Panel1.Visible:=False;
end;

end.
