unit Unit9;

interface

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

type
  TForm9 = class(TForm)
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    Button3: TButton;
    Panel2: TPanel;
    Label1: TLabel;
    Label7: TLabel;
    Label3: TLabel;
    Button5: TButton;
    Button6: TButton;
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    Table1: TTable;
    Table2: TTable;
    Table3: TTable;
    DataSource3: TDataSource;
    DateTimePicker1: TDateTimePicker;
    Query1: TQuery;
    Query1id_sp: TIntegerField;
    Query1PIB: TStringField;
    Query1Dani_pasporta: TStringField;
    Query1Ident_kod: TStringField;
    Query1Adresa: TStringField;
    Query1Telefon: TStringField;
    Query1Data_vlasht: TDateField;
    Query1Id_vid: TIntegerField;
    Query1Posada: TStringField;
    Query1Oklad: TFloatField;
    Query1Stat: TStringField;
    Table1id: TIntegerField;
    Table1id_prod: TIntegerField;
    Table1obsyag: TIntegerField;
    Table1data_vyrob: TDateField;
    Table2id: TIntegerField;
    Table2nazva: TStringField;
    Table2marka: TStringField;
    Table2od_vumiru: TStringField;
    Table1prod: TStringField;
    DBLookupComboBox1: TDBLookupComboBox;
    DBEdit1: TDBEdit;
    Table3id: TIntegerField;
    Table3pr: TStringField;
    Table3obsyag: TIntegerField;
    procedure DateTimePicker1CloseUp(Sender: TObject);
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Table1AfterPost(DataSet: TDataSet);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Table3AfterPost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    procedure Aktiv(akt: boolean);
    { Public declarations }
  end;

var
  Form9: TForm9;

implementation

{$R *.dfm}

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

procedure TForm9.DateTimePicker1CloseUp(Sender: TObject);
begin
Table1data_vyrob.AsDateTime:=DateTimePicker1.DateTime;
end;

procedure TForm9.DBGrid1ColEnter(Sender: TObject);
begin
DateTimePicker1.DateTime:=Table1data_vyrob.AsDateTime;
end;

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

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

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

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

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

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

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

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

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

procedure TForm9.Table3AfterPost(DataSet: TDataSet);
begin
Table3.Close;
Table3.Open;
end;

end.
 