unit U_PBGYSInPutSub;

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
  TfrmPBGYSInPutSub = class(TForm)
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    Panel2: TPanel;
    Label15: TLabel;
    Label16: TLabel;
    Label13: TLabel;
    Label17: TLabel;
    Label18: TLabel;
    Label19: TLabel;
    Label20: TLabel;
    Label22: TLabel;
    KHAddMX: TMemo;
    KHSJAddMX: TMemo;
    KHLXRMX: TEdit;
    KHTelMX: TEdit;
    KHNameMX: TEdit;
    KHNameJCMX: TEdit;
    KHSHMX: TEdit;
    NoteMX: TEdit;
    ToolBar1: TToolBar;
    TBSave: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    Label1: TLabel;
    LabGYSName: TLabel;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function  SaveData():Boolean;
    procedure initBtnColor(panel12:TPanel);
  public
    { Public declarations }
    FKHMainid,FTSFlag,FKHSubId:String;
  end;
var
  frmPBGYSInPutSub: TfrmPBGYSInPutSub;

implementation
uses
  U_DataLink,U_Fun,U_ZDYHelp,U_FjList_RZ;

{$R *.dfm}

procedure TfrmPBGYSInPutSub.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);
end;

procedure TfrmPBGYSInPutSub.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  Action:=caFree;
end;
procedure TfrmPBGYSInPutSub.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;
procedure TfrmPBGYSInPutSub.TBCloseClick(Sender: TObject);
begin
  Close; 
end;
procedure TfrmPBGYSInPutSub.FormShow(Sender: TObject);
begin
  KHNameMX.SetFocus;
  InitGrid();
  if TBSave.Visible=False then
  begin
    initBtnColor(Panel2);
  end;
end;

function TfrmPBGYSInPutSub.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;
      end
      else
      begin
        Edit;
      end;
      FieldByName('DataType').Value:='子公司';
      FieldByName('KHSubid').Value:=Trim(maxno);
      FieldByName('KHMainID').Value:=Trim(FKHMainid);
      RTSetsavedata(ADOQueryCmd,'KH_Sub',Panel2,2);
      FieldByName('Valid').Value:='Y';
      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)+''' 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 where KHNameJCMX='''+Trim(KHNameJCMX.Text)+'''');
      sql.Add(' and KHMainId='''+Trim(FKHMainid)+''' and Valid=''Y'' ');
      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 and A.Valid=''Y''),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 TfrmPBGYSInPutSub.TBSaveClick(Sender: TObject);
begin
  Panel2.SetFocus;
  IF trim(KHNameMX.Text)='' then
  begin
    application.MessageBox('子公司不能为空！','提示信息',MB_ICONERROR);
    exit;
  end;
  if Trim(KHNameJCMX.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;


end.
