Additional Tax : automatic compute

This commit is contained in:
sparkyx 2022-05-31 16:58:39 +02:00
parent 867838d11c
commit b25682eada
7 changed files with 123 additions and 18 deletions

View file

@ -652,6 +652,12 @@ EOF;
[$ledger_id]);
echo $code;
break;
case 'up_other_tax':
$cn =Dossier::connect();
$ledger_id=$http->request('jrn_id','number');
$acc_ledger=new Acc_Ledger($cn,$ledger_id);
echo $acc_ledger->input_additional_tax();
break;
default:
var_dump($_REQUEST);
}

View file

@ -48,6 +48,8 @@ foreach (array('t','c','p','q','n','gDossier') as $a)
}
}
$http=new HttpInput();
$tax_ac_id=$http->request("other_tax_id","number",-1);
// sometime number uses coma instead of dot for dec
$p=str_replace(",",".",$p);
$q=str_replace(",",".",$q);
@ -79,20 +81,29 @@ if ( isNUmber($p) && isNumber($q)) {
$amount = 0;
}
$total->set_parameter('amount',$amount);
$other_tax_amount=0;
if ( $tax_ac_id !=-1) {
$other_tax=new Acc_Other_Tax_SQL($cn,$tax_ac_id);
$other_tax_amount=round(bcmul($amount,$other_tax->getp("ac_rate"),4)/100,2);
}
if ( $t != -1 && isNumber($t) == 1 )
{
$total->set_parameter('amount_vat_rate',$tva_rate->get_parameter('rate'));
$total->compute_vat();
if ($tva_rate->get_parameter('both_side')== 1) $total->set_parameter('amount_vat', 0);
$tvac=($tva_rate->get_parameter('rate') == 0 || $tva_rate->get_parameter('both_side')== 1) ? $amount : bcadd($total->get_parameter('amount_vat'),$amount);
header("Content-type: text/html; charset: utf8",true);
echo '{"ctl":"'.$n.'","htva":"'.$amount.'","tva":"'.$total->get_parameter('amount_vat').'","tvac":"'.$tvac.'"}';
$result=["ctl"=>$n,"htva"=>$amount,"tva"=>$total->get_parameter("amount_vat"),"tvac"=>$tvac,
"other_tax"=>$other_tax_amount];
echo json_encode($result);
}
else
{
/* there is no vat to compute */
header("Content-type: text/html; charset: utf8",true);
echo '{"ctl":"'.$n.'","htva":"'.$amount.'","tva":"NA","tvac":"'.$amount.'"}';
$result=["ctl"=>$n,"htva"=>$amount,"tva"=>"NA","tvac"=>$amount, "other_tax"=>$other_tax_amount];
echo json_encode($result);
}
?>

View file

@ -88,7 +88,30 @@ function update_pay_method()
}
);
}
/**
* update the list of additional tax
*/
function update_other_tax()
{
waiting_box();
var jrn = g("p_jrn").value;
var dossier = g("gDossier").value;
var querystring ={gDossier: dossier,jrn_id:jrn ,op:"up_other_tax"};
var action = new Ajax.Request(
"ajax_misc.php",
{
method: 'get',
parameters: querystring,
onFailure: error_get_predef,
onSuccess: function (req) {
remove_waiting_box();
var answer = req.responseText;
answer.evalScripts();
$('additional_tax_div').innerHTML = answer;
}
}
);
}
/**
* update ctl id =jrn_name with the value of p_jrn
*/
@ -480,7 +503,11 @@ function compute_ledger(p_ctl_nb)
g('e_quant' + p_ctl_nb).value = trim(g('e_quant' + p_ctl_nb).value);
var quantity = g('e_quant' + p_ctl_nb).value;
var querystring = 'gDossier=' + dossier + '&c=' + qcode + '&t=' + tva_id + '&p=' + price + '&q=' + quantity + '&n=' + p_ctl_nb;
let other_tax=g("other_tax");
let other_tax_id=(other_tax && other_tax.checked)?other_tax.value:-1;
console.debug(`other tax is ${other_tax_id}`);
var querystring = { gDossier : dossier , c : qcode ,t :tva_id,p : price , q:quantity,n:p_ctl_nb,'other_tax_id':other_tax_id};
var action = new Ajax.Request(
"compute.php",
{
@ -500,7 +527,8 @@ function refresh_ledger()
var htva = 0;
var tvac = 0;
for (var i = 0; i < g("nb_item").value; i++)
nb_item=g("nb_item").value;
for (var i = 0; i < nb_item; i++)
{
if (g('tva_march' + i))
tva += g('tva_march' + i).value * 1;
@ -509,13 +537,22 @@ function refresh_ledger()
if (g('tvac_march' + i))
tvac += g('tvac_march' + i).value * 1;
}
id_tva=g("tva");
id_htva=g("htva");
id_tvac=g("tvac");
id_other_tax=g("other_tax_amount");
if (id_tva)
id_tva.innerHTML = Math.round(tva * 100) / 100;
if (id_htva)
id_htva.innerHTML = Math.round(htva * 100) / 100;
if ( id_other_tax) {
let total_operation=tvac+parseFloat(id_other_tax.value);
$('total_operation_other_tax').innerHTML=Math.round(total_operation*100)/100;
}
if (id_tvac)
id_tvac.innerHTML = Math.round(tvac * 100) / 100;
if (g('tva'))
g('tva').innerHTML = Math.round(tva * 100) / 100;
if (g('htva'))
g('htva').innerHTML = Math.round(htva * 100) / 100;
if (g('tvac'))
g('tvac').innerHTML = Math.round(tvac * 100) / 100;
}
/**
* update the field htva, tva_id and tvac, callback function for compute_sold
@ -529,7 +566,10 @@ function success_compute_ledger(request, json)
var rtva = answer.tva;
var rhtva = answer.htva;
var rtvac = answer.tvac;
let other_tax=g("other_tax_amount")
if ( other_tax) {
other_tax.value=answer.other_tax;
}
if (rtva == 'NA')
{
var rhtva = answer.htva * 1;
@ -574,7 +614,8 @@ function error_compute_ledger(request, json)
function compute_all_ledger()
{
var loop = 0;
for (loop = 0; loop < g("nb_item").value; loop++)
let nb_item=g("nb_item").value;
for (loop = 0; loop < nb_item; loop++)
{
compute_ledger(loop);
}
@ -582,7 +623,7 @@ function compute_all_ledger()
var htva = 0;
var tvac = 0;
for (var i = 0; i < g("nb_item").value; i++)
for (var i = 0; i < nb_item; i++)
{
if (g('tva_march'))
tva += g('tva_march' + i).value * 1;
@ -591,11 +632,12 @@ function compute_all_ledger()
if (g('tvac_march' + i))
tvac += g('tvac_march' + i).value * 1;
}
id_other_tax=g("other_tax_amount");
if (g('tva'))
g('tva').innerHTML = Math.round(tva * 100) / 100;
if (g('htva'))
g('htva').innerHTML = Math.round(htva * 100) / 100;
if (id_other_tax) { tvac+=id_other_tax.value;}
if (g('tvac'))
g('tvac').innerHTML = Math.round(tvac * 100) / 100;

View file

@ -3363,19 +3363,28 @@ class Acc_Ledger extends jrn_def_sql
if ($this->has_other_tax() == false ) { return "";}
$amount=new INum("other_tax_amount",0);
$amount->javascript='onchange="format_number(this,2);refresh_ledger();"';
$msg=_("Montant");
$row=$this->cn->get_row("select ac_id,ac_label,ac_rate from acc_other_tax where $1 = any (ajrn_def_id)",
[$this->id]);
$checkbox=new ICheckBox("other_tax",$row['ac_id']);
$checkbox->javascript=<<<EOF
onchange='if (! this.checked) { $("other_tax_amount").value=0;}compute_all_ledger();'
EOF;
$label=h($row['ac_label']);
$title=_("Autre taxe");
$out=<<<EOF
<div id="additional_tax">
<h2 class="h3">{$title}</h2>
{$checkbox->input()} {$label} {$row['ac_rate']}%: {$msg} {$amount->input()}
</div>
EOF;
$out.="<h4>"._("Total opération").
"<span class=\"mx-4\" id='total_operation_other_tax'>".
"</span>".
"</h4>";
return $out;
}

View file

@ -1217,6 +1217,7 @@ class Acc_Ledger_Purchase extends Acc_Ledger
$add_js.='update_name();';
$add_js.='update_pay_method();';
$add_js.='update_row("sold_item");';
$add_js.='update_other_tax()';
$wLedger=$this->select_ledger('ACH',2,FALSE);
@ -1499,7 +1500,9 @@ class Acc_Ledger_Purchase extends Acc_Ledger
}
// set focus on date
$r.= create_script("$('".$Date->id."').focus()");
$r.='<div id="additional_tax_div">';
$r.=$this->input_additional_tax();
$r.='</div>';
return $r;
}

View file

@ -1329,6 +1329,7 @@ EOF;
$add_js.='update_name();';
$add_js.='update_pay_method();';
$add_js.='update_row("sold_item");';
$add_js.='update_other_tax()';
$wLedger = $this->select_ledger('VEN', 2,FALSE);
if ($wLedger == null)
@ -1581,7 +1582,9 @@ EOF;
$r.=HtmlInput::hidden('jrn_type', 'VEN');
$r.= Html_Input_Noalyss::ledger_add_item("O");
$r.= create_script("$('" . $Date->id . "').focus()");
$r.='<div id="additional_tax_div">';
$r.=$this->input_additional_tax();
$r.='</div>';
return $r;
}
/**

View file

@ -0,0 +1,31 @@
<?php
//@description: test pour PostgreSQL JSON & ARRAY
$cnx=$cn->get_db();
$ret = pg_query($cnx,"select * from jrn_def ");
$result=pg_fetch_all($ret);
echo "retrieve ",count($result);
$ret=pg_query($cnx,"drop table if exists public.test_array_int");
$ret = pg_query ($cnx,
"CREATE TABLE public.test_array_int(
ac_id serial4 NOT NULL PRIMARY KEY ,
ac_label text NOT NULL,
ac_rate numeric(5, 2) NOT NULL,
ajrn_def_id _int4 NULL); "
);
if ( ! $ret ){
die ("cannot create public.test_array_int");
}
$ret=pg_query($cnx,
"insert into public.test_array_int(ac_label,ac_rate,ajrn_def_id) values ('ligne 1',1.2,'{1,2}')");
$ret=pg_query_params($cnx,
"insert into public.test_array_int(ac_label,ac_rate,ajrn_def_id) values ($1,$2,$3)",
array('ligne 1',1.2,'{1,2}'));
$ret=pg_query($cnx,"drop table if exists public.test_array_int");