unit Unit16;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Grids, DBGrids, DB, StdCtrls, DBTables, ComCtrls, Buttons, DateUtils;

type
  TForm16 = class(TForm)
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    Query3: TQuery;
    Query1Id: TIntegerField;
    Query1NazvaMagazuny: TStringField;
    Query1AdresaMagazuny: TStringField;
    Query1IdSpivrobitnuka: TIntegerField;
    Query3id: TIntegerField;
    Query3IdMagazuny: TIntegerField;
    Query3IdSpivrobitnuka: TIntegerField;
    Query3Syma: TFloatField;
    Query3Data: TDateField;
    Query3id_1: TIntegerField;
    Query3PIB: TStringField;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    ComboBox1: TComboBox;
    DateTimePicker1: TDateTimePicker;
    Edit1: TEdit;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    BitBtn6: TBitBtn;
    GroupBox3: TGroupBox;
    DateTimePicker2: TDateTimePicker;
    DateTimePicker3: TDateTimePicker;
    Label3: TLabel;
    Label4: TLabel;
    BitBtn7: TBitBtn;
    Query4: TQuery;
    Label5: TLabel;
    procedure Query1AfterScroll(DataSet: TDataSet);
    procedure BitBtn6Click(Sender: TObject);
    procedure Filter();
    procedure DateTimePicker2CloseUp(Sender: TObject);
    procedure DateTimePicker3CloseUp(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    function DateToSQLFormat(t:TDateTime):string;
    function FloatToSQL(x:real):string;
  end;

var
  Form16: TForm16;
  IdSp:array of string;

implementation

{$R *.dfm}

procedure TForm16.Query1AfterScroll(DataSet: TDataSet);
begin
Query3.Close();
if Query1.IsEmpty then exit;
Query3.SQL.Clear();
Query3.SQL.Add('Select vurychka.id, vurychka.IdMagazuny, vurychka.IdSpivrobitnuka, vurychka.Syma, vurychka.Data, magazunu.id, spivrobitnuku.PIB');
Query3.SQL.Add('From vurychka, magazunu, spivrobitnuku');
Query3.SQL.Add('Where vurychka.IdMagazuny = magazunu.Id AND vurychka.idSpivrobitnuka = spivrobitnuku.id');
Query3.SQL.Add('and vurychka.IdMagazuny='+Query1Id.AsString);
Query3.Open();
end;

procedure TForm16.BitBtn6Click(Sender: TObject);
begin
Close;
end;

procedure TForm16.Filter;
begin
Query3.Filter:='Data>='''+(DateToStr(DateTimePicker2.Date))+''''+' and Data<='''+(DateToStr(DateTimePicker3.Date))+'''';
Query3.Filtered:= True;
end;

procedure TForm16.DateTimePicker2CloseUp(Sender: TObject);
begin
  Filter;
end;

procedure TForm16.DateTimePicker3CloseUp(Sender: TObject);
begin
  Filter;
end;

procedure TForm16.BitBtn7Click(Sender: TObject);
begin
Query3.Filtered:=False;
end;

function TForm16.DateToSQLFormat(t:TDateTime):string;
var
   s1,s2,s3:string;
begin
   s1:=IntToStr(DayOf(t));
   while length(s1)<2 do s1:='0'+s1;
   s2:=IntToStr(MonthOf(t));
   while length(s2)<2 do s2:='0'+s2;
   s3:=IntToStr(YearOf(t));
   while length(s3)<2 do s3:='0'+s3;
   result:=s3+'-'+s2+'-'+s1;
end;

function TForm16.FloatToSQL(x:real):string;
begin
result:=floattostr(x);
if pos (',',result)>0 then
result[pos(',',result)]:='.';
end;

procedure TForm16.BitBtn3Click(Sender: TObject);
begin
 if   Application.MessageBox('Ви дійсно хочете видалити запис?','Увага!!!',MB_YesNo+MB_ICONQUESTION)=MrYes then
 begin
 Query4.Active:=False;
 Query4.SQL.Clear;
 Query4.SQL.Add('DELETE FROM vurychka WHERE vurychka.id='+Query3.FieldByName('Id').AsString+';');
 Query4.ExecSQL;
 end;
 Query3.Active:=False;
 Query3.Active:=True;
end;

procedure TForm16.BitBtn1Click(Sender: TObject);
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM spivrobitnuku');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdSp);
if not Query2.IsEmpty then repeat
setLength(IdSp,Length(IdSp)+1);
IdSp[high(IdSp)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('PIB').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';
  DateTimePicker1.Date:=date();
  combobox1.Enabled:=true;
  DateTimePicker1.Enabled:=true;
  Edit1.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label5.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
 Self.Tag:=1;
        end;

procedure TForm16.BitBtn2Click(Sender: TObject);
var i:integer;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM spivrobitnuku');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdSp);
if not Query2.IsEmpty then repeat
setLength(IdSp,Length(IdSp)+1);
IdSp[high(IdSp)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('PIB').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';

for i:=1 to Length (IdSp) do  begin
 if IdSp[i-1]=Query3IdSpivrobitnuka.AsString
then
  ComboBox1.ItemIndex:=i-1;  end;
IDSp[ComboBox1.ItemIndex]:=Query3.FieldByName('IdSpivrobitnuka').AsString;
Edit1.Text:=Query3.FieldByName('Syma').AsString;
datetimepicker1.date:=StrToDate(Query3Data.AsString);
  combobox1.Enabled:=true;
  DateTimePicker1.Enabled:=true;
  Edit1.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  label5.Enabled:=true;
  Self.Tag:=2;
    end;

procedure TForm16.BitBtn4Click(Sender: TObject);
var x:real;
begin
If Form16.Tag=1 then begin
if (ComboBox1.text='') or (Edit1.text='') then begin
Application.MessageBox('Введіть коректні дані','Помилка',MB_OK+MB_ICONWARNING); exit;
end; x:=StrToFloat(edit1.Text);
 Query4.SQL.Clear;
 Query4.SQL.Add('INSERT INTO vurychka (IdSpivrobitnuka, Syma, Data, IdMagazuny) values ('''+IDSp[ComboBox1.ItemIndex]+''','+QuotedStr(FloatToSQL(x))+','+Quotedstr(DateToSQLFormat(datetimepicker1.date))+','+Query1Id.AsString+')');
 Query4.ExecSQL;
 Query3.Close;
 Query3.Open; end;
If Form16.Tag=2 then
begin  x:=StrToFloat(edit1.Text);
 Query4.SQL.Clear;
 Query4.SQL.Add('UPDATE vurychka Set IdSpivrobitnuka='+IDSp[ComboBox1.ItemIndex]+', Syma='+QuotedStr(FloatToSQL(x))+', Data='+Quotedstr(DateToSQLFormat(datetimepicker1.date))+' WHERE vurychka.Id='+Query3.FieldByName('Id').AsString);
 Query4.ExecSQL;
 Query3.Close;
 Query3.Open;
 end;
  Edit1.Text:='';
  ComboBox1.Text:='';
  DateTimePicker1.Date:=date();
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  label5.Enabled:=false;
  end;

procedure TForm16.BitBtn5Click(Sender: TObject);
begin
  Edit1.Text:='';
  ComboBox1.Text:='';
  DateTimePicker1.Date:=date();
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  label5.Enabled:=false;
end;

procedure TForm16.FormActivate(Sender: TObject);
begin
  Edit1.Text:='';
  ComboBox1.Text:='';
  DateTimePicker1.Date:=date();
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  label5.Enabled:=false;
  DBGrid1.SetFocus;
end;

end.



