32require
'../../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
34require_once DOL_DOCUMENT_ROOT.
'/core/lib/ajax.lib.php';
35require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
36require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
37require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
49$langs->loadLangs(array(
'admin',
'products',
'stocks'));
56$action =
GETPOST(
'action',
'aZ09');
57$oldvatrate =
GETPOST(
'oldvatrate',
'alpha');
58$newvatrate =
GETPOST(
'newvatrate',
'alpha');
61$hookmanager->initHooks(array(
'productvatadmin'));
67if ($action ==
'convert') {
70 if ($oldvatrate == $newvatrate) {
71 $langs->load(
"errors");
72 setEventMessages($langs->trans(
"ErrorNewValueCantMatchOldValue"),
null,
'errors');
77 $country_id =
$mysoc->country_id;
79 $nbrecordsmodified = 0;
86 $vat_src_code_old =
'';
87 if (preg_match(
'/\((.*)\)/', $oldvatrate, $reg)) {
88 $vat_src_code_old = $reg[1];
89 $oldvatrateclean = preg_replace(
'/\s*\(.*\)/',
'', $oldvatrate);
91 $oldvatrateclean = $oldvatrate;
95 $vat_src_code_new =
'';
96 if (preg_match(
'/\((.*)\)/', $newvatrate, $reg)) {
97 $vat_src_code_new = $reg[1];
98 $newvatrateclean = preg_replace(
'/\s*\(.*\)/',
'', $newvatrate);
100 $newvatrateclean = $newvatrate;
104 if ($country_id ==
$mysoc->country_id) {
105 $sql =
'SELECT rowid';
106 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product';
107 $sql .=
' WHERE entity IN ('.getEntity(
'product').
')';
108 $sql .=
" AND tva_tx = '".$db->escape($oldvatrateclean).
"'";
109 if ($vat_src_code_old) {
110 $sql .=
" AND default_vat_code = '".$db->escape($vat_src_code_old).
"'";
112 $sql .=
" AND (default_vat_code IS NULL OR default_vat_code = '')";
115 $resql = $db->query($sql);
117 $num = $db->num_rows($resql);
121 $obj = $db->fetch_object($resql);
123 $objectstatic =
new Product($db);
124 $ret = $objectstatic->fetch($obj->rowid);
128 $updatelevel1 =
false;
131 $listofmulti = array_reverse($objectstatic->multiprices,
true);
132 foreach ($listofmulti as $level => $multiprices) {
133 $price_base_type = $objectstatic->multiprices_base_type[$level];
134 if (empty($price_base_type)) {
138 if ($price_base_type ==
'TTC') {
139 $newprice =
price2num($objectstatic->multiprices_ttc[$level],
'MU');
140 $newminprice = $objectstatic->multiprices_min_ttc[$level];
142 $newprice =
price2num($objectstatic->multiprices[$level],
'MU');
143 $newminprice = $objectstatic->multiprices_min[$level];
145 if ($newminprice > $newprice) {
146 $newminprice = $newprice;
149 $newvat = str_replace(
'*',
'', $newvatrate);
151 $newnpr = $objectstatic->multiprices_recuperableonly[$level];
152 $newdefaultvatcode = $vat_src_code_new;
156 $retm = $objectstatic->updatePrice((
float) $newprice, $price_base_type, $user, (
float) $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
162 if ($newlevel == 1) {
163 $updatelevel1 =
true;
168 $price_base_type = $objectstatic->price_base_type;
169 if ($price_base_type ==
'TTC') {
170 $newprice =
price2num($objectstatic->price_ttc,
'MU');
171 $newminprice = $objectstatic->price_min_ttc;
173 $newprice =
price2num($objectstatic->price,
'MU');
174 $newminprice = $objectstatic->price_min;
176 if ($newminprice > $newprice) {
177 $newminprice = $newprice;
179 $newvat = str_replace(
'*',
'', $newvatrate);
181 $newnpr = $objectstatic->tva_npr;
182 $newdefaultvatcode = $vat_src_code_new;
184 if (!empty($price_base_type) && !$updatelevel1) {
186 $ret = $objectstatic->updatePrice((
float) $newprice, $price_base_type, $user, (
float) $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
189 if ($ret < 0 || $retm < 0) {
192 $nbrecordsmodified++;
195 unset($objectstatic);
200 dol_print_error($db);
206 $sweepwhere =
" AND tva_tx = '".$db->escape($oldvatrateclean).
"'";
207 if ($vat_src_code_old) {
208 $sweepwhere .=
" AND default_vat_code = '".$db->escape($vat_src_code_old).
"'";
210 $sweepwhere .=
" AND (default_vat_code IS NULL OR default_vat_code = '')";
212 $db->query(
"UPDATE ".MAIN_DB_PREFIX.
"product SET tva_tx = '".$db->escape($newvatrateclean).
"', default_vat_code = '".$db->escape($vat_src_code_new).
"' WHERE entity IN (".
getEntity(
'product').
")".$sweepwhere);
213 $db->query(
"UPDATE ".MAIN_DB_PREFIX.
"product_price SET tva_tx = '".$db->escape($newvatrateclean).
"', default_vat_code = '".$db->escape($vat_src_code_new).
"' WHERE 1 = 1".$sweepwhere);
219 $sql =
'SELECT pfp.rowid, pfp.fk_soc, pfp.price as price, pfp.quantity as qty, pfp.fk_availability, pfp.ref_fourn';
220 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.
'societe as s';
221 $sql .=
' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity(
'product').
')';
222 $sql .=
" AND tva_tx = '".$db->escape($oldvatrate).
"'";
223 if ($vat_src_code_old) {
224 $sql .=
" AND default_vat_code = '".$db->escape($vat_src_code_old).
"'";
226 $sql .=
" AND (default_vat_code IS NULL OR default_vat_code = '')";
228 $sql .=
" AND s.fk_pays = ".((int) $country_id);
230 $resql = $db->query($sql);
232 $num = $db->num_rows($resql);
236 $obj = $db->fetch_object($resql);
239 $ret = $objectstatic2->fetch_product_fournisseur_price($obj->rowid);
243 $updatelevel1 =
false;
245 $price_base_type =
'HT';
254 $newprice =
price2num($obj->price,
'MU');
258 $newvat = str_replace(
'*',
'', $newvatrate);
262 $newdefaultvatcode = $vat_src_code_new;
264 $newpercent = $objectstatic2->fourn_remise_percent;
265 $newdeliverydelay = $objectstatic2->delivery_time_days;
266 $newsupplierreputation = $objectstatic2->supplier_reputation;
269 if (!empty($price_base_type) && !$updatelevel1) {
271 $fourn->id = $obj->fk_soc;
272 $ret = $objectstatic2->update_buyprice($obj->qty, (
float) $newprice, $user, $price_base_type, $fourn, $obj->fk_availability, $obj->ref_fourn, (
float) $newvat,
'', $newpercent, 0, $newnpr, $newdeliverydelay, $newsupplierreputation, $localtaxes_type, $newdefaultvatcode);
275 if ($ret < 0 || $retm < 0) {
278 $nbrecordsmodified++;
281 unset($objectstatic2);
286 dol_print_error($db);
291 $parameters = array(
'oldvatrate' => $oldvatrate,
'newvatrate' => $newvatrate);
292 $reshook = $hookmanager->executeHooks(
'hookAfterVatUpdate', $parameters);
294 setEventMessages($hookmanager->error, $hookmanager->errors,
'errors');
308 if ($nbrecordsmodified > 0) {
309 setEventMessages($langs->trans(
"RecordsModified", $nbrecordsmodified),
null,
'mesgs');
311 setEventMessages($langs->trans(
"NoRecordFound"),
null,
'warnings');
314 setEventMessages($langs->trans(
"Error"),
null,
'errors');
323$form =
new Form($db);
325$title = $langs->trans(
'ProductVatMassChange');
327llxHeader(
'', $title,
'',
'', 0, 0,
'',
'',
'',
'mod-product page-admin_product_tools');
331print
'<div class="info">'.$langs->trans(
"ProductVatMassChangeDesc").
'</div>';
334if (empty(
$mysoc->country_code)) {
335 $langs->load(
"errors");
336 $warnpicto = img_error($langs->trans(
"WarningMandatorySetupNotComplete"));
337 print
'<br><a href="'.DOL_URL_ROOT.
'/admin/company.php?mainmenu=home">'.$warnpicto.
' '.$langs->trans(
"WarningMandatorySetupNotComplete").
'</a>';
339 print
'<form method="POST" action="'.$_SERVER[
'PHP_SELF'].
'">';
340 print
'<input type="hidden" name="token" value="'.newToken().
'" />';
341 print
'<input type="hidden" name="action" value="convert" />';
343 print
'<table class="noborder centpercent">';
344 print
'<tr class="liste_titre">';
345 print
'<td>'.$langs->trans(
"From").
'/'.$langs->trans(
"To").
'</td>'.
"\n";
346 print
'<td class="right" width="60"></td>'.
"\n";
350 print
'<tr class="oddeven">'.
"\n";
351 print
'<td>'.$langs->trans(
"OldVATRates").
'</td>'.
"\n";
352 print
'<td width="60" class="right">'.
"\n";
355 print
'<select class="flat" name="oldvatrate" id="oldvatrate">';
356 $sqloldvat =
"SELECT tva_tx, default_vat_code, COUNT(*) as nb FROM ".MAIN_DB_PREFIX.
"product";
357 $sqloldvat .=
" WHERE entity IN (".getEntity(
'product').
") AND tva_tx IS NOT NULL";
358 $sqloldvat .=
" GROUP BY tva_tx, default_vat_code ORDER BY tva_tx DESC, default_vat_code";
359 $resqloldvat = $db->query($sqloldvat);
360 while ($resqloldvat && $objoldvat = $db->fetch_object($resqloldvat)) {
361 $rateclean =
price2num($objoldvat->tva_tx);
362 $hascode = !empty($objoldvat->default_vat_code);
363 $optval = $rateclean.($hascode ?
' ('.$objoldvat->default_vat_code.
')' :
'');
364 $optlbl =
vatrate($rateclean,
true).($hascode ?
' ('.$objoldvat->default_vat_code.
')' :
' ('.$langs->trans(
"WithoutVATCode").
')').
' ['.$objoldvat->nb.
' '.$langs->trans(
"Products").
']';
365 print
'<option value="'.dol_escape_htmltag($optval).
'"'.((string) $oldvatrate === (
string) $optval ?
' selected' :
'').
'>'.$optlbl.
'</option>';
373 print
'<tr class="oddeven">'.
"\n";
374 print
'<td>'.$langs->trans(
"NewVATRates").
'</td>'.
"\n";
375 print
'<td width="60" class="right">'.
"\n";
376 print $form->load_tva(
'newvatrate', $newvatrate,
$mysoc,
null, 0, 0,
'',
false, 1);
397 print
'<div class="center">';
398 print
'<input type="submit" id="convert_vatrate" name="convert_vatrate" value="'.$langs->trans(
"MassConvert").
'" class="button" />';
408 jQuery("#convert_vatrate").closest("form").on("submit", function() {
409 dolBlockUI(\''.dol_escape_js($langs->transnoentities(
"MassConvertInProgress")).
'\');
ajax_combobox($htmlname, $events=array(), $minLengthToAutocomplete=0, $forcefocus=0, $widthTypeOfAutocomplete='resolve', $idforemptyvalue='-1', $morecss='', $placeholder='')
Convert a html select field into an ajax combobox.
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Class to manage suppliers.
Class to manage predefined suppliers products.
Class to manage products or services.
vatrate($rate, $addpercent=false, $info_bits=0, $usestarfornpr=0, $html=0)
Return a string with VAT rate label formatted for view output Used into pdf and HTML pages.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.