From b25682eadafe675dc37a96521726260d7f2866b0 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Tue, 31 May 2022 16:58:39 +0200 Subject: [PATCH] Additional Tax : automatic compute --- html/ajax_misc.php | 6 ++ html/compute.php | 15 ++++- html/js/acc_ledger.js | 68 +++++++++++++++++---- include/class/acc_ledger.class.php | 15 ++++- include/class/acc_ledger_purchase.class.php | 3 + include/class/acc_ledger_sale.class.php | 3 + scenario/test_postgres_json_array.php | 31 ++++++++++ 7 files changed, 123 insertions(+), 18 deletions(-) diff --git a/html/ajax_misc.php b/html/ajax_misc.php index 8a2fe06ff..1f891235d 100644 --- a/html/ajax_misc.php +++ b/html/ajax_misc.php @@ -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); } diff --git a/html/compute.php b/html/compute.php index c75b70871..799ab93d1 100644 --- a/html/compute.php +++ b/html/compute.php @@ -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); } ?> diff --git a/html/js/acc_ledger.js b/html/js/acc_ledger.js index 748fc4218..1887952a7 100644 --- a/html/js/acc_ledger.js +++ b/html/js/acc_ledger.js @@ -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; diff --git a/include/class/acc_ledger.class.php b/include/class/acc_ledger.class.php index eda6c451b..01e82fc6a 100644 --- a/include/class/acc_ledger.class.php +++ b/include/class/acc_ledger.class.php @@ -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=<< +

{$title}

{$checkbox->input()} {$label} {$row['ac_rate']}%: {$msg} {$amount->input()} - - EOF; + + $out.="

"._("Total opération"). + "". + "". + "

"; return $out; } diff --git a/include/class/acc_ledger_purchase.class.php b/include/class/acc_ledger_purchase.class.php index c21205f9a..20ff4c7fd 100644 --- a/include/class/acc_ledger_purchase.class.php +++ b/include/class/acc_ledger_purchase.class.php @@ -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.='
'; $r.=$this->input_additional_tax(); + $r.='
'; return $r; } diff --git a/include/class/acc_ledger_sale.class.php b/include/class/acc_ledger_sale.class.php index 067999802..608be7a10 100644 --- a/include/class/acc_ledger_sale.class.php +++ b/include/class/acc_ledger_sale.class.php @@ -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.='
'; $r.=$this->input_additional_tax(); + $r.='
'; return $r; } /** diff --git a/scenario/test_postgres_json_array.php b/scenario/test_postgres_json_array.php index e69de29bb..4847b09c7 100644 --- a/scenario/test_postgres_json_array.php +++ b/scenario/test_postgres_json_array.php @@ -0,0 +1,31 @@ +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"); \ No newline at end of file