From aef684fa1a32d8a41c06ff9743bc36d84b453a41 Mon Sep 17 00:00:00 2001 From: Dany De Bontridder Date: Mon, 12 Apr 2010 15:00:52 +0000 Subject: [PATCH] Database set search_path to the schema with function --- html/bal_csv.php | 12 +- include/balance.inc.php | 32 +- include/class_database.php | 1 + include/class_fiche.php | 4 +- include/impress_poste.inc.php | 2 +- sql/trunk/03_change_pcm_to_alphanum.sql | 73 ++++- sql/trunk/04_function_to_change.sql | 412 ++++++++++++++++++++++++ 7 files changed, 508 insertions(+), 28 deletions(-) create mode 100644 sql/trunk/04_function_to_change.sql diff --git a/html/bal_csv.php b/html/bal_csv.php index 673b48ada..f9b49bb44 100644 --- a/html/bal_csv.php +++ b/html/bal_csv.php @@ -51,14 +51,14 @@ if ( isset($_POST['central']) ) { $bal->central='Y'; $t_cent="centralisée"; } - else +else $bal->central='N'; - $bal->jrn=$_POST['p_jrn']; - $bal->from_poste=$_POST['from_poste']; - $bal->to_poste=$_POST['to_poste']; +$bal->jrn=$_POST['p_jrn']; +$bal->from_poste=$_POST['from_poste']; +$bal->to_poste=$_POST['to_poste']; - $row=$bal->get_row($_POST['from_periode'], - $_POST['to_periode']); +$row=$bal->get_row($_POST['from_periode'], + $_POST['to_periode']); foreach ($row as $r) { echo $r['poste'].';'. diff --git a/include/balance.inc.php b/include/balance.inc.php index fb70b6b91..5f2b02308 100644 --- a/include/balance.inc.php +++ b/include/balance.inc.php @@ -134,22 +134,22 @@ if ( isset ($_POST['view'] ) ) { echo '
'. dossier::hidden(). HtmlInput::submit('bt_pdf',"Export PDF"). - $hid->input("p_action","impress"). - $hid->input("from_periode",$_POST['from_periode']). - $hid->input("to_periode",$_POST['to_periode']). - $hid->input("p_jrn",$_POST['p_jrn']). - $hid->input("from_poste",$_POST['from_poste']). - $hid->input("to_poste",$_POST['to_poste']); + HtmlInput::hidden("p_action","impress"). + HtmlInput::hidden("from_periode",$_POST['from_periode']). + HtmlInput::hidden("to_periode",$_POST['to_periode']). + HtmlInput::hidden("p_jrn",$_POST['p_jrn']). + HtmlInput::hidden("from_poste",$_POST['from_poste']). + HtmlInput::hidden("to_poste",$_POST['to_poste']); echo "
"; echo '
'. HtmlInput::submit('bt_csv',"Export CSV"). dossier::hidden(). - $hid->input("p_action","impress"). - $hid->input("from_periode",$_POST['from_periode']). - $hid->input("to_periode",$_POST['to_periode']). - $hid->input("p_jrn",$_POST['p_jrn']). - $hid->input("from_poste",$_POST['from_poste']). - $hid->input("to_poste",$_POST['to_poste']); + HtmlInput::hidden("p_action","impress"). + HtmlInput::hidden("from_periode",$_POST['from_periode']). + HtmlInput::hidden("to_periode",$_POST['to_periode']). + HtmlInput::hidden("p_jrn",$_POST['p_jrn']). + HtmlInput::hidden("from_poste",$_POST['from_poste']). + HtmlInput::hidden("to_poste",$_POST['to_poste']); echo "
"; @@ -179,10 +179,10 @@ if ( isset($_POST['view'] ) ) { $row=$bal->get_row($_POST['from_periode'], $_POST['to_periode']); - $periode=new Periode($cn); - $a=$periode->get_date_limit($_POST['from_periode']); - $b=$periode->get_date_limit($_POST['to_periode']); - echo "

période du ".$a['p_start']." au ".$b['p_end']."

"; + $periode=new Periode($cn); + $a=$periode->get_date_limit($_POST['from_periode']); + $b=$periode->get_date_limit($_POST['to_periode']); + echo "

période du ".$a['p_start']." au ".$b['p_end']."

"; echo ''; echo ''; diff --git a/include/class_database.php b/include/class_database.php index 48a4e9020..1d4a632c4 100644 --- a/include/class_database.php +++ b/include/class_database.php @@ -80,6 +80,7 @@ de données"); } $this->db=$a; + if ($p_type == 'dos'||$p_type=='mod') (pg_exec($this->db,'set search_path to public,comptaproc;'); ob_end_clean(); } diff --git a/include/class_fiche.php b/include/class_fiche.php index 55ff9c6c8..c0557108c 100644 --- a/include/class_fiche.php +++ b/include/class_fiche.php @@ -817,7 +817,7 @@ Array $class=$this->strAttribut(ATTR_DEF_ACCOUNT); $is_used_jrnx=0; if ( trim(strlen($class)) != 0 && isNumber($class) == 1 ) - $is_used_jrnx= $this->cn->count_sql("select * from jrnx where j_poste=$class"); + $is_used_jrnx= $this->cn->count_sql("select * from jrnx where j_poste=$1",array($class)); // if class is not NULL and if we use it before, we can't remove it if (FormatString($class) != null && $is_used_jrnx != 0 ) { @@ -828,7 +828,7 @@ Array // Remove in PCMN if ( trim(strlen($class)) != 0 && isNumber($class) == 1 && $is_used_jrnx == 0) { - $this->cn->exec_sql("delete from tmp_pcmn where pcm_val=$class"); + $this->cn->exec_sql("delete from tmp_pcmn where pcm_val=$1",array($class)); } } diff --git a/include/impress_poste.inc.php b/include/impress_poste.inc.php index 2e9f395d5..92310b01d 100644 --- a/include/impress_poste.inc.php +++ b/include/impress_poste.inc.php @@ -152,7 +152,7 @@ if ( isset( $_REQUEST['bt_html'] ) ) { $go=3; } // Check if the post is numeric and exists - elseif ( $cn->count_sql('select * from tmp_pcmn where pcm_val='.FormatString($_GET['poste_id'])) != 0 ) + elseif ( $cn->count_sql('select * from tmp_pcmn where pcm_val=$1',array($_GET['poste_id'])) != 0 ) { $Poste=new Acc_Account_Ledger($cn,$_GET['poste_id']);$go=1; } diff --git a/sql/trunk/03_change_pcm_to_alphanum.sql b/sql/trunk/03_change_pcm_to_alphanum.sql index ac0e7227e..df4a43fc1 100644 --- a/sql/trunk/03_change_pcm_to_alphanum.sql +++ b/sql/trunk/03_change_pcm_to_alphanum.sql @@ -16,7 +16,7 @@ alter table tmp_pcmn alter pcm_val_parent type account_type; alter table tmp_pcmn alter pcm_val type account_type; alter table centralized alter c_poste type account_type; alter table del_jrnx alter j_poste type account_type; -alter table fiche_def alter fd_class_base type account_type; +alter table fiche_def alter fd_class_base type text; alter table jrnx alter j_poste type account_type; alter table parm_poste alter p_value type account_type; @@ -185,9 +185,76 @@ CREATE OR REPLACE VIEW vw_poste_qcode AS JOIN jnt_fic_att_value USING (jft_id) WHERE jnt_fic_att_value.ad_id = 23) b USING (f_id); -alter table centralized add CONSTRAINT fk_pcmn_val foreign key (c_poste) references tmp_pcmn (pcm_val); +CREATE OR REPLACE VIEW vw_supplier AS + SELECT a.f_id, a.av_text AS name, a1.av_text AS quick_code, b.av_text AS tva_num, c.av_text AS poste_comptable, d.av_text AS rue, e.av_text AS code_postal, f.av_text AS pays, g.av_text AS telephone, h.av_text AS email + FROM ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 1) a + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 13) b USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 23) a1 USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 5) c USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 14) d USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 15) e USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 16) f USING (f_id) + JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 17) g USING (f_id) + LEFT JOIN ( SELECT jnt_fic_att_value.jft_id, fiche.f_id, fiche_def.frd_id, fiche.fd_id, fiche_def.fd_class_base, fiche_def.fd_label, fiche_def.fd_create_account, fiche_def_ref.frd_text, fiche_def_ref.frd_class_base, jnt_fic_att_value.ad_id, attr_value.av_text + FROM fiche + JOIN fiche_def USING (fd_id) + JOIN fiche_def_ref USING (frd_id) + JOIN jnt_fic_att_value USING (f_id) + JOIN attr_value USING (jft_id) + WHERE jnt_fic_att_value.ad_id = 18) h USING (f_id) + WHERE a.frd_id = 8; + alter table jrnx add CONSTRAINT fk_pcmn_val foreign key (j_poste) references tmp_pcmn (pcm_val); +alter table centralized add CONSTRAINT fk_pcmn_val foreign key (c_poste) references tmp_pcmn (pcm_val); commit; -select 'ERREUR MAUVAISE VERSION' from version where val!=71; \ No newline at end of file +select 'ERREUR MAUVAISE VERSION' from version where val!=71; diff --git a/sql/trunk/04_function_to_change.sql b/sql/trunk/04_function_to_change.sql new file mode 100644 index 000000000..3b6fc91c3 --- /dev/null +++ b/sql/trunk/04_function_to_change.sql @@ -0,0 +1,412 @@ +-- +-- PostgreSQL database dump +-- +begin; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = off; +SET check_function_bodies = false; +SET client_min_messages = warning; +SET escape_string_warning = off; + + +SET search_path = comptaproc, pg_catalog,public; + +drop function account_add (p_id poste_comptable,t character varying) ; +drop function account_parent (a poste_comptable) ; +drop function insert_jrnx (p_date character varying, p_montant numeric, p_poste poste_comptable, p_grpt integer, p_jrn_def integer, p_debit boolean, p_tech_user text, p_tech_per integer, p_qcode text, p_comment text); +drop function account_update(integer,text); +drop function account_compute(p_f_id integer) ; +drop function find_pcmn_type(numeric); +drop function account_compute(integer); +-- +-- Name: account_add(public.account_type, character varying); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION comptaproc.account_add(p_id tmp_pcmn.pcm_val_parent%type , 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_compute(integer); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION account_compute(p_f_id integer) RETURNS tmp_pcmn.pcm_val_parent%type + AS $$ +declare + class_base tmp_pcmn.pcm_val_parent%type; + maxcode tmp_pcmn.pcm_val_parent%type; +begin + select fd_class_base into class_base + from + fiche_def join fiche using (fd_id) + where + f_id=p_f_id; + raise notice 'account_compute class base %',class_base; + select count (pcm_val) into maxcode from tmp_pcmn where pcm_val_parent = class_base; + if maxcode = 0 then + maxcode:=class_base; + else + select max (pcm_val) into maxcode from tmp_pcmn where pcm_val_parent = class_base; + end if; + if maxcode = class_base then + maxcode:=class_base::numeric*1000; + end if; + maxcode:=maxcode+1; + raise notice 'account_compute Max code %',maxcode; + return maxcode; +end; +$$ + LANGUAGE plpgsql; + + +-- +-- Name: account_insert(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION account_insert(p_f_id integer, p_account text) RETURNS integer + AS $$ +declare + nParent tmp_pcmn.pcm_val_parent%type; + sName varchar; + nNew tmp_pcmn.pcm_val%type; + bAuto bool; + nFd_id integer; + sClass_Base tmp_pcmn.pcm_val_parent%type; + nCount integer; + first text; + second text; +begin + + if length(trim(p_account)) != 0 then + -- if there is coma in p_account, treat normally + if position (',' in p_account) = 0 then + raise info 'p_account is not empty'; + select count(*) into nCount from tmp_pcmn where pcm_val=p_account::account_type; + raise notice 'found in tmp_pcm %',nCount; + if nCount !=0 then + raise info 'this account exists in tmp_pcmn '; + perform attribut_insert(p_f_id,5,p_account); + else + -- account doesn't exist, create it + 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::account_type); + insert into tmp_pcmn(pcm_val,pcm_lib,pcm_val_parent) values (p_account::account_type,sName,nParent); + perform attribut_insert(p_f_id,5,p_account); + + 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; + perform attribut_insert(p_f_id,5,p_account); + end if; + else + raise info 'p_account is empty'; + 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=nFid_id; + + if bAuto = true and sClass_base ~'^\\d*$' then + raise notice 'account generated automatically'; + nNew:=account_compute(p_f_id); + raise notice '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,nNew); + + else + -- if there is an account_base then it is the default + select fd_class_base::account_type into nNew from fiche_def join fiche using (fd_id) where f_id=p_f_id; + if nNew is null or length(trim(nNew)) = 0 then + raise notice 'count is null'; + perform attribut_insert(p_f_id,5,null); + else + perform attribut_insert(p_f_id,5,nNew); + end if; + end if; + end if; + +return 0; +end; +$$ + LANGUAGE plpgsql; + + +-- +-- Name: account_parent(public.account_type); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION comptaproc.account_parent(p_account public.account_type) RETURNS public.account_type + AS $$ +declare + sSubParent tmp_pcmn.pcm_val_parent%type; + sResult tmp_pcmn.pcm_val_parent%type; + nCount integer; +begin + nParent:=0; + while nParent = 0 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; + end loop; + raise notice 'account_parent : Parent is %',nParent; + return sSubParent; +end; +$$ + LANGUAGE plpgsql; + + +-- +-- Name: account_update(integer, text); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION account_update(p_f_id integer, p_account tmp_pcmn.pcm_val_parent%type ) 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; + first text; + second text; +begin + + if length(trim(p_account)) != 0 then + if position (',' in 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:=account_parent(p_account); + 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; + 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; + + + +-- +-- Name: find_pcm_type(numeric); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION find_pcm_type(pp_value tmp_pcmn.pcm_val_parent%type) RETURNS text + AS $$ +declare + str_type text; + str_value text; + n_value numeric; + nLength integer; +begin + str_value:=pp_value; + nLength:=length(str_value); + while nLength > 0 loop + n_value:=str_value::numeric; + select p_type into str_type from parm_poste where p_value=n_value; + if FOUND then + return str_type; + end if; + nLength:=nLength-1; + str_value:=substring(str_value from 1 for nLength); + end loop; +return 'CON'; +end; +$$ + LANGUAGE plpgsql; + +-- +-- Name: insert_jrnx(character varying, numeric, public.account_type, integer, integer, boolean, text, integer, text, text); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION insert_jrnx(p_date character varying, p_montant numeric, p_poste tmp_pcmn.pcm_val_parent%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 + 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::text into sCode + from vw_poste_qcode where j_poste=p_poste; + else + sCode=NULL; + end if; + + end if; + + 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, + sCode + ); + +return; +end; +$$ + LANGUAGE plpgsql; + + +-- +-- Name: tva_insert(text, numeric, text, text); Type: FUNCTION; Schema: comptaproc; Owner: - +-- + +CREATE OR REPLACE FUNCTION tva_insert(text, numeric, text, text) RETURNS integer + AS $_$ +declare + l_tva_id integer; + p_tva_label alias for $1; + p_tva_rate alias for $2; + p_tva_comment alias for $3; + p_tva_poste alias for $4; + 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::account_type; + if nCount = 0 then return 4; end if; + select count(*) into nCount from tmp_pcmn where pcm_val=credit::account_type; + if nCount = 0 then return 4; end if; + +end if; +select into l_tva_id nextval('s_tva') ; +insert into tva_rate(tva_id,tva_label,tva_rate,tva_comment,tva_poste) + values (l_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: comptaproc; Owner: - +-- + +CREATE OR REPLACE 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::account_type; + if nCount = 0 then return 4; end if; + select count(*) into nCount from tmp_pcmn where pcm_val=credit::account_type; + 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; + +commit;
Poste Comptable