diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 78f584773c860e61d75b56fd628aad87556b3bf6..394d7e8f89f0d54cb6da898b4cf3228ef3b13dba 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -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 * vat if product is unknown * - * @param int $idprod Id of product or 0 if not a predefined product - * @param string $countrycode Country code (FR, US, IT, ...) - * @return int <0 if KO, Vat rate if OK + * @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 */ -function get_product_vat_for_country($idprod, $countrycode) +function get_product_vat_for_country($idprod, $thirdparty_seller, $idprodfournprice=0) { global $db,$mysoc; @@ -2735,9 +2736,17 @@ function get_product_vat_for_country($idprod, $countrycode) $product=new Product($db); $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; } 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. $sql.="SELECT taux as vat_rate"; $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.=$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_acheteuse Objet societe acheteuse * @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 */ -function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0) +function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0, $idprodfournprice=0) { global $conf; if (!is_object($societe_vendeuse)) 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) 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 { //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. @@ -2857,7 +2867,7 @@ function get_default_tva($societe_vendeuse, $societe_acheteuse, $idprod=0) else { //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()) { //print 'VATRULE 6'; - return get_product_vat_for_country($idprod,$societe_acheteuse->country_code); + return get_product_vat_for_country($idprod,$societe_acheteuse,$idprodfournprice); } } diff --git a/htdocs/fourn/commande/fiche.php b/htdocs/fourn/commande/fiche.php index 665e9c1deb742e030b3f2b430ced99135b22bdbb..a75d183d65c00560a3e2002fe377d4842ef1356a 100644 --- a/htdocs/fourn/commande/fiche.php +++ b/htdocs/fourn/commande/fiche.php @@ -178,7 +178,7 @@ else if ($action == 'addline' && $user->rights->fournisseur->commande->creer) $qty = $_POST['qty'] ? $_POST['qty'] : $_POST['pqty']; $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) { @@ -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"]; - $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; // Local Taxes @@ -1771,4 +1771,4 @@ if ($id > 0 || ! empty($ref)) llxFooter(); $db->close(); -?> \ No newline at end of file +?> diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index fb7d60693043c20ac93014bd207356d6cfe97ee8..f26b9199c948196dda078c3038407b7f4ad4c07c 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -459,7 +459,7 @@ elseif ($action == 'addline') if ($_POST['idprodfournprice']) // > 0 or -1 { $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) { @@ -469,7 +469,7 @@ elseif ($action == 'addline') // $label = '['.$product->ref.'] - '. $product->libelle; $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); $localtax2tx= get_localtax($tvatx, 2, $mysoc); diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index b58d68e4f0935c6a211c0288c9add7975649cc9c..7b53456c21cd620a87ef42e995d33f1ee21c2ce1 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -807,12 +807,12 @@ class Product extends CommonObject { $result = 0; $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.= " 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); if ($resql) { @@ -821,15 +821,16 @@ class Product extends CommonObject { $this->buyprice = $obj->price; // \deprecated $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; return $result; } else { // 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.= " pfp.fk_product, pfp.ref_fourn"; + $sql = "SELECT pfp.rowid, pfp.price as price, pfp.quantity as quantity, pfp.fk_soc,"; + $sql.= " pfp.fk_product, pfp.ref_fourn, pfp.tva_tx"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " WHERE pfp.ref_fourn = '".$fourn_ref."'"; $sql.= " AND pfp.fk_product = ".$product_id; @@ -837,7 +838,7 @@ class Product extends CommonObject $sql.= " ORDER BY pfp.quantity DESC"; $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); if ($resql) { @@ -846,7 +847,8 @@ class Product extends CommonObject { $this->buyprice = $obj->price; // \deprecated $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; return $result; } @@ -858,7 +860,7 @@ class Product extends CommonObject else { $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; } } @@ -866,7 +868,7 @@ class Product extends CommonObject else { $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; } }