Skip to content
Snippets Groups Projects
Commit 831fe6a2 authored by Laurent Destailleur's avatar Laurent Destailleur
Browse files

Fix: More fix about the new localtax system

parent e5fdb468
Branches
Tags
No related merge requests found
...@@ -2681,13 +2681,15 @@ function price2num($amount,$rounding='',$alreadysqlnb=0) ...@@ -2681,13 +2681,15 @@ function price2num($amount,$rounding='',$alreadysqlnb=0)
} }
/** /**
* Return localtaxe rate for a particular vat * Return localtax rate for a particular vat, when selling a product with vat $tva, from a $thirdparty_buyer to a $thirdparty_seller
* Note: It applies same rule than get_default_tva
* *
* @param float $tva Vat taxe * @param float $tva Vat taxe
* @param int $local Local tax to search and return (1 or 2 return only tax rate 1 or tax rate 2) * @param int $local Local tax to search and return (1 or 2 return only tax rate 1 or tax rate 2)
* @param Societe $thirdparty_buyer Object of buying third party * @param Societe $thirdparty_buyer Object of buying third party
* @param Societe $thirdparty_seller Object of selling third party * @param Societe $thirdparty_seller Object of selling third party
* @return int 0 if not found, localtax if found * @return int 0 if not found, localtax if found
* @see get_default_tva
*/ */
function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="") function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
{ {
...@@ -2707,8 +2709,6 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="") ...@@ -2707,8 +2709,6 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
if (is_object($thirdparty_buyer)) if (is_object($thirdparty_buyer))
{ {
if ($code_country != $thirdparty_buyer->country_code) return 0; if ($code_country != $thirdparty_buyer->country_code) return 0;
if ($local==1 && !$thirdparty_buyer->localtax1_assuj) return 0; // TODO Not sure this is good
elseif ($local==2 && !$thirdparty_buyer->localtax2_assuj) return 0; // TODO Not sure this is good
} }
// Search local taxes // Search local taxes
...@@ -2731,14 +2731,13 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="") ...@@ -2731,14 +2731,13 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="")
} }
/** /**
* Return vat rate of a product in a particular selling country or default country * Return vat rate of a product in a particular selling country or default country vat if product is unknown
* vat if product is unknown
* *
* @param int $idprod Id of product or 0 if not a predefined product * @param int $idprod Id of product or 0 if not a predefined product
* @param Societe $thirdparty_seller Thirdparty with a ->country_code defined (FR, US, IT, ...) * @param Societe $thirdparty_seller Thirdparty with a ->country_code defined (FR, US, IT, ...)
* @param int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice) * @param int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice)
* @return int <0 if KO, Vat rate if OK * @return int <0 if KO, Vat rate if OK
* TODO May be this should be better as a method of product class * @see get_product_localtax_for_country
*/ */
function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournprice=0) function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournprice=0)
{ {
...@@ -2804,25 +2803,71 @@ function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournpr ...@@ -2804,25 +2803,71 @@ function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournpr
} }
/** /**
* Return localtax rate of a product in a particular selling country * Return localtax vat rate of a product in a particular selling country or default country vat if product is unknown
* *
* @param int $idprod Id of product * @param int $idprod Id of product
* @param int $local 1 for localtax1, 2 for localtax 2 * @param int $local 1 for localtax1, 2 for localtax 2
* @param string $countrycode Country code (FR, US, IT, ...) * @param Societe $thirdparty_seller Thirdparty with a ->country_code defined (FR, US, IT, ...)
* @return int <0 if KO, Vat rate if OK * @return int <0 if KO, Vat rate if OK
* TODO May be this should be better as a method of product class * @see get_product_vat_for_country
*/ */
function get_product_localtax_for_country($idprod, $local, $countrycode) function get_product_localtax_for_country($idprod, $local, $thirdparty_seller)
{ {
global $db; global $db,$mysoc;
if (! class_exists('Product')) {
require DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
}
$ret=0;
$found=0;
if ($idprod > 0)
{
// Load product
$product=new Product($db); $product=new Product($db);
$product->fetch($idprod); $result=$product->fetch($idprod);
if ($local==1) return $product->localtax1_tx; if ($mysoc->country_code == $thirdparty_seller->country_code) // If selling country is ours
elseif ($local==2) return $product->localtax2_tx; {
/* Not defined yet, so we don't use this
if ($local==1) $ret=$product->localtax1_tx;
elseif ($local==2) $ret=$product->localtax2_tx;
$found=1;
*/
}
else
{
// TODO Read default product vat according to countrycode and product
return -1;
}
}
if (! $found)
{
// If vat of product for the country not found or not defined, we return higher vat of country.
$sql = "SELECT taux as vat_rate, localtax1, localtax2";
$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p";
$sql.= " WHERE t.active=1 AND t.fk_pays = p.rowid AND p.code='".$thirdparty_seller->country_code."'";
$sql.= " ORDER BY t.taux DESC, t.recuperableonly ASC";
$sql.= $db->plimit(1);
$resql=$db->query($sql);
if ($resql)
{
$obj=$db->fetch_object($resql);
if ($obj)
{
if ($local==1) $ret=$obj->localtax1;
elseif ($local==2) $ret=$obj->localtax2;
}
}
else dol_print_error($db);
}
dol_syslog("get_product_localtax_for_country: ret=".$ret);
return $ret;
} }
/** /**
...@@ -2839,6 +2884,7 @@ function get_product_localtax_for_country($idprod, $local, $countrycode) ...@@ -2839,6 +2884,7 @@ function get_product_localtax_for_country($idprod, $local, $countrycode)
* @param int $idprod Id product * @param int $idprod Id product
* @param int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice) * @param int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice)
* @return float Taux de tva a appliquer, -1 si ne peut etre determine * @return float Taux de tva a appliquer, -1 si ne peut etre determine
* @see get_default_localtax
*/ */
function get_default_tva($thirdparty_seller, $thirdparty_buyer, $idprod=0, $idprodfournprice=0) function get_default_tva($thirdparty_seller, $thirdparty_buyer, $idprod=0, $idprodfournprice=0)
{ {
...@@ -2926,36 +2972,40 @@ function get_default_npr($thirdparty_seller, $thirdparty_buyer, $idprod) ...@@ -2926,36 +2972,40 @@ function get_default_npr($thirdparty_seller, $thirdparty_buyer, $idprod)
/** /**
* Function that return localtax of a product line (according to seller, buyer and product vat rate) * Function that return localtax of a product line (according to seller, buyer and product vat rate)
* Si vendeur non assujeti a TVA, TVA par defaut=0. Fin de regle.
* Si le (pays vendeur = pays acheteur) alors TVA par defaut=TVA du produit vendu. Fin de regle.
* Sinon TVA proposee par defaut=0. Fin de regle.
* *
* @param Societe $thirdparty_seller Objet societe vendeuse * @param Societe $thirdparty_seller Objet societe vendeuse
* @param Societe $thirdparty_buyer Objet societe acheteuse * @param Societe $thirdparty_buyer Objet societe acheteuse
* @param int $local Localtax to process (1 or 2) * @param int $local Localtax to process (1 or 2)
* @param int $idprod Id product * @param int $idprod Id product
* @return float Taux de localtax appliquer, -1 si ne peut etre determine * @return float localtax, -1 si ne peut etre determine
* @see get_default_tva
*/ */
function get_default_localtax($thirdparty_seller, $thirdparty_buyer, $local, $idprod=0) function get_default_localtax($thirdparty_seller, $thirdparty_buyer, $local, $idprod=0)
{ {
if (!is_object($thirdparty_seller)) return -1; if (!is_object($thirdparty_seller)) return -1;
if (!is_object($thirdparty_buyer)) return -1; if (!is_object($thirdparty_buyer)) return -1;
if ($thirdparty_seller->country_id=='ES' || $thirdparty_seller->country_code=='ES')
{
if ($local==1) //RE if ($local==1) //RE
{ {
// Si achatteur non assujeti a RE, localtax1 par default=0 // Si vendeur non assujeti a RE, localtax1 par default=0
if (is_numeric($thirdparty_buyer->localtax1_assuj) && ! $thirdparty_buyer->localtax1_assuj) return 0; if (is_numeric($thirdparty_seller->localtax1_assuj) && ! $thirdparty_seller->localtax1_assuj) return 0;
if (! is_numeric($thirdparty_buyer->localtax1_assuj) && $thirdparty_buyer->localtax1_assuj=='localtax1off') return 0; if (! is_numeric($thirdparty_seller->localtax1_assuj) && $thirdparty_seller->localtax1_assuj=='localtax1off') return 0;
} }
elseif ($local==2) //IRPF elseif ($local==2) //IRPF
{ {
// Si vendeur non assujeti a IRPF, localtax2 par default=0 // Si vendeur non assujeti a IRPF, localtax2 par default=0
if (is_numeric($thirdparty_seller->localtax2_assuj) && ! $thirdparty_seller->localtax2_assuj) return 0; if (is_numeric($thirdparty_seller->localtax2_assuj) && ! $thirdparty_seller->localtax2_assuj) return 0;
if (! is_numeric($thirdparty_seller->localtax2_assuj) && $thirdparty_seller->localtax2_assuj=='localtax2off') return 0; if (! is_numeric($thirdparty_seller->localtax2_assuj) && $thirdparty_seller->localtax2_assuj=='localtax2off') return 0;
} else return -1; }
if ($idprod) return get_product_localtax_for_country($idprod, $local, $thirdparty_seller->country_code); if ($thirdparty_seller->country_code == $thirdparty_buyer->country_code)
else return -1; {
return get_product_localtax_for_country($idprod, $local, $thirdparty_seller);
} }
return 0; return 0;
} }
......
...@@ -530,9 +530,9 @@ class FunctionsTest extends PHPUnit_Framework_TestCase ...@@ -530,9 +530,9 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$companyfr->country_code='FR'; $companyfr->country_code='FR';
$companyfr->tva_assuj=1; $companyfr->tva_assuj=1;
$companymo=new Societe($db); $companymc=new Societe($db);
$companymo->country_code='MC'; $companymc->country_code='MC';
$companymo->tva_assuj=1; $companymc->tva_assuj=1;
$companyit=new Societe($db); $companyit=new Societe($db);
$companyit->country_code='IT'; $companyit->country_code='IT';
...@@ -551,7 +551,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase ...@@ -551,7 +551,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$companyus->tva_intra=''; $companyus->tva_intra='';
// Test RULE 1-2 // Test RULE 1-2
$vat=get_default_tva($companyfrnovat,$companymo,0); $vat=get_default_tva($companyfrnovat,$companymc,0);
$this->assertEquals(0,$vat); $this->assertEquals(0,$vat);
// Test RULE 3 (FR-FR) // Test RULE 3 (FR-FR)
...@@ -559,7 +559,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase ...@@ -559,7 +559,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$this->assertEquals(19.6,$vat); $this->assertEquals(19.6,$vat);
// Test RULE 3 (FR-MC) // Test RULE 3 (FR-MC)
$vat=get_default_tva($companyfr,$companymo,0); $vat=get_default_tva($companyfr,$companymc,0);
$this->assertEquals(19.6,$vat); $this->assertEquals(19.6,$vat);
// Test RULE 4 (FR-IT) // Test RULE 4 (FR-IT)
...@@ -577,5 +577,92 @@ class FunctionsTest extends PHPUnit_Framework_TestCase ...@@ -577,5 +577,92 @@ class FunctionsTest extends PHPUnit_Framework_TestCase
$vat=get_default_tva($companyfr,$companyus,0); $vat=get_default_tva($companyfr,$companyus,0);
$this->assertEquals(0,$vat); $this->assertEquals(0,$vat);
} }
/**
* testGetDefaultTva
*
* @return void
*/
public function testGetDefaultLocalTax()
{
global $conf,$user,$langs,$db;
$this->savconf=$conf;
$this->savuser=$user;
$this->savlangs=$langs;
$this->savdb=$db;
$companyfrnovat=new Societe($db);
$companyfrnovat->country_code='FR';
$companyfrnovat->tva_assuj=0;
$companyfrnovat->localtax1_assuj=0;
$companyfrnovat->localtax2_assuj=0;
$companyes=new Societe($db);
$companyes->country_code='ES';
$companyes->tva_assuj=1;
$companyes->localtax1_assuj=1;
$companyes->localtax2_assuj=1;
$companymc=new Societe($db);
$companymc->country_code='MC';
$companymc->tva_assuj=1;
$companymc->localtax1_assuj=0;
$companymc->localtax2_assuj=0;
$companyit=new Societe($db);
$companyit->country_code='IT';
$companyit->tva_assuj=1;
$companyit->tva_intra='IT99999';
$companyit->localtax1_assuj=0;
$companyit->localtax2_assuj=0;
$notcompanyit=new Societe($db);
$notcompanyit->country_code='IT';
$notcompanyit->tva_assuj=1;
$notcompanyit->tva_intra='';
$notcompanyit->typent_code='TE_PRIVATE';
$notcompanyit->localtax1_assuj=0;
$notcompanyit->localtax2_assuj=0;
$companyus=new Societe($db);
$companyus->country_code='US';
$companyus->tva_assuj=1;
$companyus->tva_intra='';
$companyus->localtax1_assuj=0;
$companyus->localtax2_assuj=0;
// Test RULE FR-MC
$vat1=get_default_localtax($companyfrnovat,$companymc,1,0);
$vat2=get_default_localtax($companyfrnovat,$companymc,2,0);
$this->assertEquals(0,$vat1);
$this->assertEquals(0,$vat2);
// Test RULE ES-ES
$vat1=get_default_localtax($companyes,$companyes,1,0);
$vat2=get_default_localtax($companyes,$companyes,2,0);
$this->assertEquals(5.2,$vat1);
$this->assertEquals(-15,$vat2);
// Test RULE ES-IT
$vat1=get_default_localtax($companyes,$companyit,1,0);
$vat2=get_default_localtax($companyes,$companyit,2,0);
$this->assertEquals(0,$vat1);
$this->assertEquals(0,$vat2);
// Test RULE ES-IT
$vat1=get_default_localtax($companyes,$notcompanyit,1,0);
$vat2=get_default_localtax($companyes,$notcompanyit,2,0);
$this->assertEquals(0,$vat1);
$this->assertEquals(0,$vat2);
// Test RULE FR-IT
// Not tested
// Test RULE ES-US
$vat1=get_default_localtax($companyes,$companyus,1,0);
$vat2=get_default_localtax($companyes,$companyus,2,0);
$this->assertEquals(0,$vat1);
$this->assertEquals(0,$vat2);
}
} }
?> ?>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment