diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 92ff891cca944a76f505353e6fc44c23fcbdeacb..73e8555a8a3aa4a8ea65f913b173205f5ff510a1 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -281,7 +281,7 @@ else if ($action == 'setconditions' && $user->rights->facture->creer) $object->cond_reglement_id=0; // To clean property $result=$object->setPaymentTerms(GETPOST('cond_reglement_id','int')); if ($result < 0) dol_print_error($db,$object->error); - + $old_date_lim_reglement=$object->date_lim_reglement; $new_date_lim_reglement=$object->calculate_date_lim_reglement(); if ($new_date_lim_reglement > $old_date_lim_reglement) $object->date_lim_reglement=$new_date_lim_reglement; @@ -2834,7 +2834,7 @@ else if ($id > 0 || ! empty($ref)) print '</table>'; // Margin Infos - if (! empty($conf->margin->enabled)) + if (! empty($conf->margin->enabled)) { print '<br>'; $object->displayMarginInfos($object->statut > 0); diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 4422e6d64535fa1028ad4b8754acdacdb16e9f5b..aef25a6d679ef224984a45cb08c6d405a4cc6e6c 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -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 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_seller Object of selling third party * @return int 0 if not found, localtax if found + * @see get_default_tva */ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="") { @@ -2706,9 +2708,7 @@ function get_localtax($tva, $local, $thirdparty_buyer="", $thirdparty_seller="") if (is_object($thirdparty_buyer)) { - 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 + if ($code_country != $thirdparty_buyer->country_code) return 0; } // Search local taxes @@ -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 - * vat if product is unknown + * Return vat rate of a product in a particular selling country or default country vat if product is unknown * * @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 int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice) * @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) { @@ -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 $local 1 for localtax1, 2 for localtax 2 - * @param string $countrycode Country code (FR, US, IT, ...) - * @return int <0 if KO, Vat rate if OK - * TODO May be this should be better as a method of product class + * @param int $idprod Id of product + * @param int $local 1 for localtax1, 2 for localtax 2 + * @param Societe $thirdparty_seller Thirdparty with a ->country_code defined (FR, US, IT, ...) + * @return int <0 if KO, Vat rate if OK + * @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; - - $product=new Product($db); - $product->fetch($idprod); + global $db,$mysoc; - if ($local==1) return $product->localtax1_tx; - elseif ($local==2) return $product->localtax2_tx; + if (! class_exists('Product')) { + require DOL_DOCUMENT_ROOT . '/product/class/product.class.php'; + } - return -1; + $ret=0; + $found=0; + + if ($idprod > 0) + { + // Load product + $product=new Product($db); + $result=$product->fetch($idprod); + + if ($mysoc->country_code == $thirdparty_seller->country_code) // If selling country is ours + { + /* 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 + + + } + } + + 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) * @param int $idprod Id product * @param int $idprodfournprice Id product_fournisseur_price (for supplier order/invoice) * @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) { @@ -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) + * 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_buyer Objet societe acheteuse * @param int $local Localtax to process (1 or 2) * @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) { if (!is_object($thirdparty_seller)) 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 - if (is_numeric($thirdparty_buyer->localtax1_assuj) && ! $thirdparty_buyer->localtax1_assuj) return 0; - if (! is_numeric($thirdparty_buyer->localtax1_assuj) && $thirdparty_buyer->localtax1_assuj=='localtax1off') return 0; - } - elseif ($local==2) //IRPF - { - // 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=='localtax2off') return 0; - } else return -1; + // Si vendeur non assujeti a RE, localtax1 par default=0 + if (is_numeric($thirdparty_seller->localtax1_assuj) && ! $thirdparty_seller->localtax1_assuj) return 0; + if (! is_numeric($thirdparty_seller->localtax1_assuj) && $thirdparty_seller->localtax1_assuj=='localtax1off') return 0; + } + elseif ($local==2) //IRPF + { + // 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=='localtax2off') return 0; + } - if ($idprod) return get_product_localtax_for_country($idprod, $local, $thirdparty_seller->country_code); - else return -1; + if ($thirdparty_seller->country_code == $thirdparty_buyer->country_code) + { + return get_product_localtax_for_country($idprod, $local, $thirdparty_seller); } + return 0; } diff --git a/test/phpunit/FunctionsTest.php b/test/phpunit/FunctionsTest.php index 9dc7ff30580aaaacd8d854379d7c1321d3558bdf..92400d36e9f77d0cad817a051a08ed823a4a64d9 100755 --- a/test/phpunit/FunctionsTest.php +++ b/test/phpunit/FunctionsTest.php @@ -530,9 +530,9 @@ class FunctionsTest extends PHPUnit_Framework_TestCase $companyfr->country_code='FR'; $companyfr->tva_assuj=1; - $companymo=new Societe($db); - $companymo->country_code='MC'; - $companymo->tva_assuj=1; + $companymc=new Societe($db); + $companymc->country_code='MC'; + $companymc->tva_assuj=1; $companyit=new Societe($db); $companyit->country_code='IT'; @@ -551,7 +551,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase $companyus->tva_intra=''; // Test RULE 1-2 - $vat=get_default_tva($companyfrnovat,$companymo,0); + $vat=get_default_tva($companyfrnovat,$companymc,0); $this->assertEquals(0,$vat); // Test RULE 3 (FR-FR) @@ -559,7 +559,7 @@ class FunctionsTest extends PHPUnit_Framework_TestCase $this->assertEquals(19.6,$vat); // Test RULE 3 (FR-MC) - $vat=get_default_tva($companyfr,$companymo,0); + $vat=get_default_tva($companyfr,$companymc,0); $this->assertEquals(19.6,$vat); // Test RULE 4 (FR-IT) @@ -577,5 +577,92 @@ class FunctionsTest extends PHPUnit_Framework_TestCase $vat=get_default_tva($companyfr,$companyus,0); $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