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

View file

@ -65,10 +65,88 @@ class Acc_Ledger_PurchaseTest extends TestCase
"p_currency_code"=>1
);
// create accounting for reversed VAT
// create accounting for reversed VAT with neg. amount
$g_connection->exec_sql("
INSERT INTO public.tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent,pcm_type,pcm_direct_use) VALUES
('4119999','TVA Test UNIT','411','ACT','Y') on conflict do nothing");
/// @var $array1 = used for autoreverse with 2 VAT Codes
$this->array1=array (
'e_client' => 'FOURNI1',
'nb_item' => '10',
'p_jrn' => '3',
'jrn_note_input' => '',
'e_comm' => 'Documentation',
'e_date' => '30.01.2020',
'e_ech' => '',
'jrn_type' => 'ACH',
'e_pj' => 'ACH53',
'e_pj_suggest' => 'ACH53',
'p_currency_rate' => '1',
'p_currency_code' => '0',
'mt' => '1734717784.385',
'e_mp' => '0',
'e_march0' => 'DOCUME',
'e_march0_price' => '120',
'e_march0_tva_id' => '5',
'e_march0_tva_amount' => '0',
'e_quant0' => '1',
'e_march1' => 'DOCUME',
'e_march1_price' => '-10',
'e_march1_tva_id' => '3',
'e_march1_tva_amount' => '0',
'e_quant1' => '1',
'e_march2' => '',
'e_march2_price' => '',
'e_march2_tva_id' => '',
'e_march2_tva_amount' => '',
'e_quant2' => '1',
'e_march3' => '',
'e_march3_price' => '',
'e_march3_tva_id' => '',
'e_march3_tva_amount' => '',
'e_quant3' => '1',
'e_march4' => '',
'e_march4_price' => '',
'e_march4_tva_id' => '',
'e_march4_tva_amount' => '',
'e_quant4' => '1',
'e_march5' => '',
'e_march5_price' => '',
'e_march5_tva_id' => '',
'e_march5_tva_amount' => '',
'e_quant5' => '1',
'e_march6' => '',
'e_march6_price' => '',
'e_march6_tva_id' => '',
'e_march6_tva_amount' => '',
'e_quant6' => '1',
'e_march7' => '',
'e_march7_price' => '',
'e_march7_tva_id' => '',
'e_march7_tva_amount' => '',
'e_quant7' => '1',
'e_march8' => '',
'e_march8_price' => '',
'e_march8_tva_id' => '',
'e_march8_tva_amount' => '',
'e_quant8' => '1',
'e_march9' => '',
'e_march9_price' => '',
'e_march9_tva_id' => '',
'e_march9_tva_amount' => '',
'e_quant9' => '1',
'ac' => 'COMPTA/MENUACH/ACH',
'bon_comm' => '',
'other_info' => '',
'opd_name' => '',
'od_description' => '',
'reverse_date' => '',
'ext_label' => '',
'jr_optype' => 'NOR',
'action_gestion' => '',
'record' => 'Enregistrement',
);
}
@ -648,4 +726,97 @@ class Acc_Ledger_PurchaseTest extends TestCase
$this->clean_operation();
}
/**
* @testdox Reverse VAT4 :Use 2 different VAT Autoreverse mix negative and positive amounts
* @covers Acc_Ledger_Sale::insert
* @return void
*/
function testInsertReverseVAT4() {
global $g_connection;
$array=$this->array1;
$old_autoreverse=$g_connection->get_value("select tva_both_side from tva_rate where tva_id=3 ");
// set autoreverse to 1
$g_connection->get_value("update tva_rate set tva_both_side = 1 where tva_id=3 ");
// clean
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
$this->object->insert($array);
$accounting=new \Acc_Operation($g_connection);
$accounting->jr_id=$this->object->jr_id;
$aResult=$accounting->get_jrnx_detail();
$this->assertTrue(count($aResult)==7, 'Number of rows is '.count($aResult)."instead of 7");
foreach($aResult as $result) {
switch ($result['j_poste']) {
case '41142':
$this->assertEquals(25.20, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '6194':
if ( $result['debit']=='D')
$this->assertEquals(120, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(10, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '4400005':
$this->assertEquals(110, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
}
}
// cancel change
$g_connection->get_value("update tva_rate set tva_both_side = $1 where tva_id=3 ",[$old_autoreverse]);
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
}
/**
* @testdox Reverse VAT5 :Use 2 same VAT Autoreverse mix negative and positive amounts
* @covers Acc_Ledger_Sale::insert
* @return void
*/
function testInsertReverseVAT5() {
global $g_connection;
$array=$this->array1;
$array['e_march1_tva_id']=5;
// clean
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
$this->object->insert($array);
$accounting=new \Acc_Operation($g_connection);
$accounting->jr_id=$this->object->jr_id;
$aResult=$accounting->get_jrnx_detail();
$this->assertTrue(count($aResult)==7, 'Number of rows is '.count($aResult)."instead of 7");
foreach($aResult as $result) {
switch ($result['j_poste']) {
case '41142':
if ( $result['debit']=='D')
$this->assertEquals(25.20, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(2.1, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '6194':
if ( $result['debit']=='D')
$this->assertEquals(120, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(10, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '4400005':
$this->assertEquals(110, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
}
}
// cancel change
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
}
}

View file

@ -74,8 +74,41 @@ class Acc_Ledger_SaleTest extends TestCase
$g_connection->exec_sql("
INSERT INTO public.tmp_pcmn (pcm_val,pcm_lib,pcm_val_parent,pcm_type,pcm_direct_use) VALUES
('4119999','TVA Test UNIT','411','ACT','Y') on conflict do nothing");
// create accounting for reversed VAT with neg. amount
$this->array1=array( 'e_client' => 'CLIENT1',
'nb_item' => '2',
'p_jrn' => '2',
'jrn_note_input' => '',
'mt' => '1734720448.0036',
'p_currency_rate' => '1',
'p_currency_code' => '0',
'e_comm' => '',
'e_date' => '27.06.2020',
'e_ech' => '',
'e_pj' => 'VEN41',
'e_pj_suggest' => 'VEN41',
'e_mp' => '0',
'jrn_type' => 'VEN',
'e_march0' => 'DEPLAC',
'e_march0_price' => '20',
'e_march0_tva_id' => '5',
'e_march0_tva_amount' => '0',
'e_quant0' => '1',
'e_march1' => 'DEPLAC',
'e_march1_price' => '-5',
'e_march1_tva_id' => '5',
'e_march1_tva_amount' => '0',
'e_quant1' => '1',
'ac' => 'COMPTA/VENMENU/VEN',
'bon_comm' => '',
'other_info' => '',
'opd_name' => '',
'od_description' => '',
'reverse_date' => '',
'ext_label' => '',
'jr_optype' => 'NOR',
'action_gestion' => '',
'record' => 'Enregistrement');
}
/**
@ -503,5 +536,100 @@ class Acc_Ledger_SaleTest extends TestCase
$ret=$this->object->get_detail_sale(92,103,'unpaid');
$this->assertEquals(5,Database::num_row($ret),'only unpaid operations');
}
/**
* @testdox Reverse VAT4 :Use 2 different VAT Autoreverse mix negative and positive amounts
* @covers Acc_Ledger_Sale::insert
* @return void
*/
function testInsertReverseVAT4() {
global $g_connection;
$array=$this->array1;
$old_autoreverse=$g_connection->get_value("select tva_both_side from tva_rate where tva_id=3 ");
// set autoreverse to 1
$g_connection->get_value("update tva_rate set tva_both_side = 1 where tva_id=3 ");
$array['e_march1_tva_id']=3;
// clean
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
$this->object->insert($array);
$accounting=new \Acc_Operation($g_connection);
$accounting->jr_id=$this->object->jr_id;
$aResult=$accounting->get_jrnx_detail();
$this->assertTrue(count($aResult)==7, 'Number of rows is '.count($aResult)."instead of 7");
foreach($aResult as $result) {
switch ($result['j_poste']) {
case '41142':
if ( $result['debit']=='D')
$this->assertEquals(4.20, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(1.05, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '7000005':
if ( $result['debit']=='D')
$this->assertEquals(5, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(20, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '4000005':
$this->assertEquals(15, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
}
}
// cancel change
$g_connection->get_value("update tva_rate set tva_both_side = $1 where tva_id=3 ",[$old_autoreverse]);
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
}
/**
* @testdox Reverse VAT5 :Use 2 same VAT Autoreverse mix negative and positive amounts
* @covers Acc_Ledger_Sale::insert
* @return void
*/
function testInsertReverseVAT5() {
global $g_connection;
$array=$this->array1;
$array['e_march1_tva_id']=5;
// clean
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
$this->object->insert($array);
$accounting=new \Acc_Operation($g_connection);
$accounting->jr_id=$this->object->jr_id;
$aResult=$accounting->get_jrnx_detail();
$this->assertTrue(count($aResult)==7, 'Number of rows is '.count($aResult)."instead of 7");
foreach($aResult as $result) {
switch ($result['j_poste']) {
case '41142':
if ( $result['debit']=='D')
$this->assertEquals(4.20, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(1.05, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '7000005':
if ( $result['debit']=='D')
$this->assertEquals(5, $result['j_montant'],"erreur account {$result['j_poste']}");
else
$this->assertEquals(20, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
case '4000005':
$this->assertEquals(15, $result['j_montant'],"erreur account {$result['j_poste']}");
break;
}
}
// cancel change
$g_connection->exec_sql("delete from jrn where jr_mt=$1",[1734717784.385]);
}
}