dolibarr 19.0.3
product_tools.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
3 * Copyright (C) 2013-2015 Laurent Destailleur <eldy@users.sourceforge.net>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25// TODO We must add a confirmation on button because this will make a mass change
26// FIXME Should also change table product_price for price levels
27
28// Load Dolibarr environment
29require '../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
34
35// Load translation files required by the page
36$langs->loadLangs(array('admin', 'products'));
37
38// Security check
39if (!$user->admin) {
41}
42
43$action = GETPOST('action', 'aZ09');
44$oldvatrate = GETPOST('oldvatrate', 'alpha');
45$newvatrate = GETPOST('newvatrate', 'alpha');
46//$price_base_type=GETPOST('price_base_type');
47
48
49
50/*
51 * Actions
52 */
53
54if ($action == 'convert') {
55 $error = 0;
56
57 if ($oldvatrate == $newvatrate) {
58 $langs->load("errors");
59 setEventMessages($langs->trans("ErrorNewValueCantMatchOldValue"), null, 'errors');
60 $error++;
61 }
62
63 if (!$error) {
64 $country_id = $mysoc->country_id; // TODO Allow to choose country into form
65
66 $nbrecordsmodified = 0;
67
68 $db->begin();
69
70 // Clean vat code old
71 $vat_src_code_old = '';
72 if (preg_match('/\‍((.*)\‍)/', $oldvatrate, $reg)) {
73 $vat_src_code_old = $reg[1];
74 $oldvatrateclean = preg_replace('/\s*\‍(.*\‍)/', '', $oldvatrate); // Remove code into vatrate.
75 } else {
76 $oldvatrateclean = $oldvatrate;
77 }
78
79 // Clean vat code new
80 $vat_src_code_new = '';
81 if (preg_match('/\‍((.*)\‍)/', $newvatrate, $reg)) {
82 $vat_src_code_new = $reg[1];
83 $newvatrateclean = preg_replace('/\s*\‍(.*\‍)/', '', $newvatrate); // Remove code into vatrate.
84 } else {
85 $newvatrateclean = $newvatrate;
86 }
87
88 // If country to edit is my country, so we change customer prices
89 if ($country_id == $mysoc->country_id) {
90 $sql = 'SELECT rowid';
91 $sql .= ' FROM '.MAIN_DB_PREFIX.'product';
92 $sql .= ' WHERE entity IN ('.getEntity('product').')';
93 $sql .= " AND tva_tx = '".$db->escape($oldvatrateclean)."'";
94 if ($vat_src_code_old) {
95 $sql .= " AND default_vat_code = '".$db->escape($vat_src_code_old)."'";
96 } else {
97 " AND default_vat_code = IS NULL";
98 }
99
100 $resql = $db->query($sql);
101 if ($resql) {
102 $num = $db->num_rows($resql);
103
104 $i = 0;
105 while ($i < $num) {
106 $obj = $db->fetch_object($resql);
107
108 $objectstatic = new Product($db); // Object init must be into loop to avoid to get value of previous step
109 $ret = $objectstatic->fetch($obj->rowid);
110 if ($ret > 0) {
111 $ret = 0;
112 $retm = 0;
113 $updatelevel1 = false;
114
115 // Update multiprice
116 $listofmulti = array_reverse($objectstatic->multiprices, true); // To finish with level 1
117 foreach ($listofmulti as $level => $multiprices) {
118 $price_base_type = $objectstatic->multiprices_base_type[$level]; // Get price_base_type of product/service to keep the same for update
119 if (empty($price_base_type)) {
120 continue; // Discard not defined price levels
121 }
122
123 if ($price_base_type == 'TTC') {
124 $newprice = price2num($objectstatic->multiprices_ttc[$level], 'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
125 $newminprice = $objectstatic->multiprices_min_ttc[$level];
126 } else {
127 $newprice = price2num($objectstatic->multiprices[$level], 'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
128 $newminprice = $objectstatic->multiprices_min[$level];
129 }
130 if ($newminprice > $newprice) {
131 $newminprice = $newprice;
132 }
133
134 $newvat = str_replace('*', '', $newvatrate);
135 $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
136 $newnpr = $objectstatic->multiprices_recuperableonly[$level];
137 $newdefaultvatcode = $vat_src_code_new;
138 $newlevel = $level;
139
140 //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
141 $retm = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
142 if ($retm < 0) {
143 $error++;
144 break;
145 }
146
147 if ($newlevel == 1) {
148 $updatelevel1 = true;
149 }
150 }
151
152 // Update single price
153 $price_base_type = $objectstatic->price_base_type; // Get price_base_type of product/service to keep the same for update
154 if ($price_base_type == 'TTC') {
155 $newprice = price2num($objectstatic->price_ttc, 'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
156 $newminprice = $objectstatic->price_min_ttc;
157 } else {
158 $newprice = price2num($objectstatic->price, 'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
159 $newminprice = $objectstatic->price_min;
160 }
161 if ($newminprice > $newprice) {
162 $newminprice = $newprice;
163 }
164 $newvat = str_replace('*', '', $newvatrate);
165 $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
166 $newnpr = $objectstatic->tva_npr;
167 $newdefaultvatcode = $vat_src_code_new;
168 $newlevel = 0;
169 if (!empty($price_base_type) && !$updatelevel1) {
170 //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
171 $ret = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
172 }
173
174 if ($ret < 0 || $retm < 0) {
175 $error++;
176 } else {
177 $nbrecordsmodified++;
178 }
179 }
180 unset($objectstatic);
181
182 $i++;
183 }
184 } else {
185 dol_print_error($db);
186 }
187 }
188
189 $fourn = new Fournisseur($db);
190
191 // Change supplier prices
192 $sql = 'SELECT pfp.rowid, pfp.fk_soc, pfp.price as price, pfp.quantity as qty, pfp.fk_availability, pfp.ref_fourn';
193 $sql .= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'societe as s';
194 $sql .= ' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity('product').')';
195 $sql .= " AND tva_tx = '".$db->escape($oldvatrate)."'";
196 if ($vat_src_code_old) {
197 $sql .= " AND default_vat_code = '".$db->escape($vat_src_code_old)."'";
198 } else {
199 " AND default_vat_code = IS NULL";
200 }
201 $sql .= " AND s.fk_pays = ".((int) $country_id);
202 //print $sql;
203 $resql = $db->query($sql);
204 if ($resql) {
205 $num = $db->num_rows($resql);
206
207 $i = 0;
208 while ($i < $num) {
209 $obj = $db->fetch_object($resql);
210
211 $objectstatic2 = new ProductFournisseur($db); // Object init must be into loop to avoid to get value of previous step
212 $ret = $objectstatic2->fetch_product_fournisseur_price($obj->rowid);
213 if ($ret > 0) {
214 $ret = 0;
215 $retm = 0;
216 $updatelevel1 = false;
217
218 $price_base_type = 'HT';
219 //$price_base_type = $objectstatic2->price_base_type; // Get price_base_type of product/service to keep the same for update
220 //if ($price_base_type == 'TTC')
221 //{
222 // $newprice=price2num($objectstatic2->price_ttc,'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
223 // $newminprice=$objectstatic2->price_min_ttc;
224 //}
225 //else
226 //{
227 $newprice = price2num($obj->price, 'MU'); // Second param must be MU (we want a unit price so 'MU'. If unit price was on 4 decimal, we must keep 4 decimals)
228 //$newminprice=$objectstatic2->fourn_price_min;
229 //}
230 //if ($newminprice > $newprice) $newminprice=$newprice;
231 $newvat = str_replace('*', '', $newvatrate);
232 $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
233 //$newnpr=$objectstatic2->tva_npr;
234 $newnpr = 0;
235 $newdefaultvatcode = $vat_src_code_new;
236
237 $newpercent = $objectstatic2->fourn_remise_percent;
238 $newdeliverydelay = $objectstatic2->delivery_time_days;
239 $newsupplierreputation = $objectstatic2->supplier_reputation;
240
241 $newlevel = 0;
242 if (!empty($price_base_type) && !$updatelevel1) {
243 //print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
244 $fourn->id = $obj->fk_soc;
245 $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);
246 }
247
248 if ($ret < 0 || $retm < 0) {
249 $error++;
250 } else {
251 $nbrecordsmodified++;
252 }
253 }
254 unset($objectstatic2);
255
256 $i++;
257 }
258 } else {
259 dol_print_error($db);
260 }
261
262 if (!$error) {
263 $db->commit();
264 } else {
265 $db->rollback();
266 }
267
268 // Output result
269 if (!$error) {
270 if ($nbrecordsmodified > 0) {
271 setEventMessages($langs->trans("RecordsModified", $nbrecordsmodified), null, 'mesgs');
272 } else {
273 setEventMessages($langs->trans("NoRecordFound"), null, 'warnings');
274 }
275 } else {
276 setEventMessages($langs->trans("Error"), null, 'errors');
277 }
278 }
279}
280
281/*
282 * View
283 */
284
285$form = new Form($db);
286
287$title = $langs->trans('ProductVatMassChange');
288
289llxHeader('', $title);
290
291print load_fiche_titre($title, '', 'title_setup');
292
293print $langs->trans("ProductVatMassChangeDesc").'<br><br>';
294
295if (empty($mysoc->country_code)) {
296 $langs->load("errors");
297 $warnpicto = img_error($langs->trans("WarningMandatorySetupNotComplete"));
298 print '<br><a href="'.DOL_URL_ROOT.'/admin/company.php?mainmenu=home">'.$warnpicto.' '.$langs->trans("WarningMandatorySetupNotComplete").'</a>';
299} else {
300 print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
301 print '<input type="hidden" name="token" value="'.newToken().'" />';
302 print '<input type="hidden" name="action" value="convert" />';
303
304 print '<table class="noborder centpercent">';
305 print '<tr class="liste_titre">';
306 print '<td>'.$langs->trans("Parameters").'</td>'."\n";
307 print '<td class="right" width="60">'.$langs->trans("Value").'</td>'."\n";
308 print '</tr>'."\n";
309
310
311 print '<tr class="oddeven">'."\n";
312 print '<td>'.$langs->trans("OldVATRates").'</td>'."\n";
313 print '<td width="60" class="right">'."\n";
314 print $form->load_tva('oldvatrate', $oldvatrate, $mysoc, null, 0, 0, '', false, 1);
315 print '</td>'."\n";
316 print '</tr>'."\n";
317
318
319 print '<tr class="oddeven">'."\n";
320 print '<td>'.$langs->trans("NewVATRates").'</td>'."\n";
321 print '<td width="60" class="right">'."\n";
322 print $form->load_tva('newvatrate', $newvatrate, $mysoc, null, 0, 0, '', false, 1);
323 print '</td>'."\n";
324 print '</tr>'."\n";
325
326 /*
327
328 print '<tr class="oddeven">'."\n";
329 print '<td>'.$langs->trans("PriceBaseTypeToChange").'</td>'."\n";
330 print '<td width="60" class="right">'."\n";
331 print $form->selectPriceBaseType($price_base_type);
332 print '</td>'."\n";
333 print '</tr>'."\n";
334 */
335
336 print '</table>';
337
338 print '<br>';
339
340 // Buttons for actions
341
342 print '<div class="center">';
343 print '<input type="submit" id="convert_vatrate" name="convert_vatrate" value="'.$langs->trans("MassConvert").'" class="button" />';
344 print '</div>';
345
346 print '</form>';
347}
348
349// End of page
350llxFooter();
351$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
Class to manage suppliers.
Class to manage predefined suppliers products.
Class to manage products or services.
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)
Return value of a param into GET or POST supervariable.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
img_error($titlealt='default')
Show error logo.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.