|
|
return: text
|
|
|
lang: plpgsql
|
|
|
parameters:
|
|
|
p0:
|
|
|
type: bigint
|
|
|
name: i_oid
|
|
|
src: |
|
|
|
DECLARE
|
|
|
result TEXT;
|
|
|
BEGIN
|
|
|
-- Paramètres
|
|
|
-- i_oid = oid ajustement ou 0 pour tout
|
|
|
|
|
|
-- Suppression écritures déjà générées
|
|
|
DELETE FROM compta.p_historique_ecritures WHERE ajustement = '1' AND (cle_originale = i_oid::text OR i_oid = 0);
|
|
|
|
|
|
-- Insertion des écritures 'simples'
|
|
|
INSERT INTO compta.p_historique_ecritures(
|
|
|
compte_id, date_ecriture, mois_comptable, montant_credit, montant_debit,
|
|
|
journal_id, section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, compte_extra_id, comptabilisee, fin_exercice,
|
|
|
ajustement, cle_originale, compte_contrepartie_id, texte, date_facture_fournisseur,
|
|
|
piece, dossier, date_echeance, journal_paiement_id, mode_paiement_id,
|
|
|
numero_cheque)
|
|
|
SELECT
|
|
|
COALESCE(t_comptes.oid, t_comptes_extra.compte_general_id, 0),
|
|
|
t_ecritures_ajustement.date_ecriture,
|
|
|
t_ecritures_ajustement.mois_comptable,
|
|
|
t_ecritures_ajustement.montant_credit,
|
|
|
t_ecritures_ajustement.montant_debit,
|
|
|
t_journaux.oid,
|
|
|
COALESCE(t_sections_analytiques.oid,0),
|
|
|
t_ecritures_ajustement.clinique_honoraire,
|
|
|
p_calendrier_mois.exercice_comptable,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.type_compta_id ELSE t_comptes_gen.type_compta_id END,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.oid ELSE COALESCE(t_comptes_extra.oid,0) END,
|
|
|
'0', '0', '1',
|
|
|
CASE WHEN t_ecritures_ajustement.ecriture_maitre_id = 0 THEN t_ecritures_ajustement.oid ELSE t_ecritures_ajustement.ecriture_maitre_id END::text,
|
|
|
0,
|
|
|
t_ecritures_ajustement.texte,
|
|
|
null, '', '', null, 0, 0, ''
|
|
|
FROM compta.t_ecritures_ajustement
|
|
|
JOIN base.p_calendrier_mois ON (t_ecritures_ajustement.mois_comptable = p_calendrier_mois.mois)
|
|
|
JOIN compta.t_journaux ON (t_ecritures_ajustement.journal_code = t_journaux.code)
|
|
|
LEFT JOIN compta.t_sections_analytiques ON (t_ecritures_ajustement.section_analytique_code = t_sections_analytiques.code)
|
|
|
LEFT JOIN compta.t_comptes ON (t_ecritures_ajustement.compte_numero = t_comptes.numero AND t_comptes.oid = t_comptes.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_extra ON (t_ecritures_ajustement.compte_numero = t_comptes_extra.numero AND t_comptes_extra.oid <> t_comptes_extra.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_gen ON (t_comptes_extra.compte_general_id = t_comptes_gen.oid)
|
|
|
WHERE t_ecritures_ajustement.est_import_data IS DISTINCT FROM '1' AND
|
|
|
(
|
|
|
t_ecritures_ajustement.oid = i_oid OR
|
|
|
t_ecritures_ajustement.ecriture_maitre_id = i_oid OR
|
|
|
i_oid = 0
|
|
|
);
|
|
|
|
|
|
-- Insertion des écritures 'simples' en report mois suivant
|
|
|
INSERT INTO compta.p_historique_ecritures(
|
|
|
compte_id, date_ecriture, mois_comptable, montant_credit, montant_debit,
|
|
|
journal_id, section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, compte_extra_id, comptabilisee, fin_exercice,
|
|
|
ajustement, cle_originale, compte_contrepartie_id, texte, date_facture_fournisseur,
|
|
|
piece, dossier, date_echeance, journal_paiement_id, mode_paiement_id,
|
|
|
numero_cheque)
|
|
|
SELECT
|
|
|
COALESCE(t_comptes.oid, t_comptes_extra.compte_general_id, 0),
|
|
|
t_ecritures_ajustement.date_ecriture,
|
|
|
CASE WHEN substr(t_ecritures_ajustement.mois_comptable, 5, 2) <> '12' THEN t_ecritures_ajustement.mois_comptable + 1 ELSE t_ecritures_ajustement.mois_comptable + 89 END,
|
|
|
t_ecritures_ajustement.montant_debit,
|
|
|
t_ecritures_ajustement.montant_credit,
|
|
|
t_journaux.oid,
|
|
|
COALESCE(t_sections_analytiques.oid,0),
|
|
|
t_ecritures_ajustement.clinique_honoraire,
|
|
|
p_calendrier_mois.exercice_comptable,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.type_compta_id ELSE t_comptes_gen.type_compta_id END,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.oid ELSE COALESCE(t_comptes_extra.oid,0) END,
|
|
|
'0', '0', '1',
|
|
|
CASE WHEN t_ecritures_ajustement.ecriture_maitre_id = 0 THEN t_ecritures_ajustement.oid ELSE t_ecritures_ajustement.ecriture_maitre_id END::text,
|
|
|
0,
|
|
|
'REPORT ' || t_ecritures_ajustement.texte,
|
|
|
null, '', '', null, 0, 0, ''
|
|
|
FROM compta.t_ecritures_ajustement
|
|
|
JOIN base.p_calendrier_mois ON (t_ecritures_ajustement.mois_comptable = p_calendrier_mois.mois)
|
|
|
JOIN compta.t_journaux ON (t_ecritures_ajustement.journal_code = t_journaux.code)
|
|
|
LEFT JOIN compta.t_sections_analytiques ON (t_ecritures_ajustement.section_analytique_code = t_sections_analytiques.code)
|
|
|
LEFT JOIN compta.t_comptes ON (t_ecritures_ajustement.compte_numero = t_comptes.numero AND t_comptes.oid = t_comptes.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_extra ON (t_ecritures_ajustement.compte_numero = t_comptes_extra.numero AND t_comptes_extra.oid <> t_comptes_extra.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_gen ON (t_comptes_extra.compte_general_id = t_comptes_gen.oid)
|
|
|
WHERE t_ecritures_ajustement.est_import_data IS DISTINCT FROM '1' AND
|
|
|
t_ecritures_ajustement.report_automatique_mois_suivant = '1' AND
|
|
|
(
|
|
|
t_ecritures_ajustement.oid = i_oid OR
|
|
|
t_ecritures_ajustement.ecriture_maitre_id = i_oid OR
|
|
|
i_oid = 0
|
|
|
);
|
|
|
-- Insertion des écritures en format Excel
|
|
|
IF NOT EXISTS (SELECT * FROM pg_class WHERE relnamespace = pg_my_temp_schema() AND relname = 'w_cti_gen_ajustements') THEN
|
|
|
CREATE TEMP TABLE w_cti_gen_ajustements(
|
|
|
oid bigint,
|
|
|
clinique_honoraire text,
|
|
|
report_automatique_mois_suivant text,
|
|
|
est_balance text,
|
|
|
texte_source text,
|
|
|
import_ligne text,
|
|
|
import_date text,
|
|
|
import_journal text,
|
|
|
import_compte text,
|
|
|
import_section text,
|
|
|
import_texte text,
|
|
|
import_debit text,
|
|
|
import_credit text,
|
|
|
date_ecriture date DEFAULT '20991231',
|
|
|
mois_comptable numeric DEFAULT 0,
|
|
|
exercice_comptable numeric DEFAULT 0,
|
|
|
montant_debit numeric default 0,
|
|
|
montant_credit numeric default 0);
|
|
|
END IF;
|
|
|
TRUNCATE w_cti_gen_ajustements;
|
|
|
INSERT INTO w_cti_gen_ajustements(oid, clinique_honoraire, report_automatique_mois_suivant, est_balance, texte_source, import_ligne)
|
|
|
SELECT
|
|
|
oid,
|
|
|
clinique_honoraire,
|
|
|
report_automatique_mois_suivant,
|
|
|
est_balance,
|
|
|
texte,
|
|
|
trim(import_ligne) AS import_ligne
|
|
|
FROM
|
|
|
(
|
|
|
SELECT oid, clinique_honoraire, report_automatique_mois_suivant, est_balance, texte, unnest(data_array) AS import_ligne
|
|
|
FROM
|
|
|
(
|
|
|
SELECT
|
|
|
t_ecritures_ajustement.oid, clinique_honoraire, report_automatique_mois_suivant, est_balance, texte, string_to_array(replace(import_data,E'\t','||'),E'\r') AS data_array
|
|
|
FROM compta.t_ecritures_ajustement
|
|
|
WHERE est_import_data = '1' AND
|
|
|
(
|
|
|
t_ecritures_ajustement.oid = i_oid OR
|
|
|
i_oid = 0
|
|
|
)
|
|
|
) subview
|
|
|
) subview
|
|
|
WHERE trim(import_ligne) <> '';
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
import_date = trim(split_part(import_ligne,'||',1)),
|
|
|
import_journal = upper(trim(split_part(import_ligne,'||',2))),
|
|
|
import_compte = trim(split_part(import_ligne,'||',3)),
|
|
|
import_section = trim(split_part(import_ligne,'||',4)),
|
|
|
import_texte = trim(split_part(import_ligne,'||',5)),
|
|
|
import_debit = trim(split_part(import_ligne,'||',6)),
|
|
|
import_credit = trim(split_part(import_ligne,'||',7))
|
|
|
;
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
date_ecriture = base.cti_to_date(import_date),
|
|
|
montant_debit = base.cti_to_number(translate(import_debit,',€? ','.')),
|
|
|
montant_credit = base.cti_to_number(translate(import_credit ,',€? ','.'));
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
mois_comptable = date_part('year',date_ecriture)*100+date_part('month',date_ecriture)
|
|
|
;
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
exercice_comptable = p_calendrier_mois.exercice_comptable
|
|
|
FROM base.p_calendrier_mois
|
|
|
WHERE p_calendrier_mois.mois = w_cti_gen_ajustements.mois_comptable
|
|
|
;
|
|
|
-- Si balance, traitement comptes en double
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
import_compte = CASE WHEN w_cti_gen_ajustements.CTID = keepCTID THEN w_cti_gen_ajustements.import_compte ELSE '' END,
|
|
|
montant_debit = subview.montant_debit,
|
|
|
montant_credit = subview.montant_credit
|
|
|
FROM
|
|
|
(
|
|
|
SELECT
|
|
|
mois_comptable,
|
|
|
clinique_honoraire,
|
|
|
import_compte,
|
|
|
SUM(montant_debit) AS montant_debit,
|
|
|
SUM(montant_credit) AS montant_credit,
|
|
|
MIN(CTID) AS keepCTID
|
|
|
FROM w_cti_gen_ajustements
|
|
|
WHERE est_balance IN ('M','X') AND
|
|
|
NOT (import_compte = '' OR import_compte < '1' OR import_compte >= '8')
|
|
|
GROUP BY 1,2,3
|
|
|
HAVING count(*) > 1
|
|
|
) subview
|
|
|
WHERE w_cti_gen_ajustements.mois_comptable = subview.mois_comptable AND
|
|
|
w_cti_gen_ajustements.import_compte = subview.import_compte AND
|
|
|
w_cti_gen_ajustements.clinique_honoraire = subview.clinique_honoraire
|
|
|
;
|
|
|
DELETE FROM w_cti_gen_ajustements
|
|
|
WHERE import_compte = '' OR import_compte < '1' OR import_compte >= '8' ;
|
|
|
|
|
|
-- Si import balances, génération des écarts
|
|
|
DROP TABLE IF EXISTS w_cti_gen_ajustements_synthese;
|
|
|
CREATE TEMP TABLE w_cti_gen_ajustements_synthese AS
|
|
|
SELECT oid,
|
|
|
mois_comptable,
|
|
|
mois_comptable AS mois_comptable_1,
|
|
|
0::numeric AS exercice_comptable,
|
|
|
clinique_honoraire,
|
|
|
substr(import_compte,1,1) AS classe,
|
|
|
MAX(import_journal) AS import_journal,
|
|
|
MAX(import_texte) AS import_texte,
|
|
|
MAX(date_ecriture) AS date_ecriture,
|
|
|
MAX(texte_source) AS texte_source,
|
|
|
MAX(report_automatique_mois_suivant) AS report_automatique_mois_suivant,
|
|
|
MAX(est_balance) AS est_balance
|
|
|
FROM w_cti_gen_ajustements
|
|
|
WHERE import_compte <> '' AND
|
|
|
est_balance IN ('M','X') AND
|
|
|
mois_comptable IS NOT NULL AND
|
|
|
substr(import_compte,1,1) IN ('1','2','3','4','5','6','7')
|
|
|
GROUP BY 1,2,3,4,5,6;
|
|
|
UPDATE w_cti_gen_ajustements_synthese SET
|
|
|
mois_comptable_1 = CASE WHEN est_balance = 'X' THEN p_calendrier_mois.exercice_mois_comptable_1 ELSE mois_comptable_1 END,
|
|
|
exercice_comptable = p_calendrier_mois.exercice_comptable
|
|
|
FROM base.p_calendrier_mois
|
|
|
WHERE mois = mois_comptable;
|
|
|
|
|
|
DROP TABLE IF EXISTS w_cti_gen_ajustements_deja;
|
|
|
CREATE TEMP TABLE w_cti_gen_ajustements_deja AS
|
|
|
SELECT w_cti_gen_ajustements_synthese.oid,
|
|
|
w_cti_gen_ajustements_synthese.mois_comptable,
|
|
|
w_cti_gen_ajustements_synthese.exercice_comptable,
|
|
|
w_cti_gen_ajustements_synthese.clinique_honoraire,
|
|
|
compte_nonsigne_numero AS numero,
|
|
|
SUM(montant_debit) AS montant_debit,
|
|
|
SUM(montant_credit) AS montant_credit,
|
|
|
SUM(montant_debit) - SUM(montant_credit) AS montant,
|
|
|
MAX(import_journal) AS import_journal,
|
|
|
MAX(import_texte) AS import_texte,
|
|
|
MAX(w_cti_gen_ajustements_synthese.date_ecriture) AS date_ecriture,
|
|
|
MAX(texte_source) AS texte_source,
|
|
|
MAX(report_automatique_mois_suivant) AS report_automatique_mois_suivant,
|
|
|
MAX(est_balance) AS est_balance
|
|
|
FROM compta.p_historique_ecritures
|
|
|
JOIN compta.t_comptes ON compte_id = t_comptes.oid
|
|
|
JOIN w_cti_gen_ajustements_synthese ON
|
|
|
substr(t_comptes.numero,1,1) = w_cti_gen_ajustements_synthese.classe AND
|
|
|
p_historique_ecritures.mois_comptable BETWEEN w_cti_gen_ajustements_synthese.mois_comptable_1 AND w_cti_gen_ajustements_synthese.mois_comptable AND
|
|
|
p_historique_ecritures.clinique_honoraire = w_cti_gen_ajustements_synthese.clinique_honoraire AND
|
|
|
p_historique_ecritures.is_budget = '0'
|
|
|
GROUP BY 1,2,3,4,5
|
|
|
ORDER BY 1,2,3,5,6
|
|
|
;
|
|
|
INSERT INTO w_cti_gen_ajustements(oid, clinique_honoraire, report_automatique_mois_suivant, est_balance, texte_source, import_journal, import_compte, import_texte, date_ecriture, exercice_comptable, mois_comptable, montant_debit, montant_credit)
|
|
|
SELECT w_cti_gen_ajustements_deja.oid,
|
|
|
w_cti_gen_ajustements_deja.clinique_honoraire,
|
|
|
w_cti_gen_ajustements_deja.report_automatique_mois_suivant,
|
|
|
w_cti_gen_ajustements_deja.est_balance,
|
|
|
w_cti_gen_ajustements_deja.texte_source,
|
|
|
w_cti_gen_ajustements_deja.import_journal,
|
|
|
w_cti_gen_ajustements_deja.numero,
|
|
|
w_cti_gen_ajustements_deja.import_texte,
|
|
|
w_cti_gen_ajustements_deja.date_ecriture,
|
|
|
w_cti_gen_ajustements_deja.exercice_comptable,
|
|
|
w_cti_gen_ajustements_deja.mois_comptable,
|
|
|
0,
|
|
|
0
|
|
|
FROM w_cti_gen_ajustements_deja
|
|
|
LEFT JOIN w_cti_gen_ajustements ON
|
|
|
w_cti_gen_ajustements_deja.mois_comptable = w_cti_gen_ajustements.mois_comptable AND
|
|
|
w_cti_gen_ajustements_deja.clinique_honoraire = w_cti_gen_ajustements.clinique_honoraire AND
|
|
|
w_cti_gen_ajustements_deja.numero = w_cti_gen_ajustements.import_compte
|
|
|
WHERE w_cti_gen_ajustements.import_compte IS NULL
|
|
|
;
|
|
|
UPDATE w_cti_gen_ajustements SET
|
|
|
montant_debit = CASE WHEN montant_ajustement <= 0 THEN 0-montant_ajustement ELSE 0 END,
|
|
|
montant_credit = CASE WHEN montant_ajustement >= 0 THEN montant_ajustement ELSE 0 END
|
|
|
FROM
|
|
|
(
|
|
|
SELECT
|
|
|
w_cti_gen_ajustements_deja.mois_comptable,
|
|
|
w_cti_gen_ajustements_deja.clinique_honoraire,
|
|
|
numero,
|
|
|
w_cti_gen_ajustements_deja.montant - (w_cti_gen_ajustements.montant_debit - w_cti_gen_ajustements.montant_credit) AS montant_ajustement
|
|
|
FROM w_cti_gen_ajustements_deja
|
|
|
JOIN w_cti_gen_ajustements ON
|
|
|
w_cti_gen_ajustements_deja.mois_comptable = w_cti_gen_ajustements.mois_comptable AND
|
|
|
w_cti_gen_ajustements_deja.clinique_honoraire = w_cti_gen_ajustements.clinique_honoraire AND
|
|
|
w_cti_gen_ajustements_deja.numero = w_cti_gen_ajustements.import_compte
|
|
|
ORDER BY 1,2,3
|
|
|
) subview
|
|
|
WHERE subview.mois_comptable = w_cti_gen_ajustements.mois_comptable AND
|
|
|
subview.clinique_honoraire = w_cti_gen_ajustements.clinique_honoraire AND
|
|
|
subview.numero = w_cti_gen_ajustements.import_compte;
|
|
|
-- Génération des écritures
|
|
|
INSERT INTO compta.p_historique_ecritures(
|
|
|
compte_id, date_ecriture, mois_comptable, montant_credit, montant_debit,
|
|
|
journal_id, section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, compte_extra_id, comptabilisee, fin_exercice,
|
|
|
ajustement, cle_originale, compte_contrepartie_id, texte, date_facture_fournisseur,
|
|
|
piece, dossier, date_echeance, journal_paiement_id, mode_paiement_id,
|
|
|
numero_cheque)
|
|
|
SELECT
|
|
|
COALESCE(t_comptes.oid, t_comptes_extra.compte_general_id, 0),
|
|
|
w_cti_gen_ajustements.date_ecriture,
|
|
|
w_cti_gen_ajustements.mois_comptable,
|
|
|
w_cti_gen_ajustements.montant_credit,
|
|
|
w_cti_gen_ajustements.montant_debit,
|
|
|
t_journaux.oid,
|
|
|
COALESCE(t_sections_analytiques.oid,0),
|
|
|
w_cti_gen_ajustements.clinique_honoraire,
|
|
|
w_cti_gen_ajustements.exercice_comptable,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.type_compta_id ELSE t_comptes_gen.type_compta_id END,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.oid ELSE COALESCE(t_comptes_extra.oid,0) END,
|
|
|
'0', '0', '1',
|
|
|
w_cti_gen_ajustements.oid::text,
|
|
|
0,
|
|
|
w_cti_gen_ajustements.texte_source || CASE WHEN w_cti_gen_ajustements.import_texte <> '' THEN '- ' || w_cti_gen_ajustements.import_texte ELSE '' END,
|
|
|
null, '', '', null, 0, 0, ''
|
|
|
FROM w_cti_gen_ajustements
|
|
|
JOIN compta.t_journaux ON (w_cti_gen_ajustements.import_journal = t_journaux.code)
|
|
|
LEFT JOIN compta.t_sections_analytiques ON (w_cti_gen_ajustements.import_section = t_sections_analytiques.code)
|
|
|
LEFT JOIN compta.t_comptes ON (w_cti_gen_ajustements.import_compte = t_comptes.numero AND t_comptes.oid = t_comptes.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_extra ON (w_cti_gen_ajustements.import_compte = t_comptes_extra.numero AND t_comptes_extra.oid <> t_comptes_extra.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_gen ON (t_comptes_extra.compte_general_id = t_comptes_gen.oid)
|
|
|
WHERE import_compte <> '' AND
|
|
|
(w_cti_gen_ajustements.montant_debit <> 0 OR
|
|
|
w_cti_gen_ajustements.montant_credit <> 0)
|
|
|
;
|
|
|
-- Report mois suivant
|
|
|
INSERT INTO compta.p_historique_ecritures(
|
|
|
compte_id, date_ecriture, mois_comptable, montant_credit, montant_debit,
|
|
|
journal_id, section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, compte_extra_id, comptabilisee, fin_exercice,
|
|
|
ajustement, cle_originale, compte_contrepartie_id, texte, date_facture_fournisseur,
|
|
|
piece, dossier, date_echeance, journal_paiement_id, mode_paiement_id,
|
|
|
numero_cheque)
|
|
|
SELECT
|
|
|
COALESCE(t_comptes.oid, t_comptes_extra.compte_general_id, 0),
|
|
|
w_cti_gen_ajustements.date_ecriture,
|
|
|
CASE WHEN substr(w_cti_gen_ajustements.mois_comptable, 5, 2) <> '12' THEN w_cti_gen_ajustements.mois_comptable + 1 ELSE w_cti_gen_ajustements.mois_comptable + 89 END,
|
|
|
w_cti_gen_ajustements.montant_debit,
|
|
|
w_cti_gen_ajustements.montant_credit,
|
|
|
t_journaux.oid,
|
|
|
COALESCE(t_sections_analytiques.oid,0),
|
|
|
w_cti_gen_ajustements.clinique_honoraire,
|
|
|
w_cti_gen_ajustements.exercice_comptable,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.type_compta_id ELSE t_comptes_gen.type_compta_id END,
|
|
|
CASE WHEN t_comptes.oid IS NOT NULL THEN t_comptes.oid ELSE COALESCE(t_comptes_extra.oid,0) END,
|
|
|
'0', '0', '1',
|
|
|
w_cti_gen_ajustements.oid::text,
|
|
|
0,
|
|
|
'REPORT ' || w_cti_gen_ajustements.texte_source || CASE WHEN w_cti_gen_ajustements.import_texte <> '' THEN '- ' || w_cti_gen_ajustements.import_texte ELSE '' END,
|
|
|
null, '', '', null, 0, 0, ''
|
|
|
FROM w_cti_gen_ajustements
|
|
|
JOIN compta.t_journaux ON (w_cti_gen_ajustements.import_journal = t_journaux.code)
|
|
|
LEFT JOIN compta.t_sections_analytiques ON (w_cti_gen_ajustements.import_section = t_sections_analytiques.code)
|
|
|
LEFT JOIN compta.t_comptes ON (w_cti_gen_ajustements.import_compte = t_comptes.numero AND t_comptes.oid = t_comptes.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_extra ON (w_cti_gen_ajustements.import_compte = t_comptes_extra.numero AND t_comptes_extra.oid <> t_comptes_extra.compte_general_id)
|
|
|
LEFT JOIN compta.t_comptes t_comptes_gen ON (t_comptes_extra.compte_general_id = t_comptes_gen.oid)
|
|
|
WHERE import_compte <> '' AND
|
|
|
w_cti_gen_ajustements.report_automatique_mois_suivant = '1' AND
|
|
|
(w_cti_gen_ajustements.montant_debit <> 0 OR
|
|
|
w_cti_gen_ajustements.montant_credit <> 0)
|
|
|
;
|
|
|
UPDATE compta.p_historique_ecritures
|
|
|
SET site_id = coalesce((SELECT MIN(oid) FROM compta.t_sites WHERE oid <> 0), 0)
|
|
|
WHERE site_id IS NULL OR site_id = 0;
|
|
|
|
|
|
-- Regénération écritures 'total'
|
|
|
DELETE FROM compta.p_historique_ecritures_total WHERE ajustement = '1' ;
|
|
|
INSERT INTO compta.p_historique_ecritures_total(
|
|
|
compte_id, mois_comptable, montant_credit, montant_debit, journal_id,
|
|
|
section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, site_id, compte_extra_id, partenaire_id, comptabilisee, fin_exercice,
|
|
|
ajustement, inter_site, ecriture_consolidee, nombre_ecritures)
|
|
|
SELECT
|
|
|
compte_id, mois_comptable, sum(montant_credit), sum(montant_debit), journal_id,
|
|
|
section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, site_id, compte_extra_id, partenaire_id, comptabilisee, fin_exercice,
|
|
|
ajustement, inter_site, ecriture_consolidee, count(*)
|
|
|
FROM compta.p_historique_ecritures
|
|
|
WHERE ajustement = '1'
|
|
|
GROUP BY
|
|
|
compte_id, mois_comptable, journal_id,
|
|
|
section_analytique_id, clinique_honoraire, exercice_comptable,
|
|
|
type_compta_id, site_id, compte_extra_id, partenaire_id, comptabilisee, fin_exercice,
|
|
|
ajustement, inter_site, ecriture_consolidee;
|
|
|
-- Report des cumuls sur données importées excel
|
|
|
UPDATE compta.t_ecritures_ajustement SET
|
|
|
journal_code = subview.journal_code,
|
|
|
mois_comptable = subview.mois_comptable,
|
|
|
montant_debit = subview.montant_debit,
|
|
|
montant_credit = subview.montant_credit
|
|
|
FROM
|
|
|
(
|
|
|
SELECT
|
|
|
cle_originale,
|
|
|
base.cti_group_concat(DISTINCT t_journaux.code) AS journal_code,
|
|
|
SUM(montant_debit) AS montant_debit,
|
|
|
SUM(montant_credit) AS montant_credit,
|
|
|
MAX(CASE WHEN p_historique_ecritures.texte NOT LIKE 'REPORT %' THEN mois_comptable ELSE 0 END) AS mois_comptable
|
|
|
FROM compta.p_historique_ecritures
|
|
|
JOIN compta.t_journaux ON journal_id = t_journaux.oid
|
|
|
WHERE ajustement = '1'
|
|
|
GROUP BY 1
|
|
|
) subview
|
|
|
WHERE t_ecritures_ajustement.oid = subview.cle_originale AND
|
|
|
t_ecritures_ajustement.est_import_data = '1';
|
|
|
|
|
|
RETURN 'OK';
|
|
|
END;
|