unit Unit11;

interface

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

type
  TForm11 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    DataSource2: TDataSource;
    DBGrid2: TDBGrid;
    Query3: TQuery;
    Query4: TQuery;
    Query1id: TIntegerField;
    Query1IdMagazuny: TIntegerField;
    Query1IdSpivrobitnuka: TIntegerField;
    Query1DataRozpodily: TDateField;
    Query1NazvaMagazuny: TStringField;
    Query1PIB: TStringField;
    Query3id: TIntegerField;
    Query3IdRozpodily: TIntegerField;
    Query3IdTovary: TIntegerField;
    Query3Kilkist: TFloatField;
    Query3CinaPostachannya: TFloatField;
    Query3id_1: TIntegerField;
    Query3nazvatovary: TStringField;
    Query3Suma: TIntegerField;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    ComboBox1: TComboBox;
    ComboBox2: TComboBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    BitBtn5: TBitBtn;
    BitBtn4: TBitBtn;
    DateTimePicker1: TDateTimePicker;
    GroupBox3: TGroupBox;
    BitBtn6: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    GroupBox4: TGroupBox;
    Label4: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    ComboBox3: TComboBox;
    BitBtn9: TBitBtn;
    BitBtn10: TBitBtn;
    Query5: TQuery;
    Query6: TQuery;
    GroupBox5: TGroupBox;
    DateTimePicker2: TDateTimePicker;
    BitBtn11: TBitBtn;
    BitBtn12: TBitBtn;
    BitBtn13: TBitBtn;
    BitBtn14: TBitBtn;
    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 BitBtn4Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn9Click(Sender: TObject);
    procedure BitBtn10Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure Filter();
    procedure DateTimePicker2CloseUp(Sender: TObject);
    procedure BitBtn11Click(Sender: TObject);
    procedure BitBtn12Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn8Click(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
  Form11: TForm11;
  IdMa,IdSp,IdTo:array of string;
  id_p:variant;

implementation

uses Unit12, Unit13, Unit18;

{$R *.dfm}

procedure TForm11.Query1AfterScroll(DataSet: TDataSet);
begin
Query3.Close();
if Query1.IsEmpty then exit;
Query3.SQL.Clear();
Query3.SQL.Add('Select pozuciirozpodily.id, pozuciirozpodily.IdRozpodily, pozuciirozpodily.IdTovary, pozuciirozpodily.Kilkist, pozuciirozpodily.CinaPostachannya, rozpodil.id, tovary.nazvatovary');
Query3.SQL.Add('From pozuciirozpodily, rozpodil, tovary');
Query3.SQL.Add('where pozuciirozpodily.IdRozpodily = rozpodil.id AND pozuciirozpodily.IdTovary = Tovary.id');
Query3.SQL.Add('and pozuciirozpodily.IdRozpodily='+Query1Id.AsString);
Query3.Open();
end;

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

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

procedure TForm11.BitBtn1Click(Sender: TObject);
var n,k:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM magazunu');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdMa);
if not Query2.IsEmpty then repeat
setLength(IdMa,Length(IdMa)+1);
IdMa[high(IdMa)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaMagazuny').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 TForm11.BitBtn2Click(Sender: TObject);
var i,n,k,j:integer;
s:string;
begin
Query2.SQL.Clear;
Query2.SQL.Add('SELECT* FROM magazunu');
Query2.Active:=False;
Query2.Active:=True;
ComboBox1.Items.Clear;
finalize(IdMa);
if not Query2.IsEmpty then repeat
setLength(IdMa,Length(IdMa)+1);
IdMa[high(IdMa)]:= Query2.FieldByName('Id').AsString;
ComboBox1.Items.Add(Query2.FieldByName('NazvaMagazuny').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 (IdMa) do
  for j:=1 to Length (IdSp) do begin
if IdMa[i-1]=Form11.Query1IdMagazuny.AsString
then begin
  ComboBox1.ItemIndex:=i-1; end;
if IdSp[j-1]=Form11.Query1IdSpivrobitnuka.AsString
then begin
  ComboBox2.ItemIndex:=j-1; end;  end;
IDMa[ComboBox1.ItemIndex]:=Query1.FieldByName('IdMagazuny').AsString;
IdSp[ComboBox2.ItemIndex]:=Query1.FieldByName('IdSpivrobitnuka').AsString;
datetimepicker1.date:=StrToDate(Query1DataRozpodily.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 TForm11.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 TForm11.BitBtn4Click(Sender: TObject);
begin
If Form11.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 rozpodil (IdMagazuny, IdSpivrobitnuka, DataRozpodily) values ('''+IDMa[ComboBox1.ItemIndex]+''','''+IDSp[ComboBox2.ItemIndex]+''','+Quotedstr(DateToSQLFormat(datetimepicker1.date))+')');
 Query6.ExecSQL;
 Query1.Close;
 Query1.Open; end;
If Form11.Tag=2 then
begin
 Query6.SQL.Clear;
 Query6.SQL.Add('UPDATE rozpodil Set IdMagazuny='+IDMa[ComboBox1.ItemIndex]+', IdSpivrobitnuka='+IDSp[ComboBox2.ItemIndex]+', DataRozpodily='+Quotedstr(DateToSQLFormat(datetimepicker1.date))+' WHERE rozpodil.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 TForm11.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 TForm11.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]=Form11.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('CinaPostachannya').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 TForm11.BitBtn9Click(Sender: TObject);
 Var x:real;
begin
If Form11.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 pozuciirozpodily (IdTovary,kilkist,CinaPostachannya,IdRozpodily) values ('''+IDto[ComboBox3.ItemIndex]+''','+QuotedStr(edit1.text)+','+QuotedStr(FloatToSQL(x))+','+Query1Id.AsString+')');
 Query4.ExecSQL;
 Query3.Close;
 Query3.Open; end;
If Form11.Tag=4 then
begin   x:=StrToFloat(edit2.Text);
 Query4.SQL.Clear;
 Query4.SQL.Add('UPDATE pozuciirozpodily Set IdTovary='+IDto[ComboBox3.ItemIndex]+', kilkist='+QuotedStr(Edit1.Text)+', cinaPostachannya='+QuotedStr(FloatToSQL(x))+', IdRozpodily='+Query1Id.AsString+' WHERE pozuciirozpodily.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 TForm11.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 TForm11.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 TForm11.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
  if not (Key in['0' .. '9', #8] ) then Key:=Chr(0);
end;

procedure TForm11.Filter;
begin
Query1.Filter:='DataRozpodily='+QuotedStr(DateToStr(DateTimePicker2.Date));
Query1.Filtered:= True;
end;

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

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

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

procedure TForm11.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 rozpodil Where Id='+Id_del);
 Query2.ExecSQL();
 Query1.Close();
 Query1.Open();
 if not Query1.IsEmpty then
 Query1.Locate('Id',Id_p,[]);
 dbgrid1.SetFocus();
end;

procedure TForm11.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 pozuciirozpodily WHERE pozuciirozpodily.id='+Query3.FieldByName('Id').AsString+';');
 Query4.ExecSQL;
 end;
 Query3.Active:=False;
 Query3.Active:=True;
end;

procedure TForm11.BitBtn13Click(Sender: TObject);
var i:integer; x,y:real;t:integer;
 begin
 i:=0;
      Query2.SQL.Clear();
      Query2.SQL.Add('select (baza.KilkistNaBazi-pozuciirozpodily.Kilkist) as kil from baza,pozuciirozpodily');
      Query2.SQL.Add('where baza.IdTovary=pozuciirozpodily.IdTovary');
      Query2.SQL.Add('and baza.IdTovary='+Query3IdTovary.AsString);
      Query2.open;
      repeat
      if Query2.FieldByName('kil').AsFloat<0 then
      i:=1;
      until Query2.MoveBy(1)<1;
      if i=1 then
      begin
      ShowMessage('На складі немає такої кількості товару');
      exit;
      end;
        if not Query3.IsEmpty then
            Query3.First();
            repeat
            Query4.SQL.Clear;
            Query4.SQL.Add('update baza SET KilkistNaBazi=KilkistNaBazi-'+Query3Kilkist.AsString);
            Query4.SQL.Add('where baza.IdTovary='+Query3IdTovary.AsString);
            Query4.ExecSQL;
            x:=StrToFloat(Query3CinaPostachannya.AsString);
            Query5.SQL.Clear;
            Query5.SQL.Add('Select (count(`Id`)) as ''cnt'' from `nayavnistvmagazuni`');
            Query5.SQL.Add('where (`IdTovary`='+Query3IdTovary.AsString+')and(`IdMagazuny`='+Query1IdMagazuny.AsString+');');
            Query5.active:=true;
            t:=Query5.FieldByName('cnt').AsInteger;
        if t>0 then
         begin
            Query5.SQL.Clear;
            Query5.SQL.Add('update nayavnistvmagazuni SET Kilkist=Kilkist+'+Query3Kilkist.AsString+', CinaOblikova='+QuotedStr(FloatToSQL(x))+', CinaRozdribna=1.25*'+QuotedStr(FloatToSQL(x)));
            Query5.SQL.Add('where nayavnistvmagazuni.IdTovary='+Query3IdTovary.AsString+ ' AND nayavnistvmagazuni.IdMagazuny='+Query1IdMagazuny.AsString);
            Query5.ExecSQL;
         end else
         Begin
            Query5.SQL.Clear;
            Query5.SQL.Add('insert into `nayavnistvmagazuni` values (NULL,'+Query1IdMagazuny.AsString+','+Query3IdTovary.AsString+','+Query3Kilkist.AsString+','+QuotedStr(FloatToSQL(x))+',1.25*'+QuotedStr(FloatToSQL(x))+');');
            Query5.ExecSQL;
         end;
            until Query3.MoveBy(1)<1;
query1.Close();
query1.Open();
Form12.Query1.Close();
Form12.Query1.Open();
Form13.Query3.Close();
Form13.Query3.Open();
Showmessage('Даний товар переміщений в магазин');
end;


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

procedure TForm11.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 TForm11.BitBtn16Click(Sender: TObject);
begin
  Edit3.Text:='';
end;

procedure TForm11.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 TForm11.BitBtn17Click(Sender: TObject);
begin
if not Query3.IsEmpty then
begin
Query1.Open();
Query3.Open();
QuickReport18.preview();
end
else
ShowMessage('Введіть дані про переміщення');
end;

end.
