diff --git a/htdocs/lib/functions.lib.php b/htdocs/lib/functions.lib.php index 58ee3f6847a..be7ded70566 100644 --- a/htdocs/lib/functions.lib.php +++ b/htdocs/lib/functions.lib.php @@ -2525,6 +2525,32 @@ function price2num($amount,$rounding='',$alreadysqlnb=0) return $amount; } +/** + * \brief Return localtaxe rate for a particular tva + * \param tva Vat taxe + * \param local Local taxe to search and return + */ +function get_localtax($tva, $local=0) +{ + global $db, $conf, $mysoc; + + $code_pays=$mysoc->pays_code; + + // Search local taxes + $sql = "SELECT t.localtax1, t.localtax2"; + $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_pays as p"; + $sql .= " WHERE t.fk_pays = p.rowid AND p.code = '".$code_pays."'"; + $sql .= " AND t.taux =".$tva." AND t.active = 1"; + $sql .= " ORDER BY t.localtax1 ASC, t.localtax2 ASC"; + + $resql=$db->query($sql); + if ($resql) + { + $obj = $db->fetch_object($resql); + if ($local==1) return $obj->localtax1; + elseif ($local==2) return $obj->localtax2; + } +} /** * \brief Return vat rate of a product in a particular selling country diff --git a/htdocs/product/fiche.php b/htdocs/product/fiche.php index 3390065e8d3..8bde602eb92 100644 --- a/htdocs/product/fiche.php +++ b/htdocs/product/fiche.php @@ -5,6 +5,7 @@ * Copyright (C) 2005-2010 Regis Houssin * Copyright (C) 2006 Andre Cianfarani * Copyright (C) 2006 Auguria SARL + * Copyright (C) 2010 Juanjo Menent * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -139,6 +140,11 @@ if ($_POST["action"] == 'add' && ($user->rights->produit->creer || $user->rights if ($product->price_base_type == 'TTC') $product->price_min_ttc = $_POST["price_min"]; else $product->price_min = $_POST["price_min"]; $product->tva_tx = $_POST["tva_tx"]; + + // local taxes. + $product->localtax1_tx = get_localtax($product->tva_tx,1); + $product->localtax2_tx = get_localtax($product->tva_tx,2); + $product->type = $_POST["type"]; $product->status = $_POST["statut"]; $product->description = dol_htmlcleanlastbr($_POST["desc"]); diff --git a/htdocs/product/product.class.php b/htdocs/product/product.class.php index 86bf8f65908..d784649f40b 100644 --- a/htdocs/product/product.class.php +++ b/htdocs/product/product.class.php @@ -696,7 +696,7 @@ class Product extends CommonObject $sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,envente,tva_tx,"; $sql.= " localtax1_tx, localtax2_tx, price_min,price_min_ttc) "; $sql.= " VALUES(".($level?$level:1).", ".$this->db->idate(mktime()).",".$this->id.",".$user->id.",".$this->price.",".$this->price_ttc.",'".$this->price_base_type."',".$this->status.",".$this->tva_tx.","; - $sql.= " ".$this->localtax1_tx.",".$this->localtax1_tx.",".$this->price_min.",".$this->price_min_ttc; + $sql.= " ".$this->localtax1_tx.",".$this->localtax2_tx.",".$this->price_min.",".$this->price_min_ttc; $sql.= ")"; dol_syslog("Product::_log_price sql=".$sql); @@ -884,7 +884,11 @@ class Product extends CommonObject } } //print 'x'.$id.'-'.$newprice.'-'.$newpricebase.'-'.$price.'-'.$price_ttc.'-'.$price_min.'-'.$price_min_ttc; - + + //Local taxes + $localtax1=get_localtax($newvat,1); + $localtax2=get_localtax($newvat,2); + // Ne pas mettre de quote sur le numeriques decimaux. // Ceci provoque des stockage avec arrondis en base au lieu des valeurs exactes. $sql = "UPDATE ".MAIN_DB_PREFIX."product SET"; @@ -893,6 +897,8 @@ class Product extends CommonObject $sql.= " price_ttc=".$price_ttc.","; $sql.= " price_min=".$price_min.","; $sql.= " price_min_ttc=".$price_min_ttc.","; + $sql.= " localtax1_tx=".$localtax1.","; + $sql.= " localtax2_tx=".$localtax2.","; $sql.= " tva_tx='".price2num($newvat)."'"; $sql.= " WHERE rowid = " . $id; @@ -906,7 +912,10 @@ class Product extends CommonObject $this->price_min_ttc = $price_min_ttc; $this->price_base_type = $newpricebase; $this->tva_tx = $newvat; - + //Local taxes + $this->localtax1_tx = $localtax1; + $this->localtax2_tx = $localtax2; + $this->_log_price($user,$level); } else