SET SQL DIALECT 3;
/* Generators definitions */

CREATE GENERATOR ARCHIVES_ID_ARCHIVE_GEN ;
CREATE GENERATOR ARTICLES_ID_ART_GEN ;
CREATE GENERATOR BC_F_ID_BC_F_GEN ;
CREATE GENERATOR CAMION_ID_CAMION_GEN ;
CREATE GENERATOR CATALOGUE_ID_CAT_GEN ;
CREATE GENERATOR CHAUFFEURS_ID_CHAUFFEUR_GEN ;
CREATE GENERATOR CLIENTS_ID_CLIENT_GEN ;
CREATE GENERATOR CODE_TAP_ID_TAP_GEN ;
CREATE GENERATOR CODE_TVA_ID_TVA_GEN ;
CREATE GENERATOR COMPTES_TRESORIE_ID_CPTE_GEN ;
CREATE GENERATOR DEPENSES_ID_DEP_GEN ;
CREATE GENERATOR DESTINATION_ID_DESTINATION_GEN ;
CREATE GENERATOR DEVISES_ID_DEVISE_GEN ;
CREATE GENERATOR EMPLACEMENTS_ID_EMPL_GEN ;
CREATE GENERATOR ENTETE_ENTREES_ID_ENT_ENT_GEN ;
CREATE GENERATOR ENTETE_SORTIES_ID_ENT_SORT_GEN ;
CREATE GENERATOR ENTETE_TRANSFERT_ID_ENTETE_GEN ;
CREATE GENERATOR ENTET_BC_F_ID_ENT_BD_F_GEN ;
CREATE GENERATOR ENTREES_ID_ENTREE_GEN ;
CREATE GENERATOR FACT_ANT_ID_FACT_GEN ;
CREATE GENERATOR FACT_DD_JOINTE_ID_FACT_GEN ;
CREATE GENERATOR FAMILLES_CHARGES_ID_FAM_GEN ;
CREATE GENERATOR FAMILLES_ID_FAMILLE_GEN ;
CREATE GENERATOR FAM_RECETTES_ID_FAM_REC_GEN ;
CREATE GENERATOR FOURNISSEURS_ID_FOURN_GEN ;
CREATE GENERATOR FRAUDEURS_ID_FRAUD_GEN ;
CREATE GENERATOR G50_ID_G50_GEN ;
CREATE GENERATOR INVENTAIRE_ID_INVENTAIRE_GEN ;
CREATE GENERATOR LIGNES_PROFORMA_ID_L_PROF_GEN ;
CREATE GENERATOR LIGNES_TRANSFERT_ID_LIGNE_GEN ;
CREATE GENERATOR LST_CHGES_ID_LISTE_CHARGE_GEN ;
CREATE GENERATOR MAGAZINS_ID_MAG_GEN ;
CREATE GENERATOR MARQUES_ID_MARQUE_GEN ;
CREATE GENERATOR NOMENCLATURES_ID_NOM_GEN ;
CREATE GENERATOR NUM_SERIES_ID_NUM_GEN ;
CREATE GENERATOR PARAMETRES_ID_PARAM_GEN ;
CREATE GENERATOR PROFORMA_ID_PROF_GEN ;
CREATE GENERATOR RECETTES_ID_REC_GEN ;
CREATE GENERATOR REGLEMENTS_ID_REGLEMENT_GEN ;
CREATE GENERATOR REGL_TIERS_ID_REGL_GEN ;
CREATE GENERATOR RENUM_CHAUFFEURS_ID_RENUM_GEN ;
CREATE GENERATOR SORTIES_ID_SORTIE_GEN ;
CREATE GENERATOR TAUX_EXTRACTION_ID_TAUX_GEN ;
CREATE GENERATOR TYPE_AUTRES_ENTREES_ID_TYPE_GEN ;
CREATE GENERATOR TYPE_AUTRES_SORTIES_ID_TYPE_GEN ;
CREATE GENERATOR UNITES_ID_UNITE_GEN ;
CREATE GENERATOR VIR_FONDS_ID_VIR_GEN ;
CREATE GENERATOR ZONES_ID_ZONE_GEN ;
CREATE GENERATOR CONSIGNATIONS_ID_CONSIGNE_GEN;
CREATE GENERATOR LIGNES_CONSIGNATION_ID_LC_GEN;
CREATE GENERATOR NORMES_ID_NORME_GEN;
CREATE GENERATOR CAT_PROD_ID_CAT_PROD_GEN;

/* Exceptions definitions */

CREATE EXCEPTION COMPTE_TRESORERIE_ACTIF 'Ce compte possede des transactions commerciales et ne peut pas être supprimé.';

CREATE EXCEPTION PRODUIT_ACTIF 'Ce produit figure dans des transactions et ne peut pas être supprimé.';

CREATE EXCEPTION RUPTURE_STOCK 'Opération  annulée  cause rupture de stock';

CREATE EXCEPTION STOCK_INSUFISANT 'Opération sur stock annulée quantité insufisante';

CREATE EXCEPTION TIER_ACTIF 'Cet opérateur possede des transactions commerciales et ne peut pas supprimé.';



/* Tables definitions */

CREATE TABLE NORMES (
  ID_NORME INTEGER NOT NULL,
  NORME VARCHAR(32));

CREATE TABLE CONSIGNATIONS (
  ID_CONSIGNE INTEGER NOT NULL,
  ID_ARTICLE INTEGER NOT NULL,
  ID_ART_CONS INTEGER NOT NULL,
  QUANTITE NUMERIC(18, 2));

CREATE TABLE LIGNES_CONSIGNATION (
  ID_LC INTEGER NOT NULL,
  ID_ENTET INTEGER,
  ID_LIGNES INTEGER,
  ID_ARTICLE INTEGER NOT NULL,
  QUANTITE NUMERIC(18, 6),
  PU NUMERIC(18, 2),
  TAUX_TVA NUMERIC(18, 2),
  HT NUMERIC(18, 2),
  TVA NUMERIC(18, 2),
  TTC NUMERIC(18, 2),
  SENS SMALLINT,
  NUM_PIECE VARCHAR(12),
  DATE_PIECE DATE,
  ID_TIERS INTEGER);

/* Structure for the `CAT_PROD` table :  */

CREATE TABLE CAT_PROD (
  ID_CAT_PROD INTEGER NOT NULL,
  LIBELLE VARCHAR(64),
  ANC_ID_CAT_PRO INTEGER,
  ID_FAM_PROD INTEGER DEFAULT 1 NOT NULL);

CREATE TABLE ARCHIVES (
    ID_ARCHIVE INTEGER NOT NULL,
    ID_PIECE INTEGER NOT NULL,
    ID_CAT INTEGER NOT NULL);

CREATE TABLE ARTICLES (
    ID_ART INTEGER NOT NULL,
    REF VARCHAR (22) CHARACTER SET NONE NOT NULL COLLATE NONE,
    CPTE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_STOCK VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_CONSOM VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_FAMILLE INTEGER,
    DESIGNATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ID_UNITE INTEGER,
    GESTION_STOCK SMALLINT,
    BLOCKER SMALLINT,
    PEREMPTION SMALLINT,
    BAR_CODE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    MATIERE SMALLINT,
    TAUX_M1 NUMERIC (18, 2),
    TAUX_M2 NUMERIC (18, 2),
    TAUX_M3 NUMERIC (18, 2),
    MARGE1 NUMERIC (18, 2),
    MARGE2 NUMERIC (18, 2),
    MARGE3 NUMERIC (18, 2),
    TAUX_TVA NUMERIC (18, 2),
    PRIX_V1 NUMERIC (18, 2),
    PRIX_V2 NUMERIC (18, 2),
    PRIX_V3 NUMERIC (18, 2),
    CPTE_VENTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    IMPORTATION SMALLINT,
    STOCK_MIN NUMERIC (18, 2),
    CONDITIONNEMENT NUMERIC (18, 6),
    ID_MARQUE INTEGER,
    DERNIER_P_ACHAT NUMERIC (18, 2),
    ID_DEFAULT_EMPLACEMENT INTEGER,
    GESTION_EMPLACEMENT SMALLINT,
    AUTRE_TAXE NUMERIC (18, 2),
    POIDS_U NUMERIC (18, 2),
    CBM_U NUMERIC (18, 2),
    QUALITE VARCHAR (35) CHARACTER SET NONE COLLATE NONE,
    ABR VARCHAR (7) CHARACTER SET NONE COLLATE NONE,
    POIDS NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    CODE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    VOLUME NUMERIC (18, 2),
    ALCOOL NUMERIC (18, 2),
    ID_CODE_TVA INTEGER,
    CODE_ORIGINE VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    CATE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    PALETTES NUMERIC (18, 6),
    P_U_F NUMERIC(18, 2),
    TX_TVA_F NUMERIC(18, 2),
    QTE_RECETTE NUMERIC(18, 6),
    ID_CODE_TAP INTEGER,
    MIN_ROT_CREANCES  SMALLINT,
    MID_ROT_CREANCES  SMALLINT,
    MAX_ROT_CREANCES  SMALLINT,
    DESIGNATION_AR          VARCHAR(128),
    STATISTIQUE SMALLINT,
    PROD_POIDS  SMALLINT,
    CHARGE_U_PROD NUMERIC(18, 2));

CREATE TABLE BC_F (
    ID_BC_F INTEGER NOT NULL,
    ID_ENTET INTEGER,
    ID_FOUR INTEGER,
    ID_ART INTEGER,
    ID_FAM INTEGER,
    ID_UNITE INTEGER,
    P_U NUMERIC (18, 2),
    TAUX_TVA NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    DATE_BC DATE,
    NUM_BC INTEGER,
    P_U_6 DOUBLE PRECISION,
    DESIGNATION VARCHAR(128));

CREATE TABLE CAMION (
    ID_CAMION INTEGER NOT NULL,
    NOM VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    MATRICULE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ID_FOURNISSEUR INTEGER,
    TARE NUMERIC(18, 2),
    DATE_TARE DATE);

CREATE TABLE CATALOGUE (
    ID_CAT INTEGER NOT NULL,
    ID_TIER INTEGER,
    ID_ART INTEGER,
    PRIX NUMERIC (18, 2));

CREATE TABLE CHAUFFEURS (
    ID_CHAUFFEUR INTEGER NOT NULL,
    NOM VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    SOLDE NUMERIC (18, 2));

CREATE TABLE CLIENTS (
    ID_CLIENT INTEGER NOT NULL,
    CODE VARCHAR (14) CHARACTER SET NONE NOT NULL COLLATE NONE,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DENOMINATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ACTIVITE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE1 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE2 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE3 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    TEL VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    FAX VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    MOBILE VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    EMAIL VARCHAR (64) CHARACTER SET NONE COLLATE NONE,
    URL VARCHAR (64) CHARACTER SET NONE COLLATE NONE,
    RC_AGR VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    REF_RC VARCHAR (27) CHARACTER SET NONE COLLATE NONE,
    DATE_RC DATE,
    AI VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    I_F VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    NIS VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    DB VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    RIB VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    CODE_POSTE VARCHAR (7) CHARACTER SET NONE COLLATE NONE,
    COMMUNE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DAIRA VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    WILAYA VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    MIXTE SMALLINT,
    CLIENT SMALLINT,
    LOCAL SMALLINT,
    SOLDE_INI NUMERIC (18, 2),
    TYPE_TIER SMALLINT,
    ID_ZONE INTEGER,
    SURFACE NUMERIC (18, 6),
    PRIX_QUOTEPART NUMERIC (18, 2),
    LOT VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    NBRE_LOTS SMALLINT,
    TRANCHE VARCHAR (12) CHARACTER SET NONE COLLATE NONE,
    QUOTEPART SMALLINT,
    CAUTION NUMERIC (18, 2),
    DATE_FIN_CAUTION DATE,
    CONTRAT VARCHAR (27) CHARACTER SET NONE COLLATE NONE,
    PARTICULIER SMALLINT,
    SERVICE_GESTION VARCHAR(64),
    MIN_ROT_CREANCES SMALLINT,
    MID_ROT_CREANCES SMALLINT,
    MAX_ROT_CREANCES SMALLINT,
    BLOCKE SMALLINT,
    SOLDE_INI_CLIENT NUMERIC (18, 2),
    SOLDE_INI_FOURN NUMERIC (18, 2),
    DENOMINATION_AR   VARCHAR(128),
    ACTIVITE_AR       VARCHAR(128),
    ADRESSE1_AR       VARCHAR(128),
    ADRESSE2_AR       VARCHAR(128),
    NIN VARCHAR(20),
    ADRESSE3_AR       VARCHAR(128),
    PAYS  varchar(56));

CREATE TABLE CODE_TAP (
    ID_TAP INTEGER NOT NULL,
    CODE VARCHAR (7) CHARACTER SET NONE NOT NULL COLLATE NONE,
    LIBELLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    TAUX NUMERIC (18, 2) NOT NULL,
    TAUX_REFACT NUMERIC (18, 2) NOT NULL,
    ID_CPTE INTEGER,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE CODE_TVA (
    ID_TVA INTEGER NOT NULL,
    CODE VARCHAR (7) CHARACTER SET NONE NOT NULL COLLATE NONE,
    LIBELLES VARCHAR (130) CHARACTER SET NONE NOT NULL COLLATE NONE,
    TAUX NUMERIC (18, 2) NOT NULL,
    ID_CPTE INTEGER,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE COMMUNES (
    ID_COM INTEGER NOT NULL,
    CODE_COM VARCHAR (6) CHARACTER SET NONE NOT NULL COLLATE NONE,
    COMMUNE VARCHAR (130) CHARACTER SET NONE NOT NULL COLLATE NONE);

CREATE TABLE COMPTES_TRESORIE (
    ID_CPTE INTEGER NOT NULL,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    NOM_CPTE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    NUM_CPTE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    SOLDE_INI NUMERIC (18, 2),
    REG_V_C SMALLINT);

CREATE TABLE DEPENSES (
    ID_DEP INTEGER NOT NULL,
    DATE_DEP DATE,
    NUM_DEP INTEGER,
    ID_CPTE SMALLINT,
    ID_CHARGE INTEGER,
    LIB_DEP VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    TRANSFERT SMALLINT,
    MONTANT NUMERIC (18, 2),
    LOCKED SMALLINT,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    NUM_CHEQUE VARCHAR (9) CHARACTER SET NONE COLLATE NONE,
    ORDRE_DE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    BARRE SMALLINT,
    ID_TIERS INTEGER,
    ETAT VARCHAR (18) CHARACTER SET NONE COLLATE NONE,
    ID_CAMION INTEGER,
    ID_CHAUFFEUR INTEGER,
    CPTE_CHARGE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    MONTANT_HT NUMERIC (18, 2),
    MONTANT_TVA NUMERIC (18, 2),
    MONTANT_TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    DATE_FACT DATE,
    MOIS SMALLINT,
    ANNEE SMALLINT,
    NUM_FACT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    COMPTABILISE_ENGAGEMENT SMALLINT,
    COMPTABILISE_REGLEMENT SMALLINT,
    IS_INVESTISSEMENT SMALLINT,
    VEROUILLE SMALLINT);

CREATE TABLE DESTINATION (
    ID_DESTINATION INTEGER NOT NULL,
    DEPRAT VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    ARRIVE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    PU_TR NUMERIC (18, 4));

CREATE TABLE DEVISES (
    ID_DEVISE INTEGER NOT NULL,
    DEVISE VARCHAR (22) CHARACTER SET NONE NOT NULL COLLATE NONE);

CREATE TABLE EMPLACEMENTS (
    ID_EMPL INTEGER NOT NULL,
    EMPLACEMENT VARCHAR (130) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE ENTETE_ENTREES (
    ID_ENT_ENT INTEGER NOT NULL,
    NUM_DOS VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DATE_DOS DATE,
    NUM_D10 VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DATE_D10 DATE,
    NUM_LC VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DATE_LC DATE,
    NUM_FACT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DATE_FACT DATE,
    ID_FOURN INTEGER NOT NULL,
    ID_DEVISES INTEGER NOT NULL,
    NATURE_ENTREE SMALLINT,
    ID_REGLEMENT INTEGER NOT NULL,
    STOCKER SMALLINT,
    AJOUTER_DETTES SMALLINT,
    CALCUL_TIMBRE SMALLINT,
    TOTAL_D10_DA NUMERIC (18, 2),
    TOTAL_AUTRES_TTC NUMERIC (18, 2),
    TOTAL_AUTRES_TVA NUMERIC (18, 2),
    TOTAL_AUTRES_HT NUMERIC (18, 2),
    TOTAL_TRANSPORT_TTC NUMERIC (18, 2),
    TOTAL_TRANSPORT_TVA NUMERIC (18, 2),
    TOTAL_TRANSPORT_HT NUMERIC (18, 2),
    TOTAL_TRANSITE_TTC NUMERIC (18, 2),
    TOTAL_TRANSITE_TVA NUMERIC (18, 2),
    TOTAL_TRANSITE_HT NUMERIC (18, 2),
    TOTAL_DD_TTC NUMERIC (18, 2),
    TOTAL_DD_TVA NUMERIC (18, 2),
    TOTAL_DD_HT NUMERIC (18, 2),
    NET_A_PAYER NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    TOTAL_TTC NUMERIC (18, 2),
    TOTAL_TVA NUMERIC (18, 2),
    TOTAL_HT NUMERIC (18, 2),
    CV_DINARS NUMERIC (18, 2),
    FRET_DA NUMERIC (18, 2),
    TAUX_CHANGE NUMERIC (18, 6),
    FRET NUMERIC (18, 2),
    MONTANT_DEVISE NUMERIC (18, 2),
    CV_D10 NUMERIC (18, 2),
    AUTRES_CHARGES NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    MOIS SMALLINT DEFAULT 1,
    ANNEE SMALLINT DEFAULT 1,
    VALEUR_STOCK NUMERIC (18, 2),
    ECART NUMERIC (18, 2),
    LOCKED SMALLINT DEFAULT 1,
    FONCTIONNEMENT SMALLINT DEFAULT 1,
    ID_TYPE_AUTRES_ENTREES INTEGER,
    DATE_SAISIE DATE,
    TOTAL_HT_AVANT_REMISE NUMERIC (18, 2),
    TAUX_REMISE NUMERIC (18, 2),
    ID_MAGAZIN INTEGER,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    NUM_BC VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    DATE_BC DATE,
    DENOMINATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    INFO_REGLEMENT VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    AUTRES_TAXES NUMERIC (18, 2),
    CHAUFFEUR VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    FACTURE SMALLINT,
    MATRICULE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    REGLEE SMALLINT,
    TYPE_PRODUCTION SMALLINT,
    DATE_ECHEANCE DATE,
    ID_DEPART INTEGER,
    PU_TR NUMERIC(18, 4),
    ID_CHAUFFEUR INTEGER,
    ID_MATRICULE INTEGER,
    VEROUILLE SMALLINT,
    PRESTATIONS VARCHAR(254),
    OBJET_PRESTATION VARCHAR(64),
    ID_FAM_ACHAT INTEGER,
CPTE_PRESTATION VARCHAR(12),
INVESTISSMENT SMALLINT,
PRECOMPTE_DEPOT NUMERIC(18,2),
TRANSPORT_LOCAL NUMERIC(18,2),
IS_SOLDE_TRANSP SMALLINT,
MONTANT_TR NUMERIC(18,2),
QTE_TR NUMERIC(18,6),
ID_FOUR_TR NUMERIC(18,2),
ID_NORME INTEGER);

CREATE TABLE ENTETE_SORTIES (
    ID_ENT_SORT INTEGER NOT NULL,
    NATURE_SORTIE SMALLINT,
    NUM_PIECE VARCHAR (14) CHARACTER SET NONE NOT NULL COLLATE NONE,
    DATE_PIECE DATE NOT NULL,
    ID_CLIENT INTEGER NOT NULL,
    REFACTION SMALLINT,
    DESTOCKER SMALLINT,
    AJOUTER_CREANCE SMALLINT,
    TOTAL_HT NUMERIC (18, 2),
    TOTAL_TVA NUMERIC (18, 2),
    TOTAL_TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    AUTRE_TAXE1 NUMERIC (18, 2),
    AUTRE_TAXE2 NUMERIC (18, 2),
    AUTRE_TAXE3 NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    TOTAL_FACTURE NUMERIC (18, 2),
    ID_REGLEMENT INTEGER,
    CALCUL_TIMBRE INTEGER,
    F_POSE SMALLINT,
    ID_FP INTEGER,
    REGLEE SMALLINT,
    LOCKED SMALLINT,
    CESSION SMALLINT,
    TAUX_REMISE NUMERIC (18, 2),
    TOTAL_HT_AVANT_REMISE NUMERIC (18, 2),
    ID_AUTRES_SORTIES INTEGER,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    CHAUFFEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    CAMION VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    MATRICULE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    TOTAL_CONSOMMATION NUMERIC (18, 2),
    ENCAISSE NUMERIC (18, 2),
    ECART NUMERIC (18, 2),
    INFO_REGLEMENT VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    DESTINATION VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    DATE_BC DATE,
    NUM_BC VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_MAGAZIN INTEGER,
    DENOMINATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    IMP SMALLINT,
    OBS VARCHAR (254) CHARACTER SET NONE COLLATE NONE,
    AUTRES_TAXES NUMERIC (18, 2),
    FRANCHISE NUMERIC (18, 2),
    RAISON_SOCIALE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE_RAISON_SOCIALE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ACTIVITE_RAISON_SOCIALE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    RC_RAISON_SOCIALE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    AI_RAISON_SOCIALE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    IF_RAISON_SOCIALE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    NIS_RAISON_SOCIALE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    CAPITAL NUMERIC (18, 2),
    REF_FRANCHISE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    BANQUE_CLIENT VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DATE_ENCAISSEMENT DATE,
    NOTRE_BANQUE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DATE_CHEQUE DATE,
    ID_DESTINATION INTEGER,
    PU_TR NUMERIC (18, 2),
    QTE_TR NUMERIC (18, 6),
    MONTANT_TR NUMERIC (18, 2),
    ID_FOUR_TR INTEGER,
    CONTRAT VARCHAR (32) CHARACTER SET NONE COLLATE NONE,
    POIDS NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    ID_FACTURE INTEGER,
    REPRESENTANT VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    CLASSE SMALLINT,
    FACTURE SMALLINT,
    REF VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DATE_REF DATE,
    ID_CHAUF1 INTEGER,
    ID_CHAUF2 INTEGER,
    ID_CAMION INTEGER,
    FRAIS_CHAUF1 NUMERIC (18, 2),
    FRAIS_CHAUF2 NUMERIC (18, 2),
    CHAUFFEUR2 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    DATE_RECEPTION DATE,
    DATE_ECHEANCE DATE,
    ID_CODE_COMMUNE INTEGER,
    IS_FRANCHISE SMALLINT,
    ID_CODE_TAP INTEGER,
    DATETIME_PIECE TIMESTAMP,
    SOLDE_ANT NUMERIC (18, 2),
    SOLDE_FIN NUMERIC (18, 2),
    TARE NUMERIC(18, 2),
    DATE_TARE DATE,
    BRUTE NUMERIC(18, 2),
    ID_CLIENT_DEST INTEGER,
    VEROUILLE SMALLINT,
    NUM_CONTRAT VARCHAR(32),
    DATE_CONTRAT DATE,
    DEPENSES NUMERIC(18,2));

CREATE TABLE ENTETE_TRANSFERT (
    ID_ENTETE INTEGER NOT NULL,
    NUM_PIECE INTEGER,
    DATE_PIECE DATE,
    ID_MAGAZIN_1 INTEGER,
    ID_MAGAZIN_2 INTEGER,
    TOTAL_HT NUMERIC (18, 2),
    LOCKED SMALLINT,
    POIDS NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    SORTIE_STOCK SMALLINT,
    ENTREE_STOCK SMALLINT,
    VEROUILLE SMALLINT);

CREATE TABLE ENTET_BC_F (
    ID_ENT_BD_F INTEGER NOT NULL,
    NUM_BC INTEGER,
    DATE_BC DATE,
    ID_TIER INTEGER,
    TOTAL_HT NUMERIC (18, 2),
    TOTAL_TVA NUMERIC (18, 2),
    TOTAL_TTC NUMERIC (18, 2),
    OBS BLOB sub_type 1 segment size 80,
    LOCKED SMALLINT,
    DATE_PROF DATE,
    NUM_PROF VARCHAR(12),
    ID_REGLEMENT INTEGER,
    TIMBRE NUMERIC(18, 2),
    TOTAL_BCF NUMERIC(18, 2));

CREATE TABLE ENTREES (
    ID_ENTREE INTEGER NOT NULL,
    ID_ENTETE INTEGER NOT NULL,
    ID_FOUR INTEGER NOT NULL,
    NATURE_ENTREE SMALLINT,
    DATE_ENTREE DATE NOT NULL,
    ID_ARTICLE INTEGER NOT NULL,
    STOCKER SMALLINT,
    AJOUTER_DETTE SMALLINT,
    MNT_DEVISE NUMERIC (18, 4),
    VALEUR_DA NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    TRANSPORT NUMERIC (18, 2),
    TRANSITE NUMERIC (18, 2),
    DD NUMERIC (18, 2),
    TAUX_DD NUMERIC (18, 2),
    COUT_DINARS NUMERIC (18, 2),
    TAUX_CHANGE NUMERIC (18, 6),
    COUT_DEVISE NUMERIC (18, 2),
    FRET_DA NUMERIC (18, 2),
    FRET NUMERIC (18, 2),
    TAUX_TVA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    P_U_DEVISE NUMERIC (18, 4),
    COUT_ACHAT NUMERIC (18, 2),
    QUANTITE NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    ECART NUMERIC (18, 2),
    CPTE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_STOCK VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_CONS VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_VENTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_UNITE INTEGER NOT NULL,
    ID_FAMILLE INTEGER NOT NULL,
    AUTRES_CHARGES NUMERIC (18, 2),
    CBM_U NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    P_U_DA NUMERIC (18, 2),
    FONCTIONNEMENT SMALLINT,
    TAUX_REMISE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    HT_AVANT_REMISE NUMERIC (18, 2),
    P_U_DA_AV_REM NUMERIC (18, 2),
    ID_TYPE_AUTRES_ENTREES INTEGER NOT NULL,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    REMISES NUMERIC (18, 2),
    QUANTITE_CONDITIONNE NUMERIC (18, 6),
    ID_MAGAZIN INTEGER,
    NBRE_PREPARATION INTEGER,
    GEST_STOCK SMALLINT,
    SENS VARCHAR (3) CHARACTER SET NONE COLLATE NONE,
    REF_ENTREE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    AUTRES_TAXES NUMERIC (18, 2),
    AUTRE_TAXE_U NUMERIC (18, 2),
    PRIX_VENTE NUMERIC (18, 2),
    QTE_SENS SMALLINT,
    NBRE_PALEETTE_U NUMERIC (18, 6),
    PALETTES NUMERIC (18, 6),
    NBRE_CARTON_U NUMERIC (18, 6),
    CARTONS NUMERIC (18, 6),
    INVESTISSEMENT SMALLINT,
    P_U_DEVISE_N NUMERIC(18,10),
    QTE_RECETTE NUMERIC(18,6));

CREATE TABLE FACT_ANT (
    ID_FACT INTEGER NOT NULL,
    NUM_FACT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_TIERS INTEGER,
    MONTANT NUMERIC (18, 2),
    REGLEE SMALLINT,
    DATE_FACT DATE,
    ID_REGL INTEGER);

CREATE TABLE FACT_DD_JOINTE (
    ID_FACT INTEGER NOT NULL,
    ID_DD INTEGER NOT NULL,
    NUM_FACT VARCHAR (18) CHARACTER SET NONE COLLATE NONE,
    DATE_FACT DATE,
    ID_FOURN INTEGER,
    MNT_HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    MOIS SMALLINT,
    ANNEE SMALLINT,
    CPTE_TIER VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_DEBIT VARCHAR (14) CHARACTER SET WIN1251 COLLATE WIN1251,
    CPTE_TBRE VARCHAR (14) CHARACTER SET WIN1251 COLLATE WIN1251,
    CPTE_TVA VARCHAR (14) CHARACTER SET WIN1251 COLLATE WIN1251,
    TYPE_FACT VARCHAR (14) CHARACTER SET WIN1251 NOT NULL COLLATE WIN1251,
    ID_FOURN_FISCAL INTEGER,
    REGLEE SMALLINT);

CREATE TABLE FAMILLES (
    ID_FAMILLE INTEGER NOT NULL,
    FAMILLE VARCHAR (130) CHARACTER SET NONE NOT NULL COLLATE NONE,
    CPTE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_STOCK VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_CONSOMMATION VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_PRODUIT VARCHAR (14) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE FAMILLES_CHARGES (
    ID_FAM INTEGER NOT NULL,
    FAMILLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE FAMILLES_RECETTES (
    ID_FAM_REC INTEGER NOT NULL,
    FAMILLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    CPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE FOURNISSEURS (
    ID_FOURN INTEGER NOT NULL,
    CODE VARCHAR (14) CHARACTER SET NONE NOT NULL COLLATE NONE,
    DENOMINATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ACTIVITE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE1 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE2 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE3 VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    CODE_POSTAL VARCHAR (7) CHARACTER SET NONE COLLATE NONE,
    COMMUNE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DAIRA VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    WILAYA VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    TEL VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    FAX VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    MOBILE VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    EMAIL VARCHAR (64) CHARACTER SET NONE COLLATE NONE,
    URL VARCHAR (64) CHARACTER SET NONE COLLATE NONE,
    RC_AG VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    REF_RC VARCHAR (27) CHARACTER SET NONE COLLATE NONE,
    DATE_RC DATE,
    AI VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    "IF" VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    NIF VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    DB VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    RIB VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    NATURE VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    SOLDE_INI NUMERIC (18, 2));

CREATE TABLE FRAUDEURS (
    ID_FRAUD INTEGER NOT NULL,
    NOM VARCHAR (30) CHARACTER SET NONE COLLATE NONE,
    RC VARCHAR (32) CHARACTER SET NONE COLLATE NONE,
    ADRESSE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    MOTIF VARCHAR (130) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE G50 (
    ID_G50 INTEGER NOT NULL,
    M_T_G50 VARCHAR (3) CHARACTER SET NONE COLLATE NONE,
    PERIODE SMALLINT,
    DATE_G50 DATE,
    TOTAL_TAP NUMERIC (18, 2),
    LIBELLE_E1M10 VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    MNT_E1M10 NUMERIC (18, 2),
    ASSIETTE_E1L20 NUMERIC (18, 2),
    MNT_E1L20 NUMERIC (18, 2),
    ASSIETTE_E1L30 NUMERIC (18, 2),
    MNT_E1L30 NUMERIC (18, 2),
    ASSIETTE_E1L40 NUMERIC (18, 2),
    MNT_E1L40 NUMERIC (18, 2),
    ASSIETTE_E1L60 NUMERIC (18, 2),
    MNT_E1L60 NUMERIC (18, 2),
    ASSIETTE_E1L80 NUMERIC (18, 2),
    MNT_E1L80 NUMERIC (18, 2),
    ASSIETTE_E1M30 NUMERIC (18, 2),
    MNT_E1M30 NUMERIC (18, 2),
    ASSIETTE_E1M40 NUMERIC (18, 2),
    MNT_E1M40 NUMERIC (18, 2),
    TOTAL_ASSIETTE_IRG NUMERIC (18, 2),
    TOTAL_MNT_IRG NUMERIC (18, 2),
    ASSIETTE_E2E00 NUMERIC (18, 2),
    MNT_E2E00 NUMERIC (18, 2),
    ASSIETTE_AUTRE_01 NUMERIC (18, 2),
    MNT_AUTRE_01 NUMERIC (18, 2),
    ASSIETTE_AUTRE_02 NUMERIC (18, 2),
    MNT_AUTRE_02 NUMERIC (18, 2),
    ASSIETTE_AUTRE_03 NUMERIC (18, 2),
    MNT_AUTRE_03 NUMERIC (18, 2),
    LIBELLE_AUTRE_01 VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    LIBELLE_AUTRE_02 VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    LIBELLE_AUTRE_03 VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    TOTAL_AUTRES NUMERIC (18, 2),
    MNT_AUTRES NUMERIC (18, 2),
    IRG_RET_SOURCE NUMERIC (18, 2),
    PRECOMPTE_ANT NUMERIC (18, 2),
    TVA_ACHATS NUMERIC (18, 2),
    TVA_INVEST NUMERIC (18, 2),
    PRORATA_ACHATS NUMERIC (18, 2),
    AVOIR_VENTES NUMERIC (18, 2),
    AUTRES_DEDUCTION NUMERIC (18, 2),
    TOTAL_DEDUCTION NUMERIC (18, 2),
    TOTAL_TVA_DU NUMERIC (18, 2),
    PRORATA_VENTE NUMERIC (18, 2),
    REVERSMENT_DEDUCTION NUMERIC (18, 2),
    TOTAL_DROIS_DUS NUMERIC (18, 2),
    TVA_A_PAYER NUMERIC (18, 2),
    PRECOMPTE NUMERIC (18, 2),
    IBS_RET_A_SOURCE NUMERIC (18, 2),
    MNT_G50 NUMERIC (18, 2),
    PERIODE_E1M10 VARCHAR (10) CHARACTER SET NONE COLLATE NONE,
    TOTAL_CA_TAP NUMERIC (18, 2),
    TOTAL_IMPO_TAP NUMERIC (18, 2),
    TOTAL_ASSIETTE_AUTRES NUMERIC (18, 2),
    TOTAL_CA_IMP_TVA NUMERIC (18, 2),
    TOTAL_EXO_TVA NUMERIC (18, 2),
    TOTAL_IMP_TVA NUMERIC (18, 2),
    DIRECTION VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    INSPECTION VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    RECETTE VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    TYPE_G50 SMALLINT);

CREATE TABLE INVENTAIRE (
    ID_INVENTAIRE INTEGER NOT NULL,
    DATE_INVENTAIRE DATE,
    ID_ARTICLE INTEGER,
    REF VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    DESIGNATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ID_FAMILLE INTEGER,
    IMPORTATION SMALLINT,
    QUANTITE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    FONCTIONNEMENT SMALLINT,
    ID_UNITE INTEGER,
    ID_MAGAZIN INTEGER,
    QUANTITE_CONDITIONNE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    PALETTES NUMERIC(18, 2),
    CARTONS NUMERIC(18, 2),
    PRIX_VENTE NUMERIC(18, 2),
    UNITE_PALETTE NUMERIC(18, 6));

CREATE TABLE LIGNES_PROFORMA (
    ID_L_PROF INTEGER NOT NULL,
    ID_ENTET INTEGER,
    ID_CLIENT INTEGER,
    ID_ART INTEGER,
    ID_FAM INTEGER,
    ID_UNITE INTEGER,
    PRIX_U NUMERIC (18, 2),
    TAUX_TVA NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    DATE_FACT DATE,
    NUM_FACT INTEGER,
    AUTRES_TAXES NUMERIC (18, 2),
    AUTRE_TAXE_U NUMERIC (18, 2));

CREATE TABLE LIGNES_TRANSFERT (
    ID_LIGNE INTEGER NOT NULL,
    ID_ENTETE INTEGER NOT NULL,
    ID_MAGAZIN_1 INTEGER NOT NULL,
    ID_MAGAZIN_2 INTEGER NOT NULL,
    DATE_PIECE DATE,
    NUM_PIECE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_ARTICLE INTEGER,
    DESIGNATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ID_UNITE INTEGER,
    ID_FAMILLE INTEGER,
    QUANTITE NUMERIC (18, 6),
    PAMP NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    CBM_U NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    POIDS_U NUMERIC (18, 2),
    POIDS NUMERIC (18, 2),
    REF_ENTREE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    STOCK NUMERIC (18, 6),
    OLD_QUANTITE NUMERIC (18, 6));

CREATE TABLE LISTE_CHARGES (
    ID_LISTE_CHARGE INTEGER NOT NULL,
    LIBELLE_CHARGE VARCHAR (66) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE MAGAZINS (
    ID_MAG INTEGER NOT NULL,
    MAGAZIN VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ADRESSE VARCHAR (255) CHARACTER SET NONE COLLATE NONE,
    A_I VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    I_F VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    NIS VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    NIN VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    RC VARCHAR (22) CHARACTER SET NONE COLLATE NONE,
    INITIAL VARCHAR (12) CHARACTER SET NONE COLLATE NONE,
    VAC SMALLINT,
    ID_COMMUNE INTEGER,
    ACTIVITE VARCHAR (255) CHARACTER SET NONE COLLATE NONE,
    DIRECTION VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    INSPECTION VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    RECETTE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    TELEPHONE VARCHAR(32),
    FAX VARCHAR(32),
    PORTABLE VARCHAR(32),
    EMAIL VARCHAR(128),
    BP VARCHAR (22));

CREATE TABLE MARQUES (
    ID_MARQUE INTEGER NOT NULL,
    MARQUE VARCHAR (66) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE NOMENCLATURES (
    ID_NOM INTEGER NOT NULL,
    ID_PRODUIT_FINI INTEGER NOT NULL,
    ID_MATIERE INTEGER NOT NULL,
    QTE NUMERIC (18, 8),
    MAT_EMB INTEGER,
    QTE_DECHET NUMERIC (18, 6),
    TX_DECHET NUMERIC (18, 2),
    ARRONDI SMALLINT,
    PROD_STRICT SMALLINT);

CREATE TABLE NUM_SERIES (
    ID_NUM INTEGER NOT NULL,
    ID_ARTICLE INTEGER,
    ARTICLE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    NUM_SERIE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    ID_ENTREE INTEGER,
    ID_SORTIE INTEGER);

CREATE TABLE PARAMETRES (
    ID_PARAM INTEGER NOT NULL,
    EXERCICE SMALLINT,
    EXER_DU DATE,
    EXER_AU DATE,
    CPTE_TVA_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TVA_INVEST VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TIMBRE_VENTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TIMBRE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TVA_PAYER VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TVA_PRECOMPTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    TAILLE_VIRGULE SMALLINT,
    CPTE_REMISE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_TVA_VENTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_VIREMENT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_CODE_TAP INTEGER,
    ID_CODE_TVA INTEGER,
    DIRECTION VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    INSPECTION VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    RECETTE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    ID_CODE_COMMUNE INTEGER,
    CPTE_RMISE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    PRECOMPTE NUMERIC (18, 2),
    CPTE_AUTRES VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_REMISE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_DEFALUT_DEPOT INTEGER,
    ID_DEF_M_REGL INTEGER,
    USE_PALETTE SMALLINT,
    ARRONDI_TIMBRE SMALLINT,
    ID_DEPOT_ACHAT INTEGER,
    ID_DEPOT_PF INTEGER,
    ID_DEPOT_MT INTEGER,
    PROD_STRICT SMALLINT,
    A_S_COUT SMALLINT,
    NEW_SQL SMALLINT,
    TRANSPORT SMALLINT,
    AUTRES_CHARGES_LIB VARCHAR(32),
    GEST_QUALITE SMALLINT,
    CLASSE SMALLINT,
    TAXE_ACHAT SMALLINT,
    ORDER_ASC SMALLINT,
    NOM_DOSSIER VARCHAR(128),
    CODE_DOSSIER VARCHAR(12),
    NIN VARCHAR(20),
    TAUX_TIMBRE         NUMERIC(18,2),
    VAL_MIN_TIMBRE      NUMERIC(18,2),
    VAL_MAX_TIMBRE      NUMERIC(18,2),
    FOURN_DEP SMALLINT,
    DOUBLE_U            SMALLINT,
    ACT_PRIX_V            SMALLINT,
    PROF_A_S SMALLINT,
    DEPUIS_FAMILLE SMALLINT,
    ID_CODE_TLS INTEGER);

CREATE TABLE PERIODES (
    ID_PER INTEGER NOT NULL,
    CODE SMALLINT NOT NULL,
    MOIS VARCHAR (22) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE PROFORMA (
    ID_PROF INTEGER NOT NULL,
    NUM_FACT INTEGER,
    DATE_FACT DATE,
    ID_CLIENT INTEGER,
    TOTAL_HT NUMERIC (18, 2),
    TOTAL_TVA NUMERIC (18, 2),
    TOTAL_TTC NUMERIC (18, 2),
    LOCKED SMALLINT,
    OBS BLOB sub_type 1 segment size 80,
    BIS VARCHAR (12) CHARACTER SET NONE COLLATE NONE,
    TIMBRE NUMERIC (18, 2),
    AUTRES_TAXES NUMERIC (18, 2),
    TAUX_REMISE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    TOTAL_FACTURE NUMERIC (18, 2),
    ID_REGLEMENT INTEGER,
    ID_DEST INTEGER,
    ID_CHAUF INTEGER,
    ID_CAMION INTEGER);

CREATE TABLE RECETTES (
    ID_REC INTEGER NOT NULL,
    DATE_REC DATE,
    NUM_REC INTEGER,
    ID_CPTE INTEGER,
    ID_TYPE_REC INTEGER,
    LIB_REC VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    MONTANT NUMERIC (18, 2),
    LOCKED SMALLINT,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    ETAT VARCHAR (18) CHARACTER SET NONE COLLATE NONE,
    NUM_CHEQUE VARCHAR (9) CHARACTER SET NONE COLLATE NONE,
    VEROUILLE SMALLINT);

CREATE TABLE REGLEMENTS (
    ID_REGLEMENT INTEGER NOT NULL,
    LIBELLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    TIMBRE SMALLINT,
    ID_COMPTE INTEGER,
    REFACTION SMALLINT,
    REG_V_C SMALLINT);

CREATE TABLE REGL_TIERS (
    ID_REGL INTEGER NOT NULL,
    DATE_REGT DATE,
    ID_CPTE INTEGER,
    ID_TIER INTEGER,
    TYPE_REGT SMALLINT,
    LIBELLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    MONTANT NUMERIC (18, 2),
    NUM_PIECE INTEGER,
    AVANCE SMALLINT,
    ID_PIECE_VENTE INTEGER,
    LOCKED SMALLINT,
    ID_ENTET_PIECE INTEGER,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    DENOMINATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    NUM_CHEQUE VARCHAR (9) CHARACTER SET NONE COLLATE NONE,
    ETAT VARCHAR (18) CHARACTER SET NONE COLLATE NONE,
    REPRESENTANT VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    DATETIME_PIECE TIMESTAMP,
    SOLDE_ANT NUMERIC (18, 2),
    SOLDE_FIN NUMERIC (18, 2),
    VEROUILLE SMALLINT);

CREATE TABLE RENUM_CHAUFFEURS (
    ID_RENUM INTEGER NOT NULL,
    ID_CHAUFFEUR INTEGER,
    DATE_RENUM DATE,
    LIBELLES VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    MONTANT NUMERIC (18, 2));

CREATE TABLE SORTIES (
    ID_SORTIE INTEGER NOT NULL,
    ID_ENTETE INTEGER,
    NATURE_SORTIE SMALLINT,
    ID_ARTICLE INTEGER NOT NULL,
    ID_CLIENT INTEGER,
    ID_UNITE INTEGER,
    DATE_PIECE DATE,
    DESTOCKER SMALLINT,
    AJOUTER_CREANCE SMALLINT,
    F_P SMALLINT,
    PRIX_UNITAIRE NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TAUX_TVA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    CONSOMATION NUMERIC (18, 2),
    CR_U NUMERIC (18, 2),
    ID_FP INTEGER,
    CPTE_VENTE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_ACHAT VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_CONS VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    CPTE_STOCK VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    ID_FAMILLE INTEGER,
    QUANTITE NUMERIC (18, 6),
    GROS SMALLINT,
    QUANTITE_CONDITIONNE NUMERIC (18, 6),
    CESSION SMALLINT,
    REMISE NUMERIC (18, 2),
    TAUX_REMISE NUMERIC (18, 2),
    HT_AVANT_REMISE NUMERIC (18, 2),
    P_U_A_REM NUMERIC (18, 2),
    ID_AUTRES_SORTIES INTEGER,
    UTILISATEUR VARCHAR (34) CHARACTER SET NONE COLLATE NONE,
    REMISES NUMERIC (18, 2),
    ID_MAGAZIN INTEGER,
    GEST_STOCK SMALLINT,
    SENS VARCHAR (3) CHARACTER SET NONE COLLATE NONE,
    REF_ENTREE VARCHAR (14) CHARACTER SET NONE COLLATE NONE,
    OLD_QUANTITE NUMERIC (18, 6),
    QUANTITE_NECESSAIRE NUMERIC (18, 6),
    AUTRES_TAXES NUMERIC (18, 2),
    AUTRE_TAXE_U NUMERIC (18, 2),
    ID_LFP INTEGER,
    STOCK NUMERIC (18, 6),
    POIDS_U NUMERIC (18, 2),
    LONGEUR NUMERIC (18, 2),
    LARGEUR NUMERIC (18, 2),
    NOMBRE NUMERIC (18, 2),
    CBM_U NUMERIC (18, 2),
    POIDS NUMERIC (18, 2),
    CBM NUMERIC (18, 2),
    PU_TTC NUMERIC (18, 2),
    VOLUME NUMERIC (18, 2),
    ALCOOL NUMERIC (18, 2),
    FULL_DESIGNATION VARCHAR (256) CHARACTER SET NONE COLLATE NONE,
    DESIGNATION VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    FRANCHISE SMALLINT,
    QTE_SENS SMALLINT,
    DECHETS NUMERIC (18, 6),
    QUANTITE_CONDITIONNE_U NUMERIC (18, 6),
    QUANTITE_PALETTE_U NUMERIC (18, 6),
    QUANTITE_PALETTES NUMERIC (18, 6),
    PROD_STRICT SMALLINT);

CREATE TABLE TAUX_EXTRACTION (
    ID_TAUX INTEGER NOT NULL,
    ID_MAT INTEGER,
    ID_PF INTEGER,
    TAUX NUMERIC (18, 2),
    ID_NORME INTEGER,
    ARRONDI SMALLINT);

CREATE TABLE TYPE_AUTRES_ENTREES (
    ID_TYPE INTEGER NOT NULL,
    LIBELLE VARCHAR (20) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE TYPE_AUTRES_SORTIES (
    ID_TYPE INTEGER NOT NULL,
    LIBELLE VARCHAR (130) CHARACTER SET NONE COLLATE NONE);

CREATE TABLE UNITES (
    ID_UNITE INTEGER NOT NULL,
    UNITE VARCHAR (66) CHARACTER SET NONE NOT NULL COLLATE NONE);

CREATE TABLE VIR_FONDS (
    ID_VIR INTEGER NOT NULL,
    NUM_VIR_FOND INTEGER NOT NULL,
    DATE_OPER DATE,
    ID_COMPTE_CREDIT INTEGER NOT NULL,
    ID_COMPTE_DEBIT INTEGER NOT NULL,
    LIBELLE VARCHAR (66) CHARACTER SET NONE COLLATE NONE,
    MONTANT NUMERIC (18, 2),
    LOCKED SMALLINT,
    NUM_CHEQUE VARCHAR (9) CHARACTER SET NONE COLLATE NONE,
    VEROUILLE SMALLINT);

CREATE TABLE ZONES (
    ID_ZONE INTEGER NOT NULL,
    ZONE VARCHAR (130) CHARACTER SET NONE COLLATE NONE,
    LET_FACT VARCHAR (6) CHARACTER SET NONE COLLATE NONE,
    PRIX_QUOTEPART NUMERIC (18, 2),
    ID_QUOTE INTEGER);



/* View: MOUVEMENT */
CREATE VIEW MOUVEMENT (
    ID_ARTICLE,
    REF_ENTREE,
    ID_MAGAZIN,
    DATE_OPER,
    COUT,
    QTE,
    VAL)
AS SELECT
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           INVENTAIRE.COUT,
           CAST(INVENTAIRE.quantite AS NUMERIC(18,6)),
           CAST(INVENTAIRE.quantite * COUT AS NUMERIC(18,2))
     FROM inventaire
     WHERE inventaire.quantite <> 0

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.cout_achat,
           CAST((ENTREES.quantite * entrees.qte_sens) AS NUMERIC(18,6)),
           CAST((ENTREES.quantite * ENTREES.cout_achat * entrees.qte_sens) AS NUMERIC(18,2))
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree <> 4)

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.cr_u,
           CAST((SORTIES.quantite * sorties.qte_sens) AS NUMERIC(18,6)),
           CAST((SORTIES.quantite * sorties.cr_u * sorties.qte_sens) AS NUMERIC(18,2))
     FROM sorties
     inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
     WHERE (SORTIES.destocker = 1) OR ((SORTIES.nature_sortie = 5)OR(SORTIES.nature_sortie = 7))

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           lignes_transfert.PAMP,
           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2))
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE entete_transfert.entree_stock = 1

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           lignes_transfert.PAMP,
           -(CAST(lignes_transfert.quantite AS NUMERIC(18,6))),
           -(CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)))
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE entete_transfert.sortie_stock = 1;



/* View: RECUP_TVA */
CREATE VIEW RECUP_TVA (
    DENOMINATION,
    ADRESSE,
    RC,
    AI,
    NIF,
    NUM_FACT,
    DATE_FACT,
    HT,
    TVA,
    MOIS,
    ANNEE,
    INVEST)
AS SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      entete_entrees.num_fact,
      entete_entrees.date_fact,
      entete_entrees.total_ht,
      entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      cast(0 AS smallint)
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE entete_entrees.nature_entree = 2
AND  entete_entrees.total_tva > 0

UNION

SELECT
      cast('D10 N? : '||entete_entrees.num_d10 AS varchar(128)),
      cast('' AS varchar(128)),
      cast('' AS varchar(25)),
      cast('' AS varchar(20)),
      cast('' AS varchar(20)),
      entete_entrees.num_d10,
      entete_entrees.date_d10,
      entete_entrees.cv_d10,
      entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      cast(0 AS smallint)
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE entete_entrees.nature_entree = 1
AND  entete_entrees.total_tva > 0

UNION

SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      CAST(fact_dd_jointe.num_fact AS varchar(12)),
      fact_dd_jointe.date_fact,
      fact_dd_jointe.mnt_ht,
      fact_dd_jointe.tva,
      fact_dd_jointe.mois,
      fact_dd_jointe.annee,
      cast(0 AS smallint)
FROM fact_dd_jointe
inner join clients on clients.id_client = fact_dd_jointe.id_fourn
WHERE fact_dd_jointe.tva > 0

UNION

SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      DEPENSES.num_fact,
      DEPENSES.date_fact,
      DEPENSES.montant_ht,
      DEPENSES.montant_tva,
      DEPENSES.mois,
      DEPENSES.annee,
      depenses.is_investissement
FROM DEPENSES
inner join clients on clients.id_client = DEPENSES.id_tiers
WHERE DEPENSES.montant_tva > 0;




/* Primary keys definition */
ALTER TABLE CAT_PROD ADD CONSTRAINT PK_CAT_PROD PRIMARY KEY (ID_CAT_PROD);
ALTER TABLE ARCHIVES ADD CONSTRAINT PK_ARCHIVES PRIMARY KEY (ID_ARCHIVE);
ALTER TABLE ARTICLES ADD CONSTRAINT PK_ARTICLES PRIMARY KEY (ID_ART);
ALTER TABLE CAMION ADD CONSTRAINT PK_CAMION PRIMARY KEY (ID_CAMION);
ALTER TABLE CATALOGUE ADD CONSTRAINT PK_CATALOGUE PRIMARY KEY (ID_CAT);
ALTER TABLE CHAUFFEURS ADD CONSTRAINT PK_CHAUFFEURS PRIMARY KEY (ID_CHAUFFEUR);
ALTER TABLE CLIENTS ADD CONSTRAINT PK_CLIENTS PRIMARY KEY (ID_CLIENT);
ALTER TABLE CODE_TAP ADD CONSTRAINT PK_CODE_TAP PRIMARY KEY (ID_TAP);
ALTER TABLE CODE_TVA ADD CONSTRAINT PK_CODE_TVA PRIMARY KEY (ID_TVA);
ALTER TABLE COMMUNES ADD CONSTRAINT PK_COMMUNES PRIMARY KEY (ID_COM);
ALTER TABLE COMPTES_TRESORIE ADD CONSTRAINT PK_COMPTES_TRESORIE PRIMARY KEY (ID_CPTE);
ALTER TABLE DEPENSES ADD CONSTRAINT PK_DEPENSES PRIMARY KEY (ID_DEP);
ALTER TABLE DESTINATION ADD CONSTRAINT PK_DESTINATION PRIMARY KEY (ID_DESTINATION);
ALTER TABLE DEVISES ADD CONSTRAINT PK_DEVISES PRIMARY KEY (ID_DEVISE);
ALTER TABLE EMPLACEMENTS ADD CONSTRAINT PK_EMPLACEMENTS PRIMARY KEY (ID_EMPL);
ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT PK_ENTETE_ENTREES PRIMARY KEY (ID_ENT_ENT);
ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT PK_ENTETE_SORTIES PRIMARY KEY (ID_ENT_SORT);
ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT PK_ENTETE_TRANSFERT PRIMARY KEY (ID_ENTETE);
ALTER TABLE ENTET_BC_F ADD CONSTRAINT PK_ENTET_BC_F PRIMARY KEY (ID_ENT_BD_F);
ALTER TABLE ENTREES ADD CONSTRAINT PK_ENTREES PRIMARY KEY (ID_ENTREE);
ALTER TABLE FACT_ANT ADD CONSTRAINT PK_FACT_ANT PRIMARY KEY (ID_FACT);
ALTER TABLE FACT_DD_JOINTE ADD CONSTRAINT PK_FACT_DD_JOINTE PRIMARY KEY (ID_FACT);
ALTER TABLE FAMILLES ADD CONSTRAINT PK_FAMILLES PRIMARY KEY (ID_FAMILLE);
ALTER TABLE FAMILLES_CHARGES ADD CONSTRAINT PK_FAMILLES_CHARGES PRIMARY KEY (ID_FAM);
ALTER TABLE FAMILLES_RECETTES ADD CONSTRAINT PK_FAMILLES_RECETTES PRIMARY KEY (ID_FAM_REC);
ALTER TABLE FOURNISSEURS ADD CONSTRAINT PK_FOURNISSEURS PRIMARY KEY (ID_FOURN);
ALTER TABLE FRAUDEURS ADD CONSTRAINT PK_FRAUDEURS PRIMARY KEY (ID_FRAUD);
ALTER TABLE G50 ADD CONSTRAINT PK_G50 PRIMARY KEY (ID_G50);
ALTER TABLE INVENTAIRE ADD CONSTRAINT PK_INVENTAIRE PRIMARY KEY (ID_INVENTAIRE);
ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT PK_LIGNES_PROFORMA PRIMARY KEY (ID_L_PROF);
ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT PK_LIGNES_TRANSFERT PRIMARY KEY (ID_LIGNE);
ALTER TABLE LISTE_CHARGES ADD CONSTRAINT PK_LISTE_CHARGES PRIMARY KEY (ID_LISTE_CHARGE);
ALTER TABLE MAGAZINS ADD CONSTRAINT PK_MAGAZINS PRIMARY KEY (ID_MAG);
ALTER TABLE MARQUES ADD CONSTRAINT PK_MARQUES PRIMARY KEY (ID_MARQUE);
ALTER TABLE NOMENCLATURES ADD CONSTRAINT PK_NOMENCLATURES PRIMARY KEY (ID_NOM);
ALTER TABLE NUM_SERIES ADD CONSTRAINT PK_NUM_SERIES PRIMARY KEY (ID_NUM);
ALTER TABLE PARAMETRES ADD CONSTRAINT PK_PARAMETRES PRIMARY KEY (ID_PARAM);
ALTER TABLE PERIODES ADD CONSTRAINT PK_PERIODES PRIMARY KEY (ID_PER);
ALTER TABLE PROFORMA ADD CONSTRAINT PK_PROFORMA PRIMARY KEY (ID_PROF);
ALTER TABLE RECETTES ADD CONSTRAINT PK_RECETTES PRIMARY KEY (ID_REC);
ALTER TABLE REGLEMENTS ADD CONSTRAINT PK_REGLEMENTS PRIMARY KEY (ID_REGLEMENT);
ALTER TABLE REGL_TIERS ADD CONSTRAINT PK_REGL_TIERS PRIMARY KEY (ID_REGL);
ALTER TABLE RENUM_CHAUFFEURS ADD CONSTRAINT PK_RENUM_CHAUFFEURS PRIMARY KEY (ID_RENUM);
ALTER TABLE SORTIES ADD CONSTRAINT PK_SORTIES PRIMARY KEY (ID_SORTIE);
ALTER TABLE TAUX_EXTRACTION ADD CONSTRAINT PK_TAUX_EXTRACTION PRIMARY KEY (ID_TAUX);
ALTER TABLE TYPE_AUTRES_ENTREES ADD CONSTRAINT PK_TYPE_AUTRES_ENTREES PRIMARY KEY (ID_TYPE);
ALTER TABLE TYPE_AUTRES_SORTIES ADD CONSTRAINT PK_TYPE_AUTRES_SORTIES PRIMARY KEY (ID_TYPE);
ALTER TABLE UNITES ADD CONSTRAINT PK_UNITES PRIMARY KEY (ID_UNITE);
ALTER TABLE VIR_FONDS ADD CONSTRAINT PK_VIR_FONDS PRIMARY KEY (ID_VIR);
ALTER TABLE ZONES ADD CONSTRAINT PK_ZONES PRIMARY KEY (ID_ZONE);
ALTER TABLE CONSIGNATIONS ADD CONSTRAINT PK_CONSIGNATIONS PRIMARY KEY (ID_CONSIGNE);
ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT PK_LIGNES_CONSIGNATION PRIMARY KEY (ID_LC);
CREATE UNIQUE INDEX PK_NORMES ON NORMES(ID_NORME);

/* Unique keys definition */

ALTER TABLE ARTICLES ADD CONSTRAINT UK_ARTICLES_REF UNIQUE (REF);
ALTER TABLE CLIENTS ADD CONSTRAINT UK_CLIENTS_CODE UNIQUE (CODE);
ALTER TABLE FOURNISSEURS ADD CONSTRAINT UK_FOURNISSEURS_CODE UNIQUE (CODE);


/* Indices definition */


CREATE INDEX IDX_ENTETE_ENTREES_DATE_FACT ON ENTETE_ENTREES(DATE_FACT);
CREATE INDEX IDX_ENTETE_ENTREES_NUM_FACT ON ENTETE_ENTREES(NUM_FACT);
CREATE INDEX IDX_ENTETE_ENTREES_STOCKER ON ENTETE_ENTREES(STOCKER);
CREATE INDEX IDX_ENT_ENTREES_ADD_DETTES ON ENTETE_ENTREES(AJOUTER_DETTES);
CREATE INDEX IDX_ENT_ENTREES_ID_FOURN ON ENTETE_ENTREES(ID_FOURN);
CREATE INDEX IDX_ENT_ENTREES_ID_MAG ON ENTETE_ENTREES(ID_MAGAZIN);
CREATE INDEX IDX_ENT_ENTREES_NATURE ON ENTETE_ENTREES(NATURE_ENTREE);
CREATE INDEX IDX_ENTETE_SORTIES_DATE ON ENTETE_SORTIES(DATE_PIECE);
CREATE INDEX IDX_ENTETE_SORTIES_NUM ON ENTETE_SORTIES(NUM_PIECE);
CREATE INDEX IDX_ENT_SORTIES_ADD_CREANCE ON ENTETE_SORTIES(AJOUTER_CREANCE);
CREATE INDEX IDX_ENT_SORTIES_DESTOCKER ON ENTETE_SORTIES(DESTOCKER);
CREATE INDEX IDX_ENT_SORTIES_ID_CLIENT ON ENTETE_SORTIES(ID_CLIENT);
CREATE INDEX IDX_ENT_SORTIES_ID_MAG ON ENTETE_SORTIES(ID_MAGAZIN);
CREATE INDEX IDX_ENT_SORTIES_NATURE ON ENTETE_SORTIES(NATURE_SORTIE);
CREATE INDEX IDX_ENTETE_TRANSFERT_DATE_PIECE ON ENTETE_TRANSFERT(DATE_PIECE);
CREATE INDEX IDX_ENTETE_TRANSFERT_ENTREE ON ENTETE_TRANSFERT(ENTREE_STOCK);
CREATE INDEX IDX_ENTETE_TRANSFERT_NUM_PIECE ON ENTETE_TRANSFERT(NUM_PIECE);
CREATE INDEX IDX_ENTETE_TRANSFERT_SORTIE ON ENTETE_TRANSFERT(SORTIE_STOCK);
CREATE INDEX IDX_ENTREES_ID_ARTICLE ON ENTREES(ID_ARTICLE);
CREATE INDEX IDX_ENTREES_NATURE_ENTREE ON ENTREES(NATURE_ENTREE);
CREATE INDEX IDX_ENTREES_STOCKER ON ENTREES(STOCKER);
CREATE INDEX IDX_INVENTAIRE_DATE ON INVENTAIRE(DATE_INVENTAIRE);
CREATE INDEX IDX_INVENTAIRE_ID_ARTICLE ON INVENTAIRE(ID_ARTICLE);
CREATE INDEX IDX_LIGNES_PROFORMA_DATE ON LIGNES_PROFORMA(DATE_FACT);
CREATE INDEX IDX_LIGNES_PROFORMA_NUM ON LIGNES_PROFORMA(NUM_FACT);
CREATE INDEX IDX_LIGNES_TRANSFERT_DATE ON LIGNES_TRANSFERT(DATE_PIECE);
CREATE INDEX IDX_LIGNES_TRANSFERT_ENTETE ON LIGNES_TRANSFERT(ID_ENTETE);
CREATE INDEX IDX_LIGNES_TRANSFERT_ID_ARTICLE ON LIGNES_TRANSFERT(ID_ARTICLE);
CREATE INDEX IDX_LIGNES_TRANSFERT_NUM ON LIGNES_TRANSFERT(NUM_PIECE);
CREATE INDEX IDX_PROFORMA_DATE ON PROFORMA(DATE_FACT);
CREATE INDEX IDX_PROFORMA_NUM ON PROFORMA(NUM_FACT);
CREATE INDEX IDX_RECETTES_DATE ON RECETTES(DATE_REC);
CREATE INDEX IDX_RECETTES_NUM ON RECETTES(NUM_REC);
CREATE INDEX IDX_REGL_TIERS_DATE ON REGL_TIERS(DATE_REGT);
CREATE INDEX IDX_REGL_TIERS_NUM ON REGL_TIERS(NUM_PIECE);
CREATE INDEX IDX_SORTIES_DATE ON SORTIES(DATE_PIECE);
CREATE INDEX IDX_SORTIES_DESTOCKER ON SORTIES(DESTOCKER);
CREATE INDEX IDX_SORTIES_ID_ARTICLE ON SORTIES(ID_ARTICLE);
CREATE INDEX IDX_SORTIES_NATURE_SORTIE ON SORTIES(NATURE_SORTIE);


/* Structure for the `CATALOGUE` table :  */
ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);

ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `CONSIGNATIONS` table :  */

ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_CON FOREIGN KEY (ID_ART_CONS) REFERENCES ARTICLES(ID_ART);

/* Structure for the `DEPENSES` table :  */

ALTER TABLE DEPENSES ADD CONSTRAINT FK_DEPENSES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);

/* Structure for the `ENTETE_ENTREES` table :  */

ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_TIER FOREIGN KEY (ID_FOURN) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `ENTETE_SORTIES` table :  */

ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `ENTETE_TRANSFERT` table :  */

ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);

/* Structure for the `ENTREES` table :  */

ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);

ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_TIER FOREIGN KEY (ID_FOUR) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `FACT_ANT` table :  */

ALTER TABLE FACT_ANT ADD CONSTRAINT FK_FACT_ANT_TIER FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `FACT_DD_JOINTE` table :  */

ALTER TABLE FACT_DD_JOINTE ADD CONSTRAINT FK_FACT_DD_JOINTE_ENTETE FOREIGN KEY (ID_DD) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);

/* Structure for the `INVENTAIRE` table :  */

ALTER TABLE INVENTAIRE ADD CONSTRAINT FK_INVENTAIRE_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

/* Structure for the `LIGNES_CONSIGNATION` table :  */

ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_TIERS FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `LIGNES_PROFORMA` table :  */

ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);

ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ENTETE FOREIGN KEY (ID_ENTET) REFERENCES PROFORMA(ID_PROF);

ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `LIGNES_TRANSFERT` table :  */

ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);

/* Structure for the `NOMENCLATURES` table :  */

ALTER TABLE NOMENCLATURES ADD CONSTRAINT FK_NOMENCLATURES_MAT FOREIGN KEY (ID_MATIERE) REFERENCES ARTICLES(ID_ART);

/* Structure for the `NUM_SERIES` table :  */

ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ENTREE FOREIGN KEY (ID_ENTREE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);

ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_SORTIE FOREIGN KEY (ID_SORTIE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);

/* Structure for the `PROFORMA` table :  */

ALTER TABLE PROFORMA ADD CONSTRAINT FK_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `RECETTES` table :  */

ALTER TABLE RECETTES ADD CONSTRAINT FK_RECETTES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);

/* Structure for the `REGL_TIERS` table :  */

ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);

ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);

/* Structure for the `SORTIES` table :  */

ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);

ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);

ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);

ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);

INSERT INTO REGLEMENTS  (ID_REGLEMENT,LIBELLE,TIMBRE,ID_COMPTE,REFACTION)VALUES  (2,'ESPECES',1,1,0);
INSERT INTO REGLEMENTS  (ID_REGLEMENT,LIBELLE,TIMBRE,ID_COMPTE,REFACTION)VALUES  (3,'CHEQUE',0,3,0);
INSERT INTO REGLEMENTS  (ID_REGLEMENT,LIBELLE,TIMBRE,ID_COMPTE,REFACTION)VALUES  (4,'VERSEMENT',0,3,0);
INSERT INTO REGLEMENTS  (ID_REGLEMENT,LIBELLE,TIMBRE,ID_COMPTE,REFACTION)VALUES  (5,'VIREMENT',0,3,0);
INSERT INTO REGLEMENTS  (ID_REGLEMENT,LIBELLE,TIMBRE,ID_COMPTE,REFACTION)VALUES  (6,'A TERME',0,2,0);
SET GENERATOR REGLEMENTS_ID_REGLEMENT_GEN TO 6;


INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (1,'CARTON');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (2,'KG');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (3,'U');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (4,'L');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (5,'M.L.');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (6,'M2');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (7,'M3');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (8,'TONNE');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (9,'PIECES');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (10,'DOUZAINE');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (11,'SAC');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (12,'QTX');
INSERT INTO UNITES  (ID_UNITE,UNITE)VALUES  (13,'FUT');
SET GENERATOR UNITES_ID_UNITE_GEN TO 13;

INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (1,'DOLLAR');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (2,'EURO');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (3,'L.S.');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (4,'D.A.');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (5,'D.T.');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (6,'D.M.');
INSERT INTO DEVISES  (ID_DEVISE,DEVISE)VALUES  (7,'R.S.');
SET GENERATOR DEVISES_ID_DEVISE_GEN TO 7;

INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1, '0101', 'ADRAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (2, '0102', 'TAMEST');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (3, '0103', 'CHAROUINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (4, '0104', 'REGGANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (5, '0105', 'IN-ZGHMIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (6, '0106', 'TIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (7, '0107', 'KSAR KADDOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (8, '0108', 'TSABIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (9, '0109', 'TIMIMOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (10, '0110', 'OULED SAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (11, '0111', 'ZAOUIET KOUNTA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (12, '0112', 'AOULEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (13, '0113', 'TIMEKTEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (14, '0114', 'TAMANTIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (15, '0115', 'FENOUGHIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (16, '0116', 'TINERKOUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (17, '0117', 'DELDOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (18, '0118', 'SALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (19, '0119', 'AKABLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (20, '0120', 'METARFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (21, '0121', 'OULED AHMED TIMMI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (22, '0122', 'BOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (23, '0123', 'AOUGROUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (24, '0124', 'TALMINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (25, '0125', 'BORDJ BAJI MOKHTAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (26, '0126', 'SEBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (27, '0127', 'OULED AISSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (28, '0128', 'TIMIAOUINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (29, '0201', 'CHLEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (30, '0202', 'TENES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (31, '0203', 'BENAIRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (32, '0204', 'EL KARIMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (33, '0205', 'TADJENA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (34, '0206', 'TAOUGRITE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (35, '0207', 'BENI HAOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (36, '0208', 'SOBHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (37, '0209', 'HARCHOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (38, '0210', 'OULED FARES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (39, '0211', 'SIDI AKKACHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (40, '0212', 'BOUKADIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (41, '0213', 'BENI RACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (42, '0214', 'TALASSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (43, '0215', 'HERENFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (44, '0216', 'OUED GOUSSINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (45, '0217', 'DAHRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (46, '0218', 'OULED ABBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (47, '0219', 'SENDJAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (48, '0220', 'ZEBOUDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (49, '0221', 'OUED SLY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (50, '0222', 'ABOU EL HASSAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (51, '0223', 'EL MARSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (52, '0224', 'CHETTIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (53, '0225', 'SIDI ABDERRAHMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (54, '0226', 'MOUSSADEK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (55, '0227', 'EL HADJADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (56, '0228', 'LABIOD MEDJADJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (57, '0229', 'OUED FODDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (58, '0230', 'OULED B.ABDELKADER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (59, '0231', 'BOUZEGHAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (60, '0232', 'AIN MERANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (61, '0233', 'OUM DROU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (62, '0234', 'BREIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (63, '0235', 'BENI BOUATEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (64, '0301', 'LAGHOUAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (65, '0302', 'KSAR EL HIRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (66, '0303', 'BENACER BEN CHOHRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (67, '0304', 'SIDI MAKHLOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (68, '0305', 'HASSI DELAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (69, '0306', 'HASSI R''MEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (70, '0307', 'AIN MADHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (71, '0308', 'TADJEMOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (72, '0309', 'KHENEG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (73, '0310', 'GUELTAT SIDI SAAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (74, '0311', 'AIN SIDI ALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (75, '0312', 'BEIDHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (76, '0313', 'BRIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (77, '0314', 'EL GHICHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (78, '0315', 'HADJ MECHRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (79, '0316', 'SEBGAG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (80, '0317', 'TAOUIALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (81, '0318', 'TADJROUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (82, '0319', 'AFLOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (83, '0320', 'EL ASSAFIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (84, '0321', 'OUED MORRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (85, '0322', 'OUED M''ZI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (86, '0323', 'EL HAOUAITA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (87, '0324', 'SIDI BOUZID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (88, '0401', 'OUM EL BOUAGHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (89, '0402', 'AIN BEIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (90, '0403', 'AIN M''LILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (91, '0404', 'BEHIR CHERGUI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (92, '0405', 'EL AMIRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (93, '0406', 'SIGUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (94, '0407', 'EL BELALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (95, '0408', 'AIN BABOUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (96, '0409', 'BERRICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (97, '0410', 'OULED HAMLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (98, '0411', 'DHALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (99, '0412', 'AIN KERCHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (100, '0413', 'HANCHIR TOUMGHANI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (101, '0414', 'EL DJAZIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (102, '0415', 'AIN DISS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (103, '0416', 'FKIRINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (104, '0417', 'SOUK NAAMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (105, '0418', 'ZORG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (106, '0419', 'EL FEDJOUDJ.B.S');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (107, '0420', 'OULED ZOUAI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (108, '0421', 'BIR CHOUHADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (109, '0422', 'KSAR SBAHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (110, '0423', 'OUED NINI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (111, '0424', 'MESKIANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (112, '0425', 'AIN FAKROUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (113, '0426', 'RAHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (114, '0427', 'AIN ZITOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (115, '0428', 'OULED GACEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (116, '0429', 'EL HARMILIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (117, '0501', 'BATNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (118, '0502', 'GHASSIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (119, '0503', 'MAAFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (120, '0504', 'MEROUANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (121, '0505', 'SERIANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (122, '0506', 'MENAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (123, '0507', 'EL MADHER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (124, '0508', 'TAZOULT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (125, '0509', 'N''GAOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (126, '0510', 'GUIGBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (127, '0511', 'INOUGHISSEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (128, '0512', 'OUYOUN EL ASSAFIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (129, '0513', 'DJERMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (130, '0514', 'BITAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (131, '0515', 'METKAOUAK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (132, '0516', 'ARRIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (133, '0517', 'KIMMEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (134, '0518', 'TILATOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (135, '0519', 'AIN DJASSER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (136, '0520', 'OULED SELLAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (137, '0521', 'TIGHERGHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (138, '0522', 'AIN YAGOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (139, '0523', 'FESDIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (140, '0524', 'SEFIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (141, '0525', 'RAHBAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (142, '0526', 'TIGHANIMINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (143, '0527', 'LEMSANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (144, '0528', 'KSAR BELLEZMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (145, '0529', 'REGGANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (146, '0530', 'ICHMOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (147, '0531', 'FOUM TOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (148, '0532', 'BENI FOUDHALA E.H');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (149, '0533', 'OUED EL MA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (150, '0534', 'TALKHAMT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (151, '0535', 'BOUZINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (152, '0536', 'CHEMORA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (153, '0537', 'OUED CHAABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (154, '0538', 'TAXLENT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (155, '0539', 'GOSBAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (156, '0540', 'OULED AOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (157, '0541', 'BOUMAGUEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (158, '0542', 'BARIKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (159, '0543', 'DJEZZAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (160, '0544', 'T''KOUTT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (161, '0545', 'AIN TOUTA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (162, '0546', 'HIDOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (163, '0547', 'TENIET EL ABED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (164, '0548', 'OUED TAGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (165, '0549', 'OULED FADEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (166, '0550', 'TIMGAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (167, '0551', 'RAS EL AIOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (168, '0552', 'CHIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (169, '0553', 'OULED SI SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (170, '0554', 'ZANAT EL BEIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (171, '0555', 'AMDOUKAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (172, '0556', 'OULED AMMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (173, '0557', 'EL HASSI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (174, '0558', 'LAZROU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (175, '0559', 'BOUMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (176, '0560', 'BOULHILAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (177, '0561', 'LARBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (178, '0601', 'BEJAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (179, '0602', 'AMIZOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (180, '0603', 'FERRAOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (181, '0604', 'TAOURIRT IGHIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (182, '0605', 'CHELATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (183, '0606', 'TAMOKRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (184, '0607', 'TIMZRIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (185, '0608', 'SOUK EL THENINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (186, '0609', 'M''CISNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (187, '0610', 'THINABDHER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (188, '0611', 'TICHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (189, '0612', 'SEMAOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (190, '0613', 'KENDIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (191, '0614', 'TIFRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (192, '0615', 'IGHRAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (193, '0616', 'AMALOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (194, '0617', 'IGHIL ALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (195, '0618', 'IFELAIN ILMATHEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (196, '0619', 'TOUDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (197, '0620', 'DARGUINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (198, '0621', 'SIDI AYAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (199, '0622', 'AOKAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (200, '0623', 'BENI DJELLIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (201, '0624', 'ADEKAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (202, '0625', 'AKBOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (203, '0626', 'SEDDOUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (204, '0627', 'TAZMALT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (205, '0628', 'AIT RIZINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (206, '0629', 'CHEMINI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (207, '0630', 'SOUK OUFELLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (208, '0631', 'TASKRIOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (209, '0632', 'TIBANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (210, '0633', 'TALA HAMZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (211, '0634', 'BARBACHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (212, '0635', 'BENI KSILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (213, '0636', 'OUZALLAGUEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (214, '0637', 'BOUHAMZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (215, '0638', 'BENI MELIKECHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (216, '0639', 'SIDI AICH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (217, '0640', 'EL KSEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (218, '0641', 'MELBOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (219, '0642', 'AKFADOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (220, '0643', 'LEFLAYE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (221, '0644', 'KHERRATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (222, '0645', 'DRAA KAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (223, '0646', 'TAMRIDJET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (224, '0647', 'AIT SMAIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (225, '0648', 'BOUKHELIFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (226, '0649', 'TIZI N''BERBER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (227, '0650', 'BENI MAOUCH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (228, '0651', 'OUED GHIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (229, '0652', 'BOUDJELLIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (230, '0701', 'BISKRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (231, '0702', 'OUMACHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (232, '0703', 'BRANIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (233, '0704', 'CHETMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (234, '0705', 'OULED DJELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (235, '0706', 'RAS EL MIAAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (236, '0707', 'BESBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (237, '0708', 'SIDI KHALED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (238, '0709', 'DOUCEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (239, '0710', 'ECH-CHAIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (240, '0711', 'SIDI OKBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (241, '0712', 'M''CHOUNECHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (242, '0713', 'EL HAOUCH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (243, '0714', 'AIN NAGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (244, '0715', 'ZERIBET EL OUED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (245, '0716', 'EL FEIDH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (246, '0717', 'EL KANTARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (247, '0718', 'AIN ZAATOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (248, '0719', 'EL OUTAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (249, '0720', 'DJEMORAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (250, '0721', 'TOLGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (251, '0722', 'LIOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (252, '0723', 'LICHANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (253, '0724', 'OURLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (254, '0725', 'M''LILI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (255, '0726', 'FOUGHALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (256, '0727', 'BORDJ BEN AZZOUZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (257, '0728', 'MEZIRAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (258, '0729', 'BOUCHAGROUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (259, '0730', 'MEKHADMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (260, '0731', 'EL GHROUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (261, '0732', 'EL HADJAB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (262, '0733', 'KHANGUET SIDINADJI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (263, '0801', 'BECHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (264, '0802', 'ERG FERRADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (265, '0803', 'OULED KHOUDIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (266, '0804', 'MERIDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (267, '0805', 'TIMOUDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (268, '0806', 'LAHMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (269, '0807', 'BENI ABBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (270, '0808', 'BENI IKHLEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (271, '0809', 'MECHRAA HOUARI B');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (272, '0810', 'KENEDSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (273, '0811', 'IGLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (274, '0812', 'TABALBALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (275, '0813', 'TAGHIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (276, '0814', 'EL OUATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (277, '0815', 'BOUKAIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (278, '0816', 'MOGHEUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (279, '0817', 'ABADLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (280, '0818', 'KERZAZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (281, '0819', 'KSABI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (282, '0820', 'TAMTERT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (283, '0821', 'BENI OUNIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (284, '0901', 'BLIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (285, '0902', 'CHEBLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (286, '0903', 'BOUINAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (287, '0904', 'OUED EL ALLEUG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (288, '0905', 'TASSALA EL MERDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (289, '0906', 'OULED CHEBEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (290, '0907', 'OULED YAICH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (291, '0908', 'CHREA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (292, '0909', 'BIRTOUTA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (293, '0910', 'EL AFFROUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (294, '0911', 'CHIFFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (295, '0912', 'HAMMAM MELOUANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (296, '0913', 'BEN KHELLIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (297, '0914', 'SOUMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (298, '0915', 'SIDI MOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (299, '0916', 'MOUZAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (300, '0917', 'SOUHANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (301, '0918', 'MEFTAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (302, '0919', 'OULED SELAMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (303, '0920', 'BOUFARIK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (304, '0921', 'LARBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (305, '0922', 'OUED DJER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (306, '0923', 'BENI TAMOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (307, '0924', 'BOUARFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (308, '0925', 'BENI MERED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (309, '0926', 'BOUGARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (310, '0927', 'GUERROUAOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (311, '0928', 'AIN ROMANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (312, '0929', 'DJEBABRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (313, '1001', 'BOUIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (314, '1002', 'EL ASNAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (315, '1003', 'GUERROUMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (316, '1004', 'SOUK EL KHEMIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (317, '1005', 'KADIRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (318, '1006', 'HANIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (319, '1007', 'DIRAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (320, '1008', 'AIT LAAZIZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (321, '1009', 'TAGHZOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (322, '1010', 'RAOURAOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (323, '1011', 'MEZDOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (324, '1012', 'HAIZER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (325, '1013', 'LAKHDARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (326, '1014', 'MAALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (327, '1015', 'EL HACHIMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (328, '1016', 'AOMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (329, '1017', 'CHORFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (330, '1018', 'BORDJ OUKHRISS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (331, '1019', 'EL ADJIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (332, '1020', 'EL-HAKIMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (333, '1021', 'EL KHEBOUZIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (334, '1022', 'AHL EL KSAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (335, '1023', 'BOUDERBALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (336, '1024', 'ZBARBAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (337, '1025', 'AIN EL HADJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (338, '1026', 'DJEBAHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (339, '1027', 'AGHBALOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (340, '1028', 'TAGUEDIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (341, '1029', 'AIN TURK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (342, '1030', 'SAHARIDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (343, '1031', 'DECHMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (344, '1032', 'RIDANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (345, '1033', 'BECHLOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (346, '1034', 'BOUKRAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (347, '1035', 'AIN BESSAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (348, '1036', 'BIR GHBALOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (349, '1037', 'M''CHEDALLAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (350, '1038', 'SOUR EL GHOZLANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (351, '1039', 'MAAMORA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (352, '1040', 'OULED RACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (353, '1041', 'AIN LALOUI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (354, '1042', 'HADJERA ZERGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (355, '1043', 'ATH MANSOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (356, '1044', 'EL MOKRANI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (357, '1045', 'OUED EL BERDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (358, '1101', 'TAMANGHASSET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (359, '1102', 'ABALESSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (360, '1103', 'IN GHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (361, '1104', 'IN GUEZZAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (362, '1105', 'IDLES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (363, '1106', 'TAZOUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (364, '1107', 'TINZAOUATINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (365, '1108', 'IN SALAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (366, '1109', 'IN AMGUEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (367, '1110', 'FOGGARET EZZAOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (368, '1201', 'TEBESSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (369, '1202', 'BIR EL ATER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (370, '1203', 'CHERIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (371, '1204', 'STAH GUENTIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (372, '1205', 'EL AOUINET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (373, '1206', 'LAHOUIDJBET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (374, '1207', 'SAFSAF EL OUESRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (375, '1208', 'HAMMAMET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (376, '1209', 'NEGRINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (377, '1210', 'BIR EL MOKADEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (378, '1211', 'EL KOUIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (379, '1212', 'MORSOTT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (380, '1213', 'EL OGLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (381, '1214', 'BIR DHEHEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (382, '1215', 'EL OGLA EL MALHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (383, '1216', 'GORRIGUER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (384, '1217', 'BEKKARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (385, '1218', 'BOUKHADRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (386, '1219', 'OUENZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (387, '1220', 'EL MA EL BIODH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (388, '1221', 'OUM ALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (389, '1222', 'THLIDJENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (390, '1223', 'AIN ZERGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (391, '1224', 'EL MERIDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (392, '1225', 'BOULHAF DYR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (393, '1226', 'BEDJENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (394, '1227', 'EL MAZERAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (395, '1228', 'FERKANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (396, '1301', 'TLEMCEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (397, '1302', 'BENI MESTER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (398, '1303', 'AIN TALLOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (399, '1304', 'REMCHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (400, '1305', 'EL FEHOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (401, '1306', 'SABRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (402, '1307', 'GHAZAOUET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (403, '1308', 'SOUANI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (404, '1309', 'DJEBALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (405, '1310', 'EL GOR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (406, '1311', 'OUED CHOULY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (407, '1312', 'AIN FEZZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (408, '1313', 'OULED MIMOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (409, '1314', 'AMIEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (410, '1315', 'AIN YOUCEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (411, '1316', 'ZENATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (412, '1317', 'BENI SNOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (413, '1318', 'BAB EL ASSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (414, '1319', 'DAR YAGHMOURACENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (415, '1320', 'FELLAOUCENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (416, '1321', 'AZAILS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (417, '1322', 'SEBBAA CHIOUKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (418, '1323', 'TERNI BENI HEDIEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (419, '1324', 'BENSEKRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (420, '1325', 'AIN NEHALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (421, '1326', 'HENNAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (422, '1327', 'MAGHNIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (423, '1328', 'HAMMAM BOUGHRARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (424, '1329', 'SOUAHLIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (425, '1330', 'MSIRDA FOUAGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (426, '1331', 'AIN FETAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (427, '1332', 'EL ARICHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (428, '1333', 'SOUK THLATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (429, '1334', 'SIDI ABDELLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (430, '1335', 'SEBDOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (431, '1336', 'BENI OUARSOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (432, '1337', 'SIDI MEDJAHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (433, '1338', 'BENI BOUSSAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (434, '1339', 'MARSA BEN M''HIDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (435, '1340', 'NEDROMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (436, '1341', 'SIDI DJILLALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (437, '1342', 'BENI BAHDEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (438, '1343', 'EL BOUIHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (439, '1344', 'HONAINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (440, '1345', 'TIANET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (441, '1346', 'OULED RIYAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (442, '1347', 'BOUHLOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (443, '1348', 'SOUK EL KHEMIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (444, '1349', 'AIN GHORABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (445, '1350', 'CHETOUANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (446, '1351', 'MANSOURAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (447, '1352', 'BENI SEMIEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (448, '1353', 'AIN KEBIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (449, '1401', 'TIARET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (450, '1402', 'MEDROUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (451, '1403', 'AIN BOUCHEKIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (452, '1404', 'SIDI ALI MELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (453, '1405', 'AIN ZARIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (454, '1406', 'AIN DEHEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (455, '1407', 'SIDI BAKHTI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (456, '1408', 'MEDRISSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (457, '1409', 'ZMALET E.ABDELKADE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (458, '1410', 'MADNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (459, '1411', 'SEBT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (460, '1412', 'MELLAKOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (461, '1413', 'DAHMOUNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (462, '1414', 'RAHOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (463, '1415', 'MAHDIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (464, '1416', 'SOUGUEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (465, '1417', 'SIDI ABDELGHANI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (466, '1418', 'AIN EL HADID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (467, '1419', 'OULED DJERAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (468, '1420', 'NAIMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (469, '1421', 'MEGHILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (470, '1422', 'GUERTOUFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (471, '1423', 'SIDI HOSNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (472, '1424', 'DJILLALI BEN AMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (473, '1425', 'SEBAINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (474, '1426', 'TOUSNINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (475, '1427', 'FRENDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (476, '1428', 'AIN KERMES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (477, '1429', 'KSAR CHELLALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (478, '1430', 'RECHAIGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (479, '1431', 'NADORAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (480, '1432', 'TAGDEMT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (481, '1433', 'OUED LILLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (482, '1434', 'MECHRAA SAFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (483, '1435', 'HAMADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (484, '1436', 'CHEHAIMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (485, '1437', 'TAKHEMARET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (486, '1438', 'SIDI ABDERRAHMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (487, '1439', 'SERGHINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (488, '1440', 'BOUGARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (489, '1441', 'FAIDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (490, '1442', 'TIDDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (491, '1501', 'TIZI OUZOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (492, '1502', 'AIN EL HAMMAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (493, '1503', 'AKBIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (494, '1504', 'FREHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (495, '1505', 'SOUAMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (496, '1506', 'MECHTRASS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (497, '1507', 'IRDJEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (498, '1508', 'TIMIZART');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (499, '1509', 'MAKOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (500, '1510', 'DRAA EL MIZAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (501, '1511', 'TIZI GHENIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (502, '1512', 'BOUNOUH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (503, '1513', 'AIT CHAFFAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (504, '1514', 'FRIKAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (505, '1515', 'BENI AISSI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (506, '1516', 'BENI ZMENZER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (507, '1517', 'IFERHOUNENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (508, '1518', 'AZAZGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (509, '1519', 'ILOULA OUMALOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (510, '1520', 'YAKOUREN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (511, '1521', 'LARBA NAIT IRATHEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (512, '1522', 'TIZI RACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (513, '1523', 'ZEKRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (514, '1524', 'OUAGUENOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (515, '1525', 'AIN ZAOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (516, '1526', 'M''KIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (517, '1527', 'AIT YAHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (518, '1528', 'AIT MAHMOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (519, '1529', 'MAATKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (520, '1530', 'AIT BOUMEHDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (521, '1531', 'ABI YOUCEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (522, '1532', 'BENI DOUALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (523, '1533', 'ILLILTEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (524, '1534', 'BOUZGUEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (525, '1535', 'AIT AGGOUACHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (526, '1536', 'OUADHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (527, '1537', 'AZZEFOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (528, '1538', 'TIGZIRT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (529, '1539', 'AIT AISSA MIMOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (530, '1540', 'BOGHNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (531, '1541', 'IFIGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (532, '1542', 'AIT OUMALOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (533, '1543', 'TIRMITINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (534, '1544', 'AKERROU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (535, '1545', 'YATAFEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (536, '1546', 'BENI ZIKI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (537, '1547', 'DRA BEN KHEDDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (538, '1548', 'OUACIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (539, '1549', 'IDJEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (540, '1550', 'MEKLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (541, '1551', 'TIZI N''THLATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (542, '1552', 'BENI YENNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (543, '1553', 'AGHRIB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (544, '1554', 'IFLISSEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (545, '1555', 'BOUDJIMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (546, '1556', 'AIT YAHIA MOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (547, '1557', 'SOUK EL THENINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (548, '1558', 'AIT KHELIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (549, '1559', 'SIDI NAAMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (550, '1560', 'IBOUDRAREN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (551, '1561', 'AGHNI GOUGHRAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (552, '1562', 'MIZRANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (553, '1563', 'IMSOUHAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (554, '1564', 'TADMAIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (555, '1565', 'AIT BOUADOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (556, '1566', 'ASSI YOUCEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (557, '1567', 'AIT TOUDERT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (558, '1601', 'ALGER CENTRE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (559, '1602', 'SIDI M''HAMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (560, '1603', 'EL MADANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (561, '1604', 'HAMMA ANASSERS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (562, '1605', 'BAB EL OUED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (563, '1606', 'BOLOGHINE IBN ZIRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (564, '1607', 'CASBAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (565, '1608', 'OUED KORICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (566, '1609', 'BIR MOURAD RAIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (567, '1610', 'EL BIAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (568, '1611', 'BOUZAREAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (569, '1612', 'BIRKHADEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (570, '1613', 'EL HARRACH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (571, '1614', 'BARAKI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (572, '1615', 'OUED SMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (573, '1616', 'BOUROUBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (574, '1617', 'HUSSEIN DEY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (575, '1618', 'KOUBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (576, '1619', 'BACHEDJERAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (577, '1620', 'DAR EL BEIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (578, '1621', 'BAB AZZOUAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (579, '1622', 'BEN AKNOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (580, '1623', 'DELY IBRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (581, '1624', 'BAINS ROMAINS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (582, '1625', 'RAIS HAMIDOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (583, '1626', 'DJASR KASENTINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (584, '1627', 'EL MOURADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (585, '1628', 'HYDRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (586, '1629', 'MOHAMMADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (587, '1630', 'BORDJ EL KIFFAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (588, '1631', 'EL MAGHARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (589, '1632', 'BENI MESSOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (590, '1633', 'LES EUCALYPTUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (591, '1701', 'DJELFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (592, '1702', 'MOUADJEBAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (593, '1703', 'EL GUEDID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (594, '1704', 'HASSI BAHBAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (595, '1705', 'AIN MAABED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (596, '1706', 'SED RAHAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (597, '1707', 'FAIDH EL BOTMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (598, '1708', 'BIRINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (599, '1709', 'BOUIRA LAHDAB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (600, '1710', 'ZACCAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (601, '1711', 'EL KHEMIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (602, '1712', 'SIDI BAIZID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (603, '1713', 'M''LILIHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (604, '1714', 'EL IDRISSIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (605, '1715', 'DOUIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (606, '1716', 'HASSI EL EUCH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (607, '1717', 'MESSAAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (608, '1718', 'GUETTARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (609, '1719', 'SIDI LADJEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (610, '1720', 'HAD SAHARY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (611, '1721', 'GUERNINI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (612, '1722', 'SELMANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (613, '1723', 'AIN CHOUHADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (614, '1724', 'OUM LAADHAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (615, '1725', 'DAR CHIOUKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (616, '1726', 'CHAREF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (617, '1727', 'BENI YAGOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (618, '1728', 'ZAAFRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (619, '1729', 'DELDOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (620, '1730', 'AIN EL IBEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (621, '1731', 'AIN OUSSERA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (622, '1732', 'BENHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (623, '1733', 'FASSI FEDOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (624, '1734', 'AMOURAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (625, '1735', 'AIN FEKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (626, '1736', 'TADMIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (627, '1801', 'JIJEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (628, '1802', 'ERRAGUENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (629, '1803', 'EL AOUANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (630, '1804', 'ZIAMMA MANSOURIAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (631, '1805', 'TAHER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (632, '1806', 'EMIR ABDELKADER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (633, '1807', 'CHEKFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (634, '1808', 'CHAHNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (635, '1809', 'EL MILIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (636, '1810', 'SIDI MAAROUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (637, '1811', 'SETTARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (638, '1812', 'EL ANCER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (639, '1813', 'SIDI ABDELAZIZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (640, '1814', 'KAOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (641, '1815', 'GHEBALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (642, '1816', 'BOURAOUI BELHADEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (643, '1817', 'DJMILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (644, '1818', 'SELMA BENZIADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (645, '1819', 'BOUSSIF O.AKSEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (646, '1820', 'EL KENNAR NOUCHFI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (647, '1821', 'OULED YAHIA KHADR.');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (648, '1822', 'BOUDRIA BNI YADJIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (649, '1823', 'KEMIR OUED ADJOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (650, '1824', 'TEXENA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (651, '1825', 'DJEMAA BENI HABIBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (652, '1826', 'BORDJ TAHER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (653, '1827', 'OULED RABAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (654, '1828', 'OUADJANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (655, '1901', 'SETIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (656, '1902', 'AIN EL KEBIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (657, '1903', 'BENI AZIZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (658, '1904', 'OULED SIDI AHMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (659, '1905', 'BOUTALEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (660, '1906', 'AIN ROUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (661, '1907', 'DRAA KEBILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (662, '1908', 'BIR EL ARCH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (663, '1909', 'BENI CHEBANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (664, '1910', 'OULED TEBBEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (665, '1911', 'HAMMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (666, '1912', 'MAAOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (667, '1913', 'AIN LEGRAJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (668, '1914', 'AIN ABESSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (669, '1915', 'DEHAMCHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (670, '1916', 'BABOR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (671, '1917', 'GUIDJEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (672, '1918', 'AIN LAHDJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (673, '1919', 'BOUSSELAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (674, '1920', 'EL EULMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (675, '1921', 'DJEMILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (676, '1922', 'BENI OUARTILANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (677, '1923', 'ROSFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (678, '1924', 'OULED ADDOUANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (679, '1925', 'BELAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (680, '1926', 'AIN ARNAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (681, '1927', 'AMOUCHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (682, '1928', 'AIN OULMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (683, '1929', 'BEIDHA BORDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (684, '1930', 'BOUANDAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (685, '1931', 'BAZER SAKHRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (686, '1932', 'HAMMAM ESSOKHNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (687, '1933', 'MEZLOUG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (688, '1934', 'BIR HADDADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (689, '1935', 'SERDJ EL GHOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (690, '1936', 'HARBIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (691, '1937', 'EL OURICIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (692, '1938', 'TIZI N''BECHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (693, '1939', 'SALAH BEY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (694, '1940', 'AIN AZAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (695, '1941', 'GUENZET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (696, '1942', 'TALAIFACENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (697, '1943', 'BOUGAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (698, '1944', 'BENI FOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (699, '1945', 'TACHOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (700, '1946', 'BENI MOUHLI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (701, '1947', 'OULED SABOR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (702, '1948', 'GUELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (703, '1949', 'AIN SEBT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (704, '1950', 'HAMMAM GUERGOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (705, '1951', 'AIT NAOUAL MEZADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (706, '1952', 'KSAR EL ABTAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (707, '1953', 'BENI HOCINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (708, '1954', 'AIT TIZI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (709, '1955', 'MAOUKLANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (710, '1956', 'GUELTA ZERKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (711, '1957', 'OUED EL BARAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (712, '1958', 'TAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (713, '1959', 'EL OULDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (714, '1960', 'TELLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (715, '2001', 'SAIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (716, '2002', 'DOUI THABET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (717, '2003', 'AIN EL HADJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (718, '2004', 'OULED KHALED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (719, '2005', 'MOULAY LARBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (720, '2006', 'YOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (721, '2007', 'HOUNET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (722, '2008', 'SIDI AMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (723, '2009', 'SIDI BOUBEKEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (724, '2010', 'EL HASSASNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (725, '2011', 'MAAMORA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (726, '2012', 'SIDI AHMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (727, '2013', 'AIN SEKHOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (728, '2014', 'OULED BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (729, '2015', 'TIRCINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (730, '2016', 'AIN SOLTANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (731, '2101', 'SKIKDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (732, '2102', 'AIN ZOUIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (733, '2103', 'EL HADAIK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (734, '2104', 'AZZABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (735, '2105', 'DJENDEL S. MOHAMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (736, '2106', 'AIN CHERCHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (737, '2107', 'BEKKOUCHE LAKHDAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (738, '2108', 'BENAZOUZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (739, '2109', 'ES-SEBT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (740, '2110', 'COLLO');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (741, '2111', 'BENI ZID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (742, '2112', 'KERKERA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (743, '2113', 'OULED ATTIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (744, '2114', 'OUED ZEHOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (745, '2115', 'ZITOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (746, '2116', 'EL HARROUCH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (747, '2117', 'ZERDAZAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (748, '2118', 'OULED HEBABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (749, '2119', 'SIDI MEZGHICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (750, '2120', 'EMDJEZ EDCHICH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (751, '2121', 'BENI OULBANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (752, '2122', 'AIN BOUZIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (753, '2123', 'RAMDANE DJAMEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (754, '2124', 'BENI BACHIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (755, '2125', 'SALAH BOUCHAOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (756, '2126', 'TAMALOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (757, '2127', 'AIN KECHRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (758, '2128', 'OUM TOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (759, '2129', 'BEIN EL OUIDEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (760, '2130', 'FIL FILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (761, '2131', 'CHERAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (762, '2132', 'KANOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (763, '2133', 'EL GHEDIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (764, '2134', 'BOUCHTATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (765, '2135', 'OULDJA BOULBALOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (766, '2136', 'KHENEG MAYOUM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (767, '2137', 'HAMADI KROUMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (768, '2138', 'EL MARSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (769, '2201', 'SIDI BEL ABBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (770, '2202', 'TESSALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (771, '2203', 'SIDI BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (772, '2204', 'MOSTEFA BEN BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (773, '2205', 'TELAGH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (774, '2206', 'MEZAOUROU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (775, '2207', 'BOUKHANAFIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (776, '2208', 'SIDI ALI BOUSSIDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (777, '2209', 'BADREDINE EL MOKR.');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (778, '2210', 'MARHOUM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (779, '2211', 'TAFISSOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (780, '2212', 'AMARNAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (781, '2213', 'TILMOUNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (782, '2214', 'SIDI LAHCENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (783, '2215', 'AIN THRID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (784, '2216', 'MAKEDRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (785, '2217', 'TENIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (786, '2218', 'MOULAY SLISSEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (787, '2219', 'EL HACAIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (788, '2220', 'HASSI ZEHANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (789, '2221', 'TABIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (790, '2222', 'MERINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (791, '2223', 'RAS EL MA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (792, '2224', 'AIN TINDAMINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (793, '2225', 'AIN KADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (794, '2226', 'M''CID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (795, '2227', 'SIDI KHALED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (796, '2228', 'AIN EL BERD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (797, '2229', 'SFISSEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (798, '2230', 'AIN ADDEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (799, '2231', 'OUED TAOURIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (800, '2232', 'DHAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (801, '2233', 'ZEROUALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (802, '2234', 'LAMTAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (803, '2235', 'SIDI CHAIB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (804, '2236', 'SIDI DAHOU DEZAIRS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (805, '2237', 'OUED SBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (806, '2238', 'BOUDJEBAA EL BORDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (807, '2239', 'SEHALA THAOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (808, '2240', 'SIDI YACOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (809, '2241', 'SIDI HAMADOUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (810, '2242', 'BELARBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (811, '2243', 'OUED SEFIOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (812, '2244', 'TEGHALIMET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (813, '2245', 'BEN BADIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (814, '2246', 'SIDI ALI BENYOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (815, '2247', 'CHETOUANE BELAILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (816, '2248', 'BIR EL HAMMAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (817, '2249', 'TAOUDMOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (818, '2250', 'REDJEM DEMOUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (819, '2251', 'BENACHIBA CHELIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (820, '2252', 'HASSI DAHOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (821, '2301', 'ANNABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (822, '2302', 'BERRAHEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (823, '2303', 'EL HADJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (824, '2304', 'EULMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (825, '2305', 'EL BOUNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (826, '2306', 'OUED EL ANEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (827, '2307', 'CHEURFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (828, '2308', 'SERAIDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (829, '2309', 'AIN BERDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (830, '2310', 'CHETAIBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (831, '2311', 'SIDI AMER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (832, '2312', 'TREAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (833, '2401', 'GUELMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (834, '2402', 'NECHMAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (835, '2403', 'BOUATI MAHMOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (836, '2404', 'OUED ZENATI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (837, '2405', 'TAMLOUKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (838, '2406', 'OUED FRAGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (839, '2407', 'AIN SANDEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (840, '2408', 'RAS EL AGBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (841, '2409', 'DAHOUARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (842, '2410', 'BELKHIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (843, '2411', 'BEN DJARAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (844, '2412', 'BOU HAMDANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (845, '2413', 'AIN MAKHLOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (846, '2414', 'AIN BEN BEIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (847, '2415', 'KHEZARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (848, '2416', 'BENI MEZLINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (849, '2417', 'BOU HACHANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (850, '2418', 'GUELAAT BOU SBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (851, '2419', 'HAMMAM MASKHOUTINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (852, '2420', 'EL FEDJOUDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (853, '2421', 'BORDJ SABAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (854, '2422', 'HAMMAN N''BAIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (855, '2423', 'AIN LARBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (856, '2424', 'MEDJEZ AMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (857, '2425', 'BOUCHEGOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (858, '2426', 'HELIOPOLIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (859, '2427', 'AIN HESSANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (860, '2428', 'ROKNIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (861, '2429', 'SALAOUA ANNOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (862, '2430', 'MEDJEZ SFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (863, '2431', 'BOUMAHRA AHMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (864, '2432', 'AIN REGGADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (865, '2433', 'OUED CHEHAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (866, '2434', 'DJEBALLAH KHEMISSI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (867, '2501', 'CONSTANTINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (868, '2502', 'HAMMA BOUZIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (869, '2503', 'EL HARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (870, '2504', 'ZIGHOUD YOUCEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (871, '2505', 'DIDOUCHE MOURAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (872, '2506', 'EL KHROUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (873, '2507', 'AIN ABID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (874, '2508', 'BENI HAMIDEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (875, '2509', 'OULED RAHMOUNE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (876, '2510', 'AIN SMARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (877, '2511', 'MESAOUD BOUDJERIOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (878, '2512', 'IBN ZIAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (879, '2601', 'MEDEA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (880, '2602', 'OUZERA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (881, '2603', 'OULED MAAREF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (882, '2604', 'AIN BOUCIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (883, '2605', 'AISSAOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (884, '2606', 'OULED DEIDE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (885, '2607', 'EL OMARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (886, '2608', 'DERRAG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (887, '2609', 'EL GUELBELKEBIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (888, '2610', 'BOUAICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (889, '2611', 'MEZERENA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (890, '2612', 'OULED BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (891, '2613', 'DAMIAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (892, '2614', 'SIDI ZIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (893, '2615', 'TAMESGUIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (894, '2616', 'EL HAMDANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (895, '2617', 'KEF LAKHDAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (896, '2618', 'CHELALET EL A.');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (897, '2619', 'BOUSKENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (898, '2620', 'REBAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (899, '2621', 'BOUCHRAHIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (900, '2622', 'OULED HELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (901, '2623', 'TAFRAOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (902, '2624', 'BAATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (903, '2625', 'BOGHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (904, '2626', 'SIDI NAAMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (905, '2627', 'OULED BOUACHRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (906, '2628', 'SIDI ZAHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (907, '2629', 'OUED HARBIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (908, '2630', 'BENCHICAO');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (909, '2631', 'SIDI DAMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (910, '2632', 'AZIZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (911, '2633', 'SOUAGUI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (912, '2634', 'ZOUBIRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (913, '2635', 'KSAR EL BOUKHARI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (914, '2636', 'EL AZIZIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (915, '2637', 'DJOUAB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (916, '2638', 'CHAHBOUNIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (917, '2639', 'MEGHRAOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (918, '2640', 'CHENIGUEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (919, '2641', 'AIN OU KSIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (920, '2642', 'OUM EL DJALIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (921, '2643', 'OUAMRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (922, '2644', 'SI MAHDJOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (923, '2645', 'TLATET EDDOAIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (924, '2646', 'BENI SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (925, '2647', 'BERROUAGHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (926, '2648', 'SEGHOUANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (927, '2649', 'MEFTAHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (928, '2650', 'MIHOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (929, '2651', 'BOUGHEZOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (930, '2652', 'TABLAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (931, '2653', 'DEUX BASSINS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (932, '2654', 'DRAA ESSAMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (933, '2655', 'SIDI ERRABIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (934, '2656', 'BIR BEN LAABED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (935, '2657', 'EL OUINET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (936, '2658', 'OULED ANTAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (937, '2659', 'BOUAICHOUNE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (938, '2660', 'HANNACHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (939, '2661', 'SEDRAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (940, '2662', 'MEDJEBAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (941, '2663', 'KHAMS DJOUAMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (942, '2664', 'SANEG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (943, '2701', 'MOSTAGANEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (944, '2702', 'SAYADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (945, '2703', 'FORNAKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (946, '2704', 'STIDIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (947, '2705', 'AIN NOUISSY');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (948, '2706', 'HASSI MAAMECHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (949, '2707', 'AIN TADLES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (950, '2708', 'SOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (951, '2709', 'OUED EL KHEIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (952, '2710', 'SIDI BELLATER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (953, '2711', 'KHEIREDINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (954, '2712', 'SIDI ALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (955, '2713', 'ABDELMALEK RAMDANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (956, '2714', 'HADJADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (957, '2715', 'NEKMARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (958, '2716', 'SIDI LAKHDAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (959, '2717', 'ACHAACHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (960, '2718', 'KHADRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (961, '2719', 'BOUGUIRAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (962, '2720', 'SIRAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (963, '2721', 'AIN SIDI CHERIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (964, '2722', 'MESRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (965, '2723', 'MANSOURAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (966, '2724', 'SOUAFLIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (967, '2725', 'OULED BOUGHALEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (968, '2726', 'OULED MAALLAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (969, '2727', 'MEZGHRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (970, '2728', 'AIN BOUDINAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (971, '2729', 'TAZGAIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (972, '2730', 'SAFSAF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (973, '2731', 'TOUAHRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (974, '2732', 'EL HASSIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (975, '2801', 'M''SILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (976, '2802', 'MAADID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (977, '2803', 'HAMMAM DHALAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (978, '2804', 'OULED DERRADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (979, '2805', 'TARMOUNT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (980, '2806', 'M''TARFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (981, '2807', 'KHOUBANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (982, '2808', 'M''CIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (983, '2809', 'CHELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (984, '2810', 'OULED MADHI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (985, '2811', 'MAGRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (986, '2812', 'BERHOUM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (987, '2813', 'AIN KHADRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (988, '2814', 'OULED ADDI GUEBALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (989, '2815', 'BELAIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (990, '2816', 'SIDI AISSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (991, '2817', 'AIN EL HADJEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (992, '2818', 'SIDI HADJERES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (993, '2819', 'OUANOUGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (994, '2820', 'BOU SAADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (995, '2821', 'OULED SIDI BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (996, '2822', 'SIDI AMEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (997, '2823', 'TAMSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (998, '2824', 'BEN SROUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (999, '2825', 'OULED SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1000, '2826', 'EL HOUAMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1001, '2827', 'EL HAMEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1002, '2828', 'OULED MANSOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1003, '2829', 'MAARIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1004, '2830', 'DEHAHNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1005, '2831', 'BOUTI SAYAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1006, '2832', 'KHETTOUTI SED-DJIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1007, '2833', 'ZARZOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1008, '2834', 'OUED CHAIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1009, '2835', 'BENZOUH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1010, '2836', 'BIR FODA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1011, '2837', 'AIN FARES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1012, '2838', 'SIDI M''HAMED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1013, '2839', 'OULED ATIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1014, '2840', 'SOUAMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1015, '2841', 'AIN EL MELH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1016, '2842', 'MEDJEDEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1017, '2843', 'SLIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1018, '2844', 'AIN ERRICH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1019, '2845', 'BENI ILMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1020, '2846', 'OULTENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1021, '2847', 'DJEBEL MESSAAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1022, '2901', 'MASCARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1023, '2902', 'BOU HANIFIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1024, '2903', 'TIZI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1025, '2904', 'HACINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1026, '2905', 'MAOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1027, '2906', 'TEGHENNIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1028, '2907', 'EL HACHEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1029, '2908', 'SIDI KADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1030, '2909', 'ZELMATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1031, '2910', 'OUED EL ABTAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1032, '2911', 'AIN FERAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1033, '2912', 'GHRISS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1034, '2913', 'FROHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1035, '2914', 'MATEMORE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1036, '2915', 'MAKDHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1037, '2916', 'SIDI BOUSSAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1038, '2917', 'EL BORDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1039, '2918', 'AIN FEKAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1040, '2919', 'BENIAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1041, '2920', 'KHALOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1042, '2921', 'EL MENAOUER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1043, '2922', 'OUED TARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1044, '2923', 'AOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1045, '2924', 'AIN FARES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1046, '2925', 'AIN FRASS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1047, '2926', 'SIG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1048, '2927', 'OGGAZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1049, '2928', 'ALAIMIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1050, '2929', 'EL GAADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1051, '2930', 'ZAHANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1052, '2931', 'MOHAMMADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1053, '2932', 'SIDI ABDELMOUMENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1054, '2933', 'FERRAGUIG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1055, '2934', 'EL GHOMRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1056, '2935', 'SEDJERARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1057, '2936', 'MOCTADOUZ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1058, '2937', 'BOU HENNI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1059, '2938', 'GUETTENA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1060, '2939', 'EL MAMOUNIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1061, '2940', 'EL KEURT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1062, '2941', 'GHARROUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1063, '2942', 'GHERDJOUM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1064, '2943', 'CHORFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1065, '2944', 'RAS AIN AMIROUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1066, '2945', 'NESMOT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1067, '2946', 'SIDI ABDELDJEBAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1068, '2947', 'SEHAILIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1069, '3001', 'OUARGLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1070, '3002', 'AIN BEIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1071, '3003', 'N''GOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1072, '3004', 'HASSI MESSAOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1073, '3005', 'ROUISSAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1074, '3006', 'BALIDAT AMEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1075, '3007', 'TEBESBEST');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1076, '3008', 'NEZLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1077, '3009', 'ZAOUIA EL ABIDIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1078, '3010', 'SIDI SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1079, '3011', 'SIDI KHOUILED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1080, '3012', 'HASSI BEN ABDELLAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1081, '3013', 'TOUGGOURT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1082, '3014', 'EL HADJIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1083, '3015', 'TAIBET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1084, '3016', 'TAMACINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1085, '3017', 'BENACEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1086, '3018', 'M''NAGUER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1087, '3019', 'MEGARINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1088, '3020', 'EL ALLIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1089, '3021', 'EL BORMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1090, '3101', 'ORAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1091, '3102', 'GDYEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1092, '3103', 'BIR EL DJIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1093, '3104', 'HASSI BOUNIF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1094, '3105', 'ES SENIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1095, '3106', 'ARZEW');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1096, '3107', 'BETHIOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1097, '3108', 'MARSAT EL HADJADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1098, '3109', 'AIN TURK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1099, '3110', 'EL ANCAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1100, '3111', 'OUED TLELAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1101, '3112', 'TAFRAOUI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1102, '3113', 'SIDI CHAMI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1103, '3114', 'BOUFATIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1104, '3115', 'MERS EL KEBIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1105, '3116', 'BOUSFER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1106, '3117', 'EL KARMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1107, '3118', 'EL BRAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1108, '3119', 'HASSI BEN OKBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1109, '3120', 'BEN FREHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1110, '3121', 'HASSI MEFSOUKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1111, '3122', 'SIDI BEN YABKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1112, '3123', 'MESSERGHIN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1113, '3124', 'BOUTLELIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1114, '3125', 'AIN KERMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1115, '3126', 'AIN BIYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1116, '3201', 'EL BAYADH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1117, '3202', 'ROGASSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1118, '3203', 'STITTEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1119, '3204', 'BREZINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1120, '3205', 'GHASSOUL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1121, '3206', 'BOUALEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1122, '3207', 'EL ABIODH S.CHEIKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1123, '3208', 'AIN EL ORAK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1124, '3209', 'ARBAOUAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1125, '3210', 'BOUGTOUB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1126, '3211', 'EL KHEITHER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1127, '3212', 'KEF EL AHMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1128, '3213', 'BOUSSEMGHOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1129, '3214', 'CHELLALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1130, '3215', 'KRAKDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1131, '3216', 'EL BNOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1132, '3217', 'CHEGUIG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1133, '3218', 'SIDI AMEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1134, '3219', 'EL MEHARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1135, '3220', 'TOUSMOULINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1136, '3221', 'SIDI SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1137, '3222', 'SIDI TIFOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1138, '3301', 'ILLIZI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1139, '3302', 'DJANET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1140, '3303', 'DEBDEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1141, '3304', 'BORDJ OMAR DRISS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1142, '3305', 'BORDJ EL HAOUASSE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1143, '3306', 'IN AMENAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1144, '3401', 'BORDJ BOU ARRERIDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1145, '3402', 'RAS EL OUED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1146, '3403', 'BORDJ ZEMOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1147, '3404', 'MANSOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1148, '3405', 'EL M''HIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1149, '3406', 'BEN DAOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1150, '3407', 'EL ACHIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1151, '3408', 'AIN TAGHROUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1152, '3409', 'BORDJ GHDIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1153, '3410', 'SIDI EMBAREK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1154, '3411', 'EL HAMADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1155, '3412', 'BELIMOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1156, '3413', 'MEDJANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1157, '3414', 'TENIET EN NASR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1158, '3415', 'DJAAFRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1159, '3416', 'EL MAIN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1160, '3417', 'OULED BRAHEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1161, '3418', 'OULED DAHMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1162, '3419', 'HASNAOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1163, '3420', 'KHELIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1164, '3421', 'TAGLAIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1165, '3422', 'KSOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1166, '3423', 'OULED SIDI BRAHIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1167, '3424', 'TAFREG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1168, '3425', 'COLLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1169, '3426', 'TIXTER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1170, '3427', 'EL ACH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1171, '3428', 'EL ANSEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1172, '3429', 'TESMART');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1173, '3430', 'AIN TESRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1174, '3431', 'BIR KASDALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1175, '3432', 'GHILASSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1176, '3433', 'RABTA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1177, '3434', 'HARAZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1178, '3501', 'BOUMERDES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1179, '3502', 'BOUDOUAOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1180, '3503', 'ROUIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1181, '3504', 'AFIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1182, '3505', 'BORDJ MENAIEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1183, '3506', 'BAGHLIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1184, '3507', 'SIDI DAOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1185, '3508', 'NACIRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1186, '3509', 'DJINET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1187, '3510', 'ISSER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1188, '3511', 'ZEMMOURI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1189, '3512', 'SI MUSTAPHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1190, '3513', 'TIDJELABINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1191, '3514', 'CHABET EL AMEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1192, '3515', 'THENIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1193, '3516', 'REGHAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1194, '3517', 'AIN TAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1195, '3518', 'TIMEZRIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1196, '3519', 'CORSO');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1197, '3520', 'OULED MOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1198, '3521', 'LARBATACHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1199, '3522', 'BOUZEGZA KEDDARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1200, '3523', 'BORDJ EL BAHRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1201, '3524', 'MARSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1202, '3525', 'TAOURGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1203, '3526', 'OULED AISSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1204, '3527', 'BEN CHOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1205, '3528', 'DELLYS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1206, '3529', 'AMMAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1207, '3530', 'BENI AMRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1208, '3531', 'SOUK EL HAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1209, '3532', 'BOUDOUAOU EL BAHRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1210, '3533', 'OULED HEDADJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1211, '3534', 'HARAOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1212, '3535', 'LAGHATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1213, '3536', 'HAMMEDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1214, '3537', 'KHEMIS EL KHECHNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1215, '3538', 'EL KHARROUBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1216, '3601', 'EL TARF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1217, '3602', 'BOUHADJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1218, '3603', 'BEN M''HIDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1219, '3604', 'BOUGOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1220, '3605', 'EL KALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1221, '3606', 'AIN EL ASSEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1222, '3607', 'EL AIOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1223, '3608', 'BOUTELDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1224, '3609', 'SOUAREKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1225, '3610', 'BERRIHANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1226, '3611', 'LAC DES OISEAUX');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1227, '3612', 'CHEFIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1228, '3613', 'DREAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1229, '3614', 'CHIHANI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1230, '3615', 'CHEBAITA MOKHTAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1231, '3616', 'BESBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1232, '3617', 'ASFOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1233, '3618', 'ECHATT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1234, '3619', 'ZERIZER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1235, '3620', 'ZITOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1236, '3621', 'AIN KERMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1237, '3622', 'OUED ZITOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1238, '3623', 'HAMMAM BENI SALAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1239, '3624', 'RAML SOUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1240, '3701', 'TINDOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1241, '3702', 'OUM EL ASSEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1242, '3801', 'TISSEMSILT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1243, '3802', 'BORDJ BOU NAAMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1244, '3803', 'THENIET EL HAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1245, '3804', 'LAZHARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1246, '3805', 'BENI CHAIB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1247, '3806', 'LARDJEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1248, '3807', 'MELAAB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1249, '3808', 'SIDI LANTRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1250, '3809', 'BORDJ E.ABDELKADER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1251, '3810', 'LAYOUNE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1252, '3811', 'KHEMISTI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1253, '3812', 'OULED BESSEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1254, '3813', 'AMMARI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1255, '3814', 'YOUSSOUFIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1256, '3815', 'SIDI BOUTOUCHENT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1257, '3816', 'LARBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1258, '3817', 'MAASEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1259, '3818', 'SIDI ABED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1260, '3819', 'TAMALAHT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1261, '3820', 'SIDI SLIMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1262, '3821', 'BOUCAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1263, '3822', 'BENI LAHCENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1264, '3901', 'EL OUED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1265, '3902', 'ROBBAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1266, '3903', 'OUED EL ALENDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1267, '3904', 'BAYADHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1268, '3905', 'NAKHLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1269, '3906', 'GUEMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1270, '3907', 'KOUININE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1271, '3908', 'REGUIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1272, '3909', 'HAMRAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1273, '3910', 'TAGHZOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1274, '3911', 'DEBILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1275, '3912', 'HASSANI ABDELKRIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1276, '3913', 'HASSI KHELIFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1277, '3914', 'TALEB LARBI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1278, '3915', 'DOUAR EL MA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1279, '3916', 'SIDI AOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1280, '3917', 'TRIFAOUI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1281, '3918', 'MAGRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1282, '3919', 'BENI GUECHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1283, '3920', 'OURMAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1284, '3921', 'STILL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1285, '3922', 'M''RARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1286, '3923', 'SIDI KHELLIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1287, '3924', 'TENDLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1288, '3925', 'EL OGLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1289, '3926', 'MIH OUANSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1290, '3927', 'EL M''GHAIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1291, '3928', 'DJAMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1292, '3929', 'OUM TOUYOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1293, '3930', 'SIDI AMRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1294, '4001', 'KHENCHELA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1295, '4002', 'M''TOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1296, '4003', 'KAIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1297, '4004', 'BAGHAI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1298, '4005', 'EL HAMMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1299, '4006', 'AIN TOUILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1300, '4007', 'TAOUZIANAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1301, '4008', 'BOUHMAMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1302, '4009', 'EL OUELDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1303, '4010', 'REMILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1304, '4011', 'CHERCHAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1305, '4012', 'DJELLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1306, '4013', 'BABAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1307, '4014', 'TAMZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1308, '4015', 'ENSIGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1309, '4016', 'OULED RECHACHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1310, '4017', 'EL MAHMAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1311, '4018', 'M''SARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1312, '4019', 'YABOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1313, '4020', 'KHIRANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1314, '4021', 'CHELIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1315, '4101', 'SOUK AHRAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1316, '4102', 'SEDRATA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1317, '4103', 'HANANCHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1318, '4104', 'MECHROHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1319, '4105', 'OULED DRISS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1320, '4106', 'TIFFECH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1321, '4107', 'ZAAROURIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1322, '4108', 'TAOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1323, '4109', 'DREA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1324, '4110', 'HADDADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1325, '4111', 'KHEDARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1326, '4112', 'MERAHNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1327, '4113', 'OULED MOUMEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1328, '4114', 'BIR BOUHOUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1329, '4115', 'M''DAOUROUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1330, '4116', 'OUM EL ADHAIM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1331, '4117', 'AIN ZANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1332, '4118', 'AIN SOLTANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1333, '4119', 'QUILLEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1334, '4120', 'SIDI FREDJ');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1335, '4121', 'SAFEL EL OUIDEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1336, '4122', 'RAGOUBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1337, '4123', 'KHEMISSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1338, '4124', 'OUED KEBERIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1339, '4125', 'TERRAGUELT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1340, '4126', 'ZOUABI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1341, '4201', 'TIPAZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1342, '4202', 'MENACEUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1343, '4203', 'LARHAT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1344, '4204', 'DOUAOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1345, '4205', 'BOURKIKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1346, '4206', 'KHEMISTI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1347, '4207', 'ZERALDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1348, '4208', 'SAOULA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1349, '4209', 'MAHELMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1350, '4210', 'AGHBAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1351, '4211', 'BABA HASSEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1352, '4212', 'HADJOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1353, '4213', 'SIDI AMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1354, '4214', 'GOURAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1355, '4215', 'NODOR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1356, '4216', 'CHAIBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1357, '4217', 'AIN TAGOURAIT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1358, '4218', 'DOUERA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1359, '4219', 'DRARIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1360, '4220', 'RAHMANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1361, '4221', 'OULED FAYET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1362, '4222', 'CHERCHEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1363, '4223', 'DAMOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1364, '4224', 'MEURAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1365, '4225', 'FOUKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1366, '4226', 'BOU ISMAIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1367, '4227', 'AHMER EL AIN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1368, '4228', 'CHERAGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1369, '4229', 'STAOUELI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1370, '4230', 'BOU HAROUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1371, '4231', 'EL ACHOUR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1372, '4232', 'SIDI GHILES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1373, '4233', 'MESSELMOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1374, '4234', 'SIDI RACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1375, '4235', 'KOLEA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1376, '4236', 'ATTATBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1377, '4237', 'SOUIDANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1378, '4238', 'KHRACIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1379, '4239', 'AIN BENIAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1380, '4240', 'SIDI SEMIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1381, '4241', 'BENI MILLEUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1382, '4242', 'HADJERAT ENNOUS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1383, '4301', 'MILA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1384, '4302', 'FERDJIOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1385, '4303', 'CHELGHOUM LAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1386, '4304', 'OUED ATHMENIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1387, '4305', 'AIN MELLOUK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1388, '4306', 'TELERGHMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1389, '4307', 'OUED SEGUEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1390, '4308', 'TADJENANET');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1391, '4309', 'BENYAHIA A.RAHMANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1392, '4310', 'OUED ENDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1393, '4311', 'AHMED RACHEDI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1394, '4312', 'OULED KHALOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1395, '4313', 'TIBERGUENT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1396, '4314', 'BOUHATEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1397, '4315', 'ROUACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1398, '4316', 'TESSALA LAMATAI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1399, '4317', 'GRAREM GOUGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1400, '4318', 'SIDI MEROUANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1401, '4319', 'TASSADANE HADDADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1402, '4320', 'DERRADJI BOUSSELAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1403, '4321', 'MINAR ZARZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1404, '4322', 'AMIRA ARRAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1405, '4323', 'TERRAI BAINEN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1406, '4324', 'HAMALA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1407, '4325', 'AIN TINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1408, '4326', 'EL MECHIRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1409, '4327', 'SIDI KHELIFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1410, '4328', 'ZEGHAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1411, '4329', 'ELAYADI BARBES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1412, '4330', 'AIN BEIDA HARRICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1413, '4331', 'YAHIA BENIGUECHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1414, '4332', 'CHIGARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1415, '4401', 'AIN DEFLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1416, '4402', 'MILIANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1417, '4403', 'BOUMEDFAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1418, '4404', 'KHEMIS MILIANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1419, '4405', 'HAMMAM RIGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1420, '4406', 'ARIB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1421, '4407', 'DJELIDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1422, '4408', 'EL AMRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1423, '4409', 'BOURACHED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1424, '4410', 'EL ATTAF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1425, '4411', 'EL ABADIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1426, '4412', 'DJENDEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1427, '4413', 'OUED CHORFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1428, '4414', 'AIN LECHIAKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1429, '4415', 'OUED DJEMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1430, '4416', 'ROUINA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1431, '4417', 'ZEDDINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1432, '4418', 'EL HASSANIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1433, '4419', 'BIR OULD KHELIFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1434, '4420', 'AIN SOLTANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1435, '4421', 'TARIK IBN ZIAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1436, '4422', 'BORDJ EMIR KHALED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1437, '4423', 'AIN TORKI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1438, '4424', 'SIDI LAKHDAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1439, '4425', 'BEN ALLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1440, '4426', 'AIN BENIAN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1441, '4427', 'HOCEINIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1442, '4428', 'BARBOUCHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1443, '4429', 'DJEMAA OULED CHIKH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1444, '4430', 'MEKHATRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1445, '4431', 'BATHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1446, '4432', 'TACHTA ZEGAGHA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1447, '4433', 'AIN BOUYAHIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1448, '4434', 'EL MAINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1449, '4435', 'TIBERKANINE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1450, '4436', 'BELAAS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1451, '4501', 'NAAMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1452, '4502', 'MECHRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1453, '4503', 'AIN SEFRA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1454, '4504', 'TIOUT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1455, '4505', 'SFISSIFA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1456, '4506', 'MOGHRAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1457, '4507', 'ASSELA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1458, '4508', 'DJENIANE BOURZEG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1459, '4509', 'AIN BEN KHELIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1460, '4510', 'MAKMAN BEN AMER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1461, '4511', 'KASDIR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1462, '4512', 'EL BIOD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1463, '4601', 'AIN TEMOUCHENT');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1464, '4602', 'CHAABET EL HAM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1465, '4603', 'AIN KIHAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1466, '4604', 'HAMMAM BOUHADJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1467, '4605', 'BOU ZEDJAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1468, '4606', 'OUED BERKECHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1469, '4607', 'AGHLAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1470, '4608', 'TERGA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1471, '4609', 'AIN EL ARBAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1472, '4610', 'TAMZOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1473, '4611', 'CHENTOUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1474, '4612', 'SIDI BEN ADDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1475, '4613', 'AOUBELLIL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1476, '4614', 'EL MALAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1477, '4615', 'SIDI BOUMEDIENE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1478, '4616', 'OUED SABAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1479, '4617', 'OULED BOUDJEMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1480, '4618', 'AIN TOLBA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1481, '4619', 'EL AMRIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1482, '4620', 'HASSI EL GHELLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1483, '4621', 'HASSASNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1484, '4622', 'OULED KIHAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1485, '4623', 'BENI SAF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1486, '4624', 'SIDI SAFI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1487, '4625', 'OULHACA EL GHERABA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1488, '4626', 'TADMAYA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1489, '4627', 'EL EMIR ABDELKADER');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1490, '4628', 'EL MESSAID');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1491, '4701', 'GHARDAIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1492, '4702', 'EL MENIAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1493, '4703', 'DHAYET BENDHAHOUA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1494, '4704', 'BERRIANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1495, '4705', 'METLILI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1496, '4706', 'EL GUERRARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1497, '4707', 'EL ATTEUF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1498, '4708', 'ZELFANA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1499, '4709', 'SEBSEB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1500, '4710', 'BOUNOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1501, '4711', 'HASSI FEHAL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1502, '4712', 'HASSI GARA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1503, '4713', 'MANSOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1504, '4801', 'RELIZANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1505, '4802', 'OUED RHIOU');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1506, '4803', 'BELAASSEL BOUZEGZA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1507, '4804', 'SIDI SAADA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1508, '4805', 'OULED AICHE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1509, '4806', 'SIDI LAZREG');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1510, '4807', 'EL HAMADNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1511, '4808', 'SIDI MHAMED BENALI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1512, '4809', 'MEDIOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1513, '4810', 'SIDI KHETTAB');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1514, '4811', 'AMMI MOUSSA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1515, '4812', 'ZEMMOURA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1516, '4813', 'BENI DERGOUN');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1517, '4814', 'DJIDIOUIA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1518, '4815', 'EL GUETTAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1519, '4816', 'HAMRI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1520, '4817', 'EL MATMAR');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1521, '4818', 'SIDI M. BEN AOUDA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1522, '4819', 'AIN TAREK');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1523, '4820', 'OUED ESSALEM');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1524, '4821', 'OUARIZANE');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1525, '4822', 'MAZOUNA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1526, '4823', 'KALAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1527, '4824', 'AIN RAHMA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1528, '4825', 'YELLEL');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1529, '4826', 'OUED EL DJEMAA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1530, '4827', 'RAMKA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1531, '4828', 'MENDES');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1532, '4829', 'LAHLEF');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1533, '4830', 'BENI ZENTIS');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1534, '4831', 'SOUK EL HAAD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1535, '4832', 'DAR BEN ABDELLAH');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1536, '4833', 'EL HASSI');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1537, '4834', 'HAD ECHKALLA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1538, '4835', 'BENDAOUD');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1539, '4836', 'EL OULDJA');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1540, '4837', 'MERDJA SIDI ABED');
INSERT INTO COMMUNES (ID_COM, CODE_COM, COMMUNE) VALUES (1541, '4838', 'OULED SIDI MIHOUB');

INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (1, 'C1A11', 'Affaires bénéficiant d’une réfaction de 50 %.', 2, 50, 0, '-');
INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (2, 'C1A12', 'Affaires bénéficiant d’une réfaction de 30 %', 2, 30, 0, '-');
INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (3, 'C1A13', 'Affaires sans réfaction', 2, 0, 0, '-');
INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (4, 'C1A14', 'Affaires exonérées', 0, 0, 0, '-');
INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (5, 'C1A20', 'Recettes professionnelles (Professions libérales)', 2, 0, 0, '-');

INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (1, 'E3B11', 'Biens, produits et denrées visées par l''''article 23 du C. TCA', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (7, 'E3B21', 'Productions : biens, produits et denrées visées par l''''art. 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (2, 'E3B12', 'Prestations de services visées par l''''article 23 du C. TCA', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (3, 'E3B13', 'Opérations immobilières visées par l''''article 23 du C. TCA', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (4, 'E3B14', 'Actes médicaux', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (5, 'E3B15', 'Commissionnaires et courtiers', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (6, 'E3B16', 'Fourniture d''''énergie', 9, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (8, 'E3B22', 'Revente en l''''état : biens, produits et denrées visées par l''''art. 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (9, 'E3B24', 'Professions libérales', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (10, 'E3B23', 'Travaux immobiliers autres que ceux soumis au taux de 7%', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (12, 'E3B36', 'Autres prestations de services visées à l''''article 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (13, 'E3B25', 'Opérations de banques et d''''assurances', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (14, 'E3B26', 'Prestations de téléphones et de télex', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (15, 'E3B28', 'Autres prestations de services', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (16, 'E3B31', 'Débits de boissons', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (17, 'E3B32', 'Productions : biens, produits et denrées visées par l''''article 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (18, 'E3B33', 'Revente en l''''état : biens, produits et denrées visées par l''''art. 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (19, 'E3B34', 'Tabacs et allumettes', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (20, 'E3B35', 'Spectacles, jeux et divertissements autres que ceux de l''''art. 21 du C. TCA', 19, 0, '-');
INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (21, 'E3B37', 'Consommations sur place', 19, 0, '-');



SET TERM ^ ;

CREATE OR ALTER PROCEDURE NORMES_ID_NORME_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
        NEW_VALUE = GEN_ID(NORMES_ID_NORME_GEN, 1);
END^

CREATE OR ALTER PROCEDURE CONSIGNATIONS_ID_CONSIGNE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  NEW_VALUE = GEN_ID(CONSIGNATIONS_ID_CONSIGNE_GEN, 1);
END^

CREATE OR ALTER PROCEDURE LIGNES_CONSIGNATION_ID_LC_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  NEW_VALUE = GEN_ID(LIGNES_CONSIGNATION_ID_LC_GEN, 1);
END^

/* Stored Procedure: ACHATS_IMP_ART_FOUR */
CREATE PROCEDURE ACHATS_IMP_ART_FOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    TOTAL_QUANTITE NUMERIC (18, 6),
    MOYEN_P_U NUMERIC (18, 2),
    TOTAL_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^

CREATE PROCEDURE CAT_PROD_ID_CAT_PROD_AI
RETURNS(NEW_VALUE INTEGER)
AS
BEGIN
  NEW_VALUE = GEN_ID(CAT_PROD_ID_CAT_PROD_GEN, 1);
END
^

/* Stored Procedure: ACHATS_LOC_ART_FOUR */
CREATE PROCEDURE ACHATS_LOC_ART_FOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    TOTAL_QUANTITE NUMERIC (18, 6),
    MOYEN_P_U NUMERIC (18, 2),
    TOTAL_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ARCHIVES_ID_ARCHIVE_AI */
CREATE PROCEDURE ARCHIVES_ID_ARCHIVE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ARTICLES_ID_ART_AI */
CREATE PROCEDURE ARTICLES_ID_ART_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BALANCE_CLIENTS */
CREATE PROCEDURE BALANCE_CLIENTS (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_C INTEGER,
    NOM_CLIENT VARCHAR (128),
    ROTATION INTEGER,
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_VENTES NUMERIC (18, 2),
    TOTAL_AVOIRS NUMERIC (18, 2),
    TOTAL_REGLEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BALANCE_FOURNISSEURS_LOC */
CREATE PROCEDURE BALANCE_FOURNISSEURS_LOC (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (128),
    ROTATION INTEGER,
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_ACHATS NUMERIC (18, 2),
    TOTAL_AVOIRS NUMERIC (18, 2),
    TOTAL_REGLEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BALANCE_STOCK_MENSUEL */
CREATE PROCEDURE BALANCE_STOCK_MENSUEL
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    VAL_STK_INI NUMERIC (18, 2),
    ACHAT_IMPO NUMERIC (18, 2),
    ACHAT_LOC NUMERIC (18, 2),
    AVOIR_ACHAT NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    AVOIR_VENTE NUMERIC (18, 2),
    STOCK_FINAL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BALANCE_VENTES_MENSUEL */
CREATE PROCEDURE BALANCE_VENTES_MENSUEL
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    AVOIR NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    T_FACT NUMERIC (18, 2),
    T_REGT NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    CONS NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BASE_CALCUL_PAMP */
CREATE PROCEDURE BASE_CALCUL_PAMP (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    DATE_OPER DATE,
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: BC_F_ID_BC_F_AI */
CREATE PROCEDURE BC_F_ID_BC_F_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CA_TVA */
CREATE PROCEDURE CA_TVA (
    ID INTEGER,
    TAUX NUMERIC (18, 2))
RETURNS (
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CALCUL_TIMBRE */
CREATE PROCEDURE CALCUL_TIMBRE (
    TTC NUMERIC (18, 2))
RETURNS (
    TIMBRE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CAMION_ID_CAMION_AI */
CREATE PROCEDURE CAMION_ID_CAMION_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CAPROD_CACLIENT */
CREATE PROCEDURE CAPROD_CACLIENT (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ID_ARTICLE INTEGER,
    REF_PRO VARCHAR (20),
    DESIGNATION VARCHAR (128),
    CACP NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    CAP NUMERIC (18, 2),
    P100CA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CATALOGUE_ID_CAT_AI */
CREATE PROCEDURE CATALOGUE_ID_CAT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CHAUFFEURS_ID_CHAUFFEUR_AI */
CREATE PROCEDURE CHAUFFEURS_ID_CHAUFFEUR_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CHIFRRE_AFFAIRES */
CREATE PROCEDURE CHIFRRE_AFFAIRES
RETURNS (
    MOIS INTEGER,
    NOM_MOIS VARCHAR (20),
    CA_MOIS NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FACTURES_MOIS */
CREATE PROCEDURE FACTURES_MOIS
RETURNS (
    ID_CLIENT INTEGER,
    DATE_FACT DATE,
    NATURE INTEGER,
    MOIS INTEGER,
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    TOTAL_FACT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CHIFRRE_AFFAIRES_MOIS */
CREATE PROCEDURE CHIFRRE_AFFAIRES_MOIS
RETURNS (
    MOIS INTEGER,
    NOM_MOIS VARCHAR (20),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    TOTAL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CLIENTS_ID_CLIENT_AI */
CREATE PROCEDURE CLIENTS_ID_CLIENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CLIENTS_TRANCHES */
CREATE PROCEDURE CLIENTS_TRANCHES (
    DU DATE,
    AU DATE,
    MT1 NUMERIC (18, 2),
    MT2 NUMERIC (18, 2))
RETURNS (
    ID_CLIENT INTEGER,
    CLIENT VARCHAR (128),
    MONTANT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CODE_TAP_ID_TAP_AI */
CREATE PROCEDURE CODE_TAP_ID_TAP_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CODE_TVA_ID_TVA_AI */
CREATE PROCEDURE CODE_TVA_ID_TVA_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_ACHATS */
CREATE PROCEDURE COMPA_ACHATS (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_ENTREE INTEGER,
    NUM_FACT VARCHAR (12),
    INFO_REGLEMENT VARCHAR (128),
    DATE_FACT DATE,
    ID_FOURN INTEGER,
    CODE_TIER VARCHAR (12),
    CPTE_ACHAT VARCHAR (12),
    CPTE_STOCK VARCHAR (12),
    ACHAT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    AUTRES VARCHAR (12),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    CPTE_TIER VARCHAR (12),
    MNT_TIER NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_CONS_FACT */
CREATE PROCEDURE COMPA_CONS_FACT (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_SORTIE INTEGER,
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSO VARCHAR (12),
    CONSOMATION NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_CONSOM_ANNUEL */
CREATE PROCEDURE COMPA_CONSOM_ANNUEL
RETURNS (
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSOM VARCHAR (12),
    CONSOM NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_CONSOM_MOIS */
CREATE PROCEDURE COMPA_CONSOM_MOIS
RETURNS (
    MOIS SMALLINT,
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSOM VARCHAR (12),
    CONSOM NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_DEPENSES */
CREATE PROCEDURE COMPA_DEPENSES (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_DEPENSE VARCHAR (12),
    DATE_DEP DATE,
    CPTE_CHARGE VARCHAR (12),
    CPTE_TRESOR VARCHAR (12),
    NUM_CHEQUE VARCHAR (7),
    MONTANT NUMERIC (18, 2),
    LIBELLE VARCHAR (128),
    TIERS VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_ENTREE_STOCK */
CREATE PROCEDURE COMPA_ENTREE_STOCK (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_ENTREE INTEGER,
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    CPTE_ACHAT VARCHAR (12),
    CPTE_STOCK VARCHAR (12),
    ACHAT NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_REGL_TIERS */
CREATE PROCEDURE COMPA_REGL_TIERS (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_REGL VARCHAR (12),
    DATE_REGL DATE,
    ID_TIER INTEGER,
    CPTE_CLIENT VARCHAR (12),
    CPTE_TRESOR VARCHAR (12),
    NUM_CHEQUE VARCHAR (12),
    MONTANT NUMERIC (18, 2),
    TYPE_REG SMALLINT,
    TIERS VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_VENTES */
CREATE PROCEDURE COMPA_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_SORTIE INTEGER,
    CODE_TIER VARCHAR (12),
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    ID_CLIENT INTEGER,
    INFO_REGLEMENT VARCHAR (64),
    CPTE_CA VARCHAR (12),
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    CPTE_TIER VARCHAR (12),
    MNT_TIER NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPA_VIREMENT */
CREATE PROCEDURE COMPA_VIREMENT (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_VIR VARCHAR (12),
    DATE_VIR DATE,
    CPTE_CREDIT VARCHAR (12),
    CPTE_DEBIT VARCHAR (12),
    NUM_CHEQUE VARCHAR (12),
    MONTANT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: COMPTES_TRESORIE_ID_CPTE_AI */
CREATE PROCEDURE COMPTES_TRESORIE_ID_CPTE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CONSOMMATION */
CREATE PROCEDURE CONSOMMATION (
    NATURE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    IMPORT INTEGER,
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CONSOMMATION_ALL */
CREATE PROCEDURE CONSOMMATION_ALL (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    MATIERE INTEGER,
    IMPORT INTEGER,
    FP SMALLINT,
    GROS SMALLINT,
    QTE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CONSOMMATION_DEPOT */
CREATE PROCEDURE CONSOMMATION_DEPOT (
    D1 DATE,
    D2 DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    MATIERE INTEGER,
    IMPORT INTEGER,
    FP SMALLINT,
    GROS SMALLINT,
    QTE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CONSOMMATION_IMPORT */
CREATE PROCEDURE CONSOMMATION_IMPORT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: CONSOMMATION_LOCAL */
CREATE PROCEDURE CONSOMMATION_LOCAL (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: DEPENSES_ID_DEP_AI */
CREATE PROCEDURE DEPENSES_ID_DEP_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: DESTINATION_ID_DESTINATION_AI */
CREATE PROCEDURE DESTINATION_ID_DESTINATION_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: DEVISES_ID_DEVISE_AI */
CREATE PROCEDURE DEVISES_ID_DEVISE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: EMPLACEMENTS_ID_EMPL_AI */
CREATE PROCEDURE EMPLACEMENTS_ID_EMPL_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MOUVEMENTS */
CREATE PROCEDURE MOUVEMENTS
RETURNS (
    ID_ARTICLE INTEGER,
    REF_ENTREE VARCHAR (12),
    ID_MAGAZIN INTEGER,
    DATE_OPER DATE,
    COUT NUMERIC (18, 2),
    QTE_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    QTE_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    QTE_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    SENS VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: EN_STOCK */
CREATE PROCEDURE EN_STOCK (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE)
RETURNS (
    STOCK NUMERIC (18, 6))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: EN_STOCK_REF_ENTREE */
CREATE PROCEDURE EN_STOCK_REF_ENTREE (
    REF_ENTREE VARCHAR (12),
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE)
RETURNS (
    STOCK NUMERIC (18, 6))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ENTET_BC_F_ID_ENT_BD_F_AI */
CREATE PROCEDURE ENTET_BC_F_ID_ENT_BD_F_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ENTETE_ENTREES_ID_ENT_ENT_AI */
CREATE PROCEDURE ENTETE_ENTREES_ID_ENT_ENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ENTETE_SORTIES_ID_ENT_SORT_AI */
CREATE PROCEDURE ENTETE_SORTIES_ID_ENT_SORT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ENTETE_TRANSFERT_ID_ENTETE_AI */
CREATE PROCEDURE ENTETE_TRANSFERT_ID_ENTETE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ENTREES_ID_ENTREE_AI */
CREATE PROCEDURE ENTREES_ID_ENTREE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_104 */
CREATE PROCEDURE ETAT_104 (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^

/* Definition for the `ETAT_104_MAGASIN` procedure :  */

CREATE OR ALTER PROCEDURE ETAT_104_MAGASIN(
  D1 DATE,
  D2 DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(128),
  R_C VARCHAR(25),
  DATE_RC DATE,
  I_F VARCHAR(20),
  NIS VARCHAR(20),
  AI VARCHAR(20),
  AV NUMERIC(18, 2),
  CA NUMERIC(18, 2),
  TVAAV NUMERIC(18, 2),
  TVACA NUMERIC(18, 2),
  HT NUMERIC(18, 2),
  TVA NUMERIC(18, 2),
  SERVICE_GESTION VARCHAR(64))
AS
BEGIN
  SUSPEND;
END^


/* Stored Procedure: ETAT_104_GROS */
CREATE PROCEDURE ETAT_104_GROS (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_104_PART */
CREATE PROCEDURE ETAT_104_PART (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_CREANCES */
CREATE PROCEDURE ETAT_CREANCES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    CREANCE NUMERIC (18, 2),
    REGLEMENT NUMERIC (18, 2),
    ROTATION NUMERIC(18, 2),
    ECART NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_DETTE_CREANCES */
CREATE PROCEDURE ETAT_DETTE_CREANCES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    CREANCE NUMERIC (18, 2),
    REGLEMENT_CLIENT NUMERIC (18, 2),
    ECART_CREANCE NUMERIC (18, 2),
    ACHATS NUMERIC (18, 2),
    REGLEMENT_FOURNISSEUR NUMERIC (18, 2),
    ECART_DETTE NUMERIC (18, 2),
    ECART NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_DETTES_LOCALES */
CREATE PROCEDURE ETAT_DETTES_LOCALES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    ACHATS NUMERIC (18, 2),
    REGLEMENT NUMERIC (18, 2),
    ECART NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_STOCK */
CREATE PROCEDURE ETAT_STOCK (
    D DATE)
RETURNS (
    ID_MAGAZIN INTEGER,
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_STOCK_DEPOT */
CREATE PROCEDURE ETAT_STOCK_DEPOT (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_MIN NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_DEPENSES_CPTE */
CREATE PROCEDURE TOTAL_DEPENSES_CPTE (
    ID INTEGER)
RETURNS (
    DEPENSE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_RECETTES_CPTE */
CREATE PROCEDURE TOTAL_RECETTES_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_RECETTE_CPTE */
CREATE PROCEDURE TOTAL_REGLT_RECETTE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_REPENSE_CPTE */
CREATE PROCEDURE TOTAL_REGLT_REPENSE_CPTE (
    ID INTEGER)
RETURNS (
    DEPENSE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_SOLDE_INI_CPTE */
CREATE PROCEDURE TOTAL_SOLDE_INI_CPTE (
    ID INTEGER)
RETURNS (
    SOLDE_INI NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VIR_FOND_DEPENSE_CPTE */
CREATE PROCEDURE TOTAL_VIR_FOND_DEPENSE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VIR_FOND_RECETTE_CPTE */
CREATE PROCEDURE TOTAL_VIR_FOND_RECETTE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_TRESORERIE */
CREATE PROCEDURE ETAT_TRESORERIE
RETURNS (
    ID_CPTE INTEGER,
    CPTE VARCHAR (64),
    SOLDE_INI NUMERIC (18, 2),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: ETAT_VENTES */
CREATE PROCEDURE ETAT_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    ACTIVITE VARCHAR (128),
    R_C VARCHAR (32),
    A_I VARCHAR (20),
    I_F VARCHAR (20),
    QTE1 NUMERIC (18, 2),
    QTE2 NUMERIC (18, 2),
    QTE3 NUMERIC (18, 2),
    QTE4 NUMERIC (18, 2),
    QTE5 NUMERIC (18, 2),
    QTE6 NUMERIC (18, 2),
    QTE7 NUMERIC (18, 2),
    QTE8 NUMERIC (18, 2),
    QTE9 NUMERIC (18, 2),
    QTE10 NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FACT_ANT_ID_FACT_AI */
CREATE PROCEDURE FACT_ANT_ID_FACT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FACT_DD_JOINTE_ID_FACT_AI */
CREATE PROCEDURE FACT_DD_JOINTE_ID_FACT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FACT_TAUX_TVA */
CREATE PROCEDURE FACT_TAUX_TVA (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ENTETE INTEGER,
    NUM VARCHAR (12),
    DATE_PIECE DATE,
    CLIENT VARCHAR (128),
    CA_00 NUMERIC (18, 2),
    TVA_00 NUMERIC (18, 2),
    CA_07 NUMERIC (18, 2),
    TVA_07 NUMERIC (18, 2),
    CA_17 NUMERIC (18, 2),
    TVA_17 NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FAMILLE_ID_ART */
CREATE PROCEDURE FAMILLE_ID_ART (
    ID INTEGER)
RETURNS (
    FAMILLE VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FAMILLES_CHARGES_ID_FAM_AI */
CREATE PROCEDURE FAMILLES_CHARGES_ID_FAM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FAMILLES_ID_FAMILLE_AI */
CREATE PROCEDURE FAMILLES_ID_FAMILLE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FAMILLES_RECETTES_ID_FAM_REC_AI */
CREATE PROCEDURE FAMILLES_RECETTES_ID_FAM_REC_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FCH_STOCK_DUAU_DPT_REF_ENTREE */
CREATE PROCEDURE FCH_STOCK_DUAU_DPT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK */
CREATE PROCEDURE FICHE_DE_STOCK (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    PRIX_VENTE NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_DEPOT */
CREATE PROCEDURE FICHE_DE_STOCK_DEPOT (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    PRIX_VENTE NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_DEPOT_REF_ENTREE */
CREATE PROCEDURE FICHE_DE_STOCK_DEPOT_REF_ENTREE (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_DU_AU */
CREATE PROCEDURE FICHE_DE_STOCK_DU_AU (
    DU DATE,
    AU DATE,
    ID_ART INTEGER)
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_DU_AU_DEPOT */
CREATE PROCEDURE FICHE_DE_STOCK_DU_AU_DEPOT (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE,
    ID_ART INTEGER)
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_DU_AU_REF_ENTREE */
CREATE PROCEDURE FICHE_DE_STOCK_DU_AU_REF_ENTREE (
    DU DATE,
    AU DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FICHE_DE_STOCK_REF_ENTREE */
CREATE PROCEDURE FICHE_DE_STOCK_REF_ENTREE (
    D DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FOURNISSEURS_ID_FOURN_AI */
CREATE PROCEDURE FOURNISSEURS_ID_FOURN_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: FRAUDEURS_ID_FRAUD_AI */
CREATE PROCEDURE FRAUDEURS_ID_FRAUD_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: G50_ID_G50_AI */
CREATE PROCEDURE G50_ID_G50_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INFO_NUM_SERIE */
CREATE PROCEDURE INFO_NUM_SERIE (
    NUM_SERIE VARCHAR (128))
RETURNS (
    SERIAL VARCHAR (128),
    ARTICLE VARCHAR (128),
    DATE_ACHAT DATE,
    NUM_FACT_ACHAT VARCHAR (12),
    LOCAL VARCHAR (12),
    FOURNISSEUR VARCHAR (128),
    DATE_VENTE DATE,
    NUM_FACT_VENTE VARCHAR (12),
    CLIENT VARCHAR (128))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MOUVEMENTS_DU_AU */
CREATE PROCEDURE MOUVEMENTS_DU_AU (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ARTICLE INTEGER,
    REF_ENTREE VARCHAR (12),
    ID_MAGAZIN INTEGER,
    DATE_OPER DATE,
    COUT NUMERIC (18, 2),
    INI_QTE_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_QTE_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_QTE_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    QTE_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    QTE_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    QTE_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    SENS VARCHAR (2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INV_DUAU_DPT_REF_ENTREE */
CREATE PROCEDURE INV_DUAU_DPT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PAMP_DEPOT */
CREATE PROCEDURE PAMP_DEPOT (
    ID_ARTICLE INTEGER,
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    P_PAMP NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_DEPOT */
CREATE PROCEDURE INVENTAIRE_DEPOT (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_DEPOT_REF_ENTREE */
CREATE PROCEDURE INVENTAIRE_DEPOT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_DU_AU */
CREATE PROCEDURE INVENTAIRE_DU_AU (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_DU_AU_DEPOT */
CREATE PROCEDURE INVENTAIRE_DU_AU_DEPOT (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_DU_AU_REF_ENTREE */
CREATE PROCEDURE INVENTAIRE_DU_AU_REF_ENTREE (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRE_ID_INVENTAIRE_AI */
CREATE PROCEDURE INVENTAIRE_ID_INVENTAIRE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRES */
CREATE PROCEDURE INVENTAIRES (
    D DATE)
RETURNS (
    ID_MAGAZIN INTEGER,
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRES_MARQUES */
CREATE PROCEDURE INVENTAIRES_MARQUES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_MARQUE INTEGER,
    DESIGNATION VARCHAR (128),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    VENTES NUMERIC (18, 2),
    MARGES NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: INVENTAIRES_REF_ENTREE */
CREATE PROCEDURE INVENTAIRES_REF_ENTREE (
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: LIGNES_PROFORMA_ID_L_PROF_AI */
CREATE PROCEDURE LIGNES_PROFORMA_ID_L_PROF_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: LIGNES_TRANSFERT_ID_LIGNE_AI */
CREATE PROCEDURE LIGNES_TRANSFERT_ID_LIGNE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: LISTE_PRODUIT */
CREATE PROCEDURE LISTE_PRODUIT (
    D DATE)
RETURNS (
    ID INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (64),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 6),
    PRIX NUMERIC (18, 6))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: LST_CHRGS_ID_LST_CHRG_AI */
CREATE PROCEDURE LST_CHRGS_ID_LST_CHRG_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MAGAZINS_ID_MAG_AI */
CREATE PROCEDURE MAGAZINS_ID_MAG_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MARQUES_ID_MARQUE_AI */
CREATE PROCEDURE MARQUES_ID_MARQUE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MAX_DATE_ENTREES */
CREATE PROCEDURE MAX_DATE_ENTREES (
    NATURE INTEGER)
RETURNS (
    DTE DATE)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MAX_DATE_SORTIES */
CREATE PROCEDURE MAX_DATE_SORTIES (
    NATURE INTEGER)
RETURNS (
    DTE DATE)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_AV_ENTREES */
CREATE PROCEDURE MVT_AV_ENTREES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_AV_SORTIES */
CREATE PROCEDURE MVT_AV_SORTIES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_ENTREES_IMP */
CREATE PROCEDURE MVT_ENTREES_IMP (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_ENTREES_LOC */
CREATE PROCEDURE MVT_ENTREES_LOC (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_ENTREES_PRODUCTION */
CREATE PROCEDURE MVT_ENTREES_PRODUCTION (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: MVT_SORTIES */
CREATE PROCEDURE MVT_SORTIES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NETOYER_ENTREES */
CREATE PROCEDURE NETOYER_ENTREES
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NETOYER_SORTIES */
CREATE PROCEDURE NETOYER_SORTIES
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NOMENCLATURES_ID_NOM_AI */
CREATE PROCEDURE NOMENCLATURES_ID_NOM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_BC_F */
CREATE PROCEDURE NUM_BC_F
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_DEP */
CREATE PROCEDURE NUM_DEP
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_DOS_IMPORTATION */
CREATE PROCEDURE NUM_DOS_IMPORTATION (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_ENTET_ENTREE */
CREATE PROCEDURE NUM_ENTET_ENTREE (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_ENTET_SORTIE */
CREATE PROCEDURE NUM_ENTET_SORTIE (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_ENTET_SORTIE_MAG */
CREATE PROCEDURE NUM_ENTET_SORTIE_MAG (
    NATURE INTEGER,
    ID_MAGASIN INTEGER)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_ENTET_SORTIE_MAGAZIN */
CREATE PROCEDURE NUM_ENTET_SORTIE_MAGAZIN (
    NATURE INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_PROFORMA */
CREATE PROCEDURE NUM_PROFORMA
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_REC */
CREATE PROCEDURE NUM_REC
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_RGLT */
CREATE PROCEDURE NUM_RGLT (
    NATURE SMALLINT)
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_SERIES_ID_NUM_AI */
CREATE PROCEDURE NUM_SERIES_ID_NUM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_TRANSFERT */
CREATE PROCEDURE NUM_TRANSFERT
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: NUM_VIR_FONDS */
CREATE PROCEDURE NUM_VIR_FONDS
RETURNS (
    NUM INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PAMP */
CREATE PROCEDURE PAMP (
    ID_ARTICLE INTEGER,
    D DATE)
RETURNS (
    P_PAMP NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PARAMETRES_ID_PARAM_AI */
CREATE PROCEDURE PARAMETRES_ID_PARAM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PRODUIT_DISPONIBLE */
CREATE PROCEDURE PRODUIT_DISPONIBLE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PRODUIT_DISPONIBLE_REF_ENTREE */
CREATE PROCEDURE PRODUIT_DISPONIBLE_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_RENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: PROFORMA_ID_PROF_AI */
CREATE PROCEDURE PROFORMA_ID_PROF_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_FACTURE_VENTE */
CREATE PROCEDURE RECAP_FACTURE_VENTE (
    D1 DATE,
    D2 DATE)
RETURNS (
    NBRE INTEGER,
    ESPECE NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    REFACTION NUMERIC (18, 2),
    SANS_REFACTION NUMERIC (18, 2),
    CESSION NUMERIC (18, 2),
    VENTE NUMERIC (18, 2),
    CONSOM NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTE_JOUR */
CREATE PROCEDURE VENTE_JOUR (
    D DATE)
RETURNS (
    NBRE INTEGER,
    HT_S_TVA NUMERIC (18, 2),
    HT_TVA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_FACTURE_VENTE_JOUR */
CREATE PROCEDURE RECAP_FACTURE_VENTE_JOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    NBRE INTEGER,
    DTE DATE,
    TIMBRE NUMERIC (18, 2),
    HT_TVA NUMERIC (18, 2),
    HT_S_TVA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_VENTE */
CREATE PROCEDURE RECAP_VENTE (
    D1 DATE,
    D2 DATE)
RETURNS (
    TICKET NUMERIC (18, 2),
    FACT_HT NUMERIC (18, 2),
    FACT_TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    FACT_TTC NUMERIC (18, 2),
    ESPECE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    SOLDE_INI_CAISSE NUMERIC (18, 2),
    RECETTE NUMERIC (18, 2),
    REGLT_CLIENTS NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 2),
    REGLT_FOURNISSEURS NUMERIC (18, 2),
    VIR_FOND_REC NUMERIC (18, 2),
    VIR_FOND_DEP NUMERIC (18, 2),
    SOLDE_CAISSE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_VENTE_TAUX_TVA */
CREATE PROCEDURE RECAP_VENTE_TAUX_TVA (
    D1 DATE,
    D2 DATE)
RETURNS (
    TX_TVA NUMERIC (18, 2),
    HT_AV_REM NUMERIC (18, 2),
    REMISES NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_VENTE_TVA */
CREATE PROCEDURE RECAP_VENTE_TVA (
    NATURE SMALLINT,
    D1 DATE,
    D2 DATE)
RETURNS (
    TAUX_TVA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECAP_VENTES */
CREATE PROCEDURE RECAP_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ID_SORTIE INTEGER,
    NUM_PIECE VARCHAR (12),
    DATE_PIECE DATE,
    SOLDE_DEB_EXE NUMERIC (18, 2),
    VENTES_ANT NUMERIC (18, 2),
    AVOIRS_ANT NUMERIC (18, 2),
    REGLTS_ANT NUMERIC (18, 2),
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_VENTE NUMERIC (18, 2),
    TOTAL_REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    QTE1 NUMERIC (18, 6),
    VAL1 NUMERIC (18, 2),
    QTE2 NUMERIC (18, 6),
    VAL2 NUMERIC (18, 2),
    QTE3 NUMERIC (18, 6),
    VAL3 NUMERIC (18, 2),
    QTE4 NUMERIC (18, 6),
    VAL4 NUMERIC (18, 2),
    QTE5 NUMERIC (18, 6),
    VAL5 NUMERIC (18, 2),
    QTE6 NUMERIC (18, 6),
    VAL6 NUMERIC (18, 2),
    QTE7 NUMERIC (18, 6),
    VAL7 NUMERIC (18, 2),
    QTE8 NUMERIC (18, 6),
    VAL8 NUMERIC (18, 2),
    QTE9 NUMERIC (18, 6),
    VAL9 NUMERIC (18, 2),
    QTE10 NUMERIC (18, 6),
    VAL10 NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RECETTES_ID_REC_AI */
CREATE PROCEDURE RECETTES_ID_REC_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: REGL_TIERS_ID_REGL_AI */
CREATE PROCEDURE REGL_TIERS_ID_REGL_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: REGLEMENTS_ID_REGLEMENT_AI */
CREATE PROCEDURE REGLEMENTS_ID_REGLEMENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_CLIENT */
CREATE PROCEDURE RELEVE_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    NUM INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    VENTE NUMERIC (18, 2),
    REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    TP_OP INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_CLIENT_DETAIL */
CREATE PROCEDURE RELEVE_CLIENT_DETAIL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    PRODUIT VARCHAR (128),
    QTE NUMERIC (18, 6),
    FRX NUMERIC (18, 6),
    PU NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    RGT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    NAT SMALLINT,
    ORD SMALLINT)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_COMPTE */
CREATE PROCEDURE RELEVE_COMPTE (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CPTE INTEGER,
    DATE_OP DATE,
    LIB VARCHAR (64),
    CHEQUE VARCHAR (7),
    FAM_REC_DEP VARCHAR (64),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2),
    OP INTEGER,
    ETAT VARCHAR (16))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_COMPTE_SANS */
CREATE PROCEDURE RELEVE_COMPTE_SANS (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CPTE INTEGER,
    DATE_OP DATE,
    LIB VARCHAR (64),
    CHEQUE VARCHAR (7),
    FAM_REC_DEP VARCHAR (64),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2),
    OP INTEGER,
    ETAT VARCHAR (16))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_FOURNISSEUR */
CREATE PROCEDURE RELEVE_FOURNISSEUR (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_FOUR INTEGER,
    NUM VARCHAR (12),
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    ACHAT NUMERIC (18, 2),
    REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    TP_OP INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RELEVE_FOURNISSEUR_DETAIL */
CREATE PROCEDURE RELEVE_FOURNISSEUR_DETAIL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    PRODUIT VARCHAR (128),
    QTE NUMERIC (18, 6),
    FRX NUMERIC (18, 6),
    PU NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    RGT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    NAT SMALLINT)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RENUM_CHAUFFEURS_ID_RENUM_AI */
CREATE PROCEDURE RENUM_CHAUFFEURS_ID_RENUM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: RORATION */
CREATE PROCEDURE RORATION
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    DTE DATE,
    NBRE INTEGER,
    CAC NUMERIC (18, 2),
    CAV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    P100CA NUMERIC (18, 2),
    CAT SMALLINT)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SOLDE_INI_TIERS */
CREATE PROCEDURE SOLDE_INI_TIERS (
    ID INTEGER)
RETURNS (
    SOLDE_INI NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_CLIENT */
CREATE PROCEDURE TOTAL_REGLT_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VENTES_CLIENT */
CREATE PROCEDURE TOTAL_VENTES_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SOLDE_CLIENT */
CREATE PROCEDURE SOLDE_CLIENT (
    ID_CLIENT INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_CLIENT_BON */
CREATE PROCEDURE TOTAL_REGLT_CLIENT_BON (
    ID_ENTET_PIECE INTEGER)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VENTES_CLIENT_BON */
CREATE PROCEDURE TOTAL_VENTES_CLIENT_BON (
    ID INTEGER,
    D TIMESTAMP)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SOLDE_CLIENT_BON */
CREATE PROCEDURE SOLDE_CLIENT_BON (
    ID_ENT_SORT INTEGER,
    ID_CLIENT INTEGER,
    D TIMESTAMP)
RETURNS (
    SOLDE_ANT NUMERIC (18, 2),
    VERSEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_CLIENT_RGLT */
CREATE PROCEDURE TOTAL_REGLT_CLIENT_RGLT (
    ID INTEGER,
    D TIMESTAMP,
    ID_REGL INTEGER)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VENTES_CLIENT_RGLT */
CREATE PROCEDURE TOTAL_VENTES_CLIENT_RGLT (
    ID INTEGER,
    D TIMESTAMP)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SOLDE_CLIENT_REGLT */
CREATE PROCEDURE SOLDE_CLIENT_REGLT (
    ID_RGLT INTEGER,
    ID_CLIENT INTEGER,
    D TIMESTAMP,
    MNT_RGLT NUMERIC (18, 2))
RETURNS (
    SOLDE_ANT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TL_RGLT_CLIENT_SF_CTE_PCE */
CREATE PROCEDURE TL_RGLT_CLIENT_SF_CTE_PCE (
    ID INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TL_VTES_CLIENT_SF_CTE_PCE */
CREATE PROCEDURE TL_VTES_CLIENT_SF_CTE_PCE (
    ID INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SOLDE_CLIENT_SAUF_CETTE */
CREATE PROCEDURE SOLDE_CLIENT_SAUF_CETTE (
    ID_CLIENT INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    SOLDE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: SORTIES_ID_SORTIE_AI */
CREATE PROCEDURE SORTIES_ID_SORTIE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: STOCK_FAMILLE */
CREATE PROCEDURE STOCK_FAMILLE (
    D DATE)
RETURNS (
    FAMILLE VARCHAR (64),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: STOCK_NOMENCLATURE */
CREATE PROCEDURE STOCK_NOMENCLATURE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    QTE_DECHET NUMERIC (18, 6),
    TX_DECHET NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2),
    NATURE SMALLINT)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: STOCK_NOMENCLATURE_EMBALLAGE */
CREATE PROCEDURE STOCK_NOMENCLATURE_EMBALLAGE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: STOCK_NOMENCLATURE_MATIERE */
CREATE PROCEDURE STOCK_NOMENCLATURE_MATIERE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: STOCK_NOT_NORMAL */
CREATE PROCEDURE STOCK_NOT_NORMAL (
    D DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: T_M_P_V */
CREATE PROCEDURE T_M_P_V (
    ID INTEGER)
RETURNS (
    T NUMERIC (18, 2),
    M NUMERIC (18, 2),
    P NUMERIC (18, 2),
    V NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TAUX_EXTRACTION_ID_TAUX_AI */
CREATE PROCEDURE TAUX_EXTRACTION_ID_TAUX_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_ACHAT_LOCAL */
CREATE PROCEDURE TOTAL_ACHAT_LOCAL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DETTES NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_ACHAT_LOCAL_ANT */
CREATE PROCEDURE TOTAL_ACHAT_LOCAL_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    DETTES NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_CLIENT_ANT */
CREATE PROCEDURE TOTAL_REGLT_CLIENT_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_FOURNISSEUR */
CREATE PROCEDURE TOTAL_REGLT_FOURNISSEUR (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_REGLT_FOURNISSEUR_ANT */
CREATE PROCEDURE TOTAL_REGLT_FOURNISSEUR_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TOTAL_VENTES_CLIENT_ANT */
CREATE PROCEDURE TOTAL_VENTES_CLIENT_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TVA_PERIODE */
CREATE PROCEDURE TVA_PERIODE (
    M1 SMALLINT,
    M2 SMALLINT,
    A SMALLINT)
RETURNS (
    CODE VARCHAR (12),
    LIBELLE VARCHAR (128),
    CA INTEGER,
    IMP INTEGER,
    EXO INTEGER,
    TVA INTEGER,
    TAUX NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TYPE_AUTRES_ENTREES_ID_TYPE_AI */
CREATE PROCEDURE TYPE_AUTRES_ENTREES_ID_TYPE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: TYPE_AUTRES_SORTIES_ID_TYPE_AI */
CREATE PROCEDURE TYPE_AUTRES_SORTIES_ID_TYPE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: UNITE_ID_ART */
CREATE PROCEDURE UNITE_ID_ART (
    ID INTEGER)
RETURNS (
    UNITE VARCHAR (64))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: UNITES_ID_UNITE_AI */
CREATE PROCEDURE UNITES_ID_UNITE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: UPDATE_PAMP */
CREATE PROCEDURE UPDATE_PAMP
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: UPT_ENT_SRT_TL_CONS */
CREATE PROCEDURE UPT_ENT_SRT_TL_CONS
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTE_CLIENT_PRODUITS */
CREATE PROCEDURE VENTE_CLIENT_PRODUITS (
    ID INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    DATE_PIECE DATE,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    PU NUMERIC (18, 2),
    QTE NUMERIC (18, 6))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTE_FAM_PRODUIT */
CREATE PROCEDURE VENTE_FAM_PRODUIT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_FAMILLE INTEGER,
    FAMILLE VARCHAR (128),
    UNITE VARCHAR (64),
    QTE NUMERIC (18, 2),
    PRIX NUMERIC (18, 2),
    MONTANT NUMERIC (18, 2),
    CONS NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTE_PROD_CLIENT */
CREATE PROCEDURE VENTE_PROD_CLIENT (
    D1 DATE,
    D2 DATE,
    ID_CLIENT INTEGER)
RETURNS (
    NUM VARCHAR (12),
    DT DATE,
    ID_C INTEGER,
    NOM_CLIENT VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    QUANTITE NUMERIC (18, 6),
    P_U NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTE_PRODUIT */
CREATE PROCEDURE VENTE_PRODUIT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (64),
    QTE NUMERIC (18, 2),
    PRIX NUMERIC (18, 2),
    MONTANT NUMERIC (18, 2),
    CONS NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTES_MENSUEL_MR */
CREATE PROCEDURE VENTES_MENSUEL_MR
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    ID_REGLEMENT INTEGER,
    MODE_REGLEMENT VARCHAR (64),
    HT NUMERIC (18, 2),
    AVOIRS NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    MARGE NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    VENTES NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTES_MENSUEL_PRODUIT */
CREATE PROCEDURE VENTES_MENSUEL_PRODUIT (
    ID_ART INTEGER)
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    VAL_STK_INI NUMERIC (18, 2),
    ACHAT_IMPO NUMERIC (18, 2),
    ACHAT_LOC NUMERIC (18, 2),
    AVOIR_ACHAT NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    AVOIR_VENTE NUMERIC (18, 2),
    STOCK_FINAL NUMERIC (18, 2),
    TOTAL_VENTE NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTES_WILAYA */
CREATE PROCEDURE VENTES_WILAYA
RETURNS (
    NBRE INTEGER,
    WILAYA VARCHAR (32),
    MONTANT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VENTES_WILAYA_PRODUITS */
CREATE PROCEDURE VENTES_WILAYA_PRODUITS
RETURNS (
    ID_ART INTEGER,
    WILAYA VARCHAR (32),
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    MONTANT NUMERIC (18, 2))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VERIF_FICHE_DE_STOCK_DEPOT */
CREATE PROCEDURE VERIF_FICHE_DE_STOCK_DEPOT (
    DU DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VERIFIER_STOCK */
CREATE PROCEDURE VERIFIER_STOCK (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE,
    OLD_QTE NUMERIC (18, 6),
    NEW_QTE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VIR_FONDS_ID_VIR_AI */
CREATE PROCEDURE VIR_FONDS_ID_VIR_AI
RETURNS (
    NEW_VALUE INTEGER)
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VRF_FCH_STK_DPT_DTE_UPD_E */
CREATE PROCEDURE VRF_FCH_STK_DPT_DTE_UPD_E (
    OLD_DATE DATE,
    NEW_DATE DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    OLD_QUANTITE NUMERIC (18, 6),
    NEW_QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS
BEGIN
  EXIT;
END
^


/* Stored Procedure: VRF_FCH_STK_DPT_DTE_UPTE_S */
CREATE PROCEDURE VRF_FCH_STK_DPT_DTE_UPTE_S (
    OLD_DATE DATE,
    NEW_DATE DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    OLD_QUANTITE NUMERIC (18, 6),
    NEW_QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS
BEGIN
  EXIT;
END
^


ALTER PROCEDURE ACHATS_IMP_ART_FOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    TOTAL_QUANTITE NUMERIC (18, 6),
    MOYEN_P_U NUMERIC (18, 2),
    TOTAL_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               entrees.id_four,
               clients.denomination,
               entrees.id_article,
               articles.designation,
               sum(entrees.quantite) AS QTE,
               avg(entrees.cout_achat) AS P_U,
               sum(entrees.valeur_stock) AS Ht
        FROM entrees
        inner join articles on articles.id_art = entrees.id_article
        inner join clients on clients.id_client = entrees.id_four
        WHERE entrees.date_entree between :D1 AND :D2
        AND entrees.nature_entree = 1
        GROUP BY
              articles.designation,
              clients.denomination,
              entrees.id_four,
              entrees.id_article
        ORDER BY 2,4
        INTO :ID_F, :NOM_FOURN, :ID_A, :NOM_PRODUIT, :TOTAL_QUANTITE, :MOYEN_P_U, :TOTAL_HT
     DO
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE ACHATS_LOC_ART_FOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    TOTAL_QUANTITE NUMERIC (18, 6),
    MOYEN_P_U NUMERIC (18, 2),
    TOTAL_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               entrees.id_four,
               clients.denomination,
               entrees.id_article,
               articles.designation,
               sum(entrees.quantite) AS QTE,
               avg(entrees.p_u_da) AS P_U,
               sum(entrees.ht) AS Ht
        FROM entrees
        inner join articles on articles.id_art = entrees.id_article
        inner join clients on clients.id_client = entrees.id_four
        WHERE entrees.date_entree between :D1 AND :D2
        AND entrees.nature_entree = 2
        GROUP BY
              articles.designation,
              clients.denomination,
              entrees.id_four,
              entrees.id_article
        ORDER BY 2,4
        INTO :ID_F, :NOM_FOURN, :ID_A, :NOM_PRODUIT, :TOTAL_QUANTITE, :MOYEN_P_U, :TOTAL_HT
     DO
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE ARCHIVES_ID_ARCHIVE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ARCHIVES_ID_ARCHIVE_GEN, 1);
END
^


ALTER PROCEDURE ARTICLES_ID_ART_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ARTICLES_ID_ART_GEN, 1);
END
^


ALTER PROCEDURE BALANCE_CLIENTS (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_C INTEGER,
    NOM_CLIENT VARCHAR (128),
    ROTATION INTEGER,
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_VENTES NUMERIC (18, 2),
    TOTAL_AVOIRS NUMERIC (18, 2),
    TOTAL_REGLEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_ANT NUMERIC (18, 2);
    DECLARE variable VENTES_ANT NUMERIC (18, 2);
    DECLARE variable AVOIRS_ANT NUMERIC (18, 2);
    DECLARE variable REGLT_ANT NUMERIC (18, 2);
BEGIN
     FOR
       SELECT
              id_client
        FROM CLIENTS
        WHERE CLIENTS.solde_ini IS NOT null
        AND CLIENTS.solde_ini <> 0
        AND CLIENTS.client = 1

        UNION

        SELECT
              id_client
        FROM entete_sorties
        WHERE ID_CLIENT > 0
        AND   ajouter_creance = 1

        UNION

        SELECT
              id_tier
        FROM regl_tiers
        WHERE regl_tiers.id_tier > 0
        AND regl_tiers.type_regt = 1

        INTO :ID_C
     DO
     BEGIN
          SELECT
                CLIENTS.denomination,
                CLIENTS.solde_ini
          FROM CLIENTS
          WHERE CLIENTS.id_client = :ID_C
          INTO :NOM_CLIENT, :SOLDE_ANT;
          if (SOLDE_ANT IS NULL) then SOLDE_ANT = 0;

          SELECT
                SUM(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE entete_sorties.id_client = :ID_C
          AND entete_sorties.date_piece < :D1
          AND ((entete_sorties.nature_sortie = 2) OR (entete_sorties.nature_sortie = 1))
          INTO :VENTES_ANT;
          if (VENTES_ANT IS NULL) then VENTES_ANT = 0;

          SELECT
                SUM(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE entete_sorties.id_client = :ID_C
          AND entete_sorties.date_piece < :D1
          AND ((entete_sorties.nature_sortie = 5) OR (entete_sorties.nature_sortie = 7))
          INTO :AVOIRS_ANT;
          if (AVOIRS_ANT IS NULL) then AVOIRS_ANT = 0;

          SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_C
          AND regl_tiers.date_regt < :D1
          AND regl_tiers.type_regt = 1
          INTO :REGLT_ANT;
          if (REGLT_ANT IS NULL) then REGLT_ANT = 0;

          SOLDE_INI = :SOLDE_ANT + :VENTES_ANT - :AVOIRS_ANT - :REGLT_ANT;

          SELECT
                SUM(entete_sorties.total_facture),
                COUNT(entete_sorties.total_ht)
          FROM entete_sorties
          WHERE entete_sorties.id_client = :ID_C
          AND entete_sorties.date_piece between :D1 AND :D2
          AND ((entete_sorties.nature_sortie = 2) OR (entete_sorties.nature_sortie = 1))
          INTO :TOTAL_VENTES, :ROTATION;
          if (TOTAL_VENTES IS NULL) then TOTAL_VENTES = 0;

          SELECT
                SUM(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE entete_sorties.id_client = :ID_C
          AND entete_sorties.date_piece between :D1 AND :D2
          AND ((entete_sorties.nature_sortie = 5) OR (entete_sorties.nature_sortie = 7))
          INTO :TOTAL_AVOIRS;
          if (TOTAL_AVOIRS IS NULL) then TOTAL_AVOIRS = 0;

          SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_C
          AND regl_tiers.date_regt between :D1 AND :D2
          AND regl_tiers.type_regt = 1
          INTO :TOTAL_REGLEMENT;
          if (TOTAL_REGLEMENT IS NULL) then TOTAL_REGLEMENT = 0;

          SOLDE = :SOLDE_INI + :TOTAL_VENTES - :TOTAL_AVOIRS - :TOTAL_REGLEMENT;

  SUSPEND;
  END
END
^


ALTER PROCEDURE BALANCE_FOURNISSEURS_LOC (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_F INTEGER,
    NOM_FOURN VARCHAR (128),
    ROTATION INTEGER,
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_ACHATS NUMERIC (18, 2),
    TOTAL_AVOIRS NUMERIC (18, 2),
    TOTAL_REGLEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_ANT NUMERIC (18, 2);
    DECLARE variable ACHATS_ANT NUMERIC (18, 2);
    DECLARE variable AVOIRS_ANT NUMERIC (18, 2);
    DECLARE variable REGLT_ANT NUMERIC (18, 2);
BEGIN
     FOR
        SELECT
              id_client
        FROM CLIENTS
        WHERE (CLIENTS.solde_ini IS NOT null)
        AND (CLIENTS.solde_ini <> 0)
        AND (CLIENTS.client = 0)

        UNION

        SELECT
              id_FOURN
        FROM entete_entrees
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 2
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              id_tier
        FROM regl_tiers
        WHERE regl_tiers.id_tier > 0
        AND regl_tiers.type_regt = 0

        INTO :ID_F
     DO
     BEGIN
          SELECT
                CLIENTS.denomination,
                CLIENTS.solde_ini
          FROM CLIENTS
          WHERE CLIENTS.id_client = :ID_F
          INTO :NOM_FOURN, :SOLDE_ANT;
          if (SOLDE_ANT IS NULL) then SOLDE_ANT = 0;

          SELECT
                SUM(entete_entrees.net_a_payer)
          FROM entete_entrees
          WHERE entete_entrees.id_fourn = :ID_F
          AND entete_entrees.date_fact < :D1
          AND entete_entrees.nature_entree = 2
          INTO :ACHATS_ANT;
          if (ACHATS_ANT IS NULL) then ACHATS_ANT = 0;

          SELECT
                SUM(entete_entrees.net_a_payer)
          FROM entete_entrees
          WHERE entete_entrees.id_fourn = :ID_F
          AND entete_entrees.date_fact < :D1
          AND entete_entrees.nature_entree = 4
          INTO :AVOIRS_ANT;
          if (AVOIRS_ANT IS NULL) then AVOIRS_ANT = 0;

           SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_F
          AND regl_tiers.date_regt < :D1
          AND regl_tiers.type_regt = 0
          INTO :REGLT_ANT;
          if (REGLT_ANT IS NULL) then REGLT_ANT = 0;

          SOLDE_INI = :SOLDE_ANT + :ACHATS_ANT - :AVOIRS_ANT - :REGLT_ANT;

          SELECT
                SUM(entete_entrees.net_a_payer),
                COUNT(entete_entrees.total_ht)
          FROM entete_entrees
          WHERE entete_entrees.id_fourn = :ID_F
          AND entete_entrees.date_fact between :D1 AND :D2
          AND entete_entrees.nature_entree = 2
          INTO :TOTAL_ACHATS, :ROTATION;
          if (TOTAL_ACHATS IS NULL) then TOTAL_ACHATS = 0;

          SELECT
                SUM(entete_entrees.net_a_payer)
          FROM entete_entrees
          WHERE entete_entrees.id_fourn = :ID_F
          AND entete_entrees.date_fact between :D1 AND :D2
          AND entete_entrees.nature_entree = 4
          INTO :TOTAL_AVOIRS;
          if (TOTAL_AVOIRS IS NULL) then TOTAL_AVOIRS = 0;

          SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_F
          AND regl_tiers.date_regt between :D1 AND :D2
          AND regl_tiers.type_regt = 0
          INTO :TOTAL_REGLEMENT;
          if (TOTAL_REGLEMENT IS NULL) then TOTAL_REGLEMENT = 0;

          SOLDE = :SOLDE_INI + :TOTAL_ACHATS - :TOTAL_AVOIRS - :TOTAL_REGLEMENT;

  SUSPEND;
  END
END
^


ALTER PROCEDURE BALANCE_STOCK_MENSUEL
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    VAL_STK_INI NUMERIC (18, 2),
    ACHAT_IMPO NUMERIC (18, 2),
    ACHAT_LOC NUMERIC (18, 2),
    AVOIR_ACHAT NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    AVOIR_VENTE NUMERIC (18, 2),
    STOCK_FINAL NUMERIC (18, 2))
AS BEGIN
     SELECT
           SUM(inventaire.valeur)
     FROM inventaire
     INTO :VAL_STK_INI;
     STOCK_FINAL = :VAL_STK_INI;

     FOR
        SELECT distinct
               CODE,
               MOIS
        FROM PERIODES
        INTO :CODE_MOIS, :MOIS
    DO
    BEGIN
         VAL_STK_INI = :STOCK_FINAL;
         SELECT
               SUM(ENTETE_ENTREES.valeur_stock)
         FROM ENTETE_ENTREES
         WHERE extract(MONTH FROM DATE_D10) = :CODE_MOIS
         AND entete_entrees.nature_entree = 1
         INTO :ACHAT_IMPO;

         SELECT
               SUM(ENTETE_ENTREES.total_ht)
         FROM ENTETE_ENTREES
         WHERE extract(MONTH FROM DATE_FACT) = :CODE_MOIS
         AND entete_entrees.nature_entree = 2
         INTO :ACHAT_LOC;

         SELECT
               SUM(ENTETE_ENTREES.total_ht)
         FROM ENTETE_ENTREES
         WHERE extract(MONTH FROM DATE_FACT) = :CODE_MOIS
         AND entete_entrees.nature_entree = 4
         INTO :AVOIR_ACHAT;

         SELECT
               SUM(entete_sorties.total_consommation)
         FROM entete_sorties
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND entete_sorties.nature_sortie <> 5
         INTO :CONSOMMATIONS;

         SELECT
               SUM(entete_sorties.total_consommation)
         FROM entete_sorties
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND entete_sorties.nature_sortie = 5
         INTO :AVOIR_VENTE;

         if ( VAL_STK_INI IS NULL ) then VAL_STK_INI = 0;
         if ( ACHAT_IMPO IS NULL ) then ACHAT_IMPO = 0;
         if ( ACHAT_LOC IS NULL ) then ACHAT_LOC = 0;
         if ( AVOIR_ACHAT IS NULL ) then AVOIR_ACHAT = 0;
         if ( CONSOMMATIONS IS NULL ) then CONSOMMATIONS = 0;
         if ( AVOIR_VENTE IS NULL ) then AVOIR_VENTE = 0;
         STOCK_FINAL = :VAL_STK_INI + :ACHAT_IMPO + :ACHAT_LOC - AVOIR_ACHAT -
                       :CONSOMMATIONS + :AVOIR_VENTE;
  SUSPEND;
  END
END
^


ALTER PROCEDURE BALANCE_VENTES_MENSUEL
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    AVOIR NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    T_FACT NUMERIC (18, 2),
    T_REGT NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    CONS NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS DECLARE variable T_CONS NUMERIC (18, 2);
    DECLARE variable AV_CONS NUMERIC (18, 2);
BEGIN

     FOR
        SELECT distinct
               CODE,
               MOIS
        FROM PERIODES
        INTO :CODE_MOIS, :MOIS
     DO
     BEGIN
         SELECT
               SUM(entete_sorties.total_consommation),
               SUM(entete_sorties.total_ht),
               SUM(entete_sorties.total_tva),
               SUM(entete_sorties.total_TTC),
               SUM(entete_sorties.tIMBRE),
               SUM(entete_sorties.remise),
               SUM(entete_sorties.total_facture)
         FROM entete_sorties
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND ((entete_sorties.nature_sortie = 1) OR (entete_sorties.nature_sortie = 2))
         INTO :T_CONS, :HT, :TVA, :TTC, :TIMBRE, :REMISE, :T_FACT;

         SELECT
               SUM(entete_sorties.total_consommation),
               SUM(entete_sorties.total_ht)
         FROM entete_sorties
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND entete_sorties.nature_sortie = 5
         INTO :AV_CONS, :AVOIR;
         if (AVOIR IS NULL) then AVOIR = 0;
         if (HT IS NULL) then HT = 0;
         CA = :HT - :AVOIR;
         if (T_CONS IS NULL) then T_CONS = 0;
         if (AV_CONS IS NULL) then AV_CONS = 0;
         CONS = :T_CONS - :AV_CONS;
         MARGE = :CA - :CONS;
         SELECT
               SUM(regl_tiers.montant)
         FROM regl_tiers
         WHERE extract(MONTH FROM regl_tiers.date_regt) = :CODE_MOIS
         AND regl_tiers.type_regt = 1
         INTO :T_REGT;
  SUSPEND;
  END
END
^


ALTER PROCEDURE BASE_CALCUL_PAMP (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    DATE_OPER DATE,
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VALEUR_ENTREE NUMERIC (18, 2);
BEGIN
     STOCK = 0;
     VALEUR_STOCK = 0;
     FOR
        SELECT
              INVENTAIRE.id_inventaire,
              CAST(INVENTAIRE.date_inventaire AS DATE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN
        and inventaire.date_inventaire <= :d

        UNION all

        SELECT
              ENTREES.id_entree,
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        and entrees.date_entree <= :d

        UNION all

        SELECT
              ENTREES.id_entree,
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        and entrees.date_entree <= :d

        UNION all

        SELECT
              ENTREES.id_entree,
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        and entrees.date_entree <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION all

        SELECT
              ENTREES.id_entree,
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        and entrees.date_entree <= :d

        UNION all

        SELECT
              lignes_transfert.id_ligne,
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_1
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND entete_transfert.entree_stock = 1
        AND entete_transfert.date_piece <= :d

        UNION all

        SELECT
              entrees.id_entree,
              CAST(ENTREES.date_entree AS DATE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        and entrees.date_entree <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        and SORTIES.date_piece <= :d

        UNION

        SELECT
              lignes_transfert.id_ligne,
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_2
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND entete_transfert.sortie_stock = 1
        AND entete_transfert.date_piece <= :d

        ORDER BY 2,6,1

        INTO :ID_ES, :DATE_OPER, :ENTREE, :SORTIE, :COUT, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             if (:COUT IS NULL) then COUT = 0;

             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));

             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then
                P_A_M_P = cast(((:VALEUR_STOCK + cast(:ENTREE * :COUT AS numeric(18,2)))/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             SUSPEND;
        END
END
^


ALTER PROCEDURE BC_F_ID_BC_F_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(BC_F_ID_BC_F_GEN, 1);
END
^


ALTER PROCEDURE CALCUL_TIMBRE (
    TTC NUMERIC (18, 2))
RETURNS (
    TIMBRE NUMERIC (18, 2))
AS BEGIN
  TIMBRE = TTC * 1 / 100;
  if (TIMBRE < 5) then TIMBRE = 5;
  if (TIMBRE > 2500) then TIMBRE = 2500;
  SUSPEND;
END
^


ALTER PROCEDURE CAMION_ID_CAMION_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CAMION_ID_CAMION_GEN, 1);
END
^


ALTER PROCEDURE CAPROD_CACLIENT (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ID_ARTICLE INTEGER,
    REF_PRO VARCHAR (20),
    DESIGNATION VARCHAR (128),
    CACP NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    CAP NUMERIC (18, 2),
    P100CA NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               SORTIES.id_client,
               SORTIES.id_article,
               SUM(SORTIES.quantite),
               SUM(SORTIES.HT)
        FROM SORTIES
        WHERE SORTIES.nature_sortie IN (1,2)
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        GROUP BY SORTIES.id_client,
               SORTIES.id_article
        INTO :ID_CLIENT, :ID_ARTICLE, :QTE, :CACP
        do
        BEGIN
             SELECT
                   CLIENTS.denomination
             FROM CLIENTS
             WHERE CLIENTS.id_client = :id_client
             INTO :DENOMINATION;

             SELECT
                   ARTICLES.designation,
                   ARTICLES.ref
             FROM ARTICLES
             WHERE ARTICLES.id_art = :ID_ARTICLE
             INTO :DESIGNATION, :REF_PRO;

             SELECT
                   SUM(SORTIES.HT)
             FROM sorties
             WHERE SORTIES.nature_sortie IN (1,2)
             AND SORTIES.id_article = :ID_ARTICLE
             INTO :CAP;

             if (:CAP <> 0) then
                P100CA = CAST((:CACP / :CAP)*100 AS numeric(18,2));
             ELSE
                 P100CA = 0;
  SUSPEND;
  END
END
^


ALTER PROCEDURE CATALOGUE_ID_CAT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CATALOGUE_ID_CAT_GEN, 1);
END
^


ALTER PROCEDURE CA_TVA (
    ID INTEGER,
    TAUX NUMERIC (18, 2))
RETURNS (
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(sorties.ht),
           sum(sorties.tva)
     FROM sorties
     WHERE sorties.id_entete = :id
     AND sorties.taux_tva = :taux
     INTO :ca, :tva;
  SUSPEND;
END
^


ALTER PROCEDURE CHAUFFEURS_ID_CHAUFFEUR_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CHAUFFEURS_ID_CHAUFFEUR_GEN, 1);
END
^


ALTER PROCEDURE CHIFRRE_AFFAIRES
RETURNS (
    MOIS INTEGER,
    NOM_MOIS VARCHAR (20),
    CA_MOIS NUMERIC (18, 2))
AS BEGIN
     SELECT
           extract (month FROM entete_sorties.date_piece)
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie <> 5
     INTO :MOIS;

  SUSPEND;
END
^


ALTER PROCEDURE FACTURES_MOIS
RETURNS(
  ID_CLIENT INTEGER,
  DATE_FACT DATE,
  NATURE INTEGER,
  MOIS INTEGER,
  HT NUMERIC(18, 2),
  TVA NUMERIC(18, 2),
  TTC NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  REMISE NUMERIC(18, 2),
  TOTAL_FACT NUMERIC(18, 2))
AS
BEGIN
        SELECT
              entete_sorties.id_client,
              entete_sorties.date_piece,
              entete_sorties.nature_sortie,
              extract (month FROM entete_sorties.date_piece) AS MOIS,
              ENTETE_SORTIES.total_ht,
              ENTETE_SORTIES.total_TVA,
              ENTETE_SORTIES.total_TTC,
              ENTETE_SORTIES.TIMBRE,
              ENTETE_SORTIES.remise,
              ENTETE_SORTIES.total_facture
        FROM entete_sorties
        WHERE entete_sorties.nature_sortie <> 5
        AND ENTETE_SORTIES.total_ht <> 0
        INTO :ID_CLIENT, :DATE_FACT, :NATURE, :MOIS, :HT, :TVA, :TTC, :TIMBRE, :REMISE, :TOTAL_FACT;
        suspend;
END
^

ALTER PROCEDURE CHIFRRE_AFFAIRES_MOIS
RETURNS(
  MOIS INTEGER,
  NOM_MOIS VARCHAR(20),
  HT NUMERIC(18, 2),
  TVA NUMERIC(18, 2),
  TTC NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  REMISE NUMERIC(18, 2),
  TOTAL NUMERIC(18, 2))
AS
begin
FOR
        SELECT
              FACTURES_MOIS.MOIS,
              SUM(FACTURES_MOIS.ht),
              SUM(FACTURES_MOIS.tVA),
              SUM(FACTURES_MOIS.TTC),
              SUM(FACTURES_MOIS.TIMBRE),
              SUM(FACTURES_MOIS.REMISE),
              SUM(FACTURES_MOIS.total_fact)
        FROM FACTURES_MOIS
        WHERE FACTURES_MOIS.nature <> 5
        GROUP BY FACTURES_MOIS.MOIS
        INTO :MOIS, :HT, :TVA, :TTC, :TIMBRE, :REMISE, :TOTAL
     DO
     BEGIN
          if (MOIS = 1 ) then NOM_MOIS = 'JANVIER';
          if (MOIS = 1 ) then NOM_MOIS = 'FEVRIER';
          if (MOIS = 1 ) then NOM_MOIS = 'MARS';
          if (MOIS = 1 ) then NOM_MOIS = 'AVRIL';
          if (MOIS = 1 ) then NOM_MOIS = 'MAI';
          if (MOIS = 1 ) then NOM_MOIS = 'JUIN';
          if (MOIS = 1 ) then NOM_MOIS = 'JUILLET';
          if (MOIS = 1 ) then NOM_MOIS = 'AOUT';
          if (MOIS = 1 ) then NOM_MOIS = 'SEPTEMBRE';
          if (MOIS = 1 ) then NOM_MOIS = 'OCTOBRE';
          if (MOIS = 11) then NOM_MOIS = 'NOVEMBRE';
          if (MOIS = 12) then NOM_MOIS = 'DECEMBRE';
  SUSPEND;
  END
END
^


ALTER PROCEDURE CLIENTS_ID_CLIENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CLIENTS_ID_CLIENT_GEN, 1);
END
^


ALTER PROCEDURE CLIENTS_TRANCHES (
    DU DATE,
    AU DATE,
    MT1 NUMERIC (18, 2),
    MT2 NUMERIC (18, 2))
RETURNS (
    ID_CLIENT INTEGER,
    CLIENT VARCHAR (128),
    MONTANT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
               entete_sorties.id_client,
               SUM(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE entete_sorties.date_piece BETWEEN :DU AND :AU
        GROUP BY entete_sorties.id_client
        HAVING ((SUM(entete_sorties.total_facture) BETWEEN :MT1 AND :MT2)
                 AND (SUM(entete_sorties.total_facture) <> 0))
        ORDER BY 2
        INTO :ID_CLIENT, :MONTANT
     do
     BEGIN
          SELECT
                CLIENTS.denomination
          FROM CLIENTS
          WHERE CLIENTS.id_client = :ID_CLIENT
          INTO CLIENT;
  SUSPEND;
  END
END
^


ALTER PROCEDURE CODE_TAP_ID_TAP_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CODE_TAP_ID_TAP_GEN, 1);
END
^


ALTER PROCEDURE CODE_TVA_ID_TVA_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(CODE_TVA_ID_TVA_GEN, 1);
END
^


ALTER PROCEDURE COMPA_ACHATS (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_ENTREE INTEGER,
    NUM_FACT VARCHAR (12),
    INFO_REGLEMENT VARCHAR (128),
    DATE_FACT DATE,
    ID_FOURN INTEGER,
    CODE_TIER VARCHAR (12),
    CPTE_ACHAT VARCHAR (12),
    CPTE_STOCK VARCHAR (12),
    ACHAT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    AUTRES VARCHAR (12),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    CPTE_TIER VARCHAR (12),
    MNT_TIER NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS BEGIN
     FOR
        SELECT distinct
              ENTREES.id_entete,
              entete_entrees.num_fact,
              entete_entrees.info_reglement,
              ENTREES.id_four,
              ENTREES.date_entree,
              ARTICLES.cpte_achat,
              ARTICLES.cpte_stock,
              CLIENTS.cpte,
              entete_entrees.total_ht *100,
              entete_entrees.total_tva*100,
              entete_entrees.net_a_payer*100,
              entete_entrees.timbre*100,
              entete_entrees.remise*100,
              entete_entrees.autres_taxes*100,
              entete_entrees.denomination,
              CLIENTS.code
        FROM entete_entrees
        INNER JOIN entrees ON entete_entrees.id_ent_ent = ENTREES.id_entete
        INNER JOIN ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        INNER JOIN CLIENTS ON CLIENTS.id_client = ENTREES.id_four
        WHERE entrees.nature_entree = 2
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        INTO :ID_ENTET_ENTREE, :NUM_FACT, :info_reglement, :ID_FOURN,  :DATE_FACT, :CPTE_ACHAT, :CPTE_STOCK,
             :CPTE_TIER, :ACHAT, :TVA,:MNT_TIER, :TIMBRE, :REMISE, :AUTRES, :TIERS, :CODE_TIER
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_CONSOM_ANNUEL
RETURNS (
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSOM VARCHAR (12),
    CONSOM NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
              ARTICLES.cpte_stock,
              ARTICLES.cpte_consom,
              SUM(SORTIES.consomation)*100
        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        GROUP BY
              ARTICLES.cpte_consom, ARTICLES.cpte_stock

        INTO :CPTE_STOCK, :CPTE_CONSOM, :CONSOM
     do
     BEGIN


  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_CONSOM_MOIS
RETURNS (
    MOIS SMALLINT,
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSOM VARCHAR (12),
    CONSOM NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
              EXTRACT(MONTH FROM SORTIES.date_piece),
              ARTICLES.cpte_stock,
              ARTICLES.cpte_consom

        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        GROUP BY
              SORTIES.date_piece,
              ARTICLES.cpte_stock,
              ARTICLES.cpte_consom

        INTO :MOIS, :CPTE_STOCK, :CPTE_CONSOM
     do
     BEGIN
          SELECT
                SUM(SORTIES.consomation)*100
          FROM SORTIES
          INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
          WHERE EXTRACT(MONTH FROM SORTIES.date_piece) = :MOIS
          AND ARTICLES.cpte_stock = :CPTE_STOCK
          INTO :CONSOM;

  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_CONS_FACT (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_SORTIE INTEGER,
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    CPTE_STOCK VARCHAR (12),
    CPTE_CONSO VARCHAR (12),
    CONSOMATION NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              SORTIES.id_entete,
              entete_sorties.num_piece,
              SORTIES.date_piece,
              ARTICLES.cpte_stock,
              ARTICLES.cpte_consom,
              SUM(SORTIES.consomation)*100
        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        INNER JOIN entete_sorties ON entete_sorties.id_ent_sort = SORTIES.id_entete
        WHERE ARTICLES.gestion_stock = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        GROUP BY SORTIES.id_entete,
                 entete_sorties.num_piece,
                 sorties.id_client,
                 SORTIES.date_piece,
                 ARTICLES.cpte_stock,
                 ARTICLES.cpte_consom

        INTO :ID_ENTET_SORTIE, :NUM_FACT, :DATE_FACT,
             :CPTE_STOCK, :CPTE_CONSO, :CONSOMATION
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_DEPENSES (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_DEPENSE VARCHAR (12),
    DATE_DEP DATE,
    CPTE_CHARGE VARCHAR (12),
    CPTE_TRESOR VARCHAR (12),
    NUM_CHEQUE VARCHAR (7),
    MONTANT NUMERIC (18, 2),
    LIBELLE VARCHAR (128),
    TIERS VARCHAR (128))
AS DECLARE variable ID_TIERS INTEGER;
BEGIN
     FOR
        SELECT
              DEPENSES.num_dep,
              DEPENSES.id_tiers,
              DEPENSES.date_dep,
              DEPENSES.cpte_charge,
              comptes_tresorie.CPTE,
              DEPENSES.num_cheque,
              DEPENSES.montant * 100,
              DEPENSES.lib_dep
        FROM DEPENSES
        INNER JOIN comptes_tresorie ON comptes_tresorie.id_cpte = DEPENSES.id_cpte
        WHERE DEPENSES.date_dep BETWEEN :DU AND :AU
        INTO :NUM_DEPENSE, :ID_TIERS, :DATE_DEP,  :CPTE_CHARGE, :CPTE_TRESOR,
             :NUM_CHEQUE, :MONTANT, :LIBELLE
     do
     BEGIN
          SELECT
                CLIENTS.denomination
          FROM CLIENTS
          WHERE CLIENTS.id_client = :ID_TIERS
          INTO TIERS;
  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_ENTREE_STOCK (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_ENTREE INTEGER,
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    CPTE_ACHAT VARCHAR (12),
    CPTE_STOCK VARCHAR (12),
    ACHAT NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS BEGIN
     FOR
        SELECT
              ENTREES.id_entete,
              entete_entrees.num_fact,
              ENTREES.date_entree,
              ARTICLES.cpte_stock,
              ARTICLES.cpte_achat,
              SUM(ENTREES.ht) * 100,
              entete_entrees.denomination
        FROM ENTREES
        INNER JOIN ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        INNER JOIN entete_entrees ON entete_entrees.id_ent_ent = ENTREES.id_entete
        WHERE ENTREES.date_entree BETWEEN :DU AND :AU
        AND entrees.nature_entree = 2
        AND ARTICLES.gestion_stock = 1
        GROUP BY ENTREES.id_entete,
                 entete_entrees.num_fact,
                 ENTREES.date_entree,
                 ARTICLES.cpte_achat,
                 ARTICLES.cpte_stock,
                 ARTICLES.cpte_achat,
                 entete_entrees.denomination
                 INTO :ID_ENTET_ENTREE, :NUM_FACT, :DATE_FACT,
                      :CPTE_STOCK, :cpte_achat, :ACHAT, :TIERS
     do
     BEGIN

  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_REGL_TIERS (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_REGL VARCHAR (12),
    DATE_REGL DATE,
    ID_TIER INTEGER,
    CPTE_CLIENT VARCHAR (12),
    CPTE_TRESOR VARCHAR (12),
    NUM_CHEQUE VARCHAR (12),
    MONTANT NUMERIC (18, 2),
    TYPE_REG SMALLINT,
    TIERS VARCHAR (128))
AS BEGIN
     FOR
        SELECT
              regl_tiers.num_piece,
              regl_tiers.date_regt,
              regl_tiers.id_regl,
              CLIENTS.cpte,
              comptes_tresorie.CPTE,
              regl_tiers.num_cheque,
              regl_tiers.montant * 100,
              regl_tiers.type_regt,
              CLIENTS.denomination
        FROM regl_tiers
        INNER JOIN CLIENTS ON CLIENTS.id_client = regl_tiers.id_tier
        INNER JOIN comptes_tresorie ON comptes_tresorie.id_cpte = regl_tiers.id_cpte
        WHERE regl_tiers.date_regt between :DU AND :AU
        INTO :NUM_REGL, :DATE_REGL,  :ID_TIER, :CPTE_CLIENT, :CPTE_TRESOR,
             :NUM_CHEQUE, :MONTANT, :TYPE_REG, :TIERS
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ENTET_SORTIE INTEGER,
    CODE_TIER VARCHAR (12),
    NUM_FACT VARCHAR (12),
    DATE_FACT DATE,
    ID_CLIENT INTEGER,
    INFO_REGLEMENT VARCHAR (64),
    CPTE_CA VARCHAR (12),
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    CPTE_TIER VARCHAR (12),
    MNT_TIER NUMERIC (18, 2),
    TIERS VARCHAR (128))
AS BEGIN
     FOR
        SELECT distinct
              SORTIES.id_entete,
              entete_sorties.num_piece,
              Entete_sorties.info_reglement,
              sorties.id_client,
              SORTIES.date_piece,
              ARTICLES.cpte_vente,
              CLIENTS.cpte,
              sum(SORTIES.ht) *100,
              entete_sorties.total_tva *100,
              entete_sorties.total_facture*100,
              entete_sorties.timbre*100,
              entete_sorties.remise*100,
              entete_sorties.autres_taxes*100,
              clients.denomination,
              CLIENTS.code
        FROM SORTIES
        INNER JOIN entete_sorties ON entete_sorties.id_ent_sort = SORTIES.id_entete
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        INNER JOIN CLIENTS ON CLIENTS.id_client = SORTIES.id_client
        WHERE SORTIES.date_piece BETWEEN :DU AND :AU
        GROUP BY
        SORTIES.id_entete,
              entete_sorties.num_piece,
              Entete_sorties.info_reglement,
              sorties.id_client,
              SORTIES.date_piece,
              ARTICLES.cpte_vente,
              CLIENTS.cpte,
              entete_sorties.total_facture,
              entete_sorties.timbre,
              entete_sorties.remise,
              entete_sorties.autres_taxes,
              clients.denomination,
              entete_sorties.total_tva,
              CLIENTS.code
        INTO :ID_ENTET_SORTIE, :NUM_FACT, :info_reglement, :ID_CLIENT,  :DATE_FACT, :CPTE_CA,
             :CPTE_TIER, :CA, :TVA, :MNT_TIER, :TIMBRE, :REMISE, :AUTRES, :tiers, :CODE_TIER
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPA_VIREMENT (
    DU DATE,
    AU DATE)
RETURNS (
    NUM_VIR VARCHAR (12),
    DATE_VIR DATE,
    CPTE_CREDIT VARCHAR (12),
    CPTE_DEBIT VARCHAR (12),
    NUM_CHEQUE VARCHAR (12),
    MONTANT NUMERIC (18, 2))
AS DECLARE variable ID_CPTE_CREDIT integer;
DECLARE variable ID_CPTE_DEBIT integer;
BEGIN
     FOR
        SELECT
              vir_fonds.num_vir_fond,
              vir_fonds.date_oper,
              vir_fonds.id_compte_credit,
              vir_fonds.id_compte_debit,
              vir_fonds.num_cheque,
              vir_fonds.montant * 100
        FROM vir_fonds
        WHERE vir_fonds.date_oper BETWEEN :DU AND :AU
        INTO :NUM_VIR, :DATE_VIR,  :ID_CPTE_CREDIT, :ID_CPTE_DEBIT,
             :NUM_CHEQUE, :MONTANT
     do
     BEGIN
          SELECT
                comptes_tresorie.cpte
          FROM comptes_tresorie
          WHERE comptes_tresorie.id_cpte = :ID_CPTE_CREDIT
          INTO :CPTE_CREDIT;

          SELECT
                comptes_tresorie.cpte
          FROM comptes_tresorie
          WHERE comptes_tresorie.id_cpte = :ID_CPTE_DEBIT
          INTO :CPTE_DEBIT;

  SUSPEND;
  END
END
^


ALTER PROCEDURE COMPTES_TRESORIE_ID_CPTE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(COMPTES_TRESORIE_ID_CPTE_GEN, 1);
END
^


ALTER PROCEDURE CONSOMMATION (
    NATURE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    IMPORT INTEGER,
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               ARTICLES.importation,
               sorties.cr_u,
               sum(sorties.QUANTITE) AS qte,
               sum(sorties.consomation) AS con
        FROM sorties
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND SORTIES.nature_sortie = :NATURE
        GROUP BY ARTICLES.importation,sorties.id_article, sorties.cr_u,ARTICLES.designation

        INTO :ID_ART, :ARTICLE, :IMPORT, :COUT, :QTE, :VALEUR
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE CONSOMMATION_ALL (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    MATIERE INTEGER,
    IMPORT INTEGER,
    FP SMALLINT,
    GROS SMALLINT,
    QTE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS DECLARE variable QTE_CONS NUMERIC (18, 6);
  DECLARE variable QTE_AVOI NUMERIC (18, 6);
  DECLARE variable VAL_CONS NUMERIC (18, 2);
  DECLARE variable VAL_AVOI NUMERIC (18, 2);
BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               ARTICLES.matiere,
               ARTICLES.importation,
               sorties.gros,
               SORTIES.f_p
        FROM sorties
        left outer JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND articles.matiere <> 1
        INTO :ID_ART, :ARTICLE, :MATIERE, :IMPORT, :GROS, :FP

     do
     BEGIN
          SELECT distinct
               sum(sorties.QUANTITE),
               sum(sorties.consomation)
          FROM sorties
          WHERE sorties.date_piece between :d1 AND :d2
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.id_article = :ID_ART
          AND SORTIES.gros = :GROS
          AND SORTIES.f_p = :FP
          INTO :QTE_CONS, :VAL_CONS;

          SELECT distinct
               sum(sorties.QUANTITE),
               sum(sorties.consomation)
          FROM sorties
          WHERE sorties.date_piece between :d1 AND :d2
          AND SORTIES.nature_sortie = 5
          AND SORTIES.id_article = :ID_ART
          AND SORTIES.gros = :GROS
          AND SORTIES.f_p = :FP
          INTO :QTE_AVOI, :VAL_AVOI;

          if (QTE_CONS IS NULL) then QTE_CONS = 0;
          if (QTE_AVOI IS NULL) then QTE_AVOI = 0;
          if (VAL_CONS IS NULL) then VAL_CONS = 0;
          if (VAL_AVOI IS NULL) then VAL_AVOI = 0;

          QTE = :QTE_CONS - :QTE_AVOI;
          VALEUR = :VAL_CONS - :VAL_AVOI;
          if (QTE <> 0) then
             COUT = VALEUR / QTE;
          else
              cout = 0;
  SUSPEND;
  END
END
^


ALTER PROCEDURE CONSOMMATION_DEPOT (
    D1 DATE,
    D2 DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    MATIERE INTEGER,
    IMPORT INTEGER,
    FP SMALLINT,
    GROS SMALLINT,
    QTE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS DECLARE variable QTE_CONS NUMERIC (18, 6);
  DECLARE variable QTE_AVOI NUMERIC (18, 6);
  DECLARE variable VAL_CONS NUMERIC (18, 2);
  DECLARE variable VAL_AVOI NUMERIC (18, 2);
BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               ARTICLES.matiere,
               ARTICLES.importation,
               sorties.gros,
               SORTIES.f_p
        FROM sorties
        left outer JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND articles.matiere <> 1
        AND sorties.id_magazin = :id_magazin
        INTO :ID_ART, :ARTICLE, :MATIERE, :IMPORT, :GROS, :FP

     do
     BEGIN
          SELECT distinct
               sum(sorties.QUANTITE),
               sum(sorties.consomation)
          FROM sorties
          WHERE sorties.date_piece between :d1 AND :d2
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.id_article = :ID_ART
          AND SORTIES.gros = :GROS
          AND SORTIES.f_p = :FP
          INTO :QTE_CONS, :VAL_CONS;

          SELECT distinct
               sum(sorties.QUANTITE),
               sum(sorties.consomation)
          FROM sorties
          WHERE sorties.date_piece between :d1 AND :d2
          AND SORTIES.nature_sortie = 5
          AND SORTIES.id_article = :ID_ART
          AND SORTIES.gros = :GROS
          AND SORTIES.f_p = :FP
          INTO :QTE_AVOI, :VAL_AVOI;

          if (QTE_CONS IS NULL) then QTE_CONS = 0;
          if (QTE_AVOI IS NULL) then QTE_AVOI = 0;
          if (VAL_CONS IS NULL) then VAL_CONS = 0;
          if (VAL_AVOI IS NULL) then VAL_AVOI = 0;

          QTE = :QTE_CONS - :QTE_AVOI;
          VALEUR = :VAL_CONS - :VAL_AVOI;
          if (QTE <> 0) then
             COUT = VALEUR / QTE;
          else
              cout = 0;
  SUSPEND;
  END
END
^


ALTER PROCEDURE CONSOMMATION_IMPORT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               sorties.cr_u,
               sum(sorties.QUANTITE) AS qte,
               sum(sorties.consomation) AS con
        FROM sorties
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND ARTICLES.importation = 1
        GROUP BY sorties.id_article, sorties.cr_u,ARTICLES.designation

        INTO :ID_ART, :ARTICLE, :COUT, :QTE, :VALEUR
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE CONSOMMATION_LOCAL (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ARTICLE VARCHAR (128),
    COUT NUMERIC (18, 2),
    QTE NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               sorties.cr_u,
               sum(sorties.QUANTITE) AS qte,
               sum(sorties.consomation) AS con
        FROM sorties
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND ARTICLES.importation = 0
        GROUP BY sorties.id_article, sorties.cr_u,ARTICLES.designation

        INTO :ID_ART, :ARTICLE, :COUT, :QTE, :VALEUR
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE DEPENSES_ID_DEP_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(DEPENSES_ID_DEP_GEN, 1);
END
^


ALTER PROCEDURE DESTINATION_ID_DESTINATION_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(DESTINATION_ID_DESTINATION_GEN, 1);
END
^


ALTER PROCEDURE DEVISES_ID_DEVISE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(DEVISES_ID_DEVISE_GEN, 1);
END
^


ALTER PROCEDURE EMPLACEMENTS_ID_EMPL_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(EMPLACEMENTS_ID_EMPL_GEN, 1);
END
^


ALTER PROCEDURE ENTETE_ENTREES_ID_ENT_ENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ENTETE_ENTREES_ID_ENT_ENT_GEN, 1);
END
^


ALTER PROCEDURE ENTETE_SORTIES_ID_ENT_SORT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ENTETE_SORTIES_ID_ENT_SORT_GEN, 1);
END
^


ALTER PROCEDURE ENTETE_TRANSFERT_ID_ENTETE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ENTETE_TRANSFERT_ID_ENTETE_GEN, 1);
END
^


ALTER PROCEDURE ENTET_BC_F_ID_ENT_BD_F_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ENTET_BC_F_ID_ENT_BD_F_GEN, 1);
END
^


ALTER PROCEDURE ENTREES_ID_ENTREE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(ENTREES_ID_ENTREE_GEN, 1);
END
^

ALTER PROCEDURE MOUVEMENTS
RETURNS (
    ID_ARTICLE INTEGER,
    REF_ENTREE VARCHAR (12),
    ID_MAGAZIN INTEGER,
    DATE_OPER DATE,
    COUT NUMERIC (18, 2),
    QTE_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    QTE_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    QTE_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    SENS VARCHAR (1))
AS BEGIN
     FOR
     SELECT
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           INVENTAIRE.COUT,

           CAST(INVENTAIRE.quantite AS NUMERIC(18,6)),
           CAST(INVENTAIRE.quantite * COUT AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),
           '1'
     FROM inventaire
     WHERE inventaire.quantite <> 0

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.cout_achat,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           '2'
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree <> 4)

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           lignes_transfert.PAMP,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           '3'
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE entete_transfert.entree_stock = 1
     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.cr_u,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           '4'
     FROM sorties
     inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
     WHERE (SORTIES.destocker = 1) AND ((SORTIES.nature_sortie = 5)OR(SORTIES.nature_sortie = 7))

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           lignes_transfert.PAMP,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           '5'
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE entete_transfert.sortie_stock = 1

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.cr_u,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           '6'
     FROM sorties
     inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
     WHERE (SORTIES.destocker = 1) AND (SORTIES.nature_sortie <> 5) AND (SORTIES.nature_sortie <> 7)

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.cout_achat,

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           '7'
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree = 4)

     INTO :ID_ARTICLE, :REF_ENTREE, :ID_MAGAZIN, :DATE_OPER, :COUT, :QTE_INI, :VAL_INI, :QTE_ENT, :VAL_ENT, :QTE_SRT, :VAL_SRT, :SENS
     do
     BEGIN
          if (COUT IS NULL) then COUT = 0;
          if (QTE_INI IS NULL) then QTE_INI = 0;
          if (QTE_ENT IS NULL) then QTE_ENT = 0;
          if (QTE_SRT IS NULL) then QTE_SRT = 0;
          if (VAL_INI IS NULL) then VAL_INI = 0;
          if (VAL_ENT IS NULL) then VAL_ENT = 0;
          if (VAL_SRT IS NULL) then VAL_SRT = 0;
  SUSPEND;
  END
END
^


ALTER PROCEDURE MOUVEMENTS_DU_AU (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ARTICLE INTEGER,
    REF_ENTREE VARCHAR (12),
    ID_MAGAZIN INTEGER,
    DATE_OPER DATE,
    COUT NUMERIC (18, 2),
    INI_QTE_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_QTE_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_QTE_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    QTE_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    QTE_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    QTE_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    SENS VARCHAR (2))
AS BEGIN
     FOR
     SELECT
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           CAST(INVENTAIRE.COUT AS numeric(18,2)),

           CAST(INVENTAIRE.quantite AS NUMERIC(18,6)),
           CAST(INVENTAIRE.quantite * COUT AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),
           CAST('01' AS VARCHAR(2))
     FROM inventaire
     WHERE inventaire.quantite <> 0
     AND INVENTAIRE.date_inventaire  < :DU

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           CAST(ENTREES.cout_achat  AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('02' AS VARCHAR(2))
     FROM ENTREES
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree <> 4)
     AND ENTREES.date_entree  < :DU

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           CAST(lignes_transfert.PAMP AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('03' AS VARCHAR(2))
     FROM lignes_transfert
     WHERE lignes_transfert.date_piece  < :DU

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           CAST(SORTIES.cr_u AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('04' AS VARCHAR(2))
     FROM sorties
     WHERE (SORTIES.destocker = 1) AND ((SORTIES.nature_sortie = 5)OR(SORTIES.nature_sortie = 7))
     AND SORTIES.date_piece  < :DU

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           CAST(lignes_transfert.PAMP AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('05' AS VARCHAR(2))
     FROM lignes_transfert
     WHERE lignes_transfert.date_piece  < :DU

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           CAST(SORTIES.cr_u AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('06' AS VARCHAR(2))
     FROM sorties
     WHERE (SORTIES.destocker = 1) AND (SORTIES.nature_sortie <> 5) AND (SORTIES.nature_sortie <> 7)
     AND SORTIES.date_piece  < :DU

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           CAST(ENTREES.cout_achat AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('07' AS VARCHAR(2))
     FROM ENTREES
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree = 4)
     AND ENTREES.date_entree < :DU


     UNION ALL

     SELECT
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           CAST(INVENTAIRE.COUT AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(INVENTAIRE.quantite AS NUMERIC(18,6)),
           CAST(INVENTAIRE.quantite * COUT AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('08' AS VARCHAR(2))
     FROM inventaire
     WHERE inventaire.quantite <> 0
     AND INVENTAIRE.date_inventaire BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           CAST(ENTREES.cout_achat AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('09' AS VARCHAR(2))
     FROM ENTREES
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree <> 4)
     AND ENTREES.date_entree BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           CAST(lignes_transfert.PAMP AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('10' AS VARCHAR(2))
     FROM lignes_transfert
     WHERE lignes_transfert.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           CAST(SORTIES.cr_u AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST('11' AS VARCHAR(2))
     FROM sorties
     WHERE (SORTIES.destocker = 1) AND (SORTIES.nature_sortie = 5)
     AND SORTIES.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           CAST(lignes_transfert.PAMP AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(lignes_transfert.quantite AS NUMERIC(18,6)),
           CAST(lignes_transfert.quantite * lignes_transfert.PAMP AS NUMERIC(18,2)),

           CAST('12' AS VARCHAR(2))
     FROM lignes_transfert
     WHERE lignes_transfert.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           CAST(SORTIES.cr_u AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(SORTIES.quantite AS NUMERIC(18,6)),
           CAST(SORTIES.quantite * sorties.cr_u AS NUMERIC(18,2)),

           CAST('13' AS VARCHAR(2))
     FROM sorties
     WHERE (SORTIES.destocker = 1) AND (SORTIES.nature_sortie <> 5)
     AND SORTIES.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           CAST(ENTREES.cout_achat AS numeric(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(0 AS NUMERIC(18,6)),
           CAST(0 AS NUMERIC(18,2)),

           CAST(ENTREES.quantite AS NUMERIC(18,6)),
           CAST(ENTREES.quantite * ENTREES.cout_achat AS NUMERIC(18,2)),

           CAST('14' AS VARCHAR(2))
     FROM ENTREES
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree = 4)
     AND ENTREES.date_entree BETWEEN :DU AND :AU

     INTO :ID_ARTICLE, :REF_ENTREE, :ID_MAGAZIN, :DATE_OPER, :COUT,
          :INI_QTE_INI, :INI_VAL_INI, :INI_QTE_ENT, :INI_VAL_ENT, :INI_QTE_SRT, :INI_VAL_SRT,
          :QTE_INI, :VAL_INI, :QTE_ENT, :VAL_ENT, :QTE_SRT, :VAL_SRT, :SENS
     do
     BEGIN
          if (COUT IS NULL) then COUT = 0;
          if (QTE_INI IS NULL) then QTE_INI = 0;
          if (QTE_ENT IS NULL) then QTE_ENT = 0;
          if (QTE_SRT IS NULL) then QTE_SRT = 0;
          if (VAL_INI IS NULL) then VAL_INI = 0;
          if (VAL_ENT IS NULL) then VAL_ENT = 0;
          if (VAL_SRT IS NULL) then VAL_SRT = 0;
  SUSPEND;
  END
END
^


ALTER PROCEDURE EN_STOCK (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE)
RETURNS (
    STOCK NUMERIC (18, 6))
AS DECLARE variable INI NUMERIC(18,6) ;
  DECLARE variable ENT NUMERIC(18,6) ;
  DECLARE variable SRT NUMERIC(18,6) ;
BEGIN
     SELECT
           sum(mouvements.qte_ini),
           sum(mouvements.qte_ent),
           sum(mouvements.qte_srt)
     FROM mouvements
     WHERE mouvements.date_oper <= :DATE_OP
     AND mouvements.id_article = :ID_ART
     AND mouvements.id_magazin = :ID_MAG
     INTO :INI,  :ENT, :SRT;
     if (INI IS NULL) then INI = 0;
     if (ENT IS NULL) then ENT = 0;
     if (SRT IS NULL) then SRT = 0;
     STOCK = :INI + :ENT - :SRT;
  /* Procedure body */
  SUSPEND;
END
^


ALTER PROCEDURE EN_STOCK_REF_ENTREE (
    REF_ENTREE VARCHAR (12),
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE)
RETURNS (
    STOCK NUMERIC (18, 6))
AS DECLARE variable INI NUMERIC(18,6) ;
  DECLARE variable ENT NUMERIC(18,6) ;
  DECLARE variable SRT NUMERIC(18,6) ;
BEGIN
     SELECT
           sum(mouvements.qte_ini),
           sum(mouvements.qte_ent),
           sum(mouvements.qte_srt)
     FROM mouvements
     WHERE mouvements.date_oper <= :DATE_OP
     AND mouvements.id_article = :ID_ART
     AND mouvements.id_magazin = :ID_MAG
     AND mouvements.ref_entree = :REF_ENTREE
     INTO :INI,  :ENT, :SRT;
     if (INI IS NULL) then INI = 0;
     if (ENT IS NULL) then ENT = 0;
     if (SRT IS NULL) then SRT = 0;
     STOCK = :INI + :ENT - :SRT;
  /* Procedure body */
  SUSPEND;
END
^


ALTER PROCEDURE ETAT_104 (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
     UPDATE clients SET adresse1 = '' WHERE adresse1 IS null;
     UPDATE clients SET adresse2 = '' WHERE adresse2 IS null;
     FOR
     SELECT
           entete_sorties.id_client,
           cast(clients.denomination AS varchar(128)),
           cast(clients.adresse1||' '||clients.adresse2 AS varchar(128)),
           cast(clients.ref_rc  AS varchar(25)),
           cast(clients.date_rc AS date),
           cast(clients.i_f  AS varchar(20)),
           cast(clients.nis AS varchar(20)),
           cast(clients.ai AS varchar(20)),
           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_ht END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_ht END),

           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_tva END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_tva END )
     FROM entete_sorties
     inner join clients on clients.id_client = entete_sorties.id_client
     WHERE ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2
     GROUP BY ENTETE_SORTIES.id_client, clients.denomination,
              clients.adresse1, clients.adresse2,clients.adresse3,
              clients.ref_rc,
              clients.date_rc,
              clients.i_f,
              clients.nis,
              clients.ai
     ORDER BY 2
     INTO :id_client, :DENOMINATION, :ADRESSE, :R_C, :DATE_RC, :I_F, :NIS, :AI, :AV, :CA, :tvaAV, :TVACA
     do
     BEGIN
          if (AV IS NULL) then AV = 0;
          if (CA IS NULL) then CA = 0;
          HT = :CA - :AV;
          if (TVACA IS NULL) then TVACA = 0;
          if (TVAAV IS NULL) then TVAAV = 0;
          TVA = :TVACA - :TVAAV;
  SUSPEND;
  END
END
^

ALTER PROCEDURE ETAT_104_MAGASIN(
  D1 DATE,
  D2 DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(128),
  R_C VARCHAR(25),
  DATE_RC DATE,
  I_F VARCHAR(20),
  NIS VARCHAR(20),
  AI VARCHAR(20),
  AV NUMERIC(18, 2),
  CA NUMERIC(18, 2),
  TVAAV NUMERIC(18, 2),
  TVACA NUMERIC(18, 2),
  HT NUMERIC(18, 2),
  TVA NUMERIC(18, 2),
  SERVICE_GESTION VARCHAR(64))
AS
BEGIN
     UPDATE clients SET adresse1 = '' WHERE adresse1 IS null;
     UPDATE clients SET adresse2 = '' WHERE adresse2 IS null;
     FOR
     SELECT
           entete_sorties.id_client,
           cast(clients.denomination AS varchar(128)),
           cast(clients.adresse1||' '||clients.adresse2 AS varchar(128)),
           cast(clients.ref_rc  AS varchar(25)),
           cast(clients.date_rc AS date),
           cast(clients.i_f  AS varchar(20)),
           cast(clients.nis AS varchar(20)),
           cast(clients.ai AS varchar(20)),
           sum(CASE WHEN (entete_sorties.nature_sortie = 5) THEN entete_sorties.total_ht END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_ht END),

           sum(CASE WHEN (entete_sorties.nature_sortie = 5) THEN entete_sorties.total_tva END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_tva END ),
           CLIENTS.SERVICE_GESTION
     FROM entete_sorties
     inner join clients on clients.id_client = entete_sorties.id_client
     WHERE ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2
	 AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAGAZIN
     AND ENTETE_SORTIES.ID_CLIENT > 0
     GROUP BY ENTETE_SORTIES.id_client, clients.denomination,
              clients.adresse1, clients.adresse2,clients.adresse3,
              clients.ref_rc,
              clients.date_rc,
              clients.i_f,
              clients.nis,
              clients.ai,
              CLIENTS.SERVICE_GESTION
     ORDER BY 2
     INTO :id_client, :DENOMINATION, :ADRESSE, :R_C, :DATE_RC, :I_F, :NIS, :AI, :AV, :CA, :tvaAV, :TVACA, :SERVICE_GESTION
     do
     BEGIN
          if (AV IS NULL) then AV = 0;
          if (CA IS NULL) then CA = 0;
          HT = :CA - :AV;
          if (TVACA IS NULL) then TVACA = 0;
          if (TVAAV IS NULL) then TVAAV = 0;
          TVA = :TVACA - :TVAAV;
  SUSPEND;
  END
END^

ALTER PROCEDURE ETAT_104_GROS (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
     UPDATE clients SET adresse1 = '' WHERE adresse1 IS null;
     UPDATE clients SET adresse2 = '' WHERE adresse2 IS null;
     FOR
     SELECT
           entete_sorties.id_client,
           cast(clients.denomination AS varchar(128)),
           cast(clients.adresse1||' '||clients.adresse2 AS varchar(128)),
           cast(clients.ref_rc  AS varchar(25)),
           cast(clients.date_rc AS date),
           cast(clients.i_f  AS varchar(20)),
           cast(clients.nis AS varchar(20)),
           cast(clients.ai AS varchar(20)),
           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_ht END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_ht END),

           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_tva END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_tva END )
     FROM entete_sorties
     inner join clients on clients.id_client = entete_sorties.id_client
     WHERE ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2
	AND ENTETE_SORTIES.refaction = 1
     GROUP BY ENTETE_SORTIES.id_client, clients.denomination,
              clients.adresse1, clients.adresse2,clients.adresse3,
              clients.ref_rc,
              clients.date_rc,
              clients.i_f,
              clients.nis,
              clients.ai
     ORDER BY 2
     INTO :id_client, :DENOMINATION, :ADRESSE, :R_C, :DATE_RC, :I_F, :NIS, :AI, :AV, :CA, :tvaAV, :TVACA
     do
     BEGIN
          if (AV IS NULL) then AV = 0;
          if (CA IS NULL) then CA = 0;
          HT = :CA - :AV;
          if (TVACA IS NULL) then TVACA = 0;
          if (TVAAV IS NULL) then TVAAV = 0;
          TVA = :TVACA - :TVAAV;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_104_PART (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    R_C VARCHAR (25),
    DATE_RC DATE,
    I_F VARCHAR (20),
    NIS VARCHAR (20),
    AI VARCHAR (20),
    AV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVAAV NUMERIC (18, 2),
    TVACA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
     FOR
     SELECT
           entete_sorties.id_client,
           cast(clients.denomination AS varchar(128)),
           cast(clients.adresse1 AS varchar(128)),
           cast(clients.ref_rc  AS varchar(25)),
           cast(clients.date_rc AS date),
           cast(clients.i_f  AS varchar(20)),
           cast(clients.nis AS varchar(20)),
           cast(clients.ai AS varchar(20)),
           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_ht END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_ht END),

           sum(CASE WHEN entete_sorties.nature_sortie = 5 THEN entete_sorties.total_tva END),
           sum(CASE WHEN (entete_sorties.cession = 0)AND(entete_sorties.nature_sortie = 1) THEN entete_sorties.total_tva END )
     FROM entete_sorties
     inner join clients on clients.id_client = entete_sorties.id_client
     WHERE ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2
     AND clients.particulier = 1
     GROUP BY ENTETE_SORTIES.id_client, clients.denomination,
              clients.adresse1, clients.adresse2,clients.adresse3,
              clients.ref_rc,
              clients.date_rc,
              clients.i_f,
              clients.nis,
              clients.ai
     ORDER BY 2
     INTO :id_client, :DENOMINATION, :ADRESSE, :R_C, :DATE_RC, :I_F, :NIS, :AI, :AV, :CA, :tvaAV, :TVACA
     do
     BEGIN
          if (AV IS NULL) then AV = 0;
          if (CA IS NULL) then CA = 0;
          HT = :CA - :AV;
          if (TVACA IS NULL) then TVACA = 0;
          if (TVAAV IS NULL) then TVAAV = 0;
          TVA = :TVACA - :TVAAV;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_CREANCES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    CREANCE NUMERIC (18, 2),
    REGLEMENT NUMERIC (18, 2),
    ROTATION NUMERIC(18, 2),
    ECART NUMERIC (18, 2))
AS DECLARE VARIABLE VENTE_ANT NUMERIC(18, 2);
DECLARE VARIABLE REGLT_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIR NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_VENTE NUMERIC(18, 2);
DECLARE VARIABLE AV_COM NUMERIC(18, 2);
BEGIN
     FOR
        SELECT
              id_client
        FROM CLIENTS
        WHERE CLIENTS.solde_ini IS NOT null
        AND CLIENTS.solde_ini <> 0
        AND CLIENTS.client = 1

        UNION

        SELECT
              id_client
        FROM entete_sorties
        WHERE ID_CLIENT > 0
        AND   ajouter_creance = 1

        UNION

        SELECT
              id_tier
        FROM regl_tiers
        WHERE regl_tiers.id_tier > 0
        AND (regl_tiers.type_regt = 1) OR (regl_tiers.type_regt = 2)
        INTO :ID_TIER

     DO
       BEGIN
            SELECT
                  SOLDE_INI,
                  denomination
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :SOLDE_INI, :DENOMINATION;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie <> 5)AND(ENTETE_SORTIES.nature_sortie <> 7))
            AND entete_sorties.date_piece < :D1
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_VENTE;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
            AND entete_sorties.date_piece < :D1
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_AVOIR;

            if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
            VENTE_ANT = :TOTAL_VENTE - :TOTAL_AVOIR;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 1)
            AND id_tier <> -1
            AND regl_tiers.date_regt < :D1
            INTO :REGLT_ANT;
            IF (REGLT_ANT is null) then REGLT_ANT = 0;

             SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 2)
            AND id_tier <> -1
            AND regl_tiers.date_regt < :D1
            INTO :AV_COM;
            IF (AV_COM is null) then AV_COM = 0;
            REGLT_ANT = :REGLT_ANT + :AV_COM;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie <> 5)AND(ENTETE_SORTIES.nature_sortie <> 7))
            AND entete_sorties.date_piece BETWEEN :D1 AND :D2
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_VENTE;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
            AND entete_sorties.date_piece BETWEEN :D1 AND :D2
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_AVOIR;

            if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
            CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 1)
            AND id_tier <> -1
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :REGLEMENT;
            IF (REGLEMENT is null) then REGLEMENT = 0;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 2)
            AND id_tier <> -1
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :AV_COM;
            IF (AV_COM is null) then AV_COM = 0;

            REGLEMENT = :REGLEMENT + :AV_COM;


            if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
            if (VENTE_ANT IS NULL) THEN VENTE_ANT = 0;
            if (REGLT_ANT IS NULL) then REGLT_ANT = 0;
            SOLDE_INI = :SOLDE_INI + :VENTE_ANT + :REGLT_ANT;
            if (CREANCE IS NULL) then CREANCE = 0;
            if (REGLEMENT IS NULL) then REGLEMENT = 0;
            ECART = :SOLDE_INI + :CREANCE - :REGLEMENT;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_DETTES_LOCALES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    ACHATS NUMERIC (18, 2),
    REGLEMENT NUMERIC (18, 2),
    ECART NUMERIC (18, 2))
AS DECLARE VARIABLE FACT_D_ANT NUMERIC(18, 2);
DECLARE VARIABLE ACHAT_ANT NUMERIC(18, 2);
DECLARE VARIABLE IMPOR_ANT NUMERIC(18, 2);
DECLARE VARIABLE AVOIR_ANT NUMERIC(18, 2);
DECLARE VARIABLE REGLT_ANT NUMERIC(18, 2);
DECLARE VARIABLE FACT_DD NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_ACHAT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_IMPOR NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIR NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_DEP NUMERIC(18, 2);
DECLARE VARIABLE AV_COM NUMERIC(18, 2);
BEGIN
      REGLEMENT = 0;
     FOR
        SELECT
              id_client
        FROM CLIENTS
        WHERE (CLIENTS.solde_ini IS NOT null)
        AND (CLIENTS.solde_ini <> 0)
        AND (CLIENTS.client = 0)

        UNION

        SELECT
              id_FOURN
        FROM entete_entrees
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 2
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              id_FOURN
        FROM entete_entrees
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 1
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              fact_dd_jointe.id_fourn
        FROM fact_dd_jointe

        UNION

        SELECT
              id_tier
        FROM regl_tiers
        WHERE regl_tiers.id_tier > 0
        AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 3))

        UNION

        SELECT
              DEPENSES.ID_TIERS
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS is not null

        INTO :ID_TIER
     DO
       BEGIN
            SELECT
                  DENOMINATION,
                  SOLDE_INI
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :DENOMINATION, :SOLDE_INI;
            if (SOLDE_INI IS NULL) then SOLDE_INI =0;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (entete_entrees.nature_entree = 2)
            AND entete_entrees.ajouter_dettes = 1
            AND entete_entrees.date_fact < :D1
            INTO :ACHAT_ANT;
            if (ACHAT_ANT IS NULL) then ACHAT_ANT =0;

            SELECT
                  sum(entete_entrees.CV_DINARS)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (entete_entrees.nature_entree = 1)
            AND entete_entrees.ajouter_dettes = 1
            AND entete_entrees.date_fact < :D1
            INTO :IMPOR_ANT ;
            if (IMPOR_ANT IS NULL) then IMPOR_ANT =0;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 4)
            AND entete_entrees.ajouter_dettes = 1
            AND entete_entrees.date_fact < :D1
            INTO :AVOIR_ANT;
            if (AVOIR_ANT IS NULL) then AVOIR_ANT =0;

            SELECT
                  sum(fact_dd_jointe.TTC)
            FROM fact_dd_jointe
            WHERE fact_dd_jointe.id_fourn = :ID_TIER
            AND fact_dd_jointe.date_fact < :D1
            INTO :FACT_D_ANT ;
            if (FACT_D_ANT IS NULL) then FACT_D_ANT =0;

            SELECT
                  sum(DEPENSES.MONTANT)
            FROM DEPENSES
            WHERE DEPENSES.ID_TIERS = :ID_TIER
            AND DEPENSES.DATE_DEP < :D1
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

            SELECT
                  SUM(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 0)
            AND regl_tiers.date_regt < :D1
            INTO :REGLT_ANT;
            if (REGLT_ANT IS NULL) then REGLT_ANT = 0;

             SELECT
                  SUM(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 3)
            AND regl_tiers.date_regt < :D1
            INTO :AV_COM;
            if (AV_COM IS NULL) then AV_COM = 0;
            REGLT_ANT = :REGLT_ANT + :AV_COM;

            SOLDE_INI = :SOLDE_INI + :ACHAT_ANT + :IMPOR_ANT + :FACT_D_ANT + :TOTAL_DEP - :AVOIR_ANT - :REGLT_ANT;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 2)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_ACHAT;
            if (TOTAL_ACHAT IS NULL) then TOTAL_ACHAT = 0;

            SELECT
                  sum(entete_entrees.CV_DINARS)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 1)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_IMPOR;
            if (TOTAL_IMPOR IS NULL) then TOTAL_IMPOR = 0;


            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 4)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_AVOIR;
            if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;

            SELECT
                  sum(fact_dd_jointe.TTC)
            FROM fact_dd_jointe
            WHERE fact_dd_jointe.id_fourn = :ID_TIER
            AND fact_dd_jointe.date_fact BETWEEN :D1 AND :D2
            INTO :FACT_DD ;
            if (FACT_DD IS NULL) then FACT_DD =0;

            SELECT
                  sum(DEPENSES.MONTANT)
            FROM DEPENSES
            WHERE DEPENSES.ID_TIERS = :ID_TIER
            AND DEPENSES.DATE_DEP BETWEEN :D1 AND :D2
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

            ACHATS = :TOTAL_ACHAT  + :TOTAL_IMPOR + :TOTAL_DEP + :FACT_DD - :TOTAL_AVOIR;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND regl_tiers.type_regt = 0
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :REGLEMENT;
            if (REGLEMENT IS NULL) then REGLEMENT = 0;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND regl_tiers.type_regt = 3
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :av_com;
            if (av_com IS NULL) then av_com = 0;
            REGLEMENT = :REGLEMENT + :av_com ;

            ECART = :SOLDE_INI + :ACHATS - :REGLEMENT;
  SUSPEND;
  END
  END
^


ALTER PROCEDURE ETAT_DETTE_CREANCES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_TIER INTEGER,
    DENOMINATION VARCHAR (128),
    SOLDE_INI NUMERIC (18, 2),
    CREANCE NUMERIC (18, 2),
    REGLEMENT_CLIENT NUMERIC (18, 2),
    ECART_CREANCE NUMERIC (18, 2),
    ACHATS NUMERIC (18, 2),
    REGLEMENT_FOURNISSEUR NUMERIC (18, 2),
    ECART_DETTE NUMERIC (18, 2),
    ECART NUMERIC (18, 2))
AS DECLARE variable VENTE_ANT NUMERIC (18, 2);
  DECLARE variable REGLT_CL_ANT NUMERIC (18, 2);
  DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR_CL NUMERIC (18, 2);

  DECLARE variable FACT_D_ANT NUMERIC (18, 2);
  DECLARE variable ACHAT_ANT NUMERIC (18, 2);
  DECLARE variable AVOIR_ANT NUMERIC (18, 2);
  DECLARE variable REGLT_FR_ANT NUMERIC (18, 2);
  DECLARE variable FACT_DD NUMERIC (18, 2);
  DECLARE variable TOTAL_ACHAT NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR_FR NUMERIC (18, 2);

BEGIN
     FOR
        SELECT
              id_client
        FROM CLIENTS
        WHERE CLIENTS.solde_ini IS NOT null
        AND CLIENTS.mixte = 1
        INTO :ID_TIER

     DO
       BEGIN
            SELECT
                  SOLDE_INI,
                  denomination
            FROM CLIENTS
            WHERE CLIENTS.ID_CLIENT = :ID_TIER
            INTO :SOLDE_INI, :DENOMINATION;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE entete_sorties.id_client = :ID_TIER
            AND ENTETE_SORTIES.nature_sortie <> 5
            AND ENTETE_SORTIES.nature_sortie <> 7
            AND entete_sorties.date_piece < :D1
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_VENTE;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE entete_sorties.id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
            AND entete_sorties.date_piece < :D1
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_AVOIR_CL;

            if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR_CL IS NULL) then TOTAL_AVOIR_CL = 0;
            VENTE_ANT = :TOTAL_VENTE - :TOTAL_AVOIR_CL;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
            AND regl_tiers.id_tier <> -1
            AND regl_tiers.date_regt < :D1
            INTO :REGLT_CL_ANT;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE entete_sorties.id_client = :ID_TIER
            AND ENTETE_SORTIES.nature_sortie <> 5
            AND ENTETE_SORTIES.nature_sortie <> 7
            AND entete_sorties.date_piece BETWEEN :D1 AND :D2
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_VENTE;

            SELECT
                  sum(entete_sorties.total_facture)
            FROM entete_sorties
            WHERE entete_sorties.id_client = :ID_TIER
            AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
            AND entete_sorties.date_piece BETWEEN :D1 AND :D2
            AND entete_sorties.ajouter_creance = 1
            INTO :TOTAL_AVOIR_CL;

            if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR_CL IS NULL) then TOTAL_AVOIR_CL = 0;
            CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR_CL;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
            AND regl_tiers.id_tier <> -1
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :REGLEMENT_CLIENT;

            if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
            if (VENTE_ANT IS NULL) THEN VENTE_ANT = 0;
            if (REGLT_CL_ANT IS NULL) then REGLT_CL_ANT = 0;
            SOLDE_INI = :SOLDE_INI + :VENTE_ANT + :REGLT_CL_ANT;
            if (CREANCE IS NULL) then CREANCE = 0;
            if (REGLEMENT_CLIENT IS NULL) then REGLEMENT_CLIENT = 0;
            ECART_CREANCE = :SOLDE_INI + :CREANCE - :REGLEMENT_CLIENT;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree <> 4)
            AND entete_entrees.ajouter_dettes = 1
            AND entete_entrees.date_fact < :D1
            INTO :ACHAT_ANT ;
            if (ACHAT_ANT IS NULL) then ACHAT_ANT =0;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 4)
            AND entete_entrees.ajouter_dettes = 1
            AND entete_entrees.date_fact < :D1
            INTO :AVOIR_ANT;
            if (AVOIR_ANT IS NULL) then AVOIR_ANT =0;

            SELECT
                  sum(fact_dd_jointe.TTC)
            FROM fact_dd_jointe
            WHERE fact_dd_jointe.id_fourn = :ID_TIER
            AND fact_dd_jointe.date_fact < :D1
            INTO :FACT_D_ANT ;
            if (FACT_D_ANT IS NULL) then FACT_D_ANT =0;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 3))
            AND id_tier <> -1
            AND regl_tiers.date_regt < :D1
            INTO :REGLT_FR_ANT;
            if (REGLT_FR_ANT IS NULL) then REGLT_FR_ANT = 0;

            SOLDE_INI = :SOLDE_INI + :ACHAT_ANT + :FACT_D_ANT - :AVOIR_ANT - :REGLT_FR_ANT;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 2)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_ACHAT;
            if (TOTAL_ACHAT IS NULL) then TOTAL_ACHAT = 0;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND (nature_entree = 4)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_AVOIR_FR;
            if (TOTAL_AVOIR_FR IS NULL) then TOTAL_AVOIR_FR = 0;

            SELECT
                  sum(fact_dd_jointe.TTC)
            FROM fact_dd_jointe
            WHERE fact_dd_jointe.id_fourn = :ID_TIER
            AND fact_dd_jointe.date_fact BETWEEN :D1 AND :D2
            INTO :FACT_DD ;
            if (FACT_DD IS NULL) then FACT_DD =0;

            ACHATS = :TOTAL_ACHAT + :FACT_DD - :TOTAL_AVOIR_FR;

            SELECT
                  sum(regl_tiers.montant)
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 3))
            AND id_tier <> -1
            AND regl_tiers.date_regt BETWEEN :D1 AND :D2
            INTO :REGLEMENT_FOURNISSEUR;
            if (REGLEMENT_FOURNISSEUR IS NULL) then REGLEMENT_FOURNISSEUR = 0;

            ECART_DETTE = :SOLDE_INI + :ACHATS - :REGLEMENT_FOURNISSEUR;
            ECART = ECART_CREANCE - ECART_DETTE;
            END
  SUSPEND;
END
^


ALTER PROCEDURE ETAT_STOCK (
    D DATE)
RETURNS (
    ID_MAGAZIN INTEGER,
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.id_magazin,
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),

              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        INNER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        INNER join unites on unites.id_unite = articles.id_unite
        INNER join familles on familles.id_famille = articles.id_famille
        WHERE articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvements.date_oper <= :D
        GROUP BY mouvements.id_magazin,
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES
          ORDER BY 3
        INTO :ID_MAGAZIN, :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT
        DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
       if (NBRE_CARTON is null) then NBRE_CARTON = 0;
       if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

  	   if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;

       /*EXECUTE PROCEDURE pamp_depot(:ID_ART, :ID_MAGAZIN, :D) RETURNING_VALUES :cout;*/
       valeur = :stock * :cout;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_STOCK_DEPOT (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_MIN NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              ARTICLES.STOCK_MIN,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),

              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)

        FROM mouvements
        inner JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        inner join unites on (unites.id_unite = articles.id_unite)
        inner join familles on (familles.id_famille = articles.id_famille)
        WHERE articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvements.date_oper <= :D
        AND mouvements.id_magazin = :ID_MAGAZIN
        GROUP BY mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              ARTICLES.STOCK_MIN,
              ARTICLES.taux_tva
          ORDER BY 3
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :STOCK_MIN,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT
         DO
  BEGIN

    /*   EXECUTE PROCEDURE pamp_depot(:ID_ART, :ID_MAGAZIN, :D) RETURNING_VALUES :cout;*/

       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
	   if (NBRE_CARTON is null) then NBRE_CARTON = 0;
       if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;
       if (STOCK is null) then STOCK = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;

       VALEUR = :cout * :stock;
       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_TRESORERIE
RETURNS (
    ID_CPTE INTEGER,
    CPTE VARCHAR (64),
    SOLDE_INI NUMERIC (18, 2),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2))
AS DECLARE VARIABLE  SD_INI NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_RECETTES NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_DEPENSES NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_RGLT_RECETTES NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_RGLT_DEPENSES NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_VIR_RECETTES NUMERIC (18, 2);
    DECLARE VARIABLE  TOTAL_VIR_DEPENSES NUMERIC (18, 2);
BEGIN
     FOR
        SELECT
              CAST(comptes_tresorie.id_cpte AS INTEGER),
              CAST(comptes_tresorie.nom_cpte AS VARCHAR(64))
        FROM comptes_tresorie
        INTO :ID_CPTE, :CPTE
     do
     BEGIN
          EXECUTE PROCEDURE total_solde_ini_cpte(:ID_CPTE) RETURNING_VALUES :SD_INI;
          EXECUTE PROCEDURE total_recettes_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_RECETTES;
          EXECUTE PROCEDURE total_DEPENSES_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_DEPENSES;
          EXECUTE PROCEDURE total_reglt_recette_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_RGLT_RECETTES;
          EXECUTE PROCEDURE total_reglt_repense_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_RGLT_DEPENSES;
          EXECUTE PROCEDURE total_vir_fond_recette_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_VIR_RECETTES;
          EXECUTE PROCEDURE total_vir_fond_depense_cpte(:ID_CPTE) RETURNING_VALUES :TOTAL_VIR_DEPENSES;
          if (SD_INI IS NULL) then SD_INI = 0;
          if (TOTAL_RECETTES IS NULL) then TOTAL_RECETTES = 0;
          if (TOTAL_DEPENSES IS NULL) then TOTAL_DEPENSES = 0;
          if (TOTAL_RGLT_RECETTES IS NULL) then TOTAL_RGLT_RECETTES = 0;
          if (TOTAL_RGLT_DEPENSES IS NULL) then TOTAL_RGLT_DEPENSES = 0;
          if (TOTAL_VIR_RECETTES IS NULL) then TOTAL_VIR_RECETTES = 0;
          if (TOTAL_VIR_DEPENSES IS NULL) then TOTAL_VIR_DEPENSES = 0;
          SOLDE_INI = :SD_INI;
          RECETTE = :TOTAL_RECETTES + :TOTAL_RGLT_RECETTES + :TOTAL_VIR_RECETTES;
          DEPENSE = :TOTAL_DEPENSES + :TOTAL_RGLT_DEPENSES + :TOTAL_VIR_DEPENSES;
          SOLDE = :SOLDE_INI + :RECETTE - :DEPENSE;
          if (:RECETTE = 0) then RECETTE = NULL;
          if (:DEPENSE = 0) then DEPENSE = NULL;
          if (:SOLDE = 0) then SOLDE = NULL;
  SUSPEND;
  END
END
^


ALTER PROCEDURE ETAT_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ADRESSE VARCHAR (128),
    ACTIVITE VARCHAR (128),
    R_C VARCHAR (32),
    A_I VARCHAR (20),
    I_F VARCHAR (20),
    QTE1 NUMERIC (18, 2),
    QTE2 NUMERIC (18, 2),
    QTE3 NUMERIC (18, 2),
    QTE4 NUMERIC (18, 2),
    QTE5 NUMERIC (18, 2),
    QTE6 NUMERIC (18, 2),
    QTE7 NUMERIC (18, 2),
    QTE8 NUMERIC (18, 2),
    QTE9 NUMERIC (18, 2),
    QTE10 NUMERIC (18, 2))
AS DECLARE variable RUB INTEGER;
  DECLARE variable RUB1 INTEGER;
  DECLARE variable RUB2 INTEGER;
  DECLARE variable RUB3 INTEGER;
  DECLARE variable RUB4 INTEGER;
  DECLARE variable RUB5 INTEGER;
  DECLARE variable RUB6 INTEGER;
  DECLARE variable RUB7 INTEGER;
  DECLARE variable RUB8 INTEGER;
  DECLARE variable RUB9 INTEGER;
  DECLARE variable RUB10 INTEGER;

  DECLARE variable I INTEGER;
  DECLARE variable ROW_MAX INTEGER;
BEGIN
     SELECT COUNT(DISTINCT(articles.id_famille))
     FROM SORTIES
     inner join articles on articles.id_art = sorties.id_article
     WHERE SORTIES.date_piece BETWEEN :DU AND :AU
     AND SORTIES.nature_sortie = 1
     INTO :ROW_MAX;
     if (ROW_MAX > 10) then ROW_MAX = 10;
     I = 1;
     WHILE (I <= :ROW_MAX) DO
         BEGIN
              SELECT
                    DISTINCT articles.id_famille
              FROM SORTIES
              inner join articles on articles.id_art = sorties.id_article
              WHERE SORTIES.date_piece BETWEEN :DU AND :AU
              AND SORTIES.nature_sortie = 1
              ROWS :I TO :I
              INTO RUB;
              if (I = 1) THEN RUB1 = RUB;
              if (I = 2) THEN RUB2 = RUB;
              if (I = 3) THEN RUB3 = RUB;
              if (I = 4) THEN RUB4 = RUB;
              if (I = 5) THEN RUB5 = RUB;
              if (I = 6) THEN RUB6 = RUB;
              if (I = 7) THEN RUB7 = RUB;
              if (I = 8) THEN RUB8 = RUB;
              if (I = 9) THEN RUB9 = RUB;
              if (I = 10) THEN RUB10 = RUB;
              I = I + 1;
         END
  FOR
     SELECT DISTINCT
           SORTIES.id_client,
           clients.denomination,
           clients.adresse1 || ' ' || clients.adresse2,
           clients.activite,
           clients.ref_rc,
           clients.ai,
           clients.i_f,
           SUM(CASE WHEN (SORTIES.id_famille = :RUB1) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB2) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB3) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB4) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB5) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB6) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB7) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB8) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB9) THEN SORTIES.quantite END),
           SUM(CASE WHEN (SORTIES.id_famille = :RUB10) THEN SORTIES.quantite END)
     FROM SORTIES
     inner join articles on articles.id_art = sorties.id_article
     INNER JOIN clients ON clients.id_client = SORTIES.id_client
     WHERE SORTIES.date_piece BETWEEN :DU AND :AU
     AND SORTIES.nature_sortie = 1
     GROUP BY SORTIES.id_client ,
           clients.denomination,
           clients.adresse1,
           clients.adresse2,
           clients.activite,
           clients.ref_rc,
           clients.ai,
           clients.i_f
     ORDER BY 2
     INTO :ID_CLIENT, :DENOMINATION, :ADRESSE, :ACTIVITE, :R_C, :A_I, :I_F, :QTE1, :QTE2, :QTE3, :QTE4, :QTE5,
          :QTE6, :QTE7, :QTE8, :QTE9, :QTE10
  DO
  BEGIN

  SUSPEND;
  END
END
^

ALTER PROCEDURE FACT_ANT_ID_FACT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FACT_ANT_ID_FACT_GEN, 1);
END
^


ALTER PROCEDURE FACT_DD_JOINTE_ID_FACT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FACT_DD_JOINTE_ID_FACT_GEN, 1);
END
^


ALTER PROCEDURE FACT_TAUX_TVA (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ENTETE INTEGER,
    NUM VARCHAR (12),
    DATE_PIECE DATE,
    CLIENT VARCHAR (128),
    CA_00 NUMERIC (18, 2),
    TVA_00 NUMERIC (18, 2),
    CA_07 NUMERIC (18, 2),
    TVA_07 NUMERIC (18, 2),
    CA_17 NUMERIC (18, 2),
    TVA_17 NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
  FOR
     SELECT DISTINCT
           entete_sorties.id_ent_sort,
           entete_sorties.num_piece,
           entete_sorties.date_piece,
           CLIENTS.denomination
     FROM entete_sorties
     INNER JOIN CLIENTS ON CLIENTS.ID_CLIENT = entete_sorties.ID_CLIENT
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     INTO :ID_ENTETE, :NUM, :DATE_PIECE, :CLIENT
  DO
  BEGIN
       EXECUTE PROCEDURE CA_TVA(:ID_ENTETE,0) RETURNING_VALUES (:CA_00,:TVA_00);
       EXECUTE PROCEDURE CA_TVA(:ID_ENTETE,9) RETURNING_VALUES (:CA_07,:TVA_07);
       EXECUTE PROCEDURE CA_TVA(:ID_ENTETE,19) RETURNING_VALUES (:CA_17,:TVA_17);

       if (CA_00 IS NULL) then CA_00 = 0;
       if (CA_07 IS NULL) then CA_07 = 0;
       if (CA_17 IS NULL) then CA_17 = 0;

       if (TVA_00 IS NULL) then TVA_00 = 0;
       if (TVA_07 IS NULL) then TVA_07 = 0;
       if (TVA_17 IS NULL) then TVA_17 = 0;

       CA  = :CA_00  + :CA_07  + :CA_17;
       TVA = :TVA_00 + :TVA_07 + :TVA_17;
  SUSPEND;
  END
END
^


ALTER PROCEDURE FAMILLES_CHARGES_ID_FAM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FAMILLES_CHARGES_ID_FAM_GEN, 1);
END
^


ALTER PROCEDURE FAMILLES_ID_FAMILLE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FAMILLES_ID_FAMILLE_GEN, 1);
END
^


ALTER PROCEDURE FAMILLES_RECETTES_ID_FAM_REC_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FAM_RECETTES_ID_FAM_REC_GEN, 1);
END
^


ALTER PROCEDURE FAMILLE_ID_ART (
    ID INTEGER)
RETURNS (
    FAMILLE VARCHAR (128))
AS BEGIN
     SELECT
           FAMILLES.famille
     FROM FAMILLES
     INNER JOIN ARTICLES ON ARTICLES.id_famille = FAMILLES.id_famille
     WHERE ARTICLES.id_art = :ID
     INTO :FAMILLE;
  SUSPEND;
END
^


ALTER PROCEDURE FCH_STOCK_DUAU_DPT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VARIATION NUMERIC (18, 6);
  DECLARE variable VAL_STK NUMERIC (18, 2);
BEGIN

     STOCK = CAST(0 AS NUMERIC(18,6));
     P_A_M_P = CAST(0 AS NUMERIC(18,2));
     VALEUR_STOCK = CAST(0 AS NUMERIC(18,2));

     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(mouvements.id_article AS integer),
              CAST(:DU -1 AS DATE),
              CAST('Stock initial de la p?riode ' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(SUM(mouvements.qte_ini) + SUM(mouvements.qte_ent) AS numeric(18,6)),
              cast(SUM(mouvements.qte_srt)  AS numeric(18,6)),
              cast(:P_A_M_P  AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM mouvements
        WHERE mouvements.date_oper < :DU
        AND mouvements.id_article = :ID_ART
        AND mouvements.id_magazin = :ID_MAGAZIN
        AND mouvements.ref_entree = :REF_ENTREE

        GROUP BY mouvements.id_article

        UNION ALL

        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND inventaire.id_magazin = :ID_MAGAZIN
        AND INVENTAIRE.date_inventaire BETWEEN :DU AND :AU
        AND INVENTAIRE.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTREES.QUANTITE AS NUMERIC(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de reour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        ORDER BY 3,9,1
        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER
      DO
        BEGIN
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    PRIX_VENTE NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS BEGIN
     STOCK = 0;
     VALEUR_STOCK = 0;
     FOR
        SELECT
              INVENTAIRE.id_inventaire,
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de retour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              lignes_transfert.id_ligne,
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_1
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND entete_transfert.entree_stock = 1

        UNION all

        SELECT
              entrees.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION

        SELECT
              lignes_transfert.id_ligne,
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_2
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND entete_transfert.sortie_stock = 1

        ORDER BY 4,10,1

        INTO :ID_ES, :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :PRIX_VENTE, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             if (:COUT IS NULL) then COUT = 0;

             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_DEPOT (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_ES INTEGER,
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    PRIX_VENTE NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS BEGIN
     STOCK = 0;
     VALEUR_STOCK = 0;
     FOR
        SELECT
              INVENTAIRE.id_inventaire,
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de retour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              ENTREES.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION all

        SELECT
              lignes_transfert.id_ligne,
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_1
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND entete_transfert.entree_stock = 1

        UNION all

        SELECT
              entrees.id_entree,
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast(0 AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION ALL

        SELECT
              SORTIES.id_sortie,
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast(sorties.prix_unitaire AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN

        UNION

        SELECT
              lignes_transfert.id_ligne,
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_2
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND entete_transfert.sortie_stock = 1

        ORDER BY 4,10,1

        INTO :ID_ES, :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :PRIX_VENTE, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             if (:COUT IS NULL) then COUT = 0;

             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_DEPOT_REF_ENTREE (
    D DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR_STOCK NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    OPER VARCHAR (1))
AS BEGIN
     STOCK = 0;
     VALEUR_STOCK = 0;
     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('ST_INI' AS varchar(12)),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN
        AND INVENTAIRE.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Bon de retour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_1
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND lignes_transfert.REF_ENTREE = :REF_ENTREE
        AND entete_transfert.entree_stock = 1

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION

        SELECT
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST(REF_ENTREE AS varchar(12)),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        inner join magazins on magazins.id_mag = lignes_transfert.id_magazin_2
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND lignes_transfert.REF_ENTREE = :REF_ENTREE
        AND entete_transfert.sortie_stock = 1

        ORDER BY 3,9,1

        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :REF_ENTREE, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER

      DO
        BEGIN
              VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_DU_AU (
    DU DATE,
    AU DATE,
    ID_ART INTEGER)
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VARIATION NUMERIC (18, 6);
  DECLARE variable VAL_STK NUMERIC (18, 2);
BEGIN

     STOCK = CAST(0 AS NUMERIC(18,6));
     P_A_M_P = CAST(0 AS NUMERIC(18,2));
     VALEUR_STOCK = CAST(0 AS NUMERIC(18,2));

     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(mouvements.id_article AS integer),
              CAST(:DU -1 AS DATE),
              CAST('Stock initial de la p?riode ' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(SUM(mouvements.qte_ini) + SUM(mouvements.qte_ent) AS numeric(18,6)),
              cast(SUM(mouvements.qte_srt)  AS numeric(18,6)),
              cast(:P_A_M_P  AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM mouvements
        WHERE mouvements.date_oper < :DU
        AND mouvements.id_article = :ID_ART
        GROUP BY mouvements.id_article

        UNION ALL

        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTREES.QUANTITE AS NUMERIC(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de retour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        ORDER BY 3,9,1
        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER
      DO
        BEGIN
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_DU_AU_DEPOT (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE,
    ID_ART INTEGER)
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VARIATION NUMERIC (18, 6);
  DECLARE variable VAL_STK NUMERIC (18, 2);
BEGIN

     STOCK = CAST(0 AS NUMERIC(18,6));
     P_A_M_P = CAST(0 AS NUMERIC(18,2));
     VALEUR_STOCK = CAST(0 AS NUMERIC(18,2));

     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(mouvements.id_article AS integer),
              CAST(:DU -1 AS DATE),
              CAST('Stock initial de la p?riode ' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(SUM(mouvements.qte_ini) + SUM(mouvements.qte_ent) AS numeric(18,6)),
              cast(SUM(mouvements.qte_srt)  AS numeric(18,6)),
              cast(:P_A_M_P  AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM mouvements
        WHERE mouvements.date_oper < :DU
        AND mouvements.id_article = :ID_ART
        AND mouvements.id_magazin = :ID_MAGAZIN
        GROUP BY mouvements.id_article

        UNION ALL

        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND inventaire.id_magazin = :ID_MAGAZIN
        AND   INVENTAIRE.date_inventaire BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTREES.QUANTITE AS NUMERIC(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de retour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.id_magazin = :ID_MAGAZIN
        AND ENTREES.date_entree BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.id_magazin = :ID_MAGAZIN
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        ORDER BY 3,9,1
        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER
      DO
        BEGIN
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_DU_AU_REF_ENTREE (
    DU DATE,
    AU DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VARIATION NUMERIC (18, 6);
  DECLARE variable VAL_STK NUMERIC (18, 2);
BEGIN

     STOCK = CAST(0 AS NUMERIC(18,6));
     P_A_M_P = CAST(0 AS NUMERIC(18,2));
     VALEUR_STOCK = CAST(0 AS NUMERIC(18,2));

     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(mouvements.id_article AS integer),
              CAST(:DU -1 AS DATE),
              CAST('Stock initial de la p?riode ' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(SUM(mouvements.qte_ini) + SUM(mouvements.qte_ent) AS numeric(18,6)),
              cast(SUM(mouvements.qte_srt)  AS numeric(18,6)),
              cast(:P_A_M_P  AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM mouvements
        WHERE mouvements.date_oper < :DU
        AND mouvements.id_article = :ID_ART
        AND mouvements.ref_entree = :REF_ENTREE
        GROUP BY mouvements.id_article

        UNION ALL

        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire BETWEEN :DU AND :AU
        AND   INVENTAIRE.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTREES.QUANTITE AS NUMERIC(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree BETWEEN :DU AND :AU
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece BETWEEN :DU AND :AU
        AND SORTIES.ref_entree = :REF_ENTREE

        ORDER BY 3,9,1
        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER
      DO
        BEGIN
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FICHE_DE_STOCK_REF_ENTREE (
    D DATE,
    ID_ART INTEGER,
    REF_ENTREE VARCHAR (12))
RETURNS (
    NUM VARCHAR (12),
    ID_ARTICLE INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (128),
    CLIENT_FOURNISSEUR VARCHAR (128),
    ENTREE NUMERIC (18, 6),
    SORTIE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    P_A_M_P NUMERIC (18, 2),
    VALEUR_ENTREE NUMERIC (18, 2),
    VALEUR_SORTIE NUMERIC (18, 2),
    VALEUR_STOCK NUMERIC (18, 2),
    OPER VARCHAR (1))
AS DECLARE variable VARIATION NUMERIC (18, 6);
  DECLARE variable VAL_STK NUMERIC (18, 2);
BEGIN
     STOCK = CAST(0 AS NUMERIC(18,6));
     P_A_M_P = CAST(0 AS NUMERIC(18,2));
     VALEUR_STOCK = CAST(0 AS NUMERIC(18,2));
     FOR
        SELECT
              cast(0 AS VARCHAR(12)),
              cast(inventaire.id_article AS integer),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              CAST('ST_INI' AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Stock initial de l''exercice' AS VARCHAR(128) CHARACTER SET NONE),
              CAST('                            ' AS VARCHAR(128) CHARACTER SET NONE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND INVENTAIRE.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(ENTREES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Importation dossier N? : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND entrees.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(ENTREES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Achat local facture N? :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTREES.QUANTITE AS NUMERIC(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND entrees.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(ENTREES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Fiche de production N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Facture d''avoir client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Bon de reour client N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(ENTREES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Autres entr?es N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
               CAST(lignes_transfert.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST('-' AS VARCHAR(128) CHARACTER SET NONE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        WHERE lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND entete_transfert.entree_stock = 1
        AND lignes_transfert.REF_ENTREE = :REF_ENTREE

        UNION all

        SELECT
              cast(entete_entrees.num_fact AS VARCHAR(12)),
              cast(ENTREES.id_article AS integer),
              CAST(ENTREES.date_entree AS DATE),
              CAST(ENTREES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Facture d''avoir founisseur N? : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM entrees
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Facture de Vente N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Bon de livraison N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Bon de caisse N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Consommation fiche de production N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Facture de Cession N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

        UNION ALL

        SELECT
              cast(entete_sorties.num_piece AS VARCHAR(12)),
              cast(SORTIES.id_article AS integer),
              CAST(SORTIES.date_piece AS DATE),
              CAST(SORTIES.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Autres sorties N? : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.REF_ENTREE = :REF_ENTREE

         UNION ALL

        SELECT
              cast(lignes_transfert.num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piEce AS DATE),
              CAST(lignes_transfert.ref_entree AS VARCHAR(12) CHARACTER SET NONE),
              CAST('Transfert N? : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST('-' AS VARCHAR(128) CHARACTER SET NONE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        WHERE lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND entete_transfert.sortie_stock = 1
        AND lignes_transfert.REF_ENTREE = :REF_ENTREE


        ORDER BY 3,9,1
        INTO :NUM, :ID_ARTICLE, :DATE_OPER, :REF_ENTREE, :LIBELLE, :CLIENT_FOURNISSEUR, :ENTREE, :SORTIE, :COUT, :OPER
      DO
        BEGIN
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             if ((ENTREE <> 0)AND((:ENTREE + :STOCK)<>0)) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             if (ENTREE + STOCK = 0) then P_A_M_P = 0;
             VALEUR_SORTIE = cast(:SORTIE * :P_A_M_P AS numeric(18,2));
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             if (ENTREE = 0) then ENTREE = NULL;
             if (SORTIE = 0) then SORTIE = NULL;
             if (VALEUR_ENTREE = 0) then VALEUR_ENTREE = NULL;
             if (VALEUR_SORTIE = 0) then VALEUR_SORTIE = NULL;

             SUSPEND;
        END
END
^


ALTER PROCEDURE FOURNISSEURS_ID_FOURN_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FOURNISSEURS_ID_FOURN_GEN, 1);
END
^


ALTER PROCEDURE FRAUDEURS_ID_FRAUD_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(FRAUDEURS_ID_FRAUD_GEN, 1);
END
^


ALTER PROCEDURE G50_ID_G50_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(G50_ID_G50_GEN, 1);
END
^


ALTER PROCEDURE INFO_NUM_SERIE (
    NUM_SERIE VARCHAR (128))
RETURNS (
    SERIAL VARCHAR (128),
    ARTICLE VARCHAR (128),
    DATE_ACHAT DATE,
    NUM_FACT_ACHAT VARCHAR (12),
    LOCAL VARCHAR (12),
    FOURNISSEUR VARCHAR (128),
    DATE_VENTE DATE,
    NUM_FACT_VENTE VARCHAR (12),
    CLIENT VARCHAR (128))
AS DECLARE variable ID_ART INTEGER;
  DECLARE variable ID_ENT INTEGER;
  DECLARE variable ID_FOU INTEGER;
  DECLARE variable ID_SOR INTEGER;
  DECLARE variable ID_CLI INTEGER;
  DECLARE variable NATURE INTEGER;

BEGIN
     SERIAL = :NUM_SERIE;
     SELECT
           num_series.id_article,
           num_series.id_entree,
           num_series.id_sortie
     FROM num_series
     WHERE num_series.num_serie = :NUM_SERIE
     INTO :ID_ART, :ID_ENT, :ID_SOR;

     SELECT
           ARTICLES.designation
     FROM ARTICLES
     WHERE ARTICLES.id_art = :ID_ART
     INTO :ARTICLE;

     SELECT
           entete_entrees.id_fourn,
           entete_entrees.date_fact,
           entete_entrees.num_fact,
           entete_entrees.nature_entree
     FROM entete_entrees
     WHERE entete_entrees.id_ent_ent = :ID_ENT
     INTO :ID_FOU, :DATE_ACHAT, :NUM_FACT_ACHAT, :NATURE;

     if (NATURE = 1 ) then LOCAL = 'IMPORTATION';
     if (NATURE = 2 ) then LOCAL = 'LOCAL';

     SELECT
           CLIENTS.denomination
     FROM clients
     WHERE CLIENTS.id_client = :ID_FOU
     INTO :FOURNISSEUR;

     SELECT
           entete_sorties.id_client,
           entete_sorties.date_piece,
           entete_sorties.num_piece
     FROM entete_sorties
     WHERE entete_sorties.id_ent_sort = :ID_SOR
     INTO :ID_CLI, :DATE_VENTE, :NUM_FACT_VENTE;

     SELECT
           CLIENTS.denomination
     FROM clients
     WHERE CLIENTS.id_client = :ID_CLI
     INTO :CLIENT;

  SUSPEND;
END
^


ALTER PROCEDURE INVENTAIRES (
    D DATE)
RETURNS (
    ID_MAGAZIN INTEGER,
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.id_magazin,
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),
              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE ID_ARTICLE IS NOT NULL
        AND articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvements.date_oper <= :D
        GROUP BY mouvements.id_magazin,
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES
          ORDER BY 3
        INTO :ID_MAGAZIN, :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
       if (NBRE_CARTON is null) then NBRE_CARTON = 0;
       if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

  	   if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;

       EXECUTE PROCEDURE pamp_depot(:ID_ART, :ID_MAGAZIN, :D) RETURNING_VALUES :cout;
       valeur = :stock * :cout;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRES_MARQUES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_MARQUE INTEGER,
    DESIGNATION VARCHAR (128),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    VENTES NUMERIC (18, 2),
    MARGES NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
               MARQUES.id_marque,
               MARQUES.marque
        FROM MARQUES
        INTO :ID_marque, :DESIGNATION DO
  BEGIN
       SELECT SUM(inventaire.valeur) FROM inventaire
       INNER JOIN ARTICLES ON ARTICLES.ID_ART = inventaire.id_article
       WHERE ARTICLES.id_marque = :ID_marque
       AND inventaire.date_inventaire >= :DU
       INTO VAL_INI;

       SELECT SUM(ENTREES.HT) FROM ENTREES
       INNER JOIN ARTICLES ON ARTICLES.ID_ART = ENTREES.id_article
       WHERE ARTICLES.id_marque = :ID_marque
       AND ENTREES.date_entree BETWEEN :DU AND :AU
       INTO VAL_ENT;

       SELECT SUM(SORTIES.consomation) FROM SORTIES
       INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.id_article
       WHERE ARTICLES.id_marque = :ID_marque
       AND SORTIES.date_piece BETWEEN :DU AND :AU
       INTO VAL_SRT;

       SELECT SUM(SORTIES.HT) FROM SORTIES
       INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.id_article
       WHERE ARTICLES.id_marque = :ID_marque
       AND SORTIES.date_piece BETWEEN :DU AND :AU
       INTO VENTES;

       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
       if (VENTES IS NULL) then VENTES = 0;
       VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;
       marges = :VENTES - VAL_SRT;
       SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRES_REF_ENTREE (
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.ID_ARTICLE,
              mouvements.REF_ENTREE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              mouvements.ref_entree,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),
              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE ID_ARTICLE IS NOT NULL
        AND mouvements.date_oper <= :D
        GROUP BY mouvements.ID_ARTICLE,
              mouvements.REF_ENTREE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
               ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              mouvements.ref_entree
          ORDER BY 3
        INTO :ID_ART, :REF_ENTREE, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :REF_ENTREE, :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
	   if (NBRE_CARTON is null) then NBRE_CARTON = 0;
       if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;

       if (STOCK = 0) then
          VALEUR = 0;
       ELSE
           VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_DEPOT (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),
              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE ID_ARTICLE IS NOT NULL
        AND articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvements.date_oper <= :D
        AND mouvements.id_magazin = :ID_MAGAZIN
        GROUP BY mouvements.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              ARTICLES.taux_tva
          ORDER BY 3
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
	   if (NBRE_CARTON is null) then NBRE_CARTON = 0;
       if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;

       EXECUTE PROCEDURE pamp_depot(:id_art, :id_magazin, :d) RETURNING_VALUES :cout;
       VALEUR = :cout * :stock;
       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_DEPOT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    NBRE_CARTON NUMERIC (18, 6),
    NBRE_PALETTE NUMERIC (18, 6),
    STOCK_CARTON NUMERIC (18, 6),
    STOCK_PALETTE NUMERIC (18, 6),
    ST_INI NUMERIC (18, 6),
    ST_ENT NUMERIC (18, 6),
    ST_SRT NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    VAL_ENT NUMERIC (18, 2),
    VAL_SRT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              mouvements.ID_ARTICLE,
              ARTICLES.ref,
              mouvements.ref_entree,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),
              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE ID_ARTICLE IS NOT NULL
        AND articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvements.date_oper <= :D
        AND mouvements.id_magazin = :ID_MAGAZIN
        GROUP BY mouvements.ID_ARTICLE,
              ARTICLES.ref,
              mouvements.ref_entree,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.CONDITIONNEMENT,
              ARTICLES.PALETTES,
              ARTICLES.taux_tva
          ORDER BY 3
        INTO :ID_ART, :REF, :REF_ENTREE, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :NBRE_CARTON, :NBRE_PALETTE, :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;
	if (NBRE_CARTON is null) then NBRE_CARTON = 0;
if (NBRE_PALETTE is null) then NBRE_PALETTE = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       if (NBRE_CARTON <> 0) THEN
       		STOCK_CARTON = STOCK / NBRE_CARTON;
       else
       		STOCK_CARTON = 0;

       if (NBRE_PALETTE <> 0) THEN
       		STOCK_PALETTE = STOCK / NBRE_PALETTE;
       else
       		STOCK_PALETTE = 0;


       if (STOCK = 0) then
          VALEUR = 0;
       ELSE
           VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_DU_AU (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);

BEGIN
     FOR
        SELECT DISTINCT
              mouvements_du_au.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,

              sum(mouvements_du_au.INI_QTE_INI),
              sum(mouvements_du_au.INI_QTE_ENT),
              sum(mouvements_du_au.INI_QTE_SRT),
              SUM(mouvements_du_au.INI_val_INI),
              SUM(mouvements_du_au.INI_val_ent),
              sum(mouvements_du_au.INI_val_srt),

              sum(mouvements_du_au.QTE_INI),
              sum(mouvements_du_au.QTE_ENT),
              sum(mouvements_du_au.QTE_SRT),
              SUM(mouvements_du_au.val_INI),
              SUM(mouvements_du_au.val_ent),
              sum(mouvements_du_au.val_srt)
        FROM mouvements_du_au(:DU, :AU)
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = mouvements_du_au.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)

        GROUP BY mouvements_du_au.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva
          ORDER BY 3
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :INI_ST_INI, :INI_ST_ENT, :INI_ST_SRT, :INI_VAL_INI, :INI_VAL_ENT, :INI_VAL_SRT,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;

       if (INI_ST_INI IS NULL) then INI_ST_INI = 0;
       if (INI_ST_ENT IS NULL) then INI_ST_ENT = 0;
       if (INI_ST_SRT IS NULL) then INI_ST_SRT = 0;
       if (INI_VAL_INI IS NULL) then INI_VAL_INI = 0;
       if (INI_VAL_ENT IS NULL) then INI_VAL_ENT = 0;
       if (INI_VAL_SRT IS NULL) then INI_VAL_SRT = 0;

       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       ST_INI = :ST_INI + :INI_ST_INI + :INI_ST_ENT - :INI_ST_SRT;
       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       VAL_INI = :VAL_INI + :INI_VAL_INI + :INI_VAL_ENT - :INI_VAL_SRT;

       if (STOCK = 0) then
          VALEUR = 0;
       ELSE
           VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_DU_AU_DEPOT (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              MOUVEMENTS_DU_AU.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,

              SUM(MOUVEMENTS_DU_AU.INI_qte_ini),
              SUM(MOUVEMENTS_DU_AU.INI_qte_ENT),
              SUM(MOUVEMENTS_DU_AU.INI_qte_srt),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_ini),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_ENT),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_SRT),

              SUM(MOUVEMENTS_DU_AU.qte_ini),
              SUM(MOUVEMENTS_DU_AU.qte_ENT),
              SUM(MOUVEMENTS_DU_AU.qte_srt),
              SUM(MOUVEMENTS_DU_AU.VAL_ini),
              SUM(MOUVEMENTS_DU_AU.VAL_ENT),
              SUM(MOUVEMENTS_DU_AU.VAL_SRT)

        FROM MOUVEMENTS_DU_AU(:DU, :AU)
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS_DU_AU.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE MOUVEMENTS_DU_AU.id_magazin = :ID_MAGAZIN
        GROUP BY MOUVEMENTS_DU_AU.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :INI_ST_INI, :INI_ST_ENT, :INI_ST_SRT, :INI_VAL_INI, :INI_VAL_ENT, :INI_VAL_SRT,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT
  DO
  BEGIN
      if (INI_ST_INI IS NULL) then INI_ST_INI = 0;
       if (INI_VAL_INI IS NULL) then INI_VAL_INI = 0;
       if (INI_ST_ENT IS NULL) then INI_ST_ENT = 0;
       if (INI_VAL_ENT IS NULL) then INI_VAL_ENT = 0;
       if (INI_ST_SRT IS NULL) then INI_ST_SRT = 0;
       if (INI_VAL_SRT IS NULL) then INI_VAL_SRT = 0;

       if (ST_INI IS NULL) then ST_INI = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       ST_INI = :ST_INI + :INI_ST_INI + :INI_ST_ENT - :INI_ST_SRT;
       VAL_INI = :VAL_INI + :INI_VAL_INI + :INI_VAL_ENT - :INI_VAL_SRT;


       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       STOCK =  :ST_INI + :ST_ENT - :ST_SRT;
       VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = VALEUR / STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;

  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_DU_AU_REF_ENTREE (
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);

BEGIN
     FOR
        SELECT DISTINCT
              mouvements_du_au.ID_ARTICLE,
              mouvements_du_au.ref_entree,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,

              sum(mouvements_du_au.INI_QTE_INI),
              sum(mouvements_du_au.INI_QTE_ENT),
              sum(mouvements_du_au.INI_QTE_SRT),
              SUM(mouvements_du_au.INI_val_INI),
              SUM(mouvements_du_au.INI_val_ent),
              sum(mouvements_du_au.INI_val_srt),

              sum(mouvements_du_au.QTE_INI),
              sum(mouvements_du_au.QTE_ENT),
              sum(mouvements_du_au.QTE_SRT),
              SUM(mouvements_du_au.val_INI),
              SUM(mouvements_du_au.val_ent),
              sum(mouvements_du_au.val_srt)
        FROM mouvements_du_au(:DU, :AU)
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = mouvements_du_au.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)

        GROUP BY mouvements_du_au.ID_ARTICLE,
              mouvements_du_au.ref_entree,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva
          ORDER BY 3
        INTO :ID_ART, :REF_ENTREE, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :INI_ST_INI, :INI_ST_ENT, :INI_ST_SRT, :INI_VAL_INI, :INI_VAL_ENT, :INI_VAL_SRT,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;

       if (INI_ST_INI IS NULL) then INI_ST_INI = 0;
       if (INI_ST_ENT IS NULL) then INI_ST_ENT = 0;
       if (INI_ST_SRT IS NULL) then INI_ST_SRT = 0;
       if (INI_VAL_INI IS NULL) then INI_VAL_INI = 0;
       if (INI_VAL_ENT IS NULL) then INI_VAL_ENT = 0;
       if (INI_VAL_SRT IS NULL) then INI_VAL_SRT = 0;

       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       ST_INI = :ST_INI + :INI_ST_INI + :INI_ST_ENT - :INI_ST_SRT;
       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       VAL_INI = :VAL_INI + :INI_VAL_INI + :INI_VAL_ENT - :INI_VAL_SRT;

       if (STOCK = 0) then
          VALEUR = 0;
       ELSE
           VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE INVENTAIRE_ID_INVENTAIRE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(INVENTAIRE_ID_INVENTAIRE_GEN, 1);
END
^


ALTER PROCEDURE INV_DUAU_DPT_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_ENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    INI_ST_INI NUMERIC (18, 6),
    INI_VAL_INI NUMERIC (18, 2),
    INI_ST_ENT NUMERIC (18, 6),
    INI_VAL_ENT NUMERIC (18, 2),
    INI_ST_SRT NUMERIC (18, 6),
    INI_VAL_SRT NUMERIC (18, 2),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);
BEGIN
     FOR
        SELECT DISTINCT
              MOUVEMENTS_DU_AU.ID_ARTICLE,
              MOUVEMENTS_DU_AU.REF_ENTREE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,

              SUM(MOUVEMENTS_DU_AU.INI_qte_ini),
              SUM(MOUVEMENTS_DU_AU.INI_qte_ENT),
              SUM(MOUVEMENTS_DU_AU.INI_qte_srt),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_ini),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_ENT),
              SUM(MOUVEMENTS_DU_AU.INI_VAL_SRT),

              SUM(MOUVEMENTS_DU_AU.qte_ini),
              SUM(MOUVEMENTS_DU_AU.qte_ENT),
              SUM(MOUVEMENTS_DU_AU.qte_srt),
              SUM(MOUVEMENTS_DU_AU.VAL_ini),
              SUM(MOUVEMENTS_DU_AU.VAL_ENT),
              SUM(MOUVEMENTS_DU_AU.VAL_SRT)

        FROM MOUVEMENTS_DU_AU(:DU, :AU)
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS_DU_AU.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE MOUVEMENTS_DU_AU.id_magazin = :ID_MAGAZIN
        GROUP BY MOUVEMENTS_DU_AU.ID_ARTICLE,
              MOUVEMENTS_DU_AU.REF_ENTREE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva
        INTO :ID_ART, :REF_ENTREE, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :INI_ST_INI, :INI_ST_ENT, :INI_ST_SRT, :INI_VAL_INI, :INI_VAL_ENT, :INI_VAL_SRT,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT
  DO
  BEGIN
      if (INI_ST_INI IS NULL) then INI_ST_INI = 0;
       if (INI_VAL_INI IS NULL) then INI_VAL_INI = 0;
       if (INI_ST_ENT IS NULL) then INI_ST_ENT = 0;
       if (INI_VAL_ENT IS NULL) then INI_VAL_ENT = 0;
       if (INI_ST_SRT IS NULL) then INI_ST_SRT = 0;
       if (INI_VAL_SRT IS NULL) then INI_VAL_SRT = 0;

       if (ST_INI IS NULL) then ST_INI = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       ST_INI = :ST_INI + :INI_ST_INI + :INI_ST_ENT - :INI_ST_SRT;
       VAL_INI = :VAL_INI + :INI_VAL_INI + :INI_VAL_ENT - :INI_VAL_SRT;


       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       STOCK =  :ST_INI + :ST_ENT - :ST_SRT;
       VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = VALEUR / STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;

  SUSPEND;
  END
END
^


ALTER PROCEDURE LIGNES_PROFORMA_ID_L_PROF_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(LIGNES_PROFORMA_ID_L_PROF_GEN, 1);
END
^


ALTER PROCEDURE LIGNES_TRANSFERT_ID_LIGNE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(LIGNES_TRANSFERT_ID_LIGNE_GEN, 1);
END
^


ALTER PROCEDURE LISTE_PRODUIT (
    D DATE)
RETURNS (
    ID INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (64),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 6),
    PRIX NUMERIC (18, 6))
AS DECLARE variable M1 NUMERIC (18, 2);
  DECLARE variable T1 NUMERIC (18, 2);
  DECLARE variable P1 NUMERIC (18, 2);
BEGIN
     FOR
        SELECT
              CAST(INVENTAIRES.id_art AS INTEGER),
              cast(INVENTAIRES.ref AS varchar(12)  CHARACTER SET NONE),
              cast(INVENTAIRES.designation AS varchar(128)  CHARACTER SET NONE),
              cast(stock AS numeric(18,6)),
              cast(cout AS  numeric(18,2)),
              CAST(UNITES.unite AS VARCHAR(64) CHARACTER SET NONE),
              cast(ARTICLES.marge1 AS  numeric(18,2)),
              cast(ARTICLES.taux_m1 AS  numeric(18,2)),
              cast(ARTICLES.prix_v1 AS  numeric(18,2))
        FROM INVENTAIRES( :D)
        INNER JOIN ARTICLES ON ARTICLES.id_art = inventaires.id_art
        INNER JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        WHERE stock <> 0
        ORDER BY 5
        INTO :ID, :ref, :DESIGNATION, :STOCK, :COUT, :UNITE, :M1, :T1, :P1



     DO
       BEGIN
             if (M1 IS NULL) then M1 = 0;
             if (T1 IS NULL) then T1 = 0;
             if (P1 IS NULL) then P1 = 0;
             if (T1 <> 0) then PRIX = :COUT + :COUT * T1 / 100;
             if (P1 <> 0) then PRIX = :P1;
             if (M1 <> 0) then PRIX = :COUT + :M1;
     SUSPEND;
     END
END
^


ALTER PROCEDURE LST_CHRGS_ID_LST_CHRG_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(LST_CHGES_ID_LISTE_CHARGE_GEN, 1);
END
^


ALTER PROCEDURE MAGAZINS_ID_MAG_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(MAGAZINS_ID_MAG_GEN, 1);
END
^


ALTER PROCEDURE MARQUES_ID_MARQUE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(MARQUES_ID_MARQUE_GEN, 1);
END
^


ALTER PROCEDURE MAX_DATE_ENTREES (
    NATURE INTEGER)
RETURNS (
    DTE DATE)
AS BEGIN
     SELECT
           MAX(CAST(entete_ENTREES.date_fact AS DATE))
     FROM entete_ENTREES
     WHERE entete_ENTREES.nature_entree = :NATURE
     INTO :DTE;

  SUSPEND;
END
^


ALTER PROCEDURE MAX_DATE_SORTIES (
    NATURE INTEGER)
RETURNS (
    DTE DATE)
AS BEGIN
     SELECT
           MAX(CAST(entete_sorties.date_piece AS DATE))
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie = :NATURE
     INTO :DTE;
  SUSPEND;
END
^


ALTER PROCEDURE MVT_AV_ENTREES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              ENTREES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(ENTREES.quantite),
              ENTREES.cout_achat,
              sum(ENTREES.ht)
        FROM ENTREES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE ENTREES.date_entree BETWEEN :D1 AND :D2
        AND ENTREES.nature_ENTREE = 4
        GROUP BY
               ENTREES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ENTREES.cout_achat
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE MVT_AV_SORTIES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              SORTIES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(SORTIES.quantite),
              SORTIES.prix_unitaire,
              sum(SORTIES.ht)
        FROM SORTIES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE SORTIES.date_piece BETWEEN :D1 AND :D2
        AND sorties.nature_sortie = 5
        GROUP BY
               SORTIES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              SORTIES.prix_unitaire
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE MVT_ENTREES_IMP (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              ENTREES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(ENTREES.quantite),
              ENTREES.cout_achat,
              sum(ENTREES.ht)
        FROM ENTREES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE ENTREES.date_entree BETWEEN :D1 AND :D2
        AND ENTREES.nature_ENTREE = 1
        GROUP BY
               ENTREES.id_article,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ENTREES.cout_achat
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE MVT_ENTREES_LOC (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              ENTREES.id_article,
              ENTREES.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(ENTREES.quantite),
              ENTREES.cout_achat,
              sum(ENTREES.ht)
        FROM ENTREES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE ENTREES.date_entree BETWEEN :D1 AND :D2
        AND ENTREES.nature_ENTREE = 2
        GROUP BY
               ENTREES.id_article,
               ENTREES.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ENTREES.cout_achat
        INTO :ID_ART, :ID_MAGAZIN, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE MVT_ENTREES_PRODUCTION (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              ENTREES.id_article,
              ENTREES.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(ENTREES.quantite),
              ENTREES.cout_achat,
              sum(ENTREES.ht)
        FROM ENTREES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = ENTREES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE ENTREES.date_entree BETWEEN :D1 AND :D2
        AND ENTREES.nature_ENTREE = 3
        GROUP BY
               ENTREES.id_article,
               ENTREES.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ENTREES.cout_achat
        INTO :ID_ART, :ID_MAGAZIN, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE MVT_SORTIES (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    QTE NUMERIC (18, 6),
    PRIX_U NUMERIC (18, 2),
    MNT_HT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              SORTIES.id_article,
              sorties.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              sum(SORTIES.quantite),
              SORTIES.prix_unitaire,
              sum(SORTIES.ht)
        FROM SORTIES
        INNER JOIN  ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        LEFT OUTER  JOIN UNITES ON UNITES.id_unite = ARTICLES.id_unite
        LEFT OUTER JOIN FAMILLES ON FAMILLES.id_famille = ARTICLES.id_famille
        WHERE SORTIES.date_piece BETWEEN :D1 AND :D2
        AND sorties.nature_sortie between 1 AND 2
        GROUP BY
               SORTIES.id_article,
               sorties.id_magazin,
              ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              SORTIES.prix_unitaire
        INTO :ID_ART, :ID_MAGAZIN, :REF, :DESIGNATION, :UNITE, :FAMILLE, :QTE, :PRIX_U, :MNT_HT
     DO
     BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE NETOYER_ENTREES
AS BEGIN
  DELETE FROM ENTREES
WHERE ((ENTREES.id_entete) NOT
      IN (SELECT entete_ENTREES.id_ent_ent FROM entete_ENTREES));
  SUSPEND;
END
^


ALTER PROCEDURE NETOYER_SORTIES
AS BEGIN
  DELETE FROM sorties
WHERE ((SORTIES.id_entete) NOT
      IN (SELECT entete_sorties.id_ent_sort FROM entete_sorties));
  SUSPEND;
END
^


ALTER PROCEDURE NOMENCLATURES_ID_NOM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(NOMENCLATURES_ID_NOM_GEN, 1);
END
^


ALTER PROCEDURE NUM_BC_F
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(entet_bc_f.num_bc)
     FROM entet_bc_f
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_DEP
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(depenses.num_dep)
     FROM depenses
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_DOS_IMPORTATION (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(CAST(entete_ENTREES.num_dos AS INTEGER))
     FROM entete_ENTREES
     WHERE entete_ENTREES.nature_entree = :NATURE
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_ENTET_ENTREE (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(CAST(entete_ENTREES.num_FACT AS INTEGER))
     FROM entete_ENTREES
     WHERE entete_ENTREES.nature_entree = :NATURE
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_ENTET_SORTIE (
    NATURE INTEGER)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(CAST(entete_sorties.num_piece AS INTEGER))
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie = :NATURE
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_ENTET_SORTIE_MAG (
    NATURE INTEGER,
    ID_MAGASIN INTEGER)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(CAST(entete_sorties.num_piece AS INTEGER))
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie = :NATURE
     AND entete_sorties.id_magazin = :ID_MAGASIN
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_ENTET_SORTIE_MAGAZIN (
    NATURE INTEGER,
    ID_MAGAZIN INTEGER)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(CAST(entete_sorties.num_piece AS INTEGER))
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie = :NATURE
     AND entete_sorties.id_magazin = :ID_MAGAZIN
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_PROFORMA
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(proforma.num_fact)
     FROM proforma
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_REC
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(RECETTES.NUM_REC)
     FROM RECETTES
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_RGLT (
    NATURE SMALLINT)
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(regl_tiers.num_piece)
     FROM regl_tiers
     WHERE regl_tiers.type_regt = :NATURE
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_SERIES_ID_NUM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(NUM_SERIES_ID_NUM_GEN, 1);
END
^


ALTER PROCEDURE NUM_TRANSFERT
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(entete_transfert.num_piece)
     FROM entete_transfert
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE NUM_VIR_FONDS
RETURNS (
    NUM INTEGER)
AS BEGIN
     SELECT
           MAX(vir_fonds.num_vir_fond)
     FROM vir_fonds
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END
^


ALTER PROCEDURE PAMP (
    ID_ARTICLE INTEGER,
    D DATE)
RETURNS (
    P_PAMP NUMERIC (18, 2))
AS DECLARE variable STOCK_in NUMERIC (18, 6);
  DECLARE variable valeur_ini NUMERIC (18, 2);

  DECLARE variable STOCK_ent NUMERIC (18, 6);
  DECLARE variable valeur_ent NUMERIC (18, 2);
  DECLARE variable cout_srt NUMERIC (18, 2);
  DECLARE variable STOCK_srt NUMERIC (18, 6);
  DECLARE variable valeur_srt NUMERIC (18, 2);
  DECLARE variable STOCK NUMERIC (18, 6);
  DECLARE variable valeur NUMERIC (18, 2);
BEGIN

     SELECT
            CAST(SUM(inventaire.quantite) AS numeric(18,6)),
            CAST(sum(inventaire.valeur) AS numeric(18,2))
     FROM inventaire
     WHERE inventaire.id_article = :ID_ARTICLE
     AND inventaire.date_inventaire <= :D
     INTO :stock_in, :valeur_ini;
     if (:stock_in IS null) then stock_in = 0;
     if (:valeur_ini IS null) then valeur_ini = 0;

     SELECT distinct
                cast(sum(ENTREES.quantite) AS numeric(18,6)),
                cast(sum(entrees.cout_achat*ENTREES.quantite) AS NUMERIC(18,2))
     FROM ENTREES
     inner join articles on (articles.id_art = entrees.id_article)
     WHERE ENTREES.date_entree <= :D
     AND entrees.id_article = :ID_ARTICLE
     AND (ENTREES.nature_entree <> 4)
     AND entrees.stocker = 1
     AND articles.gestion_stock = 1
     INTO :stock_ent, :valeur_ent;
     if (stock_ent IS null) then stock_ent = 0;
     if (valeur_ent IS null) then valeur_ent = 0;

     SELECT distinct
                cast(sum(ENTREES.quantite) AS numeric(18,6)),
                cast(sum(entrees.cout_achat*ENTREES.quantite) AS NUMERIC(18,2))
     FROM ENTREES
     inner join articles on (articles.id_art = entrees.id_article)
     WHERE ENTREES.date_entree <= :D
     AND entrees.id_article = :ID_ARTICLE
     AND (ENTREES.nature_entree = 4)
     AND entrees.stocker = 1
     AND articles.gestion_stock = 1
     INTO :stock_srt, :valeur_srt;
     if (stock_srt IS null) then stock_srt = 0;
     if (valeur_srt IS null) then valeur_srt = 0;

     stock = :stock_in + :stock_ent - :stock_srt;
     valeur = :valeur_ini + :valeur_ent - :valeur_srt;

     if (STOCK <> 0) then
        P_PAMP = :VALEUR / :STOCK;
     ELSE
         P_PAMP = 0;

  SUSPEND;
END
^


ALTER PROCEDURE PAMP_DEPOT (
    ID_ARTICLE INTEGER,
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    P_PAMP NUMERIC (18, 2))
AS BEGIN
     SELECT FIRST 1
           P_A_M_P
     FROM BASE_CALCUL_PAMP(:D, :ID_ARTICLE,:ID_MAGAZIN)
     WHERE  BASE_CALCUL_PAMP.OPER = 'E'
     ORDER BY BASE_CALCUL_PAMP.date_oper desc, BASE_CALCUL_PAMP.id_es DESC
     INTO :P_PAMP;

  SUSPEND;
END
^


ALTER PROCEDURE PARAMETRES_ID_PARAM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(PARAMETRES_ID_PARAM_GEN, 1);
END
^


ALTER PROCEDURE PRODUIT_DISPONIBLE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);

BEGIN
     FOR
      SELECT DISTINCT
              mouvement.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              sum(MOUVEMENT.QTE),
              sum(mouvement.val)
        FROM mouvement
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENT.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvement.date_oper <= :D
        AND mouvement.id_magazin = :ID_MAGAZIN
        GROUP BY mouvement.ID_ARTICLE,
              ARTICLES.ref,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :STOCK, :VALEUR
  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;
       if (stock IS null) then stock = 0;
       if (stock <> 0) then
          cout = :valeur/:stock;
       else
           cout = 0;
       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE PRODUIT_DISPONIBLE_REF_ENTREE (
    ID_MAGAZIN INTEGER,
    D DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    REF_RENTREE VARCHAR (12),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (12),
    FAMILLE VARCHAR (64),
    COUT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2),
    PRIX_HT NUMERIC (18, 2),
    PRIX_TTC NUMERIC (18, 2),
    VALEUR_HT NUMERIC (18, 2),
    VALEUR_TTC NUMERIC (18, 2))
AS DECLARE variable T NUMERIC (18, 2);
  DECLARE variable M NUMERIC (18, 2);
  DECLARE variable P NUMERIC (18, 2);
  DECLARE variable V NUMERIC (18, 2);

BEGIN
     FOR
        SELECT DISTINCT
              mouvement.ID_ARTICLE,
              ARTICLES.ref,
              mouvement.ref_entree,
              ARTICLES.designation,
              UNITES.unite,
              FAMILLES.famille,
              ARTICLES.taux_m1,
              ARTICLES.marge1,
              ARTICLES.prix_v1,
              ARTICLES.taux_tva,
              sum(MOUVEMENT.QTE),
              sum(mouvement.VAL)
        FROM mouvement
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENT.id_article
        LEFT OUTER join unites on (unites.id_unite = articles.id_unite)
        LEFT OUTER join familles on (familles.id_famille = articles.id_famille)
        WHERE articles.matiere <> 1
        AND articles.gestion_stock <> 0
        AND mouvement.date_oper <= :D
        AND mouvement.id_magazin = :ID_MAGAZIN
        GROUP BY mouvement.ID_ARTICLE,
                 mouvement.ref_entree,
                 ARTICLES.ref,
                 ARTICLES.designation,
                 UNITES.unite,
                 FAMILLES.famille,
                 ARTICLES.taux_m1,
                 ARTICLES.marge1,
                 ARTICLES.prix_v1,
                 ARTICLES.taux_tva
        INTO :ID_ART, :REF, :REF_RENTREE, :DESIGNATION, :UNITE, :FAMILLE,:T, :M, :P, :V,
             :STOCK, :VALEUR
  DO
  BEGIN
       if (T IS NULL) then T = 0;
       if (M IS NULL) then M = 0;
       if (P IS NULL) then P = 0;
       if (V IS NULL) then V = 0;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

       if (T <> 0) then PRIX_HT = :COUT + :COUT * :T /100;
       ELSE
           if (M <> 0) then PRIX_HT = :COUT + :M;
       ELSE
           if (P <> 0) then PRIX_HT = :p;
       ELSE
           PRIX_HT = 0;
       PRIX_TTC = :PRIX_HT + :PRIX_HT * V /100;
       VALEUR_HT = :STOCK * PRIX_HT;
       VALEUR_TTC = :STOCK * PRIX_TTC;
  SUSPEND;
  END
END
^


ALTER PROCEDURE PROFORMA_ID_PROF_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(PROFORMA_ID_PROF_GEN, 1);
END
^


ALTER PROCEDURE RECAP_FACTURE_VENTE (
    D1 DATE,
    D2 DATE)
RETURNS (
    NBRE INTEGER,
    ESPECE NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    REFACTION NUMERIC (18, 2),
    SANS_REFACTION NUMERIC (18, 2),
    CESSION NUMERIC (18, 2),
    VENTE NUMERIC (18, 2),
    CONSOM NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS BEGIN
     NBRE = 0;
     ESPECE = 0;
     TIMBRE = 0;
     AUTRES = 0;
     REFACTION = 0;
     SANS_REFACTION = 0;
     CESSION = 0;
     VENTE = 0;
     CONSOM = 0;
     MARGE = 0;

     SELECT
           COUNT(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     INTO :NBRE;

     SELECT
           SUM(TOTAL_HT),
           SUM(TIMBRE)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.timbre <> 0
     AND entete_sorties.cession <> 1
     INTO :ESPECE, :TIMBRE;

     SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.timbre = 0
     AND entete_sorties.cession <> 1
     INTO :AUTRES;

     SELECT
           SUM(TOTAL_HT),
           SUM(TIMBRE)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.timbre <> 0
     AND entete_sorties.cession <> 1
     INTO :ESPECE, :TIMBRE;

     SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.cession = 1
     INTO :CESSION;

     SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.refaction = 1
     AND entete_sorties.cession <> 1
     INTO :REFACTION;

      SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.refaction = 0
     AND entete_sorties.cession <> 1
     INTO :SANS_REFACTION;

     SELECT
           SUM(entete_sorties.total_ht),
           SUM(entete_sorties.total_consommation)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     AND entete_sorties.cession <> 1
     INTO :VENTE, :CONSOM;

     MARGE = :VENTE - :CONSOM;

  SUSPEND;

END
^


ALTER PROCEDURE RECAP_FACTURE_VENTE_JOUR (
    D1 DATE,
    D2 DATE)
RETURNS (
    NBRE INTEGER,
    DTE DATE,
    TIMBRE NUMERIC (18, 2),
    HT_TVA NUMERIC (18, 2),
    HT_S_TVA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS BEGIN
     NBRE = 0;
     FOR
        SELECT distinct
              entete_sorties.date_piece
        FROM entete_sorties
        WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
        INTO :dte
     do
     BEGIN
          EXECUTE PROCEDURE VENTE_JOUR(:DTE) RETURNING_VALUES(:NBRE,:HT_S_TVA,:HT_TVA,:TVA,:TIMBRE,:TTC);
  SUSPEND;
  END
END
^


ALTER PROCEDURE RECAP_VENTE (
    D1 DATE,
    D2 DATE)
RETURNS (
    TICKET NUMERIC (18, 2),
    FACT_HT NUMERIC (18, 2),
    FACT_TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    FACT_TTC NUMERIC (18, 2),
    ESPECE NUMERIC (18, 2),
    AUTRES NUMERIC (18, 2),
    SOLDE_INI_CAISSE NUMERIC (18, 2),
    RECETTE NUMERIC (18, 2),
    REGLT_CLIENTS NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 2),
    REGLT_FOURNISSEURS NUMERIC (18, 2),
    VIR_FOND_REC NUMERIC (18, 2),
    VIR_FOND_DEP NUMERIC (18, 2),
    SOLDE_CAISSE NUMERIC (18, 2))
AS BEGIN
     SELECT
           SUM(TOTAL_TTC)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 3
     INTO :TICKET;

     SELECT
           SUM(TOTAL_HT),
           SUM(TOTAL_TVA),
           SUM(TIMBRE),
           SUM(TOTAL_TTC)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = 1
     INTO :FACT_HT, :FACT_TVA, :TIMBRE,:FACT_TTC;

     SELECT
           SUM(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.date_regt BETWEEN :D1 AND :D2
     AND regl_tiers.id_cpte = 1
     INTO :ESPECE;

     SELECT
           SUM(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.date_regt BETWEEN :D1 AND :D2
     AND regl_tiers.id_cpte <> 1
     INTO :AUTRES;

     SELECT
           SUM(comptes_tresorie.solde_ini)
     FROM comptes_tresorie
     WHERE comptes_tresorie.id_cpte = 1
     INTO :SOLDE_INI_CAISSE;

     SELECT
           CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2))
     FROM regl_tiers
     WHERE regl_tiers.id_cpte = 1
     AND regl_tiers.type_regt = 1
     AND regl_tiers.date_regt <= :D2
     INTO :REGLT_CLIENTS;

     SELECT
           CAST(SUM(RECETTES.montant) AS NUMERIC(18,2))
     FROM RECETTES
     WHERE RECETTES.id_cpte = 1
     AND RECETTES.date_rec  <= :D2
     INTO :RECETTE;

     SELECT
           CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2))
     FROM regl_tiers
     WHERE regl_tiers.id_cpte = 1
     AND regl_tiers.type_regt = 0
     AND regl_tiers.date_regt  <= :D2
     INTO :REGLT_FOURNISSEURS;

     SELECT
           CAST(SUM(DEPENSES.montant) AS NUMERIC(18,2))
     FROM DEPENSES
     WHERE DEPENSES.id_cpte = 1
     AND DEPENSES.date_dep  <= :D2
     INTO :DEPENSE;

     SELECT
           CAST(SUM(vir_fonds.montant) AS NUMERIC(18,2))
     FROM vir_fonds
     WHERE vir_fonds.id_compte_debit = 1
     AND vir_fonds.date_oper  <= :D2
     INTO :VIR_FOND_REC;

     SELECT
           CAST(SUM(vir_fonds.montant) AS NUMERIC(18,2))
     FROM vir_fonds
     WHERE vir_fonds.id_compte_credit = 1
     AND vir_fonds.date_oper  <= :D2
     INTO :VIR_FOND_DEP;

     if (SOLDE_INI_CAISSE IS NULL) then SOLDE_INI_CAISSE = 0;
     if (RECETTE IS NULL) then RECETTE = 0;
     if (DEPENSE IS NULL) then DEPENSE = 0;
     if (VIR_FOND_REC IS NULL) then VIR_FOND_REC = 0;
     if (VIR_FOND_DEP IS NULL) then VIR_FOND_DEP = 0;
     if (REGLT_CLIENTS IS NULL) then REGLT_CLIENTS = 0;
     if (REGLT_FOURNISSEURS IS NULL) then REGLT_FOURNISSEURS = 0;

     SOLDE_CAISSE = :SOLDE_INI_CAISSE + :RECETTE - :DEPENSE + :VIR_FOND_REC - :VIR_FOND_DEP + :REGLT_CLIENTS - :REGLT_FOURNISSEURS;

  SUSPEND;

END
^


ALTER PROCEDURE RECAP_VENTES (
    DU DATE,
    AU DATE)
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    ID_SORTIE INTEGER,
    NUM_PIECE VARCHAR (12),
    DATE_PIECE DATE,
    SOLDE_DEB_EXE NUMERIC (18, 2),
    VENTES_ANT NUMERIC (18, 2),
    AVOIRS_ANT NUMERIC (18, 2),
    REGLTS_ANT NUMERIC (18, 2),
    SOLDE_INI NUMERIC (18, 2),
    TOTAL_VENTE NUMERIC (18, 2),
    TOTAL_REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    QTE1 NUMERIC (18, 6),
    VAL1 NUMERIC (18, 2),
    QTE2 NUMERIC (18, 6),
    VAL2 NUMERIC (18, 2),
    QTE3 NUMERIC (18, 6),
    VAL3 NUMERIC (18, 2),
    QTE4 NUMERIC (18, 6),
    VAL4 NUMERIC (18, 2),
    QTE5 NUMERIC (18, 6),
    VAL5 NUMERIC (18, 2),
    QTE6 NUMERIC (18, 6),
    VAL6 NUMERIC (18, 2),
    QTE7 NUMERIC (18, 6),
    VAL7 NUMERIC (18, 2),
    QTE8 NUMERIC (18, 6),
    VAL8 NUMERIC (18, 2),
    QTE9 NUMERIC (18, 6),
    VAL9 NUMERIC (18, 2),
    QTE10 NUMERIC (18, 6),
    VAL10 NUMERIC (18, 2))
AS DECLARE variable ID_ART1 INTEGER;
  DECLARE variable ID_ART2 INTEGER;
  DECLARE variable ID_ART3 INTEGER;
  DECLARE variable ID_ART4 INTEGER;
  DECLARE variable ID_ART5 INTEGER;
  DECLARE variable ID_ART6 INTEGER;
  DECLARE variable ID_ART7 INTEGER;
  DECLARE variable ID_ART8 INTEGER;
  DECLARE variable ID_ART9 INTEGER;
  DECLARE variable ID_ART10 INTEGER;

BEGIN
     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 1 TO 1
     INTO :ID_ART1;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 2 TO 2
     INTO :ID_ART2;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 3 TO 3
     INTO :ID_ART3;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 4 TO 4
     INTO :ID_ART4;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 5 TO 5
     INTO :ID_ART5;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ROWS 6 TO 6
     INTO :ID_ART6;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 7 TO 7
     INTO :ID_ART7;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 8 TO 8
     INTO :ID_ART8;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 9 TO 9
     INTO :ID_ART9;

     SELECT DISTINCT
            SORTIES.id_article
     FROM sorties
     WHERE  sorties.date_piece BETWEEN :DU AND :AU
     AND sorties.ajouter_creance = 1
     ORDER BY 1
     ROWS 10 TO 10
     INTO :ID_ART10;

     FOR
        SELECT DISTINCT
              entete_sorties.id_client,
              CLIENTS.denomination,
              entete_sorties.num_piece,
              entete_sorties.date_piece,
              entete_sorties.id_ent_sort
        FROM entete_sorties
        INNER JOIN CLIENTS ON CLIENTS.id_client = entete_sorties.id_client
        WHERE entete_sorties.nature_sortie IN (1,2)
        AND entete_sorties.date_piece BETWEEN :DU AND :AU
        AND entete_sorties.ajouter_creance = 1
        INTO :ID_CLIENT, :DENOMINATION, :NUM_PIECE, :DATE_PIECE, :ID_SORTIE
     DO
     BEGIN
          SELECT
                SOLDE_INI
          FROM CLIENTS
          WHERE ID_CLIENT = :ID_CLIENT
          INTO :SOLDE_DEB_EXE;
          if (SOLDE_DEB_EXE IS NULL) then SOLDE_DEB_EXE = 0;

          SELECT
                sum(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE id_client = :ID_CLIENT
          AND ENTETE_SORTIES.nature_sortie <> 5
          AND entete_sorties.date_piece < :DU
          AND entete_sorties.ajouter_creance = 1
          INTO :VENTES_ANT;
          if (VENTES_ANT IS NULL) then VENTES_ANT = 0;

          SELECT
                sum(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE id_client = :ID_CLIENT
          AND ENTETE_SORTIES.nature_sortie = 5
          AND entete_sorties.date_piece < :DU
          AND entete_sorties.ajouter_creance = 1
          INTO :AVOIRS_ANT;
          if (AVOIRS_ANT IS NULL) then AVOIRS_ANT = 0;

          SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_CLIENT
          AND regl_tiers.date_regt < :DU
          INTO :REGLTS_ANT;
          if (REGLTS_ANT IS NULL) then REGLTS_ANT = 0;

          if (VENTES_ANT IS NULL) then VENTES_ANT = 0;
          if (AVOIRS_ANT IS NULL) then AVOIRS_ANT = 0;
          if (REGLTS_ANT IS NULL) then REGLTS_ANT = 0;
          SOLDE_INI = :SOLDE_DEB_EXE + :VENTES_ANT - :AVOIRS_ANT - :REGLTS_ANT;

          SELECT
                SUM(entete_sorties.total_facture)
          FROM entete_sorties
          WHERE entete_sorties.id_client = :ID_CLIENT
          AND ENTETE_SORTIES.nature_sortie <> 5
          AND entete_sorties.date_piece BETWEEN :DU AND :AU
          AND entete_sorties.ajouter_creance = 1
          INTO :TOTAL_VENTE;
          if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;


          SELECT
                SUM(regl_tiers.montant)
          FROM regl_tiers
          WHERE regl_tiers.id_tier = :ID_CLIENT
          AND regl_tiers.date_regt BETWEEN :DU AND :AU
          INTO :TOTAL_REGLT;
          if (TOTAL_REGLT IS NULL) then TOTAL_REGLT = 0;

          SOLDE = :SOLDE_INI + :TOTAL_VENTE - :TOTAL_REGLT;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART1
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE1, :VAL1;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART2
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE2, :VAL2;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART3
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE3, :VAL3;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART4
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE4, :VAL4;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART5
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE5, :VAL5;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART6
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE6, :VAL6;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART7
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE7, :VAL7;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART8
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE8, :VAL8;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART9
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE9, :VAL9;

          SELECT
                SUM(sorties.quantite),
                SUM(sorties.HT)
          FROM sorties
          WHERE SORTIES.id_article = :ID_ART10
          AND SORTIES.id_client = :ID_CLIENT
          AND SORTIES.nature_sortie <> 5
          AND SORTIES.date_piece BETWEEN :DU AND :AU
          AND sorties.ajouter_creance = 1
          AND sorties.id_entete = :ID_SORTIE
          INTO :QTE10, :VAL10;

  SUSPEND;
  END
END
^


ALTER PROCEDURE RECAP_VENTE_TAUX_TVA (
    D1 DATE,
    D2 DATE)
RETURNS (
    TX_TVA NUMERIC (18, 2),
    HT_AV_REM NUMERIC (18, 2),
    REMISES NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
            taux_tva AS taux,
            sum(ht_avant_remise) AS ht_av,
            sum(remise) AS remise,
            sum(ht) AS ht,
            sum(tva) AS tva
        FROM SORTIES
        WHERE SORTIES.date_piece BETWEEN :D1 AND :D2
        GROUP BY taux_tva
        INTO :TX_TVA, :HT_AV_REM, :REMISES, :HT, :TVA
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE RECAP_VENTE_TVA (
    NATURE SMALLINT,
    D1 DATE,
    D2 DATE)
RETURNS (
    TAUX_TVA NUMERIC (18, 2),
    HT NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
              SORTIES.taux_tva,
              SUM(SORTIES.HT),
              SUM(SORTIES.TVA),
              SUM(SORTIES.TTC)
        FROM SORTIES
        INNER JOIN entete_sorties ON entete_sorties.id_ent_sort = SORTIES.id_entete
        WHERE sorties.nature_sortie = :NATURE
        AND sorties.cession <> 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2
        GROUP BY TAUX_TVA
        INTO :TAUX_TVA ,:HT , :TVA, :TTC
     do
     BEGIN

  SUSPEND;
  END
END
^


ALTER PROCEDURE RECETTES_ID_REC_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(RECETTES_ID_REC_GEN, 1);
END
^


ALTER PROCEDURE REGLEMENTS_ID_REGLEMENT_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(REGLEMENTS_ID_REGLEMENT_GEN, 1);
END
^


ALTER PROCEDURE REGL_TIERS_ID_REGL_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(REGL_TIERS_ID_REGL_GEN, 1);
END
^


ALTER PROCEDURE RELEVE_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CLIENT INTEGER,
    NUM INTEGER,
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    VENTE NUMERIC (18, 2),
    REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    TP_OP INTEGER)
AS BEGIN
     SOLDE = 0;
     FOR
        SELECT
              CAST(CLIENTS.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('SOLDE DEBUT DE L''EXERCICE' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(CLIENTS.solde_ini AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM CLIENTS
        WHERE CLIENTS.id_client = :ID
        AND CLIENTS.solde_ini IS NOT NULL
        AND CLIENTS.solde_ini <> 0
        AND CLIENTS.client = 1

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL TICKETS ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(SUM(entete_sorties.total_facture) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 3
        AND entete_sorties.ajouter_creance = 1
        AND entete_sorties.date_piece < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE DE VENTE ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(SUM(entete_sorties.total_facture) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 1
        AND entete_sorties.ajouter_creance = 1
        AND entete_sorties.date_piece < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL B.L. ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(SUM(entete_sorties.total_facture) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 2
        AND entete_sorties.ajouter_creance = 1
        AND entete_sorties.date_piece < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''AVOIR ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(entete_sorties.total_facture) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 5
        AND entete_sorties.ajouter_creance = 1
        AND entete_sorties.date_piece < :D1
        GROUP BY entete_sorties.id_client

       UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BONS RETOUR ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(entete_sorties.total_facture) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 7
        AND entete_sorties.ajouter_creance = 1
        AND entete_sorties.date_piece < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS INTEGER),
              CAST(entete_sorties.date_piece AS DATE),
              CAST('FACTURE DE VENTE N? :'||entete_sorties.num_piece AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(entete_sorties.total_facture AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 1
        AND entete_sorties.ajouter_creance = 1
        AND ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS INTEGER),
              CAST(entete_sorties.date_piece AS DATE),
              CAST('BON DE LIVRAISON N? :'||entete_sorties.num_piece AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(entete_sorties.total_facture AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 2
        AND entete_sorties.ajouter_creance = 1
        AND ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS INTEGER),
              CAST(entete_sorties.date_piece AS DATE),
              CAST('TICKET DE CAISSE N? :'||entete_sorties.num_piece AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(entete_sorties.total_facture AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 3
        AND entete_sorties.ajouter_creance = 1
        AND ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS INTEGER),
              CAST(entete_sorties.date_piece AS DATE),
              CAST('FACTURE D''AVOIR N? :'||entete_sorties.num_piece AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(entete_sorties.total_facture AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 5
        AND entete_sorties.ajouter_creance = 1
        AND ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS INTEGER),
              CAST(entete_sorties.date_piece AS DATE),
              CAST('BON DE RETOUR N? :'||entete_sorties.num_piece AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(entete_sorties.total_facture AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_sorties
        WHERE entete_sorties.id_client = :ID
        AND entete_sorties.nature_sortie = 7
        AND entete_sorties.ajouter_creance = 1
        AND ENTETE_SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.num_piece AS INTEGER),
              CAST(regl_tiers.date_regt AS DATE),
              CAST('BON REGL CLIENT N? :'||regl_tiers.NUM_PIECE AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(regl_tiers.montant AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 1
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.num_piece AS INTEGER),
              CAST(regl_tiers.date_regt AS DATE),
              CAST('AVOIR COM. CLIENT N? :'||regl_tiers.NUM_PIECE AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(regl_tiers.montant AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 2
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BON REGL CLIENT ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 1
        AND REGL_TIERS.date_regt < :D1
        GROUP BY REGL_TIERS.id_tier

         UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(0 AS INTEGER),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL AVOIR COM CLIENT ANT.' AS VARCHAR(64) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(-SUM(regl_tiers.montant) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 2
        AND REGL_TIERS.date_regt < :D1
        GROUP BY REGL_TIERS.id_tier

        ORDER BY 3,7,2
        INTO :ID_CLIENT, :NUM, :DATE_OPER, :LIBELLE, :VENTE, :REGLT, :TP_OP

        DO
        BEGIN
             if (VENTE IS NULL) then VENTE = 0;
             if (REGLT IS null) then REGLT = 0;
             SOLDE = :SOLDE + :VENTE - :REGLT;
  SUSPEND;
  END
END
^


ALTER PROCEDURE RELEVE_CLIENT_DETAIL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    PRODUIT VARCHAR (128),
    QTE NUMERIC (18, 6),
    FRX NUMERIC (18, 6),
    PU NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    RGT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    NAT SMALLINT,
    ORD SMALLINT)
AS DECLARE variable NUM VARCHAR(12);
BEGIN
     SOLDE = 0;
     FOR
        SELECT
              :D1-1,
              CAST('            ' AS varchar(12)),
              CAST('SOLDE INITIAL : ' AS varchar(64)),
              CAST(' ' AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              clients.solde_ini,
              cast(0 AS numeric(18,2)),
              cast(0 AS smallint)
        FROM CLIENTS
        WHERE CLIENTS.id_client = :id
        AND CLIENTS.solde_ini <> 0

        UNION ALL

         SELECT
              sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST('FACTURE N? : '||entete_sorties.num_piece AS varchar(64)),
              CAST(ARTICLES.ref||'-'||articles.designation AS varchar(128)),
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(1 AS smallint)
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              entete_sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL FACTURE N0 : '||entete_sorties.num_piece AS varchar(128)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              entete_sorties.total_facture,
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM entete_sorties
        WHERE entete_sorties.id_client = :id
        AND entete_sorties.nature_sortie = 1
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST('B.L. N? : '||entete_sorties.num_piece AS varchar(64)),
              CAST(ARTICLES.ref||'-'||articles.designation AS varchar(128)),
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(3 AS smallint)
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 2
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              entete_sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL B.L. N0 : '||entete_sorties.num_piece AS varchar(128)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              entete_sorties.total_facture,
              cast(0 AS numeric(18,2)),
              cast(4 AS smallint)
        FROM entete_sorties
        WHERE entete_sorties.id_client = :id
        AND entete_sorties.nature_sortie = 2
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST('AVOIR N? : '||entete_sorties.num_piece AS varchar(64)),
              CAST(ARTICLES.ref||'-'||articles.designation AS varchar(128)),
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(5 AS smallint)
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 5
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              entete_sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL AVOIR N0 : '||entete_sorties.num_piece AS varchar(128)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              entete_sorties.total_facture,
              cast(6 AS smallint)
        FROM entete_sorties
        WHERE entete_sorties.id_client = :id
        AND entete_sorties.nature_sortie = 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST('RETOUR N? : '||entete_sorties.num_piece AS varchar(64)),
              CAST(ARTICLES.ref||'-'||articles.designation AS varchar(128)),
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(7 AS smallint)
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 7
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
              entete_sorties.date_piece,
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL RETOUR N0 : '||entete_sorties.num_piece AS varchar(128)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              entete_sorties.total_facture,
              cast(8 AS smallint)
        FROM entete_sorties
        WHERE entete_sorties.id_client = :id
        AND entete_sorties.nature_sortie = 7
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              regl_tiers.date_regt,
              CAST(regl_tiers.num_piece AS varchar(12)),
              cast('REGLT. N? : '||regl_tiers.num_piece AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(regl_tiers.montant AS numeric(18,2)),
              cast(9 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 1
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              regl_tiers.date_regt,
              CAST(regl_tiers.num_piece AS varchar(12)),
              cast('AVOIR COM N? : '||regl_tiers.num_piece AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(regl_tiers.montant AS numeric(18,2)),
              cast(9 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 2
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        ORDER BY 1, 2, 10

        INTO :DATE_OPER, :NUM, :LIBELLE, :PRODUIT, :QTE, :FRX, :PU, :TTC, :RGT, NAT

        DO
        BEGIN
             if (TTC IS NULL) then TTC = 0;
             if (RGT IS null) then RGT = 0;
             SOLDE = :SOLDE + :TTC - :RGT ;
             if (TTC = 0) then TTC = NULL;
             if (RGT = 0) then RGT = NULL;
             if (QTE = 0) then QTE = NULL;
             if (FRX = 0) then FRX = NULL;
             if (PU = 0) then PU = NULL;

  SUSPEND;
  END
END
^


ALTER PROCEDURE RELEVE_COMPTE (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CPTE INTEGER,
    DATE_OP DATE,
    LIB VARCHAR (64),
    CHEQUE VARCHAR (7),
    FAM_REC_DEP VARCHAR (64),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2),
    OP INTEGER,
    ETAT VARCHAR (16))
AS DECLARE VARIABLE  SD NUMERIC (18, 2);
  DECLARE VARIABLE  DD DATE;
  DECLARE VARIABLE  ordder smallint;
BEGIN
     SD = 0;
     DD = :D1 - 1;
     FOR
        SELECT
               CAST(vir_fonds.id_compte_debit AS INTEGER),
               CAST('' AS VARCHAR(7)),
               cast(:DD AS date),
               CAST('TOTAL RETRAIT ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(SUM(VIR_FONDS.montant) AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM VIR_FONDS
        WHERE vir_fonds.id_compte_debit = :id
        AND VIR_FONDS.date_oper < :D1
        GROUP BY vir_fonds.id_compte_debit

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_CREDIT AS INTEGER),
               CAST('' AS VARCHAR(7)),
               cast(:DD AS date),
               CAST('TOTAL VERSEMENT ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(SUM(VIR_FONDS.montant) AS numeric(18,2)),
               CAST(2 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               2
        FROM VIR_FONDS
        WHERE vir_fonds.id_compte_CREDIT = :id
        AND VIR_FONDS.date_oper < :D1
        GROUP BY vir_fonds.id_compte_CREDIT

        UNION all

        SELECT
              CAST(comptes_tresorie.id_cpte AS INTEGER),
              CAST('' AS VARCHAR(7)),
              cast(:DD AS date),
              CAST('Solde d?but d''exercice'  AS VARCHAR(64) CHARACTER SET NONE),
              CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
              CAST(comptes_tresorie.solde_ini AS numeric(18,2)) AS recette,
              CAST(0 AS numeric(18,2)) AS depense,
              CAST(1 AS INTEGER) AS OP,
              CAST('' AS VARCHAR(16)),
              1
        FROM comptes_tresorie
        WHERE comptes_tresorie.solde_ini IS not null
        AND id_cpte = :id


        UNION all

        SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(:DD AS DATE),
               CAST('TOTAL REGLEMENTS CLIENTS ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(SUM(regl_tiers.montant) AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM regl_tiers
        WHERE regl_tiers.type_regt = 1
        AND regl_tiers.date_regt < :D1
        AND regl_tiers.ETAT <> 'Annul?'
        AND id_cpte = :id
        GROUP BY id_cpte

        UNION all

        SELECT
               CAST(RECETTES.id_cpte AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(:DD AS DATE),
               CAST('TOTAL RECETTES ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(SUM(RECETTES.montant) AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM RECETTES
        WHERE id_cpte = :id
        AND RECETTES.date_rec < :D1
        AND RECETTES.ETAT <> 'Annul?'
        GROUP BY ID_CPTE

        UNION all

        SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(:DD AS DATE),
               CAST('TOTAL REGLEMENTS FOURN. ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(SUM(regl_tiers.montant) AS numeric(18,2)),
               CAST(2 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               2
        FROM regl_tiers
        WHERE regl_tiers.type_regt = 0
        AND id_cpte = :id
        AND regl_tiers.date_regt < :d1
        AND regl_tiers.ETAT <> 'Annul?'
        GROUP BY id_cpte

        UNION ALL

        SELECT
               CAST(depenses.id_cpte AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(:DD AS DATE),
               CAST('TOTAL DEPENSES ANT.'  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(SUM(depenses.montant) AS numeric(18,2)),
               CAST(2 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               2
        FROM depenses
        WHERE id_cpte = :id
        AND DEPENSES.date_dep < :D1
        AND DEPENSES.ETAT <> 'Annul?'
        GROUP BY ID_CPTE

        UNION all

        SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               cast(regl_tiers.num_cheque AS varchar(7)),
               CAST(regl_tiers.date_regt AS DATE),
               CAST('REGL. CL. : ' ||CLIENTS.denomination  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(regl_tiers.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               cast(regl_tiers.etat AS varchar(16)),
               1
        FROM regl_tiers
        INNER JOIN CLIENTS ON CLIENTS.id_client = regl_tiers.id_tier
        WHERE regl_tiers.type_regt = 1
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
        AND regl_tiers.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(RECETTES.id_cpte AS INTEGER),
               cast(RECETTES.num_cheque AS varchar(7)),
               CAST(RECETTES.date_rec AS DATE),
               CAST(recettes.lib_rec  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(familles_recettes.famille  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(RECETTES.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               cast(RECETTES.etat AS varchar(16)),
               1
        FROM RECETTES
        LEFT OUTER JOIN familles_recettes ON familles_recettes.id_fam_rec = recettes.id_type_rec
        WHERE id_cpte = :id
        AND RECETTES.date_rec BETWEEN :D1  AND :D2
        AND RECETTES.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               cast(regl_tiers.num_cheque AS varchar(7)),
               CAST(regl_tiers.date_regt AS DATE),
               CAST('REGL. FOUR. : ' ||CLIENTS.denomination  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(regl_tiers.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               cast(regl_tiers.etat AS varchar(16)),
               2
        FROM regl_tiers
        INNER JOIN CLIENTS ON CLIENTS.id_client = regl_tiers.id_tier
        WHERE regl_tiers.type_regt = 0
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
        AND regl_tiers.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(depenses.id_cpte AS INTEGER),
               cast(DEPENSES.num_cheque AS varchar(7)),
               CAST(DEPENSES.date_dep AS DATE),
               CAST(depenses.lib_dep  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(familles_charges.famille  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(depenses.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               cast(DEPENSES.etat AS varchar(16)),
               2
        FROM depenses
        LEFT OUTER JOIN familles_charges ON familles_charges.id_fam = DEPENSES.id_charge
        WHERE id_cpte = :id
        AND DEPENSES.date_dep BETWEEN :D1  AND :D2
        AND DEPENSES.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_debit AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               CAST(VIR_FONDS.LIBELLE  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(VIR_FONDS.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM VIR_FONDS
        WHERE id_compte_debit = :id
        AND VIR_FONDS.date_oper BETWEEN :D1  AND :D2

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_CREDIT AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               CAST(VIR_FONDS.LIBELLE  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(VIR_FONDS.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               2
        FROM VIR_FONDS
        WHERE id_compte_CREDIT = :id
        AND VIR_FONDS.date_oper BETWEEN :D1  AND :D2

        ORDER BY 3,10

        INTO :ID_CPTE, :CHEQUE, :DATE_OP, :LIB, :FAM_REC_DEP, :RECETTE, :DEPENSE, :OP, :ETAT, :ordder
     do
     BEGIN
          if (:RECETTE IS NULL) then RECETTE = 0;
          if (:DEPENSE IS NULL) then DEPENSE = 0;
          SD = :SD +  :RECETTE - :DEPENSE;
          SOLDE = :SD;
          if (:RECETTE = 0) then RECETTE = NULL;
          if (:DEPENSE = 0) then DEPENSE = NULL;
          if (:SOLDE = 0) then SOLDE = NULL;
  SUSPEND;
  END
END
^


ALTER PROCEDURE RELEVE_COMPTE_SANS (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_CPTE INTEGER,
    DATE_OP DATE,
    LIB VARCHAR (64),
    CHEQUE VARCHAR (7),
    FAM_REC_DEP VARCHAR (64),
    RECETTE NUMERIC (18, 2),
    DEPENSE NUMERIC (18, 6),
    SOLDE NUMERIC (18, 2),
    OP INTEGER,
    ETAT VARCHAR (16))
AS DECLARE VARIABLE  SD NUMERIC (18, 2);
  DECLARE VARIABLE  DD DATE;
  DECLARE VARIABLE  ordder smallint;
BEGIN
     SD = 0;
     DD = :D1 - 1;
     FOR
          SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               cast(regl_tiers.num_cheque AS varchar(7)),
               CAST(regl_tiers.date_regt AS DATE),
               CAST('REGL. CL. : ' ||CLIENTS.denomination  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(regl_tiers.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               cast(regl_tiers.etat AS varchar(16)),
               1
        FROM regl_tiers
        INNER JOIN CLIENTS ON CLIENTS.id_client = regl_tiers.id_tier
        WHERE regl_tiers.type_regt = 1
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
        AND regl_tiers.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(RECETTES.id_cpte AS INTEGER),
               cast(RECETTES.num_cheque AS varchar(7)),
               CAST(RECETTES.date_rec AS DATE),
               CAST(recettes.lib_rec  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(familles_recettes.famille  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(RECETTES.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               cast(RECETTES.etat AS varchar(16)),
               1
        FROM RECETTES
        LEFT OUTER JOIN familles_recettes ON familles_recettes.id_fam_rec = recettes.id_type_rec
        WHERE id_cpte = :id
        AND RECETTES.date_rec BETWEEN :D1  AND :D2
        AND RECETTES.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(regl_tiers.id_cpte AS INTEGER),
               cast(regl_tiers.num_cheque AS varchar(7)),
               CAST(regl_tiers.date_regt AS DATE),
               CAST('REGL. FOUR. : ' ||CLIENTS.denomination  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(regl_tiers.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               cast(regl_tiers.etat AS varchar(16)),
               2
        FROM regl_tiers
        INNER JOIN CLIENTS ON CLIENTS.id_client = regl_tiers.id_tier
        WHERE regl_tiers.type_regt = 0
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
        AND regl_tiers.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(depenses.id_cpte AS INTEGER),
               cast(DEPENSES.num_cheque AS varchar(7)),
               CAST(DEPENSES.date_dep AS DATE),
               CAST(depenses.lib_dep  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(familles_charges.famille  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(depenses.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               cast(DEPENSES.etat AS varchar(16)),
               2
        FROM depenses
        LEFT OUTER JOIN familles_charges ON familles_charges.id_fam = DEPENSES.id_charge
        WHERE id_cpte = :id
        AND DEPENSES.date_dep BETWEEN :D1  AND :D2
        AND DEPENSES.etat <> 'Annul?'

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_debit AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               CAST(VIR_FONDS.LIBELLE  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(VIR_FONDS.montant AS numeric(18,2)),
               cast(0 AS numeric(18,2)),
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM VIR_FONDS
        WHERE id_compte_debit = :id
        AND VIR_FONDS.date_oper BETWEEN :D1  AND :D2

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_CREDIT AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               CAST(VIR_FONDS.LIBELLE  AS VARCHAR(64) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(64) CHARACTER SET NONE),
               cast(0 AS numeric(18,2)),
               CAST(VIR_FONDS.montant AS numeric(18,2)),
               CAST(2 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               2
        FROM VIR_FONDS
        WHERE id_compte_CREDIT = :id
        AND VIR_FONDS.date_oper BETWEEN :D1  AND :D2

        ORDER BY 3, 10

        INTO :ID_CPTE, :CHEQUE, :DATE_OP, :LIB, :FAM_REC_DEP, :RECETTE, :DEPENSE, :OP, :ETAT, :ordder
     do
     BEGIN
          if (:RECETTE IS NULL) then RECETTE = 0;
          if (:DEPENSE IS NULL) then DEPENSE = 0;
          SD = :SD +  :RECETTE - :DEPENSE;
          SOLDE = :SD;
          if (:RECETTE = 0) then RECETTE = NULL;
          if (:DEPENSE = 0) then DEPENSE = NULL;
          if (:SOLDE = 0) then SOLDE = NULL;
  SUSPEND;
  END
END
^


ALTER PROCEDURE RELEVE_FOURNISSEUR (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_FOUR INTEGER,
    NUM VARCHAR (12),
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    ACHAT NUMERIC (18, 2),
    REGLT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    TP_OP INTEGER)
AS BEGIN
     SOLDE = 0;
     FOR
        SELECT
              CAST(CLIENTS.id_client AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('SOLDE INITIAL' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(CLIENTS.solde_ini AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM CLIENTS
        WHERE CLIENTS.id_client = :ID
        AND CLIENTS.solde_ini IS NOT NULL
        AND CLIENTS.solde_ini <> 0
        AND CLIENTS.client = 0

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''ACHAT ANT.' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(SUM(entete_entrees.net_a_payer) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :ID
        AND entete_entrees.nature_entree = 2
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact < :D1
        GROUP BY entete_entrees.id_fourn

        UNION ALL

        SELECT
              CAST(DEPENSES.ID_TIERS AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL DEPENSES ANT.' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(SUM(DEPENSES.MONTANT) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :ID
        AND DEPENSES.DATE_DEP < :D1
        GROUP BY DEPENSES.ID_TIERS

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''AVOIR ANR' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(entete_entrees.net_a_payer) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :ID
        AND entete_entrees.nature_entree = 4
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact < :D1
        GROUP BY entete_entrees.id_fourn

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BON REGL FOURN ANT' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 0
        AND REGL_TIERS.date_regt < :D1
        GROUP BY regl_tiers.id_tier

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('0' AS VARCHAR(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL AVOIRS COM FOURN ANT' AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(-SUM(regl_tiers.montant) AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 3
        AND REGL_TIERS.date_regt < :D1
        GROUP BY regl_tiers.id_tier

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST(entete_entrees.num_fact AS VARCHAR(12)),
              CAST(entete_entrees.date_fact AS DATE),
              CAST('FACTURE D''ACHAT N? :'||entete_entrees.num_FACT AS VARCHAR(64) CHARACTER SET NONE),
              CAST(entete_entrees.net_a_payer AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :ID
        AND entete_entrees.nature_entree = 2
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(DEPENSES.ID_TIERS AS INTEGER),
              CAST(DEPENSES.NUM_DEP AS VARCHAR(12)),
              CAST(DEPENSES.DATE_DEP AS DATE),
              CAST('DEPENSE N? :'||DEPENSES.NUM_DEP AS VARCHAR(64) CHARACTER SET NONE),
              CAST(DEPENSES.MONTANT AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :ID
        AND DEPENSES.DATE_DEP BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(fact_dd_jointe.id_fourn AS INTEGER),
              CAST(fact_dd_jointe.num_fact AS VARCHAR(12)),
              CAST(fact_dd_jointe.date_fact AS DATE),
              CAST('FACTURE JOINTE DOS IMPORT. N? :'||entete_entrees.NUM_D10 AS VARCHAR(64) CHARACTER SET NONE),
              CAST(fact_dd_jointe.TTC AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM fact_dd_jointe
        INNER JOIN entete_entrees ON entete_entrees.id_ent_ent = fact_dd_jointe.id_dd
        WHERE fact_dd_jointe.id_fourn = :ID
        AND fact_dd_jointe.date_fact BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST(entete_entrees.num_fact AS VARCHAR(12)),
              CAST(entete_entrees.date_fact AS DATE),
              CAST('FACTURE D''AVOIR N? :'||entete_entrees.num_FACT AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(entete_entrees.net_a_payer AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :ID
        AND entete_entrees.nature_entree = 4
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.num_piece AS VARCHAR(12)),
              CAST(regl_tiers.date_regt AS DATE),
              CAST('BON REGL FOURN N? :'||regl_tiers.NUM_PIECE AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(regl_tiers.montant AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 0
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.num_piece AS VARCHAR(12)),
              CAST(regl_tiers.date_regt AS DATE),
              CAST('AVOIR COM FOURN N? :'||regl_tiers.NUM_PIECE AS VARCHAR(64) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(regl_tiers.montant AS NUMERIC(18,2)),
              CAST(2 AS NUMERIC(18,2))
        FROM regl_tiers
        WHERE REGL_TIERS.id_tier = :ID
        AND regl_tiers.type_regt = 3
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

        ORDER BY 3,6,2
        INTO :ID_FOUR, :NUM, :DATE_OPER, :LIBELLE, :ACHAT, :REGLT, :TP_OP
        DO
        BEGIN
             if (ACHAT IS NULL) then ACHAT = 0;
             if (REGLT IS null) then REGLT = 0;
             SOLDE = :SOLDE + :ACHAT - :REGLT;
  SUSPEND;
  END
END
^


ALTER PROCEDURE RELEVE_FOURNISSEUR_DETAIL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DATE_OPER DATE,
    LIBELLE VARCHAR (64),
    PRODUIT VARCHAR (128),
    QTE NUMERIC (18, 6),
    FRX NUMERIC (18, 6),
    PU NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    RGT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2),
    NAT SMALLINT)
AS DECLARE VARIABLE NUM VARCHAR(12);
BEGIN
     SOLDE = 0;
     FOR
        SELECT
              :D1,
              CAST('            ' AS varchar(12)),
              CAST('SOLDE INITIAL : ' AS varchar(64)),
              CAST(' ' AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              clients.solde_ini,
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM CLIENTS
        WHERE CLIENTS.id_client = :id
        AND CLIENTS.solde_ini <> 0

        UNION

        SELECT
              entrees.date_entree,
              cast(entete_entrees.num_fact AS varchar(12)),
              CAST('B.R. N? : '||entete_entrees.num_fact AS varchar(64)),
              articles.designation,
              entrees.quantite,
              entrees.quantite_conditionne,
              entrees.p_u_da,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM entrees
        inner join articles on articles.id_art = entrees.id_article
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.id_four = :id
        AND entrees.nature_entree <> 4
        AND entrees.date_entree BETWEEN :D1 AND :D2

        UNION

        SELECT
              entete_entrees.date_fact,
              cast(entete_entrees.num_fact AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL B.R. N? : '||entete_entrees.num_fact AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              entete_entrees.net_a_payer,
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :id
        AND entete_entrees.nature_entree <> 4
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

         UNION

        SELECT
              fact_dd_jointe.date_fact,
              CAST('            ' AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('FACTURE JOINT : '||fact_dd_jointe.num_fact || ' DOS IMPORT : '||entete_entrees.NUM_D10 AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              fact_dd_jointe.TTC,
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM fact_dd_jointe
        INNER JOIN entete_entrees ON entete_entrees.id_ent_ent = fact_dd_jointe.id_dd
        WHERE fact_dd_jointe.id_fourn = :id
        AND fact_dd_jointe.date_FACT BETWEEN :D1 AND :D2

        UNION

        SELECT
              DEPENSES.DATE_DEP,
              CAST('            ' AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('DEPENSE : '||DEPENSES.NUM_DEP AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              DEPENSES.MONTANT,
              cast(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :id
        AND DEPENSES.DATE_DEP BETWEEN :D1 AND :D2

        UNION

        SELECT
              entrees.date_entree,
              cast(entete_entrees.num_fact AS varchar(12)),
              CAST('AVOIR B.R. N? : '||entete_entrees.num_fact AS varchar(64)),
              articles.designation,
              entrees.quantite,
              entrees.quantite_conditionne,
              entrees.p_u_da,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(1 AS smallint)
        FROM entrees
        inner join articles on articles.id_art = entrees.id_article
        inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
        WHERE entrees.id_four = :id
        AND entrees.nature_entree = 4
        AND entrees.date_entree BETWEEN :D1 AND :D2

        UNION

        SELECT
              entete_entrees.date_fact,
              cast(entete_entrees.num_fact AS varchar(12)),
              CAST(' ' AS varchar(64)),
              CAST('TOTAL  AVOIR B.R. N? : '||entete_entrees.num_fact AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              entete_entrees.net_a_payer,
              cast(1 AS smallint)
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :id
        AND entete_entrees.nature_entree = 4
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

        UNION

        SELECT
              regl_tiers.date_regt,
              cast(regl_tiers.num_piece AS varchar(12)),
              cast('REGLT. N? : '||regl_tiers.num_piece AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(regl_tiers.montant AS numeric(18,2)),
              cast(1 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 0
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

         UNION

        SELECT
              regl_tiers.date_regt,
              cast(regl_tiers.num_piece AS varchar(12)),
              cast('AVOIR COM. N? : '||regl_tiers.num_piece AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(regl_tiers.montant AS numeric(18,2)),
              cast(1 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 3
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

         ORDER BY 1, 2, 10

        INTO :DATE_OPER, :NUM, :LIBELLE, :PRODUIT, :QTE, :FRX, :PU, :TTC, :RGT, NAT

        DO
        BEGIN
             if (TTC IS NULL) then TTC = 0;
             if (RGT IS null) then RGT = 0;
             SOLDE = :SOLDE + :TTC - :RGT ;
             if (TTC = 0) then TTC = NULL;
             if (RGT = 0) then RGT = NULL;
             if (QTE = 0) then QTE = NULL;
             if (FRX = 0) then FRX = NULL;
             if (PU = 0) then PU = NULL;

  SUSPEND;
  END
END
^


ALTER PROCEDURE RENUM_CHAUFFEURS_ID_RENUM_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(RENUM_CHAUFFEURS_ID_RENUM_GEN, 1);
END
^


ALTER PROCEDURE RORATION
RETURNS (
    ID_CLIENT INTEGER,
    DENOMINATION VARCHAR (128),
    DTE DATE,
    NBRE INTEGER,
    CAC NUMERIC (18, 2),
    CAV NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    P100CA NUMERIC (18, 2),
    CAT SMALLINT)
AS DECLARE variable tca NUMERIC(18,2);
DECLARE variable tav NUMERIC(18,2);
DECLARE variable A1 INTEGER;
DECLARE variable A2 INTEGER;
DECLARE variable M1 INTEGER;
DECLARE variable M2 INTEGER;
BEGIN
     SELECT
           SUM(entete_sorties.total_ht)
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie in(1,2)
     INTO :tca;

     SELECT
           SUM(entete_sorties.total_ht)
     FROM entete_sorties
     WHERE entete_sorties.nature_sortie =4
     INTO :tav;
     if (:tca IS NULL) then tca = 0;
     if (:tav IS NULL) then tav = 0;
     tca = :tca - tav;
     FOR
        SELECT distinct
               entete_sorties.id_client,
               entete_sorties.denomination,
               MAX(entete_sorties.date_piece),
               count(entete_sorties.num_piece),
               sum(CASE
                       WHEN entete_sorties.nature_sortie in(1,2) THEN entete_sorties.total_ht
                    END),
               sum(CASE
                       WHEN entete_sorties.nature_sortie =4 THEN entete_sorties.total_ht
                    END)
        FROM entete_sorties
        WHERE entete_sorties.nature_sortie in(1,2)
        GROUP BY entete_sorties.id_client,
              entete_sorties.denomination
        ORDER BY 3 desc
        INTO :id_client, :denomination, :DTE, :nbre, :CAC, :CAV
        do
        BEGIN
             A1 = EXTRACT(YEAR FROM CURRENT_DATE);
             M1 = EXTRACT(MONTH FROM CURRENT_DATE);
             A2 = EXTRACT(YEAR FROM :DTE);
             M2 = EXTRACT(MONTH FROM :DTE);

             if ((A1 > A2)OR((A1 = A2)AND(M1 > M2+2))) then
                CAT = 0;
             if ((A1 = A2)AND(M1 <= M2+2)) then
                CAT = 1;
             if ((A1 = A2)AND(M1 = M2)) then
                cAT = 2;
             if (:CAC IS NULL) then CAC = 0;
             if (:CAV IS NULL) then CAV = 0;
             ca = :CAC - :CAV;
             P100CA = CAST((:CA / :TCA)*100 AS numeric(18,2));
  SUSPEND;
  END
END
^


ALTER PROCEDURE SOLDE_CLIENT (
    ID_CLIENT INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_INI NUMERIC (18, 2);
  DECLARE variable CREANCE NUMERIC (18, 2);
  DECLARE variable REGLEMENT NUMERIC (18, 2);
BEGIN
     EXECUTE PROCEDURE SOLDE_INI_TIERS(:ID_CLIENT) RETURNING_VALUES :SOLDE_INI;
     EXECUTE PROCEDURE total_ventes_client(:ID_CLIENT,:D1,:D2) RETURNING_VALUES :CREANCE;
     EXECUTE PROCEDURE total_reglt_client(:ID_CLIENT,:D1,:D2) RETURNING_VALUES :REGLEMENT;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
     if (CREANCE IS NULL) then CREANCE = 0;
     if (REGLEMENT IS NULL) then REGLEMENT = 0;
     SOLDE = :SOLDE_INI + :CREANCE - :REGLEMENT;
  SUSPEND;
END
^


ALTER PROCEDURE SOLDE_CLIENT_BON (
    ID_ENT_SORT INTEGER,
    ID_CLIENT INTEGER,
    D TIMESTAMP)
RETURNS (
    SOLDE_ANT NUMERIC (18, 2),
    VERSEMENT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_INI NUMERIC (18, 2);
  DECLARE variable CREANCE NUMERIC (18, 2);
  DECLARE variable REGLEMENT NUMERIC (18, 2);
  DECLARE variable MNT_BON NUMERIC (18, 2);
BEGIN
     SELECT
           regl_tiers.montant
     FROM regl_tiers
     WHERE regl_tiers.id_entet_piece = :id_ent_sort
     INTO :VERSEMENT;

     SELECT
           entete_sorties.total_facture
     FROM entete_sorties
     WHERE entete_sorties.id_ent_sort = :ID_ENT_SORT
     INTO :MNT_BON;

     EXECUTE PROCEDURE SOLDE_INI_TIERS(:ID_CLIENT) RETURNING_VALUES :SOLDE_INI;
     EXECUTE PROCEDURE TOTAL_VENTES_CLIENT_BON(:ID_CLIENT,:D) RETURNING_VALUES :CREANCE;
     EXECUTE PROCEDURE TOTAL_REGLT_CLIENT_BON(:ID_ENT_SORT) RETURNING_VALUES :REGLEMENT;
     if (VERSEMENT IS NULL) then VERSEMENT = 0;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
     if (CREANCE IS NULL) then CREANCE = 0;
     if (REGLEMENT IS NULL) then REGLEMENT = 0;
     SOLDE_ANT = :SOLDE_INI + :CREANCE - :REGLEMENT;
     SOLDE = :SOLDE_ANT + :MNT_BON - :VERSEMENT;
  SUSPEND;
END
^


ALTER PROCEDURE SOLDE_CLIENT_REGLT (
    ID_RGLT INTEGER,
    ID_CLIENT INTEGER,
    D TIMESTAMP,
    MNT_RGLT NUMERIC (18, 2))
RETURNS (
    SOLDE_ANT NUMERIC (18, 2),
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_INI NUMERIC (18, 2);
  DECLARE variable CREANCE NUMERIC (18, 2);
  DECLARE variable REGLEMENT NUMERIC (18, 2);
BEGIN
     EXECUTE PROCEDURE SOLDE_INI_TIERS(:ID_CLIENT) RETURNING_VALUES :SOLDE_INI;
     EXECUTE PROCEDURE TOTAL_VENTES_CLIENT_RGLT(:ID_CLIENT,:D) RETURNING_VALUES :CREANCE;
     EXECUTE PROCEDURE TOTAL_REGLT_CLIENT_RGLT(:ID_CLIENT,:D, :ID_RGLT) RETURNING_VALUES :REGLEMENT;
     if (MNT_RGLT IS NULL) then MNT_RGLT = 0;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
     if (CREANCE IS NULL) then CREANCE = 0;
     if (REGLEMENT IS NULL) then REGLEMENT = 0;
     SOLDE_ANT = :SOLDE_INI + :CREANCE - :REGLEMENT;
     SOLDE = :SOLDE_ANT - :MNT_RGLT;
  SUSPEND;
END
^


ALTER PROCEDURE SOLDE_CLIENT_SAUF_CETTE (
    ID_CLIENT INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    SOLDE NUMERIC (18, 2))
AS DECLARE variable SOLDE_INI NUMERIC (18, 2);
  DECLARE variable CREANCE NUMERIC (18, 2);
  DECLARE variable REGLEMENT NUMERIC (18, 2);
BEGIN
     EXECUTE PROCEDURE SOLDE_INI_TIERS(:ID_CLIENT) RETURNING_VALUES :SOLDE_INI;
     EXECUTE PROCEDURE TL_VTES_CLIENT_SF_CTE_PCE(:ID_CLIENT,:ID_PIECE, :D1,:D2) RETURNING_VALUES :CREANCE;
     EXECUTE PROCEDURE TL_RGLT_CLIENT_SF_CTE_PCE(:ID_CLIENT,:ID_PIECE, :D1,:D2) RETURNING_VALUES :REGLEMENT;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
     if (CREANCE IS NULL) then CREANCE = 0;
     if (REGLEMENT IS NULL) then REGLEMENT = 0;
     SOLDE = :SOLDE_INI + :CREANCE - :REGLEMENT;
  SUSPEND;
END
^


ALTER PROCEDURE SOLDE_INI_TIERS (
    ID INTEGER)
RETURNS (
    SOLDE_INI NUMERIC (18, 2))
AS BEGIN
     SELECT
           SOLDE_INI
     FROM CLIENTS
     WHERE ID_CLIENT = :id
     INTO :SOLDE_INI;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
  SUSPEND;
END
^


ALTER PROCEDURE SORTIES_ID_SORTIE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(SORTIES_ID_SORTIE_GEN, 1);
END
^


ALTER PROCEDURE STOCK_FAMILLE (
    D DATE)
RETURNS (
    FAMILLE VARCHAR (64),
    STOCK NUMERIC (18, 6),
    VALEUR NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
              INVENTAIRES.famille,
              SUM(INVENTAIRES.stock),
              SUM(INVENTAIRES.valeur)
        FROM INVENTAIRES(:D)
        GROUP BY INVENTAIRES.famille
        INTO :FAMILLE, :STOCK, :VALEUR

DO
  BEGIN

  SUSPEND;
  END
END
^


ALTER PROCEDURE STOCK_NOMENCLATURE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    QTE_DECHET NUMERIC (18, 6),
    TX_DECHET NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2),
    NATURE SMALLINT)
AS BEGIN
     FOR
     SELECT
           nomenclatures.id_matiere,
           nomenclatures.mat_emb,
           nomenclatures.qte,
           nomenclatures.qte_dechet,
           nomenclatures.tx_dechet,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     left JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_art = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     INTO :ID_MAT,:NATURE, :QTE, :QTE_DECHET, :TX_DECHET, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE STOCK_NOMENCLATURE_EMBALLAGE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2))
AS BEGIN
     FOR
     SELECT
           nomenclatures.id_matiere,
           nomenclatures.qte,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     LEFT JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_art = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     AND nomenclatures.mat_emb = 2
     INTO :ID_MAT,:QTE, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE STOCK_NOMENCLATURE_MATIERE (
    ID INTEGER,
    D DATE,
    ID_MAGAZIN INTEGER)
RETURNS (
    ID_MAT INTEGER,
    QTE NUMERIC (18, 6),
    STOCK NUMERIC (18, 6),
    P_PAMP NUMERIC (18, 2))
AS BEGIN
FOR
     SELECT
           nomenclatures.id_matiere,
           nomenclatures.qte,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     LEFT JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_art = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     AND nomenclatures.mat_emb = 1
     INTO :ID_MAT,:QTE, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END
^


ALTER PROCEDURE STOCK_NOT_NORMAL (
    D DATE)
RETURNS (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    ST_INI NUMERIC (18, 6),
    VAL_INI NUMERIC (18, 2),
    ST_ENT NUMERIC (18, 6),
    VAL_ENT NUMERIC (18, 2),
    ST_SRT NUMERIC (18, 6),
    VAL_SRT NUMERIC (18, 2),
    STOCK NUMERIC (18, 6),
    COUT NUMERIC (18, 2),
    VALEUR NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT DISTINCT
              mouvements.ID_ARTICLE,
              mouvements.id_magazin,
              ARTICLES.ref,
              ARTICLES.designation,
              sum(MOUVEMENTS.QTE_INI),
              sum(MOUVEMENTS.QTE_ENT),
              sum(MOUVEMENTS.QTE_SRT),
              SUM(mouvements.val_INI),
              SUM(mouvements.val_ent),
              sum(mouvements.val_srt)
        FROM mouvements
        LEFT OUTER JOIN ARTICLES ON ARTICLES.ID_ART = MOUVEMENTS.id_article
        WHERE ID_ARTICLE IS NOT NULL
        AND mouvements.date_oper <= :D
        GROUP BY mouvements.ID_ARTICLE,
        mouvements.id_magazin,
              ARTICLES.ref,
              ARTICLES.designation

          ORDER BY 3
        INTO :ID_ART, :ID_MAG, :REF, :DESIGNATION,
             :ST_INI, :ST_ENT, :ST_SRT, :VAL_INI, :VAL_ENT, :VAL_SRT  DO
  BEGIN
       if (ST_INI IS NULL) then ST_INI = 0;
       if (ST_ENT IS NULL) then ST_ENT = 0;
       if (ST_SRT IS NULL) then ST_SRT = 0;
       if (VAL_INI IS NULL) then VAL_INI = 0;
       if (VAL_ENT IS NULL) then VAL_ENT = 0;
       if (VAL_SRT IS NULL) then VAL_SRT = 0;

       STOCK = :ST_INI + :ST_ENT - :ST_SRT;

       VALEUR = :VAL_INI + :VAL_ENT - :VAL_SRT;

       if (STOCK <> 0) then
          COUT = :VALEUR / :STOCK;
       ELSE
           COUT = 0;

  SUSPEND;
  END
END
^


ALTER PROCEDURE TAUX_EXTRACTION_ID_TAUX_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(TAUX_EXTRACTION_ID_TAUX_GEN, 1);
END
^


ALTER PROCEDURE TL_RGLT_CLIENT_SF_CTE_PCE (
    ID INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
     AND id_tier <> -1
     AND regl_tiers.date_regt BETWEEN :D1 AND :D2
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TL_VTES_CLIENT_SF_CTE_PCE (
    ID INTEGER,
    ID_PIECE INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.id_ent_sort <> :ID_PIECE
        AND ENTETE_SORTIES.nature_sortie <> 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_VENTE;

        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie = 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_AVOIR;

        if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
        if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_ACHAT_LOCAL (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    DETTES NUMERIC (18, 2))
AS DECLARE variable TOTAL_ACHAT NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
     SELECT
           sum(entete_entrees.net_a_payer)
     FROM entete_entrees
     WHERE entete_entrees.id_fourn = :id
     AND (nature_entree = 2)
     AND entete_entrees.date_fact BETWEEN :D1 AND :D2
     AND entete_entrees.ajouter_dettes = 1
     INTO :TOTAL_ACHAT;

     SELECT
           sum(entete_entrees.net_a_payer)
     FROM entete_entrees
     WHERE entete_entrees.id_fourn = :id
     AND (nature_entree = 4)
     AND entete_entrees.date_fact BETWEEN :D1 AND :D2
     AND entete_entrees.ajouter_dettes = 1
     INTO :TOTAL_AVOIR;

     if (TOTAL_ACHAT IS NULL) then TOTAL_ACHAT = 0;
     if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
     DETTES = :TOTAL_ACHAT - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_ACHAT_LOCAL_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    DETTES NUMERIC (18, 2))
AS DECLARE variable TOTAL_ACHAT NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
     SELECT
           sum(entete_entrees.net_a_payer)
     FROM entete_entrees
     WHERE entete_entrees.id_fourn = :id
     AND (nature_entree <> 4)
     AND entete_entrees.ajouter_dettes = 1
     AND entete_entrees.date_fact < :D1
     INTO :TOTAL_ACHAT ;

     SELECT
           sum(entete_entrees.net_a_payer)
     FROM entete_entrees
     WHERE entete_entrees.id_fourn = :id
     AND (nature_entree = 4)
     AND entete_entrees.ajouter_dettes = 1
     AND entete_entrees.date_fact < :D1
     INTO :TOTAL_AVOIR;

     if (TOTAL_ACHAT IS NULL) then TOTAL_ACHAT = 0;
     if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
     DETTES = :TOTAL_ACHAT - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_DEPENSES_CPTE (
    ID INTEGER)
RETURNS (
    DEPENSE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(DEPENSES.montant) AS NUMERIC(18,2))
     FROM DEPENSES
     WHERE DEPENSES.id_cpte = :ID
     INTO :DEPENSE;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_RECETTES_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(RECETTES.montant) AS NUMERIC(18,2))
     FROM RECETTES
     WHERE RECETTES.id_cpte = :ID
     INTO :RECETTE;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
     AND id_tier <> -1
     AND regl_tiers.date_regt BETWEEN :D1 AND :D2
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_CLIENT_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
     AND id_tier <> -1
     AND regl_tiers.date_regt < :D1
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_CLIENT_BON (
    ID_ENTET_PIECE INTEGER)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = (SELECT entete_sorties.id_client FROM entete_sorties WHERE entete_sorties.id_ent_sort = :ID_ENTET_PIECE)
     AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
     AND ((regl_tiers.id_entet_piece <> :id_entet_piece)OR(regl_tiers.id_entet_piece IS null))
     AND regl_tiers.datetime_piece < (SELECT entete_sorties.datetime_piece FROM entete_sorties WHERE entete_sorties.id_ent_sort = :ID_ENTET_PIECE)
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_CLIENT_RGLT (
    ID INTEGER,
    D TIMESTAMP,
    ID_REGL INTEGER)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 1)OR(regl_tiers.type_regt = 2))
     AND id_tier <> -1
     AND regl_tiers.datetime_piece < :D
     AND regl_tiers.id_regl <> :id_regl
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 3))
     AND id_tier <> -1
     AND regl_tiers.date_regt BETWEEN :D1 AND :D2
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    TOTAL_REGL NUMERIC (18, 2))
AS BEGIN
     SELECT
           sum(regl_tiers.montant)
     FROM regl_tiers
     WHERE regl_tiers.id_tier = :id
     AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 0))
     AND id_tier <> -1
     AND regl_tiers.date_regt < :D1
     INTO :TOTAL_REGL;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_RECETTE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2))
     FROM regl_tiers
     WHERE regl_tiers.id_cpte = :ID
     AND regl_tiers.type_regt = 1
     INTO :RECETTE;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_REGLT_REPENSE_CPTE (
    ID INTEGER)
RETURNS (
    DEPENSE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(regl_tiers.montant) AS NUMERIC(18,2))
     FROM regl_tiers
     WHERE regl_tiers.id_cpte = :ID
     AND regl_tiers.type_regt = 0
     INTO :DEPENSE;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_SOLDE_INI_CPTE (
    ID INTEGER)
RETURNS (
    SOLDE_INI NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(comptes_tresorie.solde_ini AS NUMERIC(18,2))
     FROM comptes_tresorie
     WHERE comptes_tresorie.id_cpte = :ID
     INTO :SOLDE_INI;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VENTES_CLIENT (
    ID INTEGER,
    D1 DATE,
    D2 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie <> 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_VENTE;

        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie = 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_AVOIR;

        if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
        if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VENTES_CLIENT_ANT (
    ID INTEGER,
    D1 DATE)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie <> 5
        AND entete_sorties.date_piece < :D1
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_VENTE;

        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie = 5
        AND entete_sorties.date_piece < :D1
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_AVOIR;

        if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
        if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VENTES_CLIENT_BON (
    ID INTEGER,
    D TIMESTAMP)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie <> 5
        AND ENTETE_SORTIES.nature_sortie <> 7
        AND entete_sorties.datetime_piece < :D
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_VENTE;

        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
        AND entete_sorties.datetime_piece < :D
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_AVOIR;

        if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
        if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VENTES_CLIENT_RGLT (
    ID INTEGER,
    D TIMESTAMP)
RETURNS (
    CREANCE NUMERIC (18, 2))
AS DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
BEGIN
        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ENTETE_SORTIES.nature_sortie <> 5
        AND ENTETE_SORTIES.nature_sortie <> 7
        AND entete_sorties.datetime_piece < :D
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_VENTE;

        SELECT
              sum(entete_sorties.total_facture)
        FROM entete_sorties
        WHERE id_client = :id
        AND ((ENTETE_SORTIES.nature_sortie = 5)OR(ENTETE_SORTIES.nature_sortie = 7))
        AND entete_sorties.datetime_piece < :D
        AND entete_sorties.ajouter_creance = 1
        INTO :TOTAL_AVOIR;

        if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
        if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VIR_FOND_DEPENSE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(vir_fonds.montant) AS NUMERIC(18,2))
     FROM vir_fonds
     WHERE vir_fonds.id_compte_credit = :ID
     INTO :RECETTE;
  SUSPEND;
END
^


ALTER PROCEDURE TOTAL_VIR_FOND_RECETTE_CPTE (
    ID INTEGER)
RETURNS (
    RECETTE NUMERIC (18, 2))
AS BEGIN
     SELECT
           CAST(SUM(vir_fonds.montant) AS NUMERIC(18,2))
     FROM vir_fonds
     WHERE vir_fonds.id_compte_debit = :ID
     INTO :RECETTE;
  SUSPEND;
END
^


ALTER PROCEDURE TVA_PERIODE (
    M1 SMALLINT,
    M2 SMALLINT,
    A SMALLINT)
RETURNS (
    CODE VARCHAR (12),
    LIBELLE VARCHAR (128),
    CA INTEGER,
    IMP INTEGER,
    EXO INTEGER,
    TVA INTEGER,
    TAUX NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              code_tva.code,
              code_tva.libelles,
              sum(case when (sorties.taux_tva <> 0) then sorties.ht END) AS imp,
              sum(case when (sorties.taux_tva = 0)  then sorties.ht END) AS exo,
              SUM(sorties.tva) AS tva,
              code_tva.taux
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        inner join code_tva on code_tva.id_tva = articles.id_code_tva
        WHERE (extract(month FROM sorties.date_piece)) BETWEEN :M1 AND :M2
        AND   extract(year  FROM sorties.date_piece) = :A
        AND SORTIES.nature_sortie = 1
        AND SORTIES.cession = 0
        GROUP BY code_tva.libelles, articles.id_code_tva, code_tva.code, code_tva.taux
        ORDER BY code
        INTO :CODE, :LIBELLE, :IMP, :EXO, :TVA, :TAUX
     DO
     BEGIN
     if (IMP IS NULL) then IMP = 0;
     if (EXO IS NULL) then EXO = 0;
     IMP = IMP / 10;
     IMP = IMP * 10;
     CA = IMP + EXO;
     SUSPEND;
     END
END
^


ALTER PROCEDURE TYPE_AUTRES_ENTREES_ID_TYPE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(TYPE_AUTRES_ENTREES_ID_TYPE_GEN, 1);
END
^


ALTER PROCEDURE TYPE_AUTRES_SORTIES_ID_TYPE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(TYPE_AUTRES_SORTIES_ID_TYPE_GEN, 1);
END
^


ALTER PROCEDURE T_M_P_V (
    ID INTEGER)
RETURNS (
    T NUMERIC (18, 2),
    M NUMERIC (18, 2),
    P NUMERIC (18, 2),
    V NUMERIC (18, 2))
AS BEGIN
     SELECT
           ARTICLES.taux_m1,
           ARTICLES.marge1,
           ARTICLES.prix_v1,
           ARTICLES.taux_tva
     FROM ARTICLES
     WHERE ARTICLES.id_art = : ID
     INTO :T, :M, :P, :V;
  SUSPEND;
END
^


ALTER PROCEDURE UNITES_ID_UNITE_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(UNITES_ID_UNITE_GEN, 1);
END
^


ALTER PROCEDURE UNITE_ID_ART (
    ID INTEGER)
RETURNS (
    UNITE VARCHAR (64))
AS BEGIN
     SELECT
           UNITES.unite
     FROM UNITES
     INNER JOIN ARTICLES ON ARTICLES.id_unite = UNITES.id_unite
     WHERE ARTICLES.id_art = :ID
     INTO :UNITE;
  SUSPEND;
END
^


ALTER PROCEDURE UPDATE_PAMP
AS BEGIN

UPDATE LIGNES_TRANSFERT SET LIGNES_TRANSFERT.PAMP =(SELECT pamp_depot.p_pamp
           FROM pamp_depot(LIGNES_TRANSFERT.id_article,LIGNES_TRANSFERT.ID_MAGAZIN_1,LIGNES_TRANSFERT.DATE_PIECE));

UPDATE LIGNES_TRANSFERT SET LIGNES_TRANSFERT.ht = LIGNES_TRANSFERT.QUANTITE * LIGNES_TRANSFERT.pamp;

update ENTETE_TRANSFERT set ENTETE_TRANSFERT.TOTAL_HT =
(SELECT sum(LIGNES_TRANSFERT.HT) FROM LIGNES_TRANSFERT WHERE LIGNES_TRANSFERt.id_entete = ENTETE_TRANSFERT.ID_ENTETE);


UPDATE entrees SET ENTREES.autres_charges = 0 WHERE entrees.nature_entree = 3 and ENTREES.autres_charges is null;

UPDATE sorties SET sorties.cr_u =(SELECT pamp_depot.p_pamp FROM pamp_depot(sorties.id_article,sorties.id_magazin,sorties.date_piece)) WHERE sorties.nature_sortie = 4;

UPDATE sorties SET sorties.consomation = sorties.quantite * cr_u    WHERE sorties.nature_sortie = 4;

UPDATE sorties SET sorties.ht = sorties.consomation  WHERE sorties.nature_sortie = 4;

UPDATE entete_sorties SET entete_sorties.total_consommation = (SELECT sum(sorties.consomation) FROM sorties WHERE sorties.id_entete = entete_sorties.id_ent_sort)
WHERE entete_sorties.nature_sortie = 4;

UPDATE entrees SET entrees.HT_AVANT_REMISE =
  (SELECT sum(sorties.consomation) FROM sorties WHERE sorties.id_fp = entrees.id_entete)
  WHERE entrees.nature_entree = 3;

UPDATE entrees SET entrees.HT = entrees.HT_AVANT_REMISE WHERE entrees.nature_entree = 3;

UPDATE entrees SET cout_dinars = entrees.ht + ENTREES.autres_charges WHERE entrees.nature_entree = 3;

 UPDATE entrees SET tva = ht * taux_tva / 100 WHERE entrees.nature_entree = 3;
  UPDATE entrees SET ttc = ht + tva WHERE entrees.nature_entree = 3;
  UPDATE entrees SET entrees.cout_achat = entrees.cout_dinars / entrees.quantite
  WHERE entrees.nature_entree = 3;

UPDATE entrees SET entrees.P_U_DA_AV_REM = entrees.cout_achat  WHERE entrees.nature_entree = 3;

UPDATE entrees SET entrees.P_U_DA = entrees.cout_achat  WHERE entrees.nature_entree = 3;

  UPDATE sorties SET cr_u =(SELECT pamp_depot.p_pamp
           FROM pamp_depot(sorties.id_article,sorties.id_magazin,sorties.date_piece))
  WHERE ((sorties.nature_sortie = 1) OR (sorties.nature_sortie = 2)OR (sorties.nature_sortie = 10));

  UPDATE sorties SET sorties.consomation = sorties.quantite * cr_u WHERE ((sorties.nature_sortie = 1) OR (sorties.nature_sortie = 2)OR (sorties.nature_sortie = 10));

  UPDATE entete_sorties SET entete_sorties.total_consommation =
(SELECT sum(sorties.consomation) FROM sorties WHERE sorties.id_entete = entete_sorties.id_ent_sort)
WHERE ((entete_sorties.nature_sortie = 1) OR (entete_sorties.nature_sortie = 2)OR (entete_sorties.nature_sortie = 10));

  SUSPEND;
END
^


ALTER PROCEDURE UPT_ENT_SRT_TL_CONS
AS BEGIN
     UPDATE entete_sorties
     SET entete_sorties.total_consommation = (SELECT SUM(SORTIES.consomation)
                                         FROM SORTIES
                                         WHERE SORTIES.id_entete = entete_sorties.id_ent_sort);
  SUSPEND;
END
^


ALTER PROCEDURE VENTES_MENSUEL_MR
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    ID_REGLEMENT INTEGER,
    MODE_REGLEMENT VARCHAR (64),
    HT NUMERIC (18, 2),
    AVOIRS NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    MARGE NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TTC NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    REMISE NUMERIC (18, 2),
    VENTES NUMERIC (18, 2))
AS DECLARE variable CO_AV NUMERIC(18,2);
  DECLARE variable CONSO NUMERIC(18,2);

  DECLARE variable TVA_AV NUMERIC(18,2);
  DECLARE variable TIMBRE_AV NUMERIC(18,2);
  DECLARE variable TTC_AV NUMERIC(18,2);
  DECLARE variable VENTES_AV NUMERIC(18,2);
  DECLARE variable REMISE_AV NUMERIC(18,2);
BEGIN
     FOR
        SELECT DISTINCT
              EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece),
              entete_sorties.id_reglement
        FROM ENTETE_SORTIES
        INTO :CODE_MOIS, :ID_REGLEMENT
    DO
    BEGIN
         HT = 0;
         AVOIRS = 0;
         CONSOMMATIONS = 0;
         MARGE = 0;
         TVA = 0;
         TTC = 0;
         TIMBRE = 0;
         REMISE = 0;
         VENTES = 0;
         CO_AV = 0;
         CONSO = 0;
         TVA_AV = 0;
         TIMBRE_AV = 0;
         TTC_AV = 0;
         VENTES_AV = 0;
         REMISE_AV = 0;

         SELECT
               reglements.libelle
         FROM reglements
         WHERE reglements.id_reglement = :ID_REGLEMENT
         INTO :MODE_REGLEMENT;

         SELECT
               periodes.mois
         FROM periodes
         WHERE periodes.code = :CODE_MOIS
         INTO :mois;

         SELECT
               SUM(entete_sorties.total_ht)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :HT;
         if (HT IS NULL) then HT = 0;

         SELECT
               SUM(entete_sorties.total_HT)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :AVOIRS;
         if (AVOIRS IS NULL) then AVOIRS = 0;

         SELECT
               SUM(entete_sorties.total_consommation)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :CONSO;
         if (CONSO IS NULL) then CONSO = 0;

         SELECT
               SUM(entete_sorties.total_consommation)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :CO_AV;
         if (CO_AV IS NULL) then CO_AV = 0;
         CONSOMMATIONS = :CONSO - :CO_AV;
         MARGE = :HT - :AVOIRS - :CONSOMMATIONS;

         SELECT
               SUM(entete_sorties.total_TVA)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TVA;
         if (TVA IS NULL) then TVA = 0;

         SELECT
               SUM(entete_sorties.total_ttc)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TTC;
         if (TTC IS NULL) then TTC = 0;

         SELECT
               SUM(entete_sorties.TIMBRE)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TIMBRE;
         if (TIMBRE IS NULL) then TIMBRE = 0;

         SELECT
               SUM(entete_sorties.total_facture)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :VENTES;
         if (VENTES IS NULL) then VENTES = 0;

         SELECT
               SUM(entete_sorties.total_TVA)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TVA_AV;
         if (TVA_AV IS NULL) then TVA_AV = 0;

         SELECT
               SUM(entete_sorties.total_ttc)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TTC_AV;
         if (TTC_AV IS NULL) then TTC_AV = 0;

         SELECT
               SUM(entete_sorties.TIMBRE)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :TIMBRE_AV;
         if (TIMBRE_AV IS NULL) then TIMBRE_AV = 0;

         SELECT
               SUM(entete_sorties.total_facture)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :VENTES_AV;
         if (VENTES_AV IS NULL) then VENTES_AV = 0;

         SELECT
               SUM(entete_sorties.remise)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie <> 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :REMISE;
         if (REMISE IS NULL) then REMISE = 0;

         SELECT
               SUM(entete_sorties.remise)
         FROM entete_sorties
         WHERE entete_sorties.id_reglement = :ID_REGLEMENT
         AND entete_sorties.nature_sortie = 4
         AND EXTRACT(MONTH FROM ENTETE_SORTIES.date_piece) = :CODE_MOIS
         INTO :REMISE_AV;
         if (REMISE_AV IS NULL) then REMISE_AV = 0;

         VENTES = :VENTES - :VENTES_AV;
         TVA = :TVA - :TVA_AV;
         TIMBRE = :TIMBRE - :TIMBRE_AV;
         TTC = :TTC - :TTC_AV;
         REMISE = :REMISE - :REMISE_AV;

  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTES_MENSUEL_PRODUIT (
    ID_ART INTEGER)
RETURNS (
    CODE_MOIS SMALLINT,
    MOIS VARCHAR (20),
    VAL_STK_INI NUMERIC (18, 2),
    ACHAT_IMPO NUMERIC (18, 2),
    ACHAT_LOC NUMERIC (18, 2),
    AVOIR_ACHAT NUMERIC (18, 2),
    CONSOMMATIONS NUMERIC (18, 2),
    AVOIR_VENTE NUMERIC (18, 2),
    STOCK_FINAL NUMERIC (18, 2),
    TOTAL_VENTE NUMERIC (18, 2),
    CA NUMERIC (18, 2),
    MARGE NUMERIC (18, 2))
AS BEGIN
     SELECT
           SUM(inventaire.valeur)
     FROM inventaire
     WHERE inventaire.id_article = :ID_ART
     INTO :VAL_STK_INI;
     if (VAL_STK_INI IS NULL) then VAL_STK_INI = 0;
     STOCK_FINAL = :VAL_STK_INI;

     FOR
        SELECT distinct
               CODE,
               MOIS
        FROM PERIODES
        INTO :CODE_MOIS, :MOIS
     DO
     BEGIN
         VAL_STK_INI = :STOCK_FINAL;

         SELECT
               SUM(entrees.valeur_stock)
         FROM entrees
         WHERE extract(MONTH FROM entrees.date_entree) = :CODE_MOIS
         AND entrees.nature_entree = 1
         AND ENTREES.id_article =:ID_ART
         INTO :ACHAT_IMPO;
         if (ACHAT_IMPO IS NULL) then ACHAT_IMPO = 0;

         SELECT
               SUM(entrees.HT)
         FROM entrees
         WHERE extract(MONTH FROM entrees.date_entree) = :CODE_MOIS
         AND entrees.nature_entree = 2
         AND ENTREES.id_article =:ID_ART
         INTO :ACHAT_LOC;
         if (ACHAT_LOC IS NULL) then ACHAT_LOC = 0;

         SELECT
               SUM(entrees.valeur_stock)
         FROM entrees
         WHERE extract(MONTH FROM entrees.date_entree) = :CODE_MOIS
         AND entrees.nature_entree = 4
         AND ENTREES.id_article =:ID_ART
         INTO :AVOIR_ACHAT;
         if (AVOIR_ACHAT IS NULL) then AVOIR_ACHAT = 0;

         SELECT
               SUM(SORTIES.ht),
               SUM(SORTIES.consomation)
         FROM SORTIES
         WHERE extract(MONTH FROM SORTIES.date_piece) = :CODE_MOIS
         AND ((SORTIES.nature_sortie = 1) OR (SORTIES.nature_sortie = 2))
         AND SORTIES.id_article = :ID_ART
         INTO :TOTAL_VENTE, :CONSOMMATIONS;
         if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
         if (CONSOMMATIONS IS NULL) then CONSOMMATIONS = 0;

         SELECT
               SUM(SORTIES.HT)
         FROM SORTIES
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND SORTIES.nature_sortie = 5
         AND SORTIES.id_article = :ID_ART
         INTO :AVOIR_VENTE;
         if (AVOIR_VENTE IS NULL) then AVOIR_VENTE = 0;

         CONSOMMATIONS = :CONSOMMATIONS - :AVOIR_VENTE;

         STOCK_FINAL = :VAL_STK_INI + :ACHAT_IMPO + :ACHAT_LOC - AVOIR_ACHAT -
                       :CONSOMMATIONS + :AVOIR_VENTE;
         CA = :TOTAL_VENTE - :AVOIR_VENTE;
         MARGE = :CA - :CONSOMMATIONS;

  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTES_WILAYA
RETURNS (
    NBRE INTEGER,
    WILAYA VARCHAR (32),
    MONTANT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               count(clients.id_client),
               clients.wilaya,
               sum(entete_sorties.total_facture) AS total
        FROM clients
        inner join entete_sorties on entete_sorties.id_client = clients.id_client
        GROUP BY clients.wilaya
        INTO :NBRE, :WILAYA, :MONTANT
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTES_WILAYA_PRODUITS
RETURNS (
    ID_ART INTEGER,
    WILAYA VARCHAR (32),
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    MONTANT NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               clients.wilaya,
               SORTIES.id_article,
               SUM(sorties.quantite) AS QTE
        FROM sorties
        inner join clients on clients.id_client = clients.id_client
        GROUP BY clients.wilaya,
                 SORTIES.id_article

        INTO :WILAYA, :ID_ART, :MONTANT
     do
     BEGIN
          SELECT
                articles.ref,
                ARTICLES.designation
          FROM articles
          WHERE ARTICLES.id_art = :ID_ART
          INTO :REF, :DESIGNATION;
  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTE_CLIENT_PRODUITS (
    ID INTEGER,
    DU DATE,
    AU DATE)
RETURNS (
    ID_ART INTEGER,
    DATE_PIECE DATE,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    PU NUMERIC (18, 2),
    QTE NUMERIC (18, 6))
AS BEGIN
     FOR
        SELECT DISTINCT
               SORTIES.id_article,
               sorties.date_piece,
               articles.ref,
               articles.designation,
               sorties.prix_unitaire,
               sorties.quantite
        FROM sorties
        inner join articles on articles.id_art = sorties.id_article
        WHERE sorties.id_client = :id
        AND sorties.date_piece between :du AND :au
        INTO :ID_ART, :date_piece, :ref, :designation, :pu, :qte
     do
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTE_FAM_PRODUIT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_FAMILLE INTEGER,
    FAMILLE VARCHAR (128),
    UNITE VARCHAR (64),
    QTE NUMERIC (18, 2),
    PRIX NUMERIC (18, 2),
    MONTANT NUMERIC (18, 2),
    CONS NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              SORTIES.id_famille,
              familles.famille,
              UNITES.unite,
              SORTIES.prix_unitaire,
              SUM(SORTIES.quantite),
              SUM(sorties.ht),
              SUM(sorties.consomation)
        FROM SORTIES
        INNER JOIN familles ON familles.id_famille = SORTIES.id_famille
        INNER JOIN UNITES ON UNITES.id_unite =SORTIES.id_unite
        WHERE SORTIES.date_piece BETWEEN :D1 AND :D2
        GROUP BY SORTIES.id_famille, familles.famille, UNITES.unite, SORTIES.prix_unitaire
        INTO :ID_FAMILLE, :FAMILLE, :UNITE, :PRIX, :QTE, :MONTANT, :cons
     DO
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTE_JOUR (
    D DATE)
RETURNS (
    NBRE INTEGER,
    HT_S_TVA NUMERIC (18, 2),
    HT_TVA NUMERIC (18, 2),
    TVA NUMERIC (18, 2),
    TIMBRE NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS BEGIN
     SELECT
           COUNT(TOTAL_HT),
           SUM(TIMBRE),
           SUM(TOTAL_TVA)
     FROM entete_sorties
     WHERE entete_sorties.date_piece = :D
     INTO :NBRE, :TIMBRE, :TVA;

     SELECT distinct
            sum(ht)
     FROM SORTIES
     WHERE  SORTIES.date_piece = :D
     AND tva = 0
     INTO :HT_S_TVA;

     SELECT distinct
            sum(ht)
     FROM SORTIES
     WHERE  SORTIES.date_piece = :D
     AND tva <> 0
     INTO :HT_TVA;

     if (tva IS null) then tva = 0;
     if (ht_s_tva IS null) then ht_s_tva = 0;
     if (timbre IS null) then timbre = 0;
     if (ht_tva IS null) then ht_tva = 0;
     ttc = :ht_tva + :ht_s_tva + :tva + :timbre;

  SUSPEND;

END
^


ALTER PROCEDURE VENTE_PRODUIT (
    D1 DATE,
    D2 DATE)
RETURNS (
    ID_ART INTEGER,
    REF VARCHAR (20),
    DESIGNATION VARCHAR (128),
    UNITE VARCHAR (64),
    QTE NUMERIC (18, 2),
    PRIX NUMERIC (18, 2),
    MONTANT NUMERIC (18, 2),
    CONS NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT
              SORTIES.id_article,
ARTICLES.REF,
              ARTICLES.designation,
              UNITES.unite,
              SORTIES.prix_unitaire,
              SUM(SORTIES.quantite),
              SUM(sorties.ht),
              SUM(sorties.consomation)
        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        INNER JOIN UNITES ON unites.id_unite = ARTICLES.id_unite
        WHERE SORTIES.date_piece BETWEEN :D1 AND :D2
GROUP BY SORTIES.id_article, ARTICLES.ref, ARTICLES.designation, UNITES.unite, SORTIES.prix_unitaire
        INTO :ID_ART, :REF, :DESIGNATION, :UNITE, :PRIX, :QTE, :MONTANT, :CONS
     DO
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE VENTE_PROD_CLIENT (
    D1 DATE,
    D2 DATE,
    ID_CLIENT INTEGER)
RETURNS (
    NUM VARCHAR (12),
    DT DATE,
    ID_C INTEGER,
    NOM_CLIENT VARCHAR (64),
    ID_A INTEGER,
    NOM_PRODUIT VARCHAR (64),
    QUANTITE NUMERIC (18, 6),
    P_U NUMERIC (18, 2),
    TTC NUMERIC (18, 2))
AS BEGIN
     FOR
        SELECT distinct
               entete_sorties.num_piece,
               SORTIES.date_piece,
               SORTIES.id_client,
               clients.denomination,
               SORTIES.id_article,
               articles.designation,
               SORTIES.quantite,
               SORTIES.prix_unitaire,
               SORTIES.ttc
        FROM SORTIES
        INNER JOIN entete_sorties ON entete_sorties.id_ent_sort = SORTIES.id_entete
        inner join articles on articles.id_art = SORTIES.id_article
        inner join clients on clients.id_client = SORTIES.id_client
        WHERE SORTIES.date_piece between :D1 AND :D2
        AND SORTIES.nature_sortie = 2
        AND sorties.id_client = :ID_CLIENT
        ORDER BY 2,1
        INTO :NUM, :DT, :ID_C, :NOM_CLIENT, :ID_A, :NOM_PRODUIT, :QUANTITE, :P_U, :TTC
     DO
     BEGIN
  SUSPEND;
  END
END
^


ALTER PROCEDURE VERIFIER_STOCK (
    ID_ART INTEGER,
    ID_MAG INTEGER,
    DATE_OP DATE,
    OLD_QTE NUMERIC (18, 6),
    NEW_QTE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS DECLARE variable gestion_satock smallint;
DECLARE variable matiere smallint;
DECLARE variable STOCK NUMERIC (18, 6);
DECLARE variable INI NUMERIC(18,6) ;
DECLARE variable ENT NUMERIC(18,6) ;
DECLARE variable SRT NUMERIC(18,6) ;
BEGIN
     SELECT articles.gestion_stock,articles.matiere FROM articles WHERE articles.id_art = :id_art INTO :gestion_satock,:matiere;
     if ((gestion_satock = 1)AND(matiere <> 1)) then
     BEGIN
          SELECT
                sum(mouvements.qte_ini),
                sum(mouvements.qte_ent),
                sum(mouvements.qte_srt)
          FROM mouvements
          WHERE mouvements.date_oper <= :DATE_OP
          AND mouvements.id_article = :ID_ART
          AND mouvements.id_magazin = :ID_MAG
          AND mouvements.ref_entree = :REF_ENTREE
          INTO :INI,  :ENT, :SRT;
          if (INI IS NULL) then INI = 0;
          if (ENT IS NULL) then ENT = 0;
          if (SRT IS NULL) then SRT = 0;
          STOCK = :INI + :ENT - :SRT;

          if (new_qte > (STOCK + old_qte) ) then
             BEGIN
                  exception STOCK_INSUFISANT;
                  exit;
             END
     END
  SUSPEND;
END
^


ALTER PROCEDURE VERIF_FICHE_DE_STOCK_DEPOT (
    DU DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS DECLARE variable D DATE;
DECLARE variable SORTIE NUMERIC (18, 6);
DECLARE variable DATE_OPER DATE;
DECLARE variable ENTREE NUMERIC (18, 6);
DECLARE variable STOCK NUMERIC (18, 6);
DECLARE variable OPER VARCHAR (1);
DECLARE variable gestion_satock smallint;
DECLARE variable matiere smallint;
BEGIN
     ENTREE = 0;
     SORTIE = 0;
     STOCK  = 0;
     SELECT max(entete_sorties.date_piece) FROM entete_sorties INTO :d;
     SELECT articles.gestion_stock,articles.matiere FROM articles WHERE articles.id_art = :id_art INTO :gestion_satock,:matiere;
     if ((gestion_satock = 1)AND(matiere <> 1)) then
     FOR
        SELECT
              CAST(INVENTAIRE.date_inventaire AS DATE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN
        AND INVENTAIRE.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
             cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(:Du AS DATE),
              cast(0 AS numeric(18,6)),
              cast(:QUANTITE AS numeric(18,6)),
              cast('F' AS varchar(1))
        FROM ARTICLES
        WHERE ARTICLES.id_art = :ID_ART

        ORDER BY 1,4

        INTO :DATE_OPER, :ENTREE, :SORTIE, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             if (STOCK < 0) then
                BEGIN
                     exception RUPTURE_STOCK;
                     exit;
                END
        END
      SUSPEND;
END
^


ALTER PROCEDURE VIR_FONDS_ID_VIR_AI
RETURNS (
    NEW_VALUE INTEGER)
AS BEGIN
  NEW_VALUE = GEN_ID(VIR_FONDS_ID_VIR_GEN, 1);
END
^


ALTER PROCEDURE VRF_FCH_STK_DPT_DTE_UPD_E (
    OLD_DATE DATE,
    NEW_DATE DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    OLD_QUANTITE NUMERIC (18, 6),
    NEW_QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS DECLARE variable D DATE;
DECLARE variable SORTIE NUMERIC (18, 6);
DECLARE variable DATE_OPER DATE;
DECLARE variable ENTREE NUMERIC (18, 6);
DECLARE variable STOCK NUMERIC (18, 6);
DECLARE variable OPER VARCHAR (1);
DECLARE variable gestion_satock smallint;
DECLARE variable matiere smallint;
BEGIN
     ENTREE = 0;
     SORTIE = 0;
     STOCK  = 0;
     SELECT max(entete_sorties.date_piece) FROM entete_sorties INTO :d;
     SELECT articles.gestion_stock,articles.matiere FROM articles WHERE articles.id_art = :id_art INTO :gestion_satock,:matiere;
     if ((gestion_satock = 1)AND(matiere <> 1)) then
     FOR
        SELECT
              CAST(INVENTAIRE.date_inventaire AS DATE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN
        AND INVENTAIRE.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
             cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(:OLD_DATE AS DATE),
              cast(0 AS numeric(18,6)),
              cast(:OLD_QUANTITE AS numeric(18,6)),
              cast('F' AS varchar(1))
        FROM ARTICLES
        WHERE ARTICLES.id_art = :ID_ART

        UNION ALL

        SELECT
              CAST(:NEW_DATE AS DATE),
              cast(:NEW_QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM ARTICLES
        WHERE ARTICLES.id_art = :ID_ART

        ORDER BY 1,4

        INTO :DATE_OPER, :ENTREE, :SORTIE, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             if (STOCK < 0) then
                BEGIN
                     exception RUPTURE_STOCK;
                     exit;
                END
        END
      SUSPEND;
END
^


ALTER PROCEDURE VRF_FCH_STK_DPT_DTE_UPTE_S (
    OLD_DATE DATE,
    NEW_DATE DATE,
    ID_ART INTEGER,
    ID_MAGAZIN INTEGER,
    OLD_QUANTITE NUMERIC (18, 6),
    NEW_QUANTITE NUMERIC (18, 6),
    REF_ENTREE VARCHAR (12))
AS DECLARE variable D DATE;
DECLARE variable SORTIE NUMERIC (18, 6);
DECLARE variable DATE_OPER DATE;
DECLARE variable ENTREE NUMERIC (18, 6);
DECLARE variable STOCK NUMERIC (18, 6);
DECLARE variable OPER VARCHAR (1);
DECLARE variable gestion_satock smallint;
DECLARE variable matiere smallint;
BEGIN
     ENTREE = 0;
     SORTIE = 0;
     STOCK  = 0;
     SELECT max(entete_sorties.date_piece) FROM entete_sorties INTO :d;
     SELECT articles.gestion_stock,articles.matiere FROM articles WHERE articles.id_art = :id_art INTO :gestion_satock,:matiere;
     if ((gestion_satock = 1)AND(matiere <> 1)) then
     FOR
        SELECT
              CAST(INVENTAIRE.date_inventaire AS DATE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.date_inventaire <= :D
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN
        AND INVENTAIRE.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 1
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 2
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 3
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 5
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
             cast(sorties.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 7
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 10
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_2 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_piEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION all

        SELECT
              CAST(ENTREES.date_entree AS DATE),
              cast(0 AS numeric(18,6)),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM entrees
        WHERE entrees.nature_entree = 4
        AND entrees.id_article = :ID_ART
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        AND ENTREES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 2
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 3
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 4
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 1
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 1
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(SORTIES.date_piece AS DATE),
              cast(0 AS numeric(18,6)),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM sorties
        WHERE sorties.nature_sortie = 10
        AND sorties.id_article = :ID_ART
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        AND SORTIES.date_piece <= :D
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(0 AS numeric(18,6)),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast('S' AS varchar(1))
        FROM lignes_transfert
        WHERE lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.date_PiEce <= :D
        AND lignes_transfert.ref_entree = :REF_ENTREE

        UNION ALL

        SELECT
              CAST(:OLD_DATE AS DATE),
              cast(:OLD_QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast('E' AS varchar(1))
        FROM ARTICLES
        WHERE ARTICLES.id_art = :ID_ART

        UNION ALL

        SELECT
              CAST(:NEW_DATE AS DATE),
              cast(0 AS numeric(18,6)),
              cast(:NEW_QUANTITE AS numeric(18,6)),
              cast('F' AS varchar(1))
        FROM ARTICLES
        WHERE ARTICLES.id_art = :ID_ART

        ORDER BY 1,4

        INTO :DATE_OPER, :ENTREE, :SORTIE, :OPER

      DO
        BEGIN
             if (:ENTREE IS NULL) then ENTREE = 0;
             if (:SORTIE IS NULL) then SORTIE = 0;
             STOCK = :STOCK + CAST((:ENTREE - :SORTIE) AS NUMERIC(18,6));
             if (STOCK < 0) then
                BEGIN
                     exception RUPTURE_STOCK;
                     exit;
                END
        END
      SUSPEND;
END
^


SET TERM ; ^

SET TERM ^ ;

/* Triggers definition */



/* Trigger: AI_ARCHIVES_ID_ARCHIVE */
CREATE TRIGGER AI_ARCHIVES_ID_ARCHIVE FOR ARCHIVES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ARCHIVE IS NULL) THEN
      NEW.ID_ARCHIVE = GEN_ID(ARCHIVES_ID_ARCHIVE_GEN, 1);
END
^

CREATE TRIGGER AI_CAT_PROD_ID_CAT_PROD FOR CAT_PROD ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CAT_PROD IS NULL) THEN
    NEW.ID_CAT_PROD = GEN_ID(CAT_PROD_ID_CAT_PROD_GEN, 1);
END
^

/* Trigger: AI_ARTICLES_ID_ART */
CREATE TRIGGER AI_ARTICLES_ID_ART FOR ARTICLES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ART IS NULL) THEN
      NEW.ID_ART = GEN_ID(ARTICLES_ID_ART_GEN, 1);
END
^

/* Trigger: AI_BC_F_ID_BC_F */
CREATE TRIGGER AI_BC_F_ID_BC_F FOR BC_F ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_BC_F IS NULL) THEN
      NEW.ID_BC_F = GEN_ID(BC_F_ID_BC_F_GEN, 1);
END
^

/* Trigger: AI_CAMION_ID_CAMION */
CREATE TRIGGER AI_CAMION_ID_CAMION FOR CAMION ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CAMION IS NULL) THEN
      NEW.ID_CAMION = GEN_ID(CAMION_ID_CAMION_GEN, 1);
END
^

/* Trigger: AI_CATALOGUE_ID_CAT */
CREATE TRIGGER AI_CATALOGUE_ID_CAT FOR CATALOGUE ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CAT IS NULL) THEN
      NEW.ID_CAT = GEN_ID(CATALOGUE_ID_CAT_GEN, 1);
END
^

/* Trigger: AI_CHAUFFEURS_ID_CHAUFFEUR */
CREATE TRIGGER AI_CHAUFFEURS_ID_CHAUFFEUR FOR CHAUFFEURS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CHAUFFEUR IS NULL) THEN
      NEW.ID_CHAUFFEUR = GEN_ID(CHAUFFEURS_ID_CHAUFFEUR_GEN, 1);
END
^

/* Trigger: AI_CLIENTS_ID_CLIENT */
CREATE TRIGGER AI_CLIENTS_ID_CLIENT FOR CLIENTS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CLIENT IS NULL) THEN
      NEW.ID_CLIENT = GEN_ID(CLIENTS_ID_CLIENT_GEN, 1);
END
^

/* Trigger: AI_CODE_TAP_ID_TAP */
CREATE TRIGGER AI_CODE_TAP_ID_TAP FOR CODE_TAP ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_TAP IS NULL) THEN
      NEW.ID_TAP = GEN_ID(CODE_TAP_ID_TAP_GEN, 1);
END
^

/* Trigger: AI_CODE_TVA_ID_TVA */
CREATE TRIGGER AI_CODE_TVA_ID_TVA FOR CODE_TVA ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_TVA IS NULL) THEN
      NEW.ID_TVA = GEN_ID(CODE_TVA_ID_TVA_GEN, 1);
END
^

/* Trigger: AI_COMPTES_TRESORIE_ID_CPTE */
CREATE TRIGGER AI_COMPTES_TRESORIE_ID_CPTE FOR COMPTES_TRESORIE ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_CPTE IS NULL) THEN
      NEW.ID_CPTE = GEN_ID(COMPTES_TRESORIE_ID_CPTE_GEN, 1);
END
^

/* Trigger: AI_DEPENSES_ID_DEP */
CREATE TRIGGER AI_DEPENSES_ID_DEP FOR DEPENSES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_DEP IS NULL) THEN
      NEW.ID_DEP = GEN_ID(DEPENSES_ID_DEP_GEN, 1);
END
^

/* Trigger: AI_DESTINATION_ID_DESTINATION */
CREATE TRIGGER AI_DESTINATION_ID_DESTINATION FOR DESTINATION ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_DESTINATION IS NULL) THEN
      NEW.ID_DESTINATION = GEN_ID(DESTINATION_ID_DESTINATION_GEN, 1);
END
^

/* Trigger: AI_DEVISES_ID_DEVISE */
CREATE TRIGGER AI_DEVISES_ID_DEVISE FOR DEVISES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_DEVISE IS NULL) THEN
      NEW.ID_DEVISE = GEN_ID(DEVISES_ID_DEVISE_GEN, 1);
END
^

/* Trigger: AI_EMPLACEMENTS_ID_EMPL */
CREATE TRIGGER AI_EMPLACEMENTS_ID_EMPL FOR EMPLACEMENTS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_EMPL IS NULL) THEN
      NEW.ID_EMPL = GEN_ID(EMPLACEMENTS_ID_EMPL_GEN, 1);
END
^

/* Trigger: AI_ENTETE_ENTREES_ID_ENT_ENT */
CREATE TRIGGER AI_ENTETE_ENTREES_ID_ENT_ENT FOR ENTETE_ENTREES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ENT_ENT IS NULL) THEN
      NEW.ID_ENT_ENT = GEN_ID(ENTETE_ENTREES_ID_ENT_ENT_GEN, 1);
END
^

/* Trigger: AI_ENTETE_SORTIES_ID_ENT_SORT */
CREATE TRIGGER AI_ENTETE_SORTIES_ID_ENT_SORT FOR ENTETE_SORTIES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ENT_SORT IS NULL) THEN
      NEW.ID_ENT_SORT = GEN_ID(ENTETE_SORTIES_ID_ENT_SORT_GEN, 1);
END
^

/* Trigger: AI_ENTETE_TRANSFERT_ID_ENTETE */
CREATE TRIGGER AI_ENTETE_TRANSFERT_ID_ENTETE FOR ENTETE_TRANSFERT ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ENTETE IS NULL) THEN
      NEW.ID_ENTETE = GEN_ID(ENTETE_TRANSFERT_ID_ENTETE_GEN, 1);
END
^

/* Trigger: AI_ENTET_BC_F_ID_ENT_BD_F */
CREATE TRIGGER AI_ENTET_BC_F_ID_ENT_BD_F FOR ENTET_BC_F ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ENT_BD_F IS NULL) THEN
      NEW.ID_ENT_BD_F = GEN_ID(ENTET_BC_F_ID_ENT_BD_F_GEN, 1);
END
^

/* Trigger: AI_ENTREES_ID_ENTREE */
CREATE TRIGGER AI_ENTREES_ID_ENTREE FOR ENTREES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ENTREE IS NULL) THEN
      NEW.ID_ENTREE = GEN_ID(ENTREES_ID_ENTREE_GEN, 1);
END
^

/* Trigger: AI_FACT_ANT_ID_FACT */
CREATE TRIGGER AI_FACT_ANT_ID_FACT FOR FACT_ANT ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FACT IS NULL) THEN
      NEW.ID_FACT = GEN_ID(FACT_ANT_ID_FACT_GEN, 1);
END
^

/* Trigger: AI_FACT_DD_JOINTE_ID_FACT */
CREATE TRIGGER AI_FACT_DD_JOINTE_ID_FACT FOR FACT_DD_JOINTE ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FACT IS NULL) THEN
      NEW.ID_FACT = GEN_ID(FACT_DD_JOINTE_ID_FACT_GEN, 1);
END
^

/* Trigger: AI_FAMILLES_CHARGES_ID_FAM */
CREATE TRIGGER AI_FAMILLES_CHARGES_ID_FAM FOR FAMILLES_CHARGES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FAM IS NULL) THEN
      NEW.ID_FAM = GEN_ID(FAMILLES_CHARGES_ID_FAM_GEN, 1);
END
^

/* Trigger: AI_FAMILLES_ID_FAMILLE */
CREATE TRIGGER AI_FAMILLES_ID_FAMILLE FOR FAMILLES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FAMILLE IS NULL) THEN
      NEW.ID_FAMILLE = GEN_ID(FAMILLES_ID_FAMILLE_GEN, 1);
END
^

/* Trigger: AI_FAMILLES_RECETTES_ID_FAM_REC */
CREATE TRIGGER AI_FAMILLES_RECETTES_ID_FAM_REC FOR FAMILLES_RECETTES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FAM_REC IS NULL) THEN
      NEW.ID_FAM_REC = GEN_ID(FAM_RECETTES_ID_FAM_REC_GEN, 1);
END
^

/* Trigger: AI_FOURNISSEURS_ID_FOURN */
CREATE TRIGGER AI_FOURNISSEURS_ID_FOURN FOR FOURNISSEURS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FOURN IS NULL) THEN
      NEW.ID_FOURN = GEN_ID(FOURNISSEURS_ID_FOURN_GEN, 1);
END
^

/* Trigger: AI_FRAUDEURS_ID_FRAUD */
CREATE TRIGGER AI_FRAUDEURS_ID_FRAUD FOR FRAUDEURS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_FRAUD IS NULL) THEN
      NEW.ID_FRAUD = GEN_ID(FRAUDEURS_ID_FRAUD_GEN, 1);
END
^

/* Trigger: AI_G50_ID_G50 */
CREATE TRIGGER AI_G50_ID_G50 FOR G50 ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_G50 IS NULL) THEN
      NEW.ID_G50 = GEN_ID(G50_ID_G50_GEN, 1);
END
^

/* Trigger: AI_INVENTAIRE_ID_INVENTAIRE */
CREATE TRIGGER AI_INVENTAIRE_ID_INVENTAIRE FOR INVENTAIRE ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_INVENTAIRE IS NULL) THEN
      NEW.ID_INVENTAIRE = GEN_ID(INVENTAIRE_ID_INVENTAIRE_GEN, 1);
END
^

/* Trigger: AI_LIGNES_PROFORMA_ID_L_PROF */
CREATE TRIGGER AI_LIGNES_PROFORMA_ID_L_PROF FOR LIGNES_PROFORMA ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_L_PROF IS NULL) THEN
      NEW.ID_L_PROF = GEN_ID(LIGNES_PROFORMA_ID_L_PROF_GEN, 1);
END
^

/* Trigger: AI_LIGNES_TRANSFERT_ID_LIGNE */
CREATE TRIGGER AI_LIGNES_TRANSFERT_ID_LIGNE FOR LIGNES_TRANSFERT ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_LIGNE IS NULL) THEN
      NEW.ID_LIGNE = GEN_ID(LIGNES_TRANSFERT_ID_LIGNE_GEN, 1);
END
^

/* Trigger: AI_LST_CHRGS_ID_LST_CHRG */
CREATE TRIGGER AI_LST_CHRGS_ID_LST_CHRG FOR LISTE_CHARGES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_LISTE_CHARGE IS NULL) THEN
      NEW.ID_LISTE_CHARGE = GEN_ID(LST_CHGES_ID_LISTE_CHARGE_GEN, 1);
END
^

/* Trigger: AI_MAGAZINS_ID_MAG */
CREATE TRIGGER AI_MAGAZINS_ID_MAG FOR MAGAZINS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_MAG IS NULL) THEN
      NEW.ID_MAG = GEN_ID(MAGAZINS_ID_MAG_GEN, 1);
END
^

/* Trigger: AI_MARQUES_ID_MARQUE */
CREATE TRIGGER AI_MARQUES_ID_MARQUE FOR MARQUES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_MARQUE IS NULL) THEN
      NEW.ID_MARQUE = GEN_ID(MARQUES_ID_MARQUE_GEN, 1);
END
^

/* Trigger: AI_NOMENCLATURES_ID_NOM */
CREATE TRIGGER AI_NOMENCLATURES_ID_NOM FOR NOMENCLATURES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_NOM IS NULL) THEN
      NEW.ID_NOM = GEN_ID(NOMENCLATURES_ID_NOM_GEN, 1);
END
^

/* Trigger: AI_NUM_SERIES_ID_NUM */
CREATE TRIGGER AI_NUM_SERIES_ID_NUM FOR NUM_SERIES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_NUM IS NULL) THEN
      NEW.ID_NUM = GEN_ID(NUM_SERIES_ID_NUM_GEN, 1);
END
^

/* Trigger: AI_PARAMETRES_ID_PARAM */
CREATE TRIGGER AI_PARAMETRES_ID_PARAM FOR PARAMETRES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_PARAM IS NULL) THEN
      NEW.ID_PARAM = GEN_ID(PARAMETRES_ID_PARAM_GEN, 1);
END
^

/* Trigger: AI_PROFORMA_ID_PROF */
CREATE TRIGGER AI_PROFORMA_ID_PROF FOR PROFORMA ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_PROF IS NULL) THEN
      NEW.ID_PROF = GEN_ID(PROFORMA_ID_PROF_GEN, 1);
END
^

/* Trigger: AI_RECETTES_ID_REC */
CREATE TRIGGER AI_RECETTES_ID_REC FOR RECETTES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_REC IS NULL) THEN
      NEW.ID_REC = GEN_ID(RECETTES_ID_REC_GEN, 1);
END
^

/* Trigger: AI_REGLEMENTS_ID_REGLEMENT */
CREATE TRIGGER AI_REGLEMENTS_ID_REGLEMENT FOR REGLEMENTS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_REGLEMENT IS NULL) THEN
      NEW.ID_REGLEMENT = GEN_ID(REGLEMENTS_ID_REGLEMENT_GEN, 1);
END
^

/* Trigger: AI_REGL_TIERS_ID_REGL */
CREATE TRIGGER AI_REGL_TIERS_ID_REGL FOR REGL_TIERS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_REGL IS NULL) THEN
      NEW.ID_REGL = GEN_ID(REGL_TIERS_ID_REGL_GEN, 1);
END
^

/* Trigger: AI_RENUM_CHAUFFEURS_ID_RENUM */
CREATE TRIGGER AI_RENUM_CHAUFFEURS_ID_RENUM FOR RENUM_CHAUFFEURS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_RENUM IS NULL) THEN
      NEW.ID_RENUM = GEN_ID(RENUM_CHAUFFEURS_ID_RENUM_GEN, 1);
END
^

/* Trigger: AI_SORTIES_ID_SORTIE */
CREATE TRIGGER AI_SORTIES_ID_SORTIE FOR SORTIES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_SORTIE IS NULL) THEN
      NEW.ID_SORTIE = GEN_ID(SORTIES_ID_SORTIE_GEN, 1);
END
^

/* Trigger: AI_TAUX_EXTRACTION_ID_TAUX */
CREATE TRIGGER AI_TAUX_EXTRACTION_ID_TAUX FOR TAUX_EXTRACTION ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_TAUX IS NULL) THEN
      NEW.ID_TAUX = GEN_ID(TAUX_EXTRACTION_ID_TAUX_GEN, 1);
END
^

/* Trigger: AI_TYPE_AUTRES_ENTREES_ID_TYPE */
CREATE TRIGGER AI_TYPE_AUTRES_ENTREES_ID_TYPE FOR TYPE_AUTRES_ENTREES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_TYPE IS NULL) THEN
      NEW.ID_TYPE = GEN_ID(TYPE_AUTRES_ENTREES_ID_TYPE_GEN, 1);
END
^

/* Trigger: AI_TYPE_AUTRES_SORTIES_ID_TYPE */
CREATE TRIGGER AI_TYPE_AUTRES_SORTIES_ID_TYPE FOR TYPE_AUTRES_SORTIES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_TYPE IS NULL) THEN
      NEW.ID_TYPE = GEN_ID(TYPE_AUTRES_SORTIES_ID_TYPE_GEN, 1);
END
^

/* Trigger: AI_UNITES_ID_UNITE */
CREATE TRIGGER AI_UNITES_ID_UNITE FOR UNITES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_UNITE IS NULL) THEN
      NEW.ID_UNITE = GEN_ID(UNITES_ID_UNITE_GEN, 1);
END
^

/* Trigger: AI_VIR_FONDS_ID_VIR */
CREATE TRIGGER AI_VIR_FONDS_ID_VIR FOR VIR_FONDS ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_VIR IS NULL) THEN
      NEW.ID_VIR = GEN_ID(VIR_FONDS_ID_VIR_GEN, 1);
END
^

/* Trigger: AI_ZONES_ID_ZONE */
CREATE TRIGGER AI_ZONES_ID_ZONE FOR ZONES ACTIVE
BEFORE INSERT POSITION 0
AS
BEGIN
  IF (NEW.ID_ZONE IS NULL) THEN
      NEW.ID_ZONE = GEN_ID(ZONES_ID_ZONE_GEN, 1);
END
^

CREATE TRIGGER AI_CONSIGNATIONS_ID_CONSIGNE FOR CONSIGNATIONS
ACTIVE BEFORE
  INSERT
POSITION 0
AS
BEGIN
  IF (NEW.ID_CONSIGNE IS NULL) THEN
      NEW.ID_CONSIGNE = GEN_ID(CONSIGNATIONS_ID_CONSIGNE_GEN, 1);
END^

CREATE TRIGGER AI_LIGNES_CONSIGNATION_ID_LC FOR LIGNES_CONSIGNATION
ACTIVE BEFORE
  INSERT
POSITION 0
AS
BEGIN
  IF (NEW.ID_LC IS NULL) THEN
      NEW.ID_LC = GEN_ID(LIGNES_CONSIGNATION_ID_LC_GEN, 1);
END^

CREATE TRIGGER AI_NORMES_ID_NORME FOR NORMES
ACTIVE BEFORE
  INSERT
POSITION 0
AS
BEGIN
        IF (NEW.ID_NORME IS NULL) THEN
                NEW.ID_NORME = GEN_ID(NORMES_ID_NORME_GEN, 1);
END^

SET TERM ; ^
