FIX Maxi serious problem when updating prices and vat of a product. All

mode (one price, price per level, ...) were impacted. Localtax setup
were lost.
This commit is contained in:
Laurent Destailleur 2016-02-19 02:41:00 +01:00
parent 3b5ce32cb0
commit 4d0b9946f5
8 changed files with 256 additions and 53 deletions

View File

@ -26,6 +26,16 @@
ALTER TABLE llx_don ADD COLUMN fk_country integer NOT NULL DEFAULT 0 after country;
ALTER TABLE llx_product ADD COLUMN localtax1_type varchar(10) NOT NULL DEFAULT '0' after localtax1_tx;
ALTER TABLE llx_product ADD COLUMN localtax2_type varchar(10) NOT NULL DEFAULT '0' after localtax2_tx;
ALTER TABLE llx_product_price ADD COLUMN localtax1_type varchar(10) NOT NULL DEFAULT '0' after localtax1_tx;
ALTER TABLE llx_product_price ADD COLUMN localtax2_type varchar(10) NOT NULL DEFAULT '0' after localtax2_tx;
ALTER TABLE llx_product_customer_price ADD COLUMN localtax1_type varchar(10) NOT NULL DEFAULT '0' after localtax1_tx;
ALTER TABLE llx_product_customer_price ADD COLUMN localtax2_type varchar(10) NOT NULL DEFAULT '0' after localtax2_tx;
ALTER TABLE llx_product_customer_price_log ADD COLUMN localtax1_type varchar(10) NOT NULL DEFAULT '0' after localtax1_tx;
ALTER TABLE llx_product_customer_price_log ADD COLUMN localtax2_type varchar(10) NOT NULL DEFAULT '0' after localtax2_tx;
UPDATE llx_user set api_key = null where api_key = '';

View File

@ -46,8 +46,10 @@ create table llx_product
cost_price double(24,8) DEFAULT NULL, -- Cost price without tax. Can be used for margin calculation.
tva_tx double(6,3), -- Default VAT rate of product
recuperableonly integer NOT NULL DEFAULT '0', -- French NPR VAT
localtax1_tx double(6,3) DEFAULT 0, -- Spanish local VAT 1
localtax2_tx double(6,3) DEFAULT 0, -- Spanish local VAT 2
localtax1_tx double(6,3) DEFAULT 0, --
localtax1_type varchar(10) NOT NULL DEFAULT '0', --
localtax2_tx double(6,3) DEFAULT 0, --
localtax2_type varchar(10) NOT NULL DEFAULT '0', --
fk_user_author integer DEFAULT NULL, -- user making creation
fk_user_modif integer, -- user making last change
tosell tinyint DEFAULT 1, -- Product you sell

View File

@ -36,7 +36,9 @@ create table llx_product_customer_price
tva_tx double(6,3),
recuperableonly integer NOT NULL DEFAULT '0', -- Other NPR VAT
localtax1_tx double(6,3) DEFAULT 0, -- Other local VAT 1
localtax1_type varchar(10) NOT NULL DEFAULT '0',
localtax2_tx double(6,3) DEFAULT 0, -- Other local VAT 2
localtax2_type varchar(10) NOT NULL DEFAULT '0',
fk_user integer,
import_key varchar(14) -- Import key
)ENGINE=innodb;

View File

@ -21,7 +21,7 @@
create table llx_product_customer_price_log
(
rowid integer AUTO_INCREMENT PRIMARY KEY,
entity integer DEFAULT 1 NOT NULL, -- multi company id
entity integer DEFAULT 1 NOT NULL, -- multi company id
datec datetime,
fk_product integer NOT NULL,
fk_soc integer NOT NULL,
@ -33,7 +33,9 @@ create table llx_product_customer_price_log
tva_tx double(6,3),
recuperableonly integer NOT NULL DEFAULT '0', -- Other NPR VAT
localtax1_tx double(6,3) DEFAULT 0, -- Other local VAT 1
localtax1_type varchar(10) NOT NULL DEFAULT '0',
localtax2_tx double(6,3) DEFAULT 0, -- Other local VAT 2
localtax2_type varchar(10) NOT NULL DEFAULT '0',
fk_user integer,
import_key varchar(14) -- Import key
)ENGINE=innodb;

View File

@ -34,7 +34,9 @@ create table llx_product_price
tva_tx double(6,3) NOT NULL,
recuperableonly integer NOT NULL DEFAULT '0',
localtax1_tx double(6,3) DEFAULT 0,
localtax1_type varchar(10) NOT NULL DEFAULT '0',
localtax2_tx double(6,3) DEFAULT 0,
localtax2_type varchar(10) NOT NULL DEFAULT '0',
fk_user_author integer,
tosell tinyint DEFAULT 1,
price_by_qty integer NOT NULL DEFAULT 0,

View File

@ -101,10 +101,12 @@ class Product extends CommonObject
var $tva_tx;
//! French VAT NPR (0 or 1)
var $tva_npr=0;
//! Spanish local taxes
//! Other local taxes
var $localtax1_tx;
var $localtax2_tx;
var $localtax1_type;
var $localtax2_type;
//! Stock
var $stock_reel;
//! Average price value for product entry into stock (PMP)
@ -294,7 +296,9 @@ class Product extends CommonObject
//Local taxes
if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
if (empty($this->localtax1_type)) $this->localtax1_type = '0';
if (empty($this->localtax2_type)) $this->localtax2_type = '0';
if (empty($this->price)) $this->price = 0;
if (empty($this->price_min)) $this->price_min = 0;
@ -631,6 +635,8 @@ class Product extends CommonObject
if (empty($this->tva_npr)) $this->tva_npr = 0;
if (empty($this->localtax1_tx)) $this->localtax1_tx = 0;
if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
if (empty($this->localtax1_type)) $this->localtax1_type = '0';
if (empty($this->localtax2_type)) $this->localtax2_type = '0';
if (empty($this->status)) $this->status = 0;
if (empty($this->status_buy)) $this->status_buy = 0;
@ -698,7 +704,9 @@ class Product extends CommonObject
$sql.= ", recuperableonly = " . $this->tva_npr;
$sql.= ", localtax1_tx = " . $this->localtax1_tx;
$sql.= ", localtax2_tx = " . $this->localtax2_tx;
$sql.= ", localtax1_type = " . ($this->localtax1_type!=''?"'".$this->localtax1_type."'":"'0'");
$sql.= ", localtax2_type = " . ($this->localtax2_type!=''?"'".$this->localtax2_type."'":"'0'");
$sql.= ", barcode = ". (empty($this->barcode)?"null":"'".$this->db->escape($this->barcode)."'");
$sql.= ", fk_barcode_type = ". (empty($this->barcode_type)?"null":$this->db->escape($this->barcode_type));
@ -1216,10 +1224,10 @@ class Product extends CommonObject
if (empty($this->price_by_qty)) $this->price_by_qty=0;
// Add new price
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,tosell,tva_tx,recuperableonly,";
$sql.= " localtax1_tx, localtax2_tx, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_price(price_level,date_price,fk_product,fk_user_author,price,price_ttc,price_base_type,tosell, tva_tx, recuperableonly,";
$sql.= " localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, price_min,price_min_ttc,price_by_qty,entity,fk_price_expression) ";
$sql.= " VALUES(".($level?$level:1).", '".$this->db->idate($now)."',".$this->id.",".$user->id.",".$this->price.",".$this->price_ttc.",'".$this->price_base_type."',".$this->status.",".$this->tva_tx.",".$this->tva_npr.",";
$sql.= " ".$this->localtax1_tx.",".$this->localtax2_tx.",".$this->price_min.",".$this->price_min_ttc.",".$this->price_by_qty.",".$conf->entity.",".($this->fk_price_expression > 0?$this->fk_price_expression:'null');
$sql.= " ".$this->localtax1_tx.", ".$this->localtax2_tx.", '".$this->localtax1_type."', '".$this->localtax2_type."', ".$this->price_min.",".$this->price_min_ttc.",".$this->price_by_qty.",".$conf->entity.",".($this->fk_price_expression > 0?$this->fk_price_expression:'null');
$sql.= ")";
dol_syslog(get_class($this)."::_log_price", LOG_DEBUG);
@ -1371,15 +1379,16 @@ class Product extends CommonObject
* @param double $newprice New price
* @param string $newpricebase HT or TTC
* @param User $user Object user that make change
* @param double $newvat New VAT Rate
* @param double $newvat New VAT Rate (For example 8.5. Should not be a string)
* @param double $newminprice New price min
* @param int $level 0=standard, >0 = level if multilevel prices
* @param int $newnpr 0=Standard vat rate, 1=Special vat rate for French NPR VAT
* @param int $newpsq 1 if it has price by quantity
* @param int $ignore_autogen Used to avoid infinite loops
* @return int <0 if KO, >0 if OK
* @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
* @return int <0 if KO, >0 if OK
*/
function updatePrice($newprice, $newpricebase, $user, $newvat='',$newminprice='', $level=0, $newnpr=0, $newpsq=0, $ignore_autogen=0)
function updatePrice($newprice, $newpricebase, $user, $newvat='',$newminprice='', $level=0, $newnpr=0, $newpsq=0, $ignore_autogen=0, $localtaxes_array=array())
{
global $conf,$langs;
@ -1448,13 +1457,23 @@ 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);
if (count($localtaxes_array) > 0)
{
$localtaxtype1=$localtaxes_array['0'];
$localtax1=$localtaxes_array['1'];
$localtaxtype2=$localtaxes_array['2'];
$localtax2=$localtaxes_array['3'];
}
else // old method. deprecated because ot can't retreive type
{
$localtaxtype1='0';
$localtax1=get_localtax($newvat,1);
$localtaxtype2='0';
$localtax2=get_localtax($newvat,2);
}
if (empty($localtax1)) $localtax1=0; // If = '' then = 0
if (empty($localtax2)) $localtax2=0; // If = '' then = 0
$this->db->begin();
// Ne pas mettre de quote sur les numeriques decimaux.
@ -1467,6 +1486,8 @@ class Product extends CommonObject
$sql.= " price_min_ttc=".$price_min_ttc.",";
$sql.= " localtax1_tx=".($localtax1>=0?$localtax1:'NULL').",";
$sql.= " localtax2_tx=".($localtax2>=0?$localtax2:'NULL').",";
$sql.= " localtax1_type=".($localtaxtype1!=''?"'".$localtaxtype1."'":"'0'").",";
$sql.= " localtax2_type=".($localtaxtype2!=''?"'".$localtaxtype2."'":"'0'").",";
$sql.= " tva_tx='".price2num($newvat)."',";
$sql.= " recuperableonly='".$newnpr."'";
$sql.= " WHERE rowid = ".$id;
@ -1493,7 +1514,9 @@ class Product extends CommonObject
//Local taxes
$this->localtax1_tx = $localtax1;
$this->localtax2_tx = $localtax2;
$this->localtax1_type = $localtaxtype1;
$this->localtax2_type = $localtaxtype2;
// Price by quantity
$this->price_by_qty = $newpsq;
@ -1564,12 +1587,12 @@ class Product extends CommonObject
}
$sql = "SELECT rowid, ref, ref_ext, label, description, url, note, customcode, fk_country, price, price_ttc,";
$sql.= " price_min, price_min_ttc, price_base_type, cost_price, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, tosell,";
$sql.= " price_min, price_min_ttc, price_base_type, cost_price, tva_tx, recuperableonly as tva_npr, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, tosell,";
$sql.= " tobuy, fk_product_type, duration, seuil_stock_alerte, canvas,";
$sql.= " weight, weight_units, length, length_units, surface, surface_units, volume, volume_units, barcode, fk_barcode_type, finished,";
$sql.= " accountancy_code_buy, accountancy_code_sell, stock, pmp,";
$sql.= " datec, tms, import_key, entity, desiredstock, tobatch, fk_unit";
$sql.= " , fk_price_expression, price_autogen";
$sql.= " datec, tms, import_key, entity, desiredstock, tobatch, fk_unit,";
$sql.= " fk_price_expression, price_autogen";
$sql.= " FROM ".MAIN_DB_PREFIX."product";
if ($id) $sql.= " WHERE rowid = ".$this->db->escape($id);
else
@ -1611,10 +1634,12 @@ class Product extends CommonObject
$this->tva_tx = $obj->tva_tx;
//! French VAT NPR
$this->tva_npr = $obj->tva_npr;
//! Spanish local taxes
//! Local taxes
$this->localtax1_tx = $obj->localtax1_tx;
$this->localtax2_tx = $obj->localtax2_tx;
$this->localtax1_type = $obj->localtax1_type;
$this->localtax2_type = $obj->localtax2_type;
$this->finished = $obj->finished;
$this->duration = $obj->duration;
$this->duration_value = substr($obj->duration,0,dol_strlen($obj->duration)-1);

View File

@ -42,7 +42,9 @@ class Productcustomerprice extends CommonObject
var $price_base_type;
var $tva_tx;
var $recuperableonly;
var $localtax1_type;
var $localtax1_tx;
var $localtax2_type;
var $localtax2_tx;
var $fk_user;
var $lines = array ();
@ -150,7 +152,9 @@ class Productcustomerprice extends CommonObject
$sql .= "price_base_type,";
$sql .= "tva_tx,";
$sql .= "recuperableonly,";
$sql .= "localtax1_type,";
$sql .= "localtax1_tx,";
$sql .= "localtax2_type,";
$sql .= "localtax2_tx,";
$sql .= "fk_user,";
$sql .= "import_key";
@ -168,7 +172,9 @@ class Productcustomerprice extends CommonObject
$sql .= " " . (! isset($this->price_base_type) ? 'NULL' : "'" . $this->db->escape($this->price_base_type) . "'") . ",";
$sql .= " " . (! isset($this->tva_tx) ? 'NULL' : "'" . $this->tva_tx . "'") . ",";
$sql .= " " . (! isset($this->recuperableonly) ? 'NULL' : "'" . $this->recuperableonly . "'") . ",";
$sql .= " " . (empty($this->localtax1_type) ? "'0'" : "'" . $this->localtax1_type . "'") . ",";
$sql .= " " . (! isset($this->localtax1_tx) ? 'NULL' : "'" . $this->localtax1_tx . "'") . ",";
$sql .= " " . (empty($this->localtax2_type) ? "'0'" : "'" . $this->localtax2_type . "'") . ",";
$sql .= " " . (! isset($this->localtax2_tx) ? 'NULL' : "'" . $this->localtax2_tx . "'") . ",";
$sql .= " " . $user->id . ",";
$sql .= " " . (! isset($this->import_key) ? 'NULL' : "'" . $this->db->escape($this->import_key) . "'") . "";
@ -595,6 +601,8 @@ class Productcustomerprice extends CommonObject
$sql .= "recuperableonly,";
$sql .= "localtax1_tx,";
$sql .= "localtax2_tx,";
$sql .= "localtax1_type,";
$sql .= "localtax2_type,";
$sql .= "fk_user,";
$sql .= "import_key";
@ -614,6 +622,8 @@ class Productcustomerprice extends CommonObject
$sql .= " t.recuperableonly,";
$sql .= " t.localtax1_tx,";
$sql .= " t.localtax2_tx,";
$sql .= " t.localtax1_type,";
$sql .= " t.localtax2_type,";
$sql .= " t.fk_user,";
$sql .= " t.import_key";
@ -645,6 +655,8 @@ class Productcustomerprice extends CommonObject
$sql .= " recuperableonly=" . (isset($this->recuperableonly) ? $this->recuperableonly : "null") . ",";
$sql .= " localtax1_tx=" . (isset($this->localtax1_tx) ? $this->localtax1_tx : "null") . ",";
$sql .= " localtax2_tx=" . (isset($this->localtax2_tx) ? $this->localtax2_tx : "null") . ",";
$sql .= " localtax1_type=" . (! empty($this->localtax1_type) ? "'".$this->localtax1_type."'": "'0'") . ",";
$sql .= " localtax2_type=" . (! empty($this->localtax2_type) ? "'".$this->localtax2_type."'": "'0'") . ",";
$sql .= " fk_user=" . $user->id . ",";
$sql .= " import_key=" . (isset($this->import_key) ? "'" . $this->db->escape($this->import_key) . "'" : "null") . "";

View File

@ -94,7 +94,43 @@ if (empty($reshook))
if (($action == 'update_vat') && !$cancel && ($user->rights->produit->creer || $user->rights->service->creer))
{
$object->tva_tx = GETPOST('tva_tx');
$tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5
$tva_tx_txt;
// We must define tva_tx, npr and local taxes
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
// If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
// We look into database using code
$vatratecode=$reg[1];
// Get record from code
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
$sql.= " WHERE t.fk_pays = c.rowid AND c.code = '".$mysoc->country_code."'";
$sql.= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
$npr = $obj->recuperableonly;
$localtax1 = $obj->localtax1;
$localtax2 = $obj->localtax2;
$localtax1_type = $obj->localtax1_type;
$localtax2_type = $obj->localtax2_type;
}
}
$object->tva_tx = $tva_tx;
$object->tva_npr = $npr;
$object->localtax1_tx = $localtax1;
$object->localtax2_tx = $localtax2;
$object->localtax1_type = $localtax1_type;
$object->localtax2_type = $localtax2_type;
$db->begin();
@ -106,7 +142,7 @@ if (empty($reshook))
if ($error)
{
$object->updatePrice($newprice, $newpricebase, $user, $newvat, $newprice_min, $level, $newnpr, $newpsq);
$object->updatePrice($newprice, $newpricebase, $user, $newvat, $newprice_min, $level, $newnpr, $newpsq); // FIXME Bug $newvat and $newnpr not defined
}
if (! $error)
@ -145,31 +181,41 @@ if (empty($reshook))
}
// Multiprices
if (!$error && !empty($conf->global->PRODUIT_MULTIPRICES)) {
if (! $error && ! empty($conf->global->PRODUIT_MULTIPRICES)) {
$newprice = GETPOST('price', 'array');
$newprice_min = GETPOST('price_min', 'array');
$newpricebase = GETPOST('multiprices_base_type', 'array');
$newvattx = GETPOST('tva_tx', 'array');
$newvatnpr = GETPOST('tva_npr', 'array');
$newlocaltax1_tx = GETPOST('localtax1_tx', 'array');
$newlocaltax1_type = GETPOST('localtax1_type', 'array');
$newlocaltax2_tx = GETPOST('localtax2_tx', 'array');
$newlocaltax2_type = GETPOST('localtax2_type', 'array');
//Shall we generate prices using price rules?
$object->price_autogen = GETPOST('usePriceRules') == 'on' ? true : false;
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++) {
for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i ++)
{
if (!isset($newprice[$i])) {
continue;
}
$newnpr = (preg_match('/\*/', $newvattx[$i] ? 1 : 0));
$newvat = str_replace('*', '', $newvattx[$i]);
$tva_tx_txt = $newvattx[$i];
$npr = $newvatnpr[$i];
$localtax1 = $newlocaltax1_tx[$i];
$localtax1_type = $newlocaltax1_type[$i];
$localtax2 = $newlocaltax2_tx[$i];
$localtax2_type = $newlocaltax2_type[$i];
$pricestoupdate[$i] = array(
'price' => $newprice[$i],
'price_min' => $newprice_min[$i],
'price_base_type' => $newpricebase[$i],
'vat_tx' => $newvat,
'npr' => $newnpr
'vat_tx' => $tva_tx,
'npr' => $npr,
'localtaxes_array' => array('0'=>$localtax1_type, '1'=>$localtax1, '2'=>$localtax2_type, '3'=>$localtax2)
);
//If autogeneration is enabled, then we only set the first level
@ -178,12 +224,41 @@ if (empty($reshook))
}
}
} else {
$tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' , '8.5*', '8.5 (8.5NPR) *' for example.
// We must define tva_tx, npr and local taxes
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
// If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
// We look into database using code
$vatratecode=$reg[1];
// Get record from code
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
$sql.= " WHERE t.fk_pays = c.rowid AND c.code = '".$mysoc->country_code."'";
$sql.= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
$npr = $obj->recuperableonly;
$localtax1 = $obj->localtax1;
$localtax2 = $obj->localtax2;
$localtax1_type = $obj->localtax1_type;
$localtax2_type = $obj->localtax2_type;
}
}
$pricestoupdate[0] = array(
'price' => $_POST ["price"],
'price_min' => $_POST ["price_min"],
'price_base_type' => $_POST ["price_base_type"],
'vat_tx' => str_replace('*', '', $_POST ["tva_tx"]),
'npr' => preg_match('/\*/', $_POST ["tva_tx"]) ? 1 : 0
'price' => $_POST["price"],
'price_min' => $_POST["price_min"],
'price_base_type' => $_POST["price_base_type"],
'vat_tx' => $tva_tx,
'npr' => $npr,
'localtaxes_array' => array('0'=>$localtax1_type, '1'=>$localtax1, '2'=>$localtax2_type, '3'=>$localtax2)
);
}
@ -207,7 +282,7 @@ if (empty($reshook))
break;
}
$res = $object->updatePrice($newprice, $val['price_base_type'], $user, $val['vat_tx'], $newprice_min, $key, $val['npr'], $psq);
$res = $object->updatePrice($newprice, $val['price_base_type'], $user, $val['vat_tx'], $newprice_min, $key, $val['npr'], $psq, 0, $val['localtaxes_array']);
if ($res < 0) {
$error ++;
@ -344,9 +419,43 @@ if (empty($reshook))
$prodcustprice->price = price2num(GETPOST("price"), 'MU');
$prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
$prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
$prodcustprice->tva_tx = str_replace('*', '', GETPOST("tva_tx"));
$prodcustprice->recuperableonly = (preg_match('/\*/', GETPOST("tva_tx")) ? 1 : 0);
$tva_tx_txt = GETPOST("tva_tx");
// We must define tva_tx, npr and local taxes
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
// If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
// We look into database using code
$vatratecode=$reg[1];
// Get record from code
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
$sql.= " WHERE t.fk_pays = c.rowid AND c.code = '".$mysoc->country_code."'";
$sql.= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
$npr = $obj->recuperableonly;
$localtax1 = $obj->localtax1;
$localtax2 = $obj->localtax2;
$localtax1_type = $obj->localtax1_type;
$localtax2_type = $obj->localtax2_type;
}
}
$prodcustprice->tva_tx = $tva_tx;
$prodcustprice->recuperableonly = $npr;
$prodcustprice->localtax1_tx = $localtax1;
$prodcustprice->localtax2_tx = $localtax2;
$prodcustprice->localtax1_type = $localtax1_type;
$prodcustprice->localtax2_type = $localtax2_type;
if (! ($prodcustprice->fk_soc > 0))
{
$langs->load("errors");
@ -383,9 +492,9 @@ if (empty($reshook))
$result = $prodcustprice->delete($user);
if ($result < 0) {
setEventMessages($prodcustprice->error, $prodcustprice->errors, 'mesgs');
setEventMessages($prodcustprice->error, $prodcustprice->errors, 'errors');
} else {
setEventMessages($langs->trans('RecordDeleted'), null, 'errors');
setEventMessages($langs->trans('RecordDeleted'), null, 'mesgs');
}
$action = '';
}
@ -402,9 +511,43 @@ if (empty($reshook))
$prodcustprice->price = price2num(GETPOST("price"), 'MU');
$prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
$prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
$prodcustprice->tva_tx = str_replace('*', '', GETPOST("tva_tx"));
$prodcustprice->recuperableonly = (preg_match('/\*/', GETPOST("tva_tx")) ? 1 : 0);
$tva_tx_txt = GETPOST("tva_tx");
// We must define tva_tx, npr and local taxes
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '0';
// If value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
// We look into database using code
$vatratecode=$reg[1];
// Get record from code
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type";
$sql.= " FROM ".MAIN_DB_PREFIX."c_tva as t, ".MAIN_DB_PREFIX."c_country as c";
$sql.= " WHERE t.fk_pays = c.rowid AND c.code = '".$mysoc->country_code."'";
$sql.= " AND t.taux = ".((float) $tva_tx)." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql);
if ($resql)
{
$obj = $db->fetch_object($resql);
$npr = $obj->recuperableonly;
$localtax1 = $obj->localtax1;
$localtax2 = $obj->localtax2;
$localtax1_type = $obj->localtax1_type;
$localtax2_type = $obj->localtax2_type;
}
}
$prodcustprice->tva_tx = $tva_tx;
$prodcustprice->recuperableonly = $npr;
$prodcustprice->localtax1_tx = $localtax1;
$prodcustprice->localtax2_tx = $localtax2;
$prodcustprice->localtax1_type = $localtax1_type;
$prodcustprice->localtax2_type = $localtax2_type;
if ($prodcustprice->price_min<$maxpricesupplier && !empty($conf->global->PRODUCT_MINIMUM_RECOMMENDED_PRICE))
{
setEventMessages($langs->trans("MinimumPriceLimit",price($maxpricesupplier,0,'',1,-1,-1,'auto')), null, 'errors');
@ -792,7 +935,7 @@ if ($action == 'edit_vat' && ($user->rights->produit->creer || $user->rights->se
// VAT
print '<tr><td>' . $langs->trans("VATRate") . '</td><td>';
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr);
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
print '</td></tr>';
print '</table>';
@ -825,7 +968,7 @@ if ($action == 'edit_price' && $object->getRights()->creer)
// VAT
print '<tr><td>' . $langs->trans("VATRate") . '</td><td colspan="2">';
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr);
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
print '</td></tr>';
// Price base
@ -981,11 +1124,16 @@ if ($action == 'edit_price' && $object->getRights()->creer)
// VAT
if (empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) {
print '<input type="hidden" name="tva_tx[' . $i . ']" value="' . $object->tva_tx . '">';
print '<input type="hidden" name="tva_tx[' . $i . ']" value="' . $object->tva_tx . '">';
print '<input type="hidden" name="tva_npr[' . $i . ']" value="' . $object->tva_npr . '">';
print '<input type="hidden" name="localtax1_tx[' . $i . ']" value="' . $object->localtax1_tx . '">';
print '<input type="hidden" name="localtax1_type[' . $i . ']" value="' . $object->localtax1_type . '">';
print '<input type="hidden" name="localtax2_tx[' . $i . ']" value="' . $object->localtax2_tx . '">';
print '<input type="hidden" name="localtax2_tx[' . $i . ']" value="' . $object->localtax2_type . '">';
} else {
// This option is kept for backward compatibility but has no sense
print '<td style="text-align: center">';
print $form->load_tva("tva_tx[" . $i.']', $object->multiprices_tva_tx[$i], $mysoc, '', $object->id);
print $form->load_tva("tva_tx[" . $i.']', $object->multiprices_tva_tx[$i], $mysoc, '', $object->id, false, $object->type, false, 1);
print '</td>';
}
@ -1205,7 +1353,7 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
// VAT
print '<tr><td class="fieldrequired">' . $langs->trans("VATRate") . '</td><td>';
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr);
print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr, $object->type, false, 1);
print '</td></tr>';
// Price base
@ -1289,7 +1437,7 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
// VAT
print '<tr><td>' . $langs->trans("VATRate") . '</td><td colspan="2">';
print $form->load_tva("tva_tx", $prodcustprice->tva_tx, $mysoc, '', $object->id, $prodcustprice->recuperableonly);
print $form->load_tva("tva_tx", $prodcustprice->tva_tx, $mysoc, '', $object->id, $prodcustprice->recuperableonly, $object->type, false, 1);
print '</td></tr>';
// Price base