unit Unit9;

interface

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

type
  TForm9 = class(TForm)
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Query1: TQuery;
    Query2: TQuery;
    Query1id: TIntegerField;
    Query1NazvaMagazuny: TStringField;
    Query1AdresaMagazuny: TStringField;
    Query1PIB: TStringField;
    Query1IdSpivrobitnuka: TIntegerField;
    GroupBox1: TGroupBox;
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    BitBtn3: TBitBtn;
    GroupBox2: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    ComboBox1: TComboBox;
    BitBtn4: TBitBtn;
    BitBtn5: TBitBtn;
    GroupBox3: TGroupBox;
    BitBtn6: TBitBtn;
    BitBtn7: TBitBtn;
    BitBtn8: TBitBtn;
    BitBtn9: TBitBtn;
    BitBtn10: TBitBtn;
    Query3: TQuery;
    Query4: TQuery;
    Query5: TQuery;
    procedure BitBtn10Click(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure BitBtn4Click(Sender: TObject);
    procedure BitBtn5Click(Sender: TObject);
    procedure BitBtn9Click(Sender: TObject);
    procedure BitBtn6Click(Sender: TObject);
    procedure BitBtn7Click(Sender: TObject);
    procedure BitBtn8Click(Sender: TObject);
    procedure BitBtn3Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form9: TForm9;
  IdSp:array of string;

implementation

uses Unit5, Unit6;

{$R *.dfm}

procedure TForm9.BitBtn10Click(Sender: TObject);
begin
 close;
end;

procedure TForm9.BitBtn1Click(Sender: TObject);
var n:integer;
s:string;
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:='';

  Edit1.Text:='';
  Edit2.Text:='';
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox1.Enabled:=true;
  Self.Tag:=1;
        end;

procedure TForm9.BitBtn2Click(Sender: TObject);
var i,n:integer;
s:string;
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]=Query1IdSpivrobitnuka.AsString
then
  ComboBox1.ItemIndex:=i-1; end;

Edit1.Text:=Query1.FieldByName('NazvaMagazuny').AsString;
Edit2.Text:=Query1.FieldByName('AdresaMagazuny').AsString;
IDSp[ComboBox1.ItemIndex]:=Query1.FieldByName('IdSpivrobitnuka').AsString;
  Edit1.Enabled:=true;
  Edit2.Enabled:=true;
  BitBtn4.Enabled:=true;
  BitBtn5.Enabled:=true;
  label1.Enabled:=true;
  label2.Enabled:=true;
  label3.Enabled:=true;
  BitBtn1.Enabled:=false;
  BitBtn2.Enabled:=false;
  BitBtn3.Enabled:=false;
  BitBtn6.Enabled:=false;
  BitBtn7.Enabled:=false;
  BitBtn8.Enabled:=false;
  BitBtn9.Enabled:=false;
  BitBtn10.Enabled:=false;
  combobox1.Enabled:=true;
  Self.Tag:=2;
        end;

 procedure TForm9.BitBtn4Click(Sender: TObject);
begin
If Form9.Tag=1 then begin
if (Edit1.text='') or  (Edit2.text='') then begin
Application.MessageBox('Введіть коректні дані','Помилка',MB_OK+MB_ICONWARNING);
exit;
end;
 Query3.SQL.Clear;
 Query3.SQL.Add('INSERT INTO magazunu (Nazvamagazuny,AdresaMagazuny,IdSpivrobitnuka) values ('+QuotedStr(edit1.text)+','+QuotedStr(edit2.text)+','''+IDSp[ComboBox1.ItemIndex]+''')');
 Query3.ExecSQL;
 Query1.Close;
 Query1.Open; end;
If Form9.Tag=2 then
begin
 Query3.SQL.Clear;
 Query3.SQL.Add('UPDATE magazunu Set Nazvamagazuny='+QuotedStr(Edit1.Text)+', AdresaMagazuny='+QuotedStr(Edit2.Text)+', IdSpivrobitnuka='+IdSp[ComboBox1.ItemIndex]+' WHERE magazunu.Id='+Query1.FieldByName('Id').AsString);
 Query3.ExecSQL;
 Query1.Close;
 Query1.Open;
 end;

  Edit1.Text:='';
  Edit2.Text:='';
  combobox1.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  combobox1.Enabled:=false;
   BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
  DbGrid1.SetFocus();
end;


procedure TForm9.BitBtn5Click(Sender: TObject);
begin
  Edit1.Text:='';
  Edit2.Text:='';
  combobox1.Text:='';
  Edit1.Enabled:=false;
  Edit2.Enabled:=false;
  label1.Enabled:=false;
  label2.Enabled:=false;
  label3.Enabled:=false;
  combobox1.Enabled:=false;
  BitBtn1.Enabled:=true;
  BitBtn2.Enabled:=true;
  BitBtn3.Enabled:=true;
  BitBtn4.Enabled:=false;
  BitBtn5.Enabled:=false;
  BitBtn3.Enabled:=true;
  BitBtn6.Enabled:=true;
  BitBtn7.Enabled:=true;
  BitBtn8.Enabled:=true;
  BitBtn9.Enabled:=true;
  BitBtn10.Enabled:=true;
end;

procedure TForm9.BitBtn9Click(Sender: TObject);
var
 XLApp,Sheet,Colum:Variant;
 index,index2,i,s:Integer;
begin
try
 XLApp:= CreateOleObject('Excel.Application');
 TQuery(DBGrid1.DataSource.DataSet).DisableControls;
//Замість DBGrid1 буде назва Вашого DBGrid'a, в якому відображаються дані
 XLApp.Visible:=False;
 XLApp.Workbooks.Add(-4167);
 XLApp.Workbooks[1].WorkSheets[1].Name:=Self.Caption;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Columns;
 index:=1;
 Colum:=XLApp.Workbooks[1].WorkSheets[Self.Caption].Rows;
 Colum.Rows[1].Font.Bold:=true;
 Colum.Rows[1].Font.Size:=10;
 Sheet:=XLApp.Workbooks[1].WorkSheets[Self.Caption];
 for I := 1 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Visible then
   begin
   if TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size>0 then Colum.Columns[index].ColumnWidth:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].Size
   else Colum.Columns[index].ColumnWidth:=12;
   Sheet.Cells[1,index].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[i-1].DisplayLabel;
   Inc(index);
   end;
TQuery(DBGrid1.DataSource.DataSet).First;
 for i:=2 to TQuery(DBGrid1.DataSource.DataSet).RecordCount+1 do
  begin
  index2:=1;
   for s := 0 to  TQuery(DBGrid1.DataSource.DataSet).FieldCount-1 do
   if TQuery(DBGrid1.DataSource.DataSet).Fields[s].Visible then
   begin
   Sheet.Cells[i,index2].Value:=TQuery(DBGrid1.DataSource.DataSet).Fields[s].AsString;
   Inc(index2);
   end;
   TQuery(DBGrid1.DataSource.DataSet).Next;
  end;
XLApp.Range[XLApp.Cells[1, 1], XLApp.Cells[TQuery(DBGrid1.DataSource.DataSet).RecordCount+1,index-1]].Select;
XLApp.Selection.Borders.LineStyle:=1;
XLApp.Selection.WrapText:=True;
XLApp.Range['A1'].Select;
TQuery(DBGrid1.DataSource.DataSet).EnableControls;
XLApp.Visible:=true;
except
Application.MessageBox(PChar('Microsoft Ecxel не інстальований, або виникла інша помилка.'), 'Помилка!!!', MB_ICONWARNING+MB_OK);
  if VarIsEmpty(XLApp) = false then
  begin
    XLApp.Quit;
    XLApp := 0;
  end;
exit;
end;
end;

procedure TForm9.BitBtn6Click(Sender: TObject);
var s:string;
begin
 s:=DBGrid1.SelectedField.FieldName;
 Form5.ShowModal;
 if Form5.ModalResult=mrCancel then exit;
 Query1.close;
 if Query1.SQL.Count<3 then Query1.SQL.add(' ');
 Query1.SQL[2]:=Query1.SQL[2]+' AND '+s+'='''+Form5.Edit1.Text+'''';
Form9.Query1.Open;
end;

procedure TForm9.BitBtn7Click(Sender: TObject);
var s:string;
begin
 s:=DBGrid1.SelectedField.FieldName;
 Form6.ShowModal;
 if Form6.ModalResult=mrCancel then exit;
 Query1.close;
 if Query1.SQL.Count<3 then Query1.SQL.add(' ');
 Query1.SQL[2]:=Query1.SQL[2]+' AND '+s+' LIKE ''%'+Form6.Edit1.Text+'%''';
 Form9.Query1.Open;
end;

procedure TForm9.BitBtn8Click(Sender: TObject);
begin
  Query1.Close;
  Query1.SQL[2]:='WHERE magazunu.IdSpivrobitnuka = spivrobitnuku.Id';
  Query1.Open;
end;

procedure TForm9.BitBtn3Click(Sender: TObject);
var
 uc1,uc2,uc3,uc4:integer;
begin
 Query2.Active:=False;
 Query2.SQL.Clear;
 Query2.SQL.Add(FORMAT('SELECT COUNT(*) From nayavnistvmagazuni where nayavnistvmagazuni.IdMagazuny=%s;',[Query1.FieldByName('Id').AsString]));
 Query2.Active:=True;
 uc1:=Query2.FieldByName('COUNT(*)').AsInteger;

 Query3.Active:=False;
 Query3.SQL.Clear;
 Query3.SQL.Add(FORMAT('SELECT COUNT(*) From reveziyamagazuny where reveziyamagazuny.IdMagazuny=%s;',[Query1.FieldByName('Id').AsString]));
 Query3.Active:=True;
 uc2:=Query3.FieldByName('COUNT(*)').AsInteger;

 Query4.Active:=False;
 Query4.SQL.Clear;
 Query4.SQL.Add(FORMAT('SELECT COUNT(*) From rozpodil where rozpodil.IdMagazuny=%s;',[Query1.FieldByName('Id').AsString]));
 Query4.Active:=True;
 uc3:=Query4.FieldByName('COUNT(*)').AsInteger;

 Query5.Active:=False;
 Query5.SQL.Clear;
 Query5.SQL.Add(FORMAT('SELECT COUNT(*) From vurychka where vurychka.IdMagazuny=%s;',[Query1.FieldByName('Id').AsString]));
 Query5.Active:=True;
 uc4:=Query5.FieldByName('COUNT(*)').AsInteger;

if (uc1=0) and (uc2=0) and (uc3=0) then
 Begin   if
 Application.MessageBox('Ви дійсно хочете видалити запис?','Увага!!!',MB_YesNo+MB_ICONQUESTION)=MrYes then
 begin
 Query2.Active:=False;
 Query2.SQL.Clear;
 Query2.SQL.Add('DELETE FROM magazunu WHERE magazunu.Id='+Query1.FieldByName('Id').AsString+';');
 Query2.ExecSQL;
 Query1.Active:=False;
 Query1.Active:=True;
 end;
 end else
  MessageBoxA(Handle,'Неможливо видалити, запис зустрічається в інших таблицях!','Помилка!',MB_ICONERROR);
end;


end.

