unit Unit14;

interface

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

type
  TForm14 = class(TForm)
    DataSource1: TDataSource;
    DataSource2: TDataSource;
    DBGrid1: TDBGrid;
    DBGrid2: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    Query3: TQuery;
    Query1Id: TIntegerField;
    Query1NazvaMagazuny: TStringField;
    Query1AdresaMagazuny: TStringField;
    Query1IdSpivrobitnuka: TIntegerField;
    Query2id: TIntegerField;
    Query2IdMagazuny: TIntegerField;
    Query2IdTovary: TIntegerField;
    Query2Kilkist: TFloatField;
    Query2CinaRozdribna: TFloatField;
    Query2id_1: TIntegerField;
    Query2NazvaTovary: TStringField;
    GroupBox1: TGroupBox;
    GroupBox2: TGroupBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    Query2Data: TDateField;
    Label4: TLabel;
    DateTimePicker1: TDateTimePicker;
    BitBtn6: TBitBtn;
    GroupBox3: TGroupBox;
    DateTimePicker2: TDateTimePicker;
    DateTimePicker3: TDateTimePicker;
    Label5: TLabel;
    Label6: TLabel;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    Query4: TQuery;
    procedure Query1AfterScroll(DataSet: TDataSet);
    procedure BitBtn1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn3Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure Filter();
    procedure DateTimePicker2CloseUp(Sender: TObject);
    procedure DateTimePicker3CloseUp(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure Edit2KeyPress(Sender: TObject; var Key: Char);
    procedure FormKeyPress(Sender: TObject; var Key: Char);
    procedure BitBtn8Click(Sender: TObject);
  private
    { Private declarations }
  public
      function DateToSQLFormat(t:TDateTime):string;
      function FloatToSQL(x:real):string;
  end;

var
  Form14: TForm14;
  IdTo:array of string;
     id_p:variant;
implementation

uses Unit20;

{$R *.dfm}

procedure TForm14.Query1AfterScroll(DataSet: TDataSet);
begin
Query2.Close();
if Query1.IsEmpty then exit;
Query2.SQL.Clear();
Query2.SQL.Add('Select reveziyamagazuny.id,  reveziyamagazuny.IdMagazuny,  reveziyamagazuny.IdTovary,  reveziyamagazuny.Kilkist,  reveziyamagazuny.CinaRozdribna, reveziyamagazuny.Data, magazunu.id, tovary.NazvaTovary');
Query2.SQL.Add('From  reveziyamagazuny, magazunu, tovary');
Query2.SQL.Add('Where reveziyamagazuny.IdMagazuny = magazunu.id AND  reveziyamagazuny.IdTovary = tovary.id');
Query2.SQL.Add('and reveziyamagazuny.Idmagazuny='+Query1Id.AsString);
Query2.Open();
end;

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

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

procedure TForm14.FormActivate(Sender: TObject);
begin
  Query1.Open();
  Query2.Open();
  Edit1.Text:='';
  Edit2.Text:='';
  ComboBox1.Text:='';
  combobox1.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;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  DateTimePicker1.Enabled:=false;
 end;

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

for i:=1 to Length (IdTo) do  begin
if Idto[i-1]=Form14.Query2IdTovary.AsString
then
  ComboBox1.ItemIndex:=i-1;
 end;
IDTo[ComboBox1.ItemIndex]:=Query2.FieldByName('IdTovary').AsString;
Edit1.Text:=Query2.FieldByName('kilkist').AsString;
Edit2.Text:=Query2.FieldByName('CinaRozdribna').AsString;
datetimepicker1.date:=StrToDate(Query2Data.AsString);
  DateTimePicker1.Enabled:=true;
  combobox1.Enabled:=true;
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  label4.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
 Self.Tag:=2;
      end;
      
procedure TForm14.BitBtn4Click(Sender: TObject);
 Var x:real;
begin 
If Form14.Tag=1 then begin
if (ComboBox1.text='') or (Edit1.text='') or (Edit2.text='') then begin
Application.MessageBox('Введіть дані','Помилка',MB_OK+MB_ICONWARNING); exit;
end; x:=StrToFloat(edit2.Text);
 Query3.SQL.Clear;
 Query3.SQL.Add('INSERT INTO reveziyamagazuny (IdTovary,kilkist,Cinarozdribna,Data,IdMagazuny) values ('''+IDto[ComboBox1.ItemIndex]+''','+QuotedStr(edit1.text)+','+QuotedStr(FloatToSQL(x))+','+Quotedstr(DateToSQLFormat(datetimepicker1.date))+','+Query1Id.AsString+')');
 Query3.ExecSQL;
 Query2.Close;
 Query2.Open; end;
If Form14.Tag=2 then
begin x:=StrToFloat(edit2.Text);
 Query3.SQL.Clear;
 Query3.SQL.Add('UPDATE reveziyamagazuny Set IdTovary='+IDto[ComboBox1.ItemIndex]+', kilkist='+QuotedStr(Edit1.Text)+', cinaRozdribna='+QuotedStr(FloatToSQL(x))+', data='+Quotedstr(DateToSQLFormat(datetimepicker1.date))+', IdMagazuny='+Query1Id.AsString+' WHERE reveziyamagazuny.Id='+Query2.FieldByName('Id').AsString);
 Query3.ExecSQL;
 Query2.Close;
 Query2.Open;
 Query2.Locate('Id',Id_p,[]);
 end;
  ComboBox1.Text:='';
  Edit1.Text:='';
  Edit2.Text:='';
  combobox1.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;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  DateTimePicker1.Enabled:=false;
      end;


procedure TForm14.BitBtn5Click(Sender: TObject);
begin
    ComboBox1.Text:='';
  Edit1.Text:='';
  Edit2.Text:='';
  combobox1.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;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  DateTimePicker1.Enabled:=false;
end;

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

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


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

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

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

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

procedure TForm14.BitBtn7Click(Sender: TObject);
begin
Query2.Filtered:=False;
end;

procedure TForm14.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 TForm14.FormKeyPress(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 TForm14.BitBtn8Click(Sender: TObject);
begin
if not Query2.IsEmpty then
begin
Query1.Open();
Query2.Open();
Query4.Open();
QuickReport20.preview();
end
else
ShowMessage('Введіть дані про ревізію');
end;


end.
