unit U_GKInput;

interface

uses
  Windows, Messages, StrUtils, 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, BtnEdit, cxLookAndFeels, cxLookAndFeelPainters,
  cxNavigator, dxDateRanges, dxBarBuiltInMenu, U_BaseList, cxContainer, dxCore,
  cxDateUtils, cxImage, cxDBEdit, IdExplicitTLSClientServerBase, cxMemo,
  U_BaseInput, System.ImageList, Vcl.ImgList, cxImageList, dxSkinsCore,
  dxSkinsDefaultPainters;

type
  TfrmGKInput = class(TfrmBaseInput)
    ToolBar1: TToolBar;
    TBClose: TToolButton;
    ADOQueryTemp: TADOQuery;
    ADOQueryCmd: TADOQuery;
    ADOQueryMain: TADOQuery;
    TbSave: TToolButton;
    ScrollBox1: TScrollBox;
    Label2: TLabel;
    Label6: TLabel;
    Label39: TLabel;
    GKKH: TcxTextEdit;
    GKBH: TcxTextEdit;
    GKLength: TcxTextEdit;
    Label7: TLabel;
    JZBH: TcxTextEdit;
    Label3: TLabel;
    Note: TcxMemo;
    Label1: TLabel;
    JZCH: TcxTextEdit;
    Label4: TLabel;
    GKCS: TcxTextEdit;
    Label5: TLabel;
    GKwidth: TcxTextEdit;
    Label8: TLabel;
    procedure FormDestroy(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure TBCloseClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure TBRafreshClick(Sender: TObject);
    procedure TbSaveClick(Sender: TObject);
    procedure CoareaBtnDnClick(Sender: TObject);
    procedure JZBHPropertiesChange(Sender: TObject);
    procedure GKKHExit(Sender: TObject);
  private
    { Private declarations }
    procedure InitGrid();
    function SaveData(): Boolean;
  public
    fFlileFlag: string;
    fkhType: string;
    { Public declarations }
    FGKID: string;
  end;

var
  frmGKInput: TfrmGKInput;

implementation

uses
  U_DataLink, U_RTFun, U_ZDYHelp, U_CompanySel;

{$R *.dfm}

procedure TfrmGKInput.InitGrid();
begin
  try

    ADOQueryMain.DisableControls;
    with ADOQueryMain do
    begin
      Filtered := False;
      Close;
      SQL.Clear;
      sql.Add(' select * from BS_GK where GKID=''' + Trim(FGKID) + '''');
      Open;
    end;
    SCSHData(ADOQueryMain, ScrollBox1, 2);

  finally
    ADOQueryMain.EnableControls;
  end;
end;

procedure TfrmGKInput.JZBHPropertiesChange(Sender: TObject);
begin
  inherited;
  GKBH.Text := JZBH.Text + '-' + GKKH.Text + '-' + JZCH.Text + '-' + GKLength.Text;
end;

procedure TfrmGKInput.FormDestroy(Sender: TObject);
begin
  inherited;
  frmGKInput := nil;
end;

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

procedure TfrmGKInput.TBCloseClick(Sender: TObject);
begin
  ModalResult := 1;
end;

procedure TfrmGKInput.FormShow(Sender: TObject);
begin
  inherited;
  InitGrid();

end;

procedure TfrmGKInput.GKKHExit(Sender: TObject);
var
  FKCS, FKOUHAO: double;
begin
  FKCS := StrToFloatDef(GKCS.Text, 0);
  FKOUHAO := StrToFloatDef(GKKH.Text, 0);
  if FKOUHAO <> 0 then
  begin
    GKwidth.Text := FloatToStr(RoundFloat(FKCS / FKOUHAO * 2, 2));
  end;

end;

procedure TfrmGKInput.TBRafreshClick(Sender: TObject);
begin
  InitGrid();
end;

function TfrmGKInput.SaveData(): Boolean;
var
  MaxId, MaxSubId, FCoCode, FCCID, MaxCFID: string;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;
    if Trim(FGKID) = '' then
    begin
      if GetLSNo(ADOQueryCmd, MaxId, 'GK', 'BS_GK', 4, 1) = False then
      begin
        Result := False;
        ADOQueryCmd.Connection.RollbackTrans;
        Application.MessageBox('取最大号失败！', '提示', 0);
        Exit;
      end;

    end
    else
    begin
      MaxId := Trim(FGKID);
    end;

    with ADOQueryCmd do
    begin
      Close;
      SQL.Clear;
      sql.Add('select * from BS_GK where GKID=''' + Trim(FGKID) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin
      if Trim(FGKID) = '' then
      begin
        Append;
        FieldByName('Filler').Value := Trim(DName);
      end
      else
      begin
        Edit;
        FieldByName('Editer').Value := Trim(DName);
        FieldByName('Edittime').Value := SGetServerDateTime(ADOQueryTemp);
      end;
      FieldByName('GKID').Value := Trim(MaxId);
      RTSetsavedata(ADOQueryCmd, 'BS_GK', ScrollBox1, 2);
      Post;
    end;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from BS_GK where GKBH=' + quotedstr(trim(GKBH.Text)));
      Open;
    end;
    if ADOQueryCmd.RecordCount > 1 then
    begin
      ADOQueryCmd.Connection.RollbackTrans;
      Application.MessageBox('钢扣编号重复!', '提示', 0);
      Exit;
    end;

    ADOQueryCmd.Connection.CommitTrans;
    FCCID := Trim(MaxSubId);
    Result := True;
  except
    Result := false;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存异常!', '提示', 0);
  end;
end;

procedure TfrmGKInput.TbSaveClick(Sender: TObject);
begin
  if Trim(JZBH.Text) = '' then
  begin
    Application.MessageBox('架子编号不能为空!', '提示', 0);
    Exit;
  end
  else if Trim(GKKH.Text) = '' then
  begin
    Application.MessageBox('筘号不能为空!', '提示', 0);
    Exit;
  end
  else if Trim(JZCH.Text) = '' then
  begin
    Application.MessageBox('架子层号不能为空!', '提示', 0);
    Exit;
  end
  else if Trim(GKLength.Text) = '' then
  begin
    Application.MessageBox('钢筘长度不能为空!', '提示', 0);
    Exit;
  end;

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

procedure TfrmGKInput.CoareaBtnDnClick(Sender: TObject);
begin
  TBtnEditC(Sender).Text := '';
  TBtnEditC(Sender).TxtCode := '';
end;

end.

