CREATE TABLE IF NOT EXISTS local (
  nom VARCHAR(300) NOT NULL PRIMARY KEY,
  adresse VARCHAR(300),
  tele INT,
  fax INT
);
COMMIT;


CREATE TABLE IF NOT EXISTS profile (
  idprofile INT AUTO_INCREMENT PRIMARY KEY,
  nom VARCHAR(45) NOT NULL,
  prenom VARCHAR(45) NOT NULL,
  naissance DATE,
  sexe BOOLEAN,
  num_tele INT,
  mail VARCHAR(45),
  adresse VARCHAR(60),
  date_creation DATE,
  pays VARCHAR(45)
);
COMMIT;


CREATE TABLE IF NOT EXISTS authentification (
  idauhentification INT AUTO_INCREMENT PRIMARY KEY,
  login VARCHAR(45) NOT NULL,
  password VARCHAR(45) NOT NULL,
  type_compte VARCHAR(45) NOT NULL,
  question_secrete VARCHAR(50) NOT NULL,
  reponce_question VARCHAR(50) NOT NULL,
  etat_compte BOOLEAN NOT NULL DEFAULT TRUE,
  stock_negatif BOOLEAN NOT NULL DEFAULT FALSE,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS client (
  codeclient BIGINT AUTO_INCREMENT PRIMARY KEY,
  cin INT NOT NULL,
  nomcomplet VARCHAR(300),
  nom VARCHAR(300) NOT NULL,
  prenom VARCHAR(300) NOT NULL,
  naissance DATE NOT NULL,
  telefix BIGINT,
  telemobile BIGINT,
  adresse VARCHAR(400) NOT NULL,
  codepostale BIGINT,
  mail VARCHAR(300),
  sexe BOOLEAN NOT NULL,
  date_add TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS categorie (
  nomcategorie VARCHAR(45) NOT NULL PRIMARY KEY,
  unitemesure BOOLEAN NOT NULL
);
COMMIT;


CREATE TABLE IF NOT EXISTS produit (
  reference VARCHAR(300) NOT NULL PRIMARY KEY,
  designation VARCHAR(800) NOT NULL,
  nomcategorie_cate VARCHAR(300) NOT NULL,
  marque VARCHAR(300),
  garantie INT,
  prix_achat_ht FLOAT NOT NULL,
  tva_achat FLOAT NOT NULL,
  prix_achat_ttc FLOAT NOT NULL,
  marge FLOAT NOT NULL,
  prix_vente_ht FLOAT NOT NULL,
  tva_vente FLOAT NOT NULL,
  prix_vente_ttc FLOAT NOT NULL,
  description TEXT,
  qualite INT,
  poids VARCHAR(300),
  couleur VARCHAR(300),
  dimension VARCHAR(300),
  vitesse VARCHAR(300),
  puissance VARCHAR(300),
  capacite VARCHAR(300),
  datecreation DATE,
  FOREIGN KEY (nomcategorie_cate) REFERENCES categorie (nomcategorie)
  );
COMMIT;
  
  CREATE TABLE IF NOT EXISTS client (
  codeclient BIGINT AUTO_INCREMENT PRIMARY KEY,
  cin INT NOT NULL,
  nomcomplet VARCHAR(300),
  nom VARCHAR(300) NOT NULL,
  prenom VARCHAR(300) NOT NULL,
  naissance DATE NOT NULL,
  telefix BIGINT,
  telemobile BIGINT,
  adresse VARCHAR(400) NOT NULL,
  codepostale BIGINT,
  mail VARCHAR(300),
  sexe BOOLEAN NOT NULL,
  date_add TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;
  
CREATE TABLE IF NOT EXISTS categorie (
  nomcategorie VARCHAR(45) NOT NULL PRIMARY KEY,
  unitemesure BOOLEAN NOT NULL
);
COMMIT;


CREATE TABLE IF NOT EXISTS produit (
  reference VARCHAR(300) NOT NULL PRIMARY KEY,
  designation VARCHAR(800) NOT NULL,
  nomcategorie_cate VARCHAR(300) NOT NULL,
  marque VARCHAR(300),
  garantie INT,
  prix_achat_ht FLOAT NOT NULL,
  tva_achat FLOAT NOT NULL,
  prix_achat_ttc FLOAT NOT NULL,
  marge FLOAT NOT NULL,
  prix_vente_ht FLOAT NOT NULL,
  tva_vente FLOAT NOT NULL,
  prix_vente_ttc FLOAT NOT NULL,
  description TEXT,
  qualite INT,
  poids VARCHAR(300),
  couleur VARCHAR(300),
  dimension VARCHAR(300),
  vitesse VARCHAR(300),
  puissance VARCHAR(300),
  capacite VARCHAR(300),
  datecreation DATE,
  FOREIGN KEY (nomcategorie_cate) REFERENCES categorie (nomcategorie)
);
COMMIT;
 

CREATE TABLE IF NOT EXISTS cltentreprise (
  idcliententreprise BIGINT AUTO_INCREMENT PRIMARY KEY,
  nomentrepise VARCHAR(300) NOT NULL,
  gerant VARCHAR(300) NOT NULL,
  matricule_fiscale BIGINT,
  adresse VARCHAR(400),
  telefix BIGINT NOT NULL,
  telefix2 BIGINT,
  fax BIGINT,
  code_postal BIGINT,
  mail VARCHAR(400),
  date_add TIMESTAMP NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS cltcontact (
  idcltcontact BIGINT AUTO_INCREMENT PRIMARY KEY,
  nom VARCHAR(300) NOT NULL,
  prenom VARCHAR(300) NOT NULL,
  cin BIGINT,
  gsm1 BIGINT NOT NULL,
  gsm2 VARCHAR(300),
  mail VARCHAR(300),
  idcltentreprise BIGINT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (idcltentreprise) REFERENCES cltentreprise (idcliententreprise),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;

CREATE TABLE IF NOT EXISTS entre (
  id_entre BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date DATE NOT NULL,
  heur TIME NOT NULL,
  local_emetteur VARCHAR(45) NOT NULL,
  local_recepteur VARCHAR(45) NOT NULL,
  transporteur VARCHAR(45),
  FOREIGN KEY (local_emetteur) REFERENCES local (nom),
  FOREIGN KEY (local_recepteur) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS liste_produit_entre (
  id_bon_entre BIGINT NOT NULL,
  reference_produit VARCHAR(300) NOT NULL,
  quantite INT,
  tva INT,
  prix_vente_ttc FLOAT,
  designation VARCHAR(800),
  FOREIGN KEY (id_bon_entre) REFERENCES entre (id_entre),
  FOREIGN KEY (reference_produit) REFERENCES produit (reference)
);
COMMIT;

CREATE TABLE IF NOT EXISTS sortie (
  id_sortie BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date DATE NOT NULL,
  heur TIME NOT NULL,
  local_emetteur VARCHAR(45) NOT NULL,
  local_recepteur VARCHAR(45) NOT NULL,
  transporteur VARCHAR(45),
  FOREIGN KEY (local_emetteur) REFERENCES local (nom),
  FOREIGN KEY (local_recepteur) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS liste_produit_sortie (
  id_bon_sortie BIGINT AUTO_INCREMENT NOT NULL,
  reference_produit VARCHAR(300) NOT NULL,
  quantite INT,
  prix_vente_ttc FLOAT,
  tva INT,
  designation VARCHAR(800),
  PRIMARY KEY (id_bon_sortie, reference_produit),
  FOREIGN KEY (id_bon_sortie) REFERENCES sortie (id_sortie),
  FOREIGN KEY (reference_produit) REFERENCES produit (reference)
);
COMMIT;


CREATE TABLE IF NOT EXISTS cltpassageur (
  idcltpassageur BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  nom VARCHAR(300) NOT NULL,
  prenom VARCHAR(300) NOT NULL,
  adresse VARCHAR(300),
  sexe SMALLINT NOT NULL,
  tele BIGINT,
  gsm BIGINT,
  naissance DATE,
  date_add TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;

CREATE TABLE IF NOT EXISTS devisclt (
  iddevis BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_creation DATE NOT NULL,
  fin_validite DATE,
  typeclt SMALLINT NOT NULL,
  type_reglement SMALLINT NOT NULL DEFAULT 0,
  codeclient BIGINT,
  idcliententreprise BIGINT,
  idcltpassageur BIGINT,
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  remise FLOAT NOT NULL DEFAULT 0,
  idprofile INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (codeclient) REFERENCES client (codeclient),
  FOREIGN KEY (idcliententreprise) REFERENCES cltentreprise (idcliententreprise),
  FOREIGN KEY (idcltpassageur) REFERENCES cltpassageur (idcltpassageur),
  FOREIGN KEY (idprofile) REFERENCES profile (idprofile),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;

CREATE TABLE IF NOT EXISTS factureclt (
  idfactureclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  type_reglement SMALLINT NOT NULL,
  mode_paiement SMALLINT NOT NULL,
  date_facture DATE NOT NULL,
  typeclt SMALLINT NOT NULL,
  idcltentreprise BIGINT,
  idcltexiste BIGINT,
  idcltpassageur BIGINT,
  devise VARCHAR(10),
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  remise FLOAT NOT NULL DEFAULT 0,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  type_facilite SMALLINT,
  avance FLOAT DEFAULT 0,
  total_montant_traites FLOAT,
  nbr_traite INT,
  montant_interet FLOAT,
  taux_interet FLOAT,
  idcommande_commandeclt BIGINT NOT NULL,
  id_bon_livraisonclt BIGINT,
  profile_idprofile INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (profile_idprofile) REFERENCES profile (idprofile),
  FOREIGN KEY (idcltentreprise) REFERENCES cltentreprise (idcliententreprise),
  FOREIGN KEY (idcltexiste) REFERENCES client (codeclient),
  FOREIGN KEY (idcltpassageur) REFERENCES cltpassageur (idcltpassageur),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS commandeclt (
  idcommande BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_commande DATE NOT NULL,
  typeclt SMALLINT NOT NULL,
  type_reglement SMALLINT NOT NULL,
  codeclient BIGINT,
  idcliententreprise BIGINT,
  idcltpassageur BIGINT,
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  remise FLOAT NOT NULL DEFAULT 0,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  iddevis BIGINT,
  id_bon_livraisonclt BIGINT,
  id_factureclt BIGINT,
  local_nom VARCHAR(300) NOT NULL,
  idprofile_editeur INT NOT NULL,
  FOREIGN KEY (codeclient) REFERENCES client (codeclient),
  FOREIGN KEY (idcliententreprise) REFERENCES cltentreprise (idcliententreprise),
  FOREIGN KEY (idcltpassageur) REFERENCES cltpassageur (idcltpassageur),
  FOREIGN KEY (idprofile_editeur) REFERENCES profile (idprofile),
  FOREIGN KEY (iddevis) REFERENCES devisclt (iddevis),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS bon_livraisonclt (
  idbon_livraisonclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  type_client SMALLINT NOT NULL,
  id_clt_entreprise BIGINT,
  id_clt_passageur BIGINT,
  id_clt_existe BIGINT,
  heur_livraison TIME,
  date_livraison_prevu DATE NOT NULL,
  adresse_livraison VARCHAR(45) NOT NULL,
  transporteur VARCHAR(45),
  frais FLOAT,
  poids_produit INT,
  tele1 VARCHAR(45),
  tele2 VARCHAR(45),
  devise VARCHAR(10) NOT NULL,
  total_h_t_net FLOAT,
  total_tva FLOAT,
  remise FLOAT,
  net_a_payer FLOAT,
  id_commandeclt BIGINT NOT NULL,
  idprofile_editeur INT NOT NULL,
  date_creation DATE NOT NULL,
  local_nom VARCHAR(45) NOT NULL,
  FOREIGN KEY (idprofile_editeur) REFERENCES profile (idprofile),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_clt_entreprise) REFERENCES cltentreprise (idcliententreprise),
  FOREIGN KEY (id_clt_passageur) REFERENCES cltpassageur (idcltpassageur),
  FOREIGN KEY (id_clt_existe) REFERENCES client (codeclient)
);
COMMIT;

ALTER TABLE factureclt ADD CONSTRAINT CONSTRAINT_INDEX_fact_bon_clt_454 FOREIGN KEY (id_bon_livraisonclt) REFERENCES bon_livraisonclt (idbon_livraisonclt)ON DELETE CASCADE;
ALTER TABLE factureclt ADD CONSTRAINT CONSTRAINT_INDEX_fact_com_clt_547   FOREIGN KEY (idcommande_commandeclt) REFERENCES commandeclt (idcommande)ON DELETE CASCADE;
ALTER TABLE commandeclt ADD CONSTRAINT CONSTRAINT_INDEX_com_fact_clt_884 FOREIGN KEY (id_factureclt) REFERENCES factureclt (idfactureclt) ON DELETE CASCADE;
ALTER TABLE commandeclt ADD CONSTRAINT CONSTRAINT_INDEX_com_bon_liv_clt_2 FOREIGN KEY (id_bon_livraisonclt) REFERENCES bon_livraisonclt (idbon_livraisonclt) ON DELETE CASCADE;

CREATE TABLE IF NOT EXISTS fournisseur (
  id_fournisseur INT AUTO_INCREMENT PRIMARY KEY,
  nom VARCHAR(45) NOT NULL,
  adresse VARCHAR(45) NOT NULL,
  formes VARCHAR(45),
  tele1 BIGINT NOT NULL,
  tele2 BIGINT,
  fax BIGINT,
  mail VARCHAR(45),
  site_web VARCHAR(45),
  specialite VARCHAR(100)
);
COMMIT;


CREATE TABLE IF NOT EXISTS commande_frs (
  id_commande_frs INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_commande DATE NOT NULL,
  heur_commande TIME NOT NULL,
  mode_reglement VARCHAR(45) NOT NULL,
  devise VARCHAR(45) NOT NULL,
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  id_fournisseur INT NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS reglement_frs (
  id_reglement_frs BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_fournisseur INT NOT NULL,
  montant FLOAT NOT NULL,
  date_creation TIMESTAMP NOT NULL,
  type_paiement SMALLINT NOT NULL,
  mode_paiement SMALLINT,
  etat_reglement SMALLINT NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS facture_frs (
  id_facture_frs BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  numero VARCHAR(45) NOT NULL,
  date_creation TIMESTAMP NOT NULL,
  type_reglement SMALLINT,
  etat_reglement SMALLINT,
  devise VARCHAR(10) NOT NULL,
  total_h_t_net FLOAT NOT NULL,
  remise FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  bon_reception SMALLINT,
  id_fournisseur INT NOT NULL,
  local_reception VARCHAR(300) NOT NULL,
  id_commande_frs INT,
  id_profile INT NOT NULL,
  id_reglement_frs BIGINT,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur),
  FOREIGN KEY (local_reception) REFERENCES local (nom),
  FOREIGN KEY (id_commande_frs) REFERENCES commande_frs (id_commande_frs),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (id_reglement_frs) REFERENCES reglement_frs (id_reglement_frs)
);
COMMIT;

CREATE TABLE IF NOT EXISTS bonreception_frs (
  id_bonreception BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  numero VARCHAR(45) NOT NULL,
  date_reception TIMESTAMP NOT NULL,
  transporteur VARCHAR(45),
  net_a_payer FLOAT,
  id_fournisseur INT NOT NULL,
  local_reception VARCHAR(45) NOT NULL,
  id_profile INT NOT NULL,
  id_facture_frs BIGINT,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur),
  FOREIGN KEY (local_reception) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (id_facture_frs) REFERENCES facture_frs (id_facture_frs)
);
COMMIT;

CREATE TABLE IF NOT EXISTS cheque_clt (
  id_chequeclt INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  numero VARCHAR(300) NOT NULL,
  banque VARCHAR(300) NOT NULL,
  montant FLOAT NOT NULL,
  nom_complet VARCHAR(400),
  etat_cheque SMALLINT,
  date_paiement DATE NOT NULL,
  date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  id_factureclt BIGINT,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_factureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS carte_bancaire_clt (
  id_carte_bancaire BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  num_transation VARCHAR(150) NOT NULL,
  nom_banque VARCHAR(45),
  num_carte VARCHAR(100),
  nom_complet VARCHAR(45),
  date_transation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  montant FLOAT NOT NULL,
  id_factureclt BIGINT,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_factureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;

CREATE TABLE IF NOT EXISTS vente_sans_facture (
  id_vente_sans_facture BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_vente TIMESTAMP NOT NULL,
  paiement SMALLINT NOT NULL,
  nom_prenom_client VARCHAR(50),
  operation SMALLINT,
  date_operation TIMESTAMP,
  montant FLOAT NOT NULL,
  id_chequeclt INT,
  id_carte_bancaire BIGINT,
  id_profile INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (id_chequeclt) REFERENCES cheque_clt (id_chequeclt),
  FOREIGN KEY (id_carte_bancaire) REFERENCES carte_bancaire_clt (id_carte_bancaire),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS stock_detail (
  id_detail_stock BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  local_nom VARCHAR(45) NOT NULL,
  reference_produit VARCHAR(300) NOT NULL,
  quantite FLOAT NOT NULL,
  date_add TIMESTAMP NOT NULL,
  id_profile INT NOT NULL,
  id_bon_entre BIGINT,
  id_bon_sortie BIGINT,
  id_bon_livraison BIGINT,
  id_bon_reception BIGINT,
  id_facture_frs BIGINT,
  id_vente_sans_facture BIGINT,
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (reference_produit) REFERENCES produit (reference),
  FOREIGN KEY (id_bon_entre) REFERENCES entre (id_entre),
  FOREIGN KEY (id_bon_sortie) REFERENCES sortie (id_sortie),
  FOREIGN KEY (id_bon_livraison) REFERENCES bon_livraisonclt (idbon_livraisonclt),
  FOREIGN KEY (id_bon_reception) REFERENCES bonreception_frs (id_bonreception),
  FOREIGN KEY (id_facture_frs) REFERENCES facture_frs (id_facture_frs),
  FOREIGN KEY (id_vente_sans_facture) REFERENCES vente_sans_facture (id_vente_sans_facture),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;

CREATE TABLE IF NOT EXISTS liste_produit_commandeclt (
  id_liste_produit_commandeclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  commande_idcommande BIGINT NOT NULL,
  produit_reference VARCHAR(40) NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (commande_idcommande) REFERENCES commandeclt (idcommande),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS traite_clt (
  id_traite BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_traite DATE NOT NULL,
  montant_traite FLOAT NOT NULL,
  date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  id_factureclt BIGINT NOT NULL,
  date_paiement TIMESTAMP,
  montant_payer FLOAT,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_factureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS traite_paiement_espace_clt (
  id_traite_paiement_espace BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_traite BIGINT NOT NULL,
  date_payer TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  montant_payer FLOAT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_traite) REFERENCES traite_clt (id_traite),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS devis_frs (
  id_devis_frs INT NOT NULL PRIMARY KEY,
  id_fournisseur INT NOT NULL,
  date_creation DATE NOT NULL,
  devis VARCHAR(10) NOT NULL,
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  timbre_loi FLOAT NOT NULL,
  net_a_payer FLOAT NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS list_produit_devisclt (
  id_list_produit_devisclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  iddevis_devis BIGINT NOT NULL,
  reference_produit VARCHAR(300) NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  quantie INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (reference_produit) REFERENCES produit (reference),
  FOREIGN KEY (iddevis_devis) REFERENCES devisclt (iddevis),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS liste_produit_devis_frs (
  reference_produit VARCHAR(40) NOT NULL PRIMARY KEY,
  id_devis_frs INT NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  FOREIGN KEY (reference_produit) REFERENCES produit (reference),
  FOREIGN KEY (id_devis_frs) REFERENCES devis_frs (id_devis_frs)
);
COMMIT;


CREATE TABLE IF NOT EXISTS contact_frs (
  idcontactfrs INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  nom VARCHAR(45) NOT NULL,
  prenom VARCHAR(45) NOT NULL,
  tele1 BIGINT NOT NULL,
  tele2 BIGINT,
  email VARCHAR(45),
  id_fournisseur INT NOT NULL,
  FOREIGN KEY (id_fournisseur) REFERENCES fournisseur (id_fournisseur)
);
COMMIT;


CREATE TABLE IF NOT EXISTS list_prod_factclt (
  id_list_prod_factclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  idfactureclt_factureclt BIGINT NOT NULL,
  produit_reference VARCHAR(300) NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (idfactureclt_factureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS liste_prod_livraison (
  id_liste_prod_livraison BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_bon_livraison_clt BIGINT NOT NULL,
  produit_reference VARCHAR(40) NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  local_nom VARCHAR(45) NOT NULL,
  FOREIGN KEY (id_bon_livraison_clt) REFERENCES bon_livraisonclt (idbon_livraisonclt),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;

CREATE TABLE IF NOT EXISTS liste_prod_commande_frs (
  id_liste_prod_commande_frs INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_commande_frs INT NOT NULL,
  designation_produit VARCHAR(45) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  FOREIGN KEY (id_commande_frs) REFERENCES commande_frs (id_commande_frs)
);
COMMIT;

CREATE TABLE IF NOT EXISTS list_prod_reception_frs (
  id_achat BIGINT AUTO_INCREMENT NOT NULL,
  id_bonreception_frs BIGINT NOT NULL,
  reference VARCHAR(300) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  PRIMARY KEY (id_achat),
  FOREIGN KEY (id_bonreception_frs) REFERENCES bonreception_frs (id_bonreception),
  FOREIGN KEY (reference) REFERENCES produit (reference)
);
COMMIT;


CREATE TABLE IF NOT EXISTS list_prod_fact_frs (
  id_list_prod_fact_frs INT AUTO_INCREMENT NOT NULL,
  id_facture_frs BIGINT NOT NULL,
  reference VARCHAR(40) NOT NULL,
  designation_produit VARCHAR(100) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise INT,
  p_u_h_t_net FLOAT NOT NULL,
  tva INT NOT NULL,
  montant FLOAT NOT NULL,
  PRIMARY KEY (id_list_prod_fact_frs),
  FOREIGN KEY (id_facture_frs) REFERENCES facture_frs (id_facture_frs),
  FOREIGN KEY (reference) REFERENCES produit (reference)
);
COMMIT;

CREATE TABLE IF NOT EXISTS factclt_avoir (
  idavoir BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  date_creation VARCHAR(45) NOT NULL,
  type_avoir SMALLINT NOT NULL,
  devis VARCHAR(45) NOT NULL,
  total_h_t_net FLOAT NOT NULL,
  total_tva FLOAT NOT NULL,
  timbre_loi FLOAT NOT NULL,
  net_a_reduire FLOAT NOT NULL,
  factureclt_idfactureclt BIGINT NOT NULL,
  profile_idprofile INT NOT NULL,
  local_nom VARCHAR(45) NOT NULL,
  FOREIGN KEY (factureclt_idfactureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (profile_idprofile) REFERENCES profile (idprofile),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS traite_paiement_carte_clt (
  id_traite_paiement_carte BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_traite BIGINT NOT NULL,
  montant_payer FLOAT NOT NULL,
  numero_carte VARCHAR(300) NOT NULL,
  banque VARCHAR(300) NOT NULL,
  date_payer TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_traite) REFERENCES traite_clt (id_traite),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;


CREATE TABLE IF NOT EXISTS traite_paiement_cheque_clt (
  id_traite_paiement_cheque BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  id_traite BIGINT NOT NULL,
  montant_payer FLOAT NOT NULL,
  numero VARCHAR(300) NOT NULL,
  banque VARCHAR(300) NOT NULL,
  nom_complet VARCHAR(400),
  date_payer DATE NOT NULL,
  date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  etat_cheque SMALLINT,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  FOREIGN KEY (id_traite) REFERENCES traite_clt (id_traite),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile)
);
COMMIT;

CREATE TABLE IF NOT EXISTS historique_cheque_clt (
  id_historique_cheque_clt BIGINT AUTO_INCREMENT NOT NULL,
  etat_cheque SMALLINT NOT NULL,
  date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  id_chequeclt INT,
  id_traite_paiement_cheque BIGINT,
  local_nom VARCHAR(300) NOT NULL,
  id_profile INT NOT NULL,
  PRIMARY KEY (id_historique_cheque_clt),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (id_chequeclt) REFERENCES cheque_clt (id_chequeclt),
  FOREIGN KEY (id_traite_paiement_cheque) REFERENCES traite_paiement_cheque_clt (id_traite_paiement_cheque)
);
COMMIT;


CREATE TABLE IF NOT EXISTS caisseclt (
  id_caisseclt INT AUTO_INCREMENT NOT NULL,
  date_mouvement TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  type_mouvement SMALLINT,
  id_factureclt BIGINT,
  id_avoir_factclt BIGINT,
  id_vente_sans_facture BIGINT,
  montant FLOAT NOT NULL,
  id_traite_paiement_espace BIGINT,
  id_traite_paiement_carte BIGINT,
  id_traite_paiement_cheque BIGINT,
  id_historique_cheque_clt BIGINT,
  id_profile INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  PRIMARY KEY (id_caisseclt),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (id_factureclt) REFERENCES factureclt (idfactureclt),
  FOREIGN KEY (id_avoir_factclt) REFERENCES factclt_avoir (idavoir),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_traite_paiement_espace) REFERENCES traite_paiement_espace_clt (id_traite_paiement_espace),
  FOREIGN KEY (id_traite_paiement_carte) REFERENCES traite_paiement_carte_clt (id_traite_paiement_carte),
  FOREIGN KEY (id_traite_paiement_cheque) REFERENCES traite_paiement_cheque_clt (id_traite_paiement_cheque),
  FOREIGN KEY (id_historique_cheque_clt) REFERENCES historique_cheque_clt (id_historique_cheque_clt),
  FOREIGN KEY (id_vente_sans_facture) REFERENCES vente_sans_facture (id_vente_sans_facture)
);
COMMIT;

CREATE TABLE IF NOT EXISTS bond_retourclt (
  idbonderetourclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  factclt_avoir_idavoir BIGINT NOT NULL,
  heur_retour TIME NOT NULL,
  date_retour DATE NOT NULL,
  transpoteur VARCHAR(45),
  quantite INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (factclt_avoir_idavoir) REFERENCES factclt_avoir (idavoir),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS list_prod_avoir_factclt (
  id_list_prod_avoir_factclt BIGINT AUTO_INCREMENT NOT NULL PRIMARY KEY,
  idfactclt_avoir_prod BIGINT NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise FLOAT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva FLOAT NOT NULL,
  montant FLOAT NOT NULL,
  designation VARCHAR(400),
  produit_reference VARCHAR(300),
  designation_produit VARCHAR(400),
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (idfactclt_avoir_prod) REFERENCES factclt_avoir (idavoir),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;

CREATE TABLE IF NOT EXISTS liste_prod_retour (
  id_retour BIGINT NOT NULL PRIMARY KEY,
  id_bonde_retourclt BIGINT NOT NULL,
  quantite FLOAT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise FLOAT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva FLOAT NOT NULL,
  montant FLOAT NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  FOREIGN KEY (id_bonde_retourclt) REFERENCES bond_retourclt (idbonderetourclt),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS detail_retour (
  id_retour BIGINT NOT NULL,
  id_detail BIGINT NOT NULL,
  FOREIGN KEY (id_retour) REFERENCES liste_prod_retour (id_retour),
  FOREIGN KEY (id_detail) REFERENCES stock_detail (id_detail_stock)
);
COMMIT;


CREATE TABLE IF NOT EXISTS stock (
  local_nom VARCHAR(300) NOT NULL,
  produit_reference VARCHAR(300) NOT NULL,
  quantite INT NOT NULL,
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference)
);
COMMIT;


CREATE TABLE IF NOT EXISTS carte_electronique_frs (
  id_carte_electronique_frs BIGINT AUTO_INCREMENT NOT NULL,
  id_reglement_frs BIGINT NOT NULL,
  numero_transaction VARCHAR(45) NOT NULL,
  numero_carte BIGINT,
  banque VARCHAR(45),
  date_transaction TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  montant FLOAT NOT NULL,
  PRIMARY KEY (id_carte_electronique_frs),
  FOREIGN KEY (id_reglement_frs) REFERENCES reglement_frs (id_reglement_frs)
);
COMMIT;


CREATE TABLE IF NOT EXISTS cheque_frs (
  id_cheque_frs BIGINT AUTO_INCREMENT NOT NULL,
  id_reglement_frs BIGINT NOT NULL,
  numero INT NOT NULL,
  banque VARCHAR(45) NOT NULL,
  montant FLOAT NOT NULL,
  nom_complet VARCHAR(100),
  date_paiement DATE NOT NULL,
  date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id_cheque_frs),
  FOREIGN KEY (id_reglement_frs) REFERENCES reglement_frs (id_reglement_frs)
);
COMMIT;


CREATE TABLE IF NOT EXISTS traite_frs (
  id_traite_frs BIGINT AUTO_INCREMENT NOT NULL,
  id_reglement_frs BIGINT NOT NULL,
  montant FLOAT NOT NULL,
  date_paiement DATE NOT NULL,
  date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (id_traite_frs),
  FOREIGN KEY (id_reglement_frs) REFERENCES reglement_frs (id_reglement_frs)
);
COMMIT;

CREATE TABLE IF NOT EXISTS frais (
  id_frais BIGINT AUTO_INCREMENT NOT NULL,
  montant FLOAT NOT NULL,
  object VARCHAR(500) NOT NULL,
  context TEXT,
  personne VARCHAR(200),
  mode_paiement SMALLINT NOT NULL,
  numero VARCHAR(45),
  banque VARCHAR(45),
  date_cheque DATE,
  id_profile INT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  date_creation TIMESTAMP NOT NULL,
  PRIMARY KEY (id_frais),
  FOREIGN KEY (id_profile) REFERENCES profile (idprofile),
  FOREIGN KEY (local_nom) REFERENCES local (nom)
);
COMMIT;


CREATE TABLE IF NOT EXISTS liste_prod_vente_sans_facture (
  id_liste_prod_vente_sans_facture BIGINT AUTO_INCREMENT NOT NULL,
  id_vente_sans_facture BIGINT NOT NULL,
  produit_reference VARCHAR(300) NOT NULL,
  designation_produit VARCHAR(800) NOT NULL,
  quantite INT NOT NULL,
  p_u_h_t FLOAT NOT NULL,
  remise FLOAT NOT NULL,
  p_u_h_t_net FLOAT NOT NULL,
  tva FLOAT NOT NULL,
  montant FLOAT NOT NULL,
  local_nom VARCHAR(300) NOT NULL,
  PRIMARY KEY (id_liste_prod_vente_sans_facture),
  FOREIGN KEY (produit_reference) REFERENCES produit (reference),
  FOREIGN KEY (local_nom) REFERENCES local (nom),
  FOREIGN KEY (id_vente_sans_facture) REFERENCES vente_sans_facture (id_vente_sans_facture)
);
COMMIT;
