From 1211e0d7abcc165cbd50992f8da9f7bc006adca5 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur <eldy@destailleur.fr> Date: Fri, 23 Jun 2017 12:09:21 +0200 Subject: [PATCH] FIX vat code not saved during product creation. --- htdocs/core/class/html.form.class.php | 2 +- htdocs/product/card.php | 44 ++++++++++++++++++++++---- htdocs/product/class/product.class.php | 5 +-- htdocs/product/price.php | 7 ++-- 4 files changed, 47 insertions(+), 11 deletions(-) diff --git a/htdocs/core/class/html.form.class.php b/htdocs/core/class/html.form.class.php index b440c012f04..321a4752006 100644 --- a/htdocs/core/class/html.form.class.php +++ b/htdocs/core/class/html.form.class.php @@ -4332,7 +4332,7 @@ class Form * 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) diff --git a/htdocs/product/card.php b/htdocs/product/card.php index 31a865132c8..833330126bb 100644 --- a/htdocs/product/card.php +++ b/htdocs/product/card.php @@ -239,12 +239,43 @@ if (empty($reshook)) 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'); @@ -1109,7 +1140,8 @@ else // 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>'; diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 0f28ffdea30..2e2f3007540 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -396,7 +396,7 @@ class Product extends CommonObject $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) @@ -1285,7 +1285,7 @@ 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, 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'); @@ -1721,6 +1721,7 @@ class Product extends CommonObject $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; diff --git a/htdocs/product/price.php b/htdocs/product/price.php index cbfe90a5a52..2b5617c601d 100644 --- a/htdocs/product/price.php +++ b/htdocs/product/price.php @@ -113,7 +113,7 @@ if (empty($reshook)) // 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"; @@ -1819,7 +1819,10 @@ if (! empty($conf->global->PRODUIT_CUSTOMER_PRICES)) 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>'; -- GitLab