diff --git a/html/admin/setup.php b/html/admin/setup.php index 7f469dfb5..15a44ff42 100644 --- a/html/admin/setup.php +++ b/html/admin/setup.php @@ -97,8 +97,11 @@ function GetVersion($p_cn) { function ExecuteScript($p_cn,$script) { $hf=fopen($script,'r'); $sql=""; + $flag_function=false; while (!feof($hf)) { $buffer=fgets($hf); + $buffer=str_replace ("$","\$",$buffer); + print $buffer."
"; // comment are not execute if ( substr($buffer,0,2) == "--" ) { //echo "comment $buffer"; @@ -109,17 +112,35 @@ function ExecuteScript($p_cn,$script) { //echo "Blank $buffer"; Continue; } - + if ( strpos($buffer,"create function")===0 ) { + echo "found a function"; + $flag_function=true; + $sql=$buffer; + continue; + } // No semi colon -> multiline command - if ( strpos($buffer,';') == false ) { + if ( $flag_function== false && strpos($buffer,';') == false ) { $sql.=$buffer; continue; } - // cut the semi colon - $buffer=str_replace (';','',$buffer); + if ( $flag_function ) { + if ( strpos($buffer, "language plpgsql") == false ) { + $sql.=$buffer; + continue; + } + } else { + // cut the semi colon + $buffer=str_replace (';','',$buffer); + } $sql.=$buffer; - ExecSql($p_cn,$sql); + if ( ExecSql($p_cn,$sql) == false ) { + Rollback($p_cn); + print "ERROR : $sql"; + break; + } $sql=""; + $flag_function=false; + print "
"; } // while (feof) fclose($hf); } @@ -275,8 +296,9 @@ $MaxDossier=pg_NumRows($Resdossier); for ($e=0;$e < $MaxDossier;$e++) { $db_row=pg_fetch_array($Resdossier,$e); - echo "Patching ".$db_row['dos_name']."
"; $db=DbConnect($db_row['dos_id'],'dossier'); + echo "Patching ".$db_row['dos_name']." from the version ".GetVersion($db)."
"; + if ( GetVersion($db) <= 4 ) { ExecuteScript($db,'sql/patch/upgrade4.sql'); diff --git a/html/admin/sql/patch/upgrade7.sql b/html/admin/sql/patch/upgrade7.sql index f1229ad61..ee1c38cea 100644 --- a/html/admin/sql/patch/upgrade7.sql +++ b/html/admin/sql/patch/upgrade7.sql @@ -4,7 +4,6 @@ insert into tva_rate values (5,'0%',0, 'Pas soumis update fiche_def_ref set frd_class_base=2400 where frd_id=7; -update version set val=8; -- banque n'a pas de gestion stock delete from jnt_fic_attr where fd_id=1 and ad_id=19; -- client n'a pas de gestion stock @@ -16,7 +15,79 @@ delete from jnt_fic_attr where fd_id=2 and ad_id=19; update jrnx set j_tech_per = jr_tech_per from jrn where j_grpt=jr_grpt_id and j_tech_per is null; alter table jrnx alter j_tech_per set not null; alter table jrn alter jr_tech_per set not null; +alter table jrn alter jr_montant type numeric(8,4); +alter table jrnx alter j_montant type numeric(8,4); -- version 8 + +create function check_balance (p_internal text) 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 join jrn on (j_grpt=jr_grpt_id) + where + jr_internal=p_internal + and j_debit=false; + + select sum (j_montant) into amount_jrnx_debit + from jrnx join jrn on (j_grpt=jr_grpt_id) + where + jr_internal=p_internal + and j_debit=true; + + select jr_montant into amount_jrn + from jrn + where + jr_internal=p_internal; + + 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; + +create function proc_check_balance () returns TRIGGER as $jrn$ +declare + diff numeric; + tt text; +begin + raise notice 'tg_op is %', TG_OP; + if TG_OP = 'INSERT' then + tt=NEW.jr_internal; + diff:=check_balance(tt); + if diff != 0 then + raise exception 'Rounded error %',diff ; + end if; + return NEW; + end if; +end; +$jrn$ language plpgsql; +create trigger tr_jrn_check_balance after insert on jrn for each row execute procedure proc_check_balance(); +create table user_local_pref ( + user_id text, + parameter_type text, + parameter_value text +); +comment on table user_local_pref is 'The user''s local parameter '; +comment on column user_local_pref.user_id is 'user''s login '; +comment on column user_local_pref.parameter_type is 'the type of parameter '; +comment on column user_local_pref.parameter_value is 'the value of parameter '; + +alter table user_local_pref add constraint pk_user_local_pref primary key (user_id,parameter_type); + +insert into user_local_pref (user_id,parameter_type,parameter_value) +select pref_user,'PERIODE',pref_periode from user_pref ; + update version set val=8; + + commit;