unit U_GYSInPutTab;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData,
  cxDataStorage, cxEdit, DB, cxDBData, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls,
  cxGridCustomView, cxGrid, cxMemo, cxRichEdit, ComCtrls, cxContainer,
  cxTextEdit, cxMaskEdit, cxButtonEdit, StdCtrls, ToolWin, DBClient, ADODB,
  ExtCtrls, BtnEdit, cxCalendar,StrUtils, cxDropDownEdit,jpeg,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdFTP, cxPC,
  cxGridCustomPopupMenu, cxGridPopupMenu;

type
  TfrmGYSInPutTab = class(TForm)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ScrollBox1: TScrollBox;
    ADOTemp: TADOQuery;
    ADOCmd: TADOQuery;
    ADOQuery1: TADOQuery;
    TBSave: TToolButton;
    Panel3: TPanel;
    Label1: TLabel;
    Label7: TLabel;
    Label10: TLabel;
    Label11: TLabel;
    Label13: TLabel;
    Label33: TLabel;
    Label34: TLabel;
    KHName: TEdit;
    ZKAddress: TEdit;
    ZKTelNo: TEdit;
    ZKFax: TEdit;
    KHNameJC: TEdit;
    KHCode: TEdit;
    KHType: TBtnEditA;
    Label2: TLabel;
    TJKHName: TEdit;
    Label3: TLabel;
    LXR: TEdit;
    Label4: TLabel;
    LXRTel: TEdit;
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure SYRNameBtnUpClick(Sender: TObject);
    procedure SKBankBtnDnClick(Sender: TObject);
    procedure TBSaveClick(Sender: TObject);
    procedure KHTypeBtnClick(Sender: TObject);
  private
    procedure InitData();
    function  SaveData():Boolean;
    { Private declarations }
  public
    canshu1:String;
    PState,CopyInt:Integer;
    FMainId:String;
    { Public declarations }
  end;

var
  frmGYSInPutTab: TfrmGYSInPutTab;
implementation
uses
  U_DataLink,U_ZDYHelp,U_RTFun,U_ZDYHelpSel;

{$R *.dfm}

procedure TfrmGYSInPutTab.TBCloseClick(Sender: TObject);
begin
  Close;
end;

procedure TfrmGYSInPutTab.InitData();
begin

  with ADOQuery1 do
  begin
    Close;
    sql.Clear;
    sql.Add('select * from ZH_KH_Info where ZKId='''+Trim(FMainId)+'''');
    Open;
  end;
  SCSHDataNew(ADOQuery1,Panel3,0);
  KHCode.SetFocus;
  {if Trim(FMainId)<>'' then
  begin
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from JYOrder_Main_MD_DuiZhang ');
      sql.Add(' where FactoryNo='''+Trim(FMainId)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      KHCode.ReadOnly:=True;
      KHName.ReadOnly:=True;
      KHNameJC.ReadOnly:=True;
      Exit;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from YF_Money_PaiKuan ');
      sql.Add(' where FactoryNo='''+Trim(FMainId)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      KHCode.ReadOnly:=True;
      KHName.ReadOnly:=True;
      KHNameJC.ReadOnly:=True;
      Exit;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from JYOrder_Main_MD ');
      sql.Add(' where FactoryNo='''+Trim(FMainId)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      KHCode.ReadOnly:=True;
      KHName.ReadOnly:=True;
      KHNameJC.ReadOnly:=True;
      exit;
    end;
    with ADOTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select Top 1* from YF_Money_CR ');
      sql.Add(' where FactoryNo='''+Trim(FMainId)+'''');
      Open;
    end;
    if ADOTemp.IsEmpty=False then
    begin
      KHCode.ReadOnly:=True;
      KHName.ReadOnly:=True;
      KHNameJC.ReadOnly:=True;
    end;
  end; }

end;


procedure TfrmGYSInPutTab.FormShow(Sender: TObject);
begin
  InitData();
end;

function TfrmGYSInPutTab.SaveData():Boolean;
var
  maxno:String;
begin
  Result:=False;
  try
    ADOCmd.Connection.BeginTrans;
    ///保存主表
    if Trim(FMainId)='' then
    begin
      if GetLSNo(ADOCmd,maxno,'GS','ZH_KH_Info',3,1)=False then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败!','提示',0);
        Exit;
      end;
    end else
    begin
      maxno:=Trim(FMainId);
    end;
    with ADOCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from ZH_KH_Info where ZKId='''+Trim(FMainId)+'''');
      Open;
    end;
    with ADOCmd do
    begin
      if Trim(FMainId)='' then
      begin
        Append;
      end
      else begin
        Edit;
      end;
      FieldByName('ZKId').Value:=Trim(maxno);
      if Trim(FMainId)='' then
      begin
        FieldByName('Filler').Value:=Trim(DName);
      end else
      begin
        FieldByName('Editer').Value:=Trim(DName);
        FieldByName('EditTime').Value:=SGetServerDateTime(ADOTemp);
      end;
      RTSetsavedata(ADOCmd,'ZH_KH_Info',Panel3,0);
      FieldByName('Valid').Value:='Y';
      FieldByName('Type').Value:='GYS';
      Post;
    end;
    if Trim(KHName.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHName='''+Trim(KHName.Text)+'''');
        SQL.Add(' and Type=''GYS'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('供应商名称重复!','提示',0);
        Exit;
      end;
    end;
    if Trim(KHCode.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHCode='''+Trim(KHCode.Text)+'''');
        SQL.Add(' and Type=''GYS'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('供应商编号重复!','提示',0);
        Exit;
      end;
    end;

    if Trim(KHNameJC.Text)<>'' then
    begin
      with ADOTemp do
      begin
        Close;
        sql.Clear;
        sql.Add('select * from ZH_KH_Info where KHNameJC='''+Trim(KHNameJC.Text)+'''');
        SQL.Add(' and Type=''GYS'' ');
        Open;
      end;
      if ADOTemp.RecordCount>1 then
      begin
        ADOCmd.Connection.RollbackTrans;
        Application.MessageBox('供应商简称重复!','提示',0);
        Exit;
      end;
    end;
    ADOCmd.Connection.CommitTrans;
    FMainId:=Trim(maxno);
    Result:=True;
  except
    Result:=False;
    ADOCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！','提示',0);
  end;
end;

procedure TfrmGYSInPutTab.SYRNameBtnUpClick(Sender: TObject);
var
  fsj:string;
  FWZ:Integer;
begin
  fsj:=Trim(TEdit(Sender).Hint);
  FWZ:=Pos('/',fsj);
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:=Copy(fsj,1,FWZ-1);
      flagname:=Copy(fsj,FWZ+1,Length(fsj)-fwz);
      if ShowModal=1 then
      begin
        TEdit(Sender).Text:=Trim(ClientDataSet1.fieldbyname('ZDYName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

procedure TfrmGYSInPutTab.SKBankBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text:='';
  TBtnEditC(Sender).TxtCode:='';
end;



procedure TfrmGYSInPutTab.TBSaveClick(Sender: TObject);
begin
  ToolBar1.SetFocus;
  if Trim(KHName.Text)='' then
  begin
    Application.MessageBox('供应商名称不能为空！','提示',0);
    Exit;
  end;
  if Trim(KHType.Text)='' then
  begin
    Application.MessageBox('供应商类型不能为空!','提示',0);
    Exit;
  end;
  if Trim(KHCode.Text)='' then
  begin
    Application.MessageBox('供应商编号不能为空!','提示',0);
    Exit;
  end;
  if Trim(KHNameJC.Text)='' then
  begin
    Application.MessageBox('供应商简称不能为空!','提示',0);
    Exit;
  end;
  if SaveData() then
  begin
    Application.MessageBox('保存成功！','提示',0);
  end;
end;

procedure TfrmGYSInPutTab.KHTypeBtnClick(Sender: TObject);
begin
  try
    frmZDYHelp:=TfrmZDYHelp.Create(Application);
    with frmZDYHelp do
    begin
      flag:='KHType';
      flagname:='供应商类型';
      if ShowModal=1 then
      begin
        Self.KHType.Text:=Trim(frmZDYHelp.ClientDataSet1.fieldbyname('ZdyName').AsString);
      end;
    end;
  finally
    frmZDYHelp.Free;
  end;
end;

end.
