unit U_GSInPutMainBank;

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
  TfrmGSInPutMainBank = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel2: TPanel;
    Label13: TLabel;
    Label19: TLabel;
    KHLXR: TEdit;
    KHName: TEdit;
    ToolBar1: TToolBar;
    TBAdd: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    Label12: TLabel;
    Label23: TLabel;
    Button3: TButton;
    HangNo: TEdit;
    BankNo: TEdit;
    BankName: TEdit;
    BankType: TComboBox;
    HuMing: TEdit;
    TBAddXJ: TToolButton;
    Label1: TLabel;
    BankNameEng: TEdit;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBAddClick(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure TBAddXJClick(Sender: TObject);
    procedure BankTypeChange(Sender: TObject);
  private
    { Private declarations }
    function SaveDataBank():Boolean;
    procedure initBtnColor(panel12:TPanel);
  public
    { Public declarations }
    FKHMainid,FBKID:String;
    PState:Integer;
  end;
var
  frmGSInPutMainBank: TfrmGSInPutMainBank;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp;

{$R *.dfm}

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

procedure TfrmGSInPutMainBank.TBCloseClick(Sender: TObject);
begin
  Close; 
end;
procedure TfrmGSInPutMainBank.FormShow(Sender: TObject);
begin
  KHName.SetFocus;
  with ADOQueryTemp do
  begin
    Close;
    SQL.Clear;
    sql.Add('select * from KH_Main where KHMainID='''+Trim(FKHMainid)+'''');
    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 BKID='''+Trim(FBKID)+'''');
    Open;
  end;
  SCSHDataNew(ADOQueryTemp,Panel1,3);
  BankNo.Hint:=Trim(FBKID);
  if PState=0 then
  begin
    Button3.Caption:='新增保存';
    BankType.Enabled:=False;
    BankType.Color:=clMenu;
    TBAdd.Visible:=True;
    TBAddXJ.Visible:=True;
  end else
  if PState=1 then
  begin
    Button3.Caption:='修改保存';
    BankType.Enabled:=True;
    BankType.Color:=clWindow;
    TBAdd.Visible:=False;
    TBAddXJ.Visible:=False;
  end  else
  if PState=2 then
  begin
    Button3.Visible:=False;
    BankType.Enabled:=False;
    BankType.Color:=clMenu;
    TBAdd.Visible:=False;
    TBAddXJ.Visible:=False;
    initBtnColor(Panel1);
  end;
end;



procedure TfrmGSInPutMainBank.TBAddClick(Sender: TObject);
begin
  BankNo.Hint:='';
  BankType.ItemIndex:=0;
  HuMing.Text:=Trim(KHName.Text);
  HangNo.SetFocus;
end;

procedure TfrmGSInPutMainBank.Button3Click(Sender: TObject);
begin
 
  if Trim(BankType.Text)='' then
  begin
    application.MessageBox('类型不能为空','提示');
    exit;
  end;
  if Trim(BankNo.Text)='' then
  begin
    application.MessageBox('账号不能为空','提示');
    exit;
  end;
  if Trim(HuMing.Text)='' then
  begin
    application.MessageBox('户名不能为空','提示');
    exit;
  end;
  if SaveDataBank() then
  begin
    Application.MessageBox('保存成功!','提示',0);
    ModalResult:=1;
  end;
end;
function TfrmGSInPutMainBank.SaveDataBank():Boolean;
var
  maxno:String;
begin
  result:=false;
  try
    ADOQueryCmd.Connection.BeginTrans;
    if PState=0 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 PState=1 then
    begin
      maxno:=Trim(FBKID);
    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 PState=0 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 PState=1 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(FKHMainid);
      FieldByName('BKID').Value:=Trim(maxno);
      RTSetsavedata(ADOQueryCmd,'KH_Sub_Bank',Panel1,3);
      if BankType.ItemIndex=0 then
      begin
        FieldByName('HuMing').Value:=Trim(KHName.Text);
      end;
      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(FKHMainid)+''' 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(FKHMainid)+''' and valid=''Y'' ');
      Open;
    end;
    if ADOQueryCmd.RecordCount>1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('账号重复!','提示',0);
      Exit;
    end;
    if PState=0 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 and A.Valid=''Y''),0)+1');
        sql.Add(' where BKID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
      with ADOQueryCmd do
      begin
        Close;
        sql.Clear;
        sql.Add(' UPdate KH_Sub_Bank Set BKNO=isnull((select KHNo from KH_Main A where A.KHMainId=KH_Sub_Bank.KHMainId),'''')+''-0''+Cast(XH as varchar(20))');
        sql.Add(' where BKID='''+Trim(maxno)+'''');
        ExecSQL;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    result:=true;
  except
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('数据保存失败!','提示',0);
  end;
end;

procedure TfrmGSInPutMainBank.TBAddXJClick(Sender: TObject);
begin
  BankNo.Hint:='';
  BankType.ItemIndex:=1;
  HuMing.Text:=Trim(KHLXR.Text);
  HangNo.SetFocus;
end;

procedure TfrmGSInPutMainBank.BankTypeChange(Sender: TObject);
begin
  if BankType.ItemIndex=0 then
  begin
    HuMing.Color:=clMenu;
    HuMing.ReadOnly:=True;
    HuMing.Text:=Trim(KHName.Text);
  end else
  begin
    HuMing.Color:=clWindow;
    HuMing.ReadOnly:=False;
  end;

end;
procedure TfrmGSInPutMainBank.initBtnColor(panel12:TPanel);
var
  i:Integer;
begin
  with panel12 do
  begin
    for i:=0 to panel12.ControlCount-1 do
    begin
      if Controls[i] is TEdit then
      begin
        TEdit(Controls[i]).Color:=clMenu;
        TEdit(Controls[i]).ReadOnly:=True;
      end else
      if Controls[i] is TMemo then
      begin
        TMemo(Controls[i]).Color:=clMenu;
        TMemo(Controls[i]).ReadOnly:=True;
      end  else
      if Controls[i] is TComboBox then
      begin
        TComboBox(Controls[i]).Color:=clMenu;
        TComboBox(Controls[i]).Enabled:=False;
      end;
    end;
  end;
end;

end.
