28 require
'../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/admin.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/product.lib.php';
31 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
35 $langs->loadLangs(array(
'admin',
'products'));
42 $action =
GETPOST(
'action',
'aZ09');
43 $oldvatrate =
GETPOST(
'oldvatrate',
'alpha');
44 $newvatrate =
GETPOST(
'newvatrate',
'alpha');
53 if ($action ==
'convert') {
56 if ($oldvatrate == $newvatrate) {
57 $langs->load(
"errors");
58 setEventMessages($langs->trans(
"ErrorNewValueCantMatchOldValue"),
null,
'errors');
63 $country_id = $mysoc->country_id;
65 $nbrecordsmodified = 0;
70 $vat_src_code_old =
'';
71 if (preg_match(
'/\((.*)\)/', $oldvatrate, $reg)) {
72 $vat_src_code_old = $reg[1];
73 $oldvatrateclean = preg_replace(
'/\s*\(.*\)/',
'', $oldvatrate);
75 $oldvatrateclean = $oldvatrate;
79 $vat_src_code_new =
'';
80 if (preg_match(
'/\((.*)\)/', $newvatrate, $reg)) {
81 $vat_src_code_new = $reg[1];
82 $newvatrateclean = preg_replace(
'/\s*\(.*\)/',
'', $newvatrate);
84 $newvatrateclean = $newvatrate;
88 if ($country_id == $mysoc->country_id) {
89 $sql =
'SELECT rowid';
90 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product';
91 $sql .=
' WHERE entity IN ('.getEntity(
'product').
')';
92 $sql .=
" AND tva_tx = '".$db->escape($oldvatrateclean).
"'";
93 if ($vat_src_code_old) {
94 $sql .=
" AND default_vat_code = '".$db->escape($vat_src_code_old).
"'";
96 " AND default_vat_code = IS NULL";
101 $num = $db->num_rows(
$resql);
105 $obj = $db->fetch_object(
$resql);
107 $objectstatic =
new Product($db);
108 $ret = $objectstatic->fetch($obj->rowid);
110 $ret = 0; $retm = 0; $updatelevel1 =
false;
113 $listofmulti = array_reverse($objectstatic->multiprices,
true);
114 foreach ($listofmulti as $level => $multiprices) {
115 $price_base_type = $objectstatic->multiprices_base_type[$level];
116 if (empty($price_base_type)) {
120 if ($price_base_type ==
'TTC') {
121 $newprice =
price2num($objectstatic->multiprices_ttc[$level],
'MU');
122 $newminprice = $objectstatic->multiprices_min_ttc[$level];
124 $newprice =
price2num($objectstatic->multiprices[$level],
'MU');
125 $newminprice = $objectstatic->multiprices_min[$level];
127 if ($newminprice > $newprice) {
128 $newminprice = $newprice;
131 $newvat = str_replace(
'*',
'', $newvatrate);
133 $newnpr = $objectstatic->multiprices_recuperableonly[$level];
134 $newdefaultvatcode = $vat_src_code_new;
138 $retm = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
144 if ($newlevel == 1) {
145 $updatelevel1 =
true;
150 $price_base_type = $objectstatic->price_base_type;
151 if ($price_base_type ==
'TTC') {
152 $newprice =
price2num($objectstatic->price_ttc,
'MU');
153 $newminprice = $objectstatic->price_min_ttc;
155 $newprice =
price2num($objectstatic->price,
'MU');
156 $newminprice = $objectstatic->price_min;
158 if ($newminprice > $newprice) {
159 $newminprice = $newprice;
161 $newvat = str_replace(
'*',
'', $newvatrate);
163 $newnpr = $objectstatic->recuperableonly;
164 $newdefaultvatcode = $vat_src_code_new;
166 if (!empty($price_base_type) && !$updatelevel1) {
168 $ret = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
171 if ($ret < 0 || $retm < 0) {
174 $nbrecordsmodified++;
177 unset($objectstatic);
189 $sql =
'SELECT pfp.rowid, pfp.fk_soc, pfp.price as price, pfp.quantity as qty, pfp.fk_availability, pfp.ref_fourn';
190 $sql .=
' FROM '.MAIN_DB_PREFIX.
'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.
'societe as s';
191 $sql .=
' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity(
'product').
')';
192 $sql .=
" AND tva_tx = '".$db->escape($oldvatrate).
"'";
193 if ($vat_src_code_old) {
194 $sql .=
" AND default_vat_code = '".$db->escape($vat_src_code_old).
"'";
196 " AND default_vat_code = IS NULL";
198 $sql .=
" AND s.fk_pays = ".((int) $country_id);
200 $resql = $db->query($sql);
202 $num = $db->num_rows(
$resql);
206 $obj = $db->fetch_object(
$resql);
209 $ret = $objectstatic2->fetch_product_fournisseur_price($obj->rowid);
211 $ret = 0; $retm = 0; $updatelevel1 =
false;
213 $price_base_type =
'HT';
222 $newprice =
price2num($obj->price,
'MU');
226 $newvat = str_replace(
'*',
'', $newvatrate);
230 $newdefaultvatcode = $vat_src_code_new;
232 $newpercent = $objectstatic2->fourn_remise_percent;
233 $newdeliverydelay = $objectstatic2->delivery_time_days;
234 $newsupplierreputation = $objectstatic2->supplier_reputation;
237 if (!empty($price_base_type) && !$updatelevel1) {
239 $fourn->id = $obj->fk_soc;
240 $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);
243 if ($ret < 0 || $retm < 0) {
246 $nbrecordsmodified++;
249 unset($objectstatic2);
265 if ($nbrecordsmodified > 0) {
266 setEventMessages($langs->trans(
"RecordsModified", $nbrecordsmodified),
null,
'mesgs');
282 $title = $langs->trans(
'ProductVatMassChange');
288 print $langs->trans(
"ProductVatMassChangeDesc").
'<br><br>';
290 if (empty($mysoc->country_code)) {
291 $langs->load(
"errors");
292 $warnpicto =
img_error($langs->trans(
"WarningMandatorySetupNotComplete"));
293 print
'<br><a href="'.DOL_URL_ROOT.
'/admin/company.php?mainmenu=home">'.$warnpicto.
' '.$langs->trans(
"WarningMandatorySetupNotComplete").
'</a>';
295 print
'<form method="POST" action="'.$_SERVER[
'PHP_SELF'].
'">';
296 print
'<input type="hidden" name="token" value="'.newToken().
'" />';
297 print
'<input type="hidden" name="action" value="convert" />';
299 print
'<table class="noborder centpercent">';
300 print
'<tr class="liste_titre">';
301 print
'<td>'.$langs->trans(
"Parameters").
'</td>'.
"\n";
302 print
'<td class="right" width="60">'.$langs->trans(
"Value").
'</td>'.
"\n";
306 print
'<tr class="oddeven">'.
"\n";
307 print
'<td>'.$langs->trans(
"OldVATRates").
'</td>'.
"\n";
308 print
'<td width="60" class="right">'.
"\n";
309 print
$form->load_tva(
'oldvatrate', $oldvatrate, $mysoc,
null, 0, 0,
'',
false, 1);
314 print
'<tr class="oddeven">'.
"\n";
315 print
'<td>'.$langs->trans(
"NewVATRates").
'</td>'.
"\n";
316 print
'<td width="60" class="right">'.
"\n";
317 print
$form->load_tva(
'newvatrate', $newvatrate, $mysoc,
null, 0, 0,
'',
false, 1);
337 print
'<div class="center">';
338 print
'<input type="submit" id="convert_vatrate" name="convert_vatrate" value="'.$langs->trans(
"MassConvert").
'" class="button" />';