unit Unit22;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls,
  DBCtrlsEh, Vcl.Mask, DBGridEh, DBLookupEh, DBGridEhGrouping, ToolCtrlsEh,
  DBGridEhToolCtrls, DynVarsEh, EhLibVCL, GridsEh, DBAxisGridsEh, Vcl.ComCtrls,
  Vcl.ToolWin, Data.DB, Data.Win.ADODB;

type
  TfStockEdit = class(TForm)
    Panel1: TPanel;
    bbOk: TBitBtn;
    bbCancel: TBitBtn;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    DBDateTimeEditEh1: TDBDateTimeEditEh;
    DBEditEh1: TDBEditEh;
    DBEditEh2: TDBEditEh;
    DBEditEh3: TDBEditEh;
    DBMemoEh1: TDBMemoEh;
    pTop: TPanel;
    tbMain: TToolBar;
    tbAdd: TToolButton;
    tbEdit: TToolButton;
    tbDelete: TToolButton;
    DBGridEh1: TDBGridEh;
    DBEditEh4: TDBEditEh;
    DBEditEh5: TDBEditEh;
    dsStockMove: TDataSource;
    qStockMove: TADOQuery;
    qStockMoveid: TAutoIncField;
    qStockMovestock_id: TIntegerField;
    qStockMovevalue_id: TIntegerField;
    qStockMovequantity: TIntegerField;
    qStockMovebuyPrice: TFloatField;
    qStockMoveprice: TFloatField;
    qStockMovename: TWideStringField;
    qStockMovevendor: TWideStringField;
    procedure tbEditClick(Sender: TObject);
    procedure tbAddClick(Sender: TObject);
    procedure tbDeleteClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  fStockEdit: TfStockEdit;

implementation

{$R *.dfm}

uses Unit1, Unit23, Unit2;

procedure TfStockEdit.tbAddClick(Sender: TObject);
begin
  if DM.qStockid.AsInteger > 0 then begin
    qStockMove.Append;
    qStockMovestock_id.AsInteger := DM.qStockid.AsInteger;
      if fStockMoveEdit.ShowModal = mrOk then begin
        if qStockMove.Modified then
          qStockMove.Post;
    end;
    qStockMove.Cancel;
    qStockMove.Requery();
    qStockMove.Last;
  end
  else begin
    if Application.MessageBox('Для продолжения необходимо сохранить текущую запись. Сохранить?', 'Подтверждение', MB_ICONQUESTION+MB_YESNO) = idYes then begin
      if DM.qStock.Modified then
        DM.qStock.Post;
      qStockMove.Append;
      qStockMovestock_id.AsInteger := DM.qStockid.AsInteger;
      qStockMove.Parameters[0].Value := DM.qStockid.Value;
      if fStockMoveEdit.ShowModal = mrOk then begin
        if qStockMove.Modified then
          qStockMove.Post;
      end;
      qStockMove.Cancel;
      qStockMove.Requery();
      qStockMove.Last;
      DM.qStock.Edit;
    end;
  end;
end;

procedure TfStockEdit.tbDeleteClick(Sender: TObject);
begin
   if Application.MessageBox('Удалить запись?', 'Подтверждение', MB_ICONQUESTION+MB_YESNO) = idYes then begin
      if DM.qRemove.Active then
         DM.qRemove.Close;
      DM.qRemove.SQL.Text := 'SELECT * FROM stockmove WHERE id = ' + qStockMoveid.AsString;
      DM.qRemove.Open;
      DM.qRemove.Delete;
      DM.qRemove.Close;
      qStockMove.Requery;
   end;
end;

procedure TfStockEdit.tbEditClick(Sender: TObject);
  var
    id:integer;
begin
  id := qStockMove.RecNo;
  fStockMoveEdit.qStockMove.Parameters[0].Value := qStockMovevalue_id.Value;
  fStockMoveEdit.qStockMove.Open;
  qStockMove.Edit;
  fStockMoveEdit.qResidues.Close;
  fStockMoveEdit.qResidues.Parameters[0].Value := qStockMovevalue_id.Value;
  fStockMoveEdit.qResidues.Open;
  if fStockMoveEdit.ShowModal = mrOk then begin
    if qStockMove.Modified then
      qStockMove.Post;
  end;
  fStockMoveEdit.qStockMove.Close;
  qStockMove.Cancel;
  qStockMove.Requery();
  qStockMove.MoveBy(id-1);
end;

end.
