2
0
forked from Wavyzz/dolibarr

FIX #7473 Mass update of vat rates and other bugs on localtax

This commit is contained in:
Laurent Destailleur
2017-09-25 21:41:53 +02:00
parent c5287e68b1
commit 59571b1ce1
6 changed files with 215 additions and 48 deletions

View File

@@ -3711,7 +3711,7 @@ function vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0)
if (! preg_match('/\//', $rate)) $ret=price($rate,0,'',0,0).($addpercent?'%':''); if (! preg_match('/\//', $rate)) $ret=price($rate,0,'',0,0).($addpercent?'%':'');
else else
{ {
// TODO Split on / and output with a price2num to have clean numbers with ton of 000. // TODO Split on / and output with a price2num to have clean numbers without ton of 000.
$ret=$rate.($addpercent?'%':''); $ret=$rate.($addpercent?'%':'');
} }
if ($info_bits & 1) $ret.=' *'; if ($info_bits & 1) $ret.=' *';

View File

@@ -180,16 +180,18 @@ class ProductFournisseur extends Product
* @param Societe $fourn Supplier * @param Societe $fourn Supplier
* @param int $availability Product availability * @param int $availability Product availability
* @param string $ref_fourn Supplier ref * @param string $ref_fourn Supplier ref
* @param float $tva_tx VAT rate * @param float $tva_tx New VAT Rate (For example 8.5. Should not be a string)
* @param string $charges costs affering to product * @param string $charges costs affering to product
* @param float $remise_percent Discount regarding qty (percent) * @param float $remise_percent Discount regarding qty (percent)
* @param float $remise Discount regarding qty (amount) * @param float $remise Discount regarding qty (amount)
* @param int $newnpr Set NPR or not * @param int $newnpr Set NPR or not
* @param int $delivery_time_days Delay in days for delivery (max). May be '' if not defined. * @param int $delivery_time_days Delay in days for delivery (max). May be '' if not defined.
* @param string $supplier_reputation Reputation with this product to the defined supplier (empty, FAVORITE, DONOTORDER) * @param string $supplier_reputation Reputation with this product to the defined supplier (empty, FAVORITE, DONOTORDER)
* @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
* @param string $newdefaultvatcode Default vat code
* @return int <0 if KO, >=0 if OK * @return int <0 if KO, >=0 if OK
*/ */
function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='') function update_buyprice($qty, $buyprice, $user, $price_base_type, $fourn, $availability, $ref_fourn, $tva_tx, $charges=0, $remise_percent=0, $remise=0, $newnpr=0, $delivery_time_days=0, $supplier_reputation='', $localtaxes_array=array(), $newdefaultvatcode='')
{ {
global $conf, $langs; global $conf, $langs;
//global $mysoc; //global $mysoc;
@@ -217,6 +219,25 @@ class ProductFournisseur extends Product
$now=dol_now(); $now=dol_now();
$newvat = $tva_tx;
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(); $this->db->begin();
if ($this->product_fourn_price_id > 0) if ($this->product_fourn_price_id > 0)
@@ -230,9 +251,15 @@ class ProductFournisseur extends Product
$sql.= " remise = ".$remise.","; $sql.= " remise = ".$remise.",";
$sql.= " unitprice = ".$unitBuyPrice.","; $sql.= " unitprice = ".$unitBuyPrice.",";
$sql.= " unitcharges = ".$unitCharges.","; // deprecated $sql.= " unitcharges = ".$unitCharges.","; // deprecated
$sql.= " tva_tx = ".$tva_tx.",";
$sql.= " fk_availability = ".$availability.","; $sql.= " fk_availability = ".$availability.",";
$sql.= " entity = ".$conf->entity.","; $sql.= " entity = ".$conf->entity.",";
$sql.= " tva_tx = ".price2num($tva_tx).",";
// TODO Add localtax1 and localtax2
//$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.= " default_vat_code=".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
$sql.= " info_bits = ".$newnpr.","; $sql.= " info_bits = ".$newnpr.",";
$sql.= " charges = ".$charges.","; // deprecated $sql.= " charges = ".$charges.","; // deprecated
$sql.= " delivery_time_days = ".($delivery_time_days != '' ? $delivery_time_days : 'null').","; $sql.= " delivery_time_days = ".($delivery_time_days != '' ? $delivery_time_days : 'null').",";
@@ -279,7 +306,7 @@ class ProductFournisseur extends Product
if ($resql) { if ($resql) {
// Add price for this quantity to supplier // Add price for this quantity to supplier
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price("; $sql = "INSERT INTO " . MAIN_DB_PREFIX . "product_fournisseur_price(";
$sql .= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, info_bits, entity, delivery_time_days,supplier_reputation)"; $sql .= "datec, fk_product, fk_soc, ref_fourn, fk_user, price, quantity, remise_percent, remise, unitprice, tva_tx, charges, unitcharges, fk_availability, default_vat_code, info_bits, entity, delivery_time_days, supplier_reputation)";
$sql .= " values('" . $this->db->idate($now) . "',"; $sql .= " values('" . $this->db->idate($now) . "',";
$sql .= " " . $this->id . ","; $sql .= " " . $this->id . ",";
$sql .= " " . $fourn->id . ","; $sql .= " " . $fourn->id . ",";
@@ -294,6 +321,7 @@ class ProductFournisseur extends Product
$sql .= " " . $charges . ","; $sql .= " " . $charges . ",";
$sql .= " " . $unitCharges . ","; $sql .= " " . $unitCharges . ",";
$sql .= " " . $availability . ","; $sql .= " " . $availability . ",";
$sql .= " ".($newdefaultvatcode?"'".$this->db->escape($newdefaultvatcode)."'":"null").",";
$sql .= " " . $newnpr . ","; $sql .= " " . $newnpr . ",";
$sql .= $conf->entity . ","; $sql .= $conf->entity . ",";
$sql .= $delivery_time_days . ","; $sql .= $delivery_time_days . ",";
@@ -364,7 +392,7 @@ class ProductFournisseur extends Product
function fetch_product_fournisseur_price($rowid, $ignore_expression = 0) function fetch_product_fournisseur_price($rowid, $ignore_expression = 0)
{ {
global $conf; global $conf;
$sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.fk_availability,"; $sql = "SELECT pfp.rowid, pfp.price, pfp.quantity, pfp.unitprice, pfp.remise_percent, pfp.remise, pfp.tva_tx, pfp.default_vat_code, pfp.fk_availability,";
$sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,"; // , pfp.recuperableonly as fourn_tva_npr"; FIXME this field not exist in llx_product_fournisseur_price $sql.= " pfp.fk_soc, pfp.ref_fourn, pfp.fk_product, pfp.charges, pfp.unitcharges, pfp.fk_supplier_price_expression, pfp.delivery_time_days,"; // , pfp.recuperableonly as fourn_tva_npr"; FIXME this field not exist in llx_product_fournisseur_price
$sql.= " pfp.supplier_reputation"; $sql.= " pfp.supplier_reputation";
$sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp"; $sql.= " FROM ".MAIN_DB_PREFIX."product_fournisseur_price as pfp";
@@ -392,11 +420,14 @@ class ProductFournisseur extends Product
$this->fourn_unitprice = $obj->unitprice; $this->fourn_unitprice = $obj->unitprice;
$this->fourn_unitcharges = $obj->unitcharges; // deprecated $this->fourn_unitcharges = $obj->unitcharges; // deprecated
$this->fourn_tva_tx = $obj->tva_tx; $this->fourn_tva_tx = $obj->tva_tx;
// TODO
// $this->fourn_tva_npr = $obj->fourn_tva_npr; // TODO this field not exist in llx_product_fournisseur_price. We should add it ? // $this->fourn_tva_npr = $obj->fourn_tva_npr; // TODO this field not exist in llx_product_fournisseur_price. We should add it ?
// Add also localtaxes
$this->fk_availability = $obj->fk_availability; $this->fk_availability = $obj->fk_availability;
$this->delivery_time_days = $obj->delivery_time_days; $this->delivery_time_days = $obj->delivery_time_days;
$this->fk_supplier_price_expression = $obj->fk_supplier_price_expression; $this->fk_supplier_price_expression = $obj->fk_supplier_price_expression;
$this->supplier_reputation = $obj->supplier_reputation; $this->supplier_reputation = $obj->supplier_reputation;
$this->default_vat_code = $obj->default_vat_code;
if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression)) if (empty($ignore_expression) && !empty($this->fk_supplier_price_expression))
{ {

View File

@@ -38,8 +38,8 @@ $langs->load("products");
if (! $user->admin) accessforbidden(); if (! $user->admin) accessforbidden();
$action = GETPOST('action','alpha'); $action = GETPOST('action','alpha');
$oldvatrate=GETPOST('oldvatrate'); $oldvatrate=GETPOST('oldvatrate', 'alpha');
$newvatrate=GETPOST('newvatrate'); $newvatrate=GETPOST('newvatrate', 'alpha');
//$price_base_type=GETPOST('price_base_type'); //$price_base_type=GETPOST('price_base_type');
@@ -67,13 +67,31 @@ if ($action == 'convert')
$db->begin(); $db->begin();
// Clean vat code old
$vat_src_code_old='';
if (preg_match('/\((.*)\)/', $oldvatrate, $reg))
{
$vat_src_code_old = $reg[1];
$oldvatrateclean = preg_replace('/\s*\(.*\)/', '', $oldvatrate); // Remove code into vatrate.
}
// Clean vat code new
$vat_src_code_new='';
if (preg_match('/\((.*)\)/', $newvatrate, $reg))
{
$vat_src_code_new = $reg[1];
$newvatrateclean = preg_replace('/\s*\(.*\)/', '', $newvatrate); // Remove code into vatrate.
}
// If country to edit is my country, so we change customer prices // If country to edit is my country, so we change customer prices
if ($country_id == $mysoc->country_id) if ($country_id == $mysoc->country_id)
{ {
$sql = 'SELECT rowid'; $sql = 'SELECT rowid';
$sql.= ' FROM '.MAIN_DB_PREFIX.'product'; $sql.= ' FROM '.MAIN_DB_PREFIX.'product';
$sql.= ' WHERE entity IN ('.getEntity('product').')'; $sql.= ' WHERE entity IN ('.getEntity('product').')';
$sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'"; $sql.= " AND tva_tx = '".$db->escape($oldvatrateclean)."'";
if ($vat_src_code_old) $sql.= " AND default_vat_code = '".$vat_src_code_old."'";
else " AND default_vat_code = IS NULL";
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
@@ -109,12 +127,15 @@ if ($action == 'convert')
$newminprice=$objectstatic->multiprices_min[$level]; $newminprice=$objectstatic->multiprices_min[$level];
} }
if ($newminprice > $newprice) $newminprice=$newprice; if ($newminprice > $newprice) $newminprice=$newprice;
$newvat=str_replace('*','',$newvatrate); $newvat=str_replace('*','',$newvatrate);
$localtaxes_type=getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
$newnpr=$objectstatic->multiprices_recuperableonly[$level]; $newnpr=$objectstatic->multiprices_recuperableonly[$level];
$newdefaultvatcode=$vat_src_code_new;
$newlevel=$level; $newlevel=$level;
//print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n"; //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
$retm=$objectstatic->updatePrice($newprice, $price_base_type, $user, $newvat, $newminprice, $newlevel, $newnpr); $retm=$objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatratclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
if ($retm < 0) if ($retm < 0)
{ {
$error++; $error++;
@@ -138,12 +159,14 @@ if ($action == 'convert')
} }
if ($newminprice > $newprice) $newminprice=$newprice; if ($newminprice > $newprice) $newminprice=$newprice;
$newvat=str_replace('*','',$newvatrate); $newvat=str_replace('*','',$newvatrate);
$localtaxes_type=getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
$newnpr=$objectstatic->recuperableonly; $newnpr=$objectstatic->recuperableonly;
$newdefaultvatcode=$vat_src_code_new;
$newlevel=0; $newlevel=0;
if (! empty($price_base_type) && ! $updatelevel1) if (! empty($price_base_type) && ! $updatelevel1)
{ {
//print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n"; //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
$ret=$objectstatic->updatePrice($newprice, $price_base_type, $user, $newvat, $newminprice, $newlevel, $newnpr); $ret=$objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
} }
if ($ret < 0 || $retm < 0) $error++; if ($ret < 0 || $retm < 0) $error++;
@@ -164,6 +187,8 @@ if ($action == 'convert')
$sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'societe as s'; $sql.= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'societe as s';
$sql.= ' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity('product').')'; $sql.= ' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity('product').')';
$sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'"; $sql.= " AND tva_tx = '".$db->escape($oldvatrate)."'";
if ($vat_src_code_old) $sql.= " AND default_vat_code = '".$vat_src_code_old."'";
else " AND default_vat_code = IS NULL";
$sql.= " AND s.fk_pays = '".$country_id."'"; $sql.= " AND s.fk_pays = '".$country_id."'";
//print $sql; //print $sql;
$resql=$db->query($sql); $resql=$db->query($sql);
@@ -196,13 +221,21 @@ if ($action == 'convert')
//} //}
//if ($newminprice > $newprice) $newminprice=$newprice; //if ($newminprice > $newprice) $newminprice=$newprice;
$newvat=str_replace('*','',$newvatrate); $newvat=str_replace('*','',$newvatrate);
$localtaxes_type=getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
//$newnpr=$objectstatic2->recuperableonly; //$newnpr=$objectstatic2->recuperableonly;
$newnpr=0;
$newdefaultvatcode=$vat_src_code_new;
$newpercent = $objectstatic2->fourn_remise_percent;
$newdeliverydelay = $objectstatic2->delivery_time_days;
$newsupplierreputation = $objectstatic2->supplier_reputation;
$newlevel=0; $newlevel=0;
if (! empty($price_base_type) && ! $updatelevel1) if (! empty($price_base_type) && ! $updatelevel1)
{ {
//print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n"; //print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
$fourn->id=$obj->fk_soc; $fourn->id=$obj->fk_soc;
$ret=$objectstatic2->update_buyprice($obj->qty, $newprice, $user, $price_base_type, $fourn, $obj->fk_availability, $obj->ref_fourn, $newvat); $ret=$objectstatic2->update_buyprice($obj->qty, $newprice, $user, $price_base_type, $fourn, $obj->fk_availability, $obj->ref_fourn, $newvat, '', $newpercent, 0, $newnpr, $newdeliverydelay, $newsupplierreputation, $localtaxes_type, $newdefaultvatcode);
} }
if ($ret < 0 || $retm < 0) $error++; if ($ret < 0 || $retm < 0) $error++;
@@ -215,7 +248,6 @@ if ($action == 'convert')
} }
else dol_print_error($db); else dol_print_error($db);
if (! $error) if (! $error)
{ {
$db->commit(); $db->commit();
@@ -278,7 +310,7 @@ else
print '<tr class="oddeven">'."\n"; print '<tr class="oddeven">'."\n";
print '<td>'.$langs->trans("OldVATRates").'</td>'."\n"; print '<td>'.$langs->trans("OldVATRates").'</td>'."\n";
print '<td width="60" align="right">'."\n"; print '<td width="60" align="right">'."\n";
print $form->load_tva('oldvatrate', $oldvatrate, $mysoc); print $form->load_tva('oldvatrate', $oldvatrate, $mysoc, null, 0, 0, '', false, 1);
print '</td>'."\n"; print '</td>'."\n";
print '</tr>'."\n"; print '</tr>'."\n";
@@ -286,7 +318,7 @@ else
print '<tr class="oddeven">'."\n"; print '<tr class="oddeven">'."\n";
print '<td>'.$langs->trans("NewVATRates").'</td>'."\n"; print '<td>'.$langs->trans("NewVATRates").'</td>'."\n";
print '<td width="60" align="right">'."\n"; print '<td width="60" align="right">'."\n";
print $form->load_tva('newvatrate', $newvatrate, $mysoc); print $form->load_tva('newvatrate', $newvatrate, $mysoc, null, 0, 0, '', false, 1);
print '</td>'."\n"; print '</td>'."\n";
print '</tr>'."\n"; print '</tr>'."\n";

View File

@@ -329,6 +329,8 @@ class Productcustomerprice extends CommonObject
$sql .= " t.recuperableonly,"; $sql .= " t.recuperableonly,";
$sql .= " t.localtax1_tx,"; $sql .= " t.localtax1_tx,";
$sql .= " t.localtax2_tx,"; $sql .= " t.localtax2_tx,";
$sql .= " t.localtax1_type,";
$sql .= " t.localtax2_type,";
$sql .= " t.fk_user,"; $sql .= " t.fk_user,";
$sql .= " t.import_key,"; $sql .= " t.import_key,";
$sql .= " soc.nom as socname,"; $sql .= " soc.nom as socname,";
@@ -386,6 +388,8 @@ class Productcustomerprice extends CommonObject
$line->recuperableonly = $obj->recuperableonly; $line->recuperableonly = $obj->recuperableonly;
$line->localtax1_tx = $obj->localtax1_tx; $line->localtax1_tx = $obj->localtax1_tx;
$line->localtax2_tx = $obj->localtax2_tx; $line->localtax2_tx = $obj->localtax2_tx;
$line->localtax1_type = $obj->localtax1_type;
$line->localtax2_type = $obj->localtax2_type;
$line->fk_user = $obj->fk_user; $line->fk_user = $obj->fk_user;
$line->import_key = $obj->import_key; $line->import_key = $obj->import_key;
$line->socname = $obj->socname; $line->socname = $obj->socname;

View File

@@ -107,8 +107,15 @@ if (empty($reshook))
$tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)' $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
// We must define tva_tx, npr and local taxes // We must define tva_tx, npr and local taxes
$tva_tx = $tva_tx_txt;
$vatratecode = ''; $vatratecode = '';
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
$vat_src_code = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
}
$tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '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 value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
@@ -217,8 +224,15 @@ if (empty($reshook))
$tva_tx_txt = $newvattx[$i]; $tva_tx_txt = $newvattx[$i];
$tva_tx = $tva_tx_txt;
$vatratecode = ''; $vatratecode = '';
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
{
$vat_src_code = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
}
$tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = $newlocaltax1_tx[$i]; $localtax1 = $newlocaltax1_tx[$i];
$localtax1_type = $newlocaltax1_type[$i]; $localtax1_type = $newlocaltax1_type[$i];
@@ -266,9 +280,15 @@ if (empty($reshook))
{ {
$tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)' $tva_tx_txt = GETPOST('tva_tx', 'alpha'); // tva_tx can be '8.5' or '8.5*' or '8.5 (XXX)' or '8.5* (XXX)'
$tva_tx = $tva_tx_txt;
$vatratecode = ''; $vatratecode = '';
// We must define tva_tx, npr and local taxes if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot {
$vat_src_code = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
}
$tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '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 value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
@@ -280,7 +300,7 @@ if (empty($reshook))
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type"; $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.= " 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.= " 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.taux = ".$tva_tx." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'"; $sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
@@ -469,11 +489,17 @@ if (empty($reshook))
$prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU'); $prodcustprice->price_min = price2num(GETPOST("price_min"), 'MU');
$prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha'); $prodcustprice->price_base_type = GETPOST("price_base_type", 'alpha');
$tva_tx_txt = GETPOST("tva_tx"); $tva_tx_txt = GETPOST("tva_tx",'alpha');
$tva_tx = $tva_tx_txt;
$vatratecode = ''; $vatratecode = '';
// We must define tva_tx, npr and local taxes if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot {
$vat_src_code = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
}
$tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '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 value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
@@ -485,7 +511,7 @@ if (empty($reshook))
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type"; $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.= " 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.= " 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.taux = ".$tva_tx." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'"; $sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
@@ -565,9 +591,15 @@ if (empty($reshook))
$tva_tx_txt = GETPOST("tva_tx"); $tva_tx_txt = GETPOST("tva_tx");
$tva_tx = $tva_tx_txt;
$vatratecode = ''; $vatratecode = '';
// We must define tva_tx, npr and local taxes if (preg_match('/\((.*)\)/', $tva_tx_txt, $reg))
$tva_tx = preg_replace('/[^0-9\.].*$/', '', $tva_tx_txt); // keep remove all after the numbers and dot {
$vat_src_code = $reg[1];
$tva_tx = preg_replace('/\s*\(.*\)/', '', $tva_tx_txt); // Remove code into vatrate.
}
$tva_tx = price2num(preg_replace('/\*/', '', $tva_tx)); // keep remove all after the numbers and dot
$npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0; $npr = preg_match('/\*/', $tva_tx_txt) ? 1 : 0;
$localtax1 = 0; $localtax2 = 0; $localtax1_type = '0'; $localtax2_type = '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 value contains the unique code of vat line (new recommanded method), we use it to find npr and local taxes
@@ -579,7 +611,7 @@ if (empty($reshook))
$sql = "SELECT t.rowid, t.code, t.recuperableonly, t.localtax1, t.localtax2, t.localtax1_type, t.localtax2_type"; $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.= " 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.= " 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.taux = ".$tva_tx." AND t.active = 1";
$sql.= " AND t.code ='".$vatratecode."'"; $sql.= " AND t.code ='".$vatratecode."'";
$resql=$db->query($sql); $resql=$db->query($sql);
if ($resql) if ($resql)
@@ -712,17 +744,34 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES))
if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility if (! empty($conf->global->PRODUIT_MULTIPRICES_USE_VAT_PER_LEVEL)) // using this option is a bug. kept for backward compatibility
{ {
// TVA // TVA
print '<tr><td>' . $langs->trans("VATRate") . '</td><td colspan="2">' . vatrate($object->multiprices_tva_tx[$soc->price_level], true) . '</td></tr>'; print '<tr><td>' . $langs->trans("VATRate") . '</td><td colspan="2">';
$positiverates='';
if (price2num($object->multiprices_tva_tx[$soc->price_level])) $positiverates.=($positiverates?'/':'').price2num($object->multiprices_tva_tx[$soc->price_level]);
if (price2num($object->multiprices_localtax1_type[$soc->price_level])) $positiverates.=($positiverates?'/':'').price2num($object->multiprices_localtax1_tx[$soc->price_level]);
if (price2num($object->multiprices_localtax2_type[$soc->price_level])) $positiverates.=($positiverates?'/':'').price2num($object->multiprices_localtax2_tx[$soc->price_level]);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($object->default_vat_code?' ('.$object->default_vat_code.')':''), '%', $object->tva_npr);
//print vatrate($object->multiprices_tva_tx[$soc->price_level], true);
print '</td></tr>';
} }
else else
{ {
// TVA // TVA
print '<tr><td>' . $langs->trans("VATRate") . '</td><td>'; print '<tr><td>' . $langs->trans("VATRate") . '</td><td>';
$positiverates='';
if (price2num($object->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($object->tva_tx);
if (price2num($object->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax1_tx);
if (price2num($object->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($object->default_vat_code?' ('.$object->default_vat_code.')':''), '%', $object->tva_npr);
/*
if ($object->default_vat_code) if ($object->default_vat_code)
{ {
print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')'; print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
} }
else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true); else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true);*/
print '</td></tr>'; print '</td></tr>';
} }
@@ -740,11 +789,19 @@ if (! empty($conf->global->PRODUIT_MULTIPRICES))
{ {
// TVA // TVA
print '<tr><td class="titlefield">' . $langs->trans("VATRate") . '</td><td>'; print '<tr><td class="titlefield">' . $langs->trans("VATRate") . '</td><td>';
$positiverates='';
if (price2num($object->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($object->tva_tx);
if (price2num($object->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax1_tx);
if (price2num($object->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($object->default_vat_code?' ('.$object->default_vat_code.')':''), '%', $object->tva_npr);
/*
if ($object->default_vat_code) if ($object->default_vat_code)
{ {
print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')'; print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
} }
else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true); else print vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true);*/
print '</td></tr>'; print '</td></tr>';
} }
print '</table>'; print '</table>';
@@ -889,11 +946,19 @@ else
{ {
// TVA // TVA
print '<tr><td class="titlefield">' . $langs->trans("VATRate") . '</td><td>'; print '<tr><td class="titlefield">' . $langs->trans("VATRate") . '</td><td>';
$positiverates='';
if (price2num($object->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($object->tva_tx);
if (price2num($object->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax1_tx);
if (price2num($object->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($object->default_vat_code?' ('.$object->default_vat_code.')':''), '%', $object->tva_npr);
/*
if ($object->default_vat_code) if ($object->default_vat_code)
{ {
print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')'; print vatrate($object->tva_tx, true) . ' ('.$object->default_vat_code.')';
} }
else print vatrate($object->tva_tx, true, $object->tva_npr, true); else print vatrate($object->tva_tx, true, $object->tva_npr, true);*/
print '</td></tr>'; print '</td></tr>';
// Price // Price
@@ -1314,7 +1379,7 @@ if ($action == 'edit_price' && $object->getRights()->creer)
if ((empty($conf->global->PRODUIT_CUSTOMER_PRICES) || $action=='showlog_default_price') && ! in_array($action, array('edit_price','edit_vat'))) if ((empty($conf->global->PRODUIT_CUSTOMER_PRICES) || $action=='showlog_default_price') && ! in_array($action, array('edit_price','edit_vat')))
{ {
$sql = "SELECT p.rowid, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.default_vat_code, p.recuperableonly,"; $sql = "SELECT p.rowid, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.default_vat_code, p.recuperableonly, p.localtax1_tx, p.localtax1_type, p.localtax2_tx, p.localtax2_type,";
$sql .= " p.price_level, p.price_min, p.price_min_ttc,p.price_by_qty,"; $sql .= " p.price_level, p.price_min, p.price_min_ttc,p.price_by_qty,";
$sql .= " p.date_price as dp, p.fk_price_expression, u.rowid as user_id, u.login"; $sql .= " p.date_price as dp, p.fk_price_expression, u.rowid as user_id, u.login";
$sql .= " FROM " . MAIN_DB_PREFIX . "product_price as p,"; $sql .= " FROM " . MAIN_DB_PREFIX . "product_price as p,";
@@ -1410,11 +1475,19 @@ if ((empty($conf->global->PRODUIT_CUSTOMER_PRICES) || $action=='showlog_default_
if (empty($conf->global->PRODUIT_MULTIPRICES)) if (empty($conf->global->PRODUIT_MULTIPRICES))
{ {
print '<td align="right">'; print '<td align="right">';
$positiverates='';
if (price2num($objp->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($objp->tva_tx);
if (price2num($objp->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($objp->localtax1_tx);
if (price2num($objp->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($objp->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($objp->default_vat_code?' ('.$objp->default_vat_code.')':''), '%', $objp->tva_npr);
/*
if ($objp->default_vat_code) if ($objp->default_vat_code)
{ {
print vatrate($objp->tva_tx, true) . ' ('.$objp->default_vat_code.')'; print vatrate($objp->tva_tx, true) . ' ('.$objp->default_vat_code.')';
} }
else print vatrate($objp->tva_tx, true, $objp->recuperableonly); else print vatrate($objp->tva_tx, true, $objp->recuperableonly);*/
print "</td>"; print "</td>";
} }
@@ -1769,7 +1842,17 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
print "<td>" . $staticsoc->getNomUrl(1) . "</td>"; print "<td>" . $staticsoc->getNomUrl(1) . "</td>";
print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>"; print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>";
print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>"; print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>";
print '<td align="right">' . vatrate($tva_tx, true, $line->recuperableonly) . "</td>"; print '<td align="right">';
var_dump($prodcustprice);exit;
$positiverates='';
if (price2num($objp->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($objp->tva_tx);
if (price2num($objp->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($objp->localtax1_tx);
if (price2num($objp->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($objp->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($objp->default_vat_code?' ('.$objp->default_vat_code.')':''), '%', $objp->tva_npr);
//. vatrate($tva_tx, true, $line->recuperableonly) .
print "</td>";
print '<td align="right">' . price($line->price) . "</td>"; print '<td align="right">' . price($line->price) . "</td>";
print '<td align="right">' . price($line->price_ttc) . "</td>"; print '<td align="right">' . price($line->price_ttc) . "</td>";
@@ -1873,8 +1956,16 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
print '<td align="center">' . $langs->trans($object->price_base_type) . "</td>"; print '<td align="center">' . $langs->trans($object->price_base_type) . "</td>";
print '<td align="right">'; print '<td align="right">';
print vatrate($object->tva_tx, true, $object->recuperableonly);
print $object->default_vat_code?' ('.$object->default_vat_code.')':''; $positiverates='';
if (price2num($object->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($object->tva_tx);
if (price2num($object->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax1_tx);
if (price2num($object->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($object->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($object->default_vat_code?' ('.$object->default_vat_code.')':''), '%', $object->tva_npr);
//print vatrate($object->tva_tx, true, $object->tva_npr);
//print $object->default_vat_code?' ('.$object->default_vat_code.')':'';
print "</td>"; print "</td>";
print '<td align="right">' . price($object->price) . "</td>"; print '<td align="right">' . price($object->price) . "</td>";
@@ -1939,7 +2030,16 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES))
print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>"; print "<td>" . dol_print_date($line->datec, "dayhour") . "</td>";
print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>"; print '<td align="center">' . $langs->trans($line->price_base_type) . "</td>";
print '<td align="right">' . vatrate($tva_tx, true, $line->recuperableonly) . "</td>"; print '<td align="right">';
$positiverates='';
if (price2num($line->tva_tx)) $positiverates.=($positiverates?'/':'').price2num($line->tva_tx);
if (price2num($line->localtax1_type)) $positiverates.=($positiverates?'/':'').price2num($line->localtax1_tx);
if (price2num($line->localtax2_type)) $positiverates.=($positiverates?'/':'').price2num($line->localtax2_tx);
if (empty($positiverates)) $positiverates='0';
echo vatrate($positiverates.($line->default_vat_code?' ('.$line->default_vat_code.')':''), '%', $line->tva_npr);
print "</td>";
print '<td align="right">' . price($line->price) . "</td>"; print '<td align="right">' . price($line->price) . "</td>";
print '<td align="right">' . price($line->price_ttc) . "</td>"; print '<td align="right">' . price($line->price_ttc) . "</td>";