unit Unit11;

interface

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

type
  TForm11 = class(TForm)
    DBGrid1: TDBGrid;
    Panel1: TPanel;
    DBGrid2: TDBGrid;
    Panel5: TPanel;
    Label4: TLabel;
    Label5: TLabel;
    Label1: TLabel;
    Label2: TLabel;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBLookupComboBox1: TDBLookupComboBox;
    Button10: TButton;
    Button11: TButton;
    Panel2: TPanel;
    Button9: TButton;
    Button8: TButton;
    Button7: TButton;
    Button12: TButton;
    Panel4: TPanel;
    Label7: TLabel;
    Label8: TLabel;
    Label3: TLabel;
    DBLookupComboBox3: TDBLookupComboBox;
    DBLookupComboBox4: TDBLookupComboBox;
    DateTimePicker1: TDateTimePicker;
    Button5: TButton;
    Button6: TButton;
    Panel3: TPanel;
    Button3: TButton;
    Button1: TButton;
    Button2: TButton;
    Button4: TButton;
    Table1: TTable;
    DataSource1: TDataSource;
    Table3: TTable;
    Table3id: TIntegerField;
    Table3prizvyche: TStringField;
    Table3imya: TStringField;
    Table3po_batkovi: TStringField;
    Table3data_narodsh: TDateField;
    Table3seriya_pasporta: TStringField;
    Table3nomer_pasporta: TStringField;
    Table3stat: TStringField;
    Table3index: TStringField;
    Table3adresa: TStringField;
    Table3telefon: TStringField;
    Table3ipn: TStringField;
    Table3oklad: TFloatField;
    Table3posada: TStringField;
    Table4: TTable;
    DataSource3: TDataSource;
    DataSource4: TDataSource;
    Table2: TTable;
    DataSource2: TDataSource;
    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;
    DataSource5: TDataSource;
    Table5: TTable;
    Table6: TTable;
    DataSource6: TDataSource;
    Table1id: TIntegerField;
    Table1id_sp: TIntegerField;
    Table1id_postachal: TIntegerField;
    Table1data_post: TDateField;
    Table4id: TIntegerField;
    Table4PIB_post: TStringField;
    Table4adresa: TStringField;
    Table4telefon: TStringField;
    Table4bank_post: TStringField;
    Table4n_rachunka: TStringField;
    Table4n_svidotstva: TStringField;
    Table4edrpou: TStringField;
    Table1spivrob: TStringField;
    Table1postach: TStringField;
    Table2id: TIntegerField;
    Table2id_post: TIntegerField;
    Table2id_syrov: TIntegerField;
    Table2obsyag: TIntegerField;
    Table2cina: TFloatField;
    Table5id: TIntegerField;
    Table5nazva: TStringField;
    Table5od_vymir: TStringField;
    Table6id: TIntegerField;
    Table6obsyag: TIntegerField;
    Table6od_vymiru: TStringField;
    Table2syrov: TStringField;
    Table2vartist: TFloatField;
    Table6sr: TStringField;
    procedure Table1AfterScroll(DataSet: TDataSet);
    procedure DBGrid2ColEnter(Sender: TObject);
    procedure Table2AfterPost(DataSet: TDataSet);
    procedure Button7Click(Sender: TObject);
    procedure DateTimePicker1CloseUp(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure Button10Click(Sender: TObject);
    procedure Button11Click(Sender: TObject);
    procedure DBGrid1ColEnter(Sender: TObject);
    procedure Table1AfterPost(DataSet: TDataSet);
    procedure FormActivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Table2CalcFields(DataSet: TDataSet);
    procedure Button5Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Table6AfterPost(DataSet: TDataSet);
  private
    { Private declarations }
  public
    procedure Aktiv(akt: boolean);
    procedure Aktiv2(akt: boolean);
    procedure Aktiv3(akt: boolean);
    procedure Aktiv4(akt: boolean);
    { Public declarations }
  end;

var
  Form11: TForm11;
  f: string;

implementation

{$R *.dfm}

procedure TForm11.Aktiv(akt: boolean);
begin
Button7.Enabled:=akt;
Button8.Enabled:=akt;
Button9.Enabled:=akt;
DBGrid2.Enabled:=akt;
end;

procedure TForm11.Aktiv2(akt: boolean);
begin
Button10.Enabled:=not akt;
Button11.Enabled:=not akt;
DBEdit1.Enabled:=not akt;
DBEdit2.Enabled:=not akt;
DBLookupComboBox1.Enabled:=not akt;
end;

procedure TForm11.Aktiv3(akt: boolean);
begin
Button1.Enabled:=akt;
Button2.Enabled:=akt;
Button4.Enabled:=akt;
Button3.Enabled:=akt;
Button12.Enabled:=akt;
DBGrid1.Enabled:=akt;
end;

procedure TForm11.Aktiv4(akt: boolean);
begin
DateTimePicker1.Enabled:=not akt;
DBLookupComboBox3.Enabled:=not akt;
DBLookupComboBox4.Enabled:=not akt;
Button5.Enabled:=not akt;
Button6.Enabled:=not akt;
end;


procedure TForm11.Table1AfterScroll(DataSet: TDataSet);
begin
f:=Table1id.AsString;
if f='' then f:='0';
Table2.Filter:='id_post='+QuotedStr(f);
Table2.Filtered:=true;
end;

procedure TForm11.DBGrid2ColEnter(Sender: TObject);
begin
DateTimePicker1.DateTime:=Table1data_post.AsDateTime;
end;

procedure TForm11.Table2AfterPost(DataSet: TDataSet);
begin
Table2.Close;
Table1.Close;
Table2.Open;
Table1.Open;
Table1.Last;
end;

procedure TForm11.Button7Click(Sender: TObject);
begin
Table2.Append();
Aktiv(false);
Aktiv2(false);
Aktiv3(false);
Aktiv4(true);
Table2id_post.AsInteger:=StrToInt(f);
DBLookupComboBox1.SetFocus();
end;

procedure TForm11.DateTimePicker1CloseUp(Sender: TObject);
begin
Table1data_post.AsDateTime:=DateTimePicker1.DateTime;
end;

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

procedure TForm11.Button8Click(Sender: TObject);
var s:string;
begin
if Table2.IsEmpty then
exit;
Table2.Edit();
Aktiv(false);
Aktiv2(false);
Aktiv3(false);
Aktiv4(true);
s:=Table2.FieldByName('syrov').AsString;
Table6.Filter:='sr='+QuotedStr(s);
Table6.Filtered:=true;
Table6.Edit;
Table6.FieldByName('obsyag').AsFloat:=Table6.FieldByName('obsyag').AsFloat-Table2.FieldByName('obsyag').AsFloat;
Table6.Post;
Table6.Filtered:=False;
DBLookupComboBox1.SetFocus();
end;

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

procedure TForm11.Button10Click(Sender: TObject);
var s:string;
begin
s:=Table2.FieldByName('syrov').AsString;
Aktiv(true);
Aktiv2(true);
Aktiv3(true);
Aktiv4(true);
Table6.Filter:='sr='+QuotedStr(s);
Table6.Filtered:=true;
if Table6.RecordCount=0 then
Table6.AppendRecord([nil,s,0]);
Table6.Edit;
Table6.FieldByName('obsyag').AsFloat:=Table6.FieldByName('obsyag').AsFloat+Table2.FieldByName('obsyag').AsFloat;
Table6.Post;
Table6.Filtered:=False;
Table2.post();
Button7.SetFocus();
end;

procedure TForm11.Button11Click(Sender: TObject);
var s:string; znach: real;
begin
znach:=Table2.FieldByName('obsyag').AsFloat;
if znach<>0 then begin
s:=Table2.FieldByName('syrov').AsString;
Table6.Filter:='sr='+QuotedStr(s);
Table6.Filtered:=true;
Table6.Edit;
Table6.FieldByName('obsyag').AsFloat:=Table6.FieldByName('obsyag').AsFloat+znach;
Table6.Post;
Table6.Filtered:=False; end;
Table2.Cancel();
Aktiv(true);
Aktiv2(true);
Aktiv3(true);
Aktiv4(true);
DBGrid2.SetFocus();
end;

procedure TForm11.DBGrid1ColEnter(Sender: TObject);
begin
DateTimePicker1.DateTime:=Table1data_post.AsDateTime;
end;

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

procedure TForm11.FormActivate(Sender: TObject);
begin
Table1.Open;
Table2.Open;
Table3.Open;
Table4.Open;
Table5.Open;
Table6.Open;
Aktiv(true);
Aktiv2(true);
Aktiv3(true);
Aktiv4(true);
end;

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

procedure TForm11.FormCreate(Sender: TObject);
begin
DateTimePicker1.DateTime:=Date();
end;

procedure TForm11.Button1Click(Sender: TObject);
begin
DateTimePicker1.DateTime:=Date();
Table1.Append();
Aktiv(false);
Aktiv2(true);
Aktiv3(false);
Aktiv4(false);
DBLookupComboBox3.SetFocus();
end;

procedure TForm11.Table2CalcFields(DataSet: TDataSet);
begin
Table2vartist.AsFloat:=Table2obsyag.AsInteger*Table2cina.AsFloat;
end;

procedure TForm11.Button5Click(Sender: TObject);
begin
Table1.post();
Aktiv(true);
Aktiv2(true);
Aktiv3(true);
Aktiv4(true);
DateTimePicker1.DateTime:=Date();
Button1.SetFocus();
end;

procedure TForm11.Button2Click(Sender: TObject);
begin
if Table1.IsEmpty then
exit;
Table1.Edit();
Aktiv(false);
Aktiv2(true);
Aktiv3(false);
Aktiv4(false);
DBLookupComboBox3.SetFocus();
end;

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

procedure TForm11.Button6Click(Sender: TObject);
begin
Table1.Cancel();
Aktiv(true);
Aktiv2(true);
Aktiv3(true);
Aktiv4(true);
DBGrid1.SetFocus();
end;

procedure TForm11.Table6AfterPost(DataSet: TDataSet);
begin
Table6.Close;
Table6.Open;
end;

end.
