diff --git a/html/admin/setup.php b/html/admin/setup.php
index 3edce2783..fda904ae4 100644
--- a/html/admin/setup.php
+++ b/html/admin/setup.php
@@ -319,23 +319,30 @@ if ($account == 0 ) {
if ( DEBUG=='false') ob_start();
ExecSql($cn,"create database ".domaine."account_repository encoding='latin1'");
$cn=DbConnect();
+ StartSql($cn);
ExecuteScript($cn,"sql/account_repository/schema.sql");
ExecuteScript($cn,"sql/account_repository/data.sql");
+ Commit($cn);
if ( DEBUG=='false') ob_end_clean();
echo "Creation of Démo";
if ( DEBUG=='false') ob_start();
ExecSql($cn,"create database ".domaine."dossier1 encoding='latin1'");
$cn=DbConnect(1,'dossier');
+ StartSql($cn);
ExecuteScript($cn,'sql/dossier1/schema.sql');
ExecuteScript($cn,'sql/dossier1/data.sql');
+ Commit($cn);
+
if ( DEBUG=='false') ob_end_clean();
echo "Creation of Modele1";
if ( DEBUG=='false') ob_start();
ExecSql($cn,"create database ".domaine."mod1 encoding='latin1'");
$cn=DbConnect(1,'mod');
+ StartSql($cn);
ExecuteScript($cn,'sql/mod1/schema.sql');
ExecuteScript($cn,'sql/mod1/data.sql');
+ Commit($cn);
if ( DEBUG=='false') ob_end_clean();
}// end if
// Add a french accountancy model
@@ -453,6 +460,10 @@ if ( DEBUG=='false' ) ob_start();
ExecuteScript($db,'sql/patch/upgrade13.sql');
} // version
+ if ( GetVersion($db) == 14 ) {
+ ExecuteScript($db,'sql/patch/upgrade14.sql');
+ } // version
+
if ( DEBUG == 'false') ob_end_clean();
}//for
@@ -527,6 +538,9 @@ if (DEBUG == 'false' ) ob_start();
if ( GetVersion($db) == 13 ) {
ExecuteScript($db,'sql/patch/upgrade13.sql');
} // version
+ if ( GetVersion($db) == 14 ) {
+ ExecuteScript($db,'sql/patch/upgrade14.sql');
+ } // version
if ( DEBUG == 'false') ob_end_clean();
}
diff --git a/html/admin/sql/dossier1/data.sql b/html/admin/sql/dossier1/data.sql
index 70f0ab4ee..e292ad52f 100644
--- a/html/admin/sql/dossier1/data.sql
+++ b/html/admin/sql/dossier1/data.sql
@@ -1694,7 +1694,7 @@ INSERT INTO user_sec_jrn (uj_id, uj_login, uj_jrn_id, uj_priv) VALUES (8, 'dany'
-- Data for Name: version; Type: TABLE DATA; Schema: public; Owner: phpcompta
--
-INSERT INTO version (val) VALUES (14);
+INSERT INTO version (val) VALUES (15);
--
diff --git a/html/admin/sql/dossier1/schema.sql b/html/admin/sql/dossier1/schema.sql
index e973683c1..ddd80971c 100644
--- a/html/admin/sql/dossier1/schema.sql
+++ b/html/admin/sql/dossier1/schema.sql
@@ -3,545 +3,6 @@ SET check_function_bodies = false;
SET client_min_messages = warning;
SET search_path = public, pg_catalog;
CREATE DOMAIN poste_comptable AS numeric(25,0);
-ALTER DOMAIN public.poste_comptable OWNER TO phpcompta;
-CREATE FUNCTION account_add(p_id poste_comptable, p_name character varying) RETURNS void
- AS $$
-declare
- nParent tmp_pcmn.pcm_val_parent%type;
- nCount integer;
-begin
- select count(*) into nCount from tmp_pcmn where pcm_val=p_id;
- 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);
- end if;
-return;
-end ;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_add(p_id poste_comptable, p_name character varying) OWNER TO phpcompta;
-CREATE FUNCTION account_auto(p_fd_id integer) RETURNS boolean
- 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;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_auto(p_fd_id integer) OWNER TO phpcompta;
-CREATE FUNCTION account_compute(p_f_id integer) RETURNS poste_comptable
- AS $$
-declare
- class_base poste_comptable;
- maxcode int8;
-begin
- select fd_class_base into class_base
- from
- fiche_def join fiche using (fd_id)
- where
- f_id=p_f_id;
- raise notice 'class base %',class_base;
- select max(pcm_val) into maxcode from tmp_pcmn where pcm_val = class_base;
- if maxcode = class_base then
- maxcode=class_base*1000+1;
- end if;
- raise notice 'Max code %',maxcode;
-return maxcode+1;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_compute(p_f_id integer) OWNER TO phpcompta;
-CREATE FUNCTION account_insert(p_f_id integer, p_account poste_comptable) RETURNS integer
- AS $$
-declare
-nParent tmp_pcmn.pcm_val_parent%type;
-sName varchar;
-nNew tmp_pcmn.pcm_val%type;
-bAuto bool;
-nFd_id integer;
-nCount integer;
-begin
-
- if length(trim(p_account)) != 0 then
- select * into nCount from tmp_pcmn where pcm_val=p_account;
- if nCount !=0 then
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
- nParent:=account_parent(p_account);
- insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent)
- values (p_account,sName,nParent);
- perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
-
- end if;
- else
- select fd_id into nFd_id from fiche where f_id=p_f_id;
- bAuto:= account_auto(nFd_id);
- if bAuto = true then
- nNew:=account_compute(p_f_id);
-raise debug 'nNew %', nNew;
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
- nParent:=account_parent(nNew);
- perform account_add (nNew,sName);
- perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
-
- else
- perform attribut_insert(p_f_id,5,null);
- end if;
- end if;
-
-return 0;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_insert(p_f_id integer, p_account poste_comptable) OWNER TO phpcompta;
-CREATE FUNCTION account_parent(p_account poste_comptable) RETURNS poste_comptable
- AS $$
-declare
- nParent tmp_pcmn.pcm_val_parent%type;
- sParent varchar;
- nCount integer;
-begin
- sParent:=to_char(p_account,'9999999999999999');
- sParent:=trim(sParent);
- nParent:=0;
- while nParent = 0 loop
- select count(*) into nCount
- from tmp_pcmn
- where
- pcm_val = to_number(sParent,'9999999999999999');
- if nCount != 0 then
- nParent:=to_number(sParent,'9999999999999999');
- end if;
- sParent:= substr(sParent,1,length(sParent)-1);
- if length(sParent) <= 0 then
- raise exception 'Impossible de trouver le compte parent pour %',p_account;
- end if;
- end loop;
- return nParent;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_parent(p_account poste_comptable) OWNER TO phpcompta;
-CREATE FUNCTION account_update(p_f_id integer, p_account poste_comptable) RETURNS integer
- AS $$
-declare
-nMax fiche.f_id%type;
-nCount integer;
-nParent tmp_pcmn.pcm_val_parent%type;
-sName varchar;
-nJft_id attr_value.jft_id%type;
-begin
-
- if length(trim(p_account)) != 0 then
- select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
- if nCount = 0 then
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
- nParent:=fiche_account_parent(p_f_id);
- insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
- end if;
- end if;
- select jft_id into njft_id from jnt_fic_att_value where f_id=p_f_id and ad_id=5;
- update attr_value set av_text=p_account where jft_id=njft_id;
-
-return njft_id;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.account_update(p_f_id integer, p_account poste_comptable) OWNER TO phpcompta;
-CREATE FUNCTION attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
- AS $$
-declare
- n_jft_id integer;
-begin
- select nextval('s_jnt_fic_att_value') into n_jft_id;
- insert into jnt_fic_att_value (jft_id,f_id,ad_id) values (n_jft_id,p_f_id,p_ad_id);
- insert into attr_value (jft_id,av_text) values (n_jft_id,p_value);
-return;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) OWNER TO phpcompta;
-CREATE FUNCTION card_class_base(p_f_id integer) RETURNS fiche_def.fd_class_base%type
- 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 nposte;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.card_class_base(p_f_id integer) OWNER TO phpcompta;
-CREATE FUNCTION check_balance(p_grpt integer) RETURNS numeric
- AS $$
-declare
- amount_jrnx_debit numeric;
- amount_jrnx_credit numeric;
- amount_jrn numeric;
-begin
- select sum (j_montant) into amount_jrnx_credit
- from jrnx
- where
- j_grpt=p_grpt
- and j_debit=false;
- select sum (j_montant) into amount_jrnx_debit
- from jrnx
- where
- j_grpt=p_grpt
- and j_debit=true;
- select jr_montant 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;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.check_balance(p_grpt integer) OWNER TO phpcompta;
-CREATE FUNCTION fiche_account_parent(p_f_id integer) RETURNS poste_comptable
- AS $$
-declare
-ret poste_comptable;
-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;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.fiche_account_parent(p_f_id integer) OWNER TO phpcompta;
-CREATE FUNCTION insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) RETURNS void
- AS $$
-declare
- sCode varchar;
- nCount_qcode integer;
-begin
- sCode=trim(p_qcode);
- if length(sCode) = 0 or p_qcode is null then
- select count(*) into nCount_qcode
- from vw_poste_qcode where j_poste=p_poste;
- if nCount_qcode = 1 then
- select j_qcode into sCode
- from vw_poste_qcode where j_poste=p_poste;
- else
- sCode=NULL;
- end if;
-
- end if;
- if p_montant = 0.0 then
- return;
- end if;
- insert into jrnx
- (
- j_date,
- j_montant,
- j_poste,
- j_grpt,
- j_jrn_def,
- j_debit,
- 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_tech_user,
- p_tech_per,
- sCode
- );
-return;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) OWNER TO phpcompta;
-CREATE FUNCTION insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) RETURNS void
- AS $$
-declare
- fid_client integer;
- fid_good integer;
-begin
- select f_id into fid_client from
- attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_client;
- select f_id into fid_good from
- attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_fiche;
- insert into quant_sold
- (qs_internal,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client)
- values
- (p_internal,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client);
- return;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) OWNER TO phpcompta;
-CREATE FUNCTION insert_quick_code(nf_id integer, tav_text text) RETURNS integer
- AS $$
- declare
- ns integer;
- nExist integer;
- tText text;
- begin
- tText := upper(trim(tav_text));
- tText := replace(tText,' ','');
-
- loop
- select nextval('s_jnt_fic_att_value') into ns;
- if length (tText) = 0 or tText is null then
- tText := 'FID'||ns;
- end if;
- select count(*) into nExist
- from jnt_fic_att_value join attr_value using (jft_id)
- where
- ad_id=23 and av_text=upper(tText);
- if nExist = 0 then
- exit;
- end if;
- tText:='FID'||ns;
- end loop;
- insert into jnt_fic_att_value values (ns,nf_id,23);
- insert into attr_value values (ns,upper(tText));
- return ns;
- end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.insert_quick_code(nf_id integer, tav_text text) OWNER TO phpcompta;
-CREATE FUNCTION proc_check_balance() RETURNS "trigger"
- AS $$
-declare
- diff numeric;
- tt integer;
-begin
- if TG_OP = 'INSERT' 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;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.proc_check_balance() OWNER TO phpcompta;
-CREATE FUNCTION t_document_type_insert() RETURNS "trigger"
- AS $$
- BEGIN
- execute 'create sequence seq_doc_type_'||NEW.dt_id;
-raise notice 'Creating sequence seq_doc_type_%',NEW.dt_id;
- RETURN NEW;
- END;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.t_document_type_insert() OWNER TO phpcompta;
-CREATE FUNCTION t_jrn_def_sequence() RETURNS "trigger"
- AS $$
- BEGIN
- execute 'create sequence s_jrn_'||NEW.jrn_def_id;
-raise notice 'Creating sequence s_jrn_%',NEW.jrn_def_id;
- RETURN NEW;
- END;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.t_jrn_def_sequence() OWNER TO phpcompta;
-CREATE FUNCTION trim_cvs_quote() RETURNS "trigger"
- AS $$
-declare
- modified import_tmp%ROWTYPE;
-begin
- modified.code=new.code;
- modified.montant=new.montant;
- modified.date_exec=new.date_exec;
- modified.date_valeur=new.date_valeur;
- modified.devise=replace(new.devise,'"','');
- modified.poste_comptable=replace(new.poste_comptable,'"','');
- modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
- modified.detail=replace(NEW.DETAIL,'"','');
- modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
- modified.bq_account=NEW.bq_account;
- modified.jrn=NEW.jrn;
- modified.ok=new.ok;
- return modified;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.trim_cvs_quote() OWNER TO phpcompta;
-CREATE FUNCTION trim_space_format_csv_banque() RETURNS "trigger"
- AS $$
-declare
- modified format_csv_banque%ROWTYPE;
-begin
- modified.name=trim(NEW.NAME);
- modified.include_file=trim(new.include_file);
- if ( length(modified.name) = 0 ) then
- modified.name=null;
- end if;
- if ( length(modified.include_file) = 0 ) then
- modified.include_file=null;
- end if;
- return modified;
-end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.trim_space_format_csv_banque() OWNER TO phpcompta;
-CREATE FUNCTION tva_delete(integer) RETURNS void
- AS $_$
-declare
- p_tva_id alias for $1;
- nCount integer;
-begin
- nCount=0;
- select count(*) into nCount from quant_sold where qs_vat_code=p_tva_id;
- if nCount = 0 then
- delete from tva_rate where tva_id=p_tva_id;
- end if;
- return;
-end;
-$_$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.tva_delete(integer) OWNER TO phpcompta;
-CREATE FUNCTION tva_insert(integer, text, numeric, text, text) RETURNS integer
- AS $_$
-declare
-p_tva_id alias for $1;
-p_tva_label alias for $2;
-p_tva_rate alias for $3;
-p_tva_comment alias for $4;
-p_tva_poste alias for $5;
-debit text;
-credit text;
-nCount integer;
-begin
-if length(trim(p_tva_label)) = 0 then
- return 3;
-end if;
-select count(*) into nCount from tva_rate
- where tva_id=p_tva_id;
-if nCount != 0 then
- return 5;
-end if;
-if length(trim(p_tva_poste)) != 0 then
- if position (',' in p_tva_poste) = 0 then return 4; end if;
- debit = split_part(p_tva_poste,',',1);
- credit = split_part(p_tva_poste,',',2);
- select count(*) into nCount from tmp_pcmn where pcm_val=debit;
- if nCount = 0 then return 4; end if;
- select count(*) into nCount from tmp_pcmn where pcm_val=credit;
- if nCount = 0 then return 4; end if;
-
-end if;
-insert into tva_rate(tva_id,tva_label,tva_rate,tva_comment,tva_poste)
- values (p_tva_id,p_tva_label,p_tva_rate,p_tva_comment,p_tva_poste);
-return 0;
-end;
-$_$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.tva_insert(integer, text, numeric, text, text) OWNER TO phpcompta;
-CREATE FUNCTION tva_modify(integer, text, numeric, text, text) RETURNS integer
- AS $_$declare
-p_tva_id alias for $1;
-p_tva_label alias for $2;
-p_tva_rate alias for $3;
-p_tva_comment alias for $4;
-p_tva_poste alias for $5;
-debit text;
-credit text;
-nCount integer;
-begin
-if length(trim(p_tva_label)) = 0 then
- return 3;
-end if;
-if length(trim(p_tva_poste)) != 0 then
- if position (',' in p_tva_poste) = 0 then return 4; end if;
- debit = split_part(p_tva_poste,',',1);
- credit = split_part(p_tva_poste,',',2);
- select count(*) into nCount from tmp_pcmn where pcm_val=debit;
- if nCount = 0 then return 4; end if;
- select count(*) into nCount from tmp_pcmn where pcm_val=credit;
- if nCount = 0 then return 4; end if;
-
-end if;
-update tva_rate set tva_label=p_tva_label,tva_rate=p_tva_rate,tva_comment=p_tva_comment,tva_poste=p_tva_poste
- where tva_id=p_tva_id;
-return 0;
-end;
-$_$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.tva_modify(integer, text, numeric, text, text) OWNER TO phpcompta;
-CREATE FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer
- AS $$
- declare
- ns integer;
- nExist integer;
- tText text;
- old_qcode varchar;
- begin
- select av_text into old_qcode from attr_value where jft_id=njft_id;
- if tav_text = upper( trim(old_qcode)) then
- return 0;
- end if;
-
- tText := trim(upper(tav_text));
- tText := replace(tText,' ','');
- if length ( tText) = 0 or tText is null then
- return 0;
- end if;
-
- ns := njft_id;
- loop
- select count(*) into nExist
- from jnt_fic_att_value join attr_value using (jft_id)
- where
- ad_id=23 and av_text=upper(tText);
- if nExist = 0 then
- exit;
- end if;
- if tText = 'FID'||ns then
- select nextval('s_jnt_fic_att_value') into ns;
- end if;
- tText :='FID'||ns;
-
- end loop;
- update attr_value set av_text = tText where jft_id=njft_id;
- update jrnx set j_qcode=tText where j_qcode = old_qcode;
- return ns;
- end;
-$$
- LANGUAGE plpgsql;
-ALTER FUNCTION public.update_quick_code(njft_id integer, tav_text text) OWNER TO phpcompta;
-SET default_tablespace = '';
-SET default_with_oids = true;
CREATE TABLE "action" (
ac_id integer NOT NULL,
ac_description text NOT NULL
@@ -674,9 +135,10 @@ CREATE TABLE import_tmp (
detail text,
num_compte text,
poste_comptable text,
- ok boolean DEFAULT false,
+ status char(1) default 'w' not null check (status in ('t','d','w')),
bq_account integer NOT NULL,
jrn integer NOT NULL
+
);
CREATE TABLE invoice (
iv_id integer DEFAULT nextval('s_invoice'::text) NOT NULL,
@@ -1054,6 +516,538 @@ CREATE INDEX x_poste ON jrnx USING btree (j_poste);
CREATE UNIQUE INDEX x_usr_jrn ON user_sec_jrn USING btree (uj_login, uj_jrn_id);
+CREATE FUNCTION account_add(p_id poste_comptable, p_name character varying) RETURNS void
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ nCount integer;
+begin
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_id;
+ 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);
+ end if;
+return;
+end ;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_add(p_id poste_comptable, p_name character varying) OWNER TO phpcompta;
+CREATE FUNCTION account_auto(p_fd_id integer) RETURNS boolean
+ 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;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_auto(p_fd_id integer) OWNER TO phpcompta;
+CREATE FUNCTION account_compute(p_f_id integer) RETURNS poste_comptable
+ AS $$
+declare
+ class_base poste_comptable;
+ maxcode int8;
+begin
+ select fd_class_base into class_base
+ from
+ fiche_def join fiche using (fd_id)
+ where
+ f_id=p_f_id;
+ raise notice 'class base %',class_base;
+ select max(pcm_val) into maxcode from tmp_pcmn where pcm_val = class_base;
+ if maxcode = class_base then
+ maxcode=class_base*1000+1;
+ end if;
+ raise notice 'Max code %',maxcode;
+return maxcode+1;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_compute(p_f_id integer) OWNER TO phpcompta;
+CREATE FUNCTION account_insert(p_f_id integer, p_account poste_comptable) RETURNS integer
+ AS $$
+declare
+nParent tmp_pcmn.pcm_val_parent%type;
+sName varchar;
+nNew tmp_pcmn.pcm_val%type;
+bAuto bool;
+nFd_id integer;
+nCount integer;
+begin
+
+ if length(trim(p_account)) != 0 then
+ select * into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount !=0 then
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=account_parent(p_account);
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent)
+ values (p_account,sName,nParent);
+ perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
+
+ end if;
+ else
+ select fd_id into nFd_id from fiche where f_id=p_f_id;
+ bAuto:= account_auto(nFd_id);
+ if bAuto = true then
+ nNew:=account_compute(p_f_id);
+raise debug 'nNew %', nNew;
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=account_parent(nNew);
+ perform account_add (nNew,sName);
+ perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
+
+ else
+ perform attribut_insert(p_f_id,5,null);
+ end if;
+ end if;
+
+return 0;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_insert(p_f_id integer, p_account poste_comptable) OWNER TO phpcompta;
+CREATE FUNCTION account_parent(p_account poste_comptable) RETURNS poste_comptable
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sParent varchar;
+ nCount integer;
+begin
+ sParent:=to_char(p_account,'9999999999999999');
+ sParent:=trim(sParent);
+ nParent:=0;
+ while nParent = 0 loop
+ select count(*) into nCount
+ from tmp_pcmn
+ where
+ pcm_val = to_number(sParent,'9999999999999999');
+ if nCount != 0 then
+ nParent:=to_number(sParent,'9999999999999999');
+ end if;
+ sParent:= substr(sParent,1,length(sParent)-1);
+ if length(sParent) <= 0 then
+ raise exception 'Impossible de trouver le compte parent pour %',p_account;
+ end if;
+ end loop;
+ return nParent;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_parent(p_account poste_comptable) OWNER TO phpcompta;
+CREATE FUNCTION account_update(p_f_id integer, p_account poste_comptable) RETURNS integer
+ AS $$
+declare
+nMax fiche.f_id%type;
+nCount integer;
+nParent tmp_pcmn.pcm_val_parent%type;
+sName varchar;
+nJft_id attr_value.jft_id%type;
+begin
+
+ if length(trim(p_account)) != 0 then
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount = 0 then
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+ nParent:=fiche_account_parent(p_f_id);
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
+ end if;
+ end if;
+ select jft_id into njft_id from jnt_fic_att_value where f_id=p_f_id and ad_id=5;
+ update attr_value set av_text=p_account where jft_id=njft_id;
+
+return njft_id;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.account_update(p_f_id integer, p_account poste_comptable) OWNER TO phpcompta;
+CREATE FUNCTION attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
+ AS $$
+declare
+ n_jft_id integer;
+begin
+ select nextval('s_jnt_fic_att_value') into n_jft_id;
+ insert into jnt_fic_att_value (jft_id,f_id,ad_id) values (n_jft_id,p_f_id,p_ad_id);
+ insert into attr_value (jft_id,av_text) values (n_jft_id,p_value);
+return;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) OWNER TO phpcompta;
+CREATE FUNCTION card_class_base(p_f_id integer) RETURNS fiche_def.fd_class_base%type
+ 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 nposte;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.card_class_base(p_f_id integer) OWNER TO phpcompta;
+CREATE FUNCTION check_balance(p_grpt integer) RETURNS numeric
+ AS $$
+declare
+ amount_jrnx_debit numeric;
+ amount_jrnx_credit numeric;
+ amount_jrn numeric;
+begin
+ select sum (j_montant) into amount_jrnx_credit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=false;
+ select sum (j_montant) into amount_jrnx_debit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=true;
+ select jr_montant 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;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.check_balance(p_grpt integer) OWNER TO phpcompta;
+CREATE FUNCTION fiche_account_parent(p_f_id integer) RETURNS poste_comptable
+ AS $$
+declare
+ret poste_comptable;
+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;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.fiche_account_parent(p_f_id integer) OWNER TO phpcompta;
+CREATE FUNCTION insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) RETURNS void
+ AS $$
+declare
+ sCode varchar;
+ nCount_qcode integer;
+begin
+ sCode=trim(p_qcode);
+ if length(sCode) = 0 or p_qcode is null then
+ select count(*) into nCount_qcode
+ from vw_poste_qcode where j_poste=p_poste;
+ if nCount_qcode = 1 then
+ select j_qcode into sCode
+ from vw_poste_qcode where j_poste=p_poste;
+ else
+ sCode=NULL;
+ end if;
+
+ end if;
+ if p_montant = 0.0 then
+ return;
+ end if;
+ insert into jrnx
+ (
+ j_date,
+ j_montant,
+ j_poste,
+ j_grpt,
+ j_jrn_def,
+ j_debit,
+ 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_tech_user,
+ p_tech_per,
+ sCode
+ );
+return;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) OWNER TO phpcompta;
+CREATE FUNCTION insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) RETURNS void
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+begin
+ select f_id into fid_client from
+ attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_client;
+ select f_id into fid_good from
+ attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_fiche;
+ insert into quant_sold
+ (qs_internal,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client)
+ values
+ (p_internal,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client);
+ return;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) OWNER TO phpcompta;
+CREATE FUNCTION insert_quick_code(nf_id integer, tav_text text) RETURNS integer
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ begin
+ tText := upper(trim(tav_text));
+ tText := replace(tText,' ','');
+
+ loop
+ select nextval('s_jnt_fic_att_value') into ns;
+ if length (tText) = 0 or tText is null then
+ tText := 'FID'||ns;
+ end if;
+ select count(*) into nExist
+ from jnt_fic_att_value join attr_value using (jft_id)
+ where
+ ad_id=23 and av_text=upper(tText);
+ if nExist = 0 then
+ exit;
+ end if;
+ tText:='FID'||ns;
+ end loop;
+ insert into jnt_fic_att_value values (ns,nf_id,23);
+ insert into attr_value values (ns,upper(tText));
+ return ns;
+ end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.insert_quick_code(nf_id integer, tav_text text) OWNER TO phpcompta;
+CREATE FUNCTION proc_check_balance() RETURNS "trigger"
+ AS $$
+declare
+ diff numeric;
+ tt integer;
+begin
+ if TG_OP = 'INSERT' 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;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.proc_check_balance() OWNER TO phpcompta;
+CREATE FUNCTION t_document_type_insert() RETURNS "trigger"
+ AS $$
+ BEGIN
+ execute 'create sequence seq_doc_type_'||NEW.dt_id;
+raise notice 'Creating sequence seq_doc_type_%',NEW.dt_id;
+ RETURN NEW;
+ END;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.t_document_type_insert() OWNER TO phpcompta;
+CREATE FUNCTION t_jrn_def_sequence() RETURNS "trigger"
+ AS $$
+ BEGIN
+ execute 'create sequence s_jrn_'||NEW.jrn_def_id;
+raise notice 'Creating sequence s_jrn_%',NEW.jrn_def_id;
+ RETURN NEW;
+ END;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.t_jrn_def_sequence() OWNER TO phpcompta;
+CREATE FUNCTION trim_cvs_quote() RETURNS "trigger"
+ AS $$
+declare
+ modified import_tmp%ROWTYPE;
+begin
+ modified:=NEW;
+ modified.devise=replace(new.devise,'"','');
+ modified.poste_comptable=replace(new.poste_comptable,'"','');
+ modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
+ modified.detail=replace(NEW.DETAIL,'"','');
+ modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
+ return modified;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.trim_cvs_quote() OWNER TO phpcompta;
+CREATE FUNCTION trim_space_format_csv_banque() RETURNS "trigger"
+ AS $$
+declare
+ modified format_csv_banque%ROWTYPE;
+begin
+ modified.name=trim(NEW.NAME);
+ modified.include_file=trim(new.include_file);
+ if ( length(modified.name) = 0 ) then
+ modified.name=null;
+ end if;
+ if ( length(modified.include_file) = 0 ) then
+ modified.include_file=null;
+ end if;
+ return modified;
+end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.trim_space_format_csv_banque() OWNER TO phpcompta;
+CREATE FUNCTION tva_delete(integer) RETURNS void
+ AS $_$
+declare
+ p_tva_id alias for $1;
+ nCount integer;
+begin
+ nCount=0;
+ select count(*) into nCount from quant_sold where qs_vat_code=p_tva_id;
+ if nCount = 0 then
+ delete from tva_rate where tva_id=p_tva_id;
+ end if;
+ return;
+end;
+$_$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.tva_delete(integer) OWNER TO phpcompta;
+CREATE FUNCTION tva_insert(integer, text, numeric, text, text) RETURNS integer
+ AS $_$
+declare
+p_tva_id alias for $1;
+p_tva_label alias for $2;
+p_tva_rate alias for $3;
+p_tva_comment alias for $4;
+p_tva_poste alias for $5;
+debit text;
+credit text;
+nCount integer;
+begin
+if length(trim(p_tva_label)) = 0 then
+ return 3;
+end if;
+select count(*) into nCount from tva_rate
+ where tva_id=p_tva_id;
+if nCount != 0 then
+ return 5;
+end if;
+if length(trim(p_tva_poste)) != 0 then
+ if position (',' in p_tva_poste) = 0 then return 4; end if;
+ debit = split_part(p_tva_poste,',',1);
+ credit = split_part(p_tva_poste,',',2);
+ select count(*) into nCount from tmp_pcmn where pcm_val=debit;
+ if nCount = 0 then return 4; end if;
+ select count(*) into nCount from tmp_pcmn where pcm_val=credit;
+ if nCount = 0 then return 4; end if;
+
+end if;
+insert into tva_rate(tva_id,tva_label,tva_rate,tva_comment,tva_poste)
+ values (p_tva_id,p_tva_label,p_tva_rate,p_tva_comment,p_tva_poste);
+return 0;
+end;
+$_$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.tva_insert(integer, text, numeric, text, text) OWNER TO phpcompta;
+CREATE FUNCTION tva_modify(integer, text, numeric, text, text) RETURNS integer
+ AS $_$declare
+p_tva_id alias for $1;
+p_tva_label alias for $2;
+p_tva_rate alias for $3;
+p_tva_comment alias for $4;
+p_tva_poste alias for $5;
+debit text;
+credit text;
+nCount integer;
+begin
+if length(trim(p_tva_label)) = 0 then
+ return 3;
+end if;
+if length(trim(p_tva_poste)) != 0 then
+ if position (',' in p_tva_poste) = 0 then return 4; end if;
+ debit = split_part(p_tva_poste,',',1);
+ credit = split_part(p_tva_poste,',',2);
+ select count(*) into nCount from tmp_pcmn where pcm_val=debit;
+ if nCount = 0 then return 4; end if;
+ select count(*) into nCount from tmp_pcmn where pcm_val=credit;
+ if nCount = 0 then return 4; end if;
+
+end if;
+update tva_rate set tva_label=p_tva_label,tva_rate=p_tva_rate,tva_comment=p_tva_comment,tva_poste=p_tva_poste
+ where tva_id=p_tva_id;
+return 0;
+end;
+$_$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.tva_modify(integer, text, numeric, text, text) OWNER TO phpcompta;
+CREATE FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ old_qcode varchar;
+ begin
+ select av_text into old_qcode from attr_value where jft_id=njft_id;
+ if tav_text = upper( trim(old_qcode)) then
+ return 0;
+ end if;
+
+ tText := trim(upper(tav_text));
+ tText := replace(tText,' ','');
+ if length ( tText) = 0 or tText is null then
+ return 0;
+ end if;
+
+ ns := njft_id;
+ loop
+ select count(*) into nExist
+ from jnt_fic_att_value join attr_value using (jft_id)
+ where
+ ad_id=23 and av_text=upper(tText);
+ if nExist = 0 then
+ exit;
+ end if;
+ if tText = 'FID'||ns then
+ select nextval('s_jnt_fic_att_value') into ns;
+ end if;
+ tText :='FID'||ns;
+
+ end loop;
+ update attr_value set av_text = tText where jft_id=njft_id;
+ update jrnx set j_qcode=tText where j_qcode = old_qcode;
+ return ns;
+ end;
+$$
+ LANGUAGE plpgsql;
+ALTER FUNCTION public.update_quick_code(njft_id integer, tav_text text) OWNER TO phpcompta;
+SET default_tablespace = '';
+SET default_with_oids = true;
CREATE TRIGGER tr_jrn_check_balance
AFTER INSERT ON jrn
FOR EACH ROW
diff --git a/html/admin/sql/mod1/data.sql b/html/admin/sql/mod1/data.sql
index 07e2247e4..8ee6abf09 100644
--- a/html/admin/sql/mod1/data.sql
+++ b/html/admin/sql/mod1/data.sql
@@ -1643,7 +1643,7 @@ INSERT INTO user_local_pref (user_id, parameter_type, parameter_value) VALUES ('
-- Data for Name: version; Type: TABLE DATA; Schema: public; Owner: phpcompta
--
-INSERT INTO version (val) VALUES (14);
+INSERT INTO version (val) VALUES (15);
--
diff --git a/html/admin/sql/mod1/schema.sql b/html/admin/sql/mod1/schema.sql
index 65fac8a61..1402eedba 100644
--- a/html/admin/sql/mod1/schema.sql
+++ b/html/admin/sql/mod1/schema.sql
@@ -19,745 +19,6 @@ CREATE DOMAIN poste_comptable AS numeric(25,0);
-- Name: account_add(poste_comptable, character varying); Type: FUNCTION; Schema: public; Owner: phpcompta
--
-CREATE FUNCTION account_add(p_id poste_comptable, p_name character varying) RETURNS void
- AS $$
-declare
- nParent tmp_pcmn.pcm_val_parent%type;
- nCount integer;
-begin
- select count(*) into nCount from tmp_pcmn where pcm_val=p_id;
- 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);
- end if;
-return;
-end ;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: account_auto(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION account_auto(p_fd_id integer) RETURNS boolean
- 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;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: account_compute(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION account_compute(p_f_id integer) RETURNS poste_comptable
- AS $$
-declare
- class_base poste_comptable;
- maxcode int8;
-begin
- -- Get the class base
- select fd_class_base into class_base
- from
- fiche_def join fiche using (fd_id)
- where
- f_id=p_f_id;
- raise notice 'class base %',class_base;
- select max(pcm_val) into maxcode from tmp_pcmn where pcm_val = class_base;
- if maxcode = class_base then
- maxcode=class_base*1000+1;
- end if;
- raise notice 'Max code %',maxcode;
-return maxcode+1;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: account_insert(integer, poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION account_insert(p_f_id integer, p_account poste_comptable) RETURNS integer
- AS $$
-declare
-nParent tmp_pcmn.pcm_val_parent%type;
-sName varchar;
-nNew tmp_pcmn.pcm_val%type;
-bAuto bool;
-nFd_id integer;
-nCount integer;
-begin
-
- -- if p_value empty
- if length(trim(p_account)) != 0 then
- -- does the account exist ?
- select * into nCount from tmp_pcmn where pcm_val=p_account;
- if nCount !=0 then
- -- retrieve name
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
- -- get parent
- nParent:=account_parent(p_account);
- -- account doesn't exist we need to add id
- insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent)
- values (p_account,sName,nParent);
- -- insert as card's attribute
- perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
-
- end if;
- else
- select fd_id into nFd_id from fiche where f_id=p_f_id;
- bAuto:= account_auto(nFd_id);
-
- if bAuto = true then
- -- create automatically the account
- -- compute the next account
- nNew:=account_compute(p_f_id);
-raise debug 'nNew %', nNew;
- -- retrieve name
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
-
- -- get parent
- nParent:=account_parent(nNew);
- -- account doesn't exist we need to add id
- perform account_add (nNew,sName);
- -- insert as card's attribute
- perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
-
- else
- perform attribut_insert(p_f_id,5,null);
- end if;
-
- end if;
-
-return 0;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: account_parent(poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION account_parent(p_account poste_comptable) RETURNS poste_comptable
- AS $$
-declare
- nParent tmp_pcmn.pcm_val_parent%type;
- sParent varchar;
- nCount integer;
-begin
- sParent:=to_char(p_account,'9999999999999999');
- sParent:=trim(sParent);
- nParent:=0;
- while nParent = 0 loop
- select count(*) into nCount
- from tmp_pcmn
- where
- pcm_val = to_number(sParent,'9999999999999999');
- if nCount != 0 then
- nParent:=to_number(sParent,'9999999999999999');
- end if;
- sParent:= substr(sParent,1,length(sParent)-1);
- if length(sParent) <= 0 then
- raise exception 'Impossible de trouver le compte parent pour %',p_account;
- end if;
-
- end loop;
-
- return nParent;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: account_update(integer, poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION account_update(p_f_id integer, p_account poste_comptable) RETURNS integer
- AS $$
-declare
-nMax fiche.f_id%type;
-nCount integer;
-nParent tmp_pcmn.pcm_val_parent%type;
-sName varchar;
-nJft_id attr_value.jft_id%type;
-begin
-
- -- if p_value empty
- if length(trim(p_account)) != 0 then
- -- does the account exist ?
- select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
- if nCount = 0 then
- -- retrieve name
- select av_text into sName from
- attr_value join jnt_fic_att_value using (jft_id)
- where
- ad_id=1 and f_id=p_f_id;
- -- get parent
- nParent:=fiche_account_parent(p_f_id);
- -- account doesn't exist we need to add id
- insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
- end if;
- end if;
- -- we retrieve jft_id
- select jft_id into njft_id from jnt_fic_att_value where f_id=p_f_id and ad_id=5;
- -- we update the account
- update attr_value set av_text=p_account where jft_id=njft_id;
-
-return njft_id;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: attribut_insert(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
- AS $$
-declare
- n_jft_id integer;
-begin
- select nextval('s_jnt_fic_att_value') into n_jft_id;
- insert into jnt_fic_att_value (jft_id,f_id,ad_id) values (n_jft_id,p_f_id,p_ad_id);
- insert into attr_value (jft_id,av_text) values (n_jft_id,p_value);
-return;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: card_class_base(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION card_class_base(p_f_id integer) RETURNS fiche_def.fd_class_base%type
- 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 nposte;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: check_balance(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION check_balance(p_grpt integer) RETURNS numeric
- AS $$
-declare
- amount_jrnx_debit numeric;
- amount_jrnx_credit numeric;
- amount_jrn numeric;
-begin
- select sum (j_montant) into amount_jrnx_credit
- from jrnx
- where
- j_grpt=p_grpt
- and j_debit=false;
-
- select sum (j_montant) into amount_jrnx_debit
- from jrnx
- where
- j_grpt=p_grpt
- and j_debit=true;
-
- select jr_montant 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;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: fiche_account_parent(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION fiche_account_parent(p_f_id integer) RETURNS poste_comptable
- AS $$
-declare
-ret poste_comptable;
-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;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: insert_jrnx(character varying, numeric, integer, integer, integer, boolean, text, integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) RETURNS void
- AS $$
-declare
- sCode varchar;
- nCount_qcode integer;
-begin
- sCode=trim(p_qcode);
-
- -- if p_qcode is empty try to find one
- if length(sCode) = 0 or p_qcode is null then
-
- select count(*) into nCount_qcode
- from vw_poste_qcode where j_poste=p_poste;
- -- if we find only one q_code for a accountancy account
- -- then retrieve it
- if nCount_qcode = 1 then
- select j_qcode into sCode
- from vw_poste_qcode where j_poste=p_poste;
- else
- sCode=NULL;
- end if;
-
- end if;
- if p_montant = 0.0 then
- return;
- end if;
- insert into jrnx
- (
- j_date,
- j_montant,
- j_poste,
- j_grpt,
- j_jrn_def,
- j_debit,
- 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_tech_user,
- p_tech_per,
- sCode
- );
-
-return;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: insert_quant_sold(text, character varying, integer, numeric, numeric, integer, character varying); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) RETURNS void
- AS $$
-declare
- fid_client integer;
- fid_good integer;
-begin
- select f_id into fid_client from
- attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_client;
-
- select f_id into fid_good from
- attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_fiche;
-
-
- insert into quant_sold
- (qs_internal,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client)
- values
- (p_internal,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client);
- return;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: insert_quick_code(integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION insert_quick_code(nf_id integer, tav_text text) RETURNS integer
- AS $$
- declare
- ns integer;
- nExist integer;
- tText text;
- begin
- tText := upper(trim(tav_text));
- tText := replace(tText,' ','');
-
- loop
- -- take the next sequence
- select nextval('s_jnt_fic_att_value') into ns;
- if length (tText) = 0 or tText is null then
- tText := 'FID'||ns;
- end if;
- -- av_text already used ?
- select count(*) into nExist
- from jnt_fic_att_value join attr_value using (jft_id)
- where
- ad_id=23 and av_text=upper(tText);
-
- if nExist = 0 then
- exit;
- end if;
- tText:='FID'||ns;
- end loop;
- -- insert into table jnt_fic_att_value
- insert into jnt_fic_att_value values (ns,nf_id,23);
- -- insert value into attr_value
- insert into attr_value values (ns,upper(tText));
- return ns;
- end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: proc_check_balance(); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION proc_check_balance() RETURNS "trigger"
- AS $$
-declare
- diff numeric;
- tt integer;
-begin
- if TG_OP = 'INSERT' 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;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: t_document_type_insert(); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION t_document_type_insert() RETURNS "trigger"
- AS $$
- BEGIN
- execute 'create sequence seq_doc_type_'||NEW.dt_id;
-raise notice 'Creating sequence seq_doc_type_%',NEW.dt_id;
- RETURN NEW;
- END;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: t_jrn_def_sequence(); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION t_jrn_def_sequence() RETURNS "trigger"
- AS $$
- BEGIN
- execute 'create sequence s_jrn_'||NEW.jrn_def_id;
-raise notice 'Creating sequence s_jrn_%',NEW.jrn_def_id;
- RETURN NEW;
- END;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: trim_cvs_quote(); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION trim_cvs_quote() RETURNS "trigger"
- AS $$
-declare
- modified import_tmp%ROWTYPE;
-begin
- modified.code=new.code;
- modified.montant=new.montant;
- modified.date_exec=new.date_exec;
- modified.date_valeur=new.date_valeur;
- modified.devise=replace(new.devise,'"','');
- modified.poste_comptable=replace(new.poste_comptable,'"','');
- modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
- modified.detail=replace(NEW.DETAIL,'"','');
- modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
- modified.bq_account=NEW.bq_account;
- modified.jrn=NEW.jrn;
- modified.ok=new.ok;
- return modified;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: trim_space_format_csv_banque(); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION trim_space_format_csv_banque() RETURNS "trigger"
- AS $$
-declare
- modified format_csv_banque%ROWTYPE;
-begin
- modified.name=trim(NEW.NAME);
- modified.include_file=trim(new.include_file);
- if ( length(modified.name) = 0 ) then
- modified.name=null;
- end if;
- if ( length(modified.include_file) = 0 ) then
- modified.include_file=null;
- end if;
-
- return modified;
-end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: tva_delete(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION tva_delete(integer) RETURNS void
- AS $_$
-declare
- p_tva_id alias for $1;
- nCount integer;
-begin
- nCount=0;
- select count(*) into nCount from quant_sold where qs_vat_code=p_tva_id;
- if nCount = 0 then
- delete from tva_rate where tva_id=p_tva_id;
- end if;
- return;
-end;
-$_$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: tva_insert(integer, text, numeric, text, text); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION tva_insert(integer, text, numeric, text, text) RETURNS integer
- AS $_$
-declare
-p_tva_id alias for $1;
-p_tva_label alias for $2;
-p_tva_rate alias for $3;
-p_tva_comment alias for $4;
-p_tva_poste alias for $5;
-debit text;
-credit text;
-nCount integer;
-begin
-if length(trim(p_tva_label)) = 0 then
- return 3;
-end if;
-select count(*) into nCount from tva_rate
- where tva_id=p_tva_id;
-if nCount != 0 then
- return 5;
-end if;
-if length(trim(p_tva_poste)) != 0 then
- if position (',' in p_tva_poste) = 0 then return 4; end if;
- debit = split_part(p_tva_poste,',',1);
- credit = split_part(p_tva_poste,',',2);
- select count(*) into nCount from tmp_pcmn where pcm_val=debit;
- if nCount = 0 then return 4; end if;
- select count(*) into nCount from tmp_pcmn where pcm_val=credit;
- if nCount = 0 then return 4; end if;
-
-end if;
-insert into tva_rate(tva_id,tva_label,tva_rate,tva_comment,tva_poste)
- values (p_tva_id,p_tva_label,p_tva_rate,p_tva_comment,p_tva_poste);
-return 0;
-end;
-$_$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: tva_modify(integer, text, numeric, text, text); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION tva_modify(integer, text, numeric, text, text) RETURNS integer
- AS $_$declare
-p_tva_id alias for $1;
-p_tva_label alias for $2;
-p_tva_rate alias for $3;
-p_tva_comment alias for $4;
-p_tva_poste alias for $5;
-debit text;
-credit text;
-nCount integer;
-begin
-if length(trim(p_tva_label)) = 0 then
- return 3;
-end if;
-
-if length(trim(p_tva_poste)) != 0 then
- if position (',' in p_tva_poste) = 0 then return 4; end if;
- debit = split_part(p_tva_poste,',',1);
- credit = split_part(p_tva_poste,',',2);
- select count(*) into nCount from tmp_pcmn where pcm_val=debit;
- if nCount = 0 then return 4; end if;
- select count(*) into nCount from tmp_pcmn where pcm_val=credit;
- if nCount = 0 then return 4; end if;
-
-end if;
-update tva_rate set tva_label=p_tva_label,tva_rate=p_tva_rate,tva_comment=p_tva_comment,tva_poste=p_tva_poste
- where tva_id=p_tva_id;
-return 0;
-end;
-$_$
- LANGUAGE plpgsql;
-
-
-
-
---
--- Name: update_quick_code(integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
---
-
-CREATE FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer
- AS $$
- declare
- ns integer;
- nExist integer;
- tText text;
- old_qcode varchar;
- begin
- -- get current value
- select av_text into old_qcode from attr_value where jft_id=njft_id;
- -- av_text didn't change so no update
- if tav_text = upper( trim(old_qcode)) then
- return 0;
- end if;
-
- tText := trim(upper(tav_text));
- tText := replace(tText,' ','');
- if length ( tText) = 0 or tText is null then
- return 0;
- end if;
-
- ns := njft_id;
-
- loop
- -- av_text already used ?
- select count(*) into nExist
- from jnt_fic_att_value join attr_value using (jft_id)
- where
- ad_id=23 and av_text=upper(tText);
-
- if nExist = 0 then
- exit;
- end if;
- if tText = 'FID'||ns then
- -- take the next sequence
- select nextval('s_jnt_fic_att_value') into ns;
- end if;
- tText :='FID'||ns;
-
- end loop;
- update attr_value set av_text = tText where jft_id=njft_id;
- update jrnx set j_qcode=tText where j_qcode = old_qcode;
- return ns;
- end;
-$$
- LANGUAGE plpgsql;
-
-
-
-
SET default_tablespace = '';
SET default_with_oids = true;
@@ -1116,9 +377,10 @@ CREATE TABLE import_tmp (
detail text,
num_compte text,
poste_comptable text,
- ok boolean DEFAULT false,
+ status char(1) default 'w' not null check (status in ('t','d','w')),
bq_account integer NOT NULL,
jrn integer NOT NULL
+
);
@@ -2110,6 +1372,739 @@ CREATE UNIQUE INDEX x_jrn_jr_id ON jrn USING btree (jr_id);
CREATE INDEX x_poste ON jrnx USING btree (j_poste);
+CREATE FUNCTION account_add(p_id poste_comptable, p_name character varying) RETURNS void
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ nCount integer;
+begin
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_id;
+ 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);
+ end if;
+return;
+end ;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: account_auto(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION account_auto(p_fd_id integer) RETURNS boolean
+ 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;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: account_compute(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION account_compute(p_f_id integer) RETURNS poste_comptable
+ AS $$
+declare
+ class_base poste_comptable;
+ maxcode int8;
+begin
+ -- Get the class base
+ select fd_class_base into class_base
+ from
+ fiche_def join fiche using (fd_id)
+ where
+ f_id=p_f_id;
+ raise notice 'class base %',class_base;
+ select max(pcm_val) into maxcode from tmp_pcmn where pcm_val = class_base;
+ if maxcode = class_base then
+ maxcode=class_base*1000+1;
+ end if;
+ raise notice 'Max code %',maxcode;
+return maxcode+1;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: account_insert(integer, poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION account_insert(p_f_id integer, p_account poste_comptable) RETURNS integer
+ AS $$
+declare
+nParent tmp_pcmn.pcm_val_parent%type;
+sName varchar;
+nNew tmp_pcmn.pcm_val%type;
+bAuto bool;
+nFd_id integer;
+nCount integer;
+begin
+
+ -- if p_value empty
+ if length(trim(p_account)) != 0 then
+ -- does the account exist ?
+ select * into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount !=0 then
+ -- retrieve name
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+ -- get parent
+ nParent:=account_parent(p_account);
+ -- account doesn't exist we need to add id
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent)
+ values (p_account,sName,nParent);
+ -- insert as card's attribute
+ perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
+
+ end if;
+ else
+ select fd_id into nFd_id from fiche where f_id=p_f_id;
+ bAuto:= account_auto(nFd_id);
+
+ if bAuto = true then
+ -- create automatically the account
+ -- compute the next account
+ nNew:=account_compute(p_f_id);
+raise debug 'nNew %', nNew;
+ -- retrieve name
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+
+ -- get parent
+ nParent:=account_parent(nNew);
+ -- account doesn't exist we need to add id
+ perform account_add (nNew,sName);
+ -- insert as card's attribute
+ perform attribut_insert(p_f_id,5,to_char(nNew,'999999999999'));
+
+ else
+ perform attribut_insert(p_f_id,5,null);
+ end if;
+
+ end if;
+
+return 0;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: account_parent(poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION account_parent(p_account poste_comptable) RETURNS poste_comptable
+ AS $$
+declare
+ nParent tmp_pcmn.pcm_val_parent%type;
+ sParent varchar;
+ nCount integer;
+begin
+ sParent:=to_char(p_account,'9999999999999999');
+ sParent:=trim(sParent);
+ nParent:=0;
+ while nParent = 0 loop
+ select count(*) into nCount
+ from tmp_pcmn
+ where
+ pcm_val = to_number(sParent,'9999999999999999');
+ if nCount != 0 then
+ nParent:=to_number(sParent,'9999999999999999');
+ end if;
+ sParent:= substr(sParent,1,length(sParent)-1);
+ if length(sParent) <= 0 then
+ raise exception 'Impossible de trouver le compte parent pour %',p_account;
+ end if;
+
+ end loop;
+
+ return nParent;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: account_update(integer, poste_comptable); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION account_update(p_f_id integer, p_account poste_comptable) RETURNS integer
+ AS $$
+declare
+nMax fiche.f_id%type;
+nCount integer;
+nParent tmp_pcmn.pcm_val_parent%type;
+sName varchar;
+nJft_id attr_value.jft_id%type;
+begin
+
+ -- if p_value empty
+ if length(trim(p_account)) != 0 then
+ -- does the account exist ?
+ select count(*) into nCount from tmp_pcmn where pcm_val=p_account;
+ if nCount = 0 then
+ -- retrieve name
+ select av_text into sName from
+ attr_value join jnt_fic_att_value using (jft_id)
+ where
+ ad_id=1 and f_id=p_f_id;
+ -- get parent
+ nParent:=fiche_account_parent(p_f_id);
+ -- account doesn't exist we need to add id
+ insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account,sName,nParent);
+ end if;
+ end if;
+ -- we retrieve jft_id
+ select jft_id into njft_id from jnt_fic_att_value where f_id=p_f_id and ad_id=5;
+ -- we update the account
+ update attr_value set av_text=p_account where jft_id=njft_id;
+
+return njft_id;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: attribut_insert(integer, integer, character varying); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION attribut_insert(p_f_id integer, p_ad_id integer, p_value character varying) RETURNS void
+ AS $$
+declare
+ n_jft_id integer;
+begin
+ select nextval('s_jnt_fic_att_value') into n_jft_id;
+ insert into jnt_fic_att_value (jft_id,f_id,ad_id) values (n_jft_id,p_f_id,p_ad_id);
+ insert into attr_value (jft_id,av_text) values (n_jft_id,p_value);
+return;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: card_class_base(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION card_class_base(p_f_id integer) RETURNS fiche_def.fd_class_base%type
+ 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 nposte;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: check_balance(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION check_balance(p_grpt integer) RETURNS numeric
+ AS $$
+declare
+ amount_jrnx_debit numeric;
+ amount_jrnx_credit numeric;
+ amount_jrn numeric;
+begin
+ select sum (j_montant) into amount_jrnx_credit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=false;
+
+ select sum (j_montant) into amount_jrnx_debit
+ from jrnx
+ where
+ j_grpt=p_grpt
+ and j_debit=true;
+
+ select jr_montant 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;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: fiche_account_parent(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION fiche_account_parent(p_f_id integer) RETURNS poste_comptable
+ AS $$
+declare
+ret poste_comptable;
+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;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: insert_jrnx(character varying, numeric, integer, integer, integer, boolean, text, integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION insert_jrnx(p_date character varying, p_montant numeric, p_poste integer, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text) RETURNS void
+ AS $$
+declare
+ sCode varchar;
+ nCount_qcode integer;
+begin
+ sCode=trim(p_qcode);
+
+ -- if p_qcode is empty try to find one
+ if length(sCode) = 0 or p_qcode is null then
+
+ select count(*) into nCount_qcode
+ from vw_poste_qcode where j_poste=p_poste;
+ -- if we find only one q_code for a accountancy account
+ -- then retrieve it
+ if nCount_qcode = 1 then
+ select j_qcode into sCode
+ from vw_poste_qcode where j_poste=p_poste;
+ else
+ sCode=NULL;
+ end if;
+
+ end if;
+ if p_montant = 0.0 then
+ return;
+ end if;
+ insert into jrnx
+ (
+ j_date,
+ j_montant,
+ j_poste,
+ j_grpt,
+ j_jrn_def,
+ j_debit,
+ 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_tech_user,
+ p_tech_per,
+ sCode
+ );
+
+return;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: insert_quant_sold(text, character varying, integer, numeric, numeric, integer, character varying); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION insert_quant_sold(p_internal text, p_fiche character varying, p_quant integer, p_price numeric, p_vat numeric, p_vat_code integer, p_client character varying) RETURNS void
+ AS $$
+declare
+ fid_client integer;
+ fid_good integer;
+begin
+ select f_id into fid_client from
+ attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_client;
+
+ select f_id into fid_good from
+ attr_value join jnt_fic_att_value using (jft_id) where ad_id=23 and av_text=p_fiche;
+
+
+ insert into quant_sold
+ (qs_internal,qs_fiche,qs_quantite,qs_price,qs_vat,qs_vat_code,qs_client)
+ values
+ (p_internal,fid_good,p_quant,p_price,p_vat,p_vat_code,fid_client);
+ return;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: insert_quick_code(integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION insert_quick_code(nf_id integer, tav_text text) RETURNS integer
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ begin
+ tText := upper(trim(tav_text));
+ tText := replace(tText,' ','');
+
+ loop
+ -- take the next sequence
+ select nextval('s_jnt_fic_att_value') into ns;
+ if length (tText) = 0 or tText is null then
+ tText := 'FID'||ns;
+ end if;
+ -- av_text already used ?
+ select count(*) into nExist
+ from jnt_fic_att_value join attr_value using (jft_id)
+ where
+ ad_id=23 and av_text=upper(tText);
+
+ if nExist = 0 then
+ exit;
+ end if;
+ tText:='FID'||ns;
+ end loop;
+ -- insert into table jnt_fic_att_value
+ insert into jnt_fic_att_value values (ns,nf_id,23);
+ -- insert value into attr_value
+ insert into attr_value values (ns,upper(tText));
+ return ns;
+ end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: proc_check_balance(); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION proc_check_balance() RETURNS "trigger"
+ AS $$
+declare
+ diff numeric;
+ tt integer;
+begin
+ if TG_OP = 'INSERT' 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;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: t_document_type_insert(); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION t_document_type_insert() RETURNS "trigger"
+ AS $$
+ BEGIN
+ execute 'create sequence seq_doc_type_'||NEW.dt_id;
+raise notice 'Creating sequence seq_doc_type_%',NEW.dt_id;
+ RETURN NEW;
+ END;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: t_jrn_def_sequence(); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION t_jrn_def_sequence() RETURNS "trigger"
+ AS $$
+ BEGIN
+ execute 'create sequence s_jrn_'||NEW.jrn_def_id;
+raise notice 'Creating sequence s_jrn_%',NEW.jrn_def_id;
+ RETURN NEW;
+ END;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: trim_cvs_quote(); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION trim_cvs_quote() RETURNS "trigger"
+ AS $$
+declare
+ modified import_tmp%ROWTYPE;
+begin
+ modified:=NEW;
+ modified.devise=replace(new.devise,'"','');
+ modified.poste_comptable=replace(new.poste_comptable,'"','');
+ modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
+ modified.detail=replace(NEW.DETAIL,'"','');
+ modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
+ return modified;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: trim_space_format_csv_banque(); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION trim_space_format_csv_banque() RETURNS "trigger"
+ AS $$
+declare
+ modified format_csv_banque%ROWTYPE;
+begin
+ modified.name=trim(NEW.NAME);
+ modified.include_file=trim(new.include_file);
+ if ( length(modified.name) = 0 ) then
+ modified.name=null;
+ end if;
+ if ( length(modified.include_file) = 0 ) then
+ modified.include_file=null;
+ end if;
+
+ return modified;
+end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: tva_delete(integer); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION tva_delete(integer) RETURNS void
+ AS $_$
+declare
+ p_tva_id alias for $1;
+ nCount integer;
+begin
+ nCount=0;
+ select count(*) into nCount from quant_sold where qs_vat_code=p_tva_id;
+ if nCount = 0 then
+ delete from tva_rate where tva_id=p_tva_id;
+ end if;
+ return;
+end;
+$_$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: tva_insert(integer, text, numeric, text, text); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION tva_insert(integer, text, numeric, text, text) RETURNS integer
+ AS $_$
+declare
+p_tva_id alias for $1;
+p_tva_label alias for $2;
+p_tva_rate alias for $3;
+p_tva_comment alias for $4;
+p_tva_poste alias for $5;
+debit text;
+credit text;
+nCount integer;
+begin
+if length(trim(p_tva_label)) = 0 then
+ return 3;
+end if;
+select count(*) into nCount from tva_rate
+ where tva_id=p_tva_id;
+if nCount != 0 then
+ return 5;
+end if;
+if length(trim(p_tva_poste)) != 0 then
+ if position (',' in p_tva_poste) = 0 then return 4; end if;
+ debit = split_part(p_tva_poste,',',1);
+ credit = split_part(p_tva_poste,',',2);
+ select count(*) into nCount from tmp_pcmn where pcm_val=debit;
+ if nCount = 0 then return 4; end if;
+ select count(*) into nCount from tmp_pcmn where pcm_val=credit;
+ if nCount = 0 then return 4; end if;
+
+end if;
+insert into tva_rate(tva_id,tva_label,tva_rate,tva_comment,tva_poste)
+ values (p_tva_id,p_tva_label,p_tva_rate,p_tva_comment,p_tva_poste);
+return 0;
+end;
+$_$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: tva_modify(integer, text, numeric, text, text); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION tva_modify(integer, text, numeric, text, text) RETURNS integer
+ AS $_$declare
+p_tva_id alias for $1;
+p_tva_label alias for $2;
+p_tva_rate alias for $3;
+p_tva_comment alias for $4;
+p_tva_poste alias for $5;
+debit text;
+credit text;
+nCount integer;
+begin
+if length(trim(p_tva_label)) = 0 then
+ return 3;
+end if;
+
+if length(trim(p_tva_poste)) != 0 then
+ if position (',' in p_tva_poste) = 0 then return 4; end if;
+ debit = split_part(p_tva_poste,',',1);
+ credit = split_part(p_tva_poste,',',2);
+ select count(*) into nCount from tmp_pcmn where pcm_val=debit;
+ if nCount = 0 then return 4; end if;
+ select count(*) into nCount from tmp_pcmn where pcm_val=credit;
+ if nCount = 0 then return 4; end if;
+
+end if;
+update tva_rate set tva_label=p_tva_label,tva_rate=p_tva_rate,tva_comment=p_tva_comment,tva_poste=p_tva_poste
+ where tva_id=p_tva_id;
+return 0;
+end;
+$_$
+ LANGUAGE plpgsql;
+
+
+
+
+--
+-- Name: update_quick_code(integer, text); Type: FUNCTION; Schema: public; Owner: phpcompta
+--
+
+CREATE FUNCTION update_quick_code(njft_id integer, tav_text text) RETURNS integer
+ AS $$
+ declare
+ ns integer;
+ nExist integer;
+ tText text;
+ old_qcode varchar;
+ begin
+ -- get current value
+ select av_text into old_qcode from attr_value where jft_id=njft_id;
+ -- av_text didn't change so no update
+ if tav_text = upper( trim(old_qcode)) then
+ return 0;
+ end if;
+
+ tText := trim(upper(tav_text));
+ tText := replace(tText,' ','');
+ if length ( tText) = 0 or tText is null then
+ return 0;
+ end if;
+
+ ns := njft_id;
+
+ loop
+ -- av_text already used ?
+ select count(*) into nExist
+ from jnt_fic_att_value join attr_value using (jft_id)
+ where
+ ad_id=23 and av_text=upper(tText);
+
+ if nExist = 0 then
+ exit;
+ end if;
+ if tText = 'FID'||ns then
+ -- take the next sequence
+ select nextval('s_jnt_fic_att_value') into ns;
+ end if;
+ tText :='FID'||ns;
+
+ end loop;
+ update attr_value set av_text = tText where jft_id=njft_id;
+ update jrnx set j_qcode=tText where j_qcode = old_qcode;
+ return ns;
+ end;
+$$
+ LANGUAGE plpgsql;
+
+
+
+
--
diff --git a/html/admin/sql/mod2/data.sql b/html/admin/sql/mod2/data.sql
index 00a824780..8f83646ec 100644
--- a/html/admin/sql/mod2/data.sql
+++ b/html/admin/sql/mod2/data.sql
@@ -1608,7 +1608,7 @@ INSERT INTO user_sec_jrn (uj_id, uj_login, uj_jrn_id, uj_priv) VALUES (4, 'phpco
-- Data for Name: version; Type: TABLE DATA; Schema: public; Owner: phpcompta
--
-INSERT INTO version (val) VALUES (14);
+INSERT INTO version (val) VALUES (15);
--
diff --git a/html/admin/sql/mod2/schema.sql b/html/admin/sql/mod2/schema.sql
index 4345c4ecb..de0420964 100644
--- a/html/admin/sql/mod2/schema.sql
+++ b/html/admin/sql/mod2/schema.sql
@@ -151,7 +151,7 @@ CREATE TABLE import_tmp (
detail text,
num_compte text,
poste_comptable text,
- ok boolean DEFAULT false,
+ status char(1) default 'w' not null check (status in ('t','d','w')),
bq_account integer NOT NULL,
jrn integer NOT NULL
);
@@ -954,18 +954,12 @@ CREATE FUNCTION trim_cvs_quote() RETURNS "trigger"
declare
modified import_tmp%ROWTYPE;
begin
- modified.code=new.code;
- modified.montant=new.montant;
- modified.date_exec=new.date_exec;
- modified.date_valeur=new.date_valeur;
- modified.devise=replace(new.devise,'"','');
- modified.poste_comptable=replace(new.poste_comptable,'"','');
+ modified:=NEW;
+ modified.devise=replace(new.devise,'"','');
+ modified.poste_comptable=replace(new.poste_comptable,'"','');
modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
modified.detail=replace(NEW.DETAIL,'"','');
modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
- modified.bq_account=NEW.bq_account;
- modified.jrn=NEW.jrn;
- modified.ok=new.ok;
return modified;
end;
$$
diff --git a/html/admin/sql/patch/upgrade14.sql b/html/admin/sql/patch/upgrade14.sql
new file mode 100644
index 000000000..979c9ab76
--- /dev/null
+++ b/html/admin/sql/patch/upgrade14.sql
@@ -0,0 +1,45 @@
+begin;
+alter table import_tmp add column status varchar(1);
+alter table import_tmp alter status set default 'w';
+create or replace function trim_cvs_quote() returns trigger as $trim$
+declare
+ modified import_tmp%ROWTYPE;
+begin
+ modified:=NEW;
+ modified.devise=replace(new.devise,'"','');
+ modified.poste_comptable=replace(new.poste_comptable,'"','');
+ modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
+ modified.detail=replace(NEW.DETAIL,'"','');
+ modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
+ return modified;
+end;
+$trim$ language plpgsql;
+
+update import_tmp set status = 't' where ok=true;
+update import_tmp set status = 'w' where ok = false;
+update import_tmp set status = 'w' where ok is null;
+
+alter table import_tmp add constraint chk_status check (status in ('w','d','t'));
+
+
+alter table import_tmp drop column ok ;
+comment on table import_tmp is 'Table temporaire pour l''importation des banques en format CSV';
+comment on column import_tmp.status is 'Status doit être w pour en attente, t pour transfèrer ou d à effacer';
+
+
+create or replace function trim_cvs_quote() returns trigger as $trim$
+declare
+ modified import_tmp%ROWTYPE;
+begin
+ modified:=NEW;
+ modified.devise=replace(new.devise,'"','');
+ modified.poste_comptable=replace(new.poste_comptable,'"','');
+ modified.compte_ordre=replace(NEW.COMPTE_ORDRE,'"','');
+ modified.detail=replace(NEW.DETAIL,'"','');
+ modified.num_compte=replace(NEW.NUM_COMPTE,'"','');
+ return modified;
+end;
+$trim$ language plpgsql;
+
+update version set val=15;
+commit;
\ No newline at end of file