unit Unit13;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, Mask, DBCtrls, DB, DBTables, ExtCtrls,
  Grids, DBGrids;

type
  TForm13 = class(TForm)
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    Button3: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label9: TLabel;
    Button5: TButton;
    Button6: TButton;
    DataSource1: TDataSource;
    Table1: TTable;
    Query1: TQuery;
    Table1id: TIntegerField;
    Table1id_syrov: TIntegerField;
    Table1obsyag: TIntegerField;
    Table1id_prod: TIntegerField;
    Table1data: TDateField;
    Table2: TTable;
    Table3: TTable;
    Table4: TTable;
    DataSource2: TDataSource;
    DataSource3: TDataSource;
    DataSource4: TDataSource;
    Table3id: TIntegerField;
    Table3nazva: TStringField;
    Table3marka: TStringField;
    Table3od_vumiru: TStringField;
    Table2id: TIntegerField;
    Table2nazva: TStringField;
    Table2od_vymir: TStringField;
    Table1syrov: TStringField;
    Table1prod: TStringField;
    DBLookupComboBox1: TDBLookupComboBox;
    DBEdit1: TDBEdit;
    DBLookupComboBox2: TDBLookupComboBox;
    DateTimePicker1: TDateTimePicker;
    Table4id: TIntegerField;
    Table4sr: TStringField;
    Table4obsyag: TIntegerField;
    Table4od_vymiru: TStringField;
    procedure Button1Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure DateTimePicker1CloseUp(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Table1AfterPost(DataSet: TDataSet);
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure Table4AfterPost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    procedure Aktiv (akt: boolean);
    { Public declarations }
  end;

var
  Form13: TForm13;

implementation

{$R *.dfm}

procedure TForm13.Aktiv(akt: boolean);
begin
DBGrid1.Enabled:=akt;
Button1.Enabled:=akt;
Button2.Enabled:=akt;
Button3.Enabled:=akt;
Button4.Enabled:=akt;
Button5.Enabled:= not akt;
Button6.Enabled:= not akt;
DBEdit1.Enabled:=not akt;
DBLookupComboBox1.Enabled:=not akt;
DBLookupComboBox2.Enabled:=not akt;
DateTimePicker1.Enabled:=not akt;
end;

procedure TForm13.Button1Click(Sender: TObject);
begin
Table1.Append();
Aktiv(false);
DateTimePicker1.DateTime:=Date();
DBLookupComboBox1.SetFocus();
end;

procedure TForm13.Button5Click(Sender: TObject);
var s:string;
begin
Table1data.AsDateTime:=DateTimePicker1.DateTime;
s:=Table1.FieldByName('syrov').AsString;
Table4.Filter:='sr='+QuotedStr(s);
Table4.Filtered:=true;
if Table4.RecordCount=0 then
Table4.AppendRecord([nil,s,0]);
Table4.Edit;
Table4.FieldByName('obsyag').AsFloat:=Table4.FieldByName('obsyag').AsFloat-Table1.FieldByName('obsyag').AsFloat;
Table4.Post;
Table4.Filtered:=False;
Table1.post();
Aktiv(true);
DateTimePicker1.DateTime:=Date();
Button1.SetFocus();
end;

procedure TForm13.Button2Click(Sender: TObject);
var s:string;
begin
if Table1.IsEmpty then
exit;
Table1.Edit();
Aktiv(false);
s:=Table1.FieldByName('syrov').AsString;
Table4.Filter:='sr='+QuotedStr(s);
Table4.Filtered:=true;
Table4.Edit;
Table4.FieldByName('obsyag').AsFloat:=Table4.FieldByName('obsyag').AsFloat+Table1.FieldByName('obsyag').AsFloat;
Table4.Post;
Table4.Filtered:=False;
DBLookupComboBox1.SetFocus();
end;

procedure TForm13.Button4Click(Sender: TObject);
begin
if Application.Messagebox('Ви дійсно хочете видалити запис?','Видалення',
MB_YESNO+MB_IconQuestion)= mrYes
then Table1.Delete();
end;

procedure TForm13.Button3Click(Sender: TObject);
begin
Close;
end;

procedure TForm13.DateTimePicker1CloseUp(Sender: TObject);
begin
Table1data.AsDateTime:=DateTimePicker1.DateTime;
end;

procedure TForm13.Button6Click(Sender: TObject);
var s:string; znach: real;
begin
znach:=Table1.FieldByName('obsyag').AsFloat;
if znach<>0 then begin
s:=Table1.FieldByName('syrov').AsString;
Table4.Filter:='sr='+QuotedStr(s);
Table4.Filtered:=true;
Table4.Edit;
Table4.FieldByName('obsyag').AsFloat:=Table4.FieldByName('obsyag').AsFloat-znach;
Table4.Post;
Table4.Filtered:=False; end;
Table1.Cancel();
Aktiv(true);
DBGrid1.SetFocus();
end;

procedure TForm13.FormActivate(Sender: TObject);
begin
Table1.Open;
Table2.Open;
Table3.Open;
Table4.Open;
Aktiv(true);
end;

procedure TForm13.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Table1.Close;
Table2.Close;
Table3.Close;
Table4.Close;
end;

procedure TForm13.Table1AfterPost(DataSet: TDataSet);
begin
Table1.Close;
Table1.Open;
Table1.Last;
end;

procedure TForm13.DBGrid1ColEnter(Sender: TObject);
begin
DateTimePicker1.DateTime:=Table1data.AsDateTime;
end;

procedure TForm13.Table4AfterPost(DataSet: TDataSet);
begin
Table4.Close;
Table4.Open;
end;

end.
