You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

804 lines
27 KiB

<?xml version="1.0" encoding="ISO-8859-1"?>
<ROOT>
<NODE name="INIT" label="INITIALISATIONS">
<NODE label="Détection Mois modifiés">
<sqlcmd><![CDATA[
DROP TABLE IF EXISTS w_historique_ecritures_total;
CREATE TEMP TABLE w_historique_ecritures_total AS
SELECT mois_comptable, t_comptes.numero as numero, SUM(montant_debit) as debit, SUM(montant_credit) as credit, SUM(nombre_ecritures) as count
FROM compta.p_historique_ecritures_total
JOIN compta.t_comptes ON p_historique_ecritures_total.compte_extra_id = t_comptes.oid
WHERE mois_comptable >= 200801 AND
is_budget IS DISTINCT FROM '1'
GROUP BY 1,2
ORDER BY 1,2;
-- Ecritures exercice en cours
DROP TABLE IF EXISTS w_ECRITURES;
CREATE TEMP TABLE w_ECRITURES AS
SELECT to_number(to_char(D_ECR,'YYYYMM'),'FM999999') AS MOIS,
N_CPT,
date_part('year',D_ECR) AS exercice_comptable,
'0'::text AS exercice_precedent,
MIN(D_ECR) AS PERIODEECRITURE_debut,
MAX(D_ECR) AS PERIODEECRITURE_fin,
Count(*) AS COUNT,
round(SUM(DEBIT),2) AS DEBIT,
round(SUM(CREDIT),2) AS CREDIT
FROM prod_qsp.CPTA_COMPTA_MVT
WHERE D_ECR >= '20080101'
GROUP BY 1,2,3
ORDER BY 1,2;
-- Ecritures exercice précédent
INSERT INTO w_ECRITURES
SELECT to_number(to_char(D_ECR,'YYYYMM'),'FM999999') AS MOIS,
N_CPT,
date_part('year',D_ECR) AS exercice_comptable,
'1'::text AS exercice_precedent,
MIN(D_ECR) AS PERIODEECRITURE_debut,
MAX(D_ECR) AS PERIODEECRITURE_fin,
Count(*) AS COUNT,
round(SUM(DEBIT),2) AS DEBIT,
round(SUM(CREDIT),2) AS CREDIT
FROM prod_qsp.CPTA_COMPTA_MVT_HIST
WHERE D_ECR >= '20080101'
GROUP BY 1,2,3
ORDER BY 1,2;
DROP TABLE IF EXISTS w_mois_modifies;
CREATE TEMP TABLE w_mois_modifies AS
SELECT w_ECRITURES.exercice_comptable,
w_ECRITURES.exercice_precedent,
w_ECRITURES.mois AS mois_comptable,
MIN(PERIODEECRITURE_debut) AS PERIODEECRITURE_debut,
MAX(PERIODEECRITURE_fin) AS PERIODEECRITURE_fin
FROM w_ECRITURES
LEFT JOIN w_historique_ecritures_total ON
w_historique_ecritures_total.mois_comptable = w_ECRITURES.mois AND
w_historique_ecritures_total.numero = w_ECRITURES.N_CPT
GROUP BY 1,2,3
HAVING MAX(CASE WHEN w_historique_ecritures_total.count IS DISTINCT FROM w_ECRITURES.count OR
w_historique_ecritures_total.debit IS DISTINCT FROM w_ECRITURES.debit OR
w_historique_ecritures_total.credit IS DISTINCT FROM w_ECRITURES.credit
THEN 1 ELSE 0 END) = 1;
ALTER TABLE w_mois_modifies ADD CONSTRAINT w_mois_modifies_pk PRIMARY KEY(mois_comptable);
CTISELECT_PROPERTY_READ 'MOISMODIFIES', COALESCE(base.cti_group_concat(mois_comptable),'0')
FROM w_mois_modifies;
CTISELECT_PROPERTY_READ 'MOISMODIFIESC', COALESCE(base.cti_group_concat(mois_comptable),'0')
FROM w_mois_modifies
WHERE exercice_precedent = '0';
CTISELECT_PROPERTY_READ 'MOISMODIFIESP', COALESCE(base.cti_group_concat(mois_comptable),'0')
FROM w_mois_modifies
WHERE exercice_precedent = '1';
echo Mois modifiés Exercice en cours : [MOISMODIFIESC];
echo Mois modifiés Exercice précédent : [MOISMODIFIESP];
]]></sqlcmd>
</NODE>
</NODE>
<NODE name="ECRITURES" label="RECUPERATION ECRITURES">
<condition><![CDATA[
"[MOISMODIFIESC]" != "0"
]]></condition>
<NODE label="Suppression avant regénération">
<sqlcmd><![CDATA[
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_2');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_3');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_4');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_5');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_6');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_7');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_8');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_9');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_10');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_11');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_12');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_13');
DELETE FROM compta.p_historique_ecritures
USING w_mois_modifies
WHERE p_historique_ecritures.mois_comptable = w_mois_modifies.mois_comptable AND
w_mois_modifies.exercice_precedent = '0' AND
is_budget IS DISTINCT FROM '1';
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_1');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_2');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_3');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_4');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_5');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_6');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_7');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_8');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_9');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_10');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_11');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_12');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_13');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_1');
]]></sqlcmd>
</NODE>
<NODE label="Génération détail">
<sqlcmd><![CDATA[
INSERT INTO compta.p_historique_ecritures
(site_id,
type_compta_id,
compte_id,
date_ecriture,
exercice_comptable,
mois_comptable,
clinique_honoraire,
journal_id,
section_analytique_id,
partenaire_id,
compte_extra_id,
comptabilisee,
fin_exercice,
ajustement,
inter_site,
cle_originale,
identifiant_operation,
compte_contrepartie_id,
texte,
date_facture_fournisseur,
piece,
dossier,
date_echeance,
journal_paiement_id,
mode_paiement_id,
numero_cheque,
montant_credit,
montant_debit)
SELECT
t_sites.oid AS site_id,
t_comptes.type_compta_id,
t_comptes.compte_general_id AS compte_id,
date(D_PIECE) AS date_ecriture,
w_mois_modifies.exercice_comptable,
w_mois_modifies.mois_comptable,
t_comptes.clinique_honoraire,
t_journaux.oid AS journal_id,
0::bigint AS section_analytique_id,
t_comptes.partenaire_id,
t_comptes.oid AS compte_extra_id,
'1'::text AS comptabilisee,
'0'::text AS fin_exercice,
'0'::text AS ajustement,
'0'::text AS inter_site,
N_UNIQUE_MVT || '.' || N_SEQ_MVT AS cle_originale,
N_UNIQUE_MVT AS identifiant_operation,
COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
substr(LIB_COMPTA,1,100) AS texte,
'20991231'::date AS date_facture_fournisseur,
substr(PIECE,1,255) AS piece,
substr(PIECE,1,255) AS dossier,
'20991231'::date AS date_echeance,
0::bigint AS journal_paiement_id,
0::bigint AS mode_paiement_id,
''::text AS numero_cheque,
CREDIT AS montant_credit,
DEBIT AS montant_debit
FROM prod_qsp.CPTA_COMPTA_MVT
JOIN w_mois_modifies ON
D_ECR BETWEEN PERIODEECRITURE_debut AND PERIODEECRITURE_fin AND
w_mois_modifies.exercice_precedent = '0'
JOIN compta.t_sites ON t_sites.code_original = CPTA_COMPTA_MVT.N_ETAB
JOIN compta.t_journaux ON C_JAL = t_journaux.code_original
JOIN compta.t_comptes ON N_CPT = t_comptes.numero
LEFT JOIN compta.t_comptes t_comptes_contre ON '999999' = t_comptes_contre.numero
WHERE TP_MVT = 0
ORDER BY N_UNIQUE_MVT, N_SEQ_MVT;
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_1');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_2');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_3');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_4');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_5');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_6');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_7');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_8');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_9');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_10');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_11');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_12');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_13');
]]></sqlcmd>
</NODE>
</NODE>
<NODE name="ECRITURES" label="RECUPERATION ECRITURES (exercice précédent)">
<condition><![CDATA[
"[MOISMODIFIESP]" != "0"
]]></condition>
<NODE label="Suppression avant regénération">
<sqlcmd><![CDATA[
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_2');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_3');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_4');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_5');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_6');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_7');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_8');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_9');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_10');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_11');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_12');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_13');
DELETE FROM compta.p_historique_ecritures
USING w_mois_modifies
WHERE p_historique_ecritures.mois_comptable = w_mois_modifies.mois_comptable AND
w_mois_modifies.exercice_precedent = '1' AND
is_budget IS DISTINCT FROM '1';
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_1');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_2');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_3');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_4');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_5');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_6');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_7');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_8');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_9');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_10');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_11');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_12');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_13');
SELECT base.cti_disable_index('compta', 'i_historique_ecritures_total_1');
]]></sqlcmd>
</NODE>
<NODE label="Génération détail">
<sqlcmd><![CDATA[
INSERT INTO compta.p_historique_ecritures
(site_id,
type_compta_id,
compte_id,
date_ecriture,
exercice_comptable,
mois_comptable,
clinique_honoraire,
journal_id,
section_analytique_id,
partenaire_id,
compte_extra_id,
comptabilisee,
fin_exercice,
ajustement,
inter_site,
cle_originale,
identifiant_operation,
compte_contrepartie_id,
texte,
date_facture_fournisseur,
piece,
dossier,
date_echeance,
journal_paiement_id,
mode_paiement_id,
numero_cheque,
montant_credit,
montant_debit)
SELECT
t_sites.oid AS site_id,
t_comptes.type_compta_id,
t_comptes.compte_general_id AS compte_id,
date(D_PIECE) AS date_ecriture,
w_mois_modifies.exercice_comptable,
w_mois_modifies.mois_comptable,
t_comptes.clinique_honoraire,
t_journaux.oid AS journal_id,
0::bigint AS section_analytique_id,
t_comptes.partenaire_id,
t_comptes.oid AS compte_extra_id,
'1'::text AS comptabilisee,
'0'::text AS fin_exercice,
'0'::text AS ajustement,
'0'::text AS inter_site,
N_UNIQUE_MVT || '.' || N_SEQ_MVT AS cle_originale,
N_UNIQUE_MVT AS identifiant_operation,
COALESCE(t_comptes_contre.oid,0) AS compte_contrepartie_id,
substr(LIB_COMPTA,1,100) AS texte,
'20991231'::date AS date_facture_fournisseur,
substr(PIECE,1,255) AS piece,
substr(PIECE,1,255) AS dossier,
'20991231'::date AS date_echeance,
0::bigint AS journal_paiement_id,
0::bigint AS mode_paiement_id,
''::text AS numero_cheque,
CREDIT AS montant_credit,
DEBIT AS montant_debit
FROM prod_qsp.CPTA_COMPTA_MVT_HIST CPTA_COMPTA_MVT
JOIN w_mois_modifies ON
D_ECR BETWEEN PERIODEECRITURE_debut AND PERIODEECRITURE_fin AND
w_mois_modifies.exercice_precedent = '1'
JOIN compta.t_sites ON t_sites.code_original = CPTA_COMPTA_MVT.N_ETAB
JOIN compta.t_journaux ON C_JAL = t_journaux.code_original
JOIN compta.t_comptes ON N_CPT = t_comptes.numero
LEFT JOIN compta.t_comptes t_comptes_contre ON '999999' = t_comptes_contre.numero
WHERE TP_MVT = 0
ORDER BY N_UNIQUE_MVT, N_SEQ_MVT;
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_1');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_2');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_3');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_4');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_5');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_6');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_7');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_8');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_9');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_10');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_11');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_12');
SELECT base.cti_enable_index('compta', 'i_historique_ecritures_13');
]]></sqlcmd>
</NODE>
</NODE>
<NODE name="POST" label="TRAITEMENTS COMPLEMENTAIRES COMMUNS">
<condition><![CDATA[
"[MOISMODIFIES]" != "0"
]]></condition>
<NODE label="Compléments écritures">
<sqlcmd><![CDATA[
UPDATE compta.p_historique_ecritures
SET clinique_honoraire = 'C'
FROM compta.t_sites
WHERE p_historique_ecritures.site_id = t_sites.oid
AND p_historique_ecritures.clinique_honoraire <> 'C'
AND site_id IN (SELECT oid FROM compta.t_sites WHERE clinique_honoraire <> 'H');
UPDATE compta.p_historique_ecritures
SET clinique_honoraire = 'H'
FROM compta.t_sites
WHERE p_historique_ecritures.site_id = t_sites.oid
AND p_historique_ecritures.clinique_honoraire <> 'H'
AND site_id IN (SELECT oid FROM compta.t_sites WHERE clinique_honoraire = 'H');
UPDATE compta.p_historique_ecritures
SET compte_id = t_comptes.compte_general_id
FROM compta.t_comptes
WHERE comptabilisee <> '1'
AND compte_id = t_comptes.oid
AND t_comptes.oid <> t_comptes.compte_general_id
AND compte_id = compte_extra_id;
]]></sqlcmd>
</NODE>
</NODE>
<NODE name="CHIFFRIER" label="CREATION DU CHIFFRIER">
<condition><![CDATA[
"[MOISMODIFIES]" != "0"
]]></condition>
<NODE label="Récupération chiffrier">
<sqlcmd><![CDATA[
TRUNCATE compta.p_chiffrier_comptable;
INSERT INTO compta.p_chiffrier_comptable
(
mois_comptable,
site_id,
clinique_honoraire,
montant_debit,
montant_credit
)
SELECT to_number((to_char(MOIS_JAL,'YYYYMM')),'FM999999'),
t_sites.oid,
'C'::text,
sum(DEBIT),
sum(CREDIT)
FROM prod_qsp.CUM_JAL_M
JOIN compta.t_sites ON t_sites.code_original = CUM_JAL_M.N_ETAB
GROUP BY 1,2,3
ORDER BY 1;
]]></sqlcmd>
</NODE>
</NODE>
<NODE name="PARAM" label="RECUPERATION DES PARAMETRES">
<NODE label="Paramètres">
<sqlcmd><![CDATA[
-- Préparation comptes
DROP TABLE IF EXISTS w_PLAN_C;
CREATE TEMP TABLE w_PLAN_C AS
SELECT N_CPT,
LIB_CPT,
TP_CPT,
N_CPT AS N_CPT_GENE,
SEQ_TYPE_TIERS,
CASE WHEN NUM_TIERS <> 0 THEN
TP_CPT || to_char(NUM_TIERS,'FM00000')
ELSE '' END AS NUM_TIERS,
CASE WHEN N_CPT IN (SELECT N_CPT FROM prod_QSP.CPTA_COMPTA_MVT) OR
N_CPT IN (SELECT N_CPT FROM prod_QSP.CPTA_COMPTA_MVT_HIST) THEN 1 ELSE 0 END AS USED,
0::bigint AS type_compta_id
FROM prod_QSP.PLAN_C;
INSERT INTO w_PLAN_C
SELECT N_CPT_GENE,
LIB_TYPE_TIERS,
'G'::text,
N_CPT_GENE AS N_CPT_GENE,
SEQ_TYPE_TIERS,
''::text AS NUM_TIERS,
0 AS USED,
0::bigint AS type_compta_id
FROM prod_qsp.TYPE_TIERS_CPT
WHERE N_CPT_GENE <> '' AND
N_CPT_GENE NOT IN (SELECT N_CPT FROm w_PLAN_C);
INSERT INTO w_PLAN_C
SELECT N_CPT_CLI,
LIB_TYPE_TIERS,
'G'::text,
N_CPT_CLI AS N_CPT_GENE,
SEQ_TYPE_TIERS,
''::text AS NUM_TIERS,
0 AS USED,
0::bigint AS type_compta_id
FROM prod_qsp.TYPE_TIERS_CPT
WHERE N_CPT_CLI <> '' AND
N_CPT_CLI NOT IN (SELECT N_CPT FROm w_PLAN_C);
INSERT INTO w_PLAN_C
SELECT N_CPT_TIERS,
LIB_TYPE_TIERS,
'G'::text,
N_CPT_TIERS AS N_CPT_GENE,
SEQ_TYPE_TIERS,
''::text AS NUM_TIERS,
0 AS USED,
0::bigint AS type_compta_id
FROM prod_qsp.TYPE_TIERS_CPT
WHERE N_CPT_TIERS <> '' AND
N_CPT_TIERS NOT IN (SELECT N_CPT FROm w_PLAN_C);
-- Comptes inexistants dans Plan comptable
INSERT INTO w_PLAN_C
SELECT CPTA_COMPTA_MVT.N_CPT,
'Compte ' || CPTA_COMPTA_MVT.N_CPT,
'G'::text,
CPTA_COMPTA_MVT.N_CPT AS N_CPT_GENE,
0,
''::text AS NUM_TIERS,
1 AS USED,
0::bigint AS type_compta_id
FROM prod_qsp.CPTA_COMPTA_MVT_HIST CPTA_COMPTA_MVT
LEFT JOIN prod_qsp.PLAN_C ON CPTA_COMPTA_MVT.N_CPT = PLAN_C.N_CPT
WHERE PLAN_C.N_CPT IS NULL
GROUP BY 1;
-- Type de compte par défaut si non renseigné
UPDATE w_PLAN_C
SET SEQ_TYPE_TIERS = subview.SEQ_TYPE_TIERS
FROM
(
SELECT N_CPT_PX,
(MAX(Array[nb,SEQ_TYPE_TIERS]))[2] AS SEQ_TYPE_TIERS
FROM (
SELECT
CASE WHEN TP_CPT IN ('F') THEN substr(N_CPT,1,3) ELSE substr(N_CPT,1,4) END AS N_CPT_PX,
SEQ_TYPE_TIERS,
count(*) AS nb
FROM w_PLAN_C
WHERE TP_CPT <> 'G' AND SEQ_TYPE_TIERS <> 0
GROUP BY 1,2
ORDER BY 1,3 desc
) subview
GROUP BY 1
) subview
WHERE CASE WHEN TP_CPT IN ('F') THEN substr(N_CPT,1,3) ELSE substr(N_CPT,1,4) END = N_CPT_PX AND
w_PLAN_C.SEQ_TYPE_TIERS = 0;
UPDATE w_PLAN_C
SET N_CPT_GENE =
CASE
WHEN TYPE_TIERS_CPT.N_CPT_GENE <> '' THEN TYPE_TIERS_CPT.N_CPT_GENE
WHEN TYPE_TIERS_CPT.N_CPT_CLI <> '' THEN TYPE_TIERS_CPT.N_CPT_CLI
WHEN TYPE_TIERS_CPT.N_CPT_TIERS <> '' THEN TYPE_TIERS_CPT.N_CPT_TIERS
ELSE w_PLAN_C.N_CPT_GENE END
FROM prod_qsp.TYPE_TIERS_CPT
WHERE TYPE_TIERS_CPT.SEQ_TYPE_TIERS = w_PLAN_C.SEQ_TYPE_TIERS AND
TP_CPT <> 'G';
UPDATE w_PLAN_C
SET N_CPT_GENE = w_PLAN_GEN.N_CPT
FROM w_PLAN_C w_PLAN_GEN
WHERE w_PLAN_C.TP_CPT = 'F' AND
w_PLAN_C.N_CPT = w_PLAN_C.N_CPT_GENE AND
substr(w_PLAN_C.N_CPT,1,3) || '000000' = w_PLAN_GEN.N_CPT AND
w_PLAN_GEN.TP_CPT = 'G';
UPDATE w_PLAN_C
SET N_CPT_GENE = w_PLAN_GEN.N_CPT
FROM w_PLAN_C w_PLAN_GEN
WHERE w_PLAN_C.TP_CPT = 'C' AND
w_PLAN_C.N_CPT = w_PLAN_C.N_CPT_GENE AND
substr(w_PLAN_C.N_CPT,1,4) || '00000' = w_PLAN_GEN.N_CPT AND
w_PLAN_GEN.TP_CPT = 'G';
UPDATE w_PLAN_C
SET N_CPT_GENE = w_PLAN_GEN.N_CPT
FROM w_PLAN_C w_PLAN_GEN
WHERE w_PLAN_C.TP_CPT = 'T' AND
w_PLAN_C.N_CPT = w_PLAN_C.N_CPT_GENE AND
substr(w_PLAN_C.N_CPT,1,4) || '00000' = w_PLAN_GEN.N_CPT AND
w_PLAN_GEN.TP_CPT = 'G';
UPDATE w_PLAN_C
SET USED = 1
WHERE USED = 0 AND
N_CPT IN (SELECT N_CPT_GENE FROM w_PLAN_C WHERE USED = 1);
UPDATE w_PLAN_C
SET TP_CPT = 'G'
WHERE TP_CPT <> 'G' AND
N_CPT = N_CPT_GENE;
UPDATE w_PLAN_C
SET NUM_TIERS = NUM_TIERS || '-' ||
CASE WHEN TP_CPT IN ('F') THEN substr(N_CPT,4) ELSE substr(N_CPT,3) END
WHERE USED = 1 AND
NUM_TIERS IN (
SELECT
NUM_TIERS
FROM w_PLAN_C
WHERE USED = 1 AND NUM_TIERS <> ''
GROUP BY 1
HAVING count(DISTINCT LIB_CPT) > 1
);
UPDATE w_PLAN_C
SET type_compta_id = t_types_compta.oid
FROM compta.t_types_compta
WHERE t_types_compta.code_original =
CASE
WHEN TP_CPT = 'C' THEN 'CLI'
WHEN TP_CPT = 'F' THEN 'FOU'
WHEN TP_CPT = 'R' THEN 'TIE'
WHEN TP_CPT = 'G' AND N_CPT LIKE '6%' OR N_CPT LIKE '7%' OR N_CPT LIKE '8%' THEN 'GES'
ELSE 'BIL' END;
-- Periode comptable
UPDATE base.p_calendrier_mois SET
exercice_mois_comptable_1 = subview.exercice_mois_comptable_1,
exercice_comptable = subview.exercice_comptable
FROM
(
SELECT p_calendrier_mois.mois,
p_calendrier_mois.mois - correcteur_mois AS exercice_mois_comptable_1,
date_part('year',p_calendrier_mois.date_debut) - correcteur_exercice AS exercice_comptable
FROM
base.p_calendrier_mois
JOIN (
SELECT
p_calendrier_mois.mois,
to_number(to_char(D_DEB,'YYYYMM'),'FM999999') AS exercice_mois_comptable_1,
date_part('year',D_FIN) AS exercice_comptable,
p_calendrier_mois.numero_mois,
p_calendrier_mois.mois - to_number(to_char(D_DEB,'YYYYMM'),'FM999999') AS correcteur_mois,
date_part('year',p_calendrier_mois.date_debut) - date_part('year',D_FIN) AS correcteur_exercice
FROM base.p_calendrier_mois
JOIN prod_qsp.DOSSIER_COMPTABLE_EX ON p_calendrier_mois.date_debut BETWEEN date(D_DEB) AND date(D_FIN)
ORDER BY 1
) subview ON p_calendrier_mois.numero_mois = subview.numero_mois
WHERE p_calendrier_mois.exercice_mois_comptable_1 IS DISTINCT FROM p_calendrier_mois.mois - correcteur_mois OR
p_calendrier_mois.exercice_comptable IS DISTINCT FROM date_part('year',p_calendrier_mois.date_debut) - correcteur_exercice
GROUP BY 1,2,3
ORDER BY 1) subview
WHERE p_calendrier_mois.mois = subview.mois;
-- Site = Etablissement
INSERT INTO compta.t_sites(code, texte, clinique_honoraire, code_original)
SELECT N_ETAB, NOM, 'C', N_ETAB
FROM prod_QSP.CPTA_ETABLISSEMENT
WHERE N_ETAB NOT IN (SELECT code_original FROM compta.t_sites WHERE code_original IS NOT NULL)
;
-- Types de compte
INSERT INTO compta.t_types_compta(code, code_original, texte, specialite)
SELECT code, code_original, texte, specialite
FROM
(
SELECT 'GES' AS code, 'GES' AS code_original, 'Gestion' AS texte, 'GES' AS specialite
UNION
SELECT 'BIL', 'BIL', 'Bilan', 'BIL'
UNION
SELECT 'CLI', 'CLI', 'Clients', 'CLI'
UNION
SELECT 'FOU', 'FOU', 'Fournisseurs', 'FOU'
UNION
SELECT 'TIE', 'TIE', 'Autres tiers', 'CLI'
) subview
WHERE code_original NOT IN (SELECT code_original FROM compta.t_types_compta WHERE code_original IS NOT NULL);
INSERT INTO compta.t_sections_analytiques(oid, code, code_original, texte)
SELECT 0, '0000', '', 'Non renseignée'
WHERE 0 NOT IN (SELECT oid FROM compta.t_sections_analytiques);
-- Partenaires
INSERT INTO compta.t_partenaires(oid, code, code_original, texte, texte_court)
SELECT 0, chr(127) || '***', '', 'Non renseigné', 'Non renseigné'
WHERE 0 NOT IN (SELECT oid FROM compta.t_partenaires);
INSERT INTO compta.t_partenaires(code, code_original, texte, texte_court)
SELECT NUM_TIERS, NUM_TIERS, MAX(LIB_CPT), MAX(LIB_CPT)
FROM w_PLAN_C
WHERE TP_CPT <> 'G' AND
NUM_TIERS <> '' AND
NUM_TIERS NOT IN (SELECT code_original FROM compta.t_partenaires WHERE code_original IS NOT NULL) AND
USED = 1
GROUP BY 1
ORDER BY 1;
-- Journaux
INSERT INTO compta.t_journaux(code, code_original, texte, a_nouveaux, clinique_honoraire)
SELECT C_JAL, C_JAL, LIB_JAL, '0', 'C'
FROM prod_QSP.JOURNAL
WHERE C_JAL NOT IN (SELECT code_original FROM compta.t_journaux WHERE code_original IS NOT NULL)
ORDER BY 1;
-- Comptes
UPDATE compta.t_comptes SET
type_compta_id = w_PLAN_c.type_compta_id,
texte = LIB_CPT
FROM w_PLAN_C
WHERE t_comptes.numero = N_CPT AND
(
t_comptes.type_compta_id IS DISTINCT FROM w_PLAN_c.type_compta_id OR
t_comptes.texte IS DISTINCT FROM LIB_CPT
);
INSERT INTO compta.t_comptes(
type_compta_id, numero, texte, compte_general_id, clinique_honoraire,
banque, collectif, type_compta_extra_id, numero_extra, partenaire_id)
SELECT
type_compta_id,
N_CPT AS numero,
MAX(LIB_CPT) AS texte,
0 AS compte_general_id,
'C' AS clinique_honoraire,
MAX(CASE WHEN N_CPT LIKE '512%' THEN '1' ELSE '0' END) AS banque,
'0' AS collectif,
0 AS type_compta_extra_id,
'' AS numero_extra,
0::bigint AS partenaire_id
FROM w_PLAN_C
WHERE USED = 1 AND
N_CPT NOT IN (SELECT numero FROM compta.t_comptes WHERE numero IS NOT NULL)
GROUP BY 1,2
ORDER BY 2;
-- Comptes généraux des extra comptable
UPDATE compta.t_comptes SET
compte_general_id = t_comptes_c.oid,
collectif = (CASE WHEN w_PLAN_C.TP_CPT <> 'G' THEN '0' ELSE '1' END)
FROM w_PLAN_C
JOIN compta.t_comptes t_comptes_c ON N_CPT_GENE = t_comptes_c.numero
WHERE N_CPT = t_comptes.numero AND
(
t_comptes.compte_general_id IS DISTINCT FROM t_comptes_c.oid OR
t_comptes.collectif IS DISTINCT FROM (CASE WHEN w_PLAN_C.TP_CPT <> 'G' THEN '0' ELSE '1' END)
);
-- Partenaire associé à l'extraé comptable
UPDATE compta.t_comptes
SET partenaire_id = t_partenaires.oid
FROM w_PLAN_C
JOIN compta.t_partenaires ON NUM_TIERS = t_partenaires.code_original
WHERE t_comptes.numero = w_PLAN_C.N_CPT AND
w_PLAN_C.TP_CPT <> 'G' AND
partenaire_id IS DISTINCT FROM t_partenaires.oid;
]]></sqlcmd>
</NODE>
</NODE>
</ROOT>