unit Unit7;

interface

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

type
  TForm7 = class(TForm)
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    Query1id: TIntegerField;
    Query1nazvaPostachalnuka: TStringField;
    Query1PIB: TStringField;
    Query1DataNadhodgenya: TDateField;
    Query1IdPostachalnuka: TIntegerField;
    Query1IdSpivrobitnuka: TIntegerField;
    Query3: TQuery;
    Query4: TQuery;
    Query3id: TIntegerField;
    Query3IdTovary: TIntegerField;
    Query3IdNadhodgennya: TIntegerField;
    Query3Kilkist: TFloatField;
    Query3Cina: TFloatField;
    Query3id_1: TIntegerField;
    Query3NazvaTovary: TStringField;
    Query3Suma: TFloatField;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    DateTimePicker1: TDateTimePicker;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    GroupBox3: TGroupBox;
    GroupBox4: TGroupBox;
    BitBtn6: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    ComboBox3: TComboBox;
    Edit1: TEdit;
    Edit2: TEdit;
    BitBtn9: TBitBtn;
    BitBtn10: TBitBtn;
    Query5: TQuery;
    GroupBox5: TGroupBox;
    DateTimePicker2: TDateTimePicker;
    DateTimePicker3: TDateTimePicker;
    BitBtn11: TBitBtn;
    BitBtn12: TBitBtn;
    BitBtn13: TBitBtn;
    BitBtn14: TBitBtn;
    Query6: TQuery;
    BitBtn15: TBitBtn;
    Edit3: TEdit;
    BitBtn16: TBitBtn;
    BitBtn17: TBitBtn;
    procedure Query1AfterScroll(DataSet: TDataSet);
    procedure Query3CalcFields(DataSet: TDataSet);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn10Click(Sender: TObject);
    procedure BitBtn9Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn8Click(Sender: TObject);
    procedure Filter();
    procedure DateTimePicker2CloseUp(Sender: TObject);
    procedure DateTimePicker3CloseUp(Sender: TObject);
    procedure BitBtn11Click(Sender: TObject);
    procedure BitBtn12Click(Sender: TObject);
    procedure BitBtn13Click(Sender: TObject);
    procedure BitBtn14Click(Sender: TObject);
    procedure BitBtn15Click(Sender: TObject);
    procedure BitBtn16Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn17Click(Sender: TObject);
  private
    { Private declarations }
  public
    function DateToSQLFormat(t:TDateTime):string;
    function FloatToSQL(x:real):string;
  end;

var
  Form7: TForm7;
  IdPo,IdSp,IdTo:array of string;
   id_p:variant;

implementation

uses Unit12, Unit17;

{$R *.dfm}

procedure TForm7.Query1AfterScroll(DataSet: TDataSet);
begin
Query3.Close();
if Query1.IsEmpty then exit;
Query3.SQL.Clear();
Query3.SQL.Add('Select chonadishlo.id, chonadishlo.IdTovary, chonadishlo.idNadhodgennya, chonadishlo.Kilkist, chonadishlo.Cina, nadhodgennya.id, tovary.NazvaTovary');
Query3.SQL.Add('From chonadishlo, nadhodgennya, tovary');
Query3.SQL.Add('Where chonadishlo.IdTovary = Tovary.Id AND chonadishlo.Idnadhodgennya = nadhodgennya.Id');
Query3.SQL.Add('and chonadishlo.IdNadhodgennya='+Query1Id.AsString);
Query3.Open();
end;

procedure TForm7.Query3CalcFields(DataSet: TDataSet);
begin
 Query3Suma.AsFloat:=Query3Kilkist.AsFloat*Query3Cina.AsFloat;
end;

 function TForm7.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 TForm7.FloatToSQL(x:real):string;
begin
result:=floattostr(x);
if pos (',',result)>0 then
result[pos(',',result)]:='.';
end;

procedure TForm7.BitBtn1Click(Sender: TObject);
var n,k:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM postachalnuku');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdPo);
if not Query2.IsEmpty then repeat
setLength(IdPo,Length(IdPo)+1);
IdPo[high(IdPo)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaPostachalnuka').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';

Query5.SQL.Clear;
Query5.SQL.Add('SELECT* FROM spivrobitnuku');
Query5.Active:=False;
Query5.Active:=True;
ComboBox2.Items.Clear;
finalize(IdSp);
if not Query5.IsEmpty then repeat
setLength(IdSp,Length(IdSp)+1);
IdSp[high(IdSp)]:= Query5.FieldByName('Id').AsString;
ComboBox2.Items.Add(Query5.FieldByName('PIB').AsString);
until Query5.MoveBy(1)<1;
     ComboBox2.ItemIndex:=-1;
     ComboBox2.Text:='';

  DateTimePicker1.Date:=date();
  combobox1.Enabled:=true;
  combobox2.Enabled:=true;
  DateTimePicker1.Enabled:=true;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=false;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
 Self.Tag:=1;
        end;

procedure TForm7.BitBtn2Click(Sender: TObject);
var i,n,k,j:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM postachalnuku');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdPo);
if not Query2.IsEmpty then repeat
setLength(IdPo,Length(IdPo)+1);
IdPo[high(IdPo)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaPostachalnuka').AsString);
until Query2.MoveBy(1)<1;
     ComboBox1.ItemIndex:=-1;
     ComboBox1.Text:='';

Query5.SQL.Clear;
Query5.SQL.Add('SELECT* FROM spivrobitnuku');
Query5.Active:=False;
Query5.Active:=True;
ComboBox2.Items.Clear;
finalize(IdSp);
if not Query5.IsEmpty then repeat
setLength(IdSp,Length(IdSp)+1);
IdSp[high(IdSp)]:= Query5.FieldByName('Id').AsString;
ComboBox2.Items.Add(Query5.FieldByName('PIB').AsString);
until Query5.MoveBy(1)<1;
     ComboBox2.ItemIndex:=-1;
     ComboBox2.Text:='';

for i:=1 to Length (Idpo) do
  for j:=1 to Length (IdSp) do begin
if IdPo[i-1]=Form7.Query1IdPostachalnuka.AsString
then begin
  ComboBox1.ItemIndex:=i-1; end;
if IdSp[j-1]=Form7.Query1IdSpivrobitnuka.AsString
then begin
  ComboBox2.ItemIndex:=j-1; end;  end;
IDPo[ComboBox1.ItemIndex]:=Query1.FieldByName('IdPostachalnuka').AsString;
IdSp[ComboBox2.ItemIndex]:=Query1.FieldByName('IdSpivrobitnuka').AsString;
datetimepicker1.date:=StrToDate(Query1DataNadhodgenya.AsString);

  combobox1.Enabled:=true;
  combobox2.Enabled:=true;
  DateTimePicker1.Enabled:=true;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=false;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
 Self.Tag:=2;
    end;

procedure TForm7.BitBtn5Click(Sender: TObject);
begin
  ComboBox1.Text:='';
  ComboBox2.Text:='';
  DateTimePicker1.Date:=date();
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
end;

procedure TForm7.FormActivate(Sender: TObject);
begin
  Query1.Open();
  Query3.Open();
  Edit1.Text:='';
  Edit2.Text:='';
  ComboBox1.Text:='';
  ComboBox2.Text:='';
  ComboBox3.Text:='';
  DateTimePicker1.Date:=date();
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
end;

procedure TForm7.BitBtn4Click(Sender: TObject);
begin
If Form7.Tag=1 then begin
if (ComboBox1.text='') or (ComboBox2.text='') then begin
Application.MessageBox('Введіть коректні дані','Помилка',MB_OK+MB_ICONWARNING); exit;
end;
 Query6.SQL.Clear;
 Query6.SQL.Add('INSERT INTO nadhodgennya (IdPostachalnuka, IdSpivrobitnuka, DataNadhodgenya) values ('''+IDPo[ComboBox1.ItemIndex]+''','''+IDSp[ComboBox2.ItemIndex]+''','+Quotedstr(DateToSQLFormat(datetimepicker1.date))+')');
 Query6.ExecSQL;
 Query1.Close;
 Query1.Open; end;
If Form7.Tag=2 then
begin
 Query6.SQL.Clear;
 Query6.SQL.Add('UPDATE nadhodgennya Set IdPostachalnuka='+IDPo[ComboBox1.ItemIndex]+', IdSpivrobitnuka='+IDSp[ComboBox2.ItemIndex]+', DataNadhodgenya='+Quotedstr(DateToSQLFormat(datetimepicker1.date))+' WHERE nadhodgennya.Id='+Query1.FieldByName('Id').AsString);
 Query6.ExecSQL;
 Query1.Close;
 Query1.Open;
 end;
 ComboBox1.Text:='';
  ComboBox2.Text:='';
  DateTimePicker1.Date:=date();
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
  end;

procedure TForm7.BitBtn3Click(Sender: TObject);
var
id_del:string;
begin
if Application.MessageBox('Ви дійсно хочете видалити запис?','Видалення',MB_YESNO+MB_IconQuestion)=mrNo then
Exit;
 id_del:=Query1Id.asstring;
 if Query1.MoveBy(1)<1 then
 Query1.MoveBy(-1);
 id_p:=Query1Id.asstring;
 Query2.SQL.Clear();
 Query2.SQL.Add('DELETE from nadhodgennya Where Id='+Id_del);
 Query2.ExecSQL();
 Query1.Close();
 Query1.Open();
 if not Query1.IsEmpty then
 Query1.Locate('Id',Id_p,[]);
 dbgrid1.SetFocus();
end;

procedure TForm7.BitBtn6Click(Sender: TObject);
var n:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM tovary');
Query2.Active:=False;
Query2.Active:=True;
ComboBox3.Items.Clear;
finalize(IdTo);
if not Query2.IsEmpty then repeat
setLength(IdTo,Length(IdTo)+1);
IdTo[high(IdTo)]:= Query2.FieldByName('Id').AsString;
ComboBox3.Items.Add(Query2.FieldByName('NazvaTovary').AsString);
until Query2.MoveBy(1)<1;
  ComboBox3.ItemIndex:=-1;
  ComboBox3.Text:='';
  Edit1.Text:='';
  Edit2.Text:='';
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label4.Enabled:=true;
  label5.Enabled:=true;
  label6.Enabled:=true;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
  combobox3.Enabled:=true;
 Self.Tag:=3;
        end;

procedure TForm7.BitBtn7Click(Sender: TObject);
var i,n:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM tovary');
Query2.Active:=False;
Query2.Active:=True;
ComboBox3.Items.Clear;
finalize(IdTo);
if not Query2.IsEmpty then repeat
setLength(IdTo,Length(IdTo)+1);
IdTo[high(IdTo)]:= Query2.FieldByName('Id').AsString;
ComboBox3.Items.Add(Query2.FieldByName('NazvaTovary').AsString);
until Query2.MoveBy(1)<1;
     ComboBox3.ItemIndex:=-1;
     ComboBox3.Text:='';

for i:=1 to Length (IdTo) do  begin
if Idto[i-1]=Form7.Query3IdTovary.AsString
then
  ComboBox3.ItemIndex:=i-1;
 end;
IDTo[ComboBox3.ItemIndex]:=Query3.FieldByName('IdTovary').AsString;
Edit1.Text:=Query3.FieldByName('kilkist').AsString;
Edit2.Text:=Query3.FieldByName('Cina').AsString;
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label4.Enabled:=true;
  label5.Enabled:=true;
  label6.Enabled:=true;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
  combobox3.Enabled:=true;
 Self.Tag:=4;
    end;

procedure TForm7.BitBtn10Click(Sender: TObject);
begin
  ComboBox3.Text:='';
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false;
end;

procedure TForm7.BitBtn9Click(Sender: TObject);
var x:real;
begin
If Form7.Tag=3 then begin
if (ComboBox3.text='') or (Edit1.text='') or (Edit2.text='') then begin
Application.MessageBox('Введіть дані','Помилка',MB_OK+MB_ICONWARNING); exit;
end; x:=StrToFloat(edit2.Text);
 Query4.SQL.Clear;
 Query4.SQL.Add('INSERT INTO chonadishlo (IdTovary,kilkist,Cina,IdNadhodgennya) values ('''+IDto[ComboBox3.ItemIndex]+''','+QuotedStr(edit1.text)+','+QuotedStr(FloatToSQL(x))+','+Query1Id.AsString+')');
 Query4.ExecSQL;
 Query3.Close;
 Query3.Open; end;
If Form7.Tag=4 then
begin  x:=StrToFloat(edit2.Text);
 Query4.SQL.Clear;
 Query4.SQL.Add('UPDATE chonadishlo Set IdTovary='+IDto[ComboBox3.ItemIndex]+', kilkist='+QuotedStr(Edit1.Text)+', cina='+QuotedStr(FloatToSQL(x))+', IdNadhodgennya='+Query1Id.AsString+' WHERE chonadishlo.Id='+Query3.FieldByName('Id').AsString);
 Query4.ExecSQL;
 Query3.Close;
 Query3.Open;
 Query3.Locate('Id',Id_p,[]);
 end;
 ComboBox3.Text:='';
  combobox1.Enabled:=false;
  combobox2.Enabled:=false;
  DateTimePicker1.Enabled:=false;
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  label4.Enabled:=false;;
  label5.Enabled:=false;
  label6.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox3.Enabled:=false; end;

procedure TForm7.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in['0' .. '9', #8] ) then Key:=Chr(0);
end;

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

procedure TForm7.Filter;
begin
Query1.Filter:='DataNadhodgenya>='''+(DateToStr(DateTimePicker2.Date))+''''+' and DataNadhodgenya<='''+(DateToStr(DateTimePicker3.Date))+'''';
Query1.Filtered:= True;
end;


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

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

procedure TForm7.BitBtn11Click(Sender: TObject);
begin
Query1.Filtered:=False;
end;

procedure TForm7.BitBtn12Click(Sender: TObject);
begin
  close;
end;

procedure TForm7.BitBtn13Click(Sender: TObject);
var
id_del:string;  t:integer;
begin
if not Query3.IsEmpty then
Query3.First();
repeat
  Query2.SQL.Clear;
  Query2.SQL.Add('Select (count(`Id`)) as ''cnt'' from `baza`');
  Query2.SQL.Add('where (`IdTovary`='+Query3IdTovary.AsString+');');
  Query2.active:=true;
  t:=Query2.FieldByName('cnt').AsInteger;
  if t>0 then
     begin
  Query2.SQL.Clear();
  Query2.SQL.Add('Update baza SET KilkistNaBazi=KilkistNaBazi+('+Quotedstr(Query3Kilkist.AsString)+') where IdTovary='+Query3IdTovary.AsString);
  Query2.ExecSQL;
 end else
         Begin
  Query2.SQL.Clear;
  Query2.SQL.Add('insert into `baza` values (NULL,'+Query3IdTovary.AsString+','+Query3Kilkist.AsString+');');
  Query2.ExecSQL;
         end;
until Query3.MoveBy(1)<1;
query1.Close();
query1.Open();
Form12.Query1.Close();
Form12.Query1.Open();
end;

procedure TForm7.BitBtn14Click(Sender: TObject);
begin
 Form12.ShowModal;
end;

procedure TForm7.BitBtn15Click(Sender: TObject);
var i:integer; Sum:real;
begin
Query3.First;
Sum:=0;
 for i:=1 to Query3.RecordCount do
begin
Sum:=Sum+Query3.FieldByName('Suma').AsFloat;
Query3.Next;
end;
Edit3.Text:=FloatToStr(Sum);
  end;

procedure TForm7.BitBtn16Click(Sender: TObject);
begin
  Edit3.Text:='';
end;

procedure TForm7.Edit2KeyPress(Sender: TObject; var Key: Char);
begin
   case Key of
    '0' .. '9', #8 : ;
      ',': begin
       if (Pos(',',Edit2.Text)<>0) then  Key:=Chr(0);
          end; else Key:=Chr(0);
end; 
end;

procedure TForm7.BitBtn17Click(Sender: TObject);
begin
if not Query3.IsEmpty then
begin
Query1.Open();
Query3.Open();
QuickReport17.preview();
end
else
ShowMessage('Введіть дані про постачання');
end;

end.
