add a trigger to the database to validate the rounded value
This commit is contained in:
parent
4f6a181c08
commit
b0e4fe3358
2 changed files with 100 additions and 7 deletions
|
|
@ -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."<br>";
|
||||
// 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 "<hr>";
|
||||
} // 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']."<hr>";
|
||||
$db=DbConnect($db_row['dos_id'],'dossier');
|
||||
echo "Patching ".$db_row['dos_name']." from the version ".GetVersion($db)."<hr>";
|
||||
|
||||
if ( GetVersion($db) <= 4 ) {
|
||||
ExecuteScript($db,'sql/patch/upgrade4.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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue