SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ID_CODE_TLS'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ID_CODE_TLS INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'ID_CODE_TLS')) then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD ID_CODE_TLS INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'CLIENTS_ACTIVITES' and rf.RDB$FIELD_NAME = 'PRINCIPALE')) then
execute statement 'ALTER TABLE CLIENTS_ACTIVITES ADD PRINCIPALE SMALLINT DEFAULT 0;';
END^
SET TERM ; ^

commit;

SET TERM ^ ;
EXECUTE block as
BEGIN
if (exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ID_CODE_TLS'))
then
execute statement 'update PARAMETRES set ID_CODE_TLS = 1 where ID_CODE_TLS is null ;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'ID_CODE_TLS')) then
execute statement 'update ENTETE_SORTIES set ID_CODE_TLS = 1 where ID_CODE_TLS is null;';
END^
SET TERM ; ^

CREATE OR ALTER VIEW RECUP_TVA
(
  DENOMINATION,
  ADRESSE,
  RC,
  AI,
  NIF,
  NIN,
  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_TTC + entete_entrees.TIMBRE,
      entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      0
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE ((entete_entrees.nature_entree = 2)or(entete_entrees.nature_entree = 5));

CREATE OR ALTER VIEW VIEW_MOUVEMENTS(
  ID_ES,
  ID_ARTICLE,
  REF_ENTREE,
  ID_MAGAZIN,
  DATE_OPER,
  COUT,
  QTE_INI,
  QTE_ENT,
  QTE_SRT,
  VAL_ORD)
AS
SELECT
           INVENTAIRE.ID_INVENTAIRE,
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           INVENTAIRE.COUT,
           INVENTAIRE.quantite,
           0,
           0,
           1
     FROM inventaire
order by 4, 2;

SET TERM ^ ;
CREATE OR ALTER PROCEDURE CHECK_NEGATIVE_STOCK
RETURNS(
  PDATE DATE)
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE CHECK_NEGATIVE_STOCK (
    PDATE DATE
)
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),
    ART_ID INTEGER,
    MAG_ID INTEGER
)
AS
    DECLARE VARIABLE VAR_ID_ART INTEGER;
    DECLARE VARIABLE VAR_ID_MAGAZIN INTEGER;
    DECLARE VARIABLE V_ID_ES INTEGER;
    DECLARE VARIABLE V_NUM VARCHAR(12);
    DECLARE VARIABLE V_ID_ARTICLE INTEGER;
    DECLARE VARIABLE V_DATE_OPER DATE;
    DECLARE VARIABLE V_LIBELLE VARCHAR(128);
    DECLARE VARIABLE V_CLIENT_FOURNISSEUR VARCHAR(128);
    DECLARE VARIABLE V_ENTREE NUMERIC(18,6);
    DECLARE VARIABLE V_SORTIE NUMERIC(18,6);
    DECLARE VARIABLE V_COUT NUMERIC(18,2);
    DECLARE VARIABLE V_PRIX_VENTE NUMERIC(18,2);
    DECLARE VARIABLE V_VALEUR_ENTREE NUMERIC(18,2);
    DECLARE VARIABLE V_VALEUR_SORTIE NUMERIC(18,2);
    DECLARE VARIABLE V_STOCK NUMERIC(18,6);
    DECLARE VARIABLE V_VALEUR_STOCK NUMERIC(18,2);
    DECLARE VARIABLE V_P_A_M_P NUMERIC(18,2);
    DECLARE VARIABLE V_OPER VARCHAR(1);
BEGIN
    FOR SELECT ID_ART FROM ARTICLES INTO :VAR_ID_ART DO
    BEGIN
        FOR SELECT MAGAZINS.ID_MAG FROM MAGAZINS INTO :VAR_ID_MAGAZIN DO
        BEGIN
            FOR SELECT
                ID_ES, NUM, ID_ARTICLE, DATE_OPER, LIBELLE, CLIENT_FOURNISSEUR,
                ENTREE, SORTIE, COUT, PRIX_VENTE, VALEUR_ENTREE, VALEUR_SORTIE,
                STOCK, VALEUR_STOCK, P_A_M_P, OPER
            FROM FICHE_DE_STOCK_DEPOT(:PDATE, :VAR_ID_ART, :VAR_ID_MAGAZIN)
            INTO
                :V_ID_ES, :V_NUM, :V_ID_ARTICLE, :V_DATE_OPER, :V_LIBELLE, :V_CLIENT_FOURNISSEUR,
                :V_ENTREE, :V_SORTIE, :V_COUT, :V_PRIX_VENTE, :V_VALEUR_ENTREE, :V_VALEUR_SORTIE,
                :V_STOCK, :V_VALEUR_STOCK, :V_P_A_M_P, :V_OPER
            DO
            BEGIN
                IF (:V_STOCK < 0) THEN
                BEGIN
                    ID_ES = :V_ID_ES;
                    NUM = :V_NUM;
                    ID_ARTICLE = :V_ID_ARTICLE;
                    DATE_OPER = :V_DATE_OPER;
                    LIBELLE = :V_LIBELLE;
                    CLIENT_FOURNISSEUR = :V_CLIENT_FOURNISSEUR;
                    ENTREE = :V_ENTREE;
                    SORTIE = :V_SORTIE;
                    COUT = :V_COUT;
                    PRIX_VENTE = :V_PRIX_VENTE;
                    VALEUR_ENTREE = :V_VALEUR_ENTREE;
                    VALEUR_SORTIE = :V_VALEUR_SORTIE;
                    STOCK = :V_STOCK;
                    VALEUR_STOCK = :V_VALEUR_STOCK;
                    P_A_M_P = :V_P_A_M_P;
                    OPER = :V_OPER;
                    ART_ID = :VAR_ID_ART;      -- Include the current Article ID
                    MAG_ID = :VAR_ID_MAGAZIN;  -- Include the current Warehouse ID
                SUSPEND;
                END
            END
        END
    END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'NORMES')) then
          BEGIN
               execute statement 'CREATE TABLE NORMES (   ID_NORME INTEGER NOT NULL,    NORME VARCHAR (32));';
               EXECUTE STATEMENT 'CREATE UNIQUE INDEX PK_NORMES ON NORMES (ID_NORME);';
               EXECUTE STATEMENT 'CREATE GENERATOR NORMES_ID_NORME_GEN;';
               EXECUTE STATEMENT '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';
               EXECUTE STATEMENT 'CREATE PROCEDURE NORMES_ID_NORME_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(NORMES_ID_NORME_GEN, 1); END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'CAT_PROD')) then
          BEGIN
               execute statement 'CREATE TABLE CAT_PROD (ID_CAT_PROD INTEGER NOT NULL,LIBELLE VARCHAR(64),ID_FAM_PROD INTEGER DEFAULT 1 NOT NULL, ANC_ID_CAT_PRO INTEGER);';
               EXECUTE STATEMENT 'ALTER TABLE CAT_PROD ADD CONSTRAINT PK_CAT_PROD PRIMARY KEY (ID_CAT_PROD);';
               EXECUTE STATEMENT 'CREATE GENERATOR CAT_PROD_ID_CAT_PROD_GEN;';
               EXECUTE STATEMENT '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;';
               EXECUTE STATEMENT '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;';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'NIN'))
then
execute statement 'ALTER TABLE MAGAZINS ADD NIN VARCHAR(20)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'BP'))
then
execute statement 'ALTER TABLE MAGAZINS ADD BP VARCHAR(20)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
inner join RDB$FIELDS F
  on F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE
where rf.RDB$RELATION_NAME = 'REGL_TIERS'
and rf.RDB$FIELD_NAME = 'NUM_CHEQUE'
and F.RDB$CHARACTER_LENGTH = '9'
))
then
execute statement 'ALTER TABLE REGL_TIERS ALTER COLUMN NUM_CHEQUE TYPE VARCHAR(20);';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ACT_PRIX_V'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ACT_PRIX_V SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ACT_NOM'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ACT_NOM SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'DEPUIS_FAMILLE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD DEPUIS_FAMILLE SMALLINT DEFAULT 0';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'PROF_A_S'))
then
execute statement 'ALTER TABLE PARAMETRES ADD PROF_A_S SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTET_BC_F' and rf.RDB$FIELD_NAME = 'TIMBRE'))
then
execute statement 'ALTER TABLE ENTET_BC_F ADD TIMBRE NUMERIC(18, 2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTET_BC_F' and rf.RDB$FIELD_NAME = 'TOTAL_BCF'))
then
execute statement 'ALTER TABLE ENTET_BC_F ADD TOTAL_BCF NUMERIC(18, 2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTET_BC_F' and rf.RDB$FIELD_NAME = 'ID_REGLEMENT'))
then
execute statement 'ALTER TABLE ENTET_BC_F ADD ID_REGLEMENT INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTET_BC_F' and rf.RDB$FIELD_NAME = 'DATE_PROF'))
then
execute statement 'ALTER TABLE ENTET_BC_F ADD DATE_PROF DATE';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTET_BC_F' and rf.RDB$FIELD_NAME = 'NUM_PROF'))
then
execute statement 'ALTER TABLE ENTET_BC_F ADD NUM_PROF VARCHAR(12)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'TAUX_EXTRACTION' and rf.RDB$FIELD_NAME = 'ARRONDI'))
then
execute statement 'ALTER TABLE TAUX_EXTRACTION ADD ARRONDI SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'PAYS'))
then
execute statement 'ALTER TABLE CLIENTS ADD PAYS  varchar(56)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'BLOCKE'))
then
execute statement 'ALTER TABLE CLIENTS ADD BLOCKE SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'NIN'))
then
execute statement 'ALTER TABLE CLIENTS ADD NIN  varchar(20)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'DENOMINATION_AR'))
then
execute statement 'ALTER TABLE CLIENTS ADD DENOMINATION_AR  varchar(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'ACTIVITE_AR'))
then
execute statement 'ALTER TABLE CLIENTS ADD ACTIVITE_AR  varchar(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'ADRESSE1_AR'))
then
execute statement 'ALTER TABLE CLIENTS ADD ADRESSE1_AR  varchar(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'ADRESSE2_AR'))
then
execute statement 'ALTER TABLE CLIENTS ADD ADRESSE2_AR  varchar(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'ADRESSE3_AR'))
then
execute statement 'ALTER TABLE CLIENTS ADD ADRESSE3_AR  varchar(128)';
END^


EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'SOLDE_INI_CLIENT'))
then
execute statement 'ALTER TABLE CLIENTS ADD SOLDE_INI_CLIENT  NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'SOLDE_INI_FOURN'))
then
execute statement 'ALTER TABLE CLIENTS ADD SOLDE_INI_FOURN  NUMERIC(18,2)';
END^


EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'TELEPHONE'))
then
execute statement 'ALTER TABLE MAGAZINS ADD TELEPHONE VARCHAR(32)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'FAX'))
then
execute statement 'ALTER TABLE MAGAZINS ADD FAX VARCHAR(32)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'PORTABLE'))
then
execute statement 'ALTER TABLE MAGAZINS ADD PORTABLE VARCHAR(32)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'EMAIL'))
then
execute statement 'ALTER TABLE MAGAZINS ADD EMAIL VARCHAR(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'NIN'))
then
execute statement 'ALTER TABLE PARAMETRES ADD NIN VARCHAR(20)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'FOURN_DEP'))
then
execute statement 'ALTER TABLE PARAMETRES ADD FOURN_DEP SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'TAUX_TIMBRE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD TAUX_TIMBRE  NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'VAL_MIN_TIMBRE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD VAL_MIN_TIMBRE  NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'VAL_MAX_TIMBRE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD VAL_MAX_TIMBRE  NUMERIC(18,2)';
END^

SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'DEPENSES_TR')) then
          BEGIN
               execute statement 'CREATE TABLE DEPENSES_TR (ID_DEP_TR INTEGER NOT NULL,ID_ENTETE INTEGER NOT NULL,LIBELLES VARCHAR(128) NOT NULL,MNT_DEP_TR NUMERIC(18, 2));';
               EXECUTE STATEMENT 'ALTER TABLE DEPENSES_TR ADD PRIMARY KEY (ID_DEP_TR);';
               EXECUTE STATEMENT 'CREATE GENERATOR DEPENSES_TR_ID_DEP_TR_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER BI_DEPENSES_TR_ID_DEP_TR FOR DEPENSES_TR ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_DEP_TR IS NULL) THEN NEW.ID_DEP_TR = GEN_ID(DEPENSES_TR_ID_DEP_TR_GEN, 1);END;';
               EXECUTE STATEMENT 'CREATE PROCEDURE DEPENSES_TR_ID_DEP_TR_AUTOINC RETURNS(NEW_VALUE INTEGER) AS BEGIN NEW_VALUE = GEN_ID(DEPENSES_TR_ID_DEP_TR_GEN, 1); END;';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'QUALITES')) then
          BEGIN
               execute statement 'CREATE TABLE QUALITES (ID_QUALITE INTEGER NOT NULL,QUALITE VARCHAR (32) CHARACTER SET NONE);';
               EXECUTE STATEMENT 'ALTER TABLE QUALITES ADD CONSTRAINT PK_QUALITE PRIMARY KEY (ID_QUALITE);';
               EXECUTE STATEMENT 'CREATE GENERATOR QUALITE_ID_QUAL_GEN ;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_QUALITE_ID_QUAL FOR QUALITES ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_QUALITE IS NULL) THEN NEW.ID_QUALITE = GEN_ID(QUALITE_ID_QUAL_GEN, 1);END';
               EXECUTE STATEMENT 'CREATE PROCEDURE QUALITE_ID_QUAL_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(QUALITE_ID_QUAL_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'BULLETIN_PESEES')) then
          BEGIN
               execute statement 'CREATE TABLE BULLETIN_PESEES (ID_BUL SMALLINT NOT NULL,ID_CAMION INTEGER NOT NULL,ID_CLIENT INTEGER NOT NULL,ID_ARTICLE INTEGER,ID_CHAUFFEUR INTEGER,ID_DESTINATION INTEGER,TARE NUMERIC(12, 2) NOT NULL,BRUTE NUMERIC(12, 2) NOT NULL,NET NUMERIC(12, 2) NOT NULL,DATE_TARE TIMESTAMP NOT NULL,DATE_BRUTE TIMESTAMP NOT NULL,LOCKED  SMALLINT DEFAULT 1 NOT NULL,NUM_BUL VARCHAR(12) NOT NULL,ACHAT_VENTE  SMALLINT DEFAULT 1 NOT NULL,IS_TARED SMALLINT DEFAULT 0 NOT NULL,IS_WEIGHTED  SMALLINT DEFAULT 0 NOT NULL,IS_AUTO SMALLINT DEFAULT 1 NOT NULL);';
               EXECUTE STATEMENT 'ALTER TABLE BULLETIN_PESEES ADD PRIMARY KEY (ID_BUL);';
               EXECUTE STATEMENT 'CREATE GENERATOR BULLETIN_PESEES_ID_BUL_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER BI_BULLETIN_PESEES_ID_BUL FOR BULLETIN_PESEES ACTIVE BEFORE INSERT POSITION 0 AS BEGIN  IF (NEW.ID_BUL IS NULL) THEN NEW.ID_BUL = GEN_ID(BULLETIN_PESEES_ID_BUL_GEN, 1);END;';
               EXECUTE STATEMENT 'CREATE PROCEDURE BULLETIN_PESEES_ID_QUAL_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(BULLETIN_PESEES_ID_BUL_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'ARCH_SOLDES')) then
          BEGIN
               execute statement 'CREATE TABLE ARCH_SOLDES (ID_ARCH_SOLDES INTEGER NOT NULL, ID_TIERS INTEGER NOT NULL, DATE_ARCH DATE NOT NULL, SOLDE NUMERIC (18, 2) NOT NULL);';
               EXECUTE STATEMENT 'ALTER TABLE ARCH_SOLDES ADD CONSTRAINT PK_ARCH_SOLDES PRIMARY KEY (ID_ARCH_SOLDES);';
               EXECUTE STATEMENT 'ALTER TABLE ARCH_SOLDES ADD CONSTRAINT FK_ARCH_SOLDES FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS (ID_CLIENT);';
               EXECUTE STATEMENT 'CREATE GENERATOR ARCH_SOLDES_ID_ARCH_SOLDES_GEN';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_ARCH_SOLDES_ID_ARCH_SOLDES FOR ARCH_SOLDES ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_ARCH_SOLDES IS NULL) THEN NEW.ID_ARCH_SOLDES = GEN_ID(ARCH_SOLDES_ID_ARCH_SOLDES_GEN, 1);END';
               EXECUTE STATEMENT 'CREATE PROCEDURE ARCH_SOLDES_ID_ARCH_SOLDES_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN NEW_VALUE = GEN_ID(ARCH_SOLDES_ID_ARCH_SOLDES_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'ENTET_BC_C')) then
          BEGIN
               execute statement 'CREATE TABLE ENTET_BC_C (ID_ENT_BC_C 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, RESERVE SMALLINT,  LOCKED SMALLINT );';
               EXECUTE STATEMENT 'ALTER TABLE ENTET_BC_C ADD CONSTRAINT PK_ENTET_BC_C PRIMARY KEY (ID_ENT_BC_C);';
               EXECUTE STATEMENT 'CREATE GENERATOR ENTET_BC_C_ID_ENT_BC_C_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_ENTET_BC_C_ID_ENT_BC_C FOR ENTET_BC_C ACTIVE BEFORE   INSERT POSITION 0 AS BEGIN IF (NEW.ID_ENT_BC_C IS NULL) THEN NEW.ID_ENT_BC_C = GEN_ID(ENTET_BC_C_ID_ENT_BC_C_GEN, 1); END';
               EXECUTE STATEMENT 'CREATE PROCEDURE ENTET_BC_C_ID_ENT_BC_C_AI RETURNS(  NEW_VALUE INTEGER) AS BEGIN   NEW_VALUE = GEN_ID(ENTET_BC_C_ID_ENT_BC_C_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'BC_C')) then
          BEGIN
               execute statement 'CREATE TABLE BC_C (ID_BC_C INTEGER NOT NULL,  ID_ENTET INTEGER,  ID_TIERS 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, RESERVE SMALLINT, NUM_BC INTEGER);';
               EXECUTE STATEMENT 'ALTER TABLE BC_C ADD CONSTRAINT PK_BC_C PRIMARY KEY (ID_BC_C);';
               EXECUTE STATEMENT 'CREATE GENERATOR BC_C_ID_BC_C_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_BC_C_ID_BC_C FOR BC_C ACTIVE BEFORE   INSERT POSITION 0 AS BEGIN IF (NEW.ID_BC_C IS NULL) THEN NEW.ID_BC_C = GEN_ID(BC_C_ID_BC_C_GEN, 1); END';
               EXECUTE STATEMENT 'CREATE PROCEDURE BC_C_ID_BC_C_AI RETURNS(NEW_VALUE INTEGER)AS BEGIN  NEW_VALUE = GEN_ID(BC_C_ID_BC_C_GEN, 1); END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'MAGAZINS' and rf.RDB$FIELD_NAME = 'NIS')) then
execute statement 'ALTER TABLE MAGAZINS ADD NIS VARCHAR(20);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'NOMENCLATURES' and rf.RDB$FIELD_NAME = 'PROD_STRICT')) then
execute statement 'ALTER TABLE NOMENCLATURES ADD PROD_STRICT SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'NOMENCLATURES' and rf.RDB$FIELD_NAME = 'ID_CAT_PROD')) then
execute statement 'ALTER TABLE NOMENCLATURES ADD ID_CAT_PROD INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'ID_TRANSPORTEUR')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD ID_TRANSPORTEUR INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'ID_CHAUFFEUR')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD ID_CHAUFFEUR SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'ID_DESTINATION')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD ID_DESTINATION SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'LOCKED')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD LOCKED SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'P_U')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD P_U NUMERIC(18,2);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'ACHAT_VENTE')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD ACHAT_VENTE SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'ID_ENTETE')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD ID_ENTETE INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_TARED')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_TARED SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_WEIGHTED')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_WEIGHTED SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_AUTO')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_AUTO SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'NUM_BUL')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD NUM_BUL VARCHAR(12);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_TRANSERED')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_TRANSERED SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_PU_FORFAIT')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_PU_FORFAIT SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'IS_PU_TR_FORFAIT')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD IS_PU_TR_FORFAIT SMALLINT;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'PR_TR')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD PR_TR NUMERIC(18,2);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'BULLETIN_PESEES' and rf.RDB$FIELD_NAME = 'MONTANT_TR')) then
execute statement 'ALTER TABLE BULLETIN_PESEES ADD MONTANT_TR NUMERIC(18,2);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'NOMENCLATURES' and rf.RDB$FIELD_NAME = 'PROD_STRICT')) then
execute statement 'update NOMENCLATURES set PROD_STRICT = (SELECT PROD_STRICT from parametres);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'ID_NORME')) then
execute statement 'ALTER TABLE ENTREES ADD ID_NORME INTEGER;';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'INVESTISSEMENT')) then
execute statement 'ALTER TABLE ENTREES ADD INVESTISSEMENT SMALLINT DEFAULT 0;';
END^
SET TERM ; ^

COMMIT;

SET TERM ^ ;

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CAT_PROD' and rf.RDB$FIELD_NAME = 'ANC_ID_CAT_PRO'))
then
execute statement 'ALTER TABLE CAT_PROD ADD ANC_ID_CAT_PRO INTEGER';
END^

EXECUTE block as
BEGIN
if (exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'INVESTISSEMENT')) then
execute statement 'update ENTREES set INVESTISSEMENT = 0 where INVESTISSEMENT is null;';
END^
SET TERM ; ^

COMMIT;

SET TERM ^ ;

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'CR_U')) then
execute statement 'ALTER TABLE ENTREES ADD CR_U NUMERIC(18,6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'CONSOMMATION')) then
execute statement 'ALTER TABLE ENTREES ADD CONSOMMATION NUMERIC(18,6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'QTE_RECETTE')) then
execute statement 'ALTER TABLE ENTREES ADD QTE_RECETTE NUMERIC(18,6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'P_U_DEVISE_N')) then
execute statement 'ALTER TABLE ENTREES ADD P_U_DEVISE_N NUMERIC(18,10);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTREES' and rf.RDB$FIELD_NAME = 'AUTRE_CHARGE_U')) then
execute statement 'ALTER TABLE ENTREES ADD AUTRE_CHARGE_U NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'INVESTISSMENT')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD INVESTISSMENT SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'CPTE_PRESTATION')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD CPTE_PRESTATION VARCHAR(12);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'PRECOMPTE_DEPOT')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD PRECOMPTE_DEPOT NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'TRANSPORT_LOCAL')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD TRANSPORT_LOCAL NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'IS_SOLDE_TRANSP')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD IS_SOLDE_TRANSP SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'ID_FAM_ACHAT')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD ID_FAM_ACHAT INTEGER;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'INVENTAIRE' and rf.RDB$FIELD_NAME = 'PRIX_VENTE')) then
execute statement 'ALTER TABLE INVENTAIRE ADD PRIX_VENTE NUMERIC(18, 2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'INVENTAIRE' and rf.RDB$FIELD_NAME = 'UNITE_PALETTE')) then
execute statement 'ALTER TABLE INVENTAIRE ADD UNITE_PALETTE NUMERIC(18, 6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'INVENTAIRE' and rf.RDB$FIELD_NAME = 'CARTONS')) then
execute statement 'ALTER TABLE INVENTAIRE ADD CARTONS NUMERIC(18, 2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'INVENTAIRE' and rf.RDB$FIELD_NAME = 'PALETTES')) then
execute statement 'ALTER TABLE INVENTAIRE ADD PALETTES NUMERIC(18, 2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'OBJET')) then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD OBJET VARCHAR(255);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'ID_FOUR_TR')) then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD ID_FOUR_TR INTEGER;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'DEPENSES')) then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD DEPENSES NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'LIGNES_PROFORMA' and rf.RDB$FIELD_NAME = 'REF_ENTREE')) then
execute statement 'ALTER TABLE LIGNES_PROFORMA ADD REF_ENTREE VARCHAR(12);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'LIGNES_PROFORMA' and rf.RDB$FIELD_NAME = 'FULL_DESIGNATION')) then
execute statement 'ALTER TABLE LIGNES_PROFORMA ADD FULL_DESIGNATION VARCHAR(800);';
END^


EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'REGL_TIERS' and rf.RDB$FIELD_NAME = 'DATE_CHEQUE')) then
execute statement 'ALTER TABLE REGL_TIERS ADD DATE_CHEQUE DATE;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'OBJET_PRESTATION')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD OBJET_PRESTATION VARCHAR(64);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'PRESTATIONS')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD PRESTATIONS VARCHAR(254);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'REGL_TIERS' and rf.RDB$FIELD_NAME = 'REMISE')) then
	execute statement 'ALTER TABLE REGL_TIERS ADD REMISE NUMERIC(18,2);';
else
	IF ( EXISTS(	SELECT 1 FROM RDB$RELATION_FIELDS RF JOIN RDB$FIELDS F ON (F.RDB$FIELD_NAME = RF.RDB$FIELD_SOURCE) WHERE rf.RDB$RELATION_NAME = 'REGL_TIERS' AND rf.RDB$FIELD_NAME = 'REMISE' AND F.RDB$FIELD_TYPE = 7  AND F.RDB$FIELD_SUB_TYPE = 0 )) then
  		execute statement 'ALTER TABLE REGL_TIERS ALTER COLUMN REMISE TYPE NUMERIC(18, 2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_TRANSFERT' and rf.RDB$FIELD_NAME = 'CHAUFFEUR')) then
execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD CHAUFFEUR VARCHAR(128);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_TRANSFERT' and rf.RDB$FIELD_NAME = 'MATRICULE')) then
execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD MATRICULE VARCHAR(32);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_TRANSFERT' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'DEPENSES' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE DEPENSES ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'RECETTES' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE RECETTES ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'REGL_TIERS' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE REGL_TIERS ADD VEROUILLE SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'VIR_FONDS' and rf.RDB$FIELD_NAME = 'VEROUILLE')) then
execute statement 'ALTER TABLE VIR_FONDS ADD VEROUILLE SMALLINT;';
END^
SET TERM  ;^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'NOTES_RECETTES')) then
          BEGIN
               execute statement 'CREATE TABLE NOTES_RECETTES (ID_NOTE_REC INTEGER NOT NULL,  NUM_NOTE_REC VARCHAR(12),  DATE_NOTE_REC DATE,  LIBELLE_NOTE_REC VARCHAR(64),  OPERATION_NOTE_REC VARCHAR(128),  MONTANT_NOTE_REC NUMERIC(18, 2));';
               EXECUTE STATEMENT 'alter table NOTES_RECETTES add constraint PK_NOTES_RECETTES primary key (ID_NOTE_REC);';
               EXECUTE STATEMENT 'CREATE GENERATOR NOTES_RECETTES_ID_NOTE_REC_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_NOTES_RECETTES_ID_NOTE_REC FOR NOTES_RECETTES ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_NOTE_REC IS NULL) THEN NEW.ID_NOTE_REC = GEN_ID(NOTES_RECETTES_ID_NOTE_REC_GEN, 1);END';
               EXECUTE STATEMENT 'CREATE PROCEDURE NOTES_RECETTES_ID_NOTE_REC_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN NEW_VALUE = GEN_ID(NOTES_RECETTES_ID_NOTE_REC_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'NOTES_DEPENSES')) then
          BEGIN
               execute statement 'CREATE TABLE NOTES_DEPENSES (ID_NOTE_DEP INTEGER NOT NULL, NUM_NOTE_DEP VARCHAR (12) CHARACTER SET NONE COLLATE NONE, DATE_NOTE_DEP DATE,LIBELLE_NOTE_DEP VARCHAR (64),OPERATION_NOTE_DEP VARCHAR (128),MONTANT_NOTE_DEP NUMERIC(18, 2));';
               EXECUTE STATEMENT 'alter table NOTES_DEPENSES add constraint PK_NOTES_DEPENSES primary key (ID_NOTE_DEP);';
               EXECUTE STATEMENT 'CREATE GENERATOR NOTES_DEPENSES_ID_NOTE_DEP_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_NOTES_DEPENSES_ID_NOTE_DEP FOR NOTES_DEPENSES ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_NOTE_DEP IS NULL) THEN NEW.ID_NOTE_DEP = GEN_ID(NOTES_DEPENSES_ID_NOTE_DEP_GEN, 1);END';
               EXECUTE STATEMENT 'CREATE PROCEDURE NOTES_DEPENSES_ID_NOTE_DEP_AI RETURNS (NEW_VALUE INTEGER) AS BEGIN NEW_VALUE = GEN_ID(NOTES_DEPENSES_ID_NOTE_DEP_GEN, 1);END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'DET_NTE_DEP')) then
          BEGIN
               execute statement 'CREATE TABLE DET_NTE_DEP (  ID_DET INTEGER NOT NULL,  DESIGNATION VARCHAR(64),  QTE NUMERIC(18, 6),  MONTANT NUMERIC(18, 2),  ID_ENTET INTEGER NOT NULL);';
               EXECUTE STATEMENT 'ALTER TABLE DET_NTE_DEP ADD CONSTRAINT PK_DET_NTE_DEP PRIMARY KEY (ID_DET);';
               EXECUTE STATEMENT 'ALTER TABLE DET_NTE_DEP ADD CONSTRAINT FK_DET_NTE_DEP FOREIGN KEY (ID_DET) REFERENCES NOTES_DEPENSES(ID_NOTE_DEP);';
               EXECUTE STATEMENT 'CREATE GENERATOR DET_NTE_DEP_ID_DET_GEN;';
               EXECUTE STATEMENT 'CREATE TRIGGER AI_DET_NTE_DEP_ID_DET FOR DET_NTE_DEP ACTIVE BEFORE INSERT POSITION 0 AS BEGIN IF (NEW.ID_DET IS NULL) THEN  NEW.ID_DET = GEN_ID(DET_NTE_DEP_ID_DET_GEN, 1);END';
               EXECUTE STATEMENT 'CREATE PROCEDURE DET_NTE_DEP_ID_DET_AI RETURNS(NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(DET_NTE_DEP_ID_DET_GEN, 1);END';
          END
END^
SET TERM ; ^


SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'CONSIGNATIONS')) then
          BEGIN
               execute statement 'CREATE TABLE CONSIGNATIONS (  ID_CONSIGNE INTEGER NOT NULL,  ID_ARTICLE INTEGER NOT NULL,  ID_ART_CONS INTEGER NOT NULL,  QUANTITE NUMERIC(18, 2));';
               EXECUTE STATEMENT 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT PK_CONSIGNATIONS PRIMARY KEY (ID_CONSIGNE);';
               EXECUTE STATEMENT 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
               EXECUTE STATEMENT 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_CON FOREIGN KEY (ID_ART_CONS) REFERENCES ARTICLES(ID_ART);';
               EXECUTE STATEMENT 'CREATE GENERATOR CONSIGNATIONS_ID_CONSIGNE_GEN;';
               EXECUTE STATEMENT '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';
               EXECUTE STATEMENT 'CREATE PROCEDURE CONSIGNATIONS_ID_CONSIGNE_AI RETURNS(   NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(CONSIGNATIONS_ID_CONSIGNE_GEN, 1); END';
          END
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$relations where rdb$relation_name = 'LIGNES_CONSIGNATION')) then
          BEGIN
               execute statement '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);';
               EXECUTE STATEMENT 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT PK_LIGNES_CONSIGNATION PRIMARY KEY (ID_LC);';
               EXECUTE STATEMENT 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
               EXECUTE STATEMENT 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_TIERS FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);';
               EXECUTE STATEMENT 'CREATE GENERATOR LIGNES_CONSIGNATION_ID_LC_GEN;';
               EXECUTE STATEMENT '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';
               EXECUTE STATEMENT 'CREATE PROCEDURE LIGNES_CONSIGNATION_ID_LC_AI RETURNS(   NEW_VALUE INTEGER) AS BEGIN  NEW_VALUE = GEN_ID(LIGNES_CONSIGNATION_ID_LC_GEN, 1); END';
          END
  END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from  CODE_TAP)) then
          BEGIN
               execute statement '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, ''-'');';
               EXECUTE STATEMENT '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, ''-'');';
               EXECUTE STATEMENT 'INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (3, ''C1A13'', ''Affaires sans réfaction'', 2, 0, 0, ''-'');';
               EXECUTE STATEMENT 'INSERT INTO CODE_TAP (ID_TAP, CODE, LIBELLE, TAUX, TAUX_REFACT, ID_CPTE, CPTE)VALUES (4, ''C1A14'', ''Affaires exonérées'', 0, 0, 0, ''-'');';
               EXECUTE STATEMENT '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, ''-'');';
          END
  END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from  CODE_TVA)) then
          BEGIN
          		EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (4, ''E3B14'', ''Actes médicaux'', 9, 0, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (5, ''E3B15'', ''Commissionnaires et courtiers'', 9, 0, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (6, ''E3B16'', ''Fourniture d''''énergie'', 9, 0, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (9, ''E3B24'', ''Professions libérales'', 19, 0, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (13, ''E3B25'', ''Opérations de banques et d''''assurances'', 19, 0, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (15, ''E3B28'', ''Autres prestations de services'', 19, 0, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (16, ''E3B31'', ''Débits de boissons'', 19, 0, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (19, ''E3B34'', ''Tabacs et allumettes'', 19, 0, ''-'');';
				EXECUTE STATEMENT '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, ''-'');';
				EXECUTE STATEMENT 'INSERT INTO CODE_TVA (ID_TVA, CODE, LIBELLES, TAUX, ID_CPTE, CPTE)VALUES (21, ''E3B37'', ''Consommations sur place'', 19, 0, ''-'');';
          END
  END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'G50' and rf.RDB$FIELD_NAME = 'TYPE_G50'))
then
execute statement 'ALTER TABLE G50 ADD TYPE_G50 SMALLINT';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'BC_F' and rf.RDB$FIELD_NAME = 'P_U_6'))
then
execute statement 'ALTER TABLE BC_F ADD P_U_6 DOUBLE PRECISION';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'BC_F' and rf.RDB$FIELD_NAME = 'DESIGNATION'))
then
execute statement 'ALTER TABLE BC_F ADD DESIGNATION VARCHAR(128)';
END^
SET TERM ; ^

SET TERM ^ ;

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'SERVICE_GESTION'))
then
execute statement 'ALTER TABLE CLIENTS ADD SERVICE_GESTION VARCHAR(64)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'ID_CHAUFFEUR'))
then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD ID_CHAUFFEUR INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'ID_MATRICULE'))
then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD ID_MATRICULE INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'STATISTIQUE')) then
execute statement 'ALTER TABLE ARTICLES ADD STATISTIQUE  SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'CHARGE_U_PROD')) then
execute statement 'ALTER TABLE ARTICLES ADD CHARGE_U_PROD  NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'PROD_POIDS')) then
execute statement 'ALTER TABLE ARTICLES ADD PROD_POIDS  SMALLINT;';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'CODE_ORIGINE')) then
execute statement 'ALTER TABLE ARTICLES ADD CODE_ORIGINE  VARCHAR (22);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'QTE_RECETTE')) then
execute statement 'ALTER TABLE ARTICLES ADD QTE_RECETTE  NUMERIC(18,6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'DESIGNATION_AR')) then
execute statement 'ALTER TABLE ARTICLES ADD DESIGNATION_AR varchar(128);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'P_U_F')) then
execute statement 'ALTER TABLE ARTICLES ADD P_U_F NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists(select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'TX_TVA_F')) then
execute statement 'ALTER TABLE ARTICLES ADD TX_TVA_F NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'PU_TR'))
then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD PU_TR  NUMERIC(18, 4)';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'MONTANT_TR')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD MONTANT_TR NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'PU_TR'))
then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD PU_TR NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'QTE_TR')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD QTE_TR NUMERIC(18,6);';
END^

EXECUTE block as
BEGIN
if (not exists( select 1 from RDB$RELATION_FIELDS rf where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'ID_FOUR_TR')) then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD ID_FOUR_TR NUMERIC(18,2);';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'DESTINATION' and rf.RDB$FIELD_NAME = 'PU_TR'))
then
execute statement 'ALTER TABLE DESTINATION ADD PU_TR  NUMERIC(18, 4)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'NOMENCLATURES' and rf.RDB$FIELD_NAME = 'ARRONDI'))
then
execute statement 'ALTER TABLE NOMENCLATURES ADD ARRONDI SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'MIN_ROT_CREANCES'))
then
execute statement 'ALTER TABLE ARTICLES ADD MIN_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'MID_ROT_CREANCES'))
then
execute statement 'ALTER TABLE ARTICLES ADD MID_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'MAX_ROT_CREANCES'))
then
execute statement 'ALTER TABLE ARTICLES ADD MAX_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'MIN_ROT_CREANCES'))
then
execute statement 'ALTER TABLE CLIENTS ADD MIN_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'MID_ROT_CREANCES'))
then
execute statement 'ALTER TABLE CLIENTS ADD MID_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CLIENTS' and rf.RDB$FIELD_NAME = 'MAX_ROT_CREANCES'))
then
execute statement 'ALTER TABLE CLIENTS ADD MAX_ROT_CREANCES SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'FB_REPLICA_ARCHIVE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD FB_REPLICA_ARCHIVE VARCHAR(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'NOM_DOSSIER'))
then
execute statement 'ALTER TABLE PARAMETRES ADD NOM_DOSSIER VARCHAR(128)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'CODE_DOSSIER'))
then
execute statement 'ALTER TABLE PARAMETRES ADD CODE_DOSSIER VARCHAR(12)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ORDER_ASC'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ORDER_ASC SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'AUTRES_CHARGES_LIB'))
then
execute statement 'ALTER TABLE PARAMETRES ADD AUTRES_CHARGES_LIB VARCHAR(32)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ARRONDI_TIMBRE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ARRONDI_TIMBRE smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'CLASSE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD CLASSE smallint';
END^

EXECUTE block as
BEGIN
if (exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'VEROUILLE'))
then
execute statement 'ALTER TABLE PARAMETRES DROP VEROUILLE';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'TAXE_ACHAT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD TAXE_ACHAT smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'GEST_QUALITE'))
then
execute statement 'ALTER TABLE PARAMETRES ADD GEST_QUALITE smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'NEW_SQL'))
then
execute statement 'ALTER TABLE PARAMETRES ADD NEW_SQL smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'TRANSPORT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD TRANSPORT smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'SORTIES' and rf.RDB$FIELD_NAME = 'CARTON_PALETTE'))
then
execute statement 'ALTER TABLE SORTIES ADD CARTON_PALETTE INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'SORTIES' and rf.RDB$FIELD_NAME = 'PIECE_CARTON'))
then
execute statement 'ALTER TABLE SORTIES ADD PIECE_CARTON INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'SORTIES' and rf.RDB$FIELD_NAME = 'ID_QUALITE'))
then
execute statement 'ALTER TABLE SORTIES ADD ID_QUALITE INTEGER';
END^


EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'SORTIES' and rf.RDB$FIELD_NAME = 'PROD_STRICT'))
then
execute statement 'ALTER TABLE SORTIES ADD PROD_STRICT SMALLINT';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'SORTIES' and rf.RDB$FIELD_NAME = 'DISTANCE'))
then
execute statement 'ALTER TABLE SORTIES ADD DISTANCE NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'NUM_CONTRAT'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD NUM_CONTRAT VARCHAR(32) ';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'DATE_CONTRAT'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD DATE_CONTRAT DATE';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'A_S_COUT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD A_S_COUT smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'PROD_STRICT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD PROD_STRICT smallint';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ID_DEPOT_ACHAT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ID_DEPOT_ACHAT INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ID_DEPOT_PF'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ID_DEPOT_PF INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PARAMETRES' and rf.RDB$FIELD_NAME = 'ID_DEPOT_MT'))
then
execute statement 'ALTER TABLE PARAMETRES ADD ID_DEPOT_MT INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ARTICLES' and rf.RDB$FIELD_NAME = 'ID_CODE_TAP'))
then
execute statement 'ALTER TABLE ARTICLES ADD ID_CODE_TAP INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'TAUX_EXTRACTION' and rf.RDB$FIELD_NAME = 'ID_NORME'))
then
execute statement 'ALTER TABLE TAUX_EXTRACTION ADD ID_NORME INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CAMION' and rf.RDB$FIELD_NAME = 'TARE'))
then
execute statement 'ALTER TABLE CAMION ADD TARE NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'TARE'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD TARE NUMERIC(18,2)';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'DATE_TARE'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD DATE_TARE DATE';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'BRUTE'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD BRUTE NUMERIC(18,2)';
END^



EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'CAMION' and rf.RDB$FIELD_NAME = 'DATE_TARE'))
then
execute statement 'ALTER TABLE CAMION ADD DATE_TARE DATE';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PROFORMA' and rf.RDB$FIELD_NAME = 'ID_DEST'))
then
execute statement 'ALTER TABLE PROFORMA ADD ID_DEST INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PROFORMA' and rf.RDB$FIELD_NAME = 'ID_MAG'))
then
execute statement 'ALTER TABLE PROFORMA ADD ID_MAG INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PROFORMA' and rf.RDB$FIELD_NAME = 'ID_CHAUF'))
then
execute statement 'ALTER TABLE PROFORMA ADD ID_CHAUF INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'PROFORMA' and rf.RDB$FIELD_NAME = 'ID_CAMION'))
then
execute statement 'ALTER TABLE PROFORMA ADD ID_CAMION INTEGER';
END^

EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_SORTIES' and rf.RDB$FIELD_NAME = 'ID_CLIENT_DEST'))
then
execute statement 'ALTER TABLE ENTETE_SORTIES ADD ID_CLIENT_DEST INTEGER';
END^


EXECUTE block as
BEGIN
if (not exists(
select 1 from RDB$RELATION_FIELDS rf
where rf.RDB$RELATION_NAME = 'ENTETE_ENTREES' and rf.RDB$FIELD_NAME = 'ID_DEPART'))
then
execute statement 'ALTER TABLE ENTETE_ENTREES ADD ID_DEPART INTEGER';
END^
SET TERM ; ^

COMMIT;
/*-----------------------------------*/


/*-----------------------------------*/

SET TERM ^ ;

CREATE OR ALTER PROCEDURE VENTES_CLIENTS_PRODUITS (
  DU           DATE,
  AU           DATE
) RETURNS (
  DENOMINATION VARCHAR(128),
  ACTIVITE     VARCHAR(128),
  ADRESSE      VARCHAR(256),
  MNT1         NUMERIC(18,2),
  MNT2         NUMERIC(18,2),
  MNT3         NUMERIC(18,2),
  MNT4         NUMERIC(18,2),
  MNT5         NUMERIC(18,2),
  MNT6         NUMERIC(18,2),
  MNT7         NUMERIC(18,2),
  MNT8         NUMERIC(18,2),
  MNT9         NUMERIC(18,2),
  MNT10        NUMERIC(18,2),
  MNT11        NUMERIC(18,2),
  MNT12        NUMERIC(18,2),
  MNT13        NUMERIC(18,2),
  MNT14        NUMERIC(18,2),
  MNT15        NUMERIC(18,2),
  MNT16        NUMERIC(18,2),
  MNT17        NUMERIC(18,2),
  MNT18        NUMERIC(18,2),
  MNT19        NUMERIC(18,2),
  MNT20        NUMERIC(18,2),
  MNT21        NUMERIC(18,2),
  MNT22        NUMERIC(18,2),
  MNT23        NUMERIC(18,2),
  MNT24        NUMERIC(18,2),
  MNT25        NUMERIC(18,2),
  MNT26        NUMERIC(18,2),
  MNT27        NUMERIC(18,2),
  MNT28        NUMERIC(18,2),
  MNT29        NUMERIC(18,2),
  MNT30        NUMERIC(18,2),
  MNT31        NUMERIC(18,2),
  MNT32        NUMERIC(18,2),
  MNT33        NUMERIC(18,2),
  MNT34        NUMERIC(18,2),
  MNT35        NUMERIC(18,2),
  MNT36        NUMERIC(18,2),
  MNT37        NUMERIC(18,2),
  MNT38        NUMERIC(18,2),
  MNT39        NUMERIC(18,2),
  MNT40        NUMERIC(18,2),
  MNT41        NUMERIC(18,2)
)
AS
declare variable REF INTEGER;
  declare variable REF1 INTEGER;
  declare variable REF2 INTEGER;
  declare variable REF3 INTEGER;
  declare variable REF4 INTEGER;
  declare variable REF5 INTEGER;
  declare variable REF6 INTEGER;
  declare variable REF7 INTEGER;
  declare variable REF8 INTEGER;
  declare variable REF9 INTEGER;
  declare variable REF10 INTEGER;
  declare variable REF11 INTEGER;
  declare variable REF12 INTEGER;
  declare variable REF13 INTEGER;
  declare variable REF14 INTEGER;
  declare variable REF15 INTEGER;
  declare variable REF16 INTEGER;
  declare variable REF17 INTEGER;
  declare variable REF18 INTEGER;
  declare variable REF19 INTEGER;
  declare variable REF20 INTEGER;

  declare variable REF21 INTEGER;
  declare variable REF22 INTEGER;
  declare variable REF23 INTEGER;
  declare variable REF24 INTEGER;
  declare variable REF25 INTEGER;
  declare variable REF26 INTEGER;
  declare variable REF27 INTEGER;
  declare variable REF28 INTEGER;
  declare variable REF29 INTEGER;
  declare variable REF30 INTEGER;
  declare variable REF31 INTEGER;
  declare variable REF32 INTEGER;
  declare variable REF33 INTEGER;
  declare variable REF34 INTEGER;
  declare variable REF35 INTEGER;
  declare variable REF36 INTEGER;
  declare variable REF37 INTEGER;
  declare variable REF38 INTEGER;
  declare variable REF39 INTEGER;
  declare variable REF40 INTEGER;

  declare variable I INTEGER;
  declare variable ROW_MAX INTEGER;
BEGIN
  SUSPEND;
END^
SET TERM ; ^


SET TERM ^ ;

CREATE OR ALTER PROCEDURE VENTES_CLIENTS_PRODUITS_VAL (
  DU           DATE,
  AU           DATE
) RETURNS (
  DENOMINATION VARCHAR(128),
  ACTIVITE     VARCHAR(128),
  ADRESSE      VARCHAR(256),
  MNT1         NUMERIC(18,2),
  MNT2         NUMERIC(18,2),
  MNT3         NUMERIC(18,2),
  MNT4         NUMERIC(18,2),
  MNT5         NUMERIC(18,2),
  MNT6         NUMERIC(18,2),
  MNT7         NUMERIC(18,2),
  MNT8         NUMERIC(18,2),
  MNT9         NUMERIC(18,2),
  MNT10        NUMERIC(18,2),
  MNT11        NUMERIC(18,2),
  MNT12        NUMERIC(18,2),
  MNT13        NUMERIC(18,2),
  MNT14        NUMERIC(18,2),
  MNT15        NUMERIC(18,2),
  MNT16        NUMERIC(18,2),
  MNT17        NUMERIC(18,2),
  MNT18        NUMERIC(18,2),
  MNT19        NUMERIC(18,2),
  MNT20        NUMERIC(18,2),
  MNT21        NUMERIC(18,2),
  MNT22        NUMERIC(18,2),
  MNT23        NUMERIC(18,2),
  MNT24        NUMERIC(18,2),
  MNT25        NUMERIC(18,2),
  MNT26        NUMERIC(18,2),
  MNT27        NUMERIC(18,2),
  MNT28        NUMERIC(18,2),
  MNT29        NUMERIC(18,2),
  MNT30        NUMERIC(18,2),
  MNT31        NUMERIC(18,2),
  MNT32        NUMERIC(18,2),
  MNT33        NUMERIC(18,2),
  MNT34        NUMERIC(18,2),
  MNT35        NUMERIC(18,2),
  MNT36        NUMERIC(18,2),
  MNT37        NUMERIC(18,2),
  MNT38        NUMERIC(18,2),
  MNT39        NUMERIC(18,2),
  MNT40        NUMERIC(18,2),
  MNT41        NUMERIC(18,2)
)
AS
declare variable REF INTEGER;
  declare variable REF1 INTEGER;
  declare variable REF2 INTEGER;
  declare variable REF3 INTEGER;
  declare variable REF4 INTEGER;
  declare variable REF5 INTEGER;
  declare variable REF6 INTEGER;
  declare variable REF7 INTEGER;
  declare variable REF8 INTEGER;
  declare variable REF9 INTEGER;
  declare variable REF10 INTEGER;
  declare variable REF11 INTEGER;
  declare variable REF12 INTEGER;
  declare variable REF13 INTEGER;
  declare variable REF14 INTEGER;
  declare variable REF15 INTEGER;
  declare variable REF16 INTEGER;
  declare variable REF17 INTEGER;
  declare variable REF18 INTEGER;
  declare variable REF19 INTEGER;
  declare variable REF20 INTEGER;

  declare variable REF21 INTEGER;
  declare variable REF22 INTEGER;
  declare variable REF23 INTEGER;
  declare variable REF24 INTEGER;
  declare variable REF25 INTEGER;
  declare variable REF26 INTEGER;
  declare variable REF27 INTEGER;
  declare variable REF28 INTEGER;
  declare variable REF29 INTEGER;
  declare variable REF30 INTEGER;
  declare variable REF31 INTEGER;
  declare variable REF32 INTEGER;
  declare variable REF33 INTEGER;
  declare variable REF34 INTEGER;
  declare variable REF35 INTEGER;
  declare variable REF36 INTEGER;
  declare variable REF37 INTEGER;
  declare variable REF38 INTEGER;
  declare variable REF39 INTEGER;
  declare variable REF40 INTEGER;

  declare variable I INTEGER;
  declare variable ROW_MAX INTEGER;
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_VENTES_DCP_PRODUITS(
  DU DATE,
  AU DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  R_C VARCHAR(50),
  ADRESSE VARCHAR(255),
  WILAYA VARCHAR(64),
  PRODUITS VARCHAR(128),
  QUANTITES NUMERIC(18, 2),
  MONTANTS NUMERIC(18, 2),
  NUM_FACTS VARCHAR(5000),
  ID_ARTICLE   INTEGER)
AS
DECLARE VARIABLE NUM_FACT VARCHAR(12);
BEGIN
  SUSPEND;
END^
SET TERM ; ^

 SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_VENTES_DCP_FAMILLES(
  DU DATE,
  AU DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  R_C VARCHAR(50),
  ADRESSE VARCHAR(255),
  WILAYA VARCHAR(64),
  FAMILLES VARCHAR(64),
  QUANTITES NUMERIC(18, 2),
  MONTANTS NUMERIC(18, 2),
  NUM_FACTS VARCHAR(10000),
  ID_FAMILLE   INTEGER)
AS
DECLARE VARIABLE NUM_FACT VARCHAR(12);
BEGIN
  SUSPEND;
END^
SET TERM ; ^
/*------------------------------------------------*/



 SET TERM ^ ;

CREATE or alter PROCEDURE DASHBOARD_01(
  D1 DATE,
  D2 DATE)
RETURNS(
  DATE_OP DATE,
  CAPITAM_MP NUMERIC(18, 2),
  CAPITAM_PF NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  VENTES NUMERIC(18, 2),
  CREACES NUMERIC(18, 2),
  DETTES NUMERIC(18, 2)
  )
AS
DECLARE VARIABLE ID_MAG_MP  iNTEGER;
DECLARE VARIABLE ID_MAG_PF  iNTEGER;
BEGIN
  SUSPEND;
END^
SET TERM ; ^

 SET TERM ^ ;

CREATE OR ALTER PROCEDURE MVT_DU_AU (
  DU         DATE,
  AU         DATE
) RETURNS (
  ID_MVT     INTEGER,
  ID_ARTICLE INTEGER,
  REF_ENTREE VARCHAR(12),
  ID_MAGAZIN INTEGER,
  DATE_OPER  DATE,
  COUT       NUMERIC(18,2),
  QTE_INI    NUMERIC(18,6),
  QTE_ENT    NUMERIC(18,6),
  QTE_SRT    NUMERIC(18,6),
  OPER       SMALLINT
)
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE RELEVE_CLIENT_TIME(
  ID        INTEGER,
  D1        TIMESTAMP,
  D2        TIMESTAMP
) RETURNS (
  ID_CLIENT INTEGER,
  NUM       VARCHAR(12),
  DATE_OPER TIMESTAMP,
  LIBELLE   VARCHAR(128),
  VENTE     NUMERIC(18,2),
  REGLT     NUMERIC(18,2),
  SOLDE     NUMERIC(18,2),
  TP_OP     INTEGER
)
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE NUM_NOTE_DEP
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE NUM_NOTE_REC
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE CONSOMMATION_DEPOT_REF_ENTREE(
  D1 DATE,
  D2 DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_ART INTEGER,
  REF_ENTREE VARCHAR(12),
  MATIERE INTEGER,
  ARTICLE VARCHAR(128),
  IMPORT INTEGER,
  FP SMALLINT,
  GROS SMALLINT,
  QTE NUMERIC(18, 6),
  COUT NUMERIC(18, 2),
  VALEUR NUMERIC(18, 2),
  FAMILLE VARCHAR(128))
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);
DECLARE VARIABLE ID_FAMILLE INTEGER;
BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               sorties.REF_ENTREE,
               ARTICLES.designation,
               ARTICLES.matiere,
               ARTICLES.importation,
               sorties.gros,
               SORTIES.f_p,
               ARTICLES.ID_FAMILLE
        FROM sorties
        INNER 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
        AND sorties.DESTOCKER = 1
        INTO :ID_ART, :REF_ENTREE, :ARTICLE, :MATIERE, :IMPORT, :GROS, :FP, :ID_FAMILLE

     do
     BEGIN
          SELECT
                FAMILLES.FAMILLE
          FROM FAMILLES
          WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
          INTO :FAMILLE;

          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
          AND SORTIES.REF_ENTREE = :REF_ENTREE
          AND sorties.DESTOCKER = 1
          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
          AND SORTIES.REF_ENTREE = :REF_ENTREE
          AND sorties.DESTOCKER = 1
          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^
SET TERM  ;^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE DASHBOARD(
  DU DATE,
  AU DATE)
RETURNS(
  ACHAT_SERVICES NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  AVOIRS_ACHATS NUMERIC(18, 2),
  FACT_VENTE NUMERIC(18, 2),
  BL_VENTE NUMERIC(18, 2),
  AVOIRS_VENTE NUMERIC(18, 2),
  RETOURS NUMERIC(18, 2),
  CONSOMATION NUMERIC(18, 2),
  RGLT_FOUR NUMERIC(18, 2),
  REGL_CLIENT NUMERIC(18, 2),
  DEPENSES NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE SOLDE_COMPTE(
  ID_CPTE INTEGER,
  AU DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE NUM_CONSIGNE
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^



SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE GET_DIGIT_ONLY (
    IPARAM VARCHAR (32))
RETURNS (
    OPARAM VARCHAR (32))
AS declare variable I integer;
begin
  suspend;
end^

SET TERM ; ^


SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ARCHIVES_ID_ARCHIVE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ARTICLES_ID_ART_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

/* Definition for the `UPDATE_COUT_REF_ENTREE` procedure :  */
SET TERM ^ ;

CREATE OR ALTER PROCEDURE UPDATE_COUT_REF_ENTREE
RETURNS (
  ID_ARTICLE INTEGER,
  REF_ENTREE VARCHAR(12),
  COUT       NUMERIC(18,2),
  DATE_PIECE DATE
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE BALANCE_STOCK_MENSUEL (
  MATIERE SmallInt)
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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE BASE_CALCUL_PAMP (
  D          DATE,
  ID_ART     INTEGER,
  ID_MAGAZIN INTEGER,
  ID_OPER INTEGER,
  OPER SMALLINT
) RETURNS (
  ID_MVT     INTEGER,
  DATE_OPER  DATE,
  P_A_M_P    FLOAT
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


/* Definition for the `BASE_CALCUL_PAMP_ALL` procedure :  */
SET TERM ^ ;

CREATE OR ALTER PROCEDURE BASE_CALCUL_PAMP_ALL (
  D          DATE
) RETURNS(
  ANC_ID_ART INTEGER,
  ANC_ID_MAG INTEGER,
  ID_MVT INTEGER,
  ID_ARTICLE INTEGER,
  ID_MAGAZIN INTEGER,
  DATE_OPER DATE,
  EX_STOCK NUMERIC(18, 6),
  QUANTITE NUMERIC(18, 6),
  STOCK NUMERIC(18, 6),
  COUT NUMERIC(18, 2),
  VALEUR Double Precision,
  CURRENT_PAW NUMERIC(18, 2),
  P_A_M_P FLOAT,
  ID_OPER INTEGER,
  OPER SMALLINT)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE BC_F_ID_BC_F_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CA_TVA(
  ID INTEGER,
  TAUX NUMERIC(18, 2))
RETURNS(
  CA NUMERIC(18, 2),
  TVA NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CALCUL_TIMBRE (
  ASSIETTE NUMERIC(18,2),
  TAUX     NUMERIC(18,2),
  VAL_MIN  NUMERIC(18,2),
  VAL_MAX  NUMERIC(18,2)
) RETURNS (
  TIMBRE   NUMERIC(18,2)
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CAMION_ID_CAMION_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CATALOGUE_ID_CAT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CHAUFFEURS_ID_CHAUFFEUR_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CHIFRRE_AFFAIRES
RETURNS(
  MOIS INTEGER,
  NOM_MOIS VARCHAR(20),
  CA_MOIS NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CLIENTS_ID_CLIENT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CODE_TAP_ID_TAP_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE CODE_TVA_ID_TVA_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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 NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  REMISE NUMERIC(18, 2),
  CPTE_TIER VARCHAR(12),
  MNT_TIER NUMERIC(18, 2),
  TIERS VARCHAR(128),
  ID_MAG Integer)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  ID_MAG INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_CONSOM_ANNUEL
RETURNS(
  CPTE_STOCK VARCHAR(12),
  CPTE_CONSOM VARCHAR(12),
  CONSOM NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_CONSOM_ANNUEL_DEPOT (
ID_MAG Integer)
RETURNS(
  CPTE_STOCK VARCHAR(12),
  CPTE_CONSOM VARCHAR(12),
  CONSOM NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_CONSOM_MOIS
RETURNS(
  MOIS SMALLINT,
  CPTE_STOCK VARCHAR(12),
  CPTE_CONSOM VARCHAR(12),
  CONSOM NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_CONSOM_MOIS_DEPOT(ID_MAG Integer)
RETURNS(
  MOIS SMALLINT,
  CPTE_STOCK VARCHAR(12),
  CPTE_CONSOM VARCHAR(12),
  CONSOM NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  CODE_TIERS VARCHAR(12),
  TIERS VARCHAR(128))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_ENTREE_STOCK_DEPOT(
  DU DATE,
  AU DATE,
  ID_MAG INTEGER)
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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPA_REGL_TIERS(
  DU DATE,
  AU DATE)
RETURNS(
   NUM_REGL VARCHAR(12),
  DATE_REGL DATE,
  CODE_TIERS VARCHAR(12),
  CPTE_CLIENT VARCHAR(12),
  CPTE_TRESOR VARCHAR(12),
  NUM_CHEQUE VARCHAR(12),
  MONTANT NUMERIC(18, 2),
  TYPE_REG SMALLINT,
  TIERS VARCHAR(128))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  ID_MAG INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE COMPTES_TRESORIE_ID_CPTE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  FAMILLE VARCHAR(128))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  FAMILLE VARCHAR(128))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE DEPENSES_ID_DEP_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE DESTINATION_ID_DESTINATION_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE DEVISES_ID_DEVISE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE EMPLACEMENTS_ID_EMPL_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE EN_STOCK(
  ID_ART INTEGER,
  ID_MAG INTEGER,
  DATE_OP DATE)
RETURNS(
  STOCK NUMERIC(18, 6))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ENTET_BC_F_ID_ENT_BD_F_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ENTETE_ENTREES_ID_ENT_ENT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ENTETE_SORTIES_ID_ENT_SORT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ENTETE_TRANSFERT_ID_ENTETE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ENTREES_ID_ENTREE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_104(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_104_MAGASIN(
  D1 DATE,
  D2 DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_104_GROS(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_104_PART(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_CREANCES(
  D1 DATE,
  D2 DATE)
RETURNS(
  CODE_TIERS   VARCHAR(12),
  ID_TIER      INTEGER,
  DENOMINATION VARCHAR(128),
  SOLDE_INI    NUMERIC(18,2),
  CREANCE      NUMERIC(18,2),
  REGLEMENT    NUMERIC(18,2),
  ECART        NUMERIC(18,2),
  ROTATION     INTEGER
  )
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;
CREATE OR ALTER PROCEDURE ETAT_DETTE_CREANCES (
  D1           DATE,
  D2           DATE
) RETURNS (
  ID_TIER          INTEGER,
  DENOMINATION     VARCHAR(128),
  SOLDE_INI_FOURN  NUMERIC(18,2),
  ACHATS           NUMERIC(18,2),
  REGLEMENT_FOURN        NUMERIC(18,2),
  ECART_FOURN      NUMERIC(18,2),
  SOLDE_INI_CLIENT    NUMERIC(18,2),
  CREANCE      NUMERIC(18,2),
  REGLEMENT_CLIENT    NUMERIC(18,2),
  ECART_CLIENT       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_FOURN NUMERIC(18, 2);
DECLARE VARIABLE FACT_DD NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_ACHAT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_TRANS NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_IMPOR NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIR_F NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_DEP NUMERIC(18, 2);
DECLARE VARIABLE AV_COM NUMERIC(18, 2);
DECLARE variable VENTE_ANT NUMERIC (18, 2);
DECLARE variable REGLT_ANT_CLIENT NUMERIC (18, 2);
DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
DECLARE variable TOTAL_AVOIR_C NUMERIC (18, 2);

BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_DETTES_LOCALES(
  D1 DATE,
  D2 DATE)
RETURNS(
  CODE_TIERS   VARCHAR(12),
  ID_TIER INTEGER,
  DENOMINATION VARCHAR(128),
  SOLDE_INI NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  REGLEMENT NUMERIC(18, 2),
  ECART NUMERIC(18, 2),
  ROTATION     INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_STOCK(
D DATE)
RETURNS(
  ID_ARTICLE INTEGER,
  ID_MAGAZIN 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),
  SUM_INITIALE NUMERIC(18, 6),
  SUM_ENTREE NUMERIC(18, 6),
  SUM_SORTIE NUMERIC(18, 6),
  STOCK_CARTON NUMERIC(18, 6),
  STOCK_PALETTE NUMERIC(18, 6),
  STOCK NUMERIC(18, 6),
  STOCK_MIN NUMERIC(18, 2),
  SUM_VAL_INI NUMERIC(18, 2),
  SUM_VAL_ENT NUMERIC(18, 2),
  SUM_VAL_SRT NUMERIC(18, 2),
  VALEUR_STOCK NUMERIC(18, 2),
  PRIX_HT NUMERIC(18, 2),
  PRIX_TTC NUMERIC(18, 2),
  VALEUR_HT NUMERIC(18, 2),
  VALEUR_TTC NUMERIC(18, 2),
  SMILET SMALLINT,
  TAUX_TVA NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE ETAT_STOCK_DEPOT(
ID_MAGAZIN INTEGER,
D DATE)
RETURNS(
  ID_ARTICLE 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),
  SUM_INITIALE NUMERIC(18, 6),
  SUM_ENTREE NUMERIC(18, 6),
  SUM_SORTIE NUMERIC(18, 6),
  STOCK_CARTON NUMERIC(18, 6),
  STOCK_PALETTE NUMERIC(18, 6),
  STOCK NUMERIC(18, 6),
  STOCK_MIN NUMERIC(18, 2),
  SUM_VAL_INI NUMERIC(18, 2),
  SUM_VAL_ENT NUMERIC(18, 2),
  SUM_VAL_SRT NUMERIC(18, 2),
  VALEUR_STOCK NUMERIC(18, 2),
  PRIX_HT NUMERIC(18, 2),
  PRIX_TTC NUMERIC(18, 2),
  VALEUR_HT NUMERIC(18, 2),
  VALEUR_TTC NUMERIC(18, 2),
  TAUX_TVA NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FACT_ANT_ID_FACT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FACT_DD_JOINTE_ID_FACT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FACT_TAUX_TVA_DEPOT(
  ID_MAGAZIN INTEGER,
  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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FAMILLE_ID_ART(
  ID INTEGER)
RETURNS(
  FAMILLE VARCHAR(128))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FAMILLES_CHARGES_ID_FAM_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FAMILLES_ID_FAMILLE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FAMILLES_RECETTES_ID_FAM_REC_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FOURNISSEURS_ID_FOURN_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE FRAUDEURS_ID_FRAUD_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE G50_ID_G50_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE INV_DUAU_DPT_REF_ENTREE(
  ID_MAGAZIN INTEGER,
  DU DATE,
  AU DATE)
RETURNS(
  ID_ART INTEGER,
  REF VARCHAR(20),
  DATE_ENTREE DATE,
  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),
  VAL_VTE     NUMERIC(18,2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  VAL_VTE     NUMERIC(18,2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;
CREATE OR 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),
  VAL_VTE     NUMERIC(18,2))
AS
BEGIN
  SUSPEND;
END^
SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE INVENTAIRE_DU_AU (
  DU            DATE,
  AU            DATE
) RETURNS (
    ID_ART        INTEGER,
  ID_MAGAZIN    INTEGER,
  REF           VARCHAR(20),
  DESIGNATION   VARCHAR(128),
  UNITE         VARCHAR(12),
  FAMILLE       VARCHAR(64),
  PAMP_INI      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),
  NBRE_CARTON   NUMERIC(18,6),
  NBRE_PALETTE  NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,6),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILTY        SMALLINT
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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),
  PAMP_INI      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),
  NBRE_CARTON   NUMERIC(18,6),
  NBRE_PALETTE  NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,6),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILTY        SMALLINT,
  VAL_VTE     NUMERIC(18,2)
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE INVENTAIRE_DU_AU_REF_ENTREE(
  DU DATE,
  AU DATE)
RETURNS(
  ID_ART INTEGER,
  REF VARCHAR(20),
  DATE_ENTREE DATE,
  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),
  VAL_VTE     NUMERIC(18,2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE INVENTAIRE_ID_INVENTAIRE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;
CREATE OR ALTER PROCEDURE INVENTAIRES_REF_ENTREE(
  D DATE)
RETURNS(
  ID_ART INTEGER,
  ID_MAGAZIN    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
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
CREATE OR ALTER PROCEDURE GET_COUT_REF_ENTREE (
  ID_ARTICLE      INTEGER,
  REF_ENTREE      VARCHAR(12),
  ID_MAGAZIN      INTEGER,
  DATE_OPER       DATE
) RETURNS (
  COUT_REF_ENTREE NUMERIC(18,2)
)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^


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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE LIGNES_PROFORMA_ID_L_PROF_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE LIGNES_TRANSFERT_ID_LIGNE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE LST_CHRGS_ID_LST_CHRG_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE MAGAZINS_ID_MAG_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE MARQUES_ID_MARQUE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE MAX_DATE_ENTREES(
  NATURE INTEGER)
RETURNS(
  DTE DATE)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE MAX_DATE_SORTIES(
  NATURE INTEGER)
RETURNS(
  DTE DATE)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NETOYER_ENTREES
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NETOYER_SORTIES
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NOMENCLATURES_ID_NOM_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_BC_F
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_DEP
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_DOS_IMPORTATION(
  NATURE INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_ENTET_ENTREE(
  NATURE INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_ENTET_SORTIE(
  NATURE INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_ENTET_SORTIE_MAG(
  NATURE INTEGER,
  ID_MAGASIN INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_ENTET_SORTIE_MAGAZIN(
  NATURE INTEGER,
  ID_MAGAZIN INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_PROFORMA
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_REC
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_RGLT(
  NATURE SMALLINT)
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_SERIES_ID_NUM_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_TRANSFERT
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE NUM_VIR_FONDS
RETURNS(
  NUM INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE PAMP(
  ID_ARTICLE INTEGER,
  D DATE)
RETURNS(
  P_PAMP NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE PAMP_DEPOT(
  ID_ARTICLE INTEGER,
  ID_MAGAZIN INTEGER,
  D DATE)
RETURNS(
  P_PAMP NUMERIC(18, 2))
AS
DECLARE VARIABLE MATIERE SMALLINT;
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE PARAMETRES_ID_PARAM_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE PROFORMA_ID_PROF_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RECAP_FACTURE_VENTE(
  D1 DATE,
  D2 DATE,
  NATURE INTEGER)
RETURNS(
  NBRE INTEGER,
  ESPECE NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  BASE_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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RECAP_FACTURE_VENTE_DEPOT(
  ID_MAG INTEGER,
  D1 DATE,
  D2 DATE,
  NATURE INTEGER)
RETURNS(
  NBRE INTEGER,
  ESPECE NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  BASE_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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RECAP_VENTE_TVA_DEPOT(
  ID_MAG INTEGER,
  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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RECETTES_ID_REC_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE REGL_TIERS_ID_REGL_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE REGLEMENTS_ID_REGLEMENT_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_CLIENT(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  NUM VARCHAR(12),
  DATE_OPER DATE,
  LIBELLE VARCHAR(256),
  VENTE NUMERIC(18, 2),
  REGLT NUMERIC(18, 2),
  SOLDE NUMERIC(18, 2),
  TP_OP INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_CLIENT_DETAIL(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  MATRICULE VARCHAR(128),
  CHAUFFEUR VARCHAR(128),
  DATE_OPER DATE,
  LIBELLE VARCHAR(256),
  PRODUIT VARCHAR(256),
  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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_COMPTE(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CPTE INTEGER,
  DATE_OP DATE,
  LIB VARCHAR(128),
  CHEQUE VARCHAR(7),
  FAM_REC_DEP VARCHAR(128),
  RECETTE NUMERIC(18, 2),
  DEPENSE NUMERIC(18, 6),
  SOLDE NUMERIC(18, 2),
  OP INTEGER,
  ETAT VARCHAR(16))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_COMPTE_SANS(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CPTE INTEGER,
  DATE_OP DATE,
  LIB VARCHAR(128),
  CHEQUE VARCHAR(7),
  FAM_REC_DEP VARCHAR(128),
  RECETTE NUMERIC(18, 2),
  DEPENSE NUMERIC(18, 6),
  SOLDE NUMERIC(18, 2),
  OP INTEGER,
  ETAT VARCHAR(16))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_FOURNISSEUR(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_FOUR INTEGER,
  NUM VARCHAR(12),
  DATE_OPER DATE,
  LIBELLE VARCHAR(256),
  ACHAT NUMERIC(18, 2),
  REGLT NUMERIC(18, 2),
  SOLDE NUMERIC(18, 2),
  TP_OP INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RELEVE_FOURNISSEUR_DETAIL(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  DATE_OPER DATE,
  LIBELLE VARCHAR(256),
  PRODUIT VARCHAR(256),
  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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE RENUM_CHAUFFEURS_ID_RENUM_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SOLDE_CLIENT(
  ID_CLIENT INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SOLDE_FOURN(
  ID_TIER INTEGER,
  D DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SOLDE_CLIENT_SAUF_CETTE(
  ID_CLIENT INTEGER,
  ID_PIECE INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SOLDE_INI_TIERS(
  ID INTEGER)
RETURNS(
  SOLDE_INI NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE SORTIES_ID_SORTIE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE STOCK_FAMILLE(
  D DATE)
RETURNS(
  FAMILLE VARCHAR(64),
  STOCK NUMERIC(18, 6),
  VALEUR NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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,
  ARRONDI SMALLINT)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE STOCK_NOMENCLATURE_EMBALLAGE(
  ID INTEGER,
  D DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_MAT INTEGER,
  QTE NUMERIC(18, 6),
  ARRONDI SMALLINT,
  STOCK NUMERIC(18, 6),
  P_PAMP NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE STOCK_NOMENCLATURE_MATIERE(
  ID INTEGER,
  D DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_MAT INTEGER,
  QTE NUMERIC(18, 6),
  ARRONDI SMALLINT,
  STOCK NUMERIC(18, 6),
  P_PAMP NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TAUX_EXTRACTION_ID_TAUX_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TL_VTES_CLIENT_SF_CTE_PCE(
  ID INTEGER,
  ID_PIECE INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  CREANCE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_ACHAT_LOCAL(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  DETTES NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_ACHAT_LOCAL_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  DETTES NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_DEPENSES_CPTE(
  ID INTEGER)
RETURNS(
  DEPENSE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_RECETTES_CPTE(
  ID INTEGER)
RETURNS(
  RECETTE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_CLIENT(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_CLIENT_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_CLIENT_BON(
  ID_ENTET_PIECE INTEGER)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_CLIENT_RGLT(
  ID INTEGER,
  D TIMESTAMP,
  ID_REGL INTEGER)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_RECETTE_CPTE(
  ID INTEGER)
RETURNS(
  RECETTE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_REGLT_REPENSE_CPTE(
  ID INTEGER)
RETURNS(
  DEPENSE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_SOLDE_INI_CPTE(
  ID INTEGER)
RETURNS(
  SOLDE_INI NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VENTES_CLIENT(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  CREANCE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VENTES_CLIENT_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  CREANCE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VENTES_CLIENT_BON(
  ID INTEGER,
  D TIMESTAMP)
RETURNS(
  CREANCE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VENTES_CLIENT_RGLT(
  ID INTEGER,
  D TIMESTAMP)
RETURNS(
  CREANCE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VIR_FOND_DEPENSE_CPTE(
  ID INTEGER)
RETURNS(
  RECETTE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TOTAL_VIR_FOND_RECETTE_CPTE(
  ID INTEGER)
RETURNS(
  RECETTE NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TYPE_AUTRES_ENTREES_ID_TYPE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE TYPE_AUTRES_SORTIES_ID_TYPE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE UNITE_ID_ART(
  ID INTEGER)
RETURNS(
  UNITE VARCHAR(64))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE UNITES_ID_UNITE_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE UPDATE_PAMP
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE UPT_ENT_SRT_TL_CONS
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE VENTES_WILAYA
RETURNS(
  NBRE INTEGER,
  WILAYA VARCHAR(32),
  MONTANT NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE VENTES_WILAYA_PRODUITS
RETURNS(
  ID_ART INTEGER,
  WILAYA VARCHAR(32),
  REF VARCHAR(20),
  DESIGNATION VARCHAR(128),
  MONTANT NUMERIC(18, 2))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE VERIF_FICHE_DE_STOCK_DEPOT(
  DU DATE,
  ID_ART INTEGER,
  ID_MAGAZIN INTEGER,
  QUANTITE NUMERIC(18, 6),
  REF_ENTREE VARCHAR(12))
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR ALTER PROCEDURE VIR_FONDS_ID_VIR_AI
RETURNS(
  NEW_VALUE INTEGER)
AS
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

CREATE OR 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
BEGIN
  SUSPEND;
END^

SET TERM ; ^

SET TERM ^ ;

CREATE OR ALTER PROCEDURE GET_DIGIT_ONLY (
    IPARAM VARCHAR (32))
RETURNS (
    OPARAM VARCHAR (32))
AS declare variable I integer;
begin
  oparam = '';
  i = 1;
  while (i <= char_length(:iparam)) do
  begin
    if (substring(:iparam from i for 1) similar to '[0123456789]')  then
      oparam = :oparam || (substring(:iparam from i for 1));
    i = :i + 1;
  end
  suspend;
end^

SET TERM ; ^
/*----------------------------------------------------------------------------*/

SET TERM ^ ;

ALTER PROCEDURE ETAT_VENTES_DCP_PRODUITS(
  DU DATE,
  AU DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  R_C VARCHAR(50),
  ADRESSE VARCHAR(255),
  WILAYA VARCHAR(64),
  PRODUITS VARCHAR(128),
  QUANTITES NUMERIC(18, 2),
  MONTANTS NUMERIC(18, 2),
  NUM_FACTS VARCHAR(5000),
  ID_ARTICLE INTEGER)
AS
DECLARE VARIABLE NUM_FACT VARCHAR(12);
DECLARE VARIABLE VENTES NUMERIC(18, 2);
DECLARE VARIABLE AVOIRS NUMERIC(18, 2);
DECLARE VARIABLE QTE_VENTES NUMERIC(18, 6);
DECLARE VARIABLE QTE_AVOIRS NUMERIC(18, 6);
BEGIN
	FOR
    	select DISTINCT
        	SORTIES.ID_CLIENT,
        	SORTIES.ID_ARTICLE
		from sorties
        inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
		WHERE ((SORTIES.NATURE_SORTIE = 1) OR  (SORTIES.NATURE_SORTIE = 5))
        and ENTETE_SORTIES.DATE_PIECE between :DU and :AU
        INTO :ID_CLIENT, :ID_ARTICLE
    DO
    BEGIN
         select
               clients.DENOMINATION,
               CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                    WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END,
               clients.WILAYA,
               clients.REF_RC
         from clients
         where clients.ID_CLIENT = :id_client
         Into :DENOMINATION, :ADRESSE, :WILAYA, :R_C;

         SELECT
                ARTICLES.DESIGNATION
         FROM ARTICLES
         WHERE ARTICLES.ID_ART = :ID_ARTICLE
         INTO :PRODUITS;

         SELECT
                COALESCE(SUM(SORTIES.QUANTITE),0),
                COALESCE(SUM(SORTIES.HT),0)
         FROM SORTIES
         inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
         WHERE SORTIES.ID_CLIENT = :ID_CLIENT
         AND SORTIES.ID_ARTICLE = :ID_ARTICLE
         AND SORTIES.NATURE_SORTIE = 5
         and ENTETE_SORTIES.DATE_PIECE between :DU and :AU
         into :QTE_AVOIRS, AVOIRS;

         SELECT
                COALESCE(SUM(SORTIES.QUANTITE),0),
                COALESCE(SUM(SORTIES.HT),0)
         FROM SORTIES
         inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
         WHERE SORTIES.ID_CLIENT = :ID_CLIENT
         AND SORTIES.ID_ARTICLE = :ID_ARTICLE
         AND SORTIES.NATURE_SORTIE = 1
         and ENTETE_SORTIES.DATE_PIECE between :DU and :AU
         into :QTE_VENTES, VENTES;

         QUANTITES = :QTE_VENTES - :QTE_AVOIRS;
         MONTANTS = :VENTES - :AVOIRS;

    	 NUM_FACTS = '';
         for
         	select
            	CASE WHEN ENTETE_SORTIES.NATURE_SORTIE = 1 THEN NUM_PIECE
                     WHEN ENTETE_SORTIES.NATURE_SORTIE = 5 THEN 'A'||NUM_PIECE END
            from ENTETE_SORTIES
            INNER JOIN SORTIES ON SORTIES.ID_ENTETE = ENTETE_SORTIES.ID_ENT_SORT
            where ENTETE_SORTIES.ID_CLIENT = :ID_CLIENT
            AND SORTIES.ID_ARTICLE = :ID_ARTICLE
            and ((ENTETE_SORTIES.NATURE_SORTIE = 1)or(ENTETE_SORTIES.NATURE_SORTIE = 5))
            and ENTETE_SORTIES.DATE_PIECE between :DU and :AU
            into :NUM_FACT
         do
         	begin
            	if (:NUM_FACTS = '') then
         			NUM_FACTS = :NUM_FACTS || :NUM_FACT;
                else
                	NUM_FACTS = :NUM_FACTS || '-' ||:NUM_FACT;
            end
  SUSPEND;
  END
END^

SET TERM ; ^

SET TERM ^ ;

ALTER PROCEDURE ETAT_VENTES_DCP_FAMILLES(
  DU DATE,
  AU DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  R_C VARCHAR(50),
  ADRESSE VARCHAR(255),
  WILAYA VARCHAR(64),
  FAMILLES VARCHAR(64),
  QUANTITES NUMERIC(18, 2),
  MONTANTS NUMERIC(18, 2),
  NUM_FACTS VARCHAR(10000),
  ID_FAMILLE INTEGER)
AS
DECLARE VARIABLE NUM_FACT VARCHAR(12);
DECLARE VARIABLE QTE_AVOIRS NUMERIC(18, 2);
DECLARE VARIABLE AVOIRS NUMERIC(18, 2);
DECLARE VARIABLE QTE_VENTES NUMERIC(18, 2);
DECLARE VARIABLE VENTES NUMERIC(18, 2);
BEGIN
	FOR
	   select DISTINCT
        	SORTIES.ID_CLIENT,
        	SORTIES.ID_FAMILLE
	   from sorties
       inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
	   WHERE ((SORTIES.NATURE_SORTIE = 1) OR  (SORTIES.NATURE_SORTIE = 5))
       and ENTETE_SORTIES.DATE_PIECE between :DU AND :AU
       INTO :ID_CLIENT, :ID_FAMILLE
    DO
    BEGIN
         select
               clients.DENOMINATION,
               CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                    WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END,
               clients.WILAYA,
               clients.REF_RC
         from clients
         where clients.ID_CLIENT = :id_client
         Into :DENOMINATION, :ADRESSE, :WILAYA, :R_C;

         SELECT
                FAMILLES.FAMILLE
         FROM FAMILLES
         WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
         INTO :FAMILLES;

         SELECT
                COALESCE(SUM(SORTIES.QUANTITE),0),
                COALESCE(SUM(SORTIES.HT),0)
         FROM SORTIES
         inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
         WHERE SORTIES.ID_CLIENT = :ID_CLIENT
         AND SORTIES.ID_FAMILLE = :ID_FAMILLE
         AND SORTIES.NATURE_SORTIE = 5
         and ENTETE_SORTIES.DATE_PIECE between :DU AND :AU
         into :QTE_AVOIRS, AVOIRS;

         SELECT
                COALESCE(SUM(SORTIES.QUANTITE),0),
                COALESCE(SUM(SORTIES.HT),0)
         FROM SORTIES
         inner join ENTETE_SORTIES on ENTETE_SORTIES.ID_ENT_SORT = sorties.ID_ENTETE
         WHERE SORTIES.ID_CLIENT = :ID_CLIENT
         AND SORTIES.ID_FAMILLE = :ID_FAMILLE
         AND SORTIES.NATURE_SORTIE = 1
         and ENTETE_SORTIES.DATE_PIECE between :DU AND :AU
         into :QTE_VENTES, VENTES;

         QUANTITES = :QTE_VENTES - :QTE_AVOIRS;
         MONTANTS = :VENTES - :AVOIRS;

    	   NUM_FACTS = '';
         for
         	select
            	CASE WHEN ENTETE_SORTIES.NATURE_SORTIE = 1 THEN NUM_PIECE
                     WHEN ENTETE_SORTIES.NATURE_SORTIE = 5 THEN 'A'||NUM_PIECE END
            from ENTETE_SORTIES
            INNER JOIN SORTIES ON SORTIES.ID_ENTETE = ENTETE_SORTIES.ID_ENT_SORT
            where ENTETE_SORTIES.ID_CLIENT = :ID_CLIENT
            AND SORTIES.ID_FAMILLE = :ID_FAMILLE
            and ((ENTETE_SORTIES.NATURE_SORTIE = 1)or(ENTETE_SORTIES.NATURE_SORTIE = 5))
            and ENTETE_SORTIES.DATE_PIECE between :DU and :AU
            into :NUM_FACT
         do
         	begin
            	if (:NUM_FACTS = '') then
         			NUM_FACTS = :NUM_FACTS || :NUM_FACT;
                else
                	NUM_FACTS = :NUM_FACTS || '-' ||:NUM_FACT;
            end
  SUSPEND;
  END
END^

SET TERM ; ^

SET TERM ^ ;
alter PROCEDURE DASHBOARD_01(
  D1 DATE,
  D2 DATE)
RETURNS(
  DATE_OP DATE,
  CAPITAM_MP NUMERIC(18, 2),
  CAPITAM_PF NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  VENTES NUMERIC(18, 2),
  CREACES NUMERIC(18, 2),
  DETTES NUMERIC(18, 2)
  )
AS
DECLARE VARIABLE ID_MAG_MP  iNTEGER;
DECLARE VARIABLE ID_MAG_PF  iNTEGER;

BEGIN
	 SELECT PARAMETRES.ID_DEPOT_MT FROM PARAMETRES INTO :ID_MAG_MP;
	 SELECT PARAMETRES.ID_DEPOT_PF FROM PARAMETRES INTO :ID_MAG_PF;
     FOR
  		select
        	ENTETE_ENTREES.DATE_FACT
        from ENTETE_ENTREES
		where ENTETE_ENTREES.DATE_FACT between :D1 and :D2

        union

        select
        	ENTETE_SORTIES.DATE_PIECE
        from ENTETE_SORTIES
		where ENTETE_SORTIES.DATE_PIECE between :D1 and :D2
		order by 1
     INTO :DATE_OP
     do
     BEGIN
          select sum(valeur) from INVENTAIRE_DEPOT(:ID_MAG_MP, :DATE_OP) into :CAPITAM_MP;
          select sum(valeur) from INVENTAIRE_DEPOT(:ID_MAG_PF, :DATE_OP) into :CAPITAM_PF;
          select sum(ENTETE_ENTREES.TOTAL_HT) as Achats from ENTETE_ENTREES
		  where ENTETE_ENTREES.DATE_FACT = :DATE_OP into :ACHATS;
          select sum(ENTETE_SORTIES.TOTAL_HT) as Ventes from ENTETE_SORTIES
		  where ENTETE_SORTIES.DATE_PIECE = :DATE_OP into :VENTES;
          select sum(ecart) as creances from ETAT_CREANCES((select EXER_AU from PARAMETRES), :DATE_OP) into :CREACES;
          select sum(ecart) as creances from ETAT_DETTES_LOCALES((select EXER_AU from PARAMETRES), :DATE_OP) into :DETTES;
  SUSPEND;
  END
END^
SET TERM ; ^

SET TERM ^ ;

ALTER PROCEDURE VENTES_CLIENTS_PRODUITS (
  DU           DATE,
  AU           DATE
) RETURNS (
  DENOMINATION VARCHAR(128),
  ACTIVITE     VARCHAR(128),
  ADRESSE      VARCHAR(256),
  MNT1         NUMERIC(18,2),
  MNT2         NUMERIC(18,2),
  MNT3         NUMERIC(18,2),
  MNT4         NUMERIC(18,2),
  MNT5         NUMERIC(18,2),
  MNT6         NUMERIC(18,2),
  MNT7         NUMERIC(18,2),
  MNT8         NUMERIC(18,2),
  MNT9         NUMERIC(18,2),
  MNT10        NUMERIC(18,2),
  MNT11        NUMERIC(18,2),
  MNT12        NUMERIC(18,2),
  MNT13        NUMERIC(18,2),
  MNT14        NUMERIC(18,2),
  MNT15        NUMERIC(18,2),
  MNT16        NUMERIC(18,2),
  MNT17        NUMERIC(18,2),
  MNT18        NUMERIC(18,2),
  MNT19        NUMERIC(18,2),
  MNT20        NUMERIC(18,2),
  MNT21        NUMERIC(18,2),
  MNT22        NUMERIC(18,2),
  MNT23        NUMERIC(18,2),
  MNT24        NUMERIC(18,2),
  MNT25        NUMERIC(18,2),
  MNT26        NUMERIC(18,2),
  MNT27        NUMERIC(18,2),
  MNT28        NUMERIC(18,2),
  MNT29        NUMERIC(18,2),
  MNT30        NUMERIC(18,2),
  MNT31        NUMERIC(18,2),
  MNT32        NUMERIC(18,2),
  MNT33        NUMERIC(18,2),
  MNT34        NUMERIC(18,2),
  MNT35        NUMERIC(18,2),
  MNT36        NUMERIC(18,2),
  MNT37        NUMERIC(18,2),
  MNT38        NUMERIC(18,2),
  MNT39        NUMERIC(18,2),
  MNT40        NUMERIC(18,2),
  MNT41        NUMERIC(18,2)
)
AS
declare variable REF INTEGER;
  declare variable REF1 INTEGER;
  declare variable REF2 INTEGER;
  declare variable REF3 INTEGER;
  declare variable REF4 INTEGER;
  declare variable REF5 INTEGER;
  declare variable REF6 INTEGER;
  declare variable REF7 INTEGER;
  declare variable REF8 INTEGER;
  declare variable REF9 INTEGER;
  declare variable REF10 INTEGER;
  declare variable REF11 INTEGER;
  declare variable REF12 INTEGER;
  declare variable REF13 INTEGER;
  declare variable REF14 INTEGER;
  declare variable REF15 INTEGER;
  declare variable REF16 INTEGER;
  declare variable REF17 INTEGER;
  declare variable REF18 INTEGER;
  declare variable REF19 INTEGER;
  declare variable REF20 INTEGER;

  declare variable REF21 INTEGER;
  declare variable REF22 INTEGER;
  declare variable REF23 INTEGER;
  declare variable REF24 INTEGER;
  declare variable REF25 INTEGER;
  declare variable REF26 INTEGER;
  declare variable REF27 INTEGER;
  declare variable REF28 INTEGER;
  declare variable REF29 INTEGER;
  declare variable REF30 INTEGER;
  declare variable REF31 INTEGER;
  declare variable REF32 INTEGER;
  declare variable REF33 INTEGER;
  declare variable REF34 INTEGER;
  declare variable REF35 INTEGER;
  declare variable REF36 INTEGER;
  declare variable REF37 INTEGER;
  declare variable REF38 INTEGER;
  declare variable REF39 INTEGER;
  declare variable REF40 INTEGER;

  declare variable I INTEGER;
  declare variable ROW_MAX INTEGER;
BEGIN
     I = 0;
     FOR
        SELECT
              DISTINCT
                      SORTIES.ID_ARTICLE
        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.ID_ARTICLE
        WHERE SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        AND ARTICLES.STATISTIQUE = 1
        INTO :REF
     DO
       BEGIN
            I = I +1;
            IF (I = 1) THEN REF1 = :REF;
            IF (I = 2) THEN REF2 = :REF;
            IF (I = 3) THEN REF3 = :REF;
            IF (I = 4) THEN REF4 = :REF;
            IF (I = 5) THEN REF5 = :REF;
            IF (I = 6) THEN REF6 = :REF;
            IF (I = 7) THEN REF7 = :REF;
            IF (I = 8) THEN REF8 = :REF;
            IF (I = 9) THEN REF9 = :REF;
            IF (I = 10) THEN REF10 = :REF;

            IF (I = 11) THEN REF11 = :REF;
            IF (I = 12) THEN REF12 = :REF;
            IF (I = 13) THEN REF13 = :REF;
            IF (I = 14) THEN REF14 = :REF;
            IF (I = 15) THEN REF15 = :REF;
            IF (I = 16) THEN REF16 = :REF;
            IF (I = 17) THEN REF17 = :REF;
            IF (I = 18) THEN REF18 = :REF;
            IF (I = 19) THEN REF19 = :REF;
            IF (I = 20) THEN REF20 = :REF;

            IF (I = 21) THEN REF21 = :REF;
            IF (I = 22) THEN REF22 = :REF;
            IF (I = 23) THEN REF23 = :REF;
            IF (I = 24) THEN REF24 = :REF;
            IF (I = 25) THEN REF25 = :REF;
            IF (I = 26) THEN REF26 = :REF;
            IF (I = 27) THEN REF27 = :REF;
            IF (I = 28) THEN REF28 = :REF;
            IF (I = 29) THEN REF29 = :REF;
            IF (I = 30) THEN REF30 = :REF;

            IF (I = 31) THEN REF31 = :REF;
            IF (I = 32) THEN REF32 = :REF;
            IF (I = 33) THEN REF33 = :REF;
            IF (I = 34) THEN REF34 = :REF;
            IF (I = 35) THEN REF35 = :REF;
            IF (I = 36) THEN REF36 = :REF;
            IF (I = 37) THEN REF37 = :REF;
            IF (I = 38) THEN REF38 = :REF;
            IF (I = 39) THEN REF39 = :REF;
            IF (I = 40) THEN REF40 = :REF;
       END

  FOR
     SELECT DISTINCT
           CLIENTS.DENOMINATION,
           clients.ACTIVITE,
           clients.ADRESSE1||' '||clients.adresse2,
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF1) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF2) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF3) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF4) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF5) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF6) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF7) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF8) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF9) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF10) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF11) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF12) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF13) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF14) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF15) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF16) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF17) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF18) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF19) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF20) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF21) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF22) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF23) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF24) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF25) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF26) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF27) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF28) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF29) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF30) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF31) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF32) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF33) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF34) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF35) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF36) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF37) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF38) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF39) THEN SORTIES.QUANTITE END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF40) THEN SORTIES.QUANTITE END)
     FROM SORTIES
     INNER JOIN CLIENTS ON CLIENTS.ID_CLIENT = SORTIES.ID_CLIENT
     INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.ID_ARTICLE
     WHERE SORTIES.DATE_PIECE BETWEEN :DU AND :AU
     AND ARTICLES.STATISTIQUE = 1
     GROUP BY CLIENTS.DENOMINATION, CLIENTS.ACTIVITE, CLIENTS.ADRESSE1, CLIENTS.ADRESSE2
     INTO :DENOMINATION, :ACTIVITE, :Adresse, :MNT1, :MNT2, :MNT3, :MNT4, :MNT5, :MNT6, :MNT7, :MNT8, :MNT9, :MNT10,
                         :MNT11, :MNT12, :MNT13, :MNT14, :MNT15, :MNT16, :MNT17, :MNT18, :MNT19, :MNT20,
                         :MNT21, :MNT22, :MNT23, :MNT24, :MNT25, :MNT26, :MNT27, :MNT28, :MNT29, :MNT30,
                         :MNT31, :MNT32, :MNT33, :MNT34, :MNT35, :MNT36, :MNT37, :MNT38, :MNT39, :MNT40
  DO
  BEGIN
        IF (MNT1 IS NULL) THEN MNT1 = 0;
  		IF (MNT2 IS NULL) THEN MNT2 = 0;
  		IF (MNT3 IS NULL) THEN MNT3 = 0;
  		IF (MNT4 IS NULL) THEN MNT4 = 0;
  		IF (MNT5 IS NULL) THEN MNT5 = 0;
  		IF (MNT6 IS NULL) THEN MNT6 = 0;
  		IF (MNT7 IS NULL) THEN MNT7 = 0;
  		IF (MNT8 IS NULL) THEN MNT8 = 0;
  		IF (MNT9 IS NULL) THEN MNT9 = 0;
  		IF (MNT10 IS NULL) THEN MNT10 = 0;
  		IF (MNT11 IS NULL) THEN MNT11 = 0;
  		IF (MNT12 IS NULL) THEN MNT12 = 0;
  		IF (MNT13 IS NULL) THEN MNT13 = 0;
  		IF (MNT14 IS NULL) THEN MNT14 = 0;
  		IF (MNT15 IS NULL) THEN MNT15 = 0;
  		IF (MNT16 IS NULL) THEN MNT16 = 0;
  		IF (MNT17 IS NULL) THEN MNT17 = 0;
  		IF (MNT18 IS NULL) THEN MNT18 = 0;
  		IF (MNT19 IS NULL) THEN MNT19 = 0;
  		IF (MNT20 IS NULL) THEN MNT20 = 0;
  		IF (MNT21 IS NULL) THEN MNT21 = 0;
  		IF (MNT22 IS NULL) THEN MNT22 = 0;
  		IF (MNT23 IS NULL) THEN MNT23 = 0;
  		IF (MNT24 IS NULL) THEN MNT24 = 0;
  		IF (MNT25 IS NULL) THEN MNT25 = 0;
  		IF (MNT26 IS NULL) THEN MNT26 = 0;
  		IF (MNT27 IS NULL) THEN MNT27 = 0;
  		IF (MNT28 IS NULL) THEN MNT28 = 0;
  		IF (MNT29 IS NULL) THEN MNT29 = 0;
  		IF (MNT30 IS NULL) THEN MNT30 = 0;
  		IF (MNT31 IS NULL) THEN MNT31 = 0;
  		IF (MNT32 IS NULL) THEN MNT32 = 0;
  		IF (MNT33 IS NULL) THEN MNT33 = 0;
  		IF (MNT34 IS NULL) THEN MNT34 = 0;
  		IF (MNT35 IS NULL) THEN MNT35 = 0;
  		IF (MNT36 IS NULL) THEN MNT36 = 0;
  		IF (MNT37 IS NULL) THEN MNT37 = 0;
  		IF (MNT38 IS NULL) THEN MNT38 = 0;
  		IF (MNT39 IS NULL) THEN MNT39 = 0;
  		IF (MNT40 IS NULL) THEN MNT40 = 0;

        MNT41 = MNT1 +  MNT2 +  MNT3 +  MNT4 +  MNT5 +  MNT6 +  MNT7 +  MNT8 +  MNT9 +  MNT10
              + MNT11 + MNT12 + MNT13 + MNT14 + MNT15 + MNT16 + MNT17 + MNT18 + MNT19 + MNT20
              + MNT21 + MNT22 + MNT23 + MNT24 + MNT25 + MNT26 + MNT27 + MNT28 + MNT29 + MNT30
              + MNT31 + MNT32 + MNT33 + MNT34 + MNT35 + MNT36 + MNT37 + MNT38 + MNT39 + MNT40;
  SUSPEND;
  END
END^

SET TERM ; ^

SET TERM ^ ;

ALTER PROCEDURE VENTES_CLIENTS_PRODUITS_VAL(
  DU DATE,
  AU DATE)
RETURNS(
  DENOMINATION VARCHAR(128),
  ACTIVITE     VARCHAR(128),
  ADRESSE      VARCHAR(256),
  MNT1 NUMERIC(18, 2),
  MNT2 NUMERIC(18, 2),
  MNT3 NUMERIC(18, 2),
  MNT4 NUMERIC(18, 2),
  MNT5 NUMERIC(18, 2),
  MNT6 NUMERIC(18, 2),
  MNT7 NUMERIC(18, 2),
  MNT8 NUMERIC(18, 2),
  MNT9 NUMERIC(18, 2),
  MNT10 NUMERIC(18, 2),
  MNT11 NUMERIC(18, 2),
  MNT12 NUMERIC(18, 2),
  MNT13 NUMERIC(18, 2),
  MNT14 NUMERIC(18, 2),
  MNT15 NUMERIC(18, 2),
  MNT16 NUMERIC(18, 2),
  MNT17 NUMERIC(18, 2),
  MNT18 NUMERIC(18, 2),
  MNT19 NUMERIC(18, 2),
  MNT20 NUMERIC(18, 2),
  MNT21 NUMERIC(18, 2),
  MNT22 NUMERIC(18, 2),
  MNT23 NUMERIC(18, 2),
  MNT24 NUMERIC(18, 2),
  MNT25 NUMERIC(18, 2),
  MNT26 NUMERIC(18, 2),
  MNT27 NUMERIC(18, 2),
  MNT28 NUMERIC(18, 2),
  MNT29 NUMERIC(18, 2),
  MNT30 NUMERIC(18, 2),
  MNT31 NUMERIC(18, 2),
  MNT32 NUMERIC(18, 2),
  MNT33 NUMERIC(18, 2),
  MNT34 NUMERIC(18, 2),
  MNT35 NUMERIC(18, 2),
  MNT36 NUMERIC(18, 2),
  MNT37 NUMERIC(18, 2),
  MNT38 NUMERIC(18, 2),
  MNT39 NUMERIC(18, 2),
  MNT40 NUMERIC(18, 2),
  MNT41 NUMERIC(18, 2))
AS
DECLARE VARIABLE REF INTEGER;
DECLARE VARIABLE REF1 INTEGER;
DECLARE VARIABLE REF2 INTEGER;
DECLARE VARIABLE REF3 INTEGER;
DECLARE VARIABLE REF4 INTEGER;
DECLARE VARIABLE REF5 INTEGER;
DECLARE VARIABLE REF6 INTEGER;
DECLARE VARIABLE REF7 INTEGER;
DECLARE VARIABLE REF8 INTEGER;
DECLARE VARIABLE REF9 INTEGER;
DECLARE VARIABLE REF10 INTEGER;
DECLARE VARIABLE REF11 INTEGER;
DECLARE VARIABLE REF12 INTEGER;
DECLARE VARIABLE REF13 INTEGER;
DECLARE VARIABLE REF14 INTEGER;
DECLARE VARIABLE REF15 INTEGER;
DECLARE VARIABLE REF16 INTEGER;
DECLARE VARIABLE REF17 INTEGER;
DECLARE VARIABLE REF18 INTEGER;
DECLARE VARIABLE REF19 INTEGER;
DECLARE VARIABLE REF20 INTEGER;
DECLARE VARIABLE REF21 INTEGER;
DECLARE VARIABLE REF22 INTEGER;
DECLARE VARIABLE REF23 INTEGER;
DECLARE VARIABLE REF24 INTEGER;
DECLARE VARIABLE REF25 INTEGER;
DECLARE VARIABLE REF26 INTEGER;
DECLARE VARIABLE REF27 INTEGER;
DECLARE VARIABLE REF28 INTEGER;
DECLARE VARIABLE REF29 INTEGER;
DECLARE VARIABLE REF30 INTEGER;
DECLARE VARIABLE REF31 INTEGER;
DECLARE VARIABLE REF32 INTEGER;
DECLARE VARIABLE REF33 INTEGER;
DECLARE VARIABLE REF34 INTEGER;
DECLARE VARIABLE REF35 INTEGER;
DECLARE VARIABLE REF36 INTEGER;
DECLARE VARIABLE REF37 INTEGER;
DECLARE VARIABLE REF38 INTEGER;
DECLARE VARIABLE REF39 INTEGER;
DECLARE VARIABLE REF40 INTEGER;
DECLARE VARIABLE I INTEGER;
DECLARE VARIABLE ROW_MAX INTEGER;
BEGIN
     I = 0;
     FOR
        SELECT
              DISTINCT
                      SORTIES.ID_ARTICLE
        FROM SORTIES
        INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.ID_ARTICLE
        WHERE SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        AND ARTICLES.STATISTIQUE = 1
        INTO :REF
     DO
       BEGIN
            I = I +1;
            IF (I = 1) THEN REF1 = :REF;
            IF (I = 2) THEN REF2 = :REF;
            IF (I = 3) THEN REF3 = :REF;
            IF (I = 4) THEN REF4 = :REF;
            IF (I = 5) THEN REF5 = :REF;
            IF (I = 6) THEN REF6 = :REF;
            IF (I = 7) THEN REF7 = :REF;
            IF (I = 8) THEN REF8 = :REF;
            IF (I = 9) THEN REF9 = :REF;
            IF (I = 10) THEN REF10 = :REF;

            IF (I = 11) THEN REF11 = :REF;
            IF (I = 12) THEN REF12 = :REF;
            IF (I = 13) THEN REF13 = :REF;
            IF (I = 14) THEN REF14 = :REF;
            IF (I = 15) THEN REF15 = :REF;
            IF (I = 16) THEN REF16 = :REF;
            IF (I = 17) THEN REF17 = :REF;
            IF (I = 18) THEN REF18 = :REF;
            IF (I = 19) THEN REF19 = :REF;
            IF (I = 20) THEN REF20 = :REF;

            IF (I = 21) THEN REF21 = :REF;
            IF (I = 22) THEN REF22 = :REF;
            IF (I = 23) THEN REF23 = :REF;
            IF (I = 24) THEN REF24 = :REF;
            IF (I = 25) THEN REF25 = :REF;
            IF (I = 26) THEN REF26 = :REF;
            IF (I = 27) THEN REF27 = :REF;
            IF (I = 28) THEN REF28 = :REF;
            IF (I = 29) THEN REF29 = :REF;
            IF (I = 30) THEN REF30 = :REF;

            IF (I = 31) THEN REF31 = :REF;
            IF (I = 32) THEN REF32 = :REF;
            IF (I = 33) THEN REF33 = :REF;
            IF (I = 34) THEN REF34 = :REF;
            IF (I = 35) THEN REF35 = :REF;
            IF (I = 36) THEN REF36 = :REF;
            IF (I = 37) THEN REF37 = :REF;
            IF (I = 38) THEN REF38 = :REF;
            IF (I = 39) THEN REF39 = :REF;
            IF (I = 40) THEN REF40 = :REF;
       END

  FOR
     SELECT DISTINCT
           CLIENTS.DENOMINATION,
           clients.ACTIVITE,
           clients.ADRESSE1||' '||clients.adresse2,
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF1) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF2) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF3) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF4) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF5) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF6) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF7) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF8) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF9) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF10) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF11) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF12) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF13) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF14) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF15) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF16) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF17) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF18) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF19) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF20) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF21) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF22) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF23) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF24) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF25) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF26) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF27) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF28) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF29) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF30) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF31) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF32) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF33) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF34) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF35) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF36) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF37) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF38) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF39) THEN SORTIES.TTC END),
           SUM(CASE WHEN (SORTIES.ID_ARTICLE = :REF40) THEN SORTIES.TTC END)
     FROM SORTIES
     INNER JOIN CLIENTS ON CLIENTS.ID_CLIENT = SORTIES.ID_CLIENT
     INNER JOIN ARTICLES ON ARTICLES.ID_ART = SORTIES.ID_ARTICLE
        WHERE SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        AND ARTICLES.STATISTIQUE = 1
     GROUP BY CLIENTS.DENOMINATION, CLIENTS.ACTIVITE, CLIENTS.ADRESSE1, CLIENTS.ADRESSE2
     INTO :DENOMINATION, :ACTIVITE, :Adresse, :MNT1, :MNT2, :MNT3, :MNT4, :MNT5, :MNT6, :MNT7, :MNT8, :MNT9, :MNT10,
                         :MNT11, :MNT12, :MNT13, :MNT14, :MNT15, :MNT16, :MNT17, :MNT18, :MNT19, :MNT20,
                         :MNT21, :MNT22, :MNT23, :MNT24, :MNT25, :MNT26, :MNT27, :MNT28, :MNT29, :MNT30,
                         :MNT31, :MNT32, :MNT33, :MNT34, :MNT35, :MNT36, :MNT37, :MNT38, :MNT39, :MNT40
  DO
  BEGIN
  		IF (MNT1 IS NULL) THEN MNT1 = 0;
  		IF (MNT2 IS NULL) THEN MNT2 = 0;
  		IF (MNT3 IS NULL) THEN MNT3 = 0;
  		IF (MNT4 IS NULL) THEN MNT4 = 0;
  		IF (MNT5 IS NULL) THEN MNT5 = 0;
  		IF (MNT6 IS NULL) THEN MNT6 = 0;
  		IF (MNT7 IS NULL) THEN MNT7 = 0;
  		IF (MNT8 IS NULL) THEN MNT8 = 0;
  		IF (MNT9 IS NULL) THEN MNT9 = 0;
  		IF (MNT10 IS NULL) THEN MNT10 = 0;
  		IF (MNT11 IS NULL) THEN MNT11 = 0;
  		IF (MNT12 IS NULL) THEN MNT12 = 0;
  		IF (MNT13 IS NULL) THEN MNT13 = 0;
  		IF (MNT14 IS NULL) THEN MNT14 = 0;
  		IF (MNT15 IS NULL) THEN MNT15 = 0;
  		IF (MNT16 IS NULL) THEN MNT16 = 0;
  		IF (MNT17 IS NULL) THEN MNT17 = 0;
  		IF (MNT18 IS NULL) THEN MNT18 = 0;
  		IF (MNT19 IS NULL) THEN MNT19 = 0;
  		IF (MNT20 IS NULL) THEN MNT20 = 0;
  		IF (MNT21 IS NULL) THEN MNT21 = 0;
  		IF (MNT22 IS NULL) THEN MNT22 = 0;
  		IF (MNT23 IS NULL) THEN MNT23 = 0;
  		IF (MNT24 IS NULL) THEN MNT24 = 0;
  		IF (MNT25 IS NULL) THEN MNT25 = 0;
  		IF (MNT26 IS NULL) THEN MNT26 = 0;
  		IF (MNT27 IS NULL) THEN MNT27 = 0;
  		IF (MNT28 IS NULL) THEN MNT28 = 0;
  		IF (MNT29 IS NULL) THEN MNT29 = 0;
  		IF (MNT30 IS NULL) THEN MNT30 = 0;
  		IF (MNT31 IS NULL) THEN MNT31 = 0;
  		IF (MNT32 IS NULL) THEN MNT32 = 0;
  		IF (MNT33 IS NULL) THEN MNT33 = 0;
  		IF (MNT34 IS NULL) THEN MNT34 = 0;
  		IF (MNT35 IS NULL) THEN MNT35 = 0;
  		IF (MNT36 IS NULL) THEN MNT36 = 0;
  		IF (MNT37 IS NULL) THEN MNT37 = 0;
  		IF (MNT38 IS NULL) THEN MNT38 = 0;
  		IF (MNT39 IS NULL) THEN MNT39 = 0;
  		IF (MNT40 IS NULL) THEN MNT40 = 0;

        MNT41 = MNT1 +  MNT2 +  MNT3 +  MNT4 +  MNT5 +  MNT6 +  MNT7 +  MNT8 +  MNT9 +  MNT10
              + MNT11 + MNT12 + MNT13 + MNT14 + MNT15 + MNT16 + MNT17 + MNT18 + MNT19 + MNT20
              + MNT21 + MNT22 + MNT23 + MNT24 + MNT25 + MNT26 + MNT27 + MNT28 + MNT29 + MNT30
              + MNT31 + MNT32 + MNT33 + MNT34 + MNT35 + MNT36 + MNT37 + MNT38 + MNT39 + MNT40;
  SUSPEND;
  END
END^

SET TERM ; ^


SET TERM ^ ;
ALTER PROCEDURE GET_COUT_REF_ENTREE (
  ID_ARTICLE      INTEGER,
  REF_ENTREE      VARCHAR(12),
  ID_MAGAZIN      INTEGER,
  DATE_OPER       DATE
) RETURNS (
  COUT_REF_ENTREE NUMERIC(18,2)
)
AS
BEGIN
select first 1  cout from (
select
       I.ID_ARTICLE,
       I.DATE_INVENTAIRE,
       I.REF_ENTREE,
       I.COUT
from INVENTAIRE I
where I.ID_ARTICLE = :ID_ARTICLE
and   I.REF_ENTREE = :REF_ENTREE
and   I.ID_MAGAZIN = :ID_MAGAZIN

union all

select
       E.ID_ARTICLE,
       E.DATE_ENTREE,
       E.REF_ENTREE,
       E.COUT_ACHAT
from entrees E
where E.NATURE_ENTREE <> 4
and   E.ID_ARTICLE = :ID_ARTICLE
and   E.REF_ENTREE = :REF_ENTREE
and   E.ID_MAGAZIN = :ID_MAGAZIN
and   E.DATE_ENTREE <= :DATE_OPER

union all

select
       S.ID_ARTICLE,
       S.DATE_PIECE,
       S.REF_ENTREE,
       S.CR_U
from sorties S
where ((S.NATURE_SORTIE) = 5 or (S.NATURE_SORTIE = 7))
and   S.ID_ARTICLE = :ID_ARTICLE
and   S.REF_ENTREE = :REF_ENTREE
and   S.ID_MAGAZIN = :ID_MAGAZIN
and   S.DATE_PIECE <= :DATE_OPER

union all

select
       TR.ID_ARTICLE,
       TR.DATE_PIECE,
       TR.REF_ENTREE,
       TR.PAMP
from LIGNES_TRANSFERT TR
where TR.ID_ARTICLE = :ID_ARTICLE
and   TR.REF_ENTREE = :REF_ENTREE
and   TR.ID_MAGAZIN_2 = :ID_MAGAZIN
and   TR.DATE_PIECE <= :DATE_OPER
order by 1,2 desc) INTO :COUT_REF_ENTREE;
if (COUT_REF_ENTREE is null) then :COUT_REF_ENTREE = 0;
SUSPEND;
END^
SET TERM ; ^


SET TERM ^ ;

ALTER PROCEDURE MVT_DU_AU (
  DU         DATE,
  AU         DATE
) RETURNS (
  ID_MVT     INTEGER,
  ID_ARTICLE INTEGER,
  REF_ENTREE VARCHAR(12),
  ID_MAGAZIN INTEGER,
  DATE_OPER  DATE,
  COUT       NUMERIC(18,2),
  QTE_INI    NUMERIC(18,6),
  QTE_ENT    NUMERIC(18,6),
  QTE_SRT    NUMERIC(18,6),
  OPER       SMALLINT
)
AS
BEGIN
	FOR
      SELECT
          INVENTAIRE.ID_INVENTAIRE,
          INVENTAIRE.id_article,
          inventaire.ref_entree,
          INVENTAIRE.id_magazin,
          INVENTAIRE.date_inventaire,
          INVENTAIRE.COUT,
          INVENTAIRE.quantite,
          0,
          0,
          1
     FROM inventaire


     UNION ALL

     SELECT
     	   entrees.id_entree,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           ENTREES.quantite,
           0,
           0,
           1
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree <> 4)
     AND ENTREES.date_entree < :DU

     UNION ALL

     SELECT
     	   sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           SORTIES.quantite,
           0,
           0,
           1
     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))
     AND SORTIES.date_piece < :DU

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           lignes_transfert.quantite,
           0,
           0,
           1
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE lignes_transfert.date_piece < :DU

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           -lignes_transfert.quantite,
           0,
           0,
           1
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE lignes_transfert.date_piece < :DU

      UNION ALL

     SELECT
     		ENTREES.ID_ENTREE,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           -ENTREES.quantite,
           0,
           0,
           1
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree = 4)
     AND ENTREES.date_entree < :DU

     UNION ALL

     SELECT
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           -SORTIES.quantite,
           0,
           0,
           1
     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))
     AND SORTIES.date_piece < :DU

     /*---------------------------------------------------------------------------------------------------------*/
     UNION ALL

     SELECT
     		entrees.id_entree,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           ENTREES.quantite,
           0,
           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)
     AND ENTREES.date_entree BETWEEN :DU AND :AU

     UNION ALL

     SELECT
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           SORTIES.quantite,
           0,
           3
     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))
     AND SORTIES.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           lignes_transfert.quantite,
           0,
          4
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE lignes_transfert.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           0,
           lignes_transfert.quantite,
           5
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
     WHERE lignes_transfert.date_piece BETWEEN :DU AND :AU

     UNION ALL

     SELECT
     		ENTREES.ID_ENTREE,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           0,
           ENTREES.quantite,
          6
     FROM ENTREES
     inner join entete_entrees on entete_entrees.id_ent_ent = entrees.id_entete
     WHERE (ENTREES.stocker = 1)
     AND (ENTREES.nature_entree = 4)
     AND ENTREES.date_entree BETWEEN :DU AND :AU

     UNION ALL

     SELECT
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           0,
           SORTIES.quantite,
           7
     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))
     AND SORTIES.date_piece BETWEEN :DU AND :AU
     INTO :ID_MVT, :ID_ARTICLE, :REF_ENTREE, :ID_MAGAZIN, :DATE_OPER, :COUT, :QTE_INI, :QTE_ENT, :QTE_SRT, :OPER
  DO
  SUSPEND;
END^

SET TERM ; ^

SET TERM ^ ;
ALTER PROCEDURE  UPDATE_COUT_REF_ENTREE
AS
BEGIN

UPDATE entrees SET ENTREES.autres_charges = 0 WHERE entrees.nature_entree = 3 and ENTREES.autres_charges is null;

UPDATE LIGNES_TRANSFERT SET LIGNES_TRANSFERT.PAMP = (select GET_COUT_REF_ENTREE.COUT_REF_ENTREE
                                                     FROM GET_COUT_REF_ENTREE(
                                                     LIGNES_TRANSFERT.ID_ARTICLE,
                                                     LIGNES_TRANSFERT.REF_ENTREE,
                                                     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 sorties SET sorties.cr_u = (select GET_COUT_REF_ENTREE.COUT_REF_ENTREE
                                   FROM GET_COUT_REF_ENTREE(
                                                     sorties.ID_ARTICLE,
                                                     sorties.REF_ENTREE,
                                                     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 GET_COUT_REF_ENTREE.COUT_REF_ENTREE
                                                     FROM GET_COUT_REF_ENTREE(
                                                     sorties.ID_ARTICLE,
                                                     sorties.REF_ENTREE,
                                                     sorties.ID_MAGAZIN,
                                                     sorties.DATE_PIECE ))
WHERE ((sorties.nature_sortie <> 5) and (sorties.nature_sortie <> 7)and (sorties.nature_sortie <> 4));

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));

UPDATE LIGNES_TRANSFERT SET LIGNES_TRANSFERT.PAMP =(select GET_COUT_REF_ENTREE.COUT_REF_ENTREE
                                                     FROM GET_COUT_REF_ENTREE(LIGNES_TRANSFERT.ID_ARTICLE,
                                                     LIGNES_TRANSFERT.REF_ENTREE,
                                                     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);

SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
ALTER PROCEDURE NUM_NOTE_DEP
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
     	   max(cast((select OPARAM from get_digit_only( NOTES_DEPENSES.NUM_NOTE_DEP )) as integer))
     FROM NOTES_DEPENSES
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
ALTER PROCEDURE NUM_NOTE_REC
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
     		max(cast((select OPARAM from get_digit_only( NOTES_RECETTES.NUM_NOTE_REC )) as integer))
     FROM NOTES_RECETTES
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
ALTER PROCEDURE DASHBOARD(
  DU DATE,
  AU DATE)
RETURNS(
  ACHAT_SERVICES NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  AVOIRS_ACHATS NUMERIC(18, 2),
  FACT_VENTE NUMERIC(18, 2),
  BL_VENTE NUMERIC(18, 2),
  AVOIRS_VENTE NUMERIC(18, 2),
  RETOURS NUMERIC(18, 2),
  CONSOMATION NUMERIC(18, 2),
  RGLT_FOUR NUMERIC(18, 2),
  REGL_CLIENT NUMERIC(18, 2),
  DEPENSES NUMERIC(18, 2))
AS
DECLARE VARIABLE CONS_VTE NUMERIC(18, 2);
DECLARE VARIABLE CONS_RET NUMERIC(18, 2);
BEGIN
		SELECT
                SUM(ENTETE_ENTREES.NET_A_PAYER)
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.NATURE_ENTREE = 5
        AND ENTETE_ENTREES.DATE_FACT BETWEEN :DU AND :AU
        INTO :ACHAT_SERVICES;
        if (ACHAT_SERVICES IS NULL) then ACHAT_SERVICES = 0;

        SELECT
                SUM(ENTETE_ENTREES.NET_A_PAYER)
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.NATURE_ENTREE = 2
        AND ENTETE_ENTREES.DATE_FACT BETWEEN :DU AND :AU
        INTO :ACHATS;
        if (ACHATS IS NULL) then ACHATS = 0;

        SELECT
                SUM(ENTETE_ENTREES.NET_A_PAYER)
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.NATURE_ENTREE = 4
        AND ENTETE_ENTREES.DATE_FACT BETWEEN :DU AND :AU
        INTO :AVOIRS_ACHATS;
        if (AVOIRS_ACHATS IS NULL) then AVOIRS_ACHATS = 0;

        SELECT
                SUM(ENTETE_SORTIES.TOTAL_FACTURE)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE = 1
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :FACT_VENTE;
        if (FACT_VENTE IS NULL) then FACT_VENTE = 0;

        SELECT
                SUM(ENTETE_SORTIES.TOTAL_FACTURE)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE = 2
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :BL_VENTE;
        if (BL_VENTE IS NULL) then BL_VENTE = 0;

        SELECT
                SUM(ENTETE_SORTIES.TOTAL_FACTURE)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE = 5
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :AVOIRS_VENTE;
        if (AVOIRS_VENTE IS NULL) then AVOIRS_VENTE = 0;

        SELECT
                SUM(ENTETE_SORTIES.TOTAL_FACTURE)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE = 7
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :RETOURS;
        if (RETOURS IS NULL) then RETOURS = 0;

        SELECT
        		SUM(ENTETE_SORTIES.TOTAL_CONSOMMATION)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE not IN(5, 7)
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :cons_vte;
        if (cons_vte IS NULL) then cons_vte = 0;

        SELECT
        		SUM(ENTETE_SORTIES.TOTAL_CONSOMMATION)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.NATURE_SORTIE IN(5, 7)
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :cons_ret;
        if (cons_ret IS NULL) then cons_ret = 0;

        CONSOMATION = :cons_vte - :cons_ret;

        SELECT
                SUM(REGL_TIERS.MONTANT)
        FROM REGL_TIERS
        WHERE REGL_TIERS.TYPE_REGT = 0
        AND REGL_TIERS.DATE_REGT BETWEEN :DU AND :AU
        INTO :RGLT_FOUR;
        if (RGLT_FOUR IS NULL) then RGLT_FOUR = 0;

        SELECT
                SUM(REGL_TIERS.MONTANT)
        FROM REGL_TIERS
        WHERE REGL_TIERS.TYPE_REGT = 1
        AND REGL_TIERS.DATE_REGT BETWEEN :DU AND :AU
        INTO :REGL_CLIENT;
        if (REGL_CLIENT IS NULL) then REGL_CLIENT = 0;

        SELECT
                SUM(DEPENSES.MONTANT)
        FROM DEPENSES
        WHERE DEPENSES.DATE_DEP BETWEEN :DU AND :AU
        INTO :DEPENSES;
        if (DEPENSES IS NULL) then DEPENSES = 0;



  SUSPEND;
END^
SET TERM ; ^

SET TERM ^ ;
ALTER PROCEDURE SOLDE_COMPTE(
  ID_CPTE INTEGER,
  AU DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
DECLARE VARIABLE DEBIT NUMERIC(18, 2);
DECLARE VARIABLE CREDIT NUMERIC(18, 2);
BEGIN
	DEBIT = 0;
    CREDIT = 0;
    SOLDE = 0;

    SELECT
		COALESCE( sum(COMPTES_TRESORIE.SOLDE_INI),0)
	FROM COMPTES_TRESORIE
	WHERE COMPTES_TRESORIE.ID_CPTE = :id_CPTE
	INTO :DEBIT;
    IF (:DEBIT IS NULL) THEN DEBIT = 0;
	SOLDE = :DEBIT;

  	SELECT
		COALESCE( sum(regl_tiers.montant),0)
	FROM regl_tiers
	WHERE regl_tiers.type_regt = 1
	AND id_cpte = :id_CPTE
	AND regl_tiers.date_regt <= :AU
	AND (UPPER(regl_tiers.etat) = 'OK')
	INTO :DEBIT;
    IF (:DEBIT IS NULL) THEN DEBIT = 0;
	SOLDE = :SOLDE + :DEBIT;

	SELECT
		COALESCE( sum(RECETTES.montant),0 )
	FROM RECETTES
	WHERE id_cpte = :ID_CPTE
	AND RECETTES.date_rec  <= :AU
	AND (UPPER(RECETTES.etat) = 'OK')
	INTO :DEBIT;
    IF (:DEBIT IS NULL) THEN DEBIT = 0;
    SOLDE = :SOLDE + :DEBIT;

	SELECT
    	COALESCE( SUM(regl_tiers.montant),0)
	FROM regl_tiers
	WHERE regl_tiers.type_regt = 0
	AND id_cpte = :ID_CPTE
	AND regl_tiers.date_regt  <= :AU
	AND (UPPER(regl_tiers.etat) = 'OK')
	INTO :CREDIT;
    IF (:CREDIT IS NULL) THEN CREDIT = 0;
    SOLDE = :SOLDE - :CREDIT;

	SELECT
    	COALESCE( SUM(depenses.montant),0)
	FROM depenses
	WHERE id_cpte = :ID_CPTE
	AND DEPENSES.date_dep <= :AU
	AND (UPPER(DEPENSES.etat) = 'OK')
	INTO :CREDIT;
    IF (:CREDIT IS NULL) THEN CREDIT = 0;
    SOLDE = :SOLDE - :CREDIT;


	SELECT
		COALESCE( SUM(VIR_FONDS.montant),0 )
	FROM VIR_FONDS
	WHERE id_compte_debit = :ID_CPTE
	AND VIR_FONDS.date_oper <= :AU
	INTO :DEBIT;
    IF (:DEBIT IS NULL) THEN DEBIT = 0;
	SOLDE = :SOLDE + :DEBIT;

	SELECT
    	COALESCE( SUM(VIR_FONDS.MONTANT),0 )
	FROM VIR_FONDS
	WHERE id_compte_CREDIT = :ID_CPTE
	AND VIR_FONDS.date_oper <= :AU
	INTO :CREDIT;
    IF (:CREDIT IS NULL) THEN CREDIT = 0;
    SOLDE = :SOLDE - :CREDIT;

  SUSPEND;
END^

SET TERM ; ^

SET TERM ^ ;

ALTER PROCEDURE NUM_CONSIGNE
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
           max(cast((select OPARAM from get_digit_only( LIGNES_CONSIGNATION.NUM_PIECE )) as integer))
     FROM LIGNES_CONSIGNATION
     INTO :NUM;
     if (NUM IS NULL) then  NUM = 0;
     NUM = NUM + 1;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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_client IS NOT null
        AND CLIENTS.solde_ini_client <> 0
        AND ((CLIENTS.client = 1)or(CLIENTS.mixte = 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_client
          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.date_piece < :D1
          and entete_sorties.ajouter_creance = 1
          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.ajouter_creance = 1
          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(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
          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.ajouter_creance = 1
          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.ajouter_creance = 1
          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(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
          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^

SET TERM ; ^

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

SET TERM ^ ;

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_fourn IS NOT null)
        AND (CLIENTS.solde_ini_fourn <> 0)
        AND ((CLIENTS.client = 0)or(CLIENTS.mixte = 1))

        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_fourn
          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(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
          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(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
          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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE BALANCE_STOCK_MENSUEL (
  MATIERE SmallInt)
 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
     inner join ARTICLES on ARTICLES.ID_ART = inventaire.ID_ARTICLE
     where ARTICLES.MATIERE = :MATIERE
     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(ENTREES.VALEUR_STOCK)
         FROM ENTREES
         inner join ARTICLES on ARTICLES.ID_ART = ENTREES.ID_ARTICLE
         where ARTICLES.MATIERE = :MATIERE
         and extract(MONTH FROM ENTREES.DATE_ENTREE) = :CODE_MOIS
         AND ENTREES.nature_entree = 1
         INTO :ACHAT_IMPO;

         SELECT
               SUM(HT)
         FROM ENTREES
         inner join ARTICLES on ARTICLES.ID_ART = ENTREES.ID_ARTICLE
         WHERE extract(MONTH FROM DATE_ENTREE) = :CODE_MOIS
         AND ARTICLES.MATIERE = :MATIERE
         AND entrees.nature_entree in(2,3)
         INTO :ACHAT_LOC;

         SELECT
               SUM(HT)
         FROM ENTREES
         inner join ARTICLES on ARTICLES.ID_ART = ENTREES.ID_ARTICLE
         WHERE extract(MONTH FROM DATE_ENTREE) = :CODE_MOIS
         AND ARTICLES.MATIERE = :MATIERE
         AND ENTREES.nature_entree = 4
         INTO :AVOIR_ACHAT;

         SELECT
               SUM(SORTIES.CONSOMATION)
         FROM SORTIES
         inner join ARTICLES on ARTICLES.ID_ART = SORTIES.ID_ARTICLE
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND ARTICLES.MATIERE = :MATIERE
         AND SORTIES.nature_sortie <> 5
         INTO :CONSOMMATIONS;

         SELECT
               SUM(SORTIES.HT)
         FROM SORTIES
         inner join ARTICLES on ARTICLES.ID_ART = SORTIES.ID_ARTICLE
         WHERE extract(MONTH FROM DATE_PIECE) = :CODE_MOIS
         AND ARTICLES.MATIERE = :MATIERE
         AND SORTIES.nature_sortie IN (5,7)
         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^

SET TERM ; ^

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

SET TERM ^ ;

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))
         and ENTETE_SORTIES.AJOUTER_CREANCE = 1
         and ENTETE_SORTIES.DESTOCKER = 1
         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)or(entete_sorties.nature_sortie = 7))
         and ENTETE_SORTIES.AJOUTER_CREANCE = 1
         and ENTETE_SORTIES.DESTOCKER = 1
         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(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
         FROM regl_tiers
         WHERE extract(MONTH FROM regl_tiers.date_regt) = :CODE_MOIS
         AND ((regl_tiers.type_regt = 1)or(regl_tiers.type_regt = 2))
         INTO :T_REGT;
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE BASE_CALCUL_PAMP(
  D DATE,
  ID_ART INTEGER,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_MVT INTEGER,
  DATE_OPER DATE,
  P_A_M_P FLOAT,
  ID_OPER INTEGER,
  OPER SMALLINT)
AS
DECLARE VARIABLE QUANTITE NUMERIC(18, 6);
DECLARE VARIABLE COUT NUMERIC(18, 2);
DECLARE VARIABLE STOCK NUMERIC(18, 6);
BEGIN
     STOCK = 0;
     P_A_M_P = 0;
     ID_MVT = 0;
     FOR
        SELECT
        	  CAST(inventaire.ID_INVENTAIRE as INTEGER),
              CAST(INVENTAIRE.date_inventaire AS DATE),
              cast(inventaire.QUANTITE AS numeric(18,6)),
              cast(inventaire.cout AS numeric(18,2)),
              1
        FROM inventaire
        WHERE inventaire.id_article = :ID_ART
        AND   INVENTAIRE.id_magazin = :ID_MAGAZIN

        UNION all

        SELECT
        	  CAST(ENTREES.ID_ENTREE as INTEGER),
              CAST(ENTREES.date_entree AS DATE),
              cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(ENTREES.cout_achat AS numeric(18,2)),
              2
        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
        	  CAST(SORTIES.ID_SORTIE as INTEGER),
              CAST(SORTIES.date_piece AS DATE),
              cast(sorties.QUANTITE AS numeric(18,6)),
              cast(sorties.cr_u AS numeric(18,2)),
              3
        FROM sorties
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE ((sorties.nature_sortie = 5) OR (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
        	  CAST(lignes_transfert.ID_LIGNE as INTEGER),
              CAST(lignes_transfert.date_piEce AS DATE),
              cast(lignes_transfert.QUANTITE AS numeric(18,6)),
              cast(lignes_transfert.pamp AS numeric(18,2)),
              4
        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
        	  CAST(ENTREES.ID_ENTREE as INTEGER),
              CAST(ENTREES.date_entree AS DATE),
              -cast(ENTREES.QUANTITE AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              5
        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
        	  CAST(SORTIES.ID_SORTIE as INTEGER),
              CAST(SORTIES.date_piece AS DATE),
              -cast(sorties.QUANTITE AS numeric(18,6)),
              cast(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.nature_sortie <> 5 AND sorties.nature_sortie <> 7
        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
             CAST(lignes_transfert.ID_LIGNE as INTEGER),
             CAST(lignes_transfert.date_piEce AS DATE),
             -cast(lignes_transfert.QUANTITE AS numeric(18,6)),
             cast(lignes_transfert.pamp AS numeric(18,2)),
             7
        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 2,5,1 desc
        INTO :ID_OPER, :DATE_OPER, :QUANTITE, :COUT, :Oper

      DO
        BEGIN
        	ID_MVT = :ID_MVT + 1;
        	if (:QUANTITE IS NULL) then QUANTITE = 0;
            if (:COUT IS NULL) then COUT = 0;

        	IF (:QUANTITE < 0) then
            	P_A_M_P = :P_A_M_P;
            else
            	if ((:STOCK + :QUANTITE) <> 0) then
                	P_A_M_P = ((:STOCK * :P_A_M_P) + (:QUANTITE * :COUT))/(:STOCK + :QUANTITE);
                else
                	P_A_M_P = 0;
            STOCK = :STOCK + :QUANTITE;
             SUSPEND;
        END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE BASE_CALCUL_PAMP_ALL(
  D DATE)
RETURNS(
  ANC_ID_ART INTEGER,
  ANC_ID_MAG INTEGER,
  ID_MVT INTEGER,
  ID_ARTICLE INTEGER,
  ID_MAGAZIN INTEGER,
  DATE_OPER DATE,
  EX_STOCK NUMERIC(18, 6),
  QUANTITE NUMERIC(18, 6),
  STOCK NUMERIC(18, 6),
  COUT NUMERIC(18, 2),
  VALEUR Double Precision,
  CURRENT_PAW NUMERIC(18, 2),
  P_A_M_P FLOAT,
  ID_OPER INTEGER,
  OPER SMALLINT)
AS
BEGIN
     ANC_ID_ART = -1;
     ANC_ID_MAG = -1;
     FOR
     	SELECT
	INVENTAIRE.ID_INVENTAIRE,
              inventaire.id_article,
              inventaire.ID_MAGAZIN,
              INVENTAIRE.date_inventaire,
              inventaire.QUANTITE,
              inventaire.cout,
              1
        FROM inventaire

        UNION all

        SELECT
        	ENTREES.id_entree,
        	entrees.id_article,
              ENTREES.ID_MAGAZIN,
              ENTREES.date_entree,
              ENTREES.QUANTITE,
              ENTREES.cout_achat,
              2
        FROM entrees
        WHERE entrees.nature_entree <> 4
        AND entrees.stocker = 1
        AND ENTREES.date_entree <= :D

		UNION ALL

        SELECT
        	 sorties.ID_SORTIE,
              SORTIES.id_article,
              SORTIES.ID_MAGAZIN,
              SORTIES.date_piece,
              sorties.QUANTITE,
              sorties.cr_u,
              3
        FROM sorties
        WHERE ((sorties.nature_sortie = 5)or(sorties.nature_sortie = 7))
        AND sorties.destocker = 1
        AND SORTIES.date_piece <= :D

        UNION all

        SELECT
        	LIGNES_TRANSFERT.ID_LIGNE,
              lignes_transfert.id_article,
              lignes_transfert.id_magazin_2,
              lignes_transfert.date_piEce,
              lignes_transfert.QUANTITE,
              lignes_transfert.pamp,
              4
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        WHERE entete_transfert.entree_stock = 1
		and lignes_transfert.date_piEce <= :D

        UNION all

        SELECT
        	  ENTREES.id_entree,
              ENTREES.id_article,
              ENTREES.ID_MAGAZIN,
              ENTREES.date_entree,
              -ENTREES.QUANTITE,
              0,
              5
        FROM entrees
        WHERE entrees.nature_entree = 4
        AND entrees.stocker = 1
		and ENTREES.date_entree <= :D

        UNION ALL

        SELECT
        	  sorties.ID_SORTIE,
              SORTIES.id_article,
              SORTIES.ID_MAGAZIN,
              SORTIES.date_piece,
              -sorties.QUANTITE,
              0,
              6
        FROM sorties
        WHERE ((sorties.nature_sortie <> 5)and(sorties.nature_sortie <> 7))
        AND sorties.destocker = 1
        AND SORTIES.cession = 0
        and SORTIES.date_piece <= :D

        UNION   ALL

        SELECT
        	LIGNES_TRANSFERT.ID_LIGNE,
              lignes_transfert.id_article,
              lignes_transfert.id_magazin_1,
              lignes_transfert.date_piEce,
              -lignes_transfert.QUANTITE,
              0,
              7
        FROM lignes_transfert
        inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete
        WHERE entete_transfert.sortie_stock = 1
        and lignes_transfert.date_piEce <= :D

        ORDER BY 3,2,4,7,1 desc
        INTO :ID_OPER, :ID_ARTICLE, :ID_MAGAZIN, :DATE_OPER, :QUANTITE, :COUT, :OPER

      DO
        BEGIN
			 if (ANC_ID_MAG <> ID_MAGAZIN) then
             	BEGIN
                   ANC_ID_MAG = :ID_MAGAZIN;
             		ANC_ID_ART = -1;
                END

             if (ANC_ID_ART <> ID_ARTICLE) then
             	begin
                    ANC_ID_ART = :ID_ARTICLE;
                    STOCK = 0;
                    VALEUR = 0;
                    P_A_M_P = 0;
                    ID_MVT = 0;
                    EX_STOCK = 0;
                end

             IF (:QUANTITE >= 0) then
             	BEGIN
             	     VALEUR = cast(VALEUR + (QUANTITE * COUT) as NUMERIC(18,2));
                     STOCK = STOCK + QUANTITE;
                     if ( STOCK <> 0  )then
                      current_paw = VALEUR / STOCK;
                     else
                      current_paw = 0;

                    /* IF (STOCK > 0) THEN
                        BEGIN
                             if (EX_STOCK <= 0) then
                                current_paw = COUT;
                             ELSE
                                 current_paw = VALEUR / STOCK;
                             END
                        ELSE
                            BEGIN
                                 current_paw = COUT;
                            END*/

                END
             ELSE
                 BEGIN
                      IF (STOCK > 0) THEN
                         BEGIN
                              VALEUR = cast(VALEUR + (QUANTITE * current_paw) as NUMERIC(18,2));
                              STOCK = STOCK + QUANTITE;

                         END
                      IF (STOCK <= 0) THEN
                      	BEGIN
                        	  VALEUR = 0;
                        END
                 END
             ID_MVT = ID_MVT + 1;
             P_A_M_P = cast(current_paw as NUMERIC(18,2));
             SUSPEND;
        END
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE CALCUL_TIMBRE (
  ASSIETTE NUMERIC(18,2),
  TAUX     NUMERIC(18,2),
  VAL_MIN  NUMERIC(18,2),
  VAL_MAX  NUMERIC(18,2)
) RETURNS (
  TIMBRE   NUMERIC(18,2)
)
AS
BEGIN
  TIMBRE = :ASSIETTE * :TAUX / 100;
  if (:TIMBRE < :VAL_MIN) then TIMBRE = :VAL_MIN;
  if (:TIMBRE > :VAL_MAX) then TIMBRE = :VAL_MAX;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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 NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  REMISE NUMERIC(18, 2),
  CPTE_TIER VARCHAR(12),
  MNT_TIER NUMERIC(18, 2),
  TIERS VARCHAR(128),
  ID_MAG Integer)
AS
BEGIN
     FOR
        SELECT distinct
              ENTREES.id_entete,
              entete_entrees.ID_MAGAZIN,
              entete_entrees.num_fact,
              entete_entrees.info_reglement,
              ENTREES.id_four,
              ENTREES.date_entree,
              ARTICLES.cpte_achat,
              ARTICLES.cpte_stock,
              CLIENTS.cpte,
              sum(entrees.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
        group by ENTREES.id_entete,
              entete_entrees.num_fact,
              entete_entrees.ID_MAGAZIN,
              entete_entrees.info_reglement,
              ENTREES.id_four,
              ENTREES.date_entree,
              ARTICLES.cpte_achat,
              ARTICLES.cpte_stock,
              CLIENTS.cpte,
              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

        INTO :ID_ENTET_ENTREE, :ID_MAG, :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^

SET TERM ; ^

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

SET TERM ^ ;

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),
  ID_MAG INTEGER)
AS
BEGIN
     FOR
        SELECT
              SORTIES.id_entete,
              entete_sorties.ID_MAGAZIN,
              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,
                 entete_sorties.ID_MAGAZIN,
                 sorties.id_client,
                 SORTIES.date_piece,
                 ARTICLES.cpte_stock,
                 ARTICLES.cpte_consom

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE COMPA_CONSOM_ANNUEL_DEPOT(
ID_MAG Integer)
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
        where SORTIES.ID_MAGAZIN = :ID_MAG
        GROUP BY
              ARTICLES.cpte_consom, ARTICLES.cpte_stock

        INTO :CPTE_STOCK, :CPTE_CONSOM, :CONSOM
     do
     BEGIN

  SUSPEND;
  END
END^

SET TERM ; ^


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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE COMPA_CONSOM_MOIS_DEPOT(ID_MAG Integer)
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
        where SORTIES.ID_MAGAZIN = :ID_MAG
        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
          AND SORTIES.ID_MAGAZIN = :ID_MAG
          INTO :CONSOM;

  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

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),
  CODE_TIERS VARCHAR(12),
  TIERS VARCHAR(128))
AS
DECLARE VARIABLE ID_TIERS INTEGER;
BEGIN
        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,
              clients.DENOMINATION,
              CLIENTS.CODE
        FROM DEPENSES
        INNER JOIN CLIENTS ON CLIENTS.ID_CLIENT = DEPENSES.ID_TIERS
        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, :TIERS, :CODE_TIERS;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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 + coalesce((select sum(FACT_DD_JOINTE.MNT_HT)*100 from FACT_DD_JOINTE where FACT_DD_JOINTE.ID_DD = entrees.ID_ENTETE),0),
              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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE COMPA_ENTREE_STOCK_DEPOT(
  DU DATE,
  AU DATE,
  ID_MAG INTEGER)
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 + coalesce((select sum(FACT_DD_JOINTE.MNT_HT)*100 from FACT_DD_JOINTE where FACT_DD_JOINTE.ID_DD = entrees.ID_ENTETE),0),
              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
        AND ENTETE_ENTREES.ID_MAGAZIN = :ID_MAG
        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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE COMPA_REGL_TIERS(
  DU DATE,
  AU DATE)
RETURNS(
  NUM_REGL VARCHAR(12),
  DATE_REGL DATE,
  CODE_TIERS VARCHAR(12),
  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,
              CLIENTS.CODE,
              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,  :CODE_TIERS, :CPTE_CLIENT, :CPTE_TRESOR,
             :NUM_CHEQUE, :MONTANT, :TYPE_REG, :TIERS
     do
     BEGIN
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

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),
  ID_MAG INTEGER)
AS
BEGIN
     FOR
        SELECT distinct
              SORTIES.id_entete,
              ENTETE_SORTIES.ID_MAGAZIN,
              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
        and SORTIES.NATURE_SORTIE = 1
        GROUP BY
        SORTIES.id_entete,
              entete_sorties.num_piece,
              ENTETE_SORTIES.ID_MAGAZIN,
              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,:ID_MAG, :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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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),
  FAMILLE VARCHAR(128)
)
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);
DECLARE VARIABLE ID_FAMILLE INTEGER;
BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               ARTICLES.matiere,
               ARTICLES.importation,
               ARTICLES.ID_FAMILLE
        FROM sorties
        INNER  JOIN ARTICLES ON ARTICLES.id_art = SORTIES.id_article
        WHERE sorties.date_piece between :d1 AND :d2
        AND articles.matiere <> 1
        and sorties.DESTOCKER = 1
        INTO :ID_ART, :ARTICLE, :MATIERE, :IMPORT, :ID_FAMILLE

     do
     BEGIN
          SELECT
                FAMILLES.FAMILLE
          FROM FAMILLES
          WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
          INTO :FAMILLE;

          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.DESTOCKER = 1
          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.DESTOCKER = 1
          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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE CONSOMMATION_DEPOT (
  D1         DATE,
  D2         DATE,
  ID_MAGAZIN INTEGER
) RETURNS (
  ID_ART     INTEGER,
  MATIERE    INTEGER,
  ARTICLE    VARCHAR(128),
  IMPORT     INTEGER,
  FP         SMALLINT,
  GROS       SMALLINT,
  QTE        NUMERIC(18,6),
  COUT       NUMERIC(18,2),
  VALEUR     NUMERIC(18,2),
  FAMILLE    VARCHAR(128)
)
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);
DECLARE VARIABLE ID_FAMILLE integer;
BEGIN
     FOR
        SELECT distinct
               sorties.id_article,
               ARTICLES.designation,
               ARTICLES.matiere,
               ARTICLES.importation,
               sorties.gros,
               SORTIES.f_p,
               ARTICLES.ID_FAMILLE
        FROM sorties
        INNER 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
        AND sorties.DESTOCKER = 1
        INTO :ID_ART, :ARTICLE, :MATIERE, :IMPORT, :GROS, :FP, :ID_FAMILLE

     do
     BEGIN
          SELECT
                FAMILLES.FAMILLE
          FROM FAMILLES
          WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
          INTO :FAMILLE;

          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
          AND sorties.DESTOCKER = 1
          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
          AND sorties.DESTOCKER = 1
          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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE ETAT_104(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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(CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                     WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END AS varchar(256)),
           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_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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE ETAT_104_MAGASIN(
  D1 DATE,
  D2 DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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(CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                     WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END AS varchar(256)),
           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^

SET TERM ; ^


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

SET TERM ^ ;

ALTER PROCEDURE ETAT_104_GROS(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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(CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                     WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END AS varchar(256)),
           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.refaction = 1
        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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE ETAT_104_PART(
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  DENOMINATION VARCHAR(128),
  ADRESSE VARCHAR(256),
  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
     FOR
     SELECT
           entete_sorties.id_client,
           cast(clients.denomination AS varchar(128)),
           cast(CASE WHEN clients.adresse2 IS NULL THEN clients.adresse1
                     WHEN clients.adresse2 IS NOT  NULL THEN clients.adresse1||' '||clients.adresse2
               END AS varchar(256)),
           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 clients.particulier = 1
     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^

SET TERM ; ^

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

SET TERM ^ ;

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),
  ECART        NUMERIC(18,2),
  ROTATION     INTEGER,
  CODE_TIERS   VARCHAR(12)
)
AS
DECLARE variable VENTE_ANT NUMERIC (18, 2);
  DECLARE variable REGLT_ANT NUMERIC (18, 2);
  DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
  DECLARE variable TOTAL_AVOIR NUMERIC (18, 2);
  DECLARE variable START_DATE DATE;
  DECLARE variable NBRE_JOURS INTEGER;
BEGIN
     FOR
        SELECT
              id_client
        FROM CLIENTS
        WHERE CLIENTS.solde_ini_client IS NOT null
        AND CLIENTS.solde_ini_client <> 0
        AND ((CLIENTS.client = 1)or(CLIENTS.mixte = 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)
        AND LEFT(REGL_TIERS.ETAT, 5) <> 'Annul'
        INTO :ID_TIER

     DO
       BEGIN
            SOLDE_INI = 0;
            TOTAL_VENTE = 0;
            TOTAL_AVOIR = 0;
            REGLT_ANT = 0;
       		SELECT
                  CLIENTS.DENOMINATION,
                  CLIENTS.SOLDE_INI_CLIENT,
                  CLIENTS.CODE
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :DENOMINATION, :SOLDE_INI, :CODE_TIERS;

            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;


            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 1) OR (regl_tiers.type_regt = 2))
            AND id_tier <> -1
            AND regl_tiers.date_regt < :D1
            AND LEFT(REGL_TIERS.ETAT, 5) <> 'Annul'
            INTO :REGLT_ANT;

       		if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
       		if (:SOLDE_INI <> 0) then
       		   begin
       		        SELECT
                          PARAMETRES.EXER_DU
                     FROM PARAMETRES
                     INTO :START_DATE;
       		   end
            else
                begin
                     select
                           min(ENTETE_SORTIES.DATE_PIECE)
                     from ENTETE_SORTIES
                     where ENTETE_SORTIES.ID_CLIENT = :ID_TIER
                     and ((ENTETE_SORTIES.NATURE_SORTIE = 1)or(ENTETE_SORTIES.NATURE_SORTIE = 2))
                     into :START_DATE;
                end
       		if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR IS NULL) then TOTAL_AVOIR = 0;
            if (REGLT_ANT IS NULL) then REGLT_ANT = 0;
            VENTE_ANT = :TOTAL_VENTE - :TOTAL_AVOIR;
            if (VENTE_ANT IS NULL) THEN VENTE_ANT = 0;
            SOLDE_INI = :SOLDE_INI + :VENTE_ANT - :REGLT_ANT;

            TOTAL_VENTE = 0;
            TOTAL_AVOIR = 0;
            REGLEMENT = 0;
            CREANCE = 0;

            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;
            if (TOTAL_VENTE is null) then :TOTAL_VENTE = 0;

            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_AVOIR is null) then :TOTAL_AVOIR = 0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            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
            AND LEFT(REGL_TIERS.ETAT, 5) <> 'Annul'
            INTO :REGLEMENT;
            if (REGLEMENT IS NULL) then REGLEMENT = 0;

            CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;
            if (CREANCE IS NULL) then CREANCE = 0;
            ECART = :SOLDE_INI + :CREANCE - :REGLEMENT;
            if (ECART IS NULL) then ECART = 0;

            if (:START_DATE is not null) then
               NBRE_JOURS = DATEDIFF (DAY from :START_DATE to :D2);
            else
                NBRE_JOURS = 0;

            IF ((:SOLDE_INI + :CREANCE <> 0)and(:ECART > 0)) THEN
               ROTATION = (:ECART/(:SOLDE_INI + :CREANCE))*:NBRE_JOURS;
            ELSE
                ROTATION = 0;
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE ETAT_DETTE_CREANCES (
  D1           DATE,
  D2           DATE
) RETURNS (
  ID_TIER          INTEGER,
  DENOMINATION     VARCHAR(128),
  SOLDE_INI_FOURN  NUMERIC(18,2),
  ACHATS           NUMERIC(18,2),
  REGLEMENT_FOURN        NUMERIC(18,2),
  ECART_FOURN      NUMERIC(18,2),
  SOLDE_INI_CLIENT    NUMERIC(18,2),
  CREANCE      NUMERIC(18,2),
  REGLEMENT_CLIENT    NUMERIC(18,2),
  ECART_CLIENT       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 TRANS_ANT NUMERIC(18, 2);
DECLARE VARIABLE IMPOR_ANT NUMERIC(18, 2);
DECLARE VARIABLE AVOIR_ANT NUMERIC(18, 2);
DECLARE VARIABLE REGLT_ANT_FOURN NUMERIC(18, 2);
DECLARE VARIABLE FACT_DD NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_ACHAT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_TRANS NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_IMPOR NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIR_F NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_DEP NUMERIC(18, 2);
DECLARE VARIABLE AV_COM NUMERIC(18, 2);
DECLARE variable VENTE_ANT NUMERIC (18, 2);
DECLARE variable REGLT_ANT_CLIENT NUMERIC (18, 2);
DECLARE variable TOTAL_VENTE NUMERIC (18, 2);
DECLARE variable TOTAL_AVOIR_C NUMERIC (18, 2);

BEGIN
     FOR
        SELECT
              CLIENTS.id_client,
              CLIENTS.DENOMINATION
        FROM CLIENTS
        WHERE CLIENTS.mixte = 1

              INTO :ID_TIER, :DENOMINATION
     DO
       BEGIN
            SOLDE_INI_FOURN = 0;
            ACHAT_ANT = 0;
            TRANS_ANT = 0;
            IMPOR_ANT = 0;
            AVOIR_ANT = 0;
            FACT_D_ANT = 0;

            SELECT
            	SOLDE_INI_FOURN
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :SOLDE_INI_FOURN;
            if (SOLDE_INI_FOURN IS NULL) then SOLDE_INI_FOURN =0;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND ((entete_entrees.nature_entree = 2)or(entete_entrees.nature_entree = 5))
            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_SORTIES.MONTANT_TR)
            FROM ENTETE_SORTIES
            WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_SORTIES.DATE_PIECE < :D1
            INTO :TRANS_ANT;
            if (TRANS_ANT IS NULL) then TRANS_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_d10 < :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
            and depenses.id_cpte is null
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 0)
            AND regl_tiers.date_regt < :D1
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :REGLT_ANT_FOURN;
            if (REGLT_ANT_FOURN IS NULL) then REGLT_ANT_FOURN = 0;

             SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 3)
            AND regl_tiers.date_regt < :D1
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :AV_COM;
            if (AV_COM IS NULL) then AV_COM = 0;
            REGLT_ANT_FOURN = :REGLT_ANT_FOURN + :AV_COM;

            SOLDE_INI_FOURN = :SOLDE_INI_FOURN + :ACHAT_ANT + :TRANS_ANT + :IMPOR_ANT + :FACT_D_ANT + :TOTAL_DEP - :AVOIR_ANT - :REGLT_ANT_FOURN;

            SELECT
                  sum(entete_entrees.net_a_payer)
            FROM entete_entrees
            WHERE entete_entrees.id_fourn = :ID_TIER
            AND ((entete_entrees.nature_entree = 2)or(entete_entrees.nature_entree = 5))
            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_SORTIES.MONTANT_TR)
            FROM ENTETE_SORTIES
            WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_SORTIES.DATE_PIECE BETWEEN :D1 AND :D2
            INTO :TOTAL_TRANS;
            if (TOTAL_TRANS IS NULL) then TOTAL_TRANS =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.date_d10 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 (entete_entrees.nature_entree = 4)
            AND entete_entrees.date_fact BETWEEN :D1 AND :D2
            AND entete_entrees.ajouter_dettes = 1
            INTO :TOTAL_AVOIR_F;
            if (TOTAL_AVOIR_F IS NULL) then TOTAL_AVOIR_F = 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
            and depenses.id_cpte is null
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

            ACHATS = :TOTAL_ACHAT + :TOTAL_TRANS + :TOTAL_IMPOR + :TOTAL_DEP + :FACT_DD - :TOTAL_AVOIR_F;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            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
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :REGLEMENT_FOURN;
            if (REGLEMENT_FOURN IS NULL) then REGLEMENT_FOURN = 0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            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
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :av_com;
            if (av_com IS NULL) then av_com = 0;
            REGLEMENT_FOURN = :REGLEMENT_FOURN + :av_com;
            if (REGLEMENT_FOURN IS NULL) then REGLEMENT_FOURN = 0;
            ECART_FOURN = :SOLDE_INI_FOURN + :ACHATS - :REGLEMENT_FOURN;
            if (ECART_FOURN IS NULL) then ECART_FOURN = 0;

            SOLDE_INI_CLIENT = 0;
            TOTAL_VENTE = 0;
            TOTAL_AVOIR_C = 0;
            REGLT_ANT_CLIENT = 0;
       		SELECT
                  denomination
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :DENOMINATION;

            SELECT
                  SOLDE_INI_CLIENT
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :SOLDE_INI_CLIENT;

            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_C;


            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND ((regl_tiers.type_regt = 1) OR (regl_tiers.type_regt = 2))
            AND id_tier <> -1
            AND regl_tiers.date_regt < :D1
            AND LEFT(REGL_TIERS.ETAT, 5) <> 'Annul'
            INTO :REGLT_ANT_CLIENT;
       		if (SOLDE_INI_CLIENT IS NULL) then SOLDE_INI_CLIENT = 0;
       		if (TOTAL_VENTE IS NULL) then TOTAL_VENTE = 0;
            if (TOTAL_AVOIR_C IS NULL) then TOTAL_AVOIR_C = 0;
            if (REGLT_ANT_CLIENT IS NULL) then REGLT_ANT_CLIENT = 0;
            VENTE_ANT = :TOTAL_VENTE - :TOTAL_AVOIR_C;
            if (VENTE_ANT IS NULL) THEN VENTE_ANT = 0;
            SOLDE_INI_CLIENT = :SOLDE_INI_CLIENT + :VENTE_ANT - :REGLT_ANT_CLIENT;

            TOTAL_VENTE = 0;
            TOTAL_AVOIR_C = 0;
            REGLEMENT_CLIENT = 0;
            CREANCE = 0;

            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;
            if (TOTAL_VENTE is null) then :TOTAL_VENTE = 0;

            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_C;
            if (TOTAL_AVOIR_C is null) then :TOTAL_AVOIR_C = 0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            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
            AND LEFT(REGL_TIERS.ETAT, 5) <> 'Annul'
            INTO :REGLEMENT_CLIENT;
            if (REGLEMENT_CLIENT IS NULL) then REGLEMENT_CLIENT = 0;

            CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR_C;
            if (CREANCE IS NULL) then CREANCE = 0;
            ECART_CLIENT = :SOLDE_INI_CLIENT + :CREANCE - :REGLEMENT_CLIENT;
            if (ECART_CLIENT IS NULL) then ECART_CLIENT = 0;

            ECART = :ECART_CLIENT - :ECART_FOURN;
            if (ECART IS NULL) then ECART = 0;

  SUSPEND;
  END
  END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE ETAT_DETTES_LOCALES(
  D1 DATE,
  D2 DATE)
RETURNS(
  CODE_TIERS VARCHAR(12),
  ID_TIER INTEGER,
  DENOMINATION VARCHAR(128),
  SOLDE_INI NUMERIC(18, 2),
  ACHATS NUMERIC(18, 2),
  REGLEMENT NUMERIC(18, 2),
  ECART NUMERIC(18, 2),
  ROTATION INTEGER)
AS
DECLARE VARIABLE FACT_D_ANT NUMERIC(18, 2);
DECLARE VARIABLE ACHAT_ANT NUMERIC(18, 2);
DECLARE VARIABLE TRANS_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_TRANS 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);
DECLARE VARIABLE START_DATE DATE;
DECLARE VARIABLE NBRE_JOURS INTEGER;
DECLARE VARIABLE TRANS_ACHATS NUMERIC(18, 2);
BEGIN
     FOR
     	     SELECT
              id_client,
              clients.DENOMINATION
        FROM CLIENTS
        WHERE (CLIENTS.solde_ini_FOURN IS NOT null)
        AND (CLIENTS.solde_ini_FOURN <> 0)
        AND ((CLIENTS.client = 0)or(CLIENTS.mixte = 1))

        UNION

        SELECT
              id_FOURN,
              clients.DENOMINATION
        FROM entete_entrees
        inner join clients on clients.ID_CLIENT = ENTETE_ENTREES.ID_FOURN
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 2
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              id_FOURN,
              clients.DENOMINATION
        FROM entete_entrees
        inner join clients on clients.ID_CLIENT = ENTETE_ENTREES.ID_FOURN
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 5
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              id_FOURN,
              clients.DENOMINATION
        FROM entete_entrees
        inner join clients on clients.ID_CLIENT = ENTETE_ENTREES.ID_FOURN
        WHERE ID_FOURN > 0
        AND entete_entrees.nature_entree = 1
        AND   ajouter_DETTES = 1

        UNION

        SELECT
              fact_dd_jointe.ID_FOURN,
              clients.DENOMINATION
        FROM fact_dd_jointe
        inner join clients on clients.ID_CLIENT = fact_dd_jointe.ID_FOURN

        UNION

        SELECT
              id_tier,
              clients.DENOMINATION
        FROM regl_tiers
        inner join clients on clients.ID_CLIENT = regl_tiers.ID_TIER
        WHERE regl_tiers.id_tier > 0
        AND ((regl_tiers.type_regt = 0)OR(regl_tiers.type_regt = 3))
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION

        SELECT
              DEPENSES.ID_TIERS,
              clients.DENOMINATION
        FROM DEPENSES
        inner join clients on clients.ID_CLIENT = DEPENSES.ID_TIERS
        WHERE DEPENSES.ID_TIERS is not null
        and depenses.id_cpte is null

        UNION

        SELECT
              ENTETE_SORTIES.ID_FOUR_TR,
              clients.DENOMINATION
        FROM ENTETE_SORTIES
        inner join clients on clients.ID_CLIENT = ENTETE_SORTIES.ID_FOUR_TR
        WHERE ENTETE_SORTIES.ID_FOUR_TR is not null
        and ENTETE_SORTIES.MONTANT_TR is not null

        UNION

        SELECT
              ENTETE_ENTREES.ID_FOUR_TR,
              clients.DENOMINATION
        FROM ENTETE_ENTREES
        inner join clients on clients.ID_CLIENT = ENTETE_ENTREES.ID_FOUR_TR
        WHERE ENTETE_ENTREES.ID_FOUR_TR is not null
        and ENTETE_ENTREES.MONTANT_TR is not null

        INTO :ID_TIER, :DENOMINATION
     DO
       BEGIN
            SOLDE_INI = 0;
            ACHAT_ANT = 0;
            TRANS_ANT = 0;
            IMPOR_ANT = 0;
            AVOIR_ANT = 0;
            FACT_D_ANT = 0;

            if (SOLDE_INI IS NULL) then SOLDE_INI =0;

            SELECT
            	SOLDE_INI_FOURN,
            	CODE
            FROM CLIENTS
            WHERE ID_CLIENT = :ID_TIER
            INTO :SOLDE_INI, CODE_TIERS;
            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)or(entete_entrees.nature_entree = 5))
            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_SORTIES.MONTANT_TR)
            FROM ENTETE_SORTIES
            WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_SORTIES.DATE_PIECE < :D1
            INTO :TRANS_ANT;
            if (TRANS_ANT IS NULL) then TRANS_ANT =0;


            SELECT
                  sum(ENTETE_ENTREES.MONTANT_TR)
            FROM ENTETE_ENTREES
            WHERE ENTETE_ENTREES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_ENTREES.date_fact < :D1
            INTO :TRANS_ACHATS;
            if (TRANS_ACHATS IS NULL) then TRANS_ACHATS =0;

			TRANS_ANT = :TRANS_ANT + :TRANS_ACHATS;



            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_d10 < :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
            and depenses.id_cpte is null
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 0)
            AND regl_tiers.date_regt < :D1
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :REGLT_ANT;
            if (REGLT_ANT IS NULL) then REGLT_ANT = 0;

             SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            FROM regl_tiers
            WHERE regl_tiers.id_tier = :ID_TIER
            AND (regl_tiers.type_regt = 3)
            AND regl_tiers.date_regt < :D1
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :AV_COM;
            if (AV_COM IS NULL) then AV_COM = 0;
            REGLT_ANT = :REGLT_ANT + :AV_COM;

            if (:SOLDE_INI <> 0) then
       		   begin
       		        SELECT
                          PARAMETRES.EXER_DU
                     FROM PARAMETRES
                     INTO :START_DATE;
       		   end
            else
                begin
                     select  first 1
                           case when ENTETE_ENTREES.NATURE_ENTREE = 2 then min(ENTETE_ENTREES.DATE_FACT)
                                when ENTETE_ENTREES.NATURE_ENTREE = 1 then min(ENTETE_ENTREES.DATE_D10) end
                     from ENTETE_ENTREES
                     where  ((ENTETE_ENTREES.NATURE_ENTREE = 1)or(ENTETE_ENTREES.NATURE_ENTREE = 2))
                     and ENTETE_ENTREES.ID_FOURN = :ID_TIER
                     group by ENTETE_ENTREES.ID_FOURN, ENTETE_ENTREES.NATURE_ENTREE
                     into :START_DATE;
                end

            SOLDE_INI = :SOLDE_INI + :ACHAT_ANT + :TRANS_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 ((entete_entrees.nature_entree = 2)or(entete_entrees.nature_entree = 5))
            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_SORTIES.MONTANT_TR)
            FROM ENTETE_SORTIES
            WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_SORTIES.DATE_PIECE BETWEEN :D1 AND :D2
            INTO :TOTAL_TRANS;
            if (TOTAL_TRANS IS NULL) then TOTAL_TRANS =0;

            SELECT
                  sum(ENTETE_ENTREES.MONTANT_TR)
            FROM ENTETE_ENTREES
            WHERE ENTETE_ENTREES.ID_FOUR_TR = :ID_TIER
            AND ENTETE_ENTREES.date_fact BETWEEN :D1 AND :D2
            INTO :TRANS_ACHATS;
            if (TRANS_ACHATS IS NULL) then TRANS_ACHATS =0;

            TOTAL_TRANS = :TOTAL_TRANS + :TRANS_ACHATS;

            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.date_d10 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 (entete_entrees.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
            and depenses.id_cpte is null
            INTO :TOTAL_DEP ;
            if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

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

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            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
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :REGLEMENT;
            if (REGLEMENT IS NULL) then REGLEMENT = 0;

            SELECT
                  SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
            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
            and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
            INTO :av_com;
            if (av_com IS NULL) then av_com = 0;

            REGLEMENT = :REGLEMENT + :av_com;

            ECART = :SOLDE_INI + :ACHATS - :REGLEMENT;
            if (:START_DATE is not null) then
               NBRE_JOURS = DATEDIFF (DAY from :START_DATE to :D2);
            else
                NBRE_JOURS = 0;

            IF (((:SOLDE_INI + :ACHATS) <> 0)and(:ECART > 0)) THEN
               ROTATION = (:ECART/(:SOLDE_INI + :ACHATS))*:NBRE_JOURS;
            ELSE
                ROTATION = 0;
  SUSPEND;
  END
  END^

SET TERM ; ^


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

SET TERM ^ ;

ALTER PROCEDURE ETAT_STOCK (
  D             DATE
) RETURNS (
  ID_ARTICLE    INTEGER,
  ID_MAGAZIN    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),
  SUM_INITIALE  NUMERIC(18,6),
  SUM_ENTREE    NUMERIC(18,6),
  SUM_SORTIE    NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK         NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,2),
  SUM_VAL_INI   NUMERIC(18,2),
  SUM_VAL_ENT   NUMERIC(18,2),
  SUM_VAL_SRT   NUMERIC(18,2),
  VALEUR_STOCK  NUMERIC(18,2),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILET        SMALLINT,
  TAUX_TVA NUMERIC(18, 2)
  )
AS
declare variable TAUX_M1 NUMERIC(18,2);
  DECLARE variable MARGE1 NUMERIC(18,2);
  DECLARE variable PRIX_V1 NUMERIC(18,2);
  DECLARE variable ID_UNITE INTEGER;
  DECLARE variable ID_FAMILLE INTEGER;
  BEGIN
  		for
  		   WITH LatestPaw AS (
                SELECT   distinct
                       b.id_article,
                       b.id_magazin,
                       b.p_a_m_p,
                       b.date_oper,
                        b.ID_MVT,
                       ROW_NUMBER() OVER (PARTITION BY b.id_article, b.id_magazin ORDER BY b.date_oper DESC, b.ID_MVT desc) AS row_num
                FROM  BASE_CALCUL_PAMP_ALL(:d) b
                  )
                select  DISTINCT
                a.ID_MAGAZIN,
                a.ID_ARTICLE,
    			lp.p_a_m_p,
    			sum(a.QTE_INI),
    			sum(a.QTE_ENT),
    			sum(a.QTE_SRT),
    			sum(cast(a.QTE_INI as double PRECISION) * cast(a.COUT as double PRECISION)),
    			sum(cast(a.QTE_ENT as double PRECISION) * cast(a.COUT as DOUBLE PRECISION)),
    			sum(cast(a.QTE_SRT as DOUBLE PRECISION) * cast(a.COUT as DOUBLE PRECISION)),
    			REF, DESIGNATION, CONDITIONNEMENT, PALETTES, STOCK_MIN, ID_UNITE, ID_FAMILLE, TAUX_TVA, TAUX_M1, MARGE1, PRIX_V1
			from VIEW_MOUVEMENTS a
            inner join articles	on articles.ID_ART = a.ID_ARTICLE and articles.GESTION_STOCK = 1 and articles.matiere <> 1
            JOIN LatestPaw lp ON a.id_article = lp.id_article
             AND a.id_magazin = lp.id_magazin AND lp.row_num = 1
             WHERE    a.date_oper <= :D
             GROUP BY a.ID_ARTICLE, a.ID_MAGAZIN, lp.p_a_m_p,
                      REF, DESIGNATION, CONDITIONNEMENT, PALETTES, STOCK_MIN, ID_UNITE, ID_FAMILLE, TAUX_TVA, TAUX_M1, MARGE1, PRIX_V1
             into :ID_MAGAZIN, :ID_ARTICLE, :COUT, :SUM_INITIALE, :SUM_ENTREE, :SUM_SORTIE, :SUM_VAL_INI, :SUM_VAL_ENT, :SUM_VAL_SRT,
                  :REF, :DESIGNATION, :NBRE_CARTON, :NBRE_PALETTE, :STOCK_MIN, :ID_UNITE, :ID_FAMILLE, :TAUX_TVA, :TAUX_M1, :MARGE1, :PRIX_V1
         do
			begin
                 SELECT
                       UNITES.UNITE
                 FROM UNITES
                 WHERE UNITES.ID_UNITE = :ID_UNITE
                 INTO :UNITE;

                 SELECT
                       FAMILLES.FAMILLE
                 FROM FAMILLES
                 WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
                 INTO :FAMILLE;

                 IF (:NBRE_CARTON is null) THEN NBRE_CARTON = 0;
                 IF (:NBRE_PALETTE is null) THEN NBRE_PALETTE = 0;
                 IF (:TAUX_TVA is null) THEN TAUX_TVA = 0;
                 IF (:TAUX_M1 is null) THEN TAUX_M1 = 0;
                 IF (:MARGE1 is null) THEN MARGE1 = 0;
                 IF (:PRIX_V1 is null) THEN PRIX_V1 = 0;

		         STOCK = :SUM_INITIALE + :SUM_ENTREE - :SUM_SORTIE;
                 IF (STOCK >= 0) then
                    VALEUR_STOCK = :STOCK * :COUT;
                 else
                     VALEUR_STOCK = 0;
   				 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 (:TAUX_M1 <> 0) then
                    PRIX_HT = :COUT + (:COUT * TAUX_M1 / 100);
                 else
                     if (:MARGE1 <> 0) then
                        PRIX_HT = :COUT + :MARGE1;
                     else
                         if (:PRIX_V1 <> 0) then
                            PRIX_HT = :PRIX_V1;
                         else
                             PRIX_HT = 0;
                 PRIX_TTC = :PRIX_HT + (:PRIX_HT * TAUX_TVA / 100);
                 if (:STOCK_MIN > :STOCK) then :SMILET = 0; else :SMILET =1;
                 VALEUR_HT = :STOCK * :PRIX_HT;
                 VALEUR_TTC = :STOCK * :PRIX_TTC;
  				suspend;
			end
END^

SET TERM ; ^


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

SET TERM ^ ;

ALTER PROCEDURE ETAT_STOCK_DEPOT (
  ID_MAGAZIN    INTEGER,
  D             DATE
) RETURNS (
  ID_ARTICLE    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),
  SUM_INITIALE  NUMERIC(18,6),
  SUM_ENTREE    NUMERIC(18,6),
  SUM_SORTIE    NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK         NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,2),
  SUM_VAL_INI   NUMERIC(18,2),
  SUM_VAL_ENT   NUMERIC(18,2),
  SUM_VAL_SRT   NUMERIC(18,2),
  VALEUR_STOCK  NUMERIC(18,2),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILET        SMALLINT,
  TAUX_TVA NUMERIC(18, 2)
)
AS
declare variable TAUX_M1 NUMERIC(18,2);
  DECLARE variable MARGE1 NUMERIC(18,2);
  DECLARE variable PRIX_V1 NUMERIC(18,2);
  DECLARE variable ID_UNITE INTEGER;
  DECLARE variable ID_FAMILLE INTEGER;
  BEGIN
        for
        	WITH LatestPaw AS (
                SELECT  distinct
                       b.id_article,
                       b.id_magazin,
                       b.p_a_m_p,
                       b.date_oper,
                        b.ID_MVT,
                       ROW_NUMBER() OVER (PARTITION BY b.id_article, b.id_magazin ORDER BY b.date_oper DESC, b.ID_MVT desc) AS row_num
                FROM  BASE_CALCUL_PAMP_ALL(:d) b
                where  b.id_magazin = :id_magazin
                  )
                select   DISTINCT
                a.ID_MAGAZIN,
                a.ID_ARTICLE,
    			lp.p_a_m_p,
    			sum(a.QTE_INI),
    			sum(a.QTE_ENT),
    			sum(a.QTE_SRT),
    			sum(cast(a.QTE_INI as double PRECISION) * cast(a.COUT as double PRECISION)),
    			sum(cast(a.QTE_ENT as double PRECISION) * cast(a.COUT as DOUBLE PRECISION)),
    			sum(cast(a.QTE_SRT as DOUBLE PRECISION) * cast(a.COUT as DOUBLE PRECISION)),
    			REF, DESIGNATION, CONDITIONNEMENT, PALETTES, STOCK_MIN, ID_UNITE, ID_FAMILLE, TAUX_TVA, TAUX_M1, MARGE1, PRIX_V1
			from VIEW_MOUVEMENTS a
            inner join articles	on articles.ID_ART = a.ID_ARTICLE and articles.GESTION_STOCK = 1 and articles.matiere <> 1
            JOIN LatestPaw lp ON a.id_article = lp.id_article
             AND a.id_magazin = lp.id_magazin AND lp.row_num = 1
             WHERE    a.date_oper <= :D and a.id_magazin =:id_magazin
             GROUP BY a.ID_ARTICLE, a.ID_MAGAZIN, lp.p_a_m_p,
                      REF, DESIGNATION, CONDITIONNEMENT, PALETTES, STOCK_MIN, ID_UNITE, ID_FAMILLE, TAUX_TVA, TAUX_M1, MARGE1, PRIX_V1
             into :ID_MAGAZIN, :ID_ARTICLE, :COUT, :SUM_INITIALE, :SUM_ENTREE, :SUM_SORTIE, :SUM_VAL_INI, :SUM_VAL_ENT, :SUM_VAL_SRT,
                  :REF, :DESIGNATION, :NBRE_CARTON, :NBRE_PALETTE, :STOCK_MIN, :ID_UNITE, :ID_FAMILLE, :TAUX_TVA, :TAUX_M1, :MARGE1, :PRIX_V1
         do
			begin
                 SELECT
                       UNITES.UNITE
                 FROM UNITES
                 WHERE UNITES.ID_UNITE = :ID_UNITE
                 INTO :UNITE;

                 SELECT
                       FAMILLES.FAMILLE
                 FROM FAMILLES
                 WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
                 INTO :FAMILLE;

                 IF (:NBRE_CARTON is null) THEN NBRE_CARTON = 0;
                 IF (:NBRE_PALETTE is null) THEN NBRE_PALETTE = 0;
                 IF (:TAUX_TVA is null) THEN TAUX_TVA = 0;
                 IF (:TAUX_M1 is null) THEN TAUX_M1 = 0;
                 IF (:MARGE1 is null) THEN MARGE1 = 0;
                 IF (:PRIX_V1 is null) THEN PRIX_V1 = 0;

		         STOCK = :SUM_INITIALE + :SUM_ENTREE - :SUM_SORTIE;
                 IF (STOCK >= 0) then
                    VALEUR_STOCK = :STOCK * :COUT;
                 else
                     VALEUR_STOCK = 0;
   				 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 (:TAUX_M1 <> 0) then
                    PRIX_HT = :COUT + (:COUT * TAUX_M1 / 100);
                 else
                     if (:MARGE1 <> 0) then
                        PRIX_HT = :COUT + :MARGE1;
                     else
                         if (:PRIX_V1 <> 0) then
                            PRIX_HT = :PRIX_V1;
                         else
                             PRIX_HT = 0;
                 PRIX_TTC = :PRIX_HT + (:PRIX_HT * TAUX_TVA / 100);
                 if (:STOCK_MIN > :STOCK) then :SMILET = 0; else :SMILET =1;
                 VALEUR_HT = :STOCK * :PRIX_HT;
                 VALEUR_TTC = :STOCK * :PRIX_TTC;
  				suspend;
			end
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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
     and sorties.DESTOCKER = 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.DESTOCKER = 1
              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
     and sorties.DESTOCKER = 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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

SET TERM ^ ;

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

ALTER PROCEDURE FACT_TAUX_TVA_DEPOT(
  ID_MAGAZIN INTEGER,
  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
     AND entete_sorties.ID_MAGAZIN = :ID_MAGAZIN
     INTO :ID_ENTETE, :NUM, :DATE_PIECE, :CLIENT
  DO
  BEGIN
       SELECT
             sum(sorties.ht),
             sum(sorties.tva)
       FROM sorties
       WHERE sorties.id_entete = :ID_ENTETE
       AND sorties.taux_tva = 0
       AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
       INTO :CA_00,:TVA_00;

       SELECT
             sum(sorties.ht),
             sum(sorties.tva)
       FROM sorties
       WHERE sorties.id_entete = :ID_ENTETE
       AND sorties.taux_tva = 9
       AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
       INTO :CA_07,:TVA_07;

       SELECT
             sum(sorties.ht),
             sum(sorties.tva)
       FROM sorties
       WHERE sorties.id_entete = :ID_ENTETE
       AND sorties.taux_tva = 19
       AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
       INTO :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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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^

SET TERM ; ^

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

SET TERM ^ ;

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
DECLARE VARIABLE EX_STOCK NUMERIC(18, 6);
DECLARE VARIABLE current_paw NUMERIC(18, 2);
DECLARE VARIABLE QUANTITE NUMERIC(18, 6);
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 No : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE)from 1 for 128),
              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 No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 entrees No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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('Dechets fiche de production No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              cast(0 AS numeric(18,6)),
              cast(sorties.DECHETS 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.DECHETS <> 0
        and SORTIES.DECHETS is not null
        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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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;
             EX_STOCK = STOCK;
             QUANTITE = ENTREE - SORTIE;
             IF (:QUANTITE >= 0) then
             	BEGIN
             	     VALEUR_STOCK = cast(VALEUR_STOCK + (QUANTITE * COUT) as NUMERIC(18,2));
                     STOCK = STOCK + QUANTITE;
                     IF (STOCK > 0) THEN
                        BEGIN
                             if (EX_STOCK <= 0) then
                                current_paw = COUT;
                             ELSE
                                 current_paw = VALEUR_STOCK / STOCK;
                             END
                        ELSE
                            BEGIN
                                 current_paw = COUT;
                            END
                END
             ELSE
                 BEGIN
                      VALEUR_STOCK = cast(VALEUR_STOCK + (QUANTITE * current_paw) as NUMERIC(18,2));
                      STOCK = STOCK + QUANTITE;
                      IF (STOCK < 0) THEN
                        BEGIN
                          VALEUR_STOCK = 0;
                        END
                 END
             P_A_M_P = cast(current_paw as NUMERIC(18,2));
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             VALEUR_SORTIE = cast(:SORTIE * :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^

SET TERM ; ^

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

SET TERM ^ ;

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
DECLARE VARIABLE EX_STOCK NUMERIC(18, 6);
DECLARE VARIABLE QUANTITE NUMERIC(18, 6);
DECLARE VARIABLE current_paw NUMERIC(18, 2);

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.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 No : '||entete_entrees.num_dos AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No :'||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 entrees No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_entrees.num_fact AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_ENTREES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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('Dechets fiche de production No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              cast(0 AS numeric(18,6)),
              cast(sorties.DECHETS 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.DECHETS <> 0
        and SORTIES.DECHETS is not null
        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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(ENTETE_SORTIES.DENOMINATION AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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 No : '||lignes_transfert.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              SUBSTRING(CAST(magazins.magazin AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
              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,11,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;
             EX_STOCK = STOCK;
             QUANTITE = ENTREE - SORTIE;
             IF (:QUANTITE >= 0) then
             	BEGIN
             	     VALEUR_STOCK = cast(VALEUR_STOCK + (QUANTITE * COUT) as NUMERIC(18,2));
                     STOCK = STOCK + QUANTITE;
                     IF (STOCK > 0) THEN
                        BEGIN
                             if (EX_STOCK <= 0) then
                                current_paw = COUT;
                             ELSE
                                 current_paw = VALEUR_STOCK / STOCK;
                             END
                        ELSE
                            BEGIN
                                 current_paw = COUT;
                            END
                END
             ELSE
                 BEGIN
                      VALEUR_STOCK = cast(VALEUR_STOCK + (QUANTITE * current_paw) as NUMERIC(18,2));
                      STOCK = STOCK + QUANTITE;
                      IF (STOCK < 0) THEN
                        BEGIN
                            VALEUR_STOCK = 0;
                        END
                 END
             P_A_M_P = cast(current_paw as NUMERIC(18,2));
             VALEUR_ENTREE = cast(:ENTREE * :COUT AS numeric(18,2));
             VALEUR_SORTIE = cast(:SORTIE * :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^

SET TERM ; ^

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

SET TERM ^ ;

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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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('Bon Sortie No : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.REPRESENTANT 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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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));
             if (stock >= 0) then
                VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             else
                VALEUR_STOCK = 0;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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));
             if (stock >= 0)then
                VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             else
                VALEUR_STOCK = 0;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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_transfert.NUM_PIECE AS VARCHAR(12)),
              cast(lignes_transfert.ID_ARTICLE AS integer),
              CAST(lignes_transfert.DATE_PIECE AS DATE),
              CAST('Entree Transfert No : '||entete_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_2
        WHERE lignes_transfert.id_article = :ID_ART
        AND lignes_transfert.ID_MAGAZIN_2 = :ID_MAGAZIN
        AND lignes_transfert.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('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.QUANTITE <> 0
        and sorties.QUANTITE is not null
        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

        UNION ALL

        SELECT
              cast(entete_transfert .num_piece AS VARCHAR(12)),
              cast(lignes_transfert.id_article AS integer),
              CAST(lignes_transfert.date_piece AS DATE),
              CAST('Sortie transfert No: '||entete_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 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_article = :ID_ART
        AND lignes_transfert.id_magazin_1 = :ID_MAGAZIN
        AND lignes_transfert.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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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));
             if (stock >= 0) then
                VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             else
                VALEUR_STOCK = 0;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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));
             if (stock >= 0) then
                VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             else
                VALEUR_STOCK = 0;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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.QUANTITE <> 0
        and SORTIES.QUANTITE is not null
        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('Bon sortie N° : '||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.REPRESENTANT 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 + :STOCK)>0) then P_A_M_P = cast(((:VALEUR_STOCK + :VALEUR_ENTREE)/(:ENTREE + :STOCK))AS numeric(18,2));
             else  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));
             if (stock >= 0) then
                VALEUR_STOCK = cast(:STOCK * P_A_M_P AS numeric(18,2));
             else
                VALEUR_STOCK = 0;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE INV_DUAU_DPT_REF_ENTREE(
  ID_MAGAZIN INTEGER,
  DU DATE,
  AU DATE)
RETURNS(
  ID_ART INTEGER,
  REF VARCHAR(20),
  DATE_ENTREE DATE,
  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),
  VAL_VTE 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;

       VAL_VTE = 0;
       SELECT
              coalesce(SUM(SORTIES.HT),0)
        FROM SORTIES
        WHERE SORTIES.ID_ARTICLE = :ID_ART
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.DESTOCKER = 1
        AND SORTIES.REF_ENTREE = :REF_ENTREE
        AND SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :VAL_VTE;

        SELECT PARAMETRES.EXER_DU
        FROM PARAMETRES
        INTO :DATE_ENTREE;

        SELECT FIRST 1 ENTREES.DATE_ENTREE
        FROM ENTREES WHERE ENTREES.ID_ARTICLE = :ID_ART
        AND ENTREES.REF_ENTREE = :REF_ENTREE
        AND ENTREES.ID_MAGAZIN = :ID_MAGAZIN
        INTO :DATE_ENTREE;

        IF (DATE_ENTREE is null) THEN
        	begin
            	SELECT FIRST 1 INVENTAIRE.DATE_INVENTAIRE
        		FROM INVENTAIRE WHERE INVENTAIRE.ID_ARTICLE = :ID_ART
                AND INVENTAIRE.REF_ENTREE = :REF_ENTREE
                AND INVENTAIRE.ID_MAGAZIN = :ID_MAGAZIN
        		INTO :DATE_ENTREE;
            end

       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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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


SET TERM ^ ;

CREATE OR 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);
  DECLARE variable prix_sur_achat integer;
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;

       select articles.PEREMPTION from articles where articles.ID_ART = :id_art
       into prix_sur_achat;

       if (prix_sur_achat = 1) then
        begin
        	  PRIX_HT = 0;
            if (exists(select 1 from entrees
            	where entrees.ID_ARTICLE = :ID_ART
            	and entrees.REF_ENTREE = :REF_ENTREE
            	and entrees.ID_MAGAZIN = :ID_MAGAZIN
            	)
                      ) theN
                      		BEGIN
            				select first 1 entrees.PRIX_VENTE from entrees
            				where entrees.ID_ARTICLE = :ID_ART
            				and entrees.REF_ENTREE = :REF_ENTREE
            				and entrees.ID_MAGAZIN = :ID_MAGAZIN
            				into PRIX_HT;
                            END
            ELSE
            	BEGIN
                	select first 1 INVENTAIRE.PRIX_VENTE from INVENTAIRE
            				where INVENTAIRE.ID_ARTICLE = :ID_ART
            				and INVENTAIRE.REF_ENTREE = :REF_ENTREE
            				and INVENTAIRE.ID_MAGAZIN = :ID_MAGAZIN
            				into PRIX_HT;
                END
        end

       PRIX_TTC = :PRIX_HT + (CAST(:PRIX_HT as DOUBLE PRECISION) *cast(V as DOUBLE PRECISION)) /100;
       VALEUR_HT = cast(:STOCK as DOUBLE PRECISION) * cast(PRIX_HT as DOUBLE PRECISION);
       VALEUR_TTC = cast(:STOCK as DOUBLE PRECISION) * cast(PRIX_TTC as DOUBLE PRECISION);
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE INVENTAIRE_DU_AU (
  DU            DATE,
  AU            DATE
) RETURNS (
  ID_ART        INTEGER,
  ID_MAGAZIN    INTEGER,
  REF           VARCHAR(20),
  DESIGNATION   VARCHAR(128),
  UNITE         VARCHAR(12),
  FAMILLE       VARCHAR(64),
  PAMP_INI      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),
  NBRE_CARTON   NUMERIC(18,6),
  NBRE_PALETTE  NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,6),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILTY        SMALLINT,
  VAL_VTE       NUMERIC(18,2)
)
AS
declare variable TAUX_M1 NUMERIC(18,2);
  DECLARE variable MARGE1 NUMERIC(18,2);
  DECLARE variable PRIX_V1 NUMERIC(18,2);
  DECLARE variable TAUX_TVA NUMERIC(18,2);
  DECLARE variable ID_UNITE INTEGER;
  DECLARE variable ID_FAMILLE INTEGER;
BEGIN
     FOR
        SELECT DISTINCT
               MVT_DU_AU.ID_ARTICLE,
               MVT_DU_AU.ID_MAGAZIN,
               PAMP_DU.CUMP,
               SUM(QTE_INI),
               SUM(QTE_ENT),
               SUM(QTE_ENT * COUT),
               SUM(QTE_SRT),
               SUM(QTE_SRT * COUT),
               PAMP_AU.P_A_M_P
        FROM MVT_DU_AU(:DU,:AU)
        inner join articles	on articles.ID_ART = MVT_DU_AU.ID_ARTICLE and articles.GESTION_STOCK = 1 and articles.matiere <> 1
        INNER join ( WITH TABLE_CUMP AS (SELECT
                                        BASE_CALCUL_PAMP_ALL.ID_ARTICLE,
                                        BASE_CALCUL_PAMP_ALL.ID_MAGAZIN,
                                        BASE_CALCUL_PAMP_ALL.P_A_M_P,
                                        RANK() OVER ( PARTITION BY ID_ARTICLE, ID_MAGAZIN ORDER BY ID_MVT DESC ) AS r
                                  FROM BASE_CALCUL_PAMP_ALL(:DU-1)
                                  UNION
                                  SELECT
                                        MVT_DU_AU.ID_ARTICLE,
                                        MVT_DU_AU.ID_MAGAZIN,
                                        0,
                                        1
                                  FROM MVT_DU_AU(:DU,:AU)
                                 )
                       SELECT DISTINCT
                              ID_ARTICLE,
                              ID_MAGAZIN,
                              max(P_A_M_P) as CUMP
                       FROM TABLE_CUMP
                       WHERE r = 1
                       group by TABLE_CUMP.ID_ARTICLE, TABLE_CUMP.ID_MAGAZIN
                    ) PAMP_DU  ON PAMP_DU.ID_ARTICLE = MVT_DU_AU.ID_ARTICLE
                               and PAMP_DU.ID_MAGAZIN = MVT_DU_AU.ID_MAGAZIN

        INNER join ( WITH TABLE_CUMP AS (SELECT
                                        BASE_CALCUL_PAMP_ALL.ID_ARTICLE,
                                        BASE_CALCUL_PAMP_ALL.ID_MAGAZIN,
                                        BASE_CALCUL_PAMP_ALL.P_A_M_P,
                                        RANK() OVER ( PARTITION BY ID_ARTICLE, ID_MAGAZIN ORDER BY ID_MVT DESC ) AS r
                                  FROM BASE_CALCUL_PAMP_ALL(:AU)
                                )
                          SELECT ID_ARTICLE, ID_MAGAZIN, P_A_M_P
                          FROM TABLE_CUMP
                          WHERE r = 1

   			          ) PAMP_AU  ON PAMP_AU.ID_ARTICLE = MVT_DU_AU.ID_ARTICLE
                                 and PAMP_AU.ID_MAGAZIN = MVT_DU_AU.ID_MAGAZIN
        GROUP BY MVT_DU_AU.ID_ARTICLE, MVT_DU_AU.ID_MAGAZIN, PAMP_DU.CUMP, PAMP_AU.P_A_M_P
        INTO :ID_ART, :ID_MAGAZIN, :PAMP_INI, :ST_INI, :ST_ENT, :VAL_ENT, :ST_SRT, :VAL_SRT, :COUT
  DO
  BEGIN
        select
              REF,
		      DESIGNATION,
		      CONDITIONNEMENT,
		      PALETTES,
		      STOCK_MIN,
		      ID_UNITE,
		      ID_FAMILLE,
		      TAUX_TVA,
		      TAUX_M1,
		      MARGE1,
		      PRIX_V1
        from articles where articles.ID_ART = :ID_ART
        into :REF, :DESIGNATION, :NBRE_CARTON, :NBRE_PALETTE, :STOCK_MIN, :ID_UNITE,
             :ID_FAMILLE, :TAUX_TVA, :TAUX_M1, :MARGE1, :PRIX_V1;
        SELECT
              UNITES.UNITE
        FROM UNITES
        WHERE UNITES.ID_UNITE = :ID_UNITE
        INTO :UNITE;
        SELECT
              FAMILLES.FAMILLE
        FROM FAMILLES
        WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
        INTO :FAMILLE;
        VAL_VTE = 0;
        SELECT
              coalesce(SUM(SORTIES.HT),0)
        FROM SORTIES
        WHERE SORTIES.ID_ARTICLE = :ID_ART
        AND SORTIES.DESTOCKER = 1
        AND SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :VAL_VTE;

        IF (:NBRE_CARTON is null) THEN NBRE_CARTON = 0;
        IF (:NBRE_PALETTE is null) THEN NBRE_PALETTE = 0;
        IF (:PRIX_V1 is null) THEN PRIX_V1 = 0;
        IF (:TAUX_TVA is null) THEN TAUX_TVA = 0;
        IF (:TAUX_M1 is null) THEN TAUX_M1 = 0;
        IF (:MARGE1 is null) THEN MARGE1 = 0;
        IF (:STOCK_MIN is null) THEN STOCK_MIN = 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;
        STOCK = :ST_INI + :ST_ENT - :ST_SRT;
        VAL_INI = :ST_INI * :PAMP_INI;
	    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 = :STOCK * :COUT;
       if (:TAUX_M1 <> 0) then
                    PRIX_HT = :COUT + (:COUT * TAUX_M1 / 100);
                 else
                     if (:MARGE1 <> 0) then
                        PRIX_HT = :COUT + :MARGE1;
                     else
                         if (:PRIX_V1 <> 0) then
                            PRIX_HT = :PRIX_V1;
                         else
                             PRIX_HT = 0;
                 PRIX_TTC = :PRIX_HT + (:PRIX_HT * TAUX_TVA / 100);
         IF (:STOCK_MIN > :STOCK) then SMILTY = 0; ELSE SMILTY = 1;
  SUSPEND;
  END
END^

SET TERM ; ^


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

SET TERM ^ ;

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),
  PAMP_INI      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),
  NBRE_CARTON   NUMERIC(18,6),
  NBRE_PALETTE  NUMERIC(18,6),
  STOCK_CARTON  NUMERIC(18,6),
  STOCK_PALETTE NUMERIC(18,6),
  STOCK_MIN     NUMERIC(18,6),
  PRIX_HT       NUMERIC(18,2),
  PRIX_TTC      NUMERIC(18,2),
  VALEUR_HT     NUMERIC(18,2),
  VALEUR_TTC    NUMERIC(18,2),
  SMILTY        SMALLINT,
  VAL_VTE       NUMERIC(18,2)
)
AS
DECLARE VARIABLE ID_UNITE INTEGER;
DECLARE VARIABLE ID_FAMILLE INTEGER;
DECLARE VARIABLE TAUX_TVA NUMERIC(18,2);
DECLARE VARIABLE TAUX_M1 NUMERIC(18,2);
DECLARE VARIABLE MARGE1 NUMERIC(18,2);
DECLARE VARIABLE PRIX_V1 NUMERIC(18,2);
BEGIN
     FOR
        SELECT DISTINCT
               MVT_DU_AU.ID_ARTICLE,
               PAMP_DU.CUMP,
               SUM(QTE_INI),
               SUM(QTE_ENT),
               SUM(QTE_ENT * COUT),
               SUM(QTE_SRT),
               SUM(QTE_SRT * COUT),
               PAMP_AU.P_A_M_P
        FROM MVT_DU_AU(:DU,:AU)
        inner join articles	on articles.ID_ART = MVT_DU_AU.ID_ARTICLE and articles.GESTION_STOCK = 1 and articles.matiere <> 1
        INNER join ( WITH TABLE_CUMP AS (SELECT
                                        BASE_CALCUL_PAMP_ALL.ID_ARTICLE,
                                        BASE_CALCUL_PAMP_ALL.P_A_M_P,
                                        RANK() OVER ( PARTITION BY ID_ARTICLE, ID_MAGAZIN ORDER BY ID_MVT DESC ) AS r
                                  FROM BASE_CALCUL_PAMP_ALL(:DU-1)
                                  WHERE  BASE_CALCUL_PAMP_ALL.ID_MAGAZIN = :ID_MAGAZIN
                                  UNION
                                  SELECT
                                        MVT_DU_AU.ID_ARTICLE,
                                        0,
                                        1
                                  FROM MVT_DU_AU(:DU,:AU)
                                  WHERE  MVT_DU_AU.ID_MAGAZIN = :ID_MAGAZIN
                                 )
                       SELECT DISTINCT
                              ID_ARTICLE,
                              max(P_A_M_P) as CUMP
                       FROM TABLE_CUMP
                       WHERE r = 1
                       group by TABLE_CUMP.ID_ARTICLE
                    ) PAMP_DU  ON PAMP_DU.ID_ARTICLE = MVT_DU_AU.ID_ARTICLE

        INNER join ( WITH TABLE_CUMP AS (SELECT
                                        BASE_CALCUL_PAMP_ALL.ID_ARTICLE,
                                        BASE_CALCUL_PAMP_ALL.P_A_M_P,
                                        RANK() OVER ( PARTITION BY ID_ARTICLE, ID_MAGAZIN ORDER BY ID_MVT DESC ) AS r
                                  FROM BASE_CALCUL_PAMP_ALL(:AU)
                                  WHERE BASE_CALCUL_PAMP_ALL.ID_MAGAZIN = :ID_MAGAZIN
                                )
                          SELECT ID_ARTICLE, P_A_M_P
                          FROM TABLE_CUMP
                          WHERE r = 1

   			          ) PAMP_AU  ON PAMP_AU.ID_ARTICLE = MVT_DU_AU.ID_ARTICLE
        WHERE  MVT_DU_AU.ID_MAGAZIN = :ID_MAGAZIN
        GROUP BY MVT_DU_AU.ID_ARTICLE, MVT_DU_AU.ID_MAGAZIN, PAMP_DU.CUMP, PAMP_AU.P_A_M_P
        INTO :ID_ART, :PAMP_INI, :ST_INI, :ST_ENT, :VAL_ENT, :ST_SRT, :VAL_SRT, :COUT
  DO
  BEGIN
        select
              REF,
		      DESIGNATION,
		      CONDITIONNEMENT,
		      PALETTES,
		      STOCK_MIN,
		      ID_UNITE,
		      ID_FAMILLE,
		      TAUX_TVA,
		      TAUX_M1,
		      MARGE1,
		      PRIX_V1
        from articles where articles.ID_ART = :ID_ART
        into :REF, :DESIGNATION, :NBRE_CARTON, :NBRE_PALETTE, :STOCK_MIN, :ID_UNITE,
             :ID_FAMILLE, :TAUX_TVA, :TAUX_M1, :MARGE1, :PRIX_V1;
        SELECT
              UNITES.UNITE
        FROM UNITES
        WHERE UNITES.ID_UNITE = :ID_UNITE
        INTO :UNITE;
        SELECT
              FAMILLES.FAMILLE
        FROM FAMILLES
        WHERE FAMILLES.ID_FAMILLE = :ID_FAMILLE
        INTO :FAMILLE;
        VAL_VTE = 0;
        SELECT
              coalesce(SUM(SORTIES.HT),0)
        FROM SORTIES
        WHERE SORTIES.ID_ARTICLE = :ID_ART
        AND SORTIES.ID_MAGAZIN = :ID_MAGAZIN
        AND SORTIES.DESTOCKER = 1
        AND SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :VAL_VTE;

        IF (:NBRE_CARTON is null) THEN NBRE_CARTON = 0;
        IF (:NBRE_PALETTE is null) THEN NBRE_PALETTE = 0;
        IF (:PRIX_V1 is null) THEN PRIX_V1 = 0;
        IF (:TAUX_TVA is null) THEN TAUX_TVA = 0;
        IF (:TAUX_M1 is null) THEN TAUX_M1 = 0;
        IF (:MARGE1 is null) THEN MARGE1 = 0;
        IF (:STOCK_MIN is null) THEN STOCK_MIN = 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;
        STOCK = :ST_INI + :ST_ENT - :ST_SRT;
        VAL_INI = :ST_INI * :PAMP_INI;
	    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 = :STOCK * :COUT;
       if (:TAUX_M1 <> 0) then
                    PRIX_HT = :COUT + (:COUT * TAUX_M1 / 100);
                 else
                     if (:MARGE1 <> 0) then
                        PRIX_HT = :COUT + :MARGE1;
                     else
                         if (:PRIX_V1 <> 0) then
                            PRIX_HT = :PRIX_V1;
                         else
                             PRIX_HT = 0;
                 PRIX_TTC = :PRIX_HT + (:PRIX_HT * TAUX_TVA / 100);
         IF (:STOCK_MIN > :STOCK) then SMILTY = 0; ELSE SMILTY = 1;
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE INVENTAIRE_DU_AU_REF_ENTREE(
  DU DATE,
  AU DATE)
RETURNS(
  ID_ART INTEGER,
  REF VARCHAR(20),
  REF_ENTREE VARCHAR(12),
  DATE_ENTREE DATE,
  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),
  VAL_VTE 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;

       VAL_VTE = 0;
       SELECT
              coalesce(SUM(SORTIES.HT),0)
        FROM SORTIES
        WHERE SORTIES.ID_ARTICLE = :ID_ART
        AND SORTIES.DESTOCKER = 1
        AND SORTIES.DATE_PIECE BETWEEN :DU AND :AU
        INTO :VAL_VTE;

        SELECT PARAMETRES.EXER_DU
        FROM PARAMETRES
        INTO :DATE_ENTREE;

        SELECT FIRST 1 ENTREES.DATE_ENTREE
        FROM ENTREES WHERE ENTREES.ID_ARTICLE = :ID_ART AND ENTREES.REF_ENTREE = :REF_ENTREE
        INTO :DATE_ENTREE;

        IF (DATE_ENTREE is null) THEN
        	begin
            	SELECT FIRST 1 INVENTAIRE.DATE_INVENTAIRE
        		FROM INVENTAIRE WHERE INVENTAIRE.ID_ARTICLE = :ID_ART AND INVENTAIRE.REF_ENTREE = :REF_ENTREE
        		INTO :DATE_ENTREE;
            end

       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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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 ARTICLES.GESTION_STOCK = 1
       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 ARTICLES.GESTION_STOCK = 1
       and entrees.STOCKER = 1
       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 ARTICLES.GESTION_STOCK = 1
       and sorties.DESTOCKER = 1
       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 ARTICLES.GESTION_STOCK = 1
       and sorties.DESTOCKER = 1
       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^

SET TERM ; ^

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

SET TERM ^ ;
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);
  DECLARE variable prix_sur_achat integer;
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;

       select articles.PEREMPTION from articles where articles.ID_ART = :id_art
       into prix_sur_achat;

       if (prix_sur_achat = 1) then
        begin
            select first 1 entrees.PRIX_VENTE from entrees
            where entrees.ID_ARTICLE = :ID_ART
            and entrees.REF_ENTREE = :REF_ENTREE
            into PRIX_HT;
        end

       PRIX_TTC = :PRIX_HT + (CAST(:PRIX_HT as DOUBLE PRECISION) *cast(V as DOUBLE PRECISION)) /100;
       VALEUR_HT = cast(:STOCK as DOUBLE PRECISION) * cast(PRIX_HT as DOUBLE PRECISION);
       VALEUR_TTC = cast(:STOCK as DOUBLE PRECISION) * cast(PRIX_TTC as DOUBLE PRECISION);
  SUSPEND;
  END
END^
SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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 AS DOUBLE PRECISION) * CAST(COUT AS DOUBLE PRECISION),

           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 AS DOUBLE PRECISION) * CAST(ENTREES.cout_achat AS DOUBLE PRECISION),

           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 AS DOUBLE PRECISION) * CAST(lignes_transfert.PAMP  AS DOUBLE PRECISION),

           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 AS DOUBLE PRECISION) * CAST(sorties.cr_u AS DOUBLE PRECISION),

           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 AS DOUBLE PRECISION) * CAST(lignes_transfert.PAMP AS DOUBLE PRECISION),

           '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 AS DOUBLE PRECISION) * CAST(sorties.cr_u AS DOUBLE PRECISION),

           '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 AS DOUBLE PRECISION) * CAST(ENTREES.cout_achat AS DOUBLE PRECISION),

           '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^

SET TERM ; ^

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

SET TERM ^ ;

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
     inner join ENTETE_ENTREES on ENTETE_ENTREES.ID_ENT_ENT = ENTREES.ID_ENTETE
     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
     inner join ENTETE_TRANSFERT on ENTETE_TRANSFERT.ID_ENTETE = LIGNES_TRANSFERT.ID_ENTETE
     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
     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))
     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
     inner join ENTETE_TRANSFERT on ENTETE_TRANSFERT.ID_ENTETE = LIGNES_TRANSFERT.ID_ENTETE
     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
     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)
     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
     inner join ENTETE_ENTREES on ENTETE_ENTREES.ID_ENT_ENT = ENTREES.ID_ENTETE
     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
     inner join ENTETE_ENTREES on ENTETE_ENTREES.ID_ENT_ENT = ENTREES.ID_ENTETE
     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
     inner join ENTETE_TRANSFERT on ENTETE_TRANSFERT.ID_ENTETE = LIGNES_TRANSFERT.ID_ENTETE
     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
     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))
     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
     inner join ENTETE_TRANSFERT on ENTETE_TRANSFERT.ID_ENTETE = LIGNES_TRANSFERT.ID_ENTETE
     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
     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)
     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
     inner join ENTETE_ENTREES on ENTETE_ENTREES.ID_ENT_ENT = ENTREES.ID_ENTETE
     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^

SET TERM ; ^

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

SET TERM ^ ;

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
        and entrees.STOCKER = 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^

SET TERM ; ^

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

SET TERM ^ ;

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
        and sorties.DESTOCKER = 1
        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^

SET TERM ; ^

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

SET TERM ^ ;

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
        AND ENTREES.STOCKER = 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^

SET TERM ; ^

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

SET TERM ^ ;

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)or(ENTREES.nature_ENTREE = 10))
        AND ENTREES.STOCKER = 1
        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^

SET TERM ; ^

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

SET TERM ^ ;

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.STOCKER = 1
        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^

SET TERM ; ^

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

SET TERM ^ ;

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.DESTOCKER = 1
        AND ((sorties.nature_sortie=1)or(sorties.nature_sortie=2)or(sorties.nature_sortie=10))
        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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE NUM_ENTET_ENTREE(
  NATURE INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
           max(cast((select OPARAM from get_digit_only( 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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE NUM_ENTET_SORTIE(
  NATURE INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
           max(cast((select OPARAM from get_digit_only( 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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE NUM_ENTET_SORTIE_MAG(
  NATURE INTEGER,
  ID_MAGASIN INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
           max(cast((select OPARAM from get_digit_only( 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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE NUM_ENTET_SORTIE_MAGAZIN(
  NATURE INTEGER,
  ID_MAGAZIN INTEGER)
RETURNS(
  NUM INTEGER)
AS
BEGIN
     SELECT
           max(cast((select OPARAM from get_digit_only( 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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE PAMP_DEPOT(
  ID_ARTICLE INTEGER,
  ID_MAGAZIN INTEGER,
  D DATE)
RETURNS(
  P_PAMP NUMERIC(18, 2))
AS
DECLARE VARIABLE MATIERE SMALLINT;
BEGIN
	select
    	ARTICLES.MATIERE
    from articles
    WHERE articles.ID_ART = :ID_ARTICLE
    INTO :MATIERE;

    IF (MATIERE = 1) THEN
    	P_PAMP = 0;
    ELSE
     SELECT
    		P_A_M_P
	 FROM BASE_CALCUL_PAMP(:D, :ID_ARTICLE,:ID_MAGAZIN)
	 where ID_MVT = (select max(ID_MVT) from BASE_CALCUL_PAMP(:D, :ID_ARTICLE,:ID_MAGAZIN) )
   INTO :P_PAMP;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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.QTE)
        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^

SET TERM ; ^

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

SET TERM ^ ;

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.QTE)
        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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RECAP_FACTURE_VENTE(
  D1 DATE,
  D2 DATE,
  NATURE INTEGER)
RETURNS(
  NBRE INTEGER,
  ESPECE NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  BASE_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 = :NATURE
     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 = :NATURE
     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 = :NATURE
     AND entete_sorties.timbre = 0
     AND entete_sorties.cession <> 1
     INTO :AUTRES;

     SELECT
           SUM(TOTAL_HT),
           SUM(TOTAL_TTC),
           SUM(TIMBRE)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = :NATURE
     AND entete_sorties.timbre <> 0
     AND entete_sorties.cession <> 1
     INTO :ESPECE, :BASE_TIMBRE, :TIMBRE;

     SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = :NATURE
     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 = :NATURE
     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 = :NATURE
     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 = :NATURE
     AND entete_sorties.cession <> 1
     INTO :VENTE, :CONSOM;

     MARGE = :VENTE - :CONSOM;

  SUSPEND;

END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RECAP_FACTURE_VENTE_DEPOT(
   ID_MAG INTEGER,
  D1 DATE,
  D2 DATE,
  NATURE INTEGER)
RETURNS(
  NBRE INTEGER,
  ESPECE NUMERIC(18, 2),
  TIMBRE NUMERIC(18, 2),
  BASE_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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     AND entete_sorties.timbre = 0
     AND entete_sorties.cession <> 1
     INTO :AUTRES;

     SELECT
           SUM(TOTAL_HT),
           SUM(TOTAL_TTC),
           SUM(TIMBRE)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     AND entete_sorties.timbre <> 0
     AND entete_sorties.cession <> 1
     INTO :ESPECE, :BASE_TIMBRE, :TIMBRE;

     SELECT
           SUM(TOTAL_HT)
     FROM entete_sorties
     WHERE entete_sorties.date_piece BETWEEN :D1 AND :D2
     AND entete_sorties.nature_sortie = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     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 = :NATURE
     AND ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
     AND entete_sorties.cession <> 1
     INTO :VENTE, :CONSOM;

     MARGE = :VENTE - :CONSOM;

  SUSPEND;

END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RECAP_VENTE_TVA_DEPOT(
  ID_MAG INTEGER,
  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 ENTETE_SORTIES.ID_MAGAZIN = :ID_MAG
        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^

SET TERM ; ^

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

SET TERM ^ ;

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_CLIENT
          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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_CLIENT(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CLIENT INTEGER,
  NUM VARCHAR(20),
  DATE_OPER DATE,
  LIBELLE VARCHAR(128),
  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('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('SOLDE DEBUT DE L''EXERCICE' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(CLIENTS.solde_ini_CLIENT 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_CLIENT IS NOT NULL
        AND CLIENTS.solde_ini_CLIENT <> 0
        AND ((CLIENTS.client = 1)or(CLIENTS.mixte = 1))

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL TICKETS ANT.' AS VARCHAR(128) 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('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE DE VENTE ANT.' AS VARCHAR(128) 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('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL B.L. ANT.' AS VARCHAR(128) 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('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''AVOIR ANT.' AS VARCHAR(128) 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('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BONS RETOUR ANT.' AS VARCHAR(128) 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 varchar(20)),
              CAST(entete_sorties.date_piece AS DATE),
              SUBSTRING(CAST('FACTURE DE VENTE No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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 varchar(20)),
              CAST(entete_sorties.date_piece AS DATE),
              SUBSTRING(CAST('BON DE LIVRAISON No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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 varchar(20)),
              CAST(entete_sorties.date_piece AS DATE),
              SUBSTRING(CAST('TICKET DE CAISSE No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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 varchar(20)),
              CAST(entete_sorties.date_piece AS DATE),
              SUBSTRING(CAST('FACTURE D''AVOIR No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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 varchar(20)),
              CAST(entete_sorties.date_piece AS DATE),
              SUBSTRING(CAST('BON DE RETOUR No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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_CHEQUE as varchar(20)),
              CAST(regl_tiers.date_regt AS DATE),
              SUBSTRING(CAST(regl_tiers.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.NUM_CHEQUE AS varchar(20)),
              CAST(regl_tiers.date_regt AS DATE),
              SUBSTRING(CAST(regl_tiers.LIBELLE AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BON REGL CLIENT ANT.' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM( COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) ) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        GROUP BY REGL_TIERS.id_tier

         UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL AVOIR COM CLIENT ANT.' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(-SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_CLIENT_DETAIL(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  MATRICULE VARCHAR(128),
  CHAUFFEUR VARCHAR(128),
  DATE_OPER DATE,
  LIBELLE VARCHAR(128),
  UNITE VARCHAR(20),
  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,
  MONTANT NUMERIC(18, 2))
AS
DECLARE VARIABLE SOLDE_INI_CLIENT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_VENTES_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_TICKETS_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_BL_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIR_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_RETOURS_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_AVOIRS_REGLS_ANT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_REGLS_ANT NUMERIC(18, 2);
DECLARE VARIABLE NUM VARCHAR(12);
BEGIN
	SELECT
    	clients.solde_ini_CLIENT
    FROM CLIENTS
    WHERE CLIENTS.id_client = :id
    AND CLIENTS.solde_ini_CLIENT <> 0
    AND ((CLIENTS.client = 1)or(CLIENTS.mixte = 1))
    INTO :SOLDE_INI_CLIENT;

    SELECT
    	CAST(SUM(entete_sorties.total_facture) 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
	INTO :TOTAL_VENTES_ANT;

        SELECT
              CAST(SUM(entete_sorties.total_facture) 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
        INTO TOTAL_TICKETS_ANT;


        SELECT
              CAST(SUM(entete_sorties.total_facture) 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
        INTO TOTAL_BL_ANT;

        SELECT
              CAST(SUM(entete_sorties.total_facture) 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
        INTO TOTAL_AVOIR_ANT;

        SELECT
              CAST(SUM(entete_sorties.total_facture) 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
        INTO TOTAL_RETOURS_ANT;

        SELECT
              cast(SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        INTO TOTAL_RETOURS_ANT;

        SELECT
              cast(SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        into :TOTAL_REGLS_ANT;


        if (SOLDE_INI_CLIENT IS NULL) then SOLDE_INI_CLIENT = 0;
        if (TOTAL_VENTES_ANT IS null) then TOTAL_VENTES_ANT = 0;
        if (TOTAL_TICKETS_ANT IS NULL) then TOTAL_TICKETS_ANT = 0;
        if (TOTAL_BL_ANT IS null) then TOTAL_BL_ANT = 0;
        if (TOTAL_AVOIR_ANT IS NULL) then TOTAL_AVOIR_ANT = 0;
        if (TOTAL_RETOURS_ANT IS null) then TOTAL_RETOURS_ANT = 0;
        if (TOTAL_AVOIRS_REGLS_ANT IS NULL) then TOTAL_AVOIRS_REGLS_ANT = 0;
        if (TOTAL_REGLS_ANT IS null) then TOTAL_REGLS_ANT = 0;


     SOLDE = :SOLDE_INI_CLIENT + :TOTAL_VENTES_ANT + :TOTAL_TICKETS_ANT +
             :TOTAL_BL_ANT - :TOTAL_AVOIR_ANT - :TOTAL_RETOURS_ANT -
             TOTAL_REGLS_ANT - :TOTAL_AVOIRS_REGLS_ANT;
     FOR
     	 SELECT
              CAST('' AS varchar(128)),
              :D1-1,
              CAST('0' AS varchar(12)),
              CAST('SOLDE ANTERIEUR : ' AS varchar(128)),
              CAST(' ' AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS smallint)
        FROM CLIENTS
        WHERE CLIENTS.id_client = :id
        AND :SOLDE <> 0

        UNION ALL

          SELECT
         CAST('' AS varchar(128)),
              sorties.date_piece,
              entete_sorties.num_piece,
              SUBSTRING(CAST('FCT No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(CAST(ARTICLES.ref||'-'||sorties.FULL_DESIGNATION AS varchar(128))FROM 1 FOR 128),
              UNITES.UNITE,
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              sorties.TTC,
              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
        LEFT JOIN UNITES ON UNITES.ID_UNITE = SORTIES.ID_UNITE
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 1
        AND entete_sorties.AJOUTER_CREANCE = 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
            CAMION.MATRICULE,
              entete_sorties.date_piece,
              entete_sorties.num_piece,
              CHAUFFEURs.nom,
              CAST('TOTAL FACTURE No : '||entete_sorties.num_piece AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM entete_sorties
        LEFT JOIN CHAUFFEURS ON CHAUFFEURS.ID_CHAUFFEUR =ENTETE_SORTIES.ID_CHAUF1
        LEFT JOIN CAMION ON CAMION.ID_CAMION = entete_sorties.ID_CAMION
        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('' AS varchar(128)),
              sorties.date_piece,
              entete_sorties.num_piece,
              CAST('B.L. No : '||entete_sorties.num_piece AS varchar(128)),
              SUBSTRING(CAST(ARTICLES.ref||'-'||sorties.FULL_DESIGNATION AS varchar(128))FROM 1 FOR 128),
              UNITES.UNITE,
              sorties.quantite,
              sorties.quantite_conditionne,
              sorties.prix_unitaire,
              sorties.ttc,
              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
        LEFT JOIN UNITES ON UNITES.ID_UNITE = SORTIES.ID_UNITE
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 2
        AND entete_sorties.AJOUTER_CREANCE = 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
            CAMION.MATRICULE,
              entete_sorties.date_piece,
              entete_sorties.num_piece,
              CHAUFFEURS.nom,
              SUBSTRING(CAST('TOTAL B.L. No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(4 AS smallint)
        FROM entete_sorties
        LEFT JOIN CHAUFFEURS ON CHAUFFEURS.ID_CHAUFFEUR =ENTETE_SORTIES.ID_CHAUF1
        LEFT JOIN CAMION ON CAMION.ID_CAMION = entete_sorties.ID_CAMION
        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('' AS varchar(128)),
              sorties.date_piece,
              entete_sorties.num_piece,
              SUBSTRING(CAST('AV No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(CAST(ARTICLES.ref||'-'||sorties.FULL_DESIGNATION AS varchar(128))FROM 1 FOR 128),
              UNITES.UNITE,
              -sorties.quantite,
              -sorties.quantite_conditionne,
              sorties.prix_unitaire,
              sorties.ttc,
              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
        LEFT JOIN UNITES ON UNITES.ID_UNITE = SORTIES.ID_UNITE
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 5
        AND entete_sorties.AJOUTER_CREANCE = 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
            CAMION.MATRICULE,
              entete_sorties.date_piece,
              entete_sorties.num_piece,
              CHAUFFEURS.nom,
              SUBSTRING(CAST('TOTAL AVOIR No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              CAST(' ' AS varchar(20)),
              cast(0 AS numeric(18,2)),
              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
        LEFT JOIN CHAUFFEURS ON CHAUFFEURS.ID_CHAUFFEUR =ENTETE_SORTIES.ID_CHAUF1
        LEFT JOIN CAMION ON CAMION.ID_CAMION = entete_sorties.ID_CAMION
        WHERE entete_sorties.id_client = :id
        AND entete_sorties.AJOUTER_CREANCE = 1
        AND entete_sorties.nature_sortie = 5
        AND entete_sorties.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
        CAST('' AS varchar(64)),
              sorties.date_piece,
              entete_sorties.num_piece,
              SUBSTRING(CAST('RET No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(CAST(ARTICLES.ref||'-'||sorties.FULL_DESIGNATION AS varchar(128))FROM 1 FOR 128),
              UNITES.UNITE,
              -sorties.quantite,
              -sorties.quantite_conditionne,
              sorties.prix_unitaire,
              sorties.TTC,
              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
        LEFT JOIN UNITES ON UNITES.ID_UNITE = SORTIES.ID_UNITE
        inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
        WHERE sorties.id_client = :id
        AND SORTIES.nature_sortie = 7
        AND entete_sorties.AJOUTER_CREANCE = 1
        AND SORTIES.date_piece BETWEEN :D1 AND :D2

        UNION all

        SELECT
            CAMION.MATRICULE,
              entete_sorties.date_piece,
              entete_sorties.num_piece,
              	CHAUFFEURS.nom,
              SUBSTRING(CAST('TOTAL RETOUR No : '||entete_sorties.num_piece AS varchar(128))FROM 1 FOR 128),
              CAST(' ' AS varchar(20)),
              cast(0 AS numeric(18,2)),
              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
        LEFT JOIN CHAUFFEURS ON CHAUFFEURS.ID_CHAUFFEUR =ENTETE_SORTIES.ID_CHAUF1
        LEFT JOIN CAMION ON CAMION.ID_CAMION = entete_sorties.ID_CAMION
        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('' AS varchar(64)),
              regl_tiers.date_regt,
              regl_tiers.num_piece,
              SUBSTRING(cast(regl_tiers.NUM_CHEQUE AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(cast('Reglement client No : ' || regl_tiers.num_piece || ' / ' || regl_tiers.libelle AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(cast(regl_tiers.ETAT AS varchar(20))FROM 1 FOR 20),
              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(0 AS numeric(18,2)),
              cast(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST('' AS varchar(64)),
              regl_tiers.date_regt,
              regl_tiers.num_piece,
              SUBSTRING(cast(regl_tiers.NUM_CHEQUE AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(cast('Avoir Com client No : ' || regl_tiers.num_piece || ' / ' || regl_tiers.libelle AS varchar(128))FROM 1 FOR 128),
              SUBSTRING(cast(regl_tiers.ETAT AS varchar(20))FROM 1 FOR 20),
              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(0 AS numeric(18,2)),
              cast(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) AS numeric(18,2)),
              cast(10 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
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')



     ORDER BY 2, 3, 13

        INTO :MATRICULE, :DATE_OPER, :NUM, :LIBELLE, :PRODUIT, :UNITE, :QTE, :FRX, :PU, :MONTANT,  :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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_COMPTE(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CPTE INTEGER,
  DATE_OP DATE,
  LIB VARCHAR(128),
  CHEQUE VARCHAR(7),
  FAM_REC_DEP VARCHAR(128),
  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(:id AS INTEGER),
               CAST('' AS VARCHAR(7)),
               cast(:DD AS date),
               CAST('Solde au : '||:dd  AS VARCHAR(128) CHARACTER SET NONE),
               CAST(''  AS VARCHAR(128) CHARACTER SET NONE),
               case when solde >= 0 then solde end,
               case when solde < 0 then -solde end,
               CAST(1 AS INTEGER),
               CAST('' AS VARCHAR(16)),
               1
        FROM SOLDE_COMPTE(:ID,:DD)

        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),
               SUBSTRING(CAST(regl_tiers.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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
        WHERE regl_tiers.type_regt = 1
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
        AND ((UPPER(regl_tiers.etat) = 'OK')or(UPPER(regl_tiers.etat) = 'INSTANCE'))

        UNION all

        SELECT
               CAST(RECETTES.id_cpte AS INTEGER),
               cast(RECETTES.num_cheque AS varchar(7)),
               CAST(RECETTES.date_rec AS DATE),
               SUBSTRING(CAST(recettes.lib_rec  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               SUBSTRING(CAST(familles_recettes.famille  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               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 ((UPPER(RECETTES.etat) = 'OK')or(UPPER(RECETTES.etat) = 'INSTANCE'))

        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),
               SUBSTRING(CAST(regl_tiers.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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
        WHERE regl_tiers.type_regt = 0
        AND id_cpte = :id
        AND regl_tiers.date_regt BETWEEN :D1  AND :D2
		AND ((UPPER(regl_tiers.etat) = 'OK')or(UPPER(regl_tiers.etat) = 'INSTANCE'))

        UNION all

        SELECT
               CAST(depenses.id_cpte AS INTEGER),
               cast(DEPENSES.num_cheque AS varchar(7)),
               CAST(DEPENSES.date_dep AS DATE),
               SUBSTRING(CAST(depenses.lib_dep  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               SUBSTRING(CAST(familles_charges.famille  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               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 ((UPPER(DEPENSES.etat) = 'OK')or(UPPER(DEPENSES.etat) = 'INSTANCE'))

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_debit AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               SUBSTRING(CAST(VIR_FONDS.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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),
               SUBSTRING(CAST(VIR_FONDS.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_COMPTE_SANS(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  ID_CPTE INTEGER,
  DATE_OP DATE,
  LIB VARCHAR(128),
  CHEQUE VARCHAR(7),
  FAM_REC_DEP VARCHAR(128),
  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),
               substring(CAST('REGL. CL. : ' ||CLIENTS.denomination  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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 (UPPER(regl_tiers.etat) = 'OK')

        UNION all

        SELECT
               CAST(RECETTES.id_cpte AS INTEGER),
               cast(RECETTES.num_cheque AS varchar(7)),
               CAST(RECETTES.date_rec AS DATE),
               substring(CAST(recettes.lib_rec  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               substring(CAST(familles_recettes.famille  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               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 (UPPER(RECETTES.etat) = 'OK')

        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),
               substring(CAST('REGL. FOUR. : ' ||CLIENTS.denomination  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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 (UPPER(regl_tiers.etat) = 'OK')

        UNION all

        SELECT
               CAST(depenses.id_cpte AS INTEGER),
               cast(DEPENSES.num_cheque AS varchar(7)),
               CAST(DEPENSES.date_dep AS DATE),
               substring(CAST(depenses.lib_dep  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               substring(CAST(familles_charges.famille  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               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 (UPPER(DEPENSES.etat) = 'OK')

        UNION all

        SELECT
               CAST(vir_fonds.id_compte_debit AS INTEGER),
               CAST('' AS VARCHAR(7)),
               CAST(VIR_FONDS.date_oper AS DATE),
               substring(CAST(VIR_FONDS.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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),
               substring(CAST(VIR_FONDS.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE) from 1 for 128),
               CAST(''  AS VARCHAR(128) 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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_FOURNISSEUR (
  ID        INTEGER,
  D1        DATE,
  D2        DATE
) RETURNS (
  ID_FOUR   INTEGER,
  NUM       VARCHAR(20),
  DATE_OPER DATE,
  LIBELLE   VARCHAR(128),
  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(20)),
              CAST(:D1-1 AS DATE),
              CAST('SOLDE INITIAL' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(CLIENTS.solde_ini_FOURN 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_FOURN IS NOT NULL
        AND CLIENTS.solde_ini_FOURN <> 0
        AND ((CLIENTS.client = 0)or(CLIENTS.mixte = 1))

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL IMPORTATIONS ANT.' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(SUM(entete_entrees.CV_DINARS) 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 = 1
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_d10 < :D1
        GROUP BY entete_entrees.id_fourn


        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''ACHAT ANT.' AS VARCHAR(128) 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(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE PRESTATION ANT.' AS VARCHAR(128) 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 = 5
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact < :D1
        GROUP BY entete_entrees.id_fourn

        UNION ALL

        SELECT
              CAST(ENTETE_SORTIES.ID_FOUR_TR AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL TRANS VENTES ANT.' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(SUM(ENTETE_SORTIES.MONTANT_TR) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID
        AND ENTETE_SORTIES.DATE_PIECE < :D1
        GROUP BY ENTETE_SORTIES.ID_FOUR_TR

        UNION ALL

        SELECT
              CAST(ENTETE_ENTREES.ID_FOUR_TR AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL TRANS ACHATS ANT.' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(SUM(ENTETE_ENTREES.MONTANT_TR) AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.ID_FOUR_TR = :ID
        AND ENTETE_ENTREES.DATE_FACT < :D1
        GROUP BY ENTETE_ENTREES.ID_FOUR_TR

        UNION ALL

        SELECT
              CAST(DEPENSES.ID_TIERS AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL DEPENSES ANT.' AS VARCHAR(128) 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.ID_CPTE is null
        AND DEPENSES.DATE_DEP < :D1
        GROUP BY DEPENSES.ID_TIERS

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL FACTURE D''AVOIR ANR' AS VARCHAR(128) 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(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL BON REGL FOURN ANT' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        GROUP BY regl_tiers.id_tier

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('0' AS VARCHAR(20)),
              CAST(:D1-1 AS DATE),
              CAST('TOTAL AVOIRS COM FOURN ANT' AS VARCHAR(128) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(-SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        GROUP BY regl_tiers.id_tier

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST(entete_entrees.num_fact AS VARCHAR(20)),
              CAST(entete_entrees.date_d10 AS DATE),
              CAST('DOSSIER IMPORT. No :'||entete_entrees.NUM_DOS AS VARCHAR(128) CHARACTER SET NONE),
              CAST(entete_entrees.CV_DINARS 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 = 1
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_d10 BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_entrees.id_fourn AS INTEGER),
              CAST(entete_entrees.num_fact AS VARCHAR(20)),
              CAST(entete_entrees.date_fact AS DATE),
              CAST('FACTURE D''ACHAT No :'||entete_entrees.num_FACT AS VARCHAR(128) 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(entete_entrees.id_fourn AS INTEGER),
              CAST(entete_entrees.NUM_DOS AS VARCHAR(20)),
              CAST(entete_entrees.date_fact AS DATE),
              CAST(entete_entrees.OBJET_PRESTATION AS VARCHAR(128) 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 = 5
        AND entete_entrees.ajouter_DETTES = 1
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

         UNION ALL

        SELECT
              CAST(ENTETE_SORTIES.ID_FOUR_TR AS INTEGER),
              CAST(ENTETE_SORTIES.NUM_PIECE AS VARCHAR(20)),
              CAST(ENTETE_SORTIES.DATE_PIECE AS DATE),
              CAST('TRANSPORT VENTES No :'||ENTETE_SORTIES.NUM_PIECE AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_SORTIES.MONTANT_TR AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.id_four_TR = :ID
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :D1 AND :D2
        and ENTETE_SORTIES.MONTANT_TR <> 0
        and ENTETE_SORTIES.MONTANT_TR is not null

        UNION ALL

        SELECT
              CAST(ENTETE_ENTREES.ID_FOUR_TR AS INTEGER),
              CAST(ENTETE_ENTREES.NUM_FACT AS VARCHAR(20)),
              CAST(ENTETE_ENTREES.DATE_FACT AS DATE),
              CAST('TRANSPORT ACHATS No :'||ENTETE_ENTREES.NUM_FACT AS VARCHAR(128) CHARACTER SET NONE),
              CAST(ENTETE_ENTREES.MONTANT_TR AS NUMERIC(18,2)),
              CAST(0 AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.id_four_TR = :ID
        AND ENTETE_ENTREES.DATE_FACT BETWEEN :D1 AND :D2
        and ENTETE_ENTREES.MONTANT_TR <> 0
        and ENTETE_ENTREES.MONTANT_TR is not null

        UNION ALL

        SELECT
              CAST(DEPENSES.ID_TIERS AS INTEGER),
              CAST(DEPENSES.NUM_CHEQUE AS VARCHAR(20)),
              CAST(DEPENSES.DATE_DEP AS DATE),
              CAST(depenses.LIB_DEP AS VARCHAR(128) 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(20)),
              CAST(fact_dd_jointe.date_fact AS DATE),
              CAST('FACTURE JOINTE DOS IMPORT. No :'||entete_entrees.NUM_D10 AS VARCHAR(128) 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(20)),
              CAST(entete_entrees.date_fact AS DATE),
              CAST('FACTURE D''AVOIR No :'||entete_entrees.num_FACT AS VARCHAR(128) 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_CHEQUE AS VARCHAR(20)),
              CAST(regl_tiers.date_regt AS DATE),
              CAST(regl_tiers.libelle AS VARCHAR(128) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST(DEPENSES.ID_TIERS AS INTEGER),
              CAST(DEPENSES.NUM_CHEQUE AS VARCHAR(20)),
              CAST(DEPENSES.DATE_DEP AS DATE),
              CAST(depenses.LIB_DEP AS VARCHAR(128) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(DEPENSES.MONTANT AS NUMERIC(18,2)),
              CAST(1 AS NUMERIC(18,2))
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :ID
        AND DEPENSES.ID_CPTE IS NOT NULL
        AND DEPENSES.DATE_DEP BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.NUM_CHEQUE AS VARCHAR(20)),
              CAST(regl_tiers.date_regt AS DATE),
              CAST(regl_tiers.libelle AS VARCHAR(128) CHARACTER SET NONE),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE RELEVE_FOURNISSEUR_DETAIL (
  ID        INTEGER,
  D1        DATE,
  D2        DATE
) RETURNS (
  DATE_OPER DATE,
  LIBELLE   VARCHAR(128),
  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,
  MONTANT   NUMERIC(18,2),
  UNITE     VARCHAR(20)
)
AS
DECLARE VARIABLE SOLDE_INI_FOUR NUMERIC(18, 2);
    DECLARE VARIABLE ACHATS_ANT NUMERIC(18, 2);
    DECLARE VARIABLE TR_ACHATS_ANT NUMERIC(18, 2);
    DECLARE VARIABLE TR_VENTES_ANT NUMERIC(18, 2);
    DECLARE VARIABLE FACT_JOINT_ANT NUMERIC(18, 2);
    DECLARE VARIABLE DEPENSES_ANT NUMERIC(18, 2);
    DECLARE VARIABLE AVOIR_ANT NUMERIC(18, 2);
    DECLARE VARIABLE REGL_ANT NUMERIC(18, 2);
    DECLARE VARIABLE AV_COM_ANT NUMERIC(18, 2);
BEGIN
	 SELECT
              COALESCE( clients.SOLDE_INI_FOURN,0 )
        FROM CLIENTS
        WHERE CLIENTS.id_client = :id
        AND CLIENTS.SOLDE_INI_FOURN <> 0
        AND ((CLIENTS.client = 0)or(CLIENTS.mixte = 1))
        into :SOLDE_INI_FOUR;

        SELECT
        	COALESCE(SUM(entete_entrees.net_a_payer),0)
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :id
        AND entete_entrees.nature_entree <> 4
        AND entete_entrees.date_fact < :D1
        INTO :ACHATS_ANT;

        SELECT
             COALESCE(SUM(ENTETE_SORTIES.MONTANT_TR),0)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.id_four_TR = :id
        AND ENTETE_SORTIES.DATE_PIECE < :D1
        INTO :TR_VENTES_ANT;

        SELECT
              COALESCE(SUM(ENTETE_ENTREES.MONTANT_TR),0)
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.id_four_TR = :id
        AND ENTETE_ENTREES.DATE_FACT < :D1
        INTO :TR_ACHATS_ANT;

         SELECT
              COALESCE(SUM(fact_dd_jointe.TTC),0)
        FROM fact_dd_jointe
        WHERE fact_dd_jointe.id_fourn = :id
        AND fact_dd_jointe.date_FACT < :D1
        INTO :FACT_JOINT_ANT;

        SELECT
              COALESCE(SUM(DEPENSES.MONTANT),0)
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :id
        AND DEPENSES.DATE_DEP < :D1
        INTO :DEPENSES_ANT;

        SELECT
              COALESCE(SUM(entete_entrees.net_a_payer),0)
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :id
        AND entete_entrees.nature_entree = 4
        AND entete_entrees.date_fact < :D1
        INTO :AVOIR_ANT;

        SELECT
              COALESCE(SUM(regl_tiers.montant),0)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 0
        AND regl_tiers.date_regt < :D1
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        INTO :REGL_ANT;

        SELECT
              SUM(COALESCE(regl_tiers.montant,0)) + sum(COALESCE(regl_tiers.remise,0))
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 3
        AND regl_tiers.date_regt < :D1
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        INTO :AV_COM_ANT;

        if (SOLDE_INI_FOUR is null) then SOLDE_INI_FOUR = 0;
        if (ACHATS_ANT is null) then ACHATS_ANT = 0;
        if (TR_ACHATS_ANT is null) then TR_ACHATS_ANT = 0;
        if (TR_VENTES_ANT is null) then TR_VENTES_ANT = 0;
        if (FACT_JOINT_ANT is null) then FACT_JOINT_ANT = 0;
        if (DEPENSES_ANT is null) then DEPENSES_ANT = 0;
        if (AVOIR_ANT is null) then AVOIR_ANT = 0;
        if (REGL_ANT is null) then REGL_ANT = 0;
        if (AV_COM_ANT is null) then AV_COM_ANT = 0;

     	SOLDE = :SOLDE_INI_FOUR + :ACHATS_ANT + :TR_ACHATS_ANT +
                :TR_VENTES_ANT + :FACT_JOINT_ANT + :DEPENSES_ANT -
                :AVOIR_ANT - :REGL_ANT - :AV_COM_ANT;
        IF (SOLDE IS NULL) then solde = 0;
     FOR
        SELECT
              :D1-1,
              CAST('SOLDE ANTERIEUR : ' AS varchar(128)),
              CAST(' ' AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(0 AS smallint)
        FROM CLIENTS
        WHERE CLIENTS.id_client = :id
        AND :SOLDE <> 0

        union all

        SELECT
              entrees.date_entree,
              CAST('B.R. No : '||entete_entrees.num_fact AS varchar(64)),
              case when entrees.NATURE_ENTREE = 5 then entete_entrees.OBJET_PRESTATION
                   when entrees.NATURE_ENTREE <> 5 then articles.designation

              end,

              UNITES.UNITE,
              entrees.quantite,
              entrees.quantite_conditionne,
              entrees.p_u_da,
              ENTREES.TTC,
              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
        LEFT JOIN UNITES ON UNITES.ID_UNITE = ENTREES.ID_UNITE
        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.nature_entree <> 5
        AND entrees.date_entree BETWEEN :D1 AND :D2

        UNION

        SELECT
              entete_entrees.date_fact,
              CAST(' ' AS varchar(64)),
              CAST('TOTAL B.R. No : '||entete_entrees.num_fact AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(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.nature_entree <> 5
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

        UNION

        SELECT
              entete_entrees.DATE_FACT,
              CAST('PRESTATION : '||entete_entrees.NUM_DOS AS varchar(64)),
              CAST(entete_entrees.OBJET_PRESTATION AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(2 AS smallint)
        FROM entete_entrees
        WHERE entete_entrees.id_fourn = :id
        AND entete_entrees.nature_entree = 5
        AND entete_entrees.date_fact BETWEEN :D1 AND :D2

        UNION

        SELECT
              ENTETE_SORTIES.DATE_PIECE,
              CAST(' ' AS varchar(64)),
              CAST('TRANSPORT VENTE No : '||ENTETE_SORTIES.NUM_PIECE AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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_SORTIES.MONTANT_TR,
              cast(0 AS numeric(18,2)),
              cast(3 AS smallint)
        FROM ENTETE_SORTIES
        WHERE ENTETE_SORTIES.id_four_TR = :id
        and ENTETE_SORTIES.MONTANT_TR <> 0
        and ENTETE_SORTIES.MONTANT_TR is not null
        AND ENTETE_SORTIES.DATE_PIECE BETWEEN :D1 AND :D2

        UNION

        SELECT
              ENTETE_ENTREES.DATE_FACT,
              CAST(' ' AS varchar(64)),
              CAST('TRANSPORT ACHAT No : '||ENTETE_ENTREES.NUM_FACT AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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.MONTANT_TR,
              cast(0 AS numeric(18,2)),
              cast(4 AS smallint)
        FROM ENTETE_ENTREES
        WHERE ENTETE_ENTREES.id_four_TR = :id
        AND ENTETE_ENTREES.DATE_FACT BETWEEN :D1 AND :D2

         UNION

        SELECT
              fact_dd_jointe.date_fact,
              CAST(' ' AS varchar(64)),
              CAST('FACTURE JOINT No : '||fact_dd_jointe.num_fact || ' DOS IMPORT : '||entete_entrees.NUM_D10 AS varchar(128)),
              CAST(' ' AS varchar(20)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              fact_dd_jointe.TTC,
              cast(0 AS numeric(18,2)),
              cast(5 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(DEPENSES.NUM_CHEQUE AS varchar(64)),
              CAST('DEPENSE No : '||DEPENSES.NUM_DEP AS varchar(128)),
              CAST(' ' AS varchar(20)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,6)),
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              DEPENSES.MONTANT,
              cast(0 AS numeric(18,2)),
              cast(6 AS smallint)
        FROM DEPENSES
        WHERE DEPENSES.ID_TIERS = :id
        AND DEPENSES.DATE_DEP BETWEEN :D1 AND :D2

        UNION

        SELECT
              entrees.date_entree,
              CAST('AVOIR B.R. No : '||entete_entrees.num_fact AS varchar(64)),
              articles.designation,
              UNITES.UNITE,
              entrees.quantite,
              entrees.quantite_conditionne,
              entrees.p_u_da,
              entrees.TTC,
              cast(0 AS numeric(18,2)),
              cast(0 AS numeric(18,2)),
              cast(7 AS smallint)
        FROM entrees
        inner join articles on articles.id_art = entrees.id_article
        LEFT JOIN UNITES ON UNITES.ID_UNITE = ARTICLES.ID_UNITE
        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(' ' AS varchar(64)),
              CAST('TOTAL  AVOIR B.R. No : '||entete_entrees.num_fact AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              entete_entrees.net_a_payer,
              cast(8 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_CHEQUE AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(COALESCE(regl_tiers.montant,0) + COALESCE(regl_tiers.remise,0) AS numeric(18,2)),
              cast(9 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 0
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

         UNION

        SELECT
              regl_tiers.date_regt,
              cast(regl_tiers.NUM_CHEQUE AS varchar(64)),
              cast(regl_tiers.libelle AS varchar(128)),
              CAST(' ' AS varchar(20)),
              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(0 AS numeric(18,2)),
              cast(COALESCE(regl_tiers.montant,0) + COALESCE(regl_tiers.remise,0) AS numeric(18,2)),
              cast(10 AS smallint)
        FROM regl_tiers
        WHERE regl_tiers.id_tier = :id
        AND regl_tiers.type_regt = 3
        AND (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        AND regl_tiers.date_regt BETWEEN :D1 AND :D2

         ORDER BY 1, 11

        INTO :DATE_OPER, :LIBELLE, :PRODUIT, :UNITE, :QTE, :FRX, :PU, :MONTANT, :TTC, :RGT, NAT

        DO
        BEGIN
             if (TTC IS NULL) then TTC = 0;
             if (RGT IS null) then RGT = 0;
             if (SOLDE IS null) then SOLDE = 0;
             SOLDE = :SOLDE + :TTC - :RGT ;
             if (MONTANT = 0) then MONTANT = NULL;
             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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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);
DECLARE VARIABLE CREANCE_INI NUMERIC(18, 2);
DECLARE VARIABLE REGLEMENT_INI 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;
     EXECUTE PROCEDURE TOTAL_VENTES_CLIENT_ANT(:ID_CLIENT,:D1) RETURNING_VALUES :CREANCE_INI;
     EXECUTE PROCEDURE TOTAL_REGLT_CLIENT_ANT(:ID_CLIENT,:D1) RETURNING_VALUES :REGLEMENT_INI;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
     if (CREANCE IS NULL) then CREANCE = 0;
     if (REGLEMENT IS NULL) then REGLEMENT = 0;
     if (CREANCE_INI IS NULL) then CREANCE_INI = 0;
     if (REGLEMENT_INI IS NULL) then REGLEMENT_INI = 0;
     SOLDE = :SOLDE_INI + :CREANCE_INI - :REGLEMENT_INI + :CREANCE - :REGLEMENT;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE SOLDE_FOURN(
  ID_TIER INTEGER,
  D DATE)
RETURNS(
  SOLDE NUMERIC(18, 2))
AS
DECLARE VARIABLE SOLDE_INI NUMERIC(18, 2);
DECLARE VARIABLE FACT_DD NUMERIC(18, 2);
DECLARE VARIABLE ACHATS NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_ACHAT NUMERIC(18, 2);
DECLARE VARIABLE TOTAL_TRANS 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);
DECLARE VARIABLE REGLEMENT NUMERIC(18, 2);
BEGIN
	SELECT
    	CLIENTS.SOLDE_INI_FOURN
    FROM CLIENTS
    WHERE CLIENTS.ID_CLIENT = :ID_TIER
    INTO :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)or( entete_entrees.nature_entree = 5))
    AND entete_entrees.date_fact <= :D
    AND entete_entrees.ajouter_dettes = 1
    INTO :TOTAL_ACHAT;
    if (TOTAL_ACHAT IS NULL) then TOTAL_ACHAT = 0;

    SELECT
    	sum(ENTETE_SORTIES.MONTANT_TR)
    FROM ENTETE_SORTIES
    WHERE ENTETE_SORTIES.ID_FOUR_TR = :ID_TIER
    AND ENTETE_SORTIES.DATE_PIECE <= :D
    INTO :TOTAL_TRANS;
    if (TOTAL_TRANS IS NULL) then TOTAL_TRANS =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.date_d10 <= :D
    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 (entete_entrees.nature_entree = 4)
    AND entete_entrees.date_fact <= :D
    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 <= :D
    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 <= :D
    and depenses.id_cpte is null
    INTO :TOTAL_DEP ;
    if (TOTAL_DEP IS NULL) then TOTAL_DEP =0;

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

    SELECT
    	sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
    FROM regl_tiers
    WHERE regl_tiers.id_tier = :ID_TIER
    AND regl_tiers.type_regt = 0
    AND regl_tiers.date_regt <= :D
    INTO :REGLEMENT;
    if (REGLEMENT IS NULL) then REGLEMENT = 0;

    SELECT
    	sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
    FROM regl_tiers
    WHERE regl_tiers.id_tier = :ID_TIER
    AND regl_tiers.type_regt = 3
    AND regl_tiers.date_regt <= :D
    INTO :av_com;
    if (av_com IS NULL) then av_com = 0;
    REGLEMENT = :REGLEMENT + :av_com;
    SOLDE = :SOLDE_INI + :ACHATS - :REGLEMENT;
    SUSPEND;
  END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE SOLDE_CLIENT_BON(
  ID_ENT_SORT INTEGER,
  ID_CLIENT INTEGER,
  D TIMESTAMP,
  DU DATE)
RETURNS(
  SOLDE_ANT NUMERIC(18, 2),
  VERSEMENT NUMERIC(18, 2),
  SOLDE NUMERIC(18, 2))
AS
BEGIN
    select first 1 COALESCE(solde,0) from RELEVE_CLIENT_TIME(:ID_CLIENT,:DU,:D - 0.00069)
	order by date_oper desc
    INTO :SOLDE_ANT;

    select first 1 COALESCE(solde,0) from RELEVE_CLIENT_TIME(:ID_CLIENT,:DU,:D)
	order by date_oper desc
    INTO :SOLDE;

     SELECT
           COALESCE(sum(regl_tiers.montant),0)+COALESCE(sum(regl_tiers.remise),0)
     FROM regl_tiers
     WHERE regl_tiers.DATETIME_PIECE BETWEEN :D - 0.0069 AND :D + 0.0069
     and REGL_TIERS.ID_TIER = :ID_CLIENT
     INTO :VERSEMENT;

     if (SOLDE_ANT IS NULL) then SOLDE_ANT = 0;
     if (VERSEMENT IS NULL) then VERSEMENT = 0;
     if (SOLDE IS NULL) then SOLDE = 0;

  SUSPEND;
END^
SET TERM ; ^

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

SET TERM ^ ;

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);
DECLARE VARIABLE TYPE_CLIENT SMALLINT;
DECLARE VARIABLE DU DATE;
BEGIN
	SELECT CLIENTS.CLIENT FROM CLIENTS WHERE CLIENTS.ID_CLIENT = :ID_CLIENT INTO :TYPE_CLIENT;
    select first 1 PARAMETRES.EXER_DU from PARAMETRES INTO :DU;

 IF (TYPE_CLIENT = 1) THEN
    	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;
     END

     IF (TYPE_CLIENT = 0) THEN
    	BEGIN
     		EXECUTE PROCEDURE SOLDE_INI_TIERS(:ID_CLIENT) RETURNING_VALUES :SOLDE_INI;
     		EXECUTE PROCEDURE TOTAL_ACHAT_LOCAL(:ID_CLIENT,:DU,:D) RETURNING_VALUES :CREANCE;
     		EXECUTE PROCEDURE TOTAL_REGLT_FOURNISSEUR(:ID_CLIENT,:DU, :D) RETURNING_VALUES :REGLEMENT;
        REGLEMENT = :REGLEMENT - :MNT_RGLT;
     END


     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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE SOLDE_INI_TIERS(
  ID INTEGER)
RETURNS(
  SOLDE_INI NUMERIC(18, 2))
AS
BEGIN
     SELECT
          CASE WHEN (CLIENTS.CLIENT = 1)or(clients.MIXTE = 1) THEN SOLDE_INI_CLIENT
               WHEN (CLIENTS.CLIENT = 0)or(clients.MIXTE = 1) THEN SOLDE_INI_FOURN
          END
     FROM CLIENTS
     WHERE ID_CLIENT = :id
     INTO :SOLDE_INI;
     if (SOLDE_INI IS NULL) then SOLDE_INI = 0;
  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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,
  ARRONDI SMALLINT)
AS
BEGIN
     FOR
      SELECT
           nomenclatures.id_matiere,
           nomenclatures.mat_emb,
           nomenclatures.qte,
           nomenclatures.qte_dechet,
           nomenclatures.tx_dechet,
           nomenclatures.arrondi,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     left JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_artICLE = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     INTO :ID_MAT,:NATURE, :QTE, :QTE_DECHET, :TX_DECHET,:ARRONDI, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE STOCK_NOMENCLATURE_EMBALLAGE(
  ID INTEGER,
  D DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_MAT INTEGER,
  QTE NUMERIC(18, 6),
  ARRONDI SMALLINT,
  STOCK NUMERIC(18, 6),
  P_PAMP NUMERIC(18, 2))
AS
BEGIN
     FOR
     SELECT
           nomenclatures.id_matiere,
           nomenclatures.qte,
           nomenclatures.arrondi,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     LEFT JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_artICLE = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     AND nomenclatures.mat_emb = 2
     INTO :ID_MAT,:QTE, :ARRONDI, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE STOCK_NOMENCLATURE_MATIERE(
  ID INTEGER,
  D DATE,
  ID_MAGAZIN INTEGER)
RETURNS(
  ID_MAT INTEGER,
  QTE NUMERIC(18, 6),
  ARRONDI SMALLINT,
  STOCK NUMERIC(18, 6),
  P_PAMP NUMERIC(18, 2))
AS
BEGIN
FOR
     SELECT
           nomenclatures.id_matiere,
           nomenclatures.qte,
           nomenclatures.arrondi,
           etat_stock_depot.stock,
           etat_stock_depot.cout
     FROM nomenclatures
     LEFT JOIN etat_stock_depot(:ID_MAGAZIN, :D) ON etat_stock_depot.id_artICLE = nomenclatures.id_matiere
     WHERE nomenclatures.id_produit_fini = :ID
     AND nomenclatures.mat_emb = 1
     INTO :ID_MAT,:QTE, :ARRONDI, :STOCK, :P_PAMP
do
BEGIN
  SUSPEND;
END
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

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.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
        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;

  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_CLIENT(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_CLIENT_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_CLIENT_BON(
  ID_ENTET_PIECE INTEGER)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^


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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_CLIENT_RGLT(
  ID INTEGER,
  D TIMESTAMP,
  ID_REGL INTEGER)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR(
  ID INTEGER,
  D1 DATE,
  D2 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

ALTER PROCEDURE TOTAL_REGLT_FOURNISSEUR_ANT(
  ID INTEGER,
  D1 DATE)
RETURNS(
  TOTAL_REGL NUMERIC(18, 2))
AS
BEGIN
     SELECT
           sum(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0))
     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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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.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
        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;

  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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.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
        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;
        CREANCE = :TOTAL_VENTE - :TOTAL_AVOIR;

  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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,
          COALESCE(sum(case when (sorties.taux_tva <> 0) AND (SORTIES.nature_sortie = 1) then sorties.ht END),0) -
          COALESCE(sum(case when (sorties.taux_tva <> 0) AND (SORTIES.nature_sortie = 5) then sorties.ht END),0) AS imp,
          COALESCE(sum(case when (sorties.taux_tva = 0) AND (SORTIES.nature_sortie = 1)  then sorties.ht END),0)-
          COALESCE(sum(case when (sorties.taux_tva = 0) AND (SORTIES.nature_sortie = 5) then sorties.ht END),0) AS exo,
          COALESCE(sum(case when (SORTIES.nature_sortie = 1)  then sorties.tva END),0)-
          COALESCE(sum(case when (SORTIES.nature_sortie = 5) then sorties.tva END),0) 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.cession = 0
        GROUP BY code_tva.libelles, 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;
     CA = IMP + EXO;
     SUSPEND;
     END
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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) OR (sorties.nature_sortie = 3));

  UPDATE sorties SET sorties.consomation = sorties.quantite * cr_u WHERE ((sorties.nature_sortie = 1) OR (sorties.nature_sortie = 2) OR (sorties.nature_sortie = 10)OR (sorties.nature_sortie = 3));

  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)or(entete_sorties.nature_sortie = 3));

  SUSPEND;
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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
        INNER JOIN entete_sorties ON entete_sorties.id_ent_sort = SORTIES.id_entete
        WHERE sorties.id_client = :id
        AND sorties.date_piece between :du AND :au
        and SORTIES.AJOUTER_CREANCE = 1
        AND ((SORTIES.NATURE_SORTIE = 2) OR (SORTIES.NATURE_SORTIE = 1))
        INTO :ID_ART, :date_piece, :ref, :designation, :pu, :qte
     do
     BEGIN
  SUSPEND;
  END
END^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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.AJOUTER_CREANCE = 1
        AND ((SORTIES.NATURE_SORTIE = 2) OR (SORTIES.NATURE_SORTIE = 1))
        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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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 entete_sorties.AJOUTER_CREANCE = 1
         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^

SET TERM ; ^

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

SET TERM ^ ;

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.AJOUTER_DETTE = 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.AJOUTER_DETTE = 1
         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.AJOUTER_DETTE = 1
         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
         and sorties.AJOUTER_CREANCE = 1
         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)or(SORTIES.nature_sortie = 7))
         AND SORTIES.id_article = :ID_ART
         and sorties.AJOUTER_CREANCE = 1
         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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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 = Sorties.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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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

SET TERM ; ^

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

SET TERM ^ ;

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^

SET TERM ; ^

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

SET TERM ^ ;

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 ^ ;
ALTER PROCEDURE RELEVE_CLIENT_TIME (
  ID        INTEGER,
  D1        TIMESTAMP,
  D2        TIMESTAMP
) RETURNS (
  ID_CLIENT INTEGER,
  NUM       VARCHAR(12),
  DATE_OPER TIMESTAMP,
  LIBELLE   VARCHAR(128),
  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('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('SOLDE DEBUT DE L''EXERCICE' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(CLIENTS.solde_ini_CLIENT 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_CLIENT IS NOT NULL
        AND CLIENTS.solde_ini_CLIENT <> 0
        AND ((CLIENTS.client = 1)or(CLIENTS.mixte = 1))

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL TICKETS ANT.' AS VARCHAR(128) 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.DATETIME_PIECE < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL FACTURE DE VENTE ANT.' AS VARCHAR(128) 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.DATETIME_PIECE < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL B.L. ANT.' AS VARCHAR(128) 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.DATETIME_PIECE < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL FACTURE D''AVOIR ANT.' AS VARCHAR(128) 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.DATETIME_PIECE < :D1
        GROUP BY entete_sorties.id_client

       UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL BONS RETOUR ANT.' AS VARCHAR(128) 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.DATETIME_PIECE < :D1
        GROUP BY entete_sorties.id_client

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(entete_sorties.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST('FACTURE DE VENTE No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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.DATETIME_PIECE BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(entete_sorties.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST('BON DE LIVRAISON No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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.DATETIME_PIECE BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(entete_sorties.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST('TICKET DE CAISSE No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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.DATETIME_PIECE BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(entete_sorties.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST('FACTURE D''AVOIR No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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.DATETIME_PIECE BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(entete_sorties.id_client AS INTEGER),
              CAST(entete_sorties.num_piece AS varchar(12)),
              CAST(entete_sorties.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST('BON DE RETOUR No :'||entete_sorties.num_piece AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              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.DATETIME_PIECE BETWEEN :D1 AND :D2

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              cast(regl_tiers.NUM_CHEQUE as varchar(12)),
              CAST(regl_tiers.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST(regl_tiers.LIBELLE  AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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.DATETIME_PIECE BETWEEN :D1 AND :D2
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST(regl_tiers.NUM_CHEQUE AS varchar(12)),
              CAST(regl_tiers.DATETIME_PIECE AS TIMESTAMP),
              SUBSTRING(CAST(regl_tiers.LIBELLE AS VARCHAR(128) CHARACTER SET NONE ) FROM 1 FOR 128),
              CAST(0 AS NUMERIC(18,2)),
              CAST(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) 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.DATETIME_PIECE BETWEEN :D1 AND :D2
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')

        UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1 - 0.00069 AS TIMESTAMP),
              CAST('TOTAL BON REGL CLIENT ANT.' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(SUM( COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0) ) 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.DATETIME_PIECE < :D1
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        GROUP BY REGL_TIERS.id_tier

         UNION ALL

        SELECT
              CAST(regl_tiers.id_tier AS INTEGER),
              CAST('-' AS varchar(12)),
              CAST(:D1-0.00069 AS TIMESTAMP),
              CAST('TOTAL AVOIR COM CLIENT ANT.' AS VARCHAR(128) CHARACTER SET NONE ),
              CAST(0 AS NUMERIC(18,2)),
              CAST(-SUM(COALESCE(regl_tiers.montant,0)+COALESCE(regl_tiers.remise,0)) 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.DATETIME_PIECE < :D1
        and (substring(REGL_TIERS.ETAT from 1 for 5)  <> 'Annul')
        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^
SET TERM ; ^


update PARAMETRES set PARAMETRES.NEW_SQL = 0 where PARAMETRES.NEW_SQL is null;
update PARAMETRES set PARAMETRES.A_S_COUT = 1 where PARAMETRES.A_S_COUT is null;
update PARAMETRES set PARAMETRES.TRANSPORT = 0 where PARAMETRES.TRANSPORT is null;

update PARAMETRES set PARAMETRES.CLASSE = 0 where PARAMETRES.CLASSE is null;
update PARAMETRES set PARAMETRES.GEST_QUALITE = 0 where PARAMETRES.A_S_COUT is null;
update PARAMETRES set PARAMETRES.TAXE_ACHAT = 0 where PARAMETRES.TAXE_ACHAT is null;
update PARAMETRES set PARAMETRES.GEST_QUALITE = 0 where PARAMETRES.GEST_QUALITE is null;
update PARAMETRES set PARAMETRES.TAUX_TIMBRE = 1 where PARAMETRES.TAUX_TIMBRE is null;
update PARAMETRES set PARAMETRES.VAL_MIN_TIMBRE = 5 where PARAMETRES.VAL_MIN_TIMBRE is null;
update PARAMETRES set PARAMETRES.VAL_MAX_TIMBRE = 10000 where PARAMETRES.VAL_MAX_TIMBRE is null;
update CLIENTS set CLIENTS.SOLDE_INI_CLIENT = CLIENTS.SOLDE_INI where CLIENTS.CLIENT = 1 AND  CLIENTS.SOLDE_INI_CLIENT is null;
update CLIENTS set CLIENTS.SOLDE_INI_FOURN = CLIENTS.SOLDE_INI where CLIENTS.CLIENT = 0 AND  CLIENTS.SOLDE_INI_FOURN is null;

commit;

SET TERM ^ ;

EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_ENTREES_DATE_FACT')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_ENTREES_DATE_FACT ON ENTETE_ENTREES(DATE_FACT);';
END^

EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_ENTREES_NUM_FACT')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_ENTREES_NUM_FACT ON ENTETE_ENTREES(NUM_FACT);';
END^
EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_ENTREES_STOCKER')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_ENTREES_STOCKER ON ENTETE_ENTREES(STOCKER);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_ENTREES_ADD_DETTES')) then
       	execute statement 'CREATE INDEX IDX_ENT_ENTREES_ADD_DETTES ON ENTETE_ENTREES(AJOUTER_DETTES);';
END^
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_ENTREES_ID_FOURN')) then
       	execute statement 'CREATE INDEX IDX_ENT_ENTREES_ID_FOURN ON ENTETE_ENTREES(ID_FOURN);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_ENTREES_ID_MAG')) then
       	execute statement 'CREATE INDEX IDX_ENT_ENTREES_ID_MAG ON ENTETE_ENTREES(ID_MAGAZIN);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_ENTREES_NATURE')) then
       	execute statement 'CREATE INDEX IDX_ENT_ENTREES_NATURE ON ENTETE_ENTREES(NATURE_ENTREE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_SORTIES_DATE')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_SORTIES_DATE ON ENTETE_SORTIES(DATE_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_SORTIES_NUM')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_SORTIES_NUM ON ENTETE_SORTIES(NUM_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_SORTIES_ADD_CREANCE')) then
       	execute statement 'CREATE INDEX IDX_ENT_SORTIES_ADD_CREANCE ON ENTETE_SORTIES(AJOUTER_CREANCE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_SORTIES_DESTOCKER')) then
       	execute statement 'CREATE INDEX IDX_ENT_SORTIES_DESTOCKER ON ENTETE_SORTIES(DESTOCKER);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_SORTIES_ID_CLIENT')) then
       	execute statement 'CREATE INDEX IDX_ENT_SORTIES_ID_CLIENT ON ENTETE_SORTIES(ID_CLIENT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_SORTIES_ID_MAG')) then
       	execute statement 'CREATE INDEX IDX_ENT_SORTIES_ID_MAG ON ENTETE_SORTIES(ID_MAGAZIN);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENT_SORTIES_NATURE')) then
       	execute statement 'CREATE INDEX IDX_ENT_SORTIES_NATURE ON ENTETE_SORTIES(NATURE_SORTIE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_TRANSFERT_DATE_PIECE')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_TRANSFERT_DATE_PIECE ON ENTETE_TRANSFERT(DATE_PIECE);';
END^


EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_TRANSFERT_ENTREE')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_TRANSFERT_ENTREE ON ENTETE_TRANSFERT(ENTREE_STOCK);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_TRANSFERT_NUM_PIECE')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_TRANSFERT_NUM_PIECE ON ENTETE_TRANSFERT(NUM_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTETE_TRANSFERT_SORTIE')) then
       	execute statement 'CREATE INDEX IDX_ENTETE_TRANSFERT_SORTIE ON ENTETE_TRANSFERT(SORTIE_STOCK);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTREES_ID_ARTICLE')) then
       	execute statement 'CREATE INDEX IDX_ENTREES_ID_ARTICLE ON ENTREES(ID_ARTICLE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTREES_NATURE_ENTREE')) then
       	execute statement 'CREATE INDEX IDX_ENTREES_NATURE_ENTREE ON ENTREES(NATURE_ENTREE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_ENTREES_STOCKER')) then
       	execute statement 'CREATE INDEX IDX_ENTREES_STOCKER ON ENTREES(STOCKER);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_INVENTAIRE_DATE')) then
       	execute statement 'CREATE INDEX IDX_INVENTAIRE_DATE ON INVENTAIRE(DATE_INVENTAIRE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_INVENTAIRE_ID_ARTICLE')) then
       	execute statement 'CREATE INDEX IDX_INVENTAIRE_ID_ARTICLE ON INVENTAIRE(ID_ARTICLE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_PROFORMA_DATE')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_PROFORMA_DATE ON LIGNES_PROFORMA(DATE_FACT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_PROFORMA_NUM')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_PROFORMA_NUM ON LIGNES_PROFORMA(NUM_FACT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_TRANSFERT_DATE')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_TRANSFERT_DATE ON LIGNES_TRANSFERT(DATE_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_TRANSFERT_ENTETE')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_TRANSFERT_ENTETE ON LIGNES_TRANSFERT(ID_ENTETE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_TRANSFERT_ID_ARTICLE')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_TRANSFERT_ID_ARTICLE ON LIGNES_TRANSFERT(ID_ARTICLE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_LIGNES_TRANSFERT_NUM')) then
       	execute statement 'CREATE INDEX IDX_LIGNES_TRANSFERT_NUM ON LIGNES_TRANSFERT(NUM_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_PROFORMA_DATE')) then
       	execute statement 'CREATE INDEX IDX_PROFORMA_DATE ON PROFORMA(DATE_FACT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_PROFORMA_NUM')) then
       	execute statement 'CREATE INDEX IDX_PROFORMA_NUM ON PROFORMA(NUM_FACT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_RECETTES_DATE')) then
       	execute statement 'CREATE INDEX IDX_RECETTES_DATE ON RECETTES(DATE_REC);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_RECETTES_NUM')) then
       	execute statement 'CREATE INDEX IDX_RECETTES_NUM ON RECETTES(NUM_REC);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_REGL_TIERS_DATE')) then
       	execute statement 'CREATE INDEX IDX_REGL_TIERS_DATE ON REGL_TIERS(DATE_REGT);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_REGL_TIERS_NUM')) then
       	execute statement 'CREATE INDEX IDX_REGL_TIERS_NUM ON REGL_TIERS(NUM_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_SORTIES_DATE')) then
       	execute statement 'CREATE INDEX IDX_SORTIES_DATE ON SORTIES(DATE_PIECE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_SORTIES_DESTOCKER')) then
       	execute statement 'CREATE INDEX IDX_SORTIES_DESTOCKER ON SORTIES(DESTOCKER);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_SORTIES_ID_ARTICLE')) then
       	execute statement 'CREATE INDEX IDX_SORTIES_ID_ARTICLE ON SORTIES(ID_ARTICLE);';
END^

EXECUTE BLOCK
AS
  BEGIN
        if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'IDX_SORTIES_NATURE_SORTIE')) then
       	execute statement 'CREATE INDEX IDX_SORTIES_NATURE_SORTIE ON SORTIES(NATURE_SORTIE);';
END^
SET TERM ; ^

SET TERM ^ ;
EXECUTE BLOCK
AS
  BEGIN
       if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CATALOGUE_ART')) then
       	execute statement 'ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CATALOGUE_TIER')) then
       		execute statement 'ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CONS_ART')) then
       		execute statement 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CONS_CON')) then
       		execute statement 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_CON FOREIGN KEY (ID_ART_CONS) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_DEPENSES_CPTE')) then
       		execute statement 'ALTER TABLE DEPENSES ADD CONSTRAINT FK_DEPENSES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_ENTREES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_ENTREES_TIER')) then
       		execute statement 'ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_TIER FOREIGN KEY (ID_FOURN) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_SORTIES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_SORTIES_TIER')) then
       		execute statement 'ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_TRANSFERT_MAG1')) then
       		execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_TRANSFERT_MAG2')) then
       		execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_ART')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_ENTETE')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_TIER')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_TIER FOREIGN KEY (ID_FOUR) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_FACT_ANT_TIER')) then
       		execute statement 'ALTER TABLE FACT_ANT ADD CONSTRAINT FK_FACT_ANT_TIER FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_FACT_DD_JOINTE_ENTETE')) then
       		execute statement 'ALTER TABLE FACT_DD_JOINTE ADD CONSTRAINT FK_FACT_DD_JOINTE_ENTETE FOREIGN KEY (ID_DD) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_INVENTAIRE_ART')) then
       		execute statement 'ALTER TABLE INVENTAIRE ADD CONSTRAINT FK_INVENTAIRE_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_CONS_ART')) then
       		execute statement 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_CONS_TIERS')) then
       		execute statement 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_TIERS FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_ART')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_ENTETE')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ENTETE FOREIGN KEY (ID_ENTET) REFERENCES PROFORMA(ID_PROF);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_TIER')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_ART')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_MAG1')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_MAG2')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NOMENCLATURES_MAT')) then
       		execute statement 'ALTER TABLE NOMENCLATURES ADD CONSTRAINT FK_NOMENCLATURES_MAT FOREIGN KEY (ID_MATIERE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NOMENCLATURES_PF')) then
       		execute statement 'ALTER TABLE NOMENCLATURES DROP CONSTRAINT FK_NOMENCLATURES_PF;';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NOMENCLATURES')) then
       		execute statement 'ALTER TABLE NOMENCLATURES DROP CONSTRAINT FK_NOMENCLATURES;';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_ART')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_ENTREE')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ENTREE FOREIGN KEY (ID_ENTREE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_SORTIE')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_SORTIE FOREIGN KEY (ID_SORTIE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_PROFORMA_TIER')) then
       		execute statement 'ALTER TABLE PROFORMA ADD CONSTRAINT FK_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_RECETTES_CPTE')) then
       		execute statement 'ALTER TABLE RECETTES ADD CONSTRAINT FK_RECETTES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_REGL_TIERS_CPTE')) then
       		execute statement 'ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_REGL_TIERS_TIER')) then
       		execute statement 'ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_ART')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_ENTETE')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_MAG')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^


EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_TIER')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CATALOGUE_ART')) then
       		execute statement 'ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);';
  END^


EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CATALOGUE_TIER')) then
       		execute statement 'ALTER TABLE CATALOGUE ADD CONSTRAINT FK_CATALOGUE_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CONS_ART')) then
       		execute statement 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_CONS_CON')) then
       		execute statement 'ALTER TABLE CONSIGNATIONS ADD CONSTRAINT FK_CONS_CON FOREIGN KEY (ID_ART_CONS) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_DEPENSES_CPTE')) then
       		execute statement 'ALTER TABLE DEPENSES ADD CONSTRAINT FK_DEPENSES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_ENTREES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_ENTREES_TIER')) then
       		execute statement 'ALTER TABLE ENTETE_ENTREES ADD CONSTRAINT FK_ENTETE_ENTREES_TIER FOREIGN KEY (ID_FOURN) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_SORTIES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_SORTIES_TIER')) then
       		execute statement 'ALTER TABLE ENTETE_SORTIES ADD CONSTRAINT FK_ENTETE_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_TRANSFERT_MAG1')) then
       		execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTETE_TRANSFERT_MAG2')) then
       		execute statement 'ALTER TABLE ENTETE_TRANSFERT ADD CONSTRAINT FK_ENTETE_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_ART')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_ENTETE')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_MAGID_MAG')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_MAGID_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_ENTREES_TIER')) then
       		execute statement 'ALTER TABLE ENTREES ADD CONSTRAINT FK_ENTREES_TIER FOREIGN KEY (ID_FOUR) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_FACT_ANT_TIER')) then
       		execute statement 'ALTER TABLE FACT_ANT ADD CONSTRAINT FK_FACT_ANT_TIER FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_FACT_DD_JOINTE_ENTETE')) then
       		execute statement 'ALTER TABLE FACT_DD_JOINTE ADD CONSTRAINT FK_FACT_DD_JOINTE_ENTETE FOREIGN KEY (ID_DD) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_INVENTAIRE_ART')) then
       		execute statement 'ALTER TABLE INVENTAIRE ADD CONSTRAINT FK_INVENTAIRE_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_CONS_ART')) then
       		execute statement 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_CONS_TIERS')) then
       		execute statement 'ALTER TABLE LIGNES_CONSIGNATION ADD CONSTRAINT FK_LIGNES_CONS_TIERS FOREIGN KEY (ID_TIERS) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_ART')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ART FOREIGN KEY (ID_ART) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_ENTETE')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_ENTETE FOREIGN KEY (ID_ENTET) REFERENCES PROFORMA(ID_PROF);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_PROFORMA_TIER')) then
       		execute statement 'ALTER TABLE LIGNES_PROFORMA ADD CONSTRAINT FK_LIGNES_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_ART')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_MAG1')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG1 FOREIGN KEY (ID_MAGAZIN_1) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_LIGNES_TRANSFERT_MAG2')) then
       		execute statement 'ALTER TABLE LIGNES_TRANSFERT ADD CONSTRAINT FK_LIGNES_TRANSFERT_MAG2 FOREIGN KEY (ID_MAGAZIN_2) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_ART')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_ENTREE')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_ENTREE FOREIGN KEY (ID_ENTREE) REFERENCES ENTETE_ENTREES(ID_ENT_ENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_NUM_SERIES_SORTIE')) then
       		execute statement 'ALTER TABLE NUM_SERIES ADD CONSTRAINT FK_NUM_SERIES_SORTIE FOREIGN KEY (ID_SORTIE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_PROFORMA_TIER')) then
       		execute statement 'ALTER TABLE PROFORMA ADD CONSTRAINT FK_PROFORMA_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_RECETTES_CPTE')) then
       		execute statement 'ALTER TABLE RECETTES ADD CONSTRAINT FK_RECETTES_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_REGL_TIERS_CPTE')) then
       		execute statement 'ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_CPTE FOREIGN KEY (ID_CPTE) REFERENCES COMPTES_TRESORIE(ID_CPTE);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_REGL_TIERS_TIER')) then
       		execute statement 'ALTER TABLE REGL_TIERS ADD CONSTRAINT FK_REGL_TIERS_TIER FOREIGN KEY (ID_TIER) REFERENCES CLIENTS(ID_CLIENT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_ART')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ART FOREIGN KEY (ID_ARTICLE) REFERENCES ARTICLES(ID_ART);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_ENTETE')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_ENTETE FOREIGN KEY (ID_ENTETE) REFERENCES ENTETE_SORTIES(ID_ENT_SORT);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_MAG')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_MAG FOREIGN KEY (ID_MAGAZIN) REFERENCES MAGAZINS(ID_MAG);';
  END^

EXECUTE BLOCK
AS
  BEGIN
		if (not exists(select 1 from rdb$indices ix where  ix.rdb$index_name = 'FK_SORTIES_TIER')) then
       		execute statement 'ALTER TABLE SORTIES ADD CONSTRAINT FK_SORTIES_TIER FOREIGN KEY (ID_CLIENT) REFERENCES CLIENTS(ID_CLIENT);';
  END^

SET TERM  ; ^

commit;

UPDATE ENTREES SET P_U_DEVISE_N = P_U_DEVISE WHERE P_U_DEVISE IS NOT NULL AND P_U_DEVISE_N IS NULL;

update sorties set sorties.DESIGNATION = (select articles.DESIGNATION from articles where articles.ID_ART = sorties.ID_ARTICLE) where sorties.DESIGNATION is null or sorties.DESIGNATION = '';

ALTER TABLE MAGAZINS ALTER COLUMN ADRESSE TYPE VARCHAR(255);
ALTER TABLE MAGAZINS ALTER COLUMN ACTIVITE TYPE VARCHAR(255);
ALTER TABLE ENTETE_SORTIES ALTER COLUMN ACTIVITE_RAISON_SOCIALE TYPE VARCHAR(255);
ALTER TABLE ENTETE_SORTIES ALTER COLUMN ADRESSE_RAISON_SOCIALE TYPE VARCHAR(255);
update BC_F set P_U_6 = P_U where P_U_6 is null;

UPDATE ENTETE_SORTIES SET ENTETE_SORTIES.NUM_PIECE = '0' || ENTETE_SORTIES.NUM_PIECE WHERE CHAR_LENGTH(TRIM(ENTETE_SORTIES.NUM_PIECE)) <= 4;
update CLIENTS  set BLOCKE = 0 where BLOCKE is null;

update LIGNES_TRANSFERT set DATE_PIECE = (select DATE_PIECE  from ENTETE_TRANSFERT where ENTETE_TRANSFERT.ID_ENTETE = LIGNES_TRANSFERT.ID_ENTETE);

update ARTICLES SET PROD_POIDS = 0 WHERE  PROD_POIDS IS NULL;
update PARAMETRES SET DEPUIS_FAMILLE = 0 WHERE  DEPUIS_FAMILLE IS NULL;

COMMIT;

INSERT INTO CAT_PROD (LIBELLE, ID_FAM_PROD)
SELECT 'PRINCIPALE', A.ID_ART
FROM ARTICLES A
WHERE A.MATIERE IN (4, 6)
  AND NOT EXISTS (
        SELECT 1 FROM CAT_PROD C
        WHERE C.ID_FAM_PROD = A.ID_ART
          AND C.LIBELLE = 'PRINCIPALE')
  AND (SELECT COUNT(*) FROM CAT_PROD) = 0
  AND COALESCE((SELECT FIRST 1 ACT_NOM FROM PARAMETRES), 0) <> 1;
COMMIT;

UPDATE NOMENCLATURES N
SET N.ID_CAT_PROD = (
    SELECT FIRST 1 C.ID_CAT_PROD
    FROM CAT_PROD C
    WHERE C.ID_FAM_PROD = N.ID_PRODUIT_FINI)
WHERE COALESCE((SELECT FIRST 1 ACT_NOM FROM PARAMETRES), 0) <> 1;
COMMIT;

DELETE FROM NOMENCLATURES
WHERE ID_CAT_PROD IS NULL
  AND COALESCE((SELECT FIRST 1 ACT_NOM FROM PARAMETRES), 0) <> 1;
COMMIT;

UPDATE NOMENCLATURES SET ID_PRODUIT_FINI = ID_CAT_PROD
WHERE COALESCE((SELECT FIRST 1 ACT_NOM FROM PARAMETRES), 0) <> 1;
COMMIT;

UPDATE PARAMETRES SET ACT_NOM = 1;
COMMIT;

CREATE OR ALTER VIEW VIEW_MOUVEMENTS(
  ID_ES,
  ID_ARTICLE,
  REF_ENTREE,
  ID_MAGAZIN,
  DATE_OPER,
  COUT,
  QTE_INI,
  QTE_ENT,
  QTE_SRT,
  VAL_ORD)
AS
SELECT
            INVENTAIRE.ID_INVENTAIRE,
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           INVENTAIRE.COUT,
           INVENTAIRE.quantite,
           0,
           0,
           1
     FROM inventaire

        UNION ALL

     SELECT
     		entrees.id_entree,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           ENTREES.quantite,
           0,
           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)AND (ENTREES.nature_entree <> 5)


     UNION ALL

     SELECT
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           SORTIES.quantite,
           0,
           3
     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_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           lignes_transfert.quantite,
           0,
           4
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           0,
           lignes_transfert.quantite,
           5
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete

      UNION ALL

     SELECT
     		ENTREES.ID_ENTREE,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           0,
           ENTREES.quantite,
           6
     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_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           0,
           SORTIES.quantite,
           7
     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))
     order by 4, 2;

CREATE OR ALTER VIEW RECUP_TVA
(
  DENOMINATION,
  ADRESSE,
  RC,
  AI,
  NIF,
  NIN,
  NUM_FACT,
  DATE_FACT,
  HT,
  TVA,
  MOIS,
  ANNEE,
  INVEST
)
AS
SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      clients.NIN,
      entete_entrees.num_fact,
      entete_entrees.date_fact,
      entete_entrees.TOTAL_TTC + entete_entrees.TIMBRE,
      entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      COALESCE( entete_entrees.INVESTISSMENT,0)
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE ((entete_entrees.nature_entree = 2)or(entete_entrees.nature_entree = 5))
UNION
/*SeLECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      clients.NIN,
      entete_entrees.num_fact,
      entete_entrees.date_fact,
      - entete_entrees.TOTAL_TTC - entete_entrees.TIMBRE,
      - entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      COALESCE( entete_entrees.INVESTISSMENT,0)
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE (entete_entrees.nature_entree = 4)
UNION */
SELECT
      cast('D10 No : '||entete_entrees.num_d10 AS varchar(128)),
      cast('' AS varchar(128)),
      cast('' AS varchar(25)),
      cast('' AS varchar(20)),
      cast('' AS varchar(20)),
      cast('' AS varchar(20)),
      entete_entrees.num_d10,
      entete_entrees.date_d10,
      entete_entrees.CV_D10 +  entete_entrees.TOTAL_DD_HT + entete_entrees.TOTAL_TVA + entete_entrees.TIMBRE,
      entete_entrees.total_tva,
      entete_entrees.mois,
      entete_entrees.annee,
      COALESCE( entete_entrees.INVESTISSMENT,0)
FROM entete_entrees
inner join clients on clients.id_client = entete_entrees.id_fourn
WHERE entete_entrees.nature_entree = 1
UNION
SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      clients.NIN,
      SUBSTRING(fact_dd_jointe.num_fact from 1 for 12),
      fact_dd_jointe.date_fact,
      fact_dd_jointe.MNT_HT + fact_dd_jointe.TVA + fact_dd_jointe.TIMBRE,
      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
UNION
SELECT
      clients.denomination,
      clients.adresse1,
      clients.ref_rc,
      clients.ai,
      clients.i_f,
      clients.NIN,
      DEPENSES.num_fact,
      DEPENSES.date_fact,
      DEPENSES.MONTANT_HT + DEPENSES.montant_tva + coalesce(DEPENSES.MONTANT_TIMBRE,0),
      DEPENSES.montant_tva,
      DEPENSES.mois,
      DEPENSES.annee,
      depenses.is_investissement
FROM DEPENSES
inner join clients on clients.id_client = DEPENSES.id_tiers;

CREATE OR ALTER VIEW VIEW_MOUVEMENTS(
  ID_ES,
  ID_ARTICLE,
  REF_ENTREE,
  ID_MAGAZIN,
  DATE_OPER,
  COUT,
  QTE_INI,
  QTE_ENT,
  QTE_SRT,
  VAL_ORD)
AS
SELECT
            INVENTAIRE.ID_INVENTAIRE,
           INVENTAIRE.id_article,
           inventaire.ref_entree,
           INVENTAIRE.id_magazin,
           INVENTAIRE.date_inventaire,
           INVENTAIRE.COUT,
           INVENTAIRE.quantite,
           0,
           0,
           1
     FROM inventaire

        UNION ALL

     SELECT
     		entrees.id_entree,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           ENTREES.quantite,
           0,
           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)AND (ENTREES.nature_entree <> 5)


     UNION ALL

     SELECT
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           SORTIES.quantite,
           0,
           3
     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_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_2,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           lignes_transfert.quantite,
           0,
           4
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete

     UNION ALL

     SELECT
     		LIGNES_TRANSFERT.ID_LIGNE,
           lignes_transfert.id_article,
           lignes_transfert.ref_entree,
           lignes_transfert.id_magazin_1,
           lignes_transfert.date_piece,
           LIGNES_TRANSFERT.PAMP,
           0,
           0,
           lignes_transfert.quantite,
           5
     FROM lignes_transfert
     inner join entete_transfert on entete_transfert.id_entete = lignes_transfert.id_entete

      UNION ALL

     SELECT
     		ENTREES.ID_ENTREE,
           ENTREES.id_article,
           ENTREES.ref_entree,
           ENTREES.id_magazin,
           ENTREES.date_entree,
           ENTREES.COUT_ACHAT,
           0,
           0,
           ENTREES.quantite,
           6
     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_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           0,
           SORTIES.quantite,
           7
     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
     		sorties.ID_SORTIE,
           SORTIES.id_article,
           SORTIES.ref_entree,
           SORTIES.id_magazin,
           SORTIES.date_piece,
           SORTIES.CR_U,
           0,
           0,
           SORTIES.DECHETS,
           8
     FROM sorties
     inner join entete_sorties on entete_sorties.id_ent_sort = sorties.id_entete
     WHERE (SORTIES.destocker = 1) and (SORTIES.nature_sortie = 4)
     and SORTIES.DECHETS <> 0
     and SORTIES.DECHETS is not null

     order by 4, 2;

commit;

