Skip to content

Commit

Permalink
FIX vat code not saved during product creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
eldy committed Jun 23, 2017
1 parent 4500449 commit 1211e0d
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion htdocs/core/class/html.form.class.php
Expand Up @@ -4332,7 +4332,7 @@ function load_cache_vatrates($country_code)
* Si vendeur et acheteur dans Communauté européenne et acheteur= entreprise alors TVA par défaut=0. Fin de règle.
* Sinon la TVA proposee par defaut=0. Fin de regle.
* @param bool $options_only Return HTML options lines only (for ajax treatment)
* @param int $mode 1=Add code into key in select list
* @param int $mode 0=Use vat rate as key in combo list, 1=Add VAT code after vat rate into key, -1=Use id of vat line as key
* @return string
*/
function load_tva($htmlname='tauxtva', $selectedrate='', $societe_vendeuse='', $societe_acheteuse='', $idprod=0, $info_bits=0, $type='', $options_only=false, $mode=0)
Expand Down
44 changes: 38 additions & 6 deletions htdocs/product/card.php
Expand Up @@ -239,12 +239,43 @@
else
$object->price_min = GETPOST('price_min');

$object->tva_tx = str_replace('*','',GETPOST('tva_tx'));
$object->tva_npr = preg_match('/\*/',GETPOST('tva_tx'))?1:0;
$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
$vatratecode = '';
$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 (we can't use get_localtax() because it depends on buyer that is not known). Same in update price.
$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;
}
}

// local taxes.
$object->localtax1_tx = get_localtax($object->tva_tx,1);
$object->localtax2_tx = get_localtax($object->tva_tx,2);
$object->default_vat_code = $vatratecode;
$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;

$object->type = $type;
$object->status = GETPOST('statut');
Expand Down Expand Up @@ -1109,7 +1140,8 @@

// VAT
print '<tr><td>'.$langs->trans("VATRate").'</td><td>';
print $form->load_tva("tva_tx",-1,$mysoc,'');
$defaultva=get_default_tva($mysoc, $mysoc);
print $form->load_tva("tva_tx", $defaultva, $mysoc, $mysoc, 0, 0, '', false, 1);
print '</td></tr>';

print '</table>';
Expand Down
5 changes: 3 additions & 2 deletions htdocs/product/class/product.class.php
Expand Up @@ -396,7 +396,7 @@ function create($user,$notrigger=0)
$sql = "SELECT count(*) as nb";
$sql.= " FROM ".MAIN_DB_PREFIX."product";
$sql.= " WHERE entity IN (".getEntity('product', 1).")";
$sql.= " AND ref = '" .$this->ref."'";
$sql.= " AND ref = '" .$this->db->escape($this->ref)."'";

$result = $this->db->query($sql);
if ($result)
Expand Down Expand Up @@ -1285,7 +1285,7 @@ function _log_price($user,$level=0)
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, default_vat_code, recuperableonly,";
$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, default_vat_code, 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->default_vat_code?("'".$this->default_vat_code."'"):"null").",".$this->tva_npr.",";
$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');
Expand Down Expand Up @@ -1721,6 +1721,7 @@ function fetch($id='', $ref='', $ref_ext='', $ignore_expression=0)
$this->tva_tx = $obj->tva_tx;
//! French VAT NPR
$this->tva_npr = $obj->tva_npr;
$this->recuperableonly = $obj->tva_npr; // For backward compatibility
//! Local taxes
$this->localtax1_tx = $obj->localtax1_tx;
$this->localtax2_tx = $obj->localtax2_tx;
Expand Down
7 changes: 5 additions & 2 deletions htdocs/product/price.php
Expand Up @@ -113,7 +113,7 @@
// 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
// We look into database using code (we can't use get_localtax() because it depends on buyer that is not known). Same in create product.
$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";
Expand Down Expand Up @@ -1819,7 +1819,10 @@ function on_change() {
print "<td>" . "</td>";

print '<td align="center">' . $langs->trans($object->price_base_type) . "</td>";
print '<td align="right">' . vatrate($object->tva_tx, true, $object->recuperableonly) . "</td>";
print '<td align="right">';
print vatrate($object->tva_tx, true, $object->recuperableonly);
print $object->default_vat_code?' ('.$object->default_vat_code.')':'';
print "</td>";
print '<td align="right">' . price($object->price) . "</td>";
print '<td align="right">' . price($object->price_ttc) . "</td>";
print '<td align="right">' . price($object->price_min) . '</td>';
Expand Down

0 comments on commit 1211e0d

Please sign in to comment.