diff --git a/html/install.php b/html/install.php
index 1bb7ed670..f30d2a8a2 100644
--- a/html/install.php
+++ b/html/install.php
@@ -645,9 +645,9 @@ if ($account == 0 ) {
$cn->start();
if ( DEBUGNOALYSS == 0 ) { ob_start(); }
- $cn->execute_script(NOALYSS_INCLUDE.'/sql/mod1/schema.sql');
+ $cn->execute_script(NOALYSS_INCLUDE.'/sql/mod2/schema.sql');
$cn->execute_script(NOALYSS_INCLUDE.'/sql/mod2/data.sql');
- $cn->execute_script(NOALYSS_INCLUDE.'/sql/mod1/constraint.sql');
+ $cn->execute_script(NOALYSS_INCLUDE.'/sql/mod2/constraint.sql');
$cn->commit();
if ( DEBUGNOALYSS == 0 ) ob_end_clean();
diff --git a/include/sql/mod2/constraint.sql b/include/sql/mod2/constraint.sql
new file mode 100644
index 000000000..3ba039beb
--- /dev/null
+++ b/include/sql/mod2/constraint.sql
@@ -0,0 +1,1657 @@
+
+
+
+ALTER TABLE ONLY public.action_comment_document
+ ADD CONSTRAINT action_comment_document_pkey PRIMARY KEY (acd_id);
+
+
+
+ALTER TABLE ONLY public.action_comment_document
+ ADD CONSTRAINT action_comment_document_un UNIQUE (document_id, action_gestion_comment_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion_operation
+ ADD CONSTRAINT action_comment_operation_pkey PRIMARY KEY (ago_id);
+
+
+
+ALTER TABLE ONLY public.action_detail
+ ADD CONSTRAINT action_detail_pkey PRIMARY KEY (ad_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion_comment
+ ADD CONSTRAINT action_gestion_comment_pkey PRIMARY KEY (agc_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion
+ ADD CONSTRAINT action_gestion_pkey PRIMARY KEY (ag_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion_related
+ ADD CONSTRAINT action_gestion_related_pkey PRIMARY KEY (aga_id);
+
+
+
+ALTER TABLE ONLY public.action_person_option
+ ADD CONSTRAINT action_person_option_pk PRIMARY KEY (ap_id);
+
+
+
+ALTER TABLE ONLY public.action_person
+ ADD CONSTRAINT action_person_pkey PRIMARY KEY (ap_id);
+
+
+
+ALTER TABLE ONLY public.action
+ ADD CONSTRAINT action_pkey PRIMARY KEY (ac_id);
+
+
+
+ALTER TABLE ONLY public.action_tags
+ ADD CONSTRAINT action_tags_pkey PRIMARY KEY (at_id);
+
+
+
+ALTER TABLE ONLY public.attr_def
+ ADD CONSTRAINT attr_def_pkey PRIMARY KEY (ad_id);
+
+
+
+ALTER TABLE ONLY public.bilan
+ ADD CONSTRAINT bilan_b_name_key UNIQUE (b_name);
+
+
+
+ALTER TABLE ONLY public.bilan
+ ADD CONSTRAINT bilan_pkey PRIMARY KEY (b_id);
+
+
+
+ALTER TABLE ONLY public.bookmark
+ ADD CONSTRAINT bookmark_pkey PRIMARY KEY (b_id);
+
+
+
+ALTER TABLE ONLY public.centralized
+ ADD CONSTRAINT centralized_pkey PRIMARY KEY (c_id);
+
+
+
+ALTER TABLE ONLY public.contact_option_ref
+ ADD CONSTRAINT contact_option_ref_pk PRIMARY KEY (cor_id);
+
+
+
+ALTER TABLE ONLY public.currency_history
+ ADD CONSTRAINT currency_history_pk PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.currency
+ ADD CONSTRAINT currency_pk PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.currency
+ ADD CONSTRAINT currency_un UNIQUE (cr_code_iso);
+
+
+
+ALTER TABLE ONLY public.del_action
+ ADD CONSTRAINT del_action_pkey PRIMARY KEY (del_id);
+
+
+
+ALTER TABLE ONLY public.del_jrn
+ ADD CONSTRAINT dj_id PRIMARY KEY (dj_id);
+
+
+
+ALTER TABLE ONLY public.del_jrnx
+ ADD CONSTRAINT djx_id PRIMARY KEY (djx_id);
+
+
+
+ALTER TABLE ONLY public.document_component
+ ADD CONSTRAINT document_component_pk PRIMARY KEY (dc_id);
+
+
+
+ALTER TABLE ONLY public.document_component
+ ADD CONSTRAINT document_component_un UNIQUE (dc_code);
+
+
+
+ALTER TABLE ONLY public.document_modele
+ ADD CONSTRAINT document_modele_pkey PRIMARY KEY (md_id);
+
+
+
+ALTER TABLE ONLY public.document_option
+ ADD CONSTRAINT document_option_ref_pk PRIMARY KEY (do_id);
+
+
+
+ALTER TABLE ONLY public.document_option
+ ADD CONSTRAINT document_option_un UNIQUE (do_code, document_type_id);
+
+
+
+ALTER TABLE ONLY public.document
+ ADD CONSTRAINT document_pkey PRIMARY KEY (d_id);
+
+
+
+ALTER TABLE ONLY public.document_state
+ ADD CONSTRAINT document_state_pkey PRIMARY KEY (s_id);
+
+
+
+ALTER TABLE ONLY public.document_type
+ ADD CONSTRAINT document_type_pkey PRIMARY KEY (dt_id);
+
+
+
+ALTER TABLE ONLY public.fiche_def
+ ADD CONSTRAINT fiche_def_pkey PRIMARY KEY (fd_id);
+
+
+
+ALTER TABLE ONLY public.fiche_def_ref
+ ADD CONSTRAINT fiche_def_ref_pkey PRIMARY KEY (frd_id);
+
+
+
+ALTER TABLE ONLY public.fiche
+ ADD CONSTRAINT fiche_pkey PRIMARY KEY (f_id);
+
+
+
+ALTER TABLE ONLY public.forecast_category
+ ADD CONSTRAINT forecast_cat_pk PRIMARY KEY (fc_id);
+
+
+
+ALTER TABLE ONLY public.forecast_item
+ ADD CONSTRAINT forecast_item_pkey PRIMARY KEY (fi_id);
+
+
+
+ALTER TABLE ONLY public.forecast
+ ADD CONSTRAINT forecast_pk PRIMARY KEY (f_id);
+
+
+
+ALTER TABLE ONLY public.form_detail
+ ADD CONSTRAINT form_pkey PRIMARY KEY (fo_id);
+
+
+
+ALTER TABLE ONLY public.form_definition
+ ADD CONSTRAINT formdef_pkey PRIMARY KEY (fr_id);
+
+
+
+ALTER TABLE ONLY public.attr_min
+ ADD CONSTRAINT frd_ad_attr_min_pk PRIMARY KEY (frd_id, ad_id);
+
+
+
+ALTER TABLE ONLY public.operation_analytique
+ ADD CONSTRAINT historique_analytique_pkey PRIMARY KEY (oa_id);
+
+
+
+ALTER TABLE ONLY public.tmp_pcmn
+ ADD CONSTRAINT id_ux UNIQUE (id);
+
+
+
+ALTER TABLE ONLY public.extension
+ ADD CONSTRAINT idx_ex_code UNIQUE (ex_code);
+
+
+
+ALTER TABLE ONLY public.info_def
+ ADD CONSTRAINT info_def_pkey PRIMARY KEY (id_type);
+
+
+
+ALTER TABLE ONLY public.jnt_document_option_contact
+ ADD CONSTRAINT jnt_document_option_contact_pkey PRIMARY KEY (jdoc_id);
+
+
+
+ALTER TABLE ONLY public.jnt_document_option_contact
+ ADD CONSTRAINT jnt_document_option_contact_un UNIQUE (document_type_id, contact_option_ref_id);
+
+
+
+ALTER TABLE ONLY public.fiche_detail
+ ADD CONSTRAINT jnt_fic_att_value_pkey PRIMARY KEY (jft_id);
+
+
+
+ALTER TABLE ONLY public.jnt_letter
+ ADD CONSTRAINT jnt_letter_pk PRIMARY KEY (jl_id);
+
+
+
+ALTER TABLE ONLY public.jnt_tag_group_tag
+ ADD CONSTRAINT jnt_tag_group_tag_pkey PRIMARY KEY (jt_id);
+
+
+
+ALTER TABLE ONLY public.jnt_tag_group_tag
+ ADD CONSTRAINT jnt_tag_group_tag_un UNIQUE (tag_id, tag_group_id);
+
+
+
+ALTER TABLE ONLY public.jrn_def
+ ADD CONSTRAINT jrn_def_jrn_def_name_key UNIQUE (jrn_def_name);
+
+
+
+ALTER TABLE ONLY public.jrn_def
+ ADD CONSTRAINT jrn_def_pkey PRIMARY KEY (jrn_def_id);
+
+
+
+ALTER TABLE ONLY public.jrn_info
+ ADD CONSTRAINT jrn_info_pkey PRIMARY KEY (ji_id);
+
+
+
+ALTER TABLE ONLY public.jrn_periode
+ ADD CONSTRAINT jrn_periode_periode_ledger UNIQUE (jrn_def_id, p_id);
+
+
+
+ALTER TABLE ONLY public.jrn_periode
+ ADD CONSTRAINT jrn_periode_pk PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.jrn
+ ADD CONSTRAINT jrn_pkey PRIMARY KEY (jr_id);
+
+
+
+ALTER TABLE ONLY public.jrn_rapt
+ ADD CONSTRAINT jrn_rapt_pkey PRIMARY KEY (jra_id);
+
+
+
+ALTER TABLE ONLY public.jrn_tax
+ ADD CONSTRAINT jrn_tax_pk PRIMARY KEY (jt_id);
+
+
+
+ALTER TABLE ONLY public.jrn_type
+ ADD CONSTRAINT jrn_type_pkey PRIMARY KEY (jrn_type_id);
+
+
+
+ALTER TABLE ONLY public.jrn_note
+ ADD CONSTRAINT jrnx_note_pkey PRIMARY KEY (n_id);
+
+
+
+ALTER TABLE ONLY public.jrnx
+ ADD CONSTRAINT jrnx_pkey PRIMARY KEY (j_id);
+
+
+
+ALTER TABLE ONLY public.key_distribution_activity
+ ADD CONSTRAINT key_distribution_activity_pkey PRIMARY KEY (ka_id);
+
+
+
+ALTER TABLE ONLY public.key_distribution_detail
+ ADD CONSTRAINT key_distribution_detail_pkey PRIMARY KEY (ke_id);
+
+
+
+ALTER TABLE ONLY public.key_distribution_ledger
+ ADD CONSTRAINT key_distribution_ledger_pkey PRIMARY KEY (kl_id);
+
+
+
+ALTER TABLE ONLY public.key_distribution
+ ADD CONSTRAINT key_distribution_pkey PRIMARY KEY (kd_id);
+
+
+
+ALTER TABLE ONLY public.letter_cred
+ ADD CONSTRAINT letter_cred_j_id_key UNIQUE (j_id);
+
+
+
+ALTER TABLE ONLY public.letter_cred
+ ADD CONSTRAINT letter_cred_pk PRIMARY KEY (lc_id);
+
+
+
+ALTER TABLE ONLY public.letter_deb
+ ADD CONSTRAINT letter_deb_j_id_key UNIQUE (j_id);
+
+
+
+ALTER TABLE ONLY public.letter_deb
+ ADD CONSTRAINT letter_deb_pk PRIMARY KEY (ld_id);
+
+
+
+ALTER TABLE ONLY public.link_action_type
+ ADD CONSTRAINT link_action_type_pkey PRIMARY KEY (l_id);
+
+
+
+ALTER TABLE ONLY public.menu_default
+ ADD CONSTRAINT menu_default_md_code_key UNIQUE (md_code);
+
+
+
+ALTER TABLE ONLY public.menu_default
+ ADD CONSTRAINT menu_default_pkey PRIMARY KEY (md_id);
+
+
+
+ALTER TABLE ONLY public.menu_ref
+ ADD CONSTRAINT menu_ref_pkey PRIMARY KEY (me_code);
+
+
+
+ALTER TABLE ONLY public.payment_method
+ ADD CONSTRAINT mod_payment_pkey PRIMARY KEY (mp_id);
+
+
+
+ALTER TABLE ONLY public.op_predef
+ ADD CONSTRAINT op_def_op_name_key UNIQUE (od_name, jrn_def_id);
+
+
+
+ALTER TABLE ONLY public.op_predef
+ ADD CONSTRAINT op_def_pkey PRIMARY KEY (od_id);
+
+
+
+ALTER TABLE ONLY public.op_predef_detail
+ ADD CONSTRAINT op_predef_detail_pkey PRIMARY KEY (opd_id);
+
+
+
+ALTER TABLE ONLY public.operation_currency
+ ADD CONSTRAINT operation_currency_pk PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.operation_exercice_detail
+ ADD CONSTRAINT operation_exercice_detail_pkey PRIMARY KEY (oed_id);
+
+
+
+ALTER TABLE ONLY public.operation_exercice
+ ADD CONSTRAINT operation_exercice_pkey PRIMARY KEY (oe_id);
+
+
+
+ALTER TABLE ONLY public.operation_tag
+ ADD CONSTRAINT operation_tag_pkey PRIMARY KEY (opt_id);
+
+
+
+ALTER TABLE ONLY public.parameter_extra
+ ADD CONSTRAINT parameter_extra_pkey PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.parameter
+ ADD CONSTRAINT parameter_pkey PRIMARY KEY (pr_id);
+
+
+
+ALTER TABLE ONLY public.parm_appearance
+ ADD CONSTRAINT parm_appearance_pkey PRIMARY KEY (a_code);
+
+
+
+ALTER TABLE ONLY public.parm_code
+ ADD CONSTRAINT parm_code_pkey PRIMARY KEY (p_code);
+
+
+
+ALTER TABLE ONLY public.parm_money
+ ADD CONSTRAINT parm_money_pkey PRIMARY KEY (pm_code);
+
+
+
+ALTER TABLE ONLY public.parm_periode
+ ADD CONSTRAINT parm_periode_pkey PRIMARY KEY (p_id);
+
+
+
+ALTER TABLE ONLY public.parm_poste
+ ADD CONSTRAINT parm_poste_pkey PRIMARY KEY (p_value);
+
+
+
+ALTER TABLE ONLY public.parameter_extra
+ ADD CONSTRAINT pe_code_ux UNIQUE (pe_code);
+
+
+
+ALTER TABLE ONLY public.extension
+ ADD CONSTRAINT pk_extension PRIMARY KEY (ex_id);
+
+
+
+ALTER TABLE ONLY public.groupe_analytique
+ ADD CONSTRAINT pk_ga_id PRIMARY KEY (ga_id);
+
+
+
+ALTER TABLE ONLY public.jnt_fic_attr
+ ADD CONSTRAINT pk_jnt_fic_attr PRIMARY KEY (jnt_id);
+
+
+
+ALTER TABLE ONLY public.user_local_pref
+ ADD CONSTRAINT pk_user_local_pref PRIMARY KEY (user_id, parameter_type);
+
+
+
+ALTER TABLE ONLY public.plan_analytique
+ ADD CONSTRAINT plan_analytique_pa_name_key UNIQUE (pa_name);
+
+
+
+ALTER TABLE ONLY public.plan_analytique
+ ADD CONSTRAINT plan_analytique_pkey PRIMARY KEY (pa_id);
+
+
+
+ALTER TABLE ONLY public.poste_analytique
+ ADD CONSTRAINT poste_analytique_pkey PRIMARY KEY (po_id);
+
+
+
+ALTER TABLE ONLY public.profile_menu
+ ADD CONSTRAINT profile_menu_pkey PRIMARY KEY (pm_id);
+
+
+
+ALTER TABLE ONLY public.profile_menu_type
+ ADD CONSTRAINT profile_menu_type_pkey PRIMARY KEY (pm_type);
+
+
+
+ALTER TABLE ONLY public.profile_mobile
+ ADD CONSTRAINT profile_mobile_code_uq UNIQUE (p_id, me_code);
+
+
+
+ALTER TABLE ONLY public.profile_mobile
+ ADD CONSTRAINT profile_mobile_pkey PRIMARY KEY (pmo_id);
+
+
+
+ALTER TABLE ONLY public.profile
+ ADD CONSTRAINT profile_pkey PRIMARY KEY (p_id);
+
+
+
+ALTER TABLE ONLY public.profile_sec_repository
+ ADD CONSTRAINT profile_sec_repository_pkey PRIMARY KEY (ur_id);
+
+
+
+ALTER TABLE ONLY public.profile_sec_repository
+ ADD CONSTRAINT profile_sec_repository_r_id_p_id_u UNIQUE (r_id, p_id);
+
+
+
+ALTER TABLE ONLY public.profile_user
+ ADD CONSTRAINT profile_user_pkey PRIMARY KEY (pu_id);
+
+
+
+ALTER TABLE ONLY public.profile_user
+ ADD CONSTRAINT profile_user_user_name_key UNIQUE (user_name, p_id);
+
+
+
+ALTER TABLE ONLY public.quant_purchase
+ ADD CONSTRAINT qp_id_pk PRIMARY KEY (qp_id);
+
+
+
+ALTER TABLE ONLY public.quant_sold
+ ADD CONSTRAINT qs_id_pk PRIMARY KEY (qs_id);
+
+
+
+ALTER TABLE ONLY public.quant_fin
+ ADD CONSTRAINT quant_fin_pk PRIMARY KEY (qf_id);
+
+
+
+ALTER TABLE ONLY public.stock_change
+ ADD CONSTRAINT stock_change_pkey PRIMARY KEY (c_id);
+
+
+
+ALTER TABLE ONLY public.stock_goods
+ ADD CONSTRAINT stock_goods_pkey PRIMARY KEY (sg_id);
+
+
+
+ALTER TABLE ONLY public.stock_repository
+ ADD CONSTRAINT stock_repository_pkey PRIMARY KEY (r_id);
+
+
+
+ALTER TABLE ONLY public.tag_group
+ ADD CONSTRAINT tag_group_pk PRIMARY KEY (tg_id);
+
+
+
+ALTER TABLE ONLY public.operation_tag
+ ADD CONSTRAINT tag_operation_uq UNIQUE (jrn_id, tag_id);
+
+
+
+ALTER TABLE ONLY public.tags
+ ADD CONSTRAINT tags_pkey PRIMARY KEY (t_id);
+
+
+
+ALTER TABLE ONLY public.tmp_pcmn
+ ADD CONSTRAINT tmp_pcmn_pkey PRIMARY KEY (pcm_val);
+
+
+
+ALTER TABLE ONLY public.tmp_stockgood_detail
+ ADD CONSTRAINT tmp_stockgood_detail_pkey PRIMARY KEY (d_id);
+
+
+
+ALTER TABLE ONLY public.tmp_stockgood
+ ADD CONSTRAINT tmp_stockgood_pkey PRIMARY KEY (s_id);
+
+
+
+ALTER TABLE ONLY public.todo_list
+ ADD CONSTRAINT todo_list_pkey PRIMARY KEY (tl_id);
+
+
+
+ALTER TABLE ONLY public.todo_list_shared
+ ADD CONSTRAINT todo_list_shared_pkey PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.tool_uos
+ ADD CONSTRAINT tool_uos_pkey PRIMARY KEY (uos_value);
+
+
+
+ALTER TABLE ONLY public.tva_rate
+ ADD CONSTRAINT tva_code_unique UNIQUE (tva_code);
+
+
+
+ALTER TABLE ONLY public.tva_rate
+ ADD CONSTRAINT tva_id_pk PRIMARY KEY (tva_id);
+
+
+
+ALTER TABLE ONLY public.user_sec_jrn
+ ADD CONSTRAINT uniq_user_ledger UNIQUE (uj_login, uj_jrn_id);
+
+
+
+COMMENT ON CONSTRAINT uniq_user_ledger ON public.user_sec_jrn IS 'Create an unique combination user / ledger';
+
+
+
+ALTER TABLE ONLY public.todo_list_shared
+ ADD CONSTRAINT unique_todo_list_id_login UNIQUE (todo_list_id, use_login);
+
+
+
+ALTER TABLE ONLY public.user_active_security
+ ADD CONSTRAINT user_active_security_pk PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.user_filter
+ ADD CONSTRAINT user_filter_pkey PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY public.user_sec_act
+ ADD CONSTRAINT user_sec_act_pkey PRIMARY KEY (ua_id);
+
+
+
+ALTER TABLE ONLY public.user_sec_action_profile
+ ADD CONSTRAINT user_sec_action_profile_p_id_p_granted_u UNIQUE (p_id, p_granted);
+
+
+
+ALTER TABLE ONLY public.user_sec_action_profile
+ ADD CONSTRAINT user_sec_action_profile_pkey PRIMARY KEY (ua_id);
+
+
+
+ALTER TABLE ONLY public.user_sec_jrn
+ ADD CONSTRAINT user_sec_jrn_pkey PRIMARY KEY (uj_id);
+
+
+
+ALTER TABLE ONLY public.user_widget
+ ADD CONSTRAINT user_widget_pkey PRIMARY KEY (uw_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion_related
+ ADD CONSTRAINT ux_aga_least_aga_greatest UNIQUE (aga_least, aga_greatest);
+
+
+
+ALTER TABLE ONLY public.jrn
+ ADD CONSTRAINT ux_internal UNIQUE (jr_internal);
+
+
+
+ALTER TABLE ONLY public.version
+ ADD CONSTRAINT version_pkey PRIMARY KEY (val);
+
+
+
+ALTER TABLE ONLY public.widget_dashboard
+ ADD CONSTRAINT widget_dashboard_pkey PRIMARY KEY (wd_id);
+
+
+
+ALTER TABLE ONLY public.widget_dashboard
+ ADD CONSTRAINT widget_dashboard_unique UNIQUE (wd_code);
+
+
+
+ALTER TABLE ONLY tva_belge.assujetti_chld
+ ADD CONSTRAINT assujetti_chld_pk PRIMARY KEY (ac_id);
+
+
+
+ALTER TABLE ONLY tva_belge.assujetti
+ ADD CONSTRAINT assujetti_pk PRIMARY KEY (a_id);
+
+
+
+ALTER TABLE ONLY tva_belge.declarant
+ ADD CONSTRAINT declarant_pkey PRIMARY KEY (d_id);
+
+
+
+ALTER TABLE ONLY tva_belge.declaration_amount
+ ADD CONSTRAINT declaration_amount_pkey PRIMARY KEY (da_id);
+
+
+
+ALTER TABLE ONLY tva_belge.form_detail
+ ADD CONSTRAINT form_detail_pkey PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY tva_belge.form
+ ADD CONSTRAINT form_pkey PRIMARY KEY (id);
+
+
+
+ALTER TABLE ONLY tva_belge.intracomm_chld
+ ADD CONSTRAINT intracom_chld_pk PRIMARY KEY (ic_id);
+
+
+
+ALTER TABLE ONLY tva_belge.intracomm
+ ADD CONSTRAINT intracom_pk PRIMARY KEY (i_id);
+
+
+
+ALTER TABLE ONLY tva_belge.parameter_chld
+ ADD CONSTRAINT parameter_chld_pkey PRIMARY KEY (pi_id);
+
+
+
+ALTER TABLE ONLY tva_belge.parameter
+ ADD CONSTRAINT parameter_pkey PRIMARY KEY (pcode);
+
+
+
+ALTER TABLE ONLY tva_belge.representative
+ ADD CONSTRAINT representative_pkey PRIMARY KEY (rp_id);
+
+
+
+ALTER TABLE ONLY tva_belge.version
+ ADD CONSTRAINT version_pkey PRIMARY KEY (id);
+
+
+
+CREATE UNIQUE INDEX fd_id_ad_id_x ON public.jnt_fic_attr USING btree (fd_id, ad_id);
+
+
+
+CREATE INDEX fiche_detail_attr_ix ON public.fiche_detail USING btree (ad_id);
+
+
+
+CREATE UNIQUE INDEX fiche_detail_f_id_ad_id ON public.fiche_detail USING btree (f_id, ad_id);
+
+
+
+CREATE INDEX fk_action_person_action_gestion ON public.action_person USING btree (ag_id);
+
+
+
+CREATE INDEX fk_action_person_fiche ON public.action_person USING btree (f_id);
+
+
+
+CREATE INDEX fk_stock_good_repository_r_id ON public.stock_goods USING btree (r_id);
+
+
+
+CREATE INDEX fk_stock_goods_f_id ON public.stock_goods USING btree (f_id);
+
+
+
+CREATE INDEX fk_stock_goods_j_id ON public.stock_goods USING btree (j_id);
+
+
+
+CREATE INDEX fki_f_end_date ON public.forecast USING btree (f_end_date);
+
+
+
+CREATE INDEX fki_f_start_date ON public.forecast USING btree (f_start_date);
+
+
+
+CREATE INDEX fki_jrn_jr_grpt_id ON public.jrn USING btree (jr_grpt_id);
+
+
+
+CREATE INDEX fki_jrnx_f_id ON public.jrnx USING btree (f_id);
+
+
+
+CREATE INDEX fki_jrnx_j_grpt ON public.jrnx USING btree (j_grpt);
+
+
+
+CREATE INDEX fki_profile_menu_me_code ON public.profile_menu USING btree (me_code);
+
+
+
+CREATE INDEX fki_profile_menu_profile ON public.profile_menu USING btree (p_id);
+
+
+
+CREATE INDEX fki_profile_menu_type_fkey ON public.profile_menu USING btree (p_type_display);
+
+
+
+CREATE INDEX idx_qs_internal ON public.quant_sold USING btree (qs_internal);
+
+
+
+CREATE INDEX jnt_fic_att_value_fd_id_idx ON public.fiche_detail USING btree (f_id);
+
+
+
+CREATE INDEX jnt_fic_attr_fd_id_idx ON public.jnt_fic_attr USING btree (fd_id);
+
+
+
+CREATE INDEX jrnx_j_qcode_ix ON public.jrnx USING btree (j_qcode);
+
+
+
+CREATE UNIQUE INDEX k_ag_ref ON public.action_gestion USING btree (ag_ref);
+
+
+
+CREATE INDEX link_action_type_fki ON public.action_gestion_related USING btree (aga_type);
+
+
+
+CREATE UNIQUE INDEX qcode_idx ON public.fiche_detail USING btree (ad_value) WHERE (ad_id = 23);
+
+
+
+CREATE UNIQUE INDEX qf_jr_id ON public.quant_fin USING btree (jr_id);
+
+
+
+CREATE UNIQUE INDEX qp_j_id ON public.quant_purchase USING btree (j_id);
+
+
+
+CREATE UNIQUE INDEX qs_j_id ON public.quant_sold USING btree (j_id);
+
+
+
+CREATE INDEX quant_purchase_jrn_fki ON public.quant_purchase USING btree (qp_internal);
+
+
+
+CREATE INDEX quant_sold_jrn_fki ON public.quant_sold USING btree (qs_internal);
+
+
+
+CREATE UNIQUE INDEX uj_login_uj_jrn_id ON public.user_sec_jrn USING btree (uj_login, uj_jrn_id);
+
+
+
+CREATE UNIQUE INDEX ux_po_name ON public.poste_analytique USING btree (po_name);
+
+
+
+CREATE UNIQUE INDEX x_jrn_jr_id ON public.jrn USING btree (jr_id);
+
+
+
+CREATE INDEX x_mt ON public.jrn USING btree (jr_mt);
+
+
+
+CREATE UNIQUE INDEX x_periode ON public.parm_periode USING btree (p_start, p_end);
+
+
+
+CREATE INDEX x_poste ON public.jrnx USING btree (j_poste);
+
+
+
+CREATE TRIGGER action_gestion_t_insert_update BEFORE INSERT OR UPDATE ON public.action_gestion FOR EACH ROW EXECUTE FUNCTION comptaproc.action_gestion_ins_upd();
+
+
+
+COMMENT ON TRIGGER action_gestion_t_insert_update ON public.action_gestion IS 'Truncate the column ag_title to 70 char';
+
+
+
+CREATE TRIGGER document_modele_validate BEFORE INSERT OR UPDATE ON public.document_modele FOR EACH ROW EXECUTE FUNCTION comptaproc.t_document_modele_validate();
+
+
+
+CREATE TRIGGER document_validate BEFORE INSERT OR UPDATE ON public.document FOR EACH ROW EXECUTE FUNCTION comptaproc.t_document_validate();
+
+
+
+CREATE TRIGGER fiche_def_ins_upd BEFORE INSERT OR UPDATE ON public.fiche_def FOR EACH ROW EXECUTE FUNCTION comptaproc.fiche_def_ins_upd();
+
+
+
+CREATE TRIGGER fiche_detail_check_qcode_trg BEFORE INSERT OR UPDATE ON public.fiche_detail FOR EACH ROW EXECUTE FUNCTION comptaproc.fiche_detail_check_qcode();
+
+
+
+CREATE TRIGGER fiche_detail_check_trg BEFORE INSERT OR UPDATE ON public.fiche_detail FOR EACH ROW EXECUTE FUNCTION comptaproc.fiche_detail_check();
+
+
+
+CREATE TRIGGER info_def_ins_upd_t BEFORE INSERT OR UPDATE ON public.info_def FOR EACH ROW EXECUTE FUNCTION comptaproc.info_def_ins_upd();
+
+
+
+CREATE TRIGGER jrn_def_description_ins_upd BEFORE INSERT OR UPDATE ON public.jrn_def FOR EACH ROW EXECUTE FUNCTION comptaproc.t_jrn_def_description();
+
+
+
+CREATE TRIGGER opd_limit_description BEFORE INSERT OR UPDATE ON public.op_predef FOR EACH ROW EXECUTE FUNCTION comptaproc.opd_limit_description();
+
+
+
+CREATE TRIGGER parm_periode_check_periode_trg BEFORE INSERT OR UPDATE ON public.parm_periode FOR EACH ROW EXECUTE FUNCTION comptaproc.check_periode();
+
+
+
+CREATE TRIGGER profile_user_ins_upd BEFORE INSERT OR UPDATE ON public.profile_user FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_profile_user_ins_upd();
+
+
+
+COMMENT ON TRIGGER profile_user_ins_upd ON public.profile_user IS 'Force the column user_name to lowercase';
+
+
+
+CREATE TRIGGER quant_sold_ins_upd_tr AFTER INSERT OR UPDATE ON public.quant_purchase FOR EACH ROW EXECUTE FUNCTION comptaproc.quant_purchase_ins_upd();
+
+
+
+CREATE TRIGGER quant_sold_ins_upd_tr AFTER INSERT OR UPDATE ON public.quant_sold FOR EACH ROW EXECUTE FUNCTION comptaproc.quant_sold_ins_upd();
+
+
+
+CREATE TRIGGER remove_action_gestion AFTER DELETE ON public.fiche FOR EACH ROW EXECUTE FUNCTION comptaproc.card_after_delete();
+
+
+
+CREATE TRIGGER t_check_balance AFTER INSERT OR UPDATE ON public.jrn FOR EACH ROW EXECUTE FUNCTION comptaproc.proc_check_balance();
+
+
+
+CREATE TRIGGER t_check_jrn BEFORE INSERT OR DELETE OR UPDATE ON public.jrn FOR EACH ROW EXECUTE FUNCTION comptaproc.jrn_check_periode();
+
+
+
+CREATE TRIGGER t_code BEFORE INSERT OR UPDATE ON public.document_component FOR EACH ROW EXECUTE FUNCTION comptaproc.four_upper_letter();
+
+
+
+CREATE TRIGGER t_group_analytic_del BEFORE DELETE ON public.groupe_analytique FOR EACH ROW EXECUTE FUNCTION comptaproc.group_analytique_del();
+
+
+
+CREATE TRIGGER t_group_analytic_ins_upd BEFORE INSERT OR UPDATE ON public.groupe_analytique FOR EACH ROW EXECUTE FUNCTION comptaproc.group_analytic_ins_upd();
+
+
+
+CREATE TRIGGER t_jnt_fic_attr_ins AFTER INSERT ON public.jnt_fic_attr FOR EACH ROW EXECUTE FUNCTION comptaproc.jnt_fic_attr_ins();
+
+
+
+CREATE TRIGGER t_jrn_currency BEFORE INSERT OR UPDATE ON public.jrn FOR EACH ROW EXECUTE FUNCTION comptaproc.jrn_currency();
+
+
+
+CREATE TRIGGER t_jrn_def_add_periode AFTER INSERT ON public.jrn_def FOR EACH ROW EXECUTE FUNCTION comptaproc.jrn_def_add();
+
+
+
+CREATE TRIGGER t_jrn_def_delete BEFORE DELETE ON public.jrn_def FOR EACH ROW EXECUTE FUNCTION comptaproc.jrn_def_delete();
+
+
+
+CREATE TRIGGER t_jrn_del BEFORE DELETE ON public.jrn FOR EACH ROW EXECUTE FUNCTION comptaproc.jrn_del();
+
+
+
+CREATE TRIGGER t_jrnx_del BEFORE DELETE ON public.jrnx FOR EACH ROW EXECUTE FUNCTION comptaproc.jrnx_del();
+
+
+
+CREATE TRIGGER t_jrnx_ins BEFORE INSERT ON public.jrnx FOR EACH ROW EXECUTE FUNCTION comptaproc.jrnx_ins();
+
+
+
+COMMENT ON TRIGGER t_jrnx_ins ON public.jrnx IS 'check that the qcode used by the card exists and format it : uppercase and trim the space';
+
+
+
+CREATE TRIGGER t_letter_del AFTER DELETE ON public.jrnx FOR EACH ROW EXECUTE FUNCTION comptaproc.jrnx_letter_del();
+
+
+
+COMMENT ON TRIGGER t_letter_del ON public.jrnx IS 'Delete the lettering for this row';
+
+
+
+CREATE TRIGGER t_plan_analytique_ins_upd BEFORE INSERT OR UPDATE ON public.plan_analytique FOR EACH ROW EXECUTE FUNCTION comptaproc.plan_analytic_ins_upd();
+
+
+
+CREATE TRIGGER t_poste_analytique_ins_upd BEFORE INSERT OR UPDATE ON public.poste_analytique FOR EACH ROW EXECUTE FUNCTION comptaproc.poste_analytique_ins_upd();
+
+
+
+CREATE TRIGGER t_remove_script_tag BEFORE INSERT OR UPDATE ON public.action_gestion_comment FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_remove_script_tag();
+
+
+
+CREATE TRIGGER t_tmp_pcm_alphanum_ins_upd BEFORE INSERT OR UPDATE ON public.tmp_pcmn FOR EACH ROW EXECUTE FUNCTION comptaproc.tmp_pcmn_alphanum_ins_upd();
+
+
+
+CREATE TRIGGER t_tmp_pcmn_ins BEFORE INSERT ON public.tmp_pcmn FOR EACH ROW EXECUTE FUNCTION comptaproc.tmp_pcmn_ins();
+
+
+
+CREATE TRIGGER todo_list_ins_upd BEFORE INSERT OR UPDATE ON public.todo_list FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_todo_list_ins_upd();
+
+
+
+COMMENT ON TRIGGER todo_list_ins_upd ON public.todo_list IS 'Force the column use_login to lowercase';
+
+
+
+CREATE TRIGGER todo_list_shared_ins_upd BEFORE INSERT OR UPDATE ON public.todo_list_shared FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_todo_list_shared_ins_upd();
+
+
+
+COMMENT ON TRIGGER todo_list_shared_ins_upd ON public.todo_list_shared IS 'Force the column ua_login to lowercase';
+
+
+
+CREATE TRIGGER trg_action_gestion_related BEFORE INSERT OR UPDATE ON public.action_gestion_related FOR EACH ROW EXECUTE FUNCTION comptaproc.action_gestion_related_ins_up();
+
+
+
+CREATE TRIGGER trg_category_card_before_delete BEFORE DELETE ON public.fiche_def FOR EACH ROW EXECUTE FUNCTION comptaproc.category_card_before_delete();
+
+
+
+CREATE TRIGGER trg_extension_ins_upd BEFORE INSERT OR UPDATE ON public.extension FOR EACH ROW EXECUTE FUNCTION comptaproc.extension_ins_upd();
+
+
+
+CREATE TRIGGER trg_set_tech_user BEFORE INSERT OR UPDATE ON public.operation_exercice FOR EACH ROW EXECUTE FUNCTION comptaproc.set_tech_user();
+
+
+
+CREATE TRIGGER trigger_document_type_i AFTER INSERT ON public.document_type FOR EACH ROW EXECUTE FUNCTION comptaproc.t_document_type_insert();
+
+
+
+CREATE TRIGGER trigger_jrn_def_sequence_i AFTER INSERT ON public.jrn_def FOR EACH ROW EXECUTE FUNCTION comptaproc.t_jrn_def_sequence();
+
+
+
+CREATE TRIGGER trigger_parameter_extra_format_code_biu BEFORE INSERT OR UPDATE ON public.parameter_extra FOR EACH ROW EXECUTE FUNCTION comptaproc.t_parameter_extra_code();
+
+
+
+CREATE TRIGGER user_sec_act_ins_upd BEFORE INSERT OR UPDATE ON public.user_sec_act FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_user_sec_act_ins_upd();
+
+
+
+COMMENT ON TRIGGER user_sec_act_ins_upd ON public.user_sec_act IS 'Force the column ua_login to lowercase';
+
+
+
+CREATE TRIGGER user_sec_jrn_after_ins_upd BEFORE INSERT OR UPDATE ON public.user_sec_jrn FOR EACH ROW EXECUTE FUNCTION comptaproc.trg_user_sec_jrn_ins_upd();
+
+
+
+COMMENT ON TRIGGER user_sec_jrn_after_ins_upd ON public.user_sec_jrn IS 'Force the column uj_login to lowercase';
+
+
+
+ALTER TABLE ONLY public.centralized
+ ADD CONSTRAINT "$1" FOREIGN KEY (c_jrn_def) REFERENCES public.jrn_def(jrn_def_id);
+
+
+
+ALTER TABLE ONLY public.user_sec_act
+ ADD CONSTRAINT "$1" FOREIGN KEY (ua_act_id) REFERENCES public.action(ac_id);
+
+
+
+ALTER TABLE ONLY public.fiche_def
+ ADD CONSTRAINT "$1" FOREIGN KEY (frd_id) REFERENCES public.fiche_def_ref(frd_id);
+
+
+
+ALTER TABLE ONLY public.attr_min
+ ADD CONSTRAINT "$1" FOREIGN KEY (frd_id) REFERENCES public.fiche_def_ref(frd_id);
+
+
+
+ALTER TABLE ONLY public.fiche
+ ADD CONSTRAINT "$1" FOREIGN KEY (fd_id) REFERENCES public.fiche_def(fd_id);
+
+
+
+ALTER TABLE ONLY public.fiche_detail
+ ADD CONSTRAINT "$1" FOREIGN KEY (f_id) REFERENCES public.fiche(f_id);
+
+
+
+ALTER TABLE ONLY public.jnt_fic_attr
+ ADD CONSTRAINT "$1" FOREIGN KEY (fd_id) REFERENCES public.fiche_def(fd_id);
+
+
+
+ALTER TABLE ONLY public.jrn
+ ADD CONSTRAINT "$1" FOREIGN KEY (jr_def_id) REFERENCES public.jrn_def(jrn_def_id);
+
+
+
+ALTER TABLE ONLY public.jrn_def
+ ADD CONSTRAINT "$1" FOREIGN KEY (jrn_def_type) REFERENCES public.jrn_type(jrn_type_id);
+
+
+
+ALTER TABLE ONLY public.jrnx
+ ADD CONSTRAINT "$2" FOREIGN KEY (j_jrn_def) REFERENCES public.jrn_def(jrn_def_id);
+
+
+
+ALTER TABLE ONLY public.attr_min
+ ADD CONSTRAINT "$2" FOREIGN KEY (ad_id) REFERENCES public.attr_def(ad_id);
+
+
+
+ALTER TABLE ONLY public.action_comment_document
+ ADD CONSTRAINT action_comment_document_action_gestion_comment_id_fkey FOREIGN KEY (action_gestion_comment_id) REFERENCES public.action_gestion_comment(agc_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_comment_document
+ ADD CONSTRAINT action_comment_document_document_id_fkey FOREIGN KEY (document_id) REFERENCES public.document(d_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_operation
+ ADD CONSTRAINT action_comment_operation_ag_id_fkey FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_operation
+ ADD CONSTRAINT action_comment_operation_jr_id_fkey FOREIGN KEY (jr_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_detail
+ ADD CONSTRAINT action_detail_ag_id_fkey FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_person
+ ADD CONSTRAINT action_gestion_ag_id_fk2 FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_comment
+ ADD CONSTRAINT action_gestion_comment_ag_id_fkey FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_related
+ ADD CONSTRAINT action_gestion_related_aga_greatest_fkey FOREIGN KEY (aga_greatest) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_related
+ ADD CONSTRAINT action_gestion_related_aga_least_fkey FOREIGN KEY (aga_least) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion_related
+ ADD CONSTRAINT action_gestion_related_aga_type_fkey FOREIGN KEY (aga_type) REFERENCES public.link_action_type(l_id);
+
+
+
+ALTER TABLE ONLY public.action_person
+ ADD CONSTRAINT action_person_ag_id_fkey FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_person
+ ADD CONSTRAINT action_person_f_id_fkey FOREIGN KEY (f_id) REFERENCES public.fiche(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_person_option
+ ADD CONSTRAINT action_person_option_fk FOREIGN KEY (action_person_id) REFERENCES public.action_person(ap_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_tags
+ ADD CONSTRAINT action_tags_ag_id_fkey FOREIGN KEY (ag_id) REFERENCES public.action_gestion(ag_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_tags
+ ADD CONSTRAINT action_tags_t_id_fkey FOREIGN KEY (t_id) REFERENCES public.tags(t_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_person_option
+ ADD CONSTRAINT contact_option_ref_fk FOREIGN KEY (contact_option_ref_id) REFERENCES public.contact_option_ref(cor_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.currency_history
+ ADD CONSTRAINT currency_history_currency_fk FOREIGN KEY (currency_id) REFERENCES public.currency(id) ON UPDATE CASCADE ON DELETE RESTRICT;
+
+
+
+ALTER TABLE ONLY public.document_modele
+ ADD CONSTRAINT document_modele_fk FOREIGN KEY (md_affect) REFERENCES public.document_component(dc_code) ON UPDATE CASCADE;
+
+
+
+ALTER TABLE ONLY public.document_option
+ ADD CONSTRAINT document_option_ref_fk FOREIGN KEY (document_type_id) REFERENCES public.document_type(dt_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.fiche_detail
+ ADD CONSTRAINT fiche_detail_attr_def_fk FOREIGN KEY (ad_id) REFERENCES public.attr_def(ad_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_person
+ ADD CONSTRAINT fiche_f_id_fk2 FOREIGN KEY (f_id) REFERENCES public.fiche(f_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion
+ ADD CONSTRAINT fiche_f_id_fk3 FOREIGN KEY (f_id_dest) REFERENCES public.fiche(f_id);
+
+
+
+ALTER TABLE ONLY public.action_gestion
+ ADD CONSTRAINT fk_action_gestion_document_type FOREIGN KEY (ag_type) REFERENCES public.document_type(dt_id);
+
+
+
+ALTER TABLE ONLY public.quant_fin
+ ADD CONSTRAINT fk_card FOREIGN KEY (qf_bank) REFERENCES public.fiche(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_fin
+ ADD CONSTRAINT fk_card_other FOREIGN KEY (qf_other) REFERENCES public.fiche(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.forecast_item
+ ADD CONSTRAINT fk_forecast FOREIGN KEY (fc_id) REFERENCES public.forecast_category(fc_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_info
+ ADD CONSTRAINT fk_info_def FOREIGN KEY (id_type) REFERENCES public.info_def(id_type) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_info
+ ADD CONSTRAINT fk_jrn FOREIGN KEY (jr_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_fin
+ ADD CONSTRAINT fk_jrn FOREIGN KEY (jr_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.groupe_analytique
+ ADD CONSTRAINT fk_pa_id FOREIGN KEY (pa_id) REFERENCES public.plan_analytique(pa_id) ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrnx
+ ADD CONSTRAINT fk_pcmn_val FOREIGN KEY (j_poste) REFERENCES public.tmp_pcmn(pcm_val);
+
+
+
+ALTER TABLE ONLY public.centralized
+ ADD CONSTRAINT fk_pcmn_val FOREIGN KEY (c_poste) REFERENCES public.tmp_pcmn(pcm_val);
+
+
+
+ALTER TABLE ONLY public.stock_goods
+ ADD CONSTRAINT fk_stock_good_f_id FOREIGN KEY (f_id) REFERENCES public.fiche(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.todo_list_shared
+ ADD CONSTRAINT fk_todo_list_shared_todo_list FOREIGN KEY (todo_list_id) REFERENCES public.todo_list(tl_id);
+
+
+
+ALTER TABLE ONLY public.forecast_category
+ ADD CONSTRAINT forecast_child FOREIGN KEY (f_id) REFERENCES public.forecast(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.forecast
+ ADD CONSTRAINT forecast_f_end_date_fkey FOREIGN KEY (f_end_date) REFERENCES public.parm_periode(p_id) ON UPDATE SET NULL ON DELETE SET NULL;
+
+
+
+ALTER TABLE ONLY public.forecast
+ ADD CONSTRAINT forecast_f_start_date_fkey FOREIGN KEY (f_start_date) REFERENCES public.parm_periode(p_id) ON UPDATE SET NULL ON DELETE SET NULL;
+
+
+
+ALTER TABLE ONLY public.form_detail
+ ADD CONSTRAINT formdef_fk FOREIGN KEY (fo_fr_id) REFERENCES public.form_definition(fr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.letter_cred
+ ADD CONSTRAINT jnt_cred_fk FOREIGN KEY (jl_id) REFERENCES public.jnt_letter(jl_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.letter_deb
+ ADD CONSTRAINT jnt_deb_fk FOREIGN KEY (jl_id) REFERENCES public.jnt_letter(jl_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jnt_document_option_contact
+ ADD CONSTRAINT jnt_document_option_contact_contact_option_ref_id_fkey FOREIGN KEY (contact_option_ref_id) REFERENCES public.contact_option_ref(cor_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jnt_document_option_contact
+ ADD CONSTRAINT jnt_document_option_contact_document_type_id_fkey FOREIGN KEY (document_type_id) REFERENCES public.document_type(dt_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jnt_fic_attr
+ ADD CONSTRAINT jnt_fic_attr_attr_def_fk FOREIGN KEY (ad_id) REFERENCES public.attr_def(ad_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jnt_tag_group_tag
+ ADD CONSTRAINT jnt_tag_group_tag_fk FOREIGN KEY (tag_id) REFERENCES public.tags(t_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jnt_tag_group_tag
+ ADD CONSTRAINT jnt_tag_group_tag_fk_1 FOREIGN KEY (tag_group_id) REFERENCES public.tag_group(tg_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn
+ ADD CONSTRAINT jrn_currency_fk FOREIGN KEY (currency_id) REFERENCES public.currency(id) ON UPDATE RESTRICT ON DELETE RESTRICT;
+
+
+
+ALTER TABLE ONLY public.jrn_def
+ ADD CONSTRAINT jrn_def_currency_fk FOREIGN KEY (currency_id) REFERENCES public.currency(id);
+
+
+
+ALTER TABLE ONLY public.op_predef
+ ADD CONSTRAINT jrn_def_id_fk FOREIGN KEY (jrn_def_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_periode
+ ADD CONSTRAINT jrn_per_jrn_def_id FOREIGN KEY (jrn_def_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_periode
+ ADD CONSTRAINT jrn_periode_p_id FOREIGN KEY (p_id) REFERENCES public.parm_periode(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_rapt
+ ADD CONSTRAINT jrn_rapt_jr_id_fkey FOREIGN KEY (jr_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_rapt
+ ADD CONSTRAINT jrn_rapt_jra_concerned_fkey FOREIGN KEY (jra_concerned) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_tax
+ ADD CONSTRAINT jrn_tax_acc_other_tax_fk FOREIGN KEY (ac_id) REFERENCES public.acc_other_tax(ac_id);
+
+
+
+ALTER TABLE ONLY public.jrn_tax
+ ADD CONSTRAINT jrn_tax_fk FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrnx
+ ADD CONSTRAINT jrnx_f_id_fkey FOREIGN KEY (f_id) REFERENCES public.fiche(f_id) ON UPDATE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_fin
+ ADD CONSTRAINT jrnx_j_id_fk FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.jrn_note
+ ADD CONSTRAINT jrnx_note_j_id_fkey FOREIGN KEY (jr_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_activity
+ ADD CONSTRAINT key_distribution_activity_ke_id_fkey FOREIGN KEY (ke_id) REFERENCES public.key_distribution_detail(ke_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_activity
+ ADD CONSTRAINT key_distribution_activity_pa_id_fkey FOREIGN KEY (pa_id) REFERENCES public.plan_analytique(pa_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_activity
+ ADD CONSTRAINT key_distribution_activity_po_id_fkey FOREIGN KEY (po_id) REFERENCES public.poste_analytique(po_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_detail
+ ADD CONSTRAINT key_distribution_detail_kd_id_fkey FOREIGN KEY (kd_id) REFERENCES public.key_distribution(kd_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_ledger
+ ADD CONSTRAINT key_distribution_ledger_jrn_def_id_fkey FOREIGN KEY (jrn_def_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.key_distribution_ledger
+ ADD CONSTRAINT key_distribution_ledger_kd_id_fkey FOREIGN KEY (kd_id) REFERENCES public.key_distribution(kd_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.letter_cred
+ ADD CONSTRAINT letter_cred_fk FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.letter_deb
+ ADD CONSTRAINT letter_deb_fk FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.document_modele
+ ADD CONSTRAINT md_type FOREIGN KEY (md_type) REFERENCES public.document_type(dt_id);
+
+
+
+ALTER TABLE ONLY public.payment_method
+ ADD CONSTRAINT mod_payment_jrn_def_id_fk FOREIGN KEY (jrn_def_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.payment_method
+ ADD CONSTRAINT mod_payment_mp_fd_id_fkey FOREIGN KEY (mp_fd_id) REFERENCES public.fiche_def(fd_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.payment_method
+ ADD CONSTRAINT mod_payment_mp_jrn_def_id_fkey FOREIGN KEY (mp_jrn_def_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_analytique
+ ADD CONSTRAINT operation_analytique_fiche_id_fk FOREIGN KEY (f_id) REFERENCES public.fiche(f_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_analytique
+ ADD CONSTRAINT operation_analytique_j_id_fkey FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_analytique
+ ADD CONSTRAINT operation_analytique_po_id_fkey FOREIGN KEY (po_id) REFERENCES public.poste_analytique(po_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_currency
+ ADD CONSTRAINT operation_currency_jrnx_fk FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_exercice_detail
+ ADD CONSTRAINT operation_exercice_detail_oe_id_fkey FOREIGN KEY (oe_id) REFERENCES public.operation_exercice(oe_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_tag
+ ADD CONSTRAINT opt_jrnx FOREIGN KEY (jrn_id) REFERENCES public.jrn(jr_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.operation_tag
+ ADD CONSTRAINT opt_tag_id FOREIGN KEY (tag_id) REFERENCES public.tags(t_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.poste_analytique
+ ADD CONSTRAINT poste_analytique_pa_id_fkey FOREIGN KEY (pa_id) REFERENCES public.plan_analytique(pa_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.action_gestion
+ ADD CONSTRAINT profile_fkey FOREIGN KEY (ag_dest) REFERENCES public.profile(p_id) ON UPDATE SET NULL ON DELETE SET NULL;
+
+
+
+ALTER TABLE ONLY public.profile_menu
+ ADD CONSTRAINT profile_menu_me_code_fkey FOREIGN KEY (me_code) REFERENCES public.menu_ref(me_code) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.profile_menu
+ ADD CONSTRAINT profile_menu_p_id_fkey FOREIGN KEY (p_id) REFERENCES public.profile(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.profile_menu
+ ADD CONSTRAINT profile_menu_type_fkey FOREIGN KEY (p_type_display) REFERENCES public.profile_menu_type(pm_type);
+
+
+
+ALTER TABLE ONLY public.profile_mobile
+ ADD CONSTRAINT profile_mobile_menu_ref_fk FOREIGN KEY (me_code) REFERENCES public.menu_ref(me_code);
+
+
+
+ALTER TABLE ONLY public.profile_mobile
+ ADD CONSTRAINT profile_mobile_profile_fk FOREIGN KEY (p_id) REFERENCES public.profile(p_id);
+
+
+
+ALTER TABLE ONLY public.profile_sec_repository
+ ADD CONSTRAINT profile_sec_repository_p_id_fkey FOREIGN KEY (p_id) REFERENCES public.profile(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.profile_sec_repository
+ ADD CONSTRAINT profile_sec_repository_r_id_fkey FOREIGN KEY (r_id) REFERENCES public.stock_repository(r_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.profile_user
+ ADD CONSTRAINT profile_user_p_id_fkey FOREIGN KEY (p_id) REFERENCES public.profile(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_purchase
+ ADD CONSTRAINT qp_vat_code_fk FOREIGN KEY (qp_vat_code) REFERENCES public.tva_rate(tva_id) ON UPDATE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_sold
+ ADD CONSTRAINT qs_vat_code_fk FOREIGN KEY (qs_vat_code) REFERENCES public.tva_rate(tva_id) ON UPDATE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_purchase
+ ADD CONSTRAINT quant_purchase_j_id_fkey FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_purchase
+ ADD CONSTRAINT quant_purchase_qp_internal_fkey FOREIGN KEY (qp_internal) REFERENCES public.jrn(jr_internal) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
+
+
+
+ALTER TABLE ONLY public.quant_sold
+ ADD CONSTRAINT quant_sold_j_id_fkey FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.quant_sold
+ ADD CONSTRAINT quant_sold_qs_internal_fkey FOREIGN KEY (qs_internal) REFERENCES public.jrn(jr_internal) ON UPDATE CASCADE ON DELETE CASCADE DEFERRABLE INITIALLY DEFERRED;
+
+
+
+ALTER TABLE ONLY public.stock_change
+ ADD CONSTRAINT stock_change_r_id_fkey FOREIGN KEY (r_id) REFERENCES public.stock_repository(r_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.stock_goods
+ ADD CONSTRAINT stock_goods_c_id_fkey FOREIGN KEY (c_id) REFERENCES public.stock_change(c_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.stock_goods
+ ADD CONSTRAINT stock_goods_j_id_fkey FOREIGN KEY (j_id) REFERENCES public.jrnx(j_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.tmp_stockgood_detail
+ ADD CONSTRAINT tmp_stockgood_detail_s_id_fkey FOREIGN KEY (s_id) REFERENCES public.tmp_stockgood(s_id) ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.user_sec_jrn
+ ADD CONSTRAINT uj_priv_id_fkey FOREIGN KEY (uj_jrn_id) REFERENCES public.jrn_def(jrn_def_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.user_sec_action_profile
+ ADD CONSTRAINT user_sec_action_profile_p_granted_fkey FOREIGN KEY (p_granted) REFERENCES public.profile(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.user_sec_action_profile
+ ADD CONSTRAINT user_sec_action_profile_p_id_fkey FOREIGN KEY (p_id) REFERENCES public.profile(p_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY public.user_widget
+ ADD CONSTRAINT user_widget_dashboard_widget_id_fkey FOREIGN KEY (dashboard_widget_id) REFERENCES public.widget_dashboard(wd_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY tva_belge.assujetti_chld
+ ADD CONSTRAINT assujetti_fk FOREIGN KEY (a_id) REFERENCES tva_belge.assujetti(a_id) ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY tva_belge.intracomm_chld
+ ADD CONSTRAINT intracom_fk FOREIGN KEY (i_id) REFERENCES tva_belge.intracomm(i_id) ON DELETE CASCADE;
+
+
+
+ALTER TABLE ONLY tva_belge.parameter_chld
+ ADD CONSTRAINT parameter_chld_tva_id_fkey FOREIGN KEY (tva_id) REFERENCES public.tva_rate(tva_id) ON UPDATE CASCADE ON DELETE CASCADE;
+
+
+
diff --git a/include/sql/mod2/schema.sql b/include/sql/mod2/schema.sql
new file mode 100644
index 000000000..fdabc9401
--- /dev/null
+++ b/include/sql/mod2/schema.sql
@@ -0,0 +1,7909 @@
+
+
+SET statement_timeout = 0;
+SET lock_timeout = 0;
+SET idle_in_transaction_session_timeout = 0;
+SET client_encoding = 'UTF8';
+SET standard_conforming_strings = on;
+SELECT pg_catalog.set_config('search_path', '', false);
+SET check_function_bodies = false;
+SET xmloption = content;
+SET client_min_messages = warning;
+SET row_security = off;
+
+
+CREATE SCHEMA comptaproc;
+
+
+
+CREATE SCHEMA tva_belge;
+
+
+
+CREATE DOMAIN public.account_type AS character varying(40);
+
+
+
+CREATE TYPE public.anc_table_account_type AS (
+ po_id bigint,
+ pa_id bigint,
+ po_name text,
+ po_description text,
+ sum_amount numeric(25,4),
+ card_account text,
+ name text
+);
+
+
+
+CREATE TYPE public.anc_table_card_type AS (
+ po_id bigint,
+ pa_id bigint,
+ po_name text,
+ po_description text,
+ sum_amount numeric(25,4),
+ f_id bigint,
+ card_account text,
+ name text
+);
+
+
+
+CREATE TYPE public.menu_tree AS (
+ code text,
+ description text
+);
+
+
+
+CREATE FUNCTION comptaproc.account_add(p_id public.account_type, p_name character varying) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ nCount integer;
+ sReturn text;
+begin
+ -- patch 189
+ sReturn:= format_account(p_id);
+ select count(*) into nCount from tmp_pcmn where pcm_val=sReturn;
+ if nCount = 0 then
+ nParent=account_parent(p_id);
+ insert into tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent)
+ values (p_id, p_name,nParent) returning pcm_val into sReturn;
+ end if;
+ return sReturn;
+end ;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_alphanum() RETURNS boolean
+ LANGUAGE plpgsql
+ AS $$
+declare
+ l_auto bool;
+begin
+ l_auto := true;
+ select pr_value into l_auto from parameter where pr_id='MY_ALPHANUM';
+ if l_auto = 'N' or l_auto is null then
+ l_auto:=false;
+ end if;
+ return l_auto;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_auto(p_fd_id integer) RETURNS boolean
+ LANGUAGE plpgsql
+ AS $$
+declare
+ l_auto bool;
+begin
+
+ select fd_create_account into l_auto from fiche_def where fd_id=p_fd_id;
+ if l_auto is null then
+ l_auto:=false;
+ end if;
+ return l_auto;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_compute(p_f_id integer) RETURNS public.account_type
+ LANGUAGE plpgsql
+ AS $$
+declare
+ class_base fiche_def.fd_class_base%type;
+ maxcode numeric;
+ sResult account_type;
+ bAlphanum bool;
+ sName text;
+ nCount integer;
+ sNumber text;
+begin
+ -- patch 189
+ select fd_class_base into class_base
+ from
+ fiche_def join fiche using (fd_id)
+ where
+ f_id=p_f_id;
+
+ bAlphanum := account_alphanum();
+ if bAlphanum = false then
+ select max (pcm_val::numeric) into maxcode
+ from tmp_pcmn
+ where pcm_val_parent = class_base and pcm_val !~* '[[:alpha:]]' ;
+ if maxcode is null or length(maxcode::text) < length(class_base)+4 then
+ maxcode:=class_base::numeric*10000+1;
+ else
+ select max (pcm_val::numeric) into maxcode
+ from tmp_pcmn
+ where pcm_val !~* '[[:alpha:]]'
+ and pcm_val_parent = class_base
+ and substr(pcm_val::text,1,length(class_base))=class_base;
+
+ sNumber := substr(maxcode::text,length(class_base)+1);
+ nCount := sNumber::numeric+1;
+ sNumber := lpad (nCount::text,4,'0');
+
+ maxcode:=class_base||sNumber;
+ end if;
+ sResult:=maxcode::account_type;
+ else
+ -- if alphanum, use name
+ select ad_value into sName from fiche_detail where f_id=p_f_id and ad_id=1;
+ if sName is null then
+ raise exception 'Cannot compute an accounting without the name of the card for %',p_f_id;
+ end if;
+ sResult := account_compute_alpha(class_base,sName);
+ end if;
+ return sResult;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_compute_alpha(p_class text, p_name text) RETURNS public.account_type
+ LANGUAGE plpgsql
+ AS $$
+declare
+ sResult account_type;
+ sAccount account_type;
+ sFormatedAccount account_type;
+ nCount int;
+ idx int :=0;
+begin
+ sFormatedAccount := comptaproc.format_account(p_name);
+
+ sAccount := p_class||substring(sFormatedAccount for 5);
+ nCount := 0;
+ loop
+ select count(*) into nCount from tmp_pcmn where pcm_val = comptaproc.format_account(sAccount);
+
+ exit when nCount = 0;
+ idx := idx + 1;
+ sAccount := p_class || substring(sFormatedAccount for 5)||idx::text;
+ end loop;
+ sResult := comptaproc.format_account(sAccount);
+ return sResult;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_insert(p_f_id integer, p_account text) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sName varchar;
+ sNew tmp_pcmn.pcm_val%type;
+ bAuto bool;
+ nFd_id integer;
+ sClass_Base fiche_def.fd_class_base%TYPE;
+ nCount integer;
+ first text;
+ second text;
+ s_account text;
+begin
+ -- patch 189
+ -- accouting is given
+ if p_account is not null and length(trim(p_account)) != 0 then
+ -- if there is coma in p_account, treat normally
+ if position (',' in p_account) = 0 then
+ s_account := format_account(substr( p_account,1 , 40)::account_type);
+ select count(*) into nCount from tmp_pcmn where pcm_val=s_account::account_type;
+ if nCount !=0 then
+ perform attribut_insert(p_f_id,5,s_account);
+ else
+ -- account doesn't exist, create it
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+ -- retrieve parent account from card
+ select fd_class_base::account_type into nParent from fiche_def where fd_id=(select fd_id from fiche where f_id=p_f_id);
+ if nParent = null or nParent = '' then
+ nParent:=account_parent(s_account::account_type);
+ end if;
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (s_account::account_type,sName,nParent);
+ perform attribut_insert(p_f_id,5,s_account);
+
+ end if;
+ return s_account;
+ else
+ -- there is 2 accounts separated by a comma
+ first := split_part(p_account,',',1);
+ second := split_part(p_account,',',2);
+ -- check there is no other coma
+
+ if position (',' in first) != 0 or position (',' in second) != 0 then
+ raise exception 'Too many comas, invalid account';
+ end if;
+ perform attribut_insert(p_f_id,5,p_account);
+
+ end if;
+ return s_account;
+ end if;
+
+ select fd_id into nFd_id from fiche where f_id=p_f_id;
+ bAuto:= account_auto(nFd_id);
+
+ select fd_class_base into sClass_base from fiche_def where fd_id=nFd_id;
+ if bAuto = true and sClass_base similar to '[[:digit:]]*' then
+ sNew:=account_compute(p_f_id);
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=sClass_Base::account_type;
+ sNew := account_add (sNew,sName);
+ update tmp_pcmn set pcm_val_parent=nParent where pcm_val=sNew;
+ perform attribut_insert(p_f_id,5,sNew);
+ return sNew;
+ else
+ -- if there is an account_base then it is the default
+ if trim(coalesce(sClass_base::text,'')) = '' then
+ perform attribut_insert(p_f_id,5,null);
+ else
+ perform attribut_insert(p_f_id,5,sClass_base);
+ end if;
+ return sClass_base;
+ end if;
+
+ raise notice 'ai89.account_insert nothing done : error';
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_parent(p_account public.account_type) RETURNS public.account_type
+ LANGUAGE plpgsql
+ AS $$
+declare
+ sSubParent tmp_pcmn.pcm_val_parent%type;
+ sResult tmp_pcmn.pcm_val_parent%type;
+ nCount integer;
+begin
+ if p_account is NULL then
+ return NULL;
+ end if;
+ sSubParent:=p_account;
+ while true loop
+ select count(*) into nCount
+ from tmp_pcmn
+ where
+ pcm_val = sSubParent;
+ if nCount != 0 then
+ sResult:= sSubParent;
+ exit;
+ end if;
+ sSubParent:= substr(sSubParent,1,length(sSubParent)-1);
+ if length(sSubParent) <= 0 then
+ raise exception 'Impossible de trouver le compte parent pour %',p_account;
+ end if;
+ raise notice 'sSubParent % % ',sSubParent,length(sSubParent);
+ end loop;
+ raise notice 'account_parent : Parent is %',sSubParent;
+ return sSubParent;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.account_update(p_f_id integer, p_account public.account_type) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nMax fiche.f_id%type;
+ nCount integer;
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sName varchar;
+ first text;
+ second text;
+begin
+
+ if length(trim(p_account)) != 0 then
+ -- 2 accounts in card separated by comma
+ if position (',' in p_account) = 0 then
+ p_account := format_account(p_account);
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount = 0 then
+ select ad_value into sName from
+ fiche_detail
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=account_parent(p_account);
+ raise notice 'insert into tmp_pcmn % %',p_account,sName;
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
+ end if;
+ else
+ raise info 'presence of a comma';
+ -- there is 2 accounts separated by a comma
+ first := split_part(p_account,',',1);
+ second := split_part(p_account,',',2);
+ -- check there is no other coma
+ raise info 'first value % second value %', first, second;
+
+ if position (',' in first) != 0 or position (',' in second) != 0 then
+ raise exception 'Too many comas, invalid account';
+ end if;
+ -- check that both account are in PCMN
+
+ end if;
+ else
+ -- account is null
+ update fiche_detail set ad_value=null where f_id=p_f_id and ad_id=5 ;
+ return 0;
+ end if;
+
+ update fiche_detail set ad_value=p_account where f_id=p_f_id and ad_id=5 ;
+
+return 0;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.action_gestion_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+NEW.ag_title := substr(trim(NEW.ag_title),1,70);
+NEW.ag_hour := substr(trim(NEW.ag_hour),1,5);
+NEW.ag_owner := lower(NEW.ag_owner);
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.action_gestion_related_ins_up() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nTmp bigint;
+begin
+
+if NEW.aga_least > NEW.aga_greatest then
+ nTmp := NEW.aga_least;
+ NEW.aga_least := NEW.aga_greatest;
+ NEW.aga_greatest := nTmp;
+end if;
+
+if NEW.aga_least = NEW.aga_greatest then
+ return NULL;
+end if;
+
+return NEW;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.anc_correct_tvand() RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ n_count numeric;
+ i record;
+ newrow_tva record;
+begin
+ for i in select * from operation_analytique where oa_jrnx_id_source is not null loop
+ -- Get all the anc accounting from the base operation and insert the missing record for VAT
+ for newrow_tva in select * from operation_analytique where j_id=i.oa_jrnx_id_source and po_id <> i.po_id loop
+
+ -- check if the record is yet present
+ select count(*) into n_count from operation_analytique where po_id=newrow_tva.po_id and oa_jrnx_id_source=i.oa_jrnx_id_source;
+
+ if n_count = 0 then
+ raise info 'insert operation analytique po_id = % oa_group = % ',i.po_id, i.oa_group;
+ insert into operation_analytique
+ (po_id,oa_amount,oa_description,oa_debit,j_id,oa_group,oa_date,oa_jrnx_id_source,oa_positive)
+ values (newrow_tva.po_id,i.oa_amount,i.oa_description,i.oa_debit,i.j_id,i.oa_group,i.oa_date,i.oa_jrnx_id_source,i.oa_positive);
+ end if;
+
+ end loop;
+
+
+ end loop;
+end;
+ $$;
+
+
+
+CREATE FUNCTION comptaproc.attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nResult bigint;
+begin
+ update fiche_detail set ad_value=p_value where ad_id=p_ad_id and f_id=p_f_id returning jft_id into nResult;
+ if nResult is null then
+ insert into fiche_detail (f_id,ad_id, ad_value) values (p_f_id,p_ad_id,p_value);
+ end if;
+
+return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.attribute_correct_order() RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ crs_correct cursor for select A.jnt_id,A.jnt_order from jnt_fic_attr as A join jnt_fic_attr as B using (fd_id) where A.jnt_order=B.jnt_order and A.jnt_id > B.jnt_id;
+ rec record;
+begin
+ open crs_correct;
+ loop
+ fetch crs_correct into rec;
+ if NOT FOUND then
+ close crs_correct;
+ return;
+ end if;
+ update jnt_fic_attr set jnt_order=jnt_order + 1 where jnt_id = rec.jnt_id;
+ end loop;
+ close crs_correct;
+ perform attribute_correct_order ();
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.card_after_delete() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+
+begin
+
+ delete from action_gestion where f_id_dest = OLD.f_id;
+ return OLD;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.card_class_base(p_f_id integer) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+declare
+ n_poste fiche_def.fd_class_base%type;
+begin
+
+ select fd_class_base into n_poste from fiche_def join fiche using
+(fd_id)
+ where f_id=p_f_id;
+ if not FOUND then
+ raise exception 'Invalid fiche card_class_base(%)',p_f_id;
+ end if;
+return n_poste;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.category_card_before_delete() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+
+begin
+ if OLD.fd_id > 499000 then
+ return null;
+ end if;
+ return OLD;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.check_balance(p_grpt integer) RETURNS numeric
+ LANGUAGE plpgsql
+ AS $$
+declare
+ amount_jrnx_debit numeric;
+ amount_jrnx_credit numeric;
+ amount_jrn numeric;
+begin
+ select coalesce(sum (j_montant),0) into amount_jrnx_credit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=false;
+
+ select coalesce(sum (j_montant),0) into amount_jrnx_debit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=true;
+
+ select coalesce(jr_montant,0) into amount_jrn
+ from jrn
+ where
+ jr_grpt_id=p_grpt;
+
+ if ( amount_jrnx_debit != amount_jrnx_credit )
+ then
+ return abs(amount_jrnx_debit-amount_jrnx_credit);
+ end if;
+ if ( amount_jrn != amount_jrnx_credit)
+ then
+ return -1*abs(amount_jrn - amount_jrnx_credit);
+ end if;
+ return 0;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.check_periode() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nPeriode int;
+ nExerciceLabel int;
+begin
+ nPeriode:=periode_exist(to_char(NEW.p_start,'DD.MM.YYYY'),NEW.p_id);
+ if nPeriode <> -1 then
+ raise info 'Overlap periode start % periode %',NEW.p_start,nPeriode;
+ return null;
+ end if;
+ if new.p_exercice_label is null or trim (new.p_exercice_label ) = '' then
+ new.p_exercice_label := new.p_exercice;
+ end if;
+ select count(*) into nExerciceLabel
+ from parm_periode
+ where
+ (p_exercice =new.p_exercice and p_exercice_label <> new.p_exercice_label)
+ or
+ (p_exercice <> new.p_exercice and p_exercice_label = new.p_exercice_label);
+
+ if nExerciceLabel > 0 then
+ raise exception 'a label cannot be on two exercices';
+ return null;
+ end if;
+
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.correct_sequence(p_sequence text, p_col text, p_table text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+last_sequence int8;
+max_sequence int8;
+n integer;
+begin
+ select count(*) into n from pg_class where relkind='S' and relname=lower(p_sequence);
+ if n = 0 then
+ raise exception ' Unknow sequence % ',p_sequence;
+ end if;
+ select count(*) into n from pg_class where relkind='r' and relname=lower(p_table);
+ if n = 0 then
+ raise exception ' Unknow table % ',p_table;
+ end if;
+
+ execute 'select last_value from '||p_sequence into last_sequence;
+ raise notice 'Last value of the sequence is %', last_sequence;
+
+ execute 'select max('||p_col||') from '||p_table into max_sequence;
+ if max_sequence is null then
+ max_sequence := 0;
+ end if;
+ raise notice 'Max value of the sequence is %', max_sequence;
+ max_sequence:= max_sequence +1;
+ execute 'alter sequence '||p_sequence||' restart with '||max_sequence;
+return 0;
+
+end;
+$$;
+
+
+
+COMMENT ON FUNCTION comptaproc.correct_sequence(p_sequence text, p_col text, p_table text) IS ' Often the primary key is a sequence number and sometimes the value of the sequence is not synchronized with the primary key ( p_sequence : sequence name, p_col : col of the pk,p_table : concerned table';
+
+
+
+CREATE FUNCTION comptaproc.create_missing_sequence() RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+p_sequence text;
+nSeq integer;
+c1 cursor for select jrn_def_id from jrn_def;
+begin
+ open c1;
+ loop
+ fetch c1 into nSeq;
+ if not FOUND THEN
+ close c1;
+ return 0;
+ end if;
+ p_sequence:='s_jrn_pj'||nSeq::text;
+ execute 'create sequence '||p_sequence;
+ end loop;
+close c1;
+return 0;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.drop_index(p_constraint character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nCount integer;
+begin
+ select count(*) into nCount from pg_indexes where indexname=p_constraint;
+ if nCount = 1 then
+ execute 'drop index '||p_constraint ;
+ end if;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.drop_it(p_constraint character varying) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nCount integer;
+begin
+ select count(*) into nCount from pg_constraint where conname=p_constraint;
+ if nCount = 1 then
+ execute 'alter table parm_periode drop constraint '||p_constraint ;
+ end if;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.extension_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ sCode text;
+ sFile text;
+begin
+sCode:=trim(upper(NEW.ex_code));
+sCode:=replace(sCode,' ','_');
+sCode:=substr(sCode,1,15);
+sCode=upper(sCode);
+NEW.ex_code:=sCode;
+sFile:=NEW.ex_file;
+sFile:=replace(sFile,';','_');
+sFile:=replace(sFile,'<','_');
+sFile:=replace(sFile,'>','_');
+sFile:=replace(sFile,'..','');
+sFile:=replace(sFile,'&','');
+sFile:=replace(sFile,'|','');
+
+
+
+return NEW;
+
+end;
+
+$$;
+
+
+
+CREATE FUNCTION comptaproc.fiche_account_parent(p_f_id integer) RETURNS public.account_type
+ LANGUAGE plpgsql
+ AS $$
+declare
+ret tmp_pcmn.pcm_val%TYPE;
+begin
+ select fd_class_base into ret from fiche_def join fiche using (fd_id) where f_id=p_f_id;
+ if not FOUND then
+ raise exception '% N''existe pas',p_f_id;
+ end if;
+ return ret;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.fiche_attribut_synchro(p_fd_id integer) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ -- this sql gives the f_id and the missing attribute (ad_id)
+ list_missing cursor for select f_id,fd_id,ad_id,jnt_order from jnt_fic_attr join fiche as A using (fd_id) where fd_id=p_fd_id and ad_id not in (select ad_id from fiche join fiche_detail using (f_id) where fd_id=jnt_fic_attr.fd_id and A.f_id=f_id);
+ rec record;
+begin
+ open list_missing;
+ loop
+
+ fetch list_missing into rec;
+ IF NOT FOUND then
+ exit;
+ end if;
+
+ -- now we insert into attr_value
+ insert into fiche_detail (f_id,ad_id,ad_value) values (rec.f_id,rec.ad_id,null);
+ end loop;
+ close list_missing;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.fiche_def_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+
+if position (',' in NEW.fd_class_base) != 0 then
+ NEW.fd_create_account='f';
+
+end if;
+return NEW;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.fiche_detail_check() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ BEGIN
+ if new.ad_id = 23 and coalesce (new.ad_value,'') = '' then
+ raise exception 'QUICKCODE can not be empty';
+ end if;
+ if new.ad_id = 1 and coalesce (new.ad_value,'') = '' then
+ raise exception 'NAME can not be empty';
+ end if;
+ return new;
+ END;
+
+$$;
+
+
+
+CREATE FUNCTION comptaproc.fiche_detail_check_qcode() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ i record;
+begin
+ if NEW.ad_id=23 and NEW.ad_value != OLD.ad_value then
+ update jrnx set j_qcode=NEW.ad_value where j_qcode = OLD.ad_value;
+ update op_predef_detail set opd_poste=NEW.ad_value where opd_poste=OLD.ad_value;
+ for i in select ad_id from attr_def where ad_type = 'card' or ad_id=25 loop
+ update fiche_detail set ad_value=NEW.ad_value where ad_value=OLD.ad_value and ad_id=i.ad_id;
+ if i.ad_id=19 then
+ update stock_goods set sg_code=NEW.ad_value where sg_code=OLD.ad_value;
+ end if;
+
+ end loop;
+ end if;
+ return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.fill_quant_fin() RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ sBank text;
+ sCassa text;
+ sCustomer text;
+ sSupplier text;
+ rec record;
+ recBank record;
+ recSupp_Cust record;
+ nCount integer;
+ nAmount numeric;
+ nBank integer;
+ nOther integer;
+ nSupp_Cust integer;
+begin
+ select p_value into sBank from parm_code where p_code='BANQUE';
+ select p_value into sCassa from parm_code where p_code='CAISSE';
+ select p_value into sSupplier from parm_code where p_code='SUPPLIER';
+ select p_value into sCustomer from parm_code where p_code='CUSTOMER';
+
+ for rec in select jr_id,jr_grpt_id from jrn
+ where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN')
+ and jr_id not in (select jr_id from quant_fin)
+ loop
+ -- there are only 2 lines for bank operations
+ -- first debit
+ select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
+ if nCount > 2 then
+ raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
+ return;
+ end if;
+ nBank := 0; nOther:=0;
+ for recBank in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
+ loop
+ if recBank.j_poste like sBank||'%' then
+ -- retrieve f_id for bank
+ select f_id into nBank from vw_poste_qcode where j_qcode=recBank.j_qcode;
+ if recBank.j_debit = false then
+ nAmount=recBank.j_montant*(-1);
+ else
+ nAmount=recBank.j_montant;
+ end if;
+ else
+ select f_id into nOther from vw_poste_qcode where j_qcode=recBank.j_qcode;
+ end if;
+ end loop;
+ if nBank != 0 and nOther != 0 then
+ insert into quant_fin (jr_id,qf_bank,qf_other,qf_amount) values (rec.jr_id,nBank,nOther,nAmount);
+ end if;
+ end loop;
+ for rec in select jr_id,jr_grpt_id from jrn
+ where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN') and jr_id not in (select jr_id from quant_fin)
+ loop
+ -- there are only 2 lines for bank operations
+ -- first debit
+ select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
+ if nCount > 2 then
+ raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
+ return;
+ end if;
+ nBank := 0; nOther:=0;
+ for recBank in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
+ loop
+ if recBank.j_poste like sCassa||'%' then
+ -- retrieve f_id for bank
+ select f_id into nBank from vw_poste_qcode where j_qcode=recBank.j_qcode;
+ if recBank.j_debit = false then
+ nAmount=recBank.j_montant*(-1);
+ else
+ nAmount=recBank.j_montant;
+ end if;
+ else
+ select f_id into nOther from vw_poste_qcode where j_qcode=recBank.j_qcode;
+ end if;
+ end loop;
+ if nBank != 0 and nOther != 0 then
+ insert into quant_fin (jr_id,qf_bank,qf_other,qf_amount) values (rec.jr_id,nBank,nOther,nAmount);
+ end if;
+ end loop;
+
+ for rec in select jr_id,jr_grpt_id from jrn
+ where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN') and jr_id not in (select jr_id from quant_fin)
+ loop
+ -- there are only 2 lines for bank operations
+ -- first debit
+ select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
+ if nCount > 2 then
+ raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
+ return;
+ end if;
+ nSupp_Cust := 0; nOther:=0;
+ for recSupp_Cust in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
+ loop
+ if recSupp_Cust.j_poste like sSupplier||'%' then
+ -- retrieve f_id for bank
+ select f_id into nSupp_Cust from vw_poste_qcode where j_qcode=recSupp_Cust.j_qcode;
+ if recSupp_Cust.j_debit = true then
+ nAmount=recSupp_Cust.j_montant*(-1);
+ else
+ nAmount=recSupp_Cust.j_montant;
+ end if;
+ else if recSupp_Cust.j_poste like sCustomer||'%' then
+ select f_id into nSupp_Cust from vw_poste_qcode where j_qcode=recSupp_Cust.j_qcode;
+ if recSupp_Cust.j_debit = false then
+ nAmount=recSupp_Cust.j_montant*(-1);
+ else
+ nAmount=recSupp_Cust.j_montant;
+ end if;
+ else
+ select f_id into nOther from vw_poste_qcode where j_qcode=recSupp_Cust.j_qcode;
+
+ end if;
+ end if;
+ end loop;
+ if nSupp_Cust != 0 and nOther != 0 then
+ insert into quant_fin (jr_id,qf_bank,qf_other,qf_amount) values (rec.jr_id,nOther,nSupp_Cust,nAmount);
+ end if;
+ end loop;
+ for rec in select jr_id,jr_grpt_id from jrn
+ where jr_def_id in (select jrn_def_id from jrn_def where jrn_def_type='FIN') and jr_id not in (select jr_id from quant_fin)
+ loop
+ -- there are only 2 lines for bank operations
+ -- first debit
+ select count(j_id) into nCount from jrnx where j_grpt=rec.jr_grpt_id;
+ if nCount > 2 then
+ raise notice 'Trop de valeur pour jr_grpt_id % count %',rec.jr_grpt_id,nCount;
+ return;
+ end if;
+ nSupp_Cust := 0; nOther:=0;
+ for recSupp_Cust in select j_id, j_montant,j_debit,j_qcode,j_poste from jrnx where j_grpt=rec.jr_grpt_id
+ loop
+ if recSupp_Cust.j_poste like '441%' then
+ -- retrieve f_id for bank
+ select f_id into nSupp_Cust from vw_poste_qcode where j_qcode=recSupp_Cust.j_qcode;
+ if recSupp_Cust.j_debit = false then
+ nAmount=recSupp_Cust.j_montant*(-1);
+ else
+ nAmount=recSupp_Cust.j_montant;
+ end if;
+ else
+ select f_id into nOther from vw_poste_qcode where j_qcode=recSupp_Cust.j_qcode;
+
+
+ end if;
+ end loop;
+ if nSupp_Cust != 0 and nOther != 0 then
+ insert into quant_fin (jr_id,qf_bank,qf_other,qf_amount) values (rec.jr_id,nOther,nSupp_Cust,nAmount);
+ end if;
+ end loop;
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.find_pcm_type(pp_value public.account_type) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+declare
+ str_type parm_poste.p_type%TYPE;
+ str_value parm_poste.p_type%TYPE;
+ nLength integer;
+begin
+ str_value:=pp_value;
+ nLength:=length(str_value::text);
+
+ while nLength > 0 loop
+ select p_type into str_type from parm_poste where p_value=str_value;
+ if FOUND then
+ raise info 'Type of %s is %s',str_value,str_type;
+ return str_type;
+ end if;
+ nLength:=nLength-1;
+ str_value:=substring(str_value::text from 1 for nLength)::account_type;
+ end loop;
+ str_value := pp_value;
+ nLength:=length(str_value::text);
+ str_value:=substring(str_value::text from 1 for nLength)::account_type;
+ while nLength > 0 loop
+ select pcm_type into str_type from tmp_pcmn tp where pcm_val=str_value;
+ if FOUND then
+ raise info 'Type of %s is %s',str_value,str_type;
+ return str_type;
+ end if;
+ nLength:=nLength-1;
+ str_value:=substring(str_value::text from 1 for nLength)::account_type;
+ end loop;
+return 'CON';
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.find_periode(p_date text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+
+declare n_p_id int4;
+begin
+
+select p_id into n_p_id
+ from parm_periode
+ where
+ p_start <= to_date(p_date,'DD.MM.YYYY')
+ and
+ p_end >= to_date(p_date,'DD.MM.YYYY');
+
+if NOT FOUND then
+ return -1;
+end if;
+
+return n_p_id;
+
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.format_account(p_account public.account_type) RETURNS public.account_type
+ LANGUAGE plpgsql
+ AS $_$
+
+declare
+
+sResult account_type;
+
+begin
+sResult := lower(p_account);
+
+sResult := translate(sResult,E'éèêëàâäïîüûùöôç','eeeeaaaiiuuuooc');
+sResult := translate(sResult,E' $€µ£%.+-/\\!(){}(),;_&|"#''^<>*','');
+
+return upper(sResult);
+
+end;
+$_$;
+
+
+
+COMMENT ON FUNCTION comptaproc.format_account(p_account public.account_type) IS 'format the accounting :
+- upper case
+- remove space and special char.
+';
+
+
+
+CREATE FUNCTION comptaproc.format_quickcode(p_qcode text) RETURNS text
+ LANGUAGE plpgsql
+ AS $_$
+declare
+ tText text;
+BEGIN
+ tText := lower(trim(p_qcode));
+ tText := replace(tText,' ','');
+ tText:= translate(tText,E' $€µ£%+/\\!(){}(),;&|"#''^<>*','');
+ tText := translate(tText,E'éèêëàâäïîüûùöôç','eeeeaaaiiuuuooc');
+
+ return upper(tText);
+END;
+$_$;
+
+
+
+COMMENT ON FUNCTION comptaproc.format_quickcode(p_qcode text) IS 'Put in upper case and remove invalid char';
+
+
+
+CREATE FUNCTION comptaproc.four_upper_letter() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+ new.dc_code=transform_to_code(new.dc_code);
+ new.dc_code:=substr(new.dc_code,1,4);
+ return new;
+END;
+$$;
+
+
+
+COMMENT ON FUNCTION comptaproc.four_upper_letter() IS 'Cut to the 4 first letter in uppercase';
+
+
+
+CREATE FUNCTION comptaproc.get_follow_up_tree(action_gestion_id integer) RETURNS SETOF integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+ i int;
+ x int;
+ e int;
+begin
+ for x in select aga_least
+ from action_gestion_related
+ where
+ aga_greatest = action_gestion_id
+ loop
+ return next x;
+
+ for e in select * from comptaproc.get_follow_up_tree(x)
+ loop
+ return next e;
+ end loop;
+
+ end loop;
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.get_letter_jnt(a bigint) RETURNS bigint
+ LANGUAGE plpgsql
+ AS $$
+declare
+ nResult bigint;
+begin
+ select jl_id into nResult from jnt_letter join letter_deb using (jl_id) where j_id = a;
+ if NOT FOUND then
+ select jl_id into nResult from jnt_letter join letter_cred using (jl_id) where j_id = a;
+ if NOT found then
+ return null;
+ end if;
+ end if;
+return nResult;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.get_menu_dependency(profile_menu_id integer) RETURNS SETOF integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+ i int;
+ x int;
+ e int;
+begin
+ for x in select pm_id,me_code
+ from profile_menu
+ where me_code_dep in (select me_code from profile_menu where pm_id=profile_menu_id)
+ and p_id = (select p_id from profile_menu where pm_id=profile_menu_id)
+ loop
+ return next x;
+
+ for e in select * from comptaproc.get_menu_dependency(x)
+ loop
+ return next e;
+ end loop;
+
+ end loop;
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.get_menu_tree(p_code text, p_profile integer) RETURNS SETOF public.menu_tree
+ LANGUAGE plpgsql
+ AS $$
+declare
+ i menu_tree;
+ e menu_tree;
+ a text;
+ x v_all_menu%ROWTYPE;
+begin
+ for x in select * from v_all_menu where me_code_dep=p_code::text and p_id=p_profile
+ loop
+ if x.me_code_dep is not null then
+ i.code := x.me_code_dep||'/'||x.me_code;
+ else
+ i.code := x.me_code;
+ end if;
+
+ i.description := x.me_description;
+
+ return next i;
+
+ for e in select * from get_menu_tree(x.me_code,p_profile)
+ loop
+ e.code:=x.me_code_dep||'/'||e.code;
+ return next e;
+ end loop;
+
+ end loop;
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.get_pcm_tree(source public.account_type) RETURNS SETOF public.account_type
+ LANGUAGE plpgsql
+ AS $$
+declare
+ i account_type;
+ e account_type;
+begin
+ for i in select pcm_val from tmp_pcmn where pcm_val_parent=source
+ loop
+ return next i;
+ for e in select get_pcm_tree from get_pcm_tree(i)
+ loop
+ return next e;
+ end loop;
+
+ end loop;
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.get_profile_menu(p_profile integer) RETURNS SETOF public.menu_tree
+ LANGUAGE plpgsql
+ AS $$
+declare
+ a menu_tree;
+ e menu_tree;
+begin
+for a in select me_code,me_description from v_all_menu where p_id=p_profile
+ and me_code_dep is null and me_type <> 'PR' and me_type <>'SP'
+loop
+ return next a;
+
+ for e in select * from get_menu_tree(a.code,p_profile)
+ loop
+ return next e;
+ end loop;
+
+ end loop;
+return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.group_analytic_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+name text;
+begin
+name:=upper(NEW.ga_id);
+name:=trim(name);
+name:=replace(name,' ','');
+NEW.ga_id:=name;
+return NEW;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.group_analytique_del() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+update poste_analytique set ga_id=null
+where ga_id=OLD.ga_id;
+return OLD;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.html_quote(p_string text) RETURNS text
+ LANGUAGE plpgsql
+ AS $$
+declare
+ r text;
+begin
+ r:=p_string;
+ r:=replace(r,'<','<');
+ r:=replace(r,'>','>');
+ r:=replace(r,'''','"');
+ return r;
+end;$$;
+
+
+
+COMMENT ON FUNCTION comptaproc.html_quote(p_string text) IS 'remove harmfull HTML char';
+
+
+
+CREATE FUNCTION comptaproc.info_def_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ row_info_def info_def%ROWTYPE;
+ str_type text;
+begin
+row_info_def:=NEW;
+str_type:=upper(trim(NEW.id_type));
+str_type:=replace(str_type,' ','');
+str_type:=replace(str_type,',','');
+str_type:=replace(str_type,';','');
+if length(str_type) =0 then
+ raise exception 'id_type cannot be null';
+end if;
+row_info_def.id_type:=str_type;
+return row_info_def;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.insert_jrnx(p_date character varying, p_montant numeric, p_poste public.account_type, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text, p_comment text) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+begin
+ insert into jrnx
+ (
+ j_date,
+ j_montant,
+ j_poste,
+ j_grpt,
+ j_jrn_def,
+ j_debit,
+ j_text,
+ j_tech_user,
+ j_tech_per,
+ j_qcode
+ ) values
+ (
+ to_date(p_date,'DD.MM.YYYY'),
+ p_montant,
+ p_poste,
+ p_grpt,
+ p_jrn_def,
+ p_debit,
+ p_comment,
+ p_tech_user,
+ p_tech_per,
+ p_qcode
+ );
+
+return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.insert_quant_purchase(p_internal text, p_j_id numeric, p_fiche character varying, p_quant numeric, p_price numeric, p_vat numeric, p_vat_code integer, p_nd_amount numeric, p_nd_tva numeric, p_nd_tva_recup numeric, p_dep_priv numeric, p_client character varying, p_tva_sided numeric, p_price_unit numeric) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+ account_priv account_type;
+ fid_good_account account_type;
+ n_dep_priv numeric;
+begin
+ n_dep_priv := p_dep_priv;
+ select p_value into account_priv from parm_code where p_code='DEP_PRIV';
+ select f_id into fid_client from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_client));
+ select f_id into fid_good from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_fiche));
+ select ad_value into fid_good_account from fiche_detail where ad_id=5 and f_id=fid_good;
+ if strpos( fid_good_account , account_priv ) = 1 then
+ n_dep_priv=p_price;
+ end if;
+
+ insert into quant_purchase
+ (qp_internal,
+ j_id,
+ qp_fiche,
+ qp_quantite,
+ qp_price,
+ qp_vat,
+ qp_vat_code,
+ qp_nd_amount,
+ qp_nd_tva,
+ qp_nd_tva_recup,
+ qp_supplier,
+ qp_dep_priv,
+ qp_vat_sided,
+ qp_unit)
+ values
+ (p_internal,
+ p_j_id,
+ fid_good,
+ p_quant,
+ p_price,
+ p_vat,
+ p_vat_code,
+ p_nd_amount,
+ p_nd_tva,
+ p_nd_tva_recup,
+ fid_client,
+ n_dep_priv,
+ p_tva_sided,
+ p_price_unit);
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.insert_quant_sold(p_internal text, p_jid numeric, p_fiche character varying, p_quant numeric, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying, p_tva_sided numeric, p_price_unit numeric) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+begin
+
+ select f_id into fid_client from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_client));
+ select f_id into fid_good from
+ fiche_detail where ad_id=23 and ad_value=upper(trim(p_fiche));
+ insert into quant_sold
+ (qs_internal,j_id,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client,qs_valid,qs_vat_sided,qs_unit)
+ values
+ (p_internal,p_jid,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client,'Y',p_tva_sided,p_price_unit);
+ return;
+end;
+ $$;
+
+
+
+CREATE FUNCTION comptaproc.insert_quick_code(nf_id integer, tav_text text) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+declare
+ ns integer;
+ nExist integer;
+ tText text;
+ tBase text;
+ tName text;
+ nCount Integer;
+ nDuplicate Integer;
+begin
+ tText := comptaproc.format_quickcode(tav_text);
+ nDuplicate := 0;
+ tBase := tText;
+ -- take the next sequence
+ select nextval('s_jnt_fic_att_value') into ns;
+ loop
+ if length (tText) = 0 or tText is null then
+ select count(*) into nCount from fiche_detail where f_id=nf_id and ad_id=1;
+ if nCount = 0 then
+ tBase := 'CRD';
+ else
+ select ad_value into tName from fiche_detail where f_id=nf_id and ad_id=1;
+ tName := comptaproc.format_quickcode(tName);
+ tName := substr(tName,1,6);
+ tBase := tName;
+ if nDuplicate = 0 then
+ tText := tName;
+ else
+ tText := tBase||nDuplicate::text;
+ end if;
+ end if;
+ end if;
+ if coalesce(tText,'') = '' then
+ tText := 'CRD';
+ end if;
+ -- av_text already used ?
+ select count(*) into nExist
+ from fiche_detail
+ where
+ ad_id=23 and ad_value=tText;
+
+ if nExist = 0 then
+ exit;
+ end if;
+ nDuplicate := nDuplicate + 1 ;
+ tText := tBase || nDuplicate::text;
+
+ if nDuplicate > 99999 then
+ raise Exception 'too many duplicate % duplicate# %',tText,nDuplicate;
+ end if;
+ end loop;
+
+
+ insert into fiche_detail(jft_id,f_id,ad_id,ad_value) values (ns,nf_id,23,upper(tText));
+ return ns;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.is_closed(p_periode integer, p_jrn_def_id integer) RETURNS boolean
+ LANGUAGE plpgsql
+ AS $$
+declare
+bClosed bool;
+str_status text;
+begin
+select p_closed into bClosed from parm_periode
+ where p_id=p_periode;
+
+if bClosed = true then
+ return bClosed;
+end if;
+
+select status into str_status from jrn_periode
+ where p_id =p_periode and jrn_def_id=p_jrn_def_id;
+
+if str_status <> 'OP' then
+ return bClosed;
+end if;
+return false;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jnt_fic_attr_ins() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ r_record jnt_fic_attr%ROWTYPE;
+ i_max integer;
+begin
+r_record=NEW;
+perform comptaproc.fiche_attribut_synchro(r_record.fd_id);
+select coalesce(max(jnt_order),0) into i_max from jnt_fic_attr where fd_id=r_record.fd_id;
+i_max := i_max + 10;
+NEW.jnt_order=i_max;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_add_note(p_jrid bigint, p_note text) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ tmp bigint;
+begin
+ if length(trim(p_note)) = 0 then
+ delete from jrn_note where jr_id= p_jrid;
+ return;
+ end if;
+
+ select n_id into tmp from jrn_note where jr_id = p_jrid;
+
+ if FOUND then
+ update jrn_note set n_text=trim(p_note) where jr_id = p_jrid;
+ else
+ insert into jrn_note (jr_id,n_text) values ( p_jrid, p_note);
+
+ end if;
+
+ return;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_check_periode() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+bClosed bool;
+str_status text;
+ljr_tech_per jrn.jr_tech_per%TYPE;
+ljr_def_id jrn.jr_def_id%TYPE;
+lreturn jrn%ROWTYPE;
+begin
+if TG_OP='UPDATE' then
+ ljr_tech_per :=OLD.jr_tech_per ;
+ NEW.jr_tech_per := comptaproc.find_periode(to_char(NEW.jr_date,'DD.MM.YYYY'));
+ ljr_def_id :=OLD.jr_def_id;
+ lreturn :=NEW;
+ if NEW.jr_date = OLD.jr_date then
+ return NEW;
+ end if;
+ if comptaproc.is_closed(NEW.jr_tech_per,NEW.jr_def_id) = true then
+ raise exception 'Periode fermee';
+ end if;
+end if;
+
+if TG_OP='INSERT' then
+ NEW.jr_tech_per := comptaproc.find_periode(to_char(NEW.jr_date,'DD.MM.YYYY'));
+ ljr_tech_per :=NEW.jr_tech_per ;
+ ljr_def_id :=NEW.jr_def_id;
+ lreturn :=NEW;
+end if;
+
+if TG_OP='DELETE' then
+ ljr_tech_per :=OLD.jr_tech_per;
+ ljr_def_id :=OLD.jr_def_id;
+ lreturn :=OLD;
+end if;
+
+if comptaproc.is_closed (ljr_tech_per,ljr_def_id) = true then
+ raise exception 'Periode fermee';
+end if;
+
+return lreturn;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_currency() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+ if new.currency_id is null then
+ new.currency_id := 0;
+ new.currency_rate := 1;
+ new.currency_rate_ref := 1;
+ end if;
+ return new;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_def_add() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$begin
+execute 'insert into jrn_periode(p_id,jrn_def_id,status) select p_id,'||NEW.jrn_def_id||',
+ case when p_central=true then ''CE''
+ when p_closed=true then ''CL''
+ else ''OP''
+ end
+from
+parm_periode ';
+return NEW;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_def_delete() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+nb numeric;
+begin
+select count(*) into nb from jrn where jr_def_id=OLD.jrn_def_id;
+
+if nb <> 0 then
+ raise exception 'EFFACEMENT INTERDIT: JOURNAL UTILISE';
+end if;
+return OLD;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.jrn_del() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+row jrn%ROWTYPE;
+begin
+row:=OLD;
+insert into del_jrn ( jr_id,
+ jr_def_id,
+ jr_montant,
+ jr_comment,
+ jr_date,
+ jr_grpt_id,
+ jr_internal,
+ jr_tech_date,
+ jr_tech_per,
+ jrn_ech,
+ jr_ech,
+ jr_rapt,
+ jr_valid,
+ jr_opid,
+ jr_c_opid,
+ jr_pj,
+ jr_pj_name,
+ jr_pj_type,
+ jr_pj_number,
+ del_jrn_date)
+ select jr_id,
+ jr_def_id,
+ jr_montant,
+ jr_comment,
+ jr_date,
+ jr_grpt_id,
+ jr_internal,
+ jr_tech_date,
+ jr_tech_per,
+ jrn_ech,
+ jr_ech,
+ jr_rapt,
+ jr_valid,
+ jr_opid,
+ jr_c_opid,
+ jr_pj,
+ jr_pj_name,
+ jr_pj_type,
+ jr_pj_number
+ ,now() from jrn where jr_id=row.jr_id;
+return row;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrnx_del() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+row jrnx%ROWTYPE;
+begin
+row:=OLD;
+
+
+insert into del_jrnx(
+ j_id, j_date, j_montant, j_poste, j_grpt, j_rapt, j_jrn_def,
+ j_debit, j_text, j_centralized, j_internal, j_tech_user, j_tech_date,
+ j_tech_per, j_qcode, f_id) SELECT j_id, j_date, j_montant, j_poste, j_grpt, j_rapt, j_jrn_def,
+ j_debit, j_text, j_centralized, j_internal, j_tech_user, j_tech_date,
+ j_tech_per, j_qcode, f_id from jrnx where j_id=row.j_id;
+return row;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrnx_ins() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+n_fid bigint;
+nCount integer;
+sQcode text;
+
+begin
+n_fid := NULL;
+sQcode := NULL;
+
+NEW.j_tech_per := comptaproc.find_periode(to_char(NEW.j_date,'DD.MM.YYYY'));
+if NEW.j_tech_per = -1 then
+ raise exception 'Période invalide';
+end if;
+
+if trim(coalesce(NEW.j_qcode,'')) = '' then
+ -- how many card has this accounting
+ select count(*) into nCount from fiche_detail where ad_id=5 and ad_value=NEW.j_poste;
+ -- only one card is found , then we change the j_qcode by the card
+ if nCount = 1 then
+ select f_id into n_fid from fiche_detail where ad_id = 5 and ad_value=NEW.j_poste;
+ if FOUND then
+ select ad_value into sQcode from fiche_detail where f_id=n_fid and ad_id = 23;
+ NEW.f_id := n_fid;
+ NEW.j_qcode = sQcode;
+ raise info 'comptaproc.jrnx_ins : found card % qcode %',n_fid,sQcode;
+ end if;
+ end if;
+
+end if;
+
+NEW.j_qcode=trim(upper(coalesce(NEW.j_qcode,'')));
+
+if length (coalesce(NEW.j_qcode,'')) = 0 then
+ NEW.j_qcode=NULL;
+ else
+ select f_id into n_fid from fiche_detail where ad_id=23 and ad_value=NEW.j_qcode;
+ if NOT FOUND then
+ raise exception 'La fiche dont le quick code est % n''existe pas',NEW.j_qcode;
+ end if;
+end if;
+
+NEW.f_id:=n_fid;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.jrnx_letter_del() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+row jrnx%ROWTYPE;
+begin
+row:=OLD;
+delete from jnt_letter
+ where (jl_id in (select jl_id from letter_deb) and jl_id not in(select jl_id from letter_cred ))
+ or (jl_id not in (select jl_id from letter_deb ) and jl_id in(select jl_id from letter_cred ));
+return row;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.menu_complete_dependency(n_profile numeric) RETURNS void
+ LANGUAGE plpgsql
+ AS $$
+declare
+ n_count integer;
+ csr_root_menu cursor (p_profile numeric) is select pm_id,
+ me_code,
+ me_code_dep
+
+ from profile_menu
+ where
+ me_code in
+ (select a.me_code_dep
+ from profile_menu as a
+ join profile_menu as b on (a.me_code=b.me_code and a.me_code_dep=b.me_code_dep and a.pm_id <> b.pm_id and a.p_id=b.p_id)
+ where a.p_id=n_profile)
+ and p_id=p_profile;
+
+begin
+ for duplicate in csr_root_menu(n_profile)
+ loop
+ raise notice 'found %',duplicate;
+ update profile_menu set pm_id_dep = duplicate.pm_id
+ where pm_id in (select a.pm_id
+ from profile_menu as a
+ left join profile_menu as b on (a.me_code=b.me_code and a.me_code_dep=b.me_code_dep)
+ where
+ a.p_id=n_profile
+ and b.p_id=n_profile
+ and a.pm_id_dep is null
+ and a.me_code_dep = duplicate.me_code
+ and a.pm_id < b.pm_id);
+ end loop;
+
+ for duplicate in csr_root_menu(n_profile)
+ loop
+ select count(*) into n_count from profile_menu where p_id=n_profile and pm_id_dep = duplicate.pm_id;
+ raise notice '% use % times',duplicate,n_count;
+ if n_count = 0 then
+ raise notice ' Update with %',duplicate;
+ update profile_menu set pm_id_dep = duplicate.pm_id where p_id = n_profile and me_code_dep = duplicate.me_code and pm_id_dep is null;
+ end if;
+
+ end loop;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.opd_limit_description() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ declare
+ sDescription text;
+ begin
+ sDescription := NEW.od_description;
+ NEW.od_description := substr(sDescription,1,80);
+ return NEW;
+ end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.periode_exist(p_date text, p_periode_id bigint) RETURNS integer
+ LANGUAGE plpgsql
+ AS $$
+
+declare n_p_id int4;
+begin
+
+select p_id into n_p_id
+ from parm_periode
+ where
+ p_start <= to_date(p_date,'DD.MM.YYYY')
+ and
+ p_end >= to_date(p_date,'DD.MM.YYYY')
+ and
+ p_id <> p_periode_id;
+
+if NOT FOUND then
+ return -1;
+end if;
+
+return n_p_id;
+
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.plan_analytic_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ name text;
+begin
+ name:=upper(NEW.pa_name);
+ name:=trim(name);
+ name:=replace(name,' ','');
+ NEW.pa_name:=name;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.poste_analytique_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$declare
+name text;
+rCount record;
+
+begin
+name:=upper(NEW.po_name);
+name:=trim(name);
+name:=replace(name,' ','');
+NEW.po_name:=name;
+
+if NEW.ga_id is NULL then
+return NEW;
+end if;
+
+if length(trim(NEW.ga_id)) = 0 then
+ NEW.ga_id:=NULL;
+ return NEW;
+end if;
+perform 'select ga_id from groupe_analytique where ga_id='||NEW.ga_id;
+if NOT FOUND then
+ raise exception' Inexistent Group Analytic %',NEW.ga_id;
+end if;
+return NEW;
+end;$$;
+
+
+
+CREATE FUNCTION comptaproc.proc_check_balance() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ diff numeric;
+ tt integer;
+begin
+ if TG_OP = 'INSERT' or TG_OP='UPDATE' then
+ tt=NEW.jr_grpt_id;
+ diff:=check_balance(tt);
+ if diff != 0 then
+ raise exception 'balance error %',diff ;
+ end if;
+ return NEW;
+ end if;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.quant_purchase_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ begin
+ if NEW.qp_price < 0 OR NEW.qp_quantite <0 THEN
+ NEW.qp_price := abs (NEW.qp_price)*(-1);
+ NEW.qp_quantite := abs (NEW.qp_quantite)*(-1);
+ end if;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.quant_sold_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ begin
+ if NEW.qs_price < 0 OR NEW.qs_quantite <0 THEN
+ NEW.qs_price := abs (NEW.qs_price)*(-1);
+ NEW.qs_quantite := abs (NEW.qs_quantite)*(-1);
+ end if;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.set_tech_user() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ /* variable */
+ noalyss_user text;
+begin
+ new.tech_user := current_setting('noalyss.user_login');
+ new.tech_date := now();
+ return NEW;
+
+exception when others then
+ new.tech_date := now();
+ new.tech_user := current_user;
+ return NEW;
+end ;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_document_modele_validate() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ lText text;
+ modified document_modele%ROWTYPE;
+begin
+ modified:=NEW;
+
+ modified.md_filename:=replace(NEW.md_filename,' ','_');
+ return modified;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_document_type_insert() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+nCounter integer;
+ BEGIN
+select count(*) into nCounter from pg_class where relname='seq_doc_type_'||NEW.dt_id;
+if nCounter = 0 then
+ execute 'create sequence seq_doc_type_'||NEW.dt_id;
+end if;
+ RETURN NEW;
+ END;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_document_validate() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ lText text;
+ modified document%ROWTYPE;
+begin
+ modified:=NEW;
+ modified.d_filename:=replace(NEW.d_filename,' ','_');
+ return modified;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_jrn_def_description() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+ declare
+ str varchar(200);
+ BEGIN
+ str := substr(NEW.jrn_def_description,1,200);
+ NEW.jrn_def_description := str;
+
+ RETURN NEW;
+ END;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_jrn_def_sequence() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+nCounter integer;
+
+ BEGIN
+ select count(*) into nCounter
+ from pg_class where relname='s_jrn_'||NEW.jrn_def_id;
+ if nCounter = 0 then
+ execute 'create sequence s_jrn_'||NEW.jrn_def_id;
+ raise notice 'Creating sequence s_jrn_%',NEW.jrn_def_id;
+ end if;
+
+ RETURN NEW;
+ END;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.t_parameter_extra_code() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+begin
+ new.pe_code := comptaproc.transform_to_code (new.pe_code);
+ return new;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.tmp_pcmn_alphanum_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ r_record tmp_pcmn%ROWTYPE;
+begin
+r_record := NEW;
+r_record.pcm_val:=format_account(NEW.pcm_val);
+
+return r_record;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.tmp_pcmn_ins() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+declare
+ r_record tmp_pcmn%ROWTYPE;
+begin
+r_record := NEW;
+if length(trim(r_record.pcm_type))=0 or r_record.pcm_type is NULL then
+ r_record.pcm_type:=find_pcm_type(NEW.pcm_val);
+ return r_record;
+end if;
+return NEW;
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.transform_to_code(p_account text) RETURNS text
+ LANGUAGE plpgsql
+ AS $_$
+
+declare
+
+sResult text;
+
+begin
+sResult := lower(p_account);
+
+sResult := translate(sResult,E'éèêëàâäïîüûùöôç','eeeeaaaiiuuuooc');
+sResult := translate(sResult,E' $€µ£%.+-/\\!(){}(),;&|"#''^<>*','');
+
+return upper(sResult);
+
+end;
+$_$;
+
+
+
+CREATE FUNCTION comptaproc.trg_profile_user_ins_upd() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+
+begin
+
+NEW.user_name := lower(NEW.user_name);
+return NEW;
+
+end;
+$$;
+
+
+
+CREATE FUNCTION comptaproc.trg_remove_script_tag() RETURNS trigger
+ LANGUAGE plpgsql
+ AS $$
+
+begin
+
+ NEW.agc_comment_raw:= regexp_replace(NEW.agc_comment_raw, '