Fix 2398 : bug si melange de montants négatifs et positifs avec de la

TVA en autoliquidation
This commit is contained in:
sparkyx 2024-12-20 19:45:16 +01:00
parent c64f356cfa
commit 846171f4e1
5 changed files with 455 additions and 47 deletions

View file

@ -554,19 +554,36 @@ class Acc_Ledger_Purchase extends Acc_Ledger
try
{
bcscale(4);
// total amount of the purchase
/// @var $tot_amount float : total amount of the purchase (debit)
$tot_amount=0;
/// @var $tot_tva float : total amount of the VAT
$tot_tva=0;
$tot_debit=0;
$this->db->start();
/// @var $tot_nd float total not Deductible
$tot_nd=0;
/// @var $tot_perso float total private amount
$tot_perso=0;
/// @var $tot_tva_nd float total vat not deductible
$tot_tva_nd=0;
/// @var $tot_tva_ndded float total vat not deductible - deductible via another tax
$tot_tva_ndded=0;
$tot_tva_reversed=0;
$tot_tva_reversed=0; //@todo to remove
/// @var $tva array that will contain all the VAT Amount
$tva=array();
/// @var $tva_reverse array that contain all the VAT autoreverse AND negative
$tva_reverse = array();
/// @var $tot_amount_cur : total amount in currency
$tot_amount_cur=0;
// find the currency from v_currency_last_value
/// @var $currency_rate_ref Acc_Currency , currency object for this operation
$currency_rate_ref=new Acc_Currency($this->db, $p_currency_code);
/* Save all the items without vat and no deductible vat and expense*/
@ -577,7 +594,8 @@ class Acc_Ledger_Purchase extends Acc_Ledger
/* First we save all the items without vat */
$fiche=new Fiche($this->db);
$fiche->get_by_qcode(${"e_march".$i});
$tva_both=0;
/// @var $tva_both integer 1 for autoreverse ,0 normal, fetch it once for this item,
$tva_both=0;
/* tva */
if ($g_parameter->MY_TVA_USE=='Y')
{
@ -682,13 +700,14 @@ class Acc_Ledger_Purchase extends Acc_Ledger
if ( $g_parameter->MY_TVA_USE=='Y')
{
$tva_item=$acc_amount->amount_vat;
if (isset($tva[$idx_tva]))
{
$tva[$idx_tva]=(isset( $tva[$idx_tva]))? $tva[$idx_tva]:0;
if ($tva_both == 0 || $tva_item >= 0){
$tva[$idx_tva]=bcadd($tva[$idx_tva], $tva_item);
}else {
// $tva_item < 0 && $tva_both == 1
$tva_reverse[$idx_tva]=(isset($tva_reverse[$idx_tva]))?$tva_reverse[$idx_tva]:0;
$tva_reverse[$idx_tva]=bcadd($tva_item,$tva_reverse[$idx_tva]);
}
else
$tva[$idx_tva]=$tva_item;
}
/* Save the stock */
/* if the quantity is < 0 then the stock increase (return of
@ -859,8 +878,6 @@ class Acc_Ledger_Purchase extends Acc_Ledger
foreach ($tva as $i => $value)
{
$oTva=Acc_Tva::build($this->db,$i);
$oTva->load();
$poste_vat=$oTva->get_side('d');
$cust_amount=round(bcadd($tot_amount,$tot_tva),2);
@ -873,6 +890,7 @@ class Acc_Ledger_Purchase extends Acc_Ledger
$acc_operation->type='d';
$acc_operation->periode=$tperiode;
if ( $value > 0 ) $tot_debit=bcadd($tot_debit,abs($value));
if ( $oTva->get_parameter("both_side") == 1 && $value ==0 ) continue;
$acc_operation->insert_jrnx();
// if TVA is on both side, we deduce it immediately
@ -888,16 +906,54 @@ class Acc_Ledger_Purchase extends Acc_Ledger
$acc_operation=new Acc_Operation($this->db);
$acc_operation->date=$e_date;
$acc_operation->poste=$poste_vat;
$acc_operation->amount=$tot_tva_reversed;
$acc_operation->amount=$value;
$acc_operation->grpt=$seq;
$acc_operation->jrn=$p_jrn;
$acc_operation->type='c';
$acc_operation->periode=$tperiode;
$acc_operation->insert_jrnx();
if ( $value < 0 ) $tot_debit=bcadd($tot_debit,abs($value));
//if ( $value < 0 ) $tot_debit=bcadd($tot_debit,abs($value));
}
}
} // LOOP : foreach $tva
foreach ($tva_reverse as $i => $value) {
$oTva = Acc_Tva::build($this->db,$i);
$poste_vat = $oTva->get_side('d');
if ( $poste_vat == '#')
{
$poste_vat=$oTva->get_side('c');
}
$acc_operation = new Acc_Operation($this->db);
$acc_operation->date = $e_date;
$acc_operation->poste = $poste_vat;
$acc_operation->amount = $value;
$acc_operation->grpt = $seq;
$acc_operation->jrn = $p_jrn;
$acc_operation->type = 'd';
$acc_operation->periode = $tperiode;
$acc_operation->insert_jrnx();
// if TVA is on both side, we deduce it immediately
$poste_vat = $oTva->get_side('c');
if ( $poste_vat == '#')
{
$poste_vat=$oTva->get_side('d');
}
$acc_operation = new Acc_Operation($this->db);
$acc_operation->date = $e_date;
$acc_operation->poste = $poste_vat;
$acc_operation->amount = $value;
$acc_operation->grpt = $seq;
$acc_operation->jrn = $p_jrn;
$acc_operation->type = 'c';
$acc_operation->periode = $tperiode;
$acc_operation->insert_jrnx();
$tot_debit = bcadd($tot_debit, $value);
$tot_debit = round($tot_debit, 2);
$n_both = $value;
} //LOOP: foreach $tva_reverse
}
/* insert into jrn */

View file

@ -300,21 +300,28 @@ class Acc_Ledger_Sale extends Acc_Ledger {
bcscale(4);
try {
// total amount of the sales (credit)
/// @var $tot_amount : total amount of the sales (credit)
$tot_amount = 0;
// total amount of the VAT
/// @var $tot_tva : total amount of the VAT
$tot_tva = 0;
// tot debit if item's amount < 0
$tot_debit = 0;
// total amount in currency
/// @var $tot_amount_cur : total amount in currency
$tot_amount_cur=0;
$this->db->start();
/// @var $tva array that will contain all the VAT Amount
$tva = array();
/// @var $tva_reverse array that contain all the VAT autoreverse AND negative
$tva_reverse = array();
// find the currency from v_currency_last_value
/// @var $currency_rate_ref Acc_Currency , currency object for this operation
$currency_rate_ref=new Acc_Currency($this->db, $p_currency_code);
/* Save all the items without vat */
for ($i = 0; $i < $nb_item; $i++) {
/// @var $n_both float auto-reverse amount
$n_both = 0;
if ( empty(${'e_march'.$i}) || empty(${'e_quant'.$i}) ) continue;
@ -328,7 +335,6 @@ class Acc_Ledger_Sale extends Acc_Ledger {
$tot_amount = bcadd($tot_amount, $amount);
$tot_amount = round($tot_amount, 2);
if ( DEBUGNOALYSS > 1 ) { echo __LINE__." tot_amount $tot_amount<br>";}
$acc_operation = new Acc_Operation($this->db);
$acc_operation->date = $e_date;
$sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
@ -367,39 +373,44 @@ class Acc_Ledger_Sale extends Acc_Ledger {
if ($g_parameter->MY_TVA_USE == 'Y') {
/* Compute sum vat */
$oTva = Acc_Tva::build($this->db, trim(${'e_march' . $i . '_tva_id'}));
$oTva->load();
$idx_tva =$oTva->get_parameter("id");
/// @var $auto_reverse = if the oTVA autoreverse, fetch it once for this item,
$auto_reverse=$oTva->get_parameter("both_side");
$tva_item_currency = ${'e_march' . $i . '_tva_amount'};
/* if empty then we need to compute it */
if (trim($tva_item_currency) == '' || ${'e_march'.$i.'_tva_amount'} == 0) {
/* retrieve tva */
$l = Acc_Tva::build($this->db, $idx_tva);
$l->load();
$tva_item_currency = bcmul($amount, $l->get_parameter('rate'));
$tva_item=round($tva_item_currency,2);
$tva_item_currency = bcmul($amount, $oTva->get_parameter('rate'));
$tva_item=round($tva_item_currency,2);
}
$tva_item=bcdiv($tva_item_currency,$p_currency_rate);
$tva_item=round($tva_item,2);
if (isset($tva[$idx_tva]))
$tva[$idx_tva]=(isset($tva[$idx_tva]))?$tva[$idx_tva]:0;
if ( $auto_reverse == 0)
{
$tva[$idx_tva]=bcadd($tva_item,$tva[$idx_tva]);
$tva[$idx_tva]=round($tva[$idx_tva],2);
$tot_tva = bcadd($tva_item, $tot_tva);
$tot_tva = round($tot_tva, 2);
}
else
{
$tva[$idx_tva]=$tva_item;
}
if ($oTva->get_parameter("both_side") == 0) {
$tot_tva = bcadd($tva_item, $tot_tva);
$tot_tva = round($tot_tva, 2);
} else {
$n_both = $tva_item;
$tva_item_currency = 0;
if ($n_both<0)
{
$tot_debit=round(bcadd($tot_debit, abs($n_both)),2);
$tva_reverse[$idx_tva]=(isset($tva_reverse[$idx_tva]))?$tva_reverse[$idx_tva]:0;
$tva_reverse[$idx_tva]=bcadd($tva_item,$tva_reverse[$idx_tva]);
$tva_reverse[$idx_tva]=round($tva_reverse[$idx_tva],2);
} else {
$tva[$idx_tva]=bcadd($tva_item,$tva[$idx_tva]);
$tva[$idx_tva]=round($tva[$idx_tva],2);
}
}
}
@ -548,20 +559,16 @@ class Acc_Ledger_Sale extends Acc_Ledger {
$operation_currency->j_id=$let_tiers ;
$operation_currency->insert();
/** save all vat
/**************************************************************************************************
* save all vat
* $i contains the tva_id and value contains the vat amount
* if if ($g_parameter->MY_TVA_USE == 'Y' )
*/
************************************************************************************************** */
if ($g_parameter->MY_TVA_USE == 'Y') {
if ( DEBUGNOALYSS > 1 ) {
var_dump($tva);
}
foreach ($tva as $i => $value) {
$oTva = Acc_Tva::build($this->db,$i);
$oTva->load();
$poste_vat = $oTva->get_side('c');
$cust_amount = bcadd($tot_amount, $tot_tva);
@ -578,13 +585,11 @@ class Acc_Ledger_Sale extends Acc_Ledger {
$tot_debit=bcadd($tot_debit, abs($value));
$tot_debit=round($tot_debit, 2);
}
if ( $oTva->get_parameter("both_side") == 1 && $value ==0 ) continue;
$acc_operation->insert_jrnx();
if ( DEBUGNOALYSS > 1 ) {
echo __LINE__." tot_tva $tot_tva<br>";
}
// if TVA is on both side, we deduce it immediately
if ($oTva->get_parameter("both_side") == 1) {
if ($oTva->get_parameter("both_side") == 1 ) {
// $x temp variable is the tva_reverse_account and will be used to check $poste_vat
$x=$oTva->get_parameter("tva_reverse_account");
@ -604,7 +609,52 @@ class Acc_Ledger_Sale extends Acc_Ledger {
$tot_debit = round($tot_debit, 2);
$n_both = $value;
}
}
foreach ($tva_reverse as $i => $value) {
$oTva = Acc_Tva::build($this->db,$i);
$poste_vat = $oTva->get_side('c');
if ( $poste_vat == '#')
{
$poste_vat=$oTva->get_side('d');
}
$acc_operation = new Acc_Operation($this->db);
$acc_operation->date = $e_date;
$acc_operation->poste = $poste_vat;
$acc_operation->amount = $value;
$acc_operation->grpt = $seq;
$acc_operation->jrn = $p_jrn;
$acc_operation->type = 'c';
$acc_operation->periode = $tperiode;
if ($value<0)
{
$tot_debit=bcadd($tot_debit, abs($value));
$tot_debit=round($tot_debit, 2);
}
$acc_operation->insert_jrnx();
// if TVA is on both side, we deduce it immediately
$poste_vat = $oTva->get_side('d');
if ( $poste_vat == '#')
{
$poste_vat=$oTva->get_side('c');
}
$acc_operation = new Acc_Operation($this->db);
$acc_operation->date = $e_date;
$acc_operation->poste = $poste_vat;
$acc_operation->amount = $value;
$acc_operation->grpt = $seq;
$acc_operation->jrn = $p_jrn;
$acc_operation->type = 'd';
$acc_operation->periode = $tperiode;
$acc_operation->insert_jrnx();
$tot_debit = bcadd($tot_debit, $value);
$tot_debit = round($tot_debit, 2);
$n_both = $value;
}
} // if ($g_parameter->MY_TVA_USE=='Y')
/*
* Balance the amount on D and C , the difference must be inserted as "difference due to a rounded value"

View file

@ -860,9 +860,12 @@ EOF;
$cn=Dossier::connect();
$g_user=new Noalyss_user($cn);
$a=new Acc_Operation($cn);
$a->jr_id=1444;
$a->jr_id=993;
$b=$a->get_quant();
echo h1('contain of get_quant() ');
var_dump($b);
echo h1('contain of get_jrnx_detail()');
var_dump($a->get_jrnx_detail());
}
/**
* Return a select object to choose the type of operation