unit U_FLAddPN;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxDataStorage,
  cxEdit, DB, cxDBData, ADODB, DBClient, cxGridLevel, cxGridCustomTableView,
  cxGridTableView, cxGridDBTableView, cxClasses, cxControls, cxGridCustomView,
  cxGrid, ComCtrls, ToolWin, cxGridCustomPopupMenu, cxGridPopupMenu, cxTextEdit,
  cxButtonEdit, StdCtrls, ExtCtrls, cxCurrencyEdit, BtnEdit;

type
  TfrmFLAddPN = class(TForm)
    ToolBar1: TToolBar;
    ToolButton1: TToolButton;
    TBClose: TToolButton;
    CDS_Sub: TClientDataSet;
    ADOQueryMain: TADOQuery;
    DataSource2: TDataSource;
    ADOQueryCmd: TADOQuery;
    ADOQueryTemp: TADOQuery;
    ScrollBox1: TScrollBox;
    Label1: TLabel;
    Label7: TLabel;
    Label11: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    CYNo: TEdit;
    CYNote: TMemo;
    MXHS: TEdit;
    CYID: TEdit;
    CYEName: TEdit;
    Label5: TLabel;
    Label6: TLabel;
    Label8: TLabel;
    cyhx: TEdit;
    CYPUNIT: TEdit;
    CYCOLOR: TEdit;
    Label9: TLabel;
    MHJS: TEdit;
    Label10: TLabel;
    JSUNIT: TEdit;
    Label12: TLabel;
    MC: TEdit;
    Label13: TLabel;
    MCUNIT: TEdit;
    Label15: TLabel;
    BZCC: TEdit;
    Label16: TLabel;
    DJZL: TEdit;
    Label17: TLabel;
    CYCF: TEdit;
    Label2: TLabel;
    CYMJID: TEdit;
    procedure FormShow(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
    procedure ToolButton1Click(Sender: TObject);
    procedure CYColorKeyPress(Sender: TObject; var Key: Char);
  private
    canshu1: string;
    Fint: Integer;
    procedure InitSubGrid();
    function SaveData(): Boolean;
    { Private declarations }
  public
    FCYID, FCYCode, FCPID, FCPNO, FCPName: string;
    CopyInt: Integer;

    { Public declarations }
  end;

var
  frmFLAddPN: TfrmFLAddPN;

implementation

uses
  U_DataLink, U_Fun, U_iniParam, U_ZDYHelp, U_ZdyAttachGYS, U_FileUp;

{$R *.dfm}

procedure TfrmFLAddPN.InitSubGrid();
begin

  try
    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Close;
      SQL.Clear;
      SQL.Add('select * from FL_YDang where CYID=''' + Trim(FCYID) + '''');
//      ShowMessage(SQL.Text);
      Open;
    end;

    SCSHDataNew(ADOQueryMain, ScrollBox1, 2);
    SCSHDataNew(ADOQueryMain, ScrollBox1, 0);
  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmFLAddPN.FormShow(Sender: TObject);
begin
  InitSubGrid();
  if CopyInt = 1 then
  begin
    FCYID := '';
    CYID.text := '';

    CYNo.text := '';
  end;
  if CYID.text <> '' then
    CYID.Enabled := False
  else
    CYID.Enabled := True;
end;

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

function TfrmFLAddPN.SaveData(): Boolean;
var
  maxId, FCYNo: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FCYID) = '' then
    begin
      if Trim(CYID.Text) = '' then
      begin
        if GetLSNo(ADOQueryCmd, maxId, '87', 'FL_YDang', 4, 1) = False then
        begin
          Result := False;
          ADOQueryCmd.Connection.RollbackTrans;
          Application.MessageBox('取最大号失败！', '提示', 0);
          Exit;
        end;
        CYID.Text := trim(maxId);
        CYID.Enabled := False;
      end
      else
      begin
        maxId := Trim(CYID.Text);
      end;

    end
    else
    begin
      maxId := Trim(FCYID);
    end;
    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from FL_YDang where CYID=''' + Trim(FCYID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FCYID) = '' then
      begin
        Append;
        FieldByName('CYType').Value := Trim(FCPID);
        FieldByName('FILLER').Value := Trim(DName);
        FieldByName('FILLTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end
      else
      begin
        Edit;
        FieldByName('EDITER').Value := Trim(DName);
        FieldByName('EDITTIME').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('CYID').Value := Trim(maxId);
      if trim(CYNo.Text) = '' then
        CYNo.Text := Trim(maxId);
      SSetsaveSqlNew(ADOQueryCmd, 'FL_YDang', ScrollBox1, 2);
      Post;
    end;
    with ADOQueryTemp do
    begin
      Close;
      sql.Clear;
      sql.Add('select count(*) as AA from FL_YDang where CYNo=''' + Trim(CYNo.Text) + '''');
      Open;
      if FieldByName('AA').AsInteger > 1 then
      begin
        ADOQueryCmd.Connection.RollbackTrans;
        Result := False;
        Application.MessageBox('编号重复！', '提示', 0);
        Exit;
      end;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    FCYID := maxId;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败!', '提示', 0);
  end;
end;

procedure TfrmFLAddPN.ToolButton1Click(Sender: TObject);
begin
  if CYEName.Text = '' then
  begin
    Application.MessageBox('英文名称不能为空！', '提示', 0);
    Exit;
  end;

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

procedure TfrmFLAddPN.CYColorKeyPress(Sender: TObject; var Key: Char);
var
  c: Integer;
begin
  if Key = '.' then
  begin
    for c := 1 to length(TEdit(Sender).text) do
    begin
      if Tedit(Sender).text[c] = '.' then
        Key := #0;
    end;
  end
  else if Key = #13 then
    PerForm(WM_NEXTDLGCTL, 0, 0)
  else if Key = #8 then
    Key := #8
  else if (Key < '0') or (Key > '9') then
    Key := #0;
end;

end.

