unit U_LSEdit;

interface

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

type
  TfrmLSEdit = class(TForm)
    ToolBar1: TToolBar;
    ToolButton2: TToolButton;
    TBClose: TToolButton;
    Panel1: TPanel;
    JYID: TEdit;
    ADOTmp: TADOQuery;
    ADOQueryMain: TADOQuery;
    ADOQueryCmd: TADOQuery;
    Label2: TLabel;
    Label3: TLabel;
    Label14: TLabel;
    Label4: TLabel;
    Label6: TLabel;
    Label7: TLabel;
    ZGKS: TEdit;
    ZXZL: TEdit;
    ZGZL: TEdit;
    Label1: TLabel;
    ZGColor: TComboBox;
    Label8: TLabel;
    JYGW: TEdit;
    Label5: TLabel;
    MachNO: TComboBox;
    Label9: TLabel;
    Y_Type: TComboBox;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormDestroy(Sender: TObject);
    procedure JYIDKeyPress(Sender: TObject; var Key: Char);
    procedure ToolButton2Click(Sender: TObject);
    procedure TBCloseClick(Sender: TObject);
  private
    { Private declarations }
    function SaveData(): Boolean;
  public
    { Public declarations }
  end;

var
  frmLSEdit: TfrmLSEdit;

implementation

uses
  U_Fun, U_ZDYHelp, U_DataLink, U_SCPerson;

{$R *.dfm}

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

procedure TfrmLSEdit.FormDestroy(Sender: TObject);
begin
  frmLSEdit := nil;
end;

procedure TfrmLSEdit.JYIDKeyPress(Sender: TObject; var Key: Char);
begin
  if Key = #13 then
  begin
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      sql.Add('select * from CK_YT_CR where JYID=''' + Trim(JYID.Text) + ''' and CRFlag=''出库''');
      Open;
    end;
    if ADOTmp.IsEmpty = False then
    begin
      Application.MessageBox('已出库数据不能编辑!', '提示', 0);
      Exit;
    end;
    with ADOTmp do
    begin
      Close;
      sql.Clear;
      SQL.Add('select A.*  from JY_YT A');
      sql.Add(' where A.JYID=''' + Trim(JYID.Text) + '''');
      Open;
    end;

    SCSHDataNew(ADOTmp, Panel1, 0);

  end;
end;

function TfrmLSEdit.SaveData(): Boolean;
var
  maxno: string;
  FFreal: Double;
begin
  try
    ADOQueryCmd.Connection.BeginTrans;

    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      SQL.Add('select * from JY_YT where JYID=''' + Trim(JYID.Text) + '''');
      Open;
    end;
    with ADOQueryCmd do
    begin

      Edit;
      FieldByName('Editer').Value := Trim(DName);
      FieldByName('EditTime').Value := SGetServerDateTime(ADOTmp);

      RTSetsavedata(ADOQueryCmd, 'JY_YT', Panel1, 0);
      FieldByName('CRType').Value := '已入库';

      Post;
    end;

     //出入库及库存
    with ADOQueryCmd do
    begin
      Close;
      sql.Clear;
      sql.Add('exec P_JY_Insert_JTS @JYID=''' + Trim(JYID.Text) + '''');
      execsql;
    end;
    ADOQueryCmd.Connection.CommitTrans;
    Result := True;
  except
    Result := False;
    ADOQueryCmd.Connection.RollbackTrans;
    Application.MessageBox('保存失败！', '提示', 0);
  end;
end;

procedure TfrmLSEdit.ToolButton2Click(Sender: TObject);
begin
  if Trim(JYID.Text) = '' then
    Exit;
  if SaveData() then
  begin
    Application.MessageBox('保存成功!', '提示', 0);
    ModalResult := 1;
  end;
end;

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

end.

