unit Udata;

interface

uses

   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, Grids, DBGrids, ExtCtrls, StdCtrls,DateUtils, jpeg,
  Calendar, ComCtrls,DB, DBTables, ImgList,qdialogs,QControls;

type
    Tdata = class(TDataModule)
    Darticle: TDataSource;
    Dtypeop: TDataSource;
    Dheader: TDataSource;
    Dentretien: TDataSource;
    Dbus: TDataSource;
    Tanalyse: TTable;
    Tarticle: TTable;
    Theader: TTable;
    Tbus: TTable;
    Ttypeop: TTable;
    Tentretien: TTable;
    Tkm: TTable;
    Tproprietaire: TTable;
    Ttypebus: TTable;
    Tdetailcommande: TTable;
    Dkm: TDataSource;
    Dproprietaire: TDataSource;
    Dtypebus: TDataSource;
    Ddetailcommande: TDataSource;
    Danalyse: TDataSource;
    Doperation: TDataSource;
    Dbusoperation: TDataSource;
    Dcontrole: TDataSource;
    Toperation: TTable;
    Tbusoperation: TTable;
    Tcontrole: TTable;
    Tcpville: TTable;
    Dcpville: TDataSource;
    Dfournisseur: TDataSource;
    Tfournisseur: TTable;
    TarticleART_ID: TFloatField;
    TarticleART_NUMFOU: TStringField;
    TarticleART_REF: TStringField;
    TarticleART_DES: TStringField;
    TarticleART_PXUNI: TFloatField;
    TarticleART_FOURID: TFloatField;
    TproprietairePRO_ID: TFloatField;
    TproprietairePRO_NOM: TStringField;
    TproprietairePRO_RUE: TStringField;
    TproprietairePRO_NUM: TStringField;
    TproprietairePRO_CP_ID: TFloatField;
    TdetailcommandeDET_ID: TFloatField;
    TdetailcommandeDET_QTT: TFloatField;
    TdetailcommandeDET_LIV: TBooleanField;
    TdetailcommandeDET_BUS_ID: TFloatField;
    TdetailcommandeDET_HEA_ID: TFloatField;
    TdetailcommandeDET_ART_ID: TFloatField;
    TfournisseurFOUR_ID: TFloatField;
    TfournisseurFOUR_SOC: TStringField;
    TfournisseurFOUR_RUE: TStringField;
    TfournisseurFOUR_NUM: TStringField;
    TfournisseurFOUR_PRE: TStringField;
    TfournisseurFOUR_CP_ID: TFloatField;
    TheaderHEA_ID: TFloatField;
    TheaderHEA_NUM: TFloatField;
    TheaderHEA_DATE: TDateField;
    TheaderHEA_REM: TMemoField;
    TheaderHEA_COM: TStringField;
    TheaderHEA_LIV: TBooleanField;
    TheaderHEA_PROIDL: TFloatField;
    TheaderHEA_PROIDF: TFloatField;
    TcontroleCONT_ID: TFloatField;
    TcontroleCONT_DTRE: TDateField;
    TcontroleCONT_DTLI: TDateField;
    TcontroleCONT_RESU: TStringField;
    TcontroleCONT_BUSID: TFloatField;
    TbusoperationBOP_TYBUID: TFloatField;
    TbusoperationBOP_TYOPID: TFloatField;
    ToperationOP_ENT_ID: TFloatField;
    ToperationOP_TYOP_ID: TFloatField;
    ToperationOP_COM: TStringField;
    TtypebusTYBU_ID: TFloatField;
    TtypebusTYBU_MOD: TStringField;
    TkmKM_KM: TFloatField;
    TkmKM_DATE: TDateField;
    TkmKM_BUS_ID: TFloatField;
    TentretienENT_ID: TFloatField;
    TentretienENT_KMREEL: TFloatField;
    TentretienENT_DATE: TDateField;
    TentretienENT_BUS_ID: TFloatField;
    TtypeopTYOP_ID: TFloatField;
    TtypeopTYOP_LIB: TStringField;
    TtypeopTYOP_KIL: TFloatField;
    TbusBUS_ID: TFloatField;
    TbusBUS_NUMPL: TStringField;
    TbusBUS_DTMES: TDateField;
    TbusBUS_NUMCHA: TStringField;
    TbusBUS_NUMID: TStringField;
    TbusBUS_TYBUID: TFloatField;
    TbusBUS_PRO_ID: TFloatField;
    TanalyseANA_ID: TFloatField;
    TanalyseANA_DATEEN: TDateField;
    TanalyseANA_DATERE: TDateField;
    TanalyseANA_RESULT: TStringField;
    Dtempo: TDataSource;
    Ttempo: TTable;
    TtempoTEMPO_ID: TFloatField;
    TtempoTEMPO_LIB: TStringField;
    TtempoTEMPO_COC: TBooleanField;
    TanalyseANA_BUS_ID: TFloatField;
    TarticleART_LIB: TStringField;
    TcpvilleCP_ID: TSmallintField;
    TcpvilleCP_NUM: TStringField;
    TcpvilleCP_LOC: TStringField;
    TfournisseurFOU_C_CP: TStringField;
    TfournisseurFOU_CP_VILLE: TStringField;
    TdetailcommandeDET_BUS_NUMID: TStringField;
    TdetailcommandeDET_ART_LIB: TStringField;
    TdetailcommandeDET_HEA_NUM: TIntegerField;
    Ddemarage: TDataSource;
    Tdemarage: TTable;
    TarticleART_FOUR_SOC: TStringField;
    TbusBus_tybu_mod: TStringField;
    Tbusbus_pro_nom: TStringField;
    Toperationop_ent_date: TDateField;
    Toperationop_tyop_lib: TStringField;
    Tproprietairepro_cp_ville: TStringField;
    Tbusoperationbop_tybumod: TStringField;
    Tbusoperationbop_tyoplib: TStringField;
    TentretienENT_COM: TStringField;
    TdemarageDE_BUSNUMP: TStringField;
    TdemarageDE_BUSNUMI: TStringField;
    TdemarageDE_TYBUMOD: TStringField;
    TdemarageDE_CONTDTR: TDateField;
    TdemarageDE_KMKM: TFloatField;
    TdemarageDE_TYOPLIB: TStringField;
    procedure TanalyseNewRecord(DataSet: TDataSet);
    procedure TarticleBeforeInsert(DataSet: TDataSet);
    procedure TarticleNewRecord(DataSet: TDataSet);
    procedure TheaderBeforeInsert(DataSet: TDataSet);
    procedure TheaderNewRecord(DataSet: TDataSet);
    procedure TbusBeforeInsert(DataSet: TDataSet);
    procedure TbusNewRecord(DataSet: TDataSet);
    procedure TtypeopBeforeInsert(DataSet: TDataSet);
    procedure TtypeopNewRecord(DataSet: TDataSet);
    procedure TentretienBeforeInsert(DataSet: TDataSet);
    procedure TentretienNewRecord(DataSet: TDataSet);
    procedure TproprietaireBeforeInsert(DataSet: TDataSet);
    procedure TproprietaireNewRecord(DataSet: TDataSet);
    procedure TtypebusBeforeInsert(DataSet: TDataSet);
    procedure TtypebusNewRecord(DataSet: TDataSet);
    procedure TdetailcommandeBeforeInsert(DataSet: TDataSet);
    procedure TdetailcommandeNewRecord(DataSet: TDataSet);
    procedure TbusBeforeDelete(DataSet: TDataSet);
    procedure TentretienBeforeDelete(DataSet: TDataSet);
    procedure TtypeopBeforeDelete(DataSet: TDataSet);
    procedure TanalyseBeforeInsert(DataSet: TDataSet);
    procedure TcontroleBeforeInsert(DataSet: TDataSet);
    procedure TcontroleNewRecord(DataSet: TDataSet);
    procedure TcpvilleNewRecord(DataSet: TDataSet);
    procedure TcpvilleBeforeInsert(DataSet: TDataSet);
    procedure TfournisseurBeforeInsert(DataSet: TDataSet);
    procedure TfournisseurNewRecord(DataSet: TDataSet);
    procedure TheaderBeforeDelete(DataSet: TDataSet);
    procedure TproprietaireBeforeDelete(DataSet: TDataSet);
    procedure TcpvilleBeforeDelete(DataSet: TDataSet);
    procedure TfournisseurBeforeDelete(DataSet: TDataSet);
    procedure TarticleBeforeDelete(DataSet: TDataSet);
    procedure TtypebusBeforeDelete(DataSet: TDataSet);


  private
    { Déclarations privées }
  public
    { Déclarations publiques }
  end;

var
  data: Tdata;
  newnum:integer;

implementation
{$R *.dfm}
uses uprinc;
procedure Incrementation (table : TTable ; champid : TField ; indexname : string);
Var IndexActuel : STRING;
    filtre:boolean;
    maitre:Tdatasource;

begin
IndexActuel := table.IndexName;
filtre := table.Filtered;
maitre := table.MasterSource;
table.IndexName :=indexname;
table.Filtered := false;
table.MasterSource := nil;
table.Last;
NewNum := champid.AsInteger + 1 ;
table.IndexName := IndexActuel;
table.Filtered := filtre;
table.MasterSource := maitre;
end;

procedure Tdata.TanalyseBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tanalyse,TanalyseAna_id,'IANA_PK');
end;
procedure Tdata.TarticleBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tarticle,TarticleART_ID,'IART_PK');
end;
procedure Tdata.TanalyseNewRecord(DataSet: TDataSet);
begin
Data.TanalyseANA_ID.AsInteger := NewNum;
end;
procedure Tdata.TarticleNewRecord(DataSet: TDataSet);
begin
Data.TarticleART_ID.AsInteger := NewNum;
end;
procedure Tdata.TheaderBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Theader,TheaderHEA_ID,'IHEA_PK');
end;
procedure Tdata.TheaderNewRecord(DataSet: TDataSet);
begin
 Data.TheaderHEA_ID.AsInteger := NewNum;
end;

procedure Tdata.TbusBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tbus,TbusBUS_ID,'IBUS_PK');
end;

procedure Tdata.TbusNewRecord(DataSet: TDataSet);
begin
Data.TbusBUS_ID.AsInteger := NewNum;
end;

procedure Tdata.TtypeopBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Ttypeop,TtypeopTYOP_ID,'ITYOP_PK');

end;

procedure Tdata.TtypeopNewRecord(DataSet: TDataSet);
begin
 Data.TtypeopTYOP_ID.AsInteger := NewNum;
end;

procedure Tdata.TentretienBeforeInsert(DataSet: TDataSet);

begin
Incrementation(Tentretien,TentretienENT_ID,'IENT_PK');
end;

procedure Tdata.TentretienNewRecord(DataSet: TDataSet);
begin
 Data.TentretienENT_ID.AsInteger := NewNum;
end;


procedure Tdata.TproprietaireBeforeInsert(DataSet: TDataSet);

begin
Incrementation(Tproprietaire,TproprietairePRO_ID,'IPRO_PK');

end;

procedure Tdata.TproprietaireNewRecord(DataSet: TDataSet);
begin
  Data.TproprietairePRO_ID.AsInteger := NewNum;
end;

procedure Tdata.TtypebusBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Ttypebus,TtypebusTYBU_ID,'ITYBU_PK');
end;

procedure Tdata.TtypebusNewRecord(DataSet: TDataSet);
begin
 Data.TtypebusTYBU_ID.AsInteger := NewNum;
end;

procedure Tdata.TdetailcommandeBeforeInsert(DataSet: TDataSet);

begin
Incrementation(Tdetailcommande,TdetailcommandeDET_ID,'IDET_PK');
end;

procedure Tdata.TdetailcommandeNewRecord(DataSet: TDataSet);
begin
 Data.TdetailcommandeDET_ID.AsInteger := NewNum;
end;

procedure Tdata.TbusBeforeDelete(DataSet: TDataSet);
var cle :string;
begin
cle:=data.TbusBUS_ID.AsString;
if (data.Tanalyse.Locate('ana_bus_id',cle,[]) or data.Tentretien.Locate('ent_bus_id',cle,[]) or
      data.Tkm.Locate('km_bus_id',cle,[]) or data.Tcontrole.Locate('cont_busid',cle,[]) or
       data.Tdetailcommande.Locate('det_bus_id',cle,[]))
        then
            begin
            raise exception.Create('Erreur d''integrite');
            showmessage('erreurd''integrite') ;
            end
        else
                begin
                         if messagedlg('Etes vous  sur de vouloir supprimer cet enregistrement?',
                        mtconfirmation,[mbyes,mbno],0) = mrno
                        then
                        raise  exception.Create('Vous avez change d''avis');


                end;

end;

procedure Tdata.TentretienBeforeDelete(DataSet: TDataSet);
var cle:string;
begin
cle:=data.TentretienENT_ID.AsString;

if data.Toperation.Locate('op_ent_id',cle,[])
        then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Tentretien.Delete;
                end;
end;




procedure Tdata.TtypeopBeforeDelete(DataSet: TDataSet);
var cle:string;
begin
cle:=data.TtypeopTYOP_ID.AsString;
if (data.Tbusoperation.Locate('bop_tyopid',cle,[]) or data.Toperation.Locate('op_tyopid',cle,[]))
        then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Ttypeop.Delete;
                end;
end;






procedure Tdata.TcontroleBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tcontrole,TcontroleCONT_ID,'ICONT_PK');
end;

procedure Tdata.TcontroleNewRecord(DataSet: TDataSet);
begin
 Data.TcontroleCONT_ID.AsInteger := NewNum;
end;

procedure Tdata.TcpvilleNewRecord(DataSet: TDataSet);
begin
 Data.TcpvilleCP_ID.AsInteger := NewNum;
end;

procedure Tdata.TcpvilleBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tcpville,TcpvilleCP_id,'ICP_PK');
end;

procedure Tdata.TfournisseurBeforeInsert(DataSet: TDataSet);
begin
Incrementation(Tfournisseur,TfournisseurFOUR_ID,'IFOUR_PK');

end;

procedure Tdata.TfournisseurNewRecord(DataSet: TDataSet);
begin
 Data.TfournisseurFOUR_ID.AsInteger := NewNum;
end;

procedure Tdata.TheaderBeforeDelete(DataSet: TDataSet);
var cle:string;
begin
cle:=data.TheaderHEA_ID.AsString;
if (data.Tdetailcommande.Locate('det_hea_id',cle,[]))
        then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Theader.Delete;
                end;
end;



procedure Tdata.TproprietaireBeforeDelete(DataSet: TDataSet);
var cle :string;
begin
cle:=data.TproprietairePRO_ID.AsString;
if(data.Tbus.Locate('bus_pro_id',cle,[])or data.Theader.Locate('hea_proidl',cle,[])
   or data.Theader.Locate('hea_proidf',cle,[]) )
    then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Tproprietaire.Delete;
                end;
end;

procedure Tdata.TcpvilleBeforeDelete(DataSet: TDataSet);
var cle :string;
begin
cle:=data.TcpvilleCP_ID.AsString;
if(data.Tproprietaire.Locate('pro_cp_id',cle,[])or data.Tfournisseur.Locate('four_cp_id',cle,[]))
    then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Tcpville.Delete;
                end;
end;

procedure Tdata.TfournisseurBeforeDelete(DataSet: TDataSet);
var cle :string;
begin
cle:=data.TfournisseurFOUR_ID.AsString;
if(data.Tarticle.Locate('art_fourid',cle,[]))
    then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Tfournisseur.Delete;
                end;
end;

procedure Tdata.TarticleBeforeDelete(DataSet: TDataSet);
var cle :string;
begin
cle:=data.TarticleART_ID.AsString;
if(data.Tdetailcommande.Locate('det_art_id',cle,[]))
    then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Tarticle.Delete;
                end;
end;

procedure Tdata.TtypebusBeforeDelete(DataSet: TDataSet);
var cle:string;
begin

cle:=data.TtypebusTYBU_ID.AsString;
if (data.Tbusoperation.Locate('bop_tybuid',cle,[]) or data.Tbus.Locate('bus_tybuid',cle,[]))
        then  raise exception.Create('Erreur d''integrite')
        else
                begin
                        If MessageDlg('Etes vous  sur de vouloir supprimer cet enregistrement?',MtConfirmation,[MbYes,MbNo],0) = MrYes Then
                        Data.Ttypebus.Delete;


                end;

end;

end.


