2
0
forked from Wavyzz/dolibarr

Fix: [ bug #397 ] Product VAT is not coming from buying price

This commit is contained in:
Laurent Destailleur
2012-05-08 21:55:52 +02:00
parent a772fff5ed
commit 161846be77
4 changed files with 39 additions and 27 deletions

View File

@@ -2717,12 +2717,13 @@ function get_localtax($tva, $local, $societe_acheteuse="")
* 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 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 * @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 * TODO May be this should be better as a method of product class
*/ */
function get_product_vat_for_country($idprod, $countrycode) function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournprice=0)
{ {
global $db,$mysoc; global $db,$mysoc;
@@ -2735,9 +2736,17 @@ function get_product_vat_for_country($idprod, $countrycode)
$product=new Product($db); $product=new Product($db);
$result=$product->fetch($idprod); $result=$product->fetch($idprod);
if ($mysoc->pays_code == $countrycode) // If selling country is ours if ($mysoc->pays_code == $thirdparty_seller->country_code) // If selling country is ours
{ {
$ret=$product->tva_tx; // Default vat of product we defined if ($idprodfournprice > 0) // We want vat for product for a supplier order or invoice
{
$product->get_buyprice($idprodfournprice,0,0,0);
$ret=$product->vatrate_supplier;
}
else
{
$ret=$product->tva_tx; // Default vat of product we defined
}
$found=1; $found=1;
} }
else else
@@ -2753,7 +2762,7 @@ function get_product_vat_for_country($idprod, $countrycode)
// If vat of product for the country not found or not defined, we return higher vat of country. // If vat of product for the country not found or not defined, we return higher vat of country.
$sql.="SELECT taux as vat_rate"; $sql.="SELECT taux as vat_rate";
$sql.=" FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p"; $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='".$countrycode."'"; $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.=" ORDER BY t.taux DESC, t.recuperableonly ASC";
$sql.=$db->plimit(1); $sql.=$db->plimit(1);
@@ -2807,16 +2816,17 @@ function get_product_localtax_for_country($idprod, $local, $countrycode)
* @param Societe $societe_vendeuse Objet societe vendeuse * @param Societe $societe_vendeuse Objet societe vendeuse
* @param Societe $societe_acheteuse Objet societe acheteuse * @param Societe $societe_acheteuse Objet societe acheteuse
* @param int $idprod Id product * @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 * @return float Taux de tva a appliquer, -1 si ne peut etre determine
*/ */
function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0) function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0, $idprodfournprice=0)
{ {
global $conf; global $conf;
if (!is_object($societe_vendeuse)) return -1; if (!is_object($societe_vendeuse)) return -1;
if (!is_object($societe_acheteuse)) return -1; if (!is_object($societe_acheteuse)) return -1;
dol_syslog("get_default_tva: seller use vat=".$societe_vendeuse->tva_assuj.", seller country=".$societe_vendeuse->pays_code.", seller in cee=".$societe_vendeuse->isInEEC().", buyer country=".$societe_acheteuse->pays_code.", buyer in cee=".$societe_acheteuse->isInEEC().", idprod=".$idprod.", SERVICE_ARE_ECOMMERCE_200238EC=".$conf->global->SERVICES_ARE_ECOMMERCE_200238EC); dol_syslog("get_default_tva: seller use vat=".$societe_vendeuse->tva_assuj.", seller country=".$societe_vendeuse->pays_code.", seller in cee=".$societe_vendeuse->isInEEC().", buyer country=".$societe_acheteuse->pays_code.", buyer in cee=".$societe_acheteuse->isInEEC().", idprod=".$idprod.", idprodfournprice=".$idprodfournprice.", SERVICE_ARE_ECOMMERCE_200238EC=".$conf->global->SERVICES_ARE_ECOMMERCE_200238EC);
// Si vendeur non assujeti a TVA (tva_assuj vaut 0/1 ou franchise/reel) // Si vendeur non assujeti a TVA (tva_assuj vaut 0/1 ou franchise/reel)
if (is_numeric($societe_vendeuse->tva_assuj) && ! $societe_vendeuse->tva_assuj) if (is_numeric($societe_vendeuse->tva_assuj) && ! $societe_vendeuse->tva_assuj)
@@ -2838,7 +2848,7 @@ function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0)
|| (in_array($societe_vendeuse->country_code,array('FR,MC')) && in_array($societe_acheteuse->country_code,array('FR','MC')))) // Warning ->country_code not always defined || (in_array($societe_vendeuse->country_code,array('FR,MC')) && in_array($societe_acheteuse->country_code,array('FR','MC')))) // Warning ->country_code not always defined
{ {
//print 'VATRULE 3'; //print 'VATRULE 3';
return get_product_vat_for_country($idprod,$societe_vendeuse->country_code); return get_product_vat_for_country($idprod,$societe_vendeuse,$idprodfournprice);
} }
// Si (vendeur et acheteur dans Communaute europeenne) et (bien vendu = moyen de transports neuf comme auto, bateau, avion) alors TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle. // Si (vendeur et acheteur dans Communaute europeenne) et (bien vendu = moyen de transports neuf comme auto, bateau, avion) alors TVA par defaut=0 (La TVA doit etre paye par l'acheteur au centre d'impots de son pays et non au vendeur). Fin de regle.
@@ -2857,7 +2867,7 @@ function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0)
else else
{ {
//print 'VATRULE 5'; //print 'VATRULE 5';
return get_product_vat_for_country($idprod,$societe_vendeuse->country_code); return get_product_vat_for_country($idprod,$societe_vendeuse,$idprodfournprice);
} }
} }
@@ -2869,7 +2879,7 @@ function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0)
if (! $societe_vendeuse->isInEEC() && $societe_acheteuse->isInEEC() && ! $societe_acheteuse->isACompany()) if (! $societe_vendeuse->isInEEC() && $societe_acheteuse->isInEEC() && ! $societe_acheteuse->isACompany())
{ {
//print 'VATRULE 6'; //print 'VATRULE 6';
return get_product_vat_for_country($idprod,$societe_acheteuse->country_code); return get_product_vat_for_country($idprod,$societe_acheteuse,$idprodfournprice);
} }
} }

View File

@@ -178,7 +178,7 @@ else if ($action == 'addline' && $user->rights->fournisseur->commande->creer)
$qty = $_POST['qty'] ? $_POST['qty'] : $_POST['pqty']; $qty = $_POST['qty'] ? $_POST['qty'] : $_POST['pqty'];
$productsupplier = new ProductFournisseur($db); $productsupplier = new ProductFournisseur($db);
$idprod=$productsupplier->get_buyprice($_POST['idprodfournprice'], $qty); $idprod=$productsupplier->get_buyprice($_POST['idprodfournprice'], $qty); // Just to see if a price exists for the quantity. Not used to found vat
if ($idprod > 0) if ($idprod > 0)
{ {
@@ -194,7 +194,7 @@ else if ($action == 'addline' && $user->rights->fournisseur->commande->creer)
$remise_percent = $_POST["remise_percent"] ? $_POST["remise_percent"] : $_POST["p_remise_percent"]; $remise_percent = $_POST["remise_percent"] ? $_POST["remise_percent"] : $_POST["p_remise_percent"];
$tva_tx = get_default_tva($object->thirdparty,$mysoc,$productsupplier->id); $tva_tx = get_default_tva($object->thirdparty, $mysoc, $productsupplier->id, $_POST['idprodfournprice']);
$type = $productsupplier->type; $type = $productsupplier->type;
// Local Taxes // Local Taxes
@@ -1771,4 +1771,4 @@ if ($id > 0 || ! empty($ref))
llxFooter(); llxFooter();
$db->close(); $db->close();
?> ?>

View File

@@ -459,7 +459,7 @@ elseif ($action == 'addline')
if ($_POST['idprodfournprice']) // > 0 or -1 if ($_POST['idprodfournprice']) // > 0 or -1
{ {
$product=new Product($db); $product=new Product($db);
$idprod=$product->get_buyprice($_POST['idprodfournprice'], $_POST['qty']); $idprod=$product->get_buyprice($_POST['idprodfournprice'], $_POST['qty']); // Just to see if a price exists for the quantity. Not used to found vat
if ($idprod > 0) if ($idprod > 0)
{ {
@@ -469,7 +469,7 @@ elseif ($action == 'addline')
// $label = '['.$product->ref.'] - '. $product->libelle; // $label = '['.$product->ref.'] - '. $product->libelle;
$label = $product->description; $label = $product->description;
$tvatx=get_default_tva($object->thirdparty,$mysoc,$product->id); $tvatx=get_default_tva($object->thirdparty, $mysoc, $product->id, $_POST['idprodfournprice']);
$localtax1tx= get_localtax($tvatx, 1, $mysoc); $localtax1tx= get_localtax($tvatx, 1, $mysoc);
$localtax2tx= get_localtax($tvatx, 2, $mysoc); $localtax2tx= get_localtax($tvatx, 2, $mysoc);

View File

@@ -807,12 +807,12 @@ class Product extends CommonObject
{ {
$result = 0; $result = 0;
$sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity,"; $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity,";
$sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc"; $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.fk_soc, pfp.tva_tx";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE pfp.rowid = ".$prodfournprice; $sql.= " WHERE pfp.rowid = ".$prodfournprice;
$sql.= " AND pfp.quantity <= ".$qty; if ($qty) $sql.= " AND pfp.quantity <= ".$qty;
dol_syslog(get_class($this)."get_buyprice sql=".$sql); dol_syslog(get_class($this)."::get_buyprice sql=".$sql);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@@ -821,15 +821,16 @@ class Product extends CommonObject
{ {
$this->buyprice = $obj->price; // \deprecated $this->buyprice = $obj->price; // \deprecated
$this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id $this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id
$this->ref_fourn = $obj->ref_fourn; $this->ref_fourn = $obj->ref_fourn; // Ref supplier
$this->vatrate_supplier = $obj->tva_tx; // Vat ref supplier
$result=$obj->fk_product; $result=$obj->fk_product;
return $result; return $result;
} }
else else
{ {
// On refait le meme select sur la ref et l'id du produit // On refait le meme select sur la ref et l'id du produit
$sql = "SELECT pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,"; $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,";
$sql.= " pfp.fk_product, pfp.ref_fourn"; $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.tva_tx";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
$sql.= " WHERE pfp.ref_fourn = '".$fourn_ref."'"; $sql.= " WHERE pfp.ref_fourn = '".$fourn_ref."'";
$sql.= " AND pfp.fk_product = ".$product_id; $sql.= " AND pfp.fk_product = ".$product_id;
@@ -837,7 +838,7 @@ class Product extends CommonObject
$sql.= " ORDER BY pfp.quantity DESC"; $sql.= " ORDER BY pfp.quantity DESC";
$sql.= " LIMIT 1"; $sql.= " LIMIT 1";
dol_syslog(get_class($this)."get_buyprice sql=".$sql); dol_syslog(get_class($this)."::get_buyprice sql=".$sql);
$resql = $this->db->query($sql); $resql = $this->db->query($sql);
if ($resql) if ($resql)
{ {
@@ -846,7 +847,8 @@ class Product extends CommonObject
{ {
$this->buyprice = $obj->price; // \deprecated $this->buyprice = $obj->price; // \deprecated
$this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id $this->fourn_pu = $obj->price / $obj->quantity; // Prix unitaire du produit pour le fournisseur $fourn_id
$this->ref_fourn = $obj->ref_fourn; $this->ref_fourn = $obj->ref_fourn; // Ref supplier
$this->vatrate_supplier = $obj->tva_tx; // Vat ref supplier
$result=$obj->fk_product; $result=$obj->fk_product;
return $result; return $result;
} }
@@ -858,7 +860,7 @@ class Product extends CommonObject
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dol_syslog("Product:get_buyprice ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::get_buyprice ".$this->error, LOG_ERR);
return -3; return -3;
} }
} }
@@ -866,7 +868,7 @@ class Product extends CommonObject
else else
{ {
$this->error=$this->db->error(); $this->error=$this->db->error();
dol_syslog("Product:get_buyprice ".$this->error, LOG_ERR); dol_syslog(get_class($this)."::get_buyprice ".$this->error, LOG_ERR);
return -2; return -2;
} }
} }