product_tools.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /**
  19. * \file htdocs/product/admin/product_tools.php
  20. * \ingroup product
  21. * \brief Setup page of product module
  22. */
  23. // TODO We must add a confirmation on button because this will make a mass change
  24. // FIXME Should also change table product_price for price levels
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array('admin', 'products'));
  33. // Security check
  34. if (!$user->admin) {
  35. accessforbidden();
  36. }
  37. $action = GETPOST('action', 'aZ09');
  38. $oldvatrate = GETPOST('oldvatrate', 'alpha');
  39. $newvatrate = GETPOST('newvatrate', 'alpha');
  40. //$price_base_type=GETPOST('price_base_type');
  41. /*
  42. * Actions
  43. */
  44. if ($action == 'convert') {
  45. $error = 0;
  46. if ($oldvatrate == $newvatrate) {
  47. $langs->load("errors");
  48. setEventMessages($langs->trans("ErrorNewValueCantMatchOldValue"), null, 'errors');
  49. $error++;
  50. }
  51. if (!$error) {
  52. $country_id = $mysoc->country_id; // TODO Allow to choose country into form
  53. $nbrecordsmodified = 0;
  54. $db->begin();
  55. // Clean vat code old
  56. $vat_src_code_old = '';
  57. if (preg_match('/\((.*)\)/', $oldvatrate, $reg)) {
  58. $vat_src_code_old = $reg[1];
  59. $oldvatrateclean = preg_replace('/\s*\(.*\)/', '', $oldvatrate); // Remove code into vatrate.
  60. } else {
  61. $oldvatrateclean = $oldvatrate;
  62. }
  63. // Clean vat code new
  64. $vat_src_code_new = '';
  65. if (preg_match('/\((.*)\)/', $newvatrate, $reg)) {
  66. $vat_src_code_new = $reg[1];
  67. $newvatrateclean = preg_replace('/\s*\(.*\)/', '', $newvatrate); // Remove code into vatrate.
  68. } else {
  69. $newvatrateclean = $newvatrate;
  70. }
  71. // If country to edit is my country, so we change customer prices
  72. if ($country_id == $mysoc->country_id) {
  73. $sql = 'SELECT rowid';
  74. $sql .= ' FROM '.MAIN_DB_PREFIX.'product';
  75. $sql .= ' WHERE entity IN ('.getEntity('product').')';
  76. $sql .= " AND tva_tx = '".$db->escape($oldvatrateclean)."'";
  77. if ($vat_src_code_old) {
  78. $sql .= " AND default_vat_code = '".$db->escape($vat_src_code_old)."'";
  79. } else {
  80. " AND default_vat_code = IS NULL";
  81. }
  82. $resql = $db->query($sql);
  83. if ($resql) {
  84. $num = $db->num_rows($resql);
  85. $i = 0;
  86. while ($i < $num) {
  87. $obj = $db->fetch_object($resql);
  88. $objectstatic = new Product($db); // Object init must be into loop to avoid to get value of previous step
  89. $ret = $objectstatic->fetch($obj->rowid);
  90. if ($ret > 0) {
  91. $ret = 0; $retm = 0; $updatelevel1 = false;
  92. // Update multiprice
  93. $listofmulti = array_reverse($objectstatic->multiprices, true); // To finish with level 1
  94. foreach ($listofmulti as $level => $multiprices) {
  95. $price_base_type = $objectstatic->multiprices_base_type[$level]; // Get price_base_type of product/service to keep the same for update
  96. if (empty($price_base_type)) {
  97. continue; // Discard not defined price levels
  98. }
  99. if ($price_base_type == 'TTC') {
  100. $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)
  101. $newminprice = $objectstatic->multiprices_min_ttc[$level];
  102. } else {
  103. $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)
  104. $newminprice = $objectstatic->multiprices_min[$level];
  105. }
  106. if ($newminprice > $newprice) {
  107. $newminprice = $newprice;
  108. }
  109. $newvat = str_replace('*', '', $newvatrate);
  110. $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
  111. $newnpr = $objectstatic->multiprices_recuperableonly[$level];
  112. $newdefaultvatcode = $vat_src_code_new;
  113. $newlevel = $level;
  114. //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
  115. $retm = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
  116. if ($retm < 0) {
  117. $error++;
  118. break;
  119. }
  120. if ($newlevel == 1) {
  121. $updatelevel1 = true;
  122. }
  123. }
  124. // Update single price
  125. $price_base_type = $objectstatic->price_base_type; // Get price_base_type of product/service to keep the same for update
  126. if ($price_base_type == 'TTC') {
  127. $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)
  128. $newminprice = $objectstatic->price_min_ttc;
  129. } else {
  130. $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)
  131. $newminprice = $objectstatic->price_min;
  132. }
  133. if ($newminprice > $newprice) {
  134. $newminprice = $newprice;
  135. }
  136. $newvat = str_replace('*', '', $newvatrate);
  137. $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
  138. $newnpr = $objectstatic->recuperableonly;
  139. $newdefaultvatcode = $vat_src_code_new;
  140. $newlevel = 0;
  141. if (!empty($price_base_type) && !$updatelevel1) {
  142. //print "$objectstatic->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
  143. $ret = $objectstatic->updatePrice($newprice, $price_base_type, $user, $newvatrateclean, $newminprice, $newlevel, $newnpr, 0, 0, $localtaxes_type, $newdefaultvatcode);
  144. }
  145. if ($ret < 0 || $retm < 0) {
  146. $error++;
  147. } else {
  148. $nbrecordsmodified++;
  149. }
  150. }
  151. unset($objectstatic);
  152. $i++;
  153. }
  154. } else {
  155. dol_print_error($db);
  156. }
  157. }
  158. $fourn = new Fournisseur($db);
  159. // Change supplier prices
  160. $sql = 'SELECT pfp.rowid, pfp.fk_soc, pfp.price as price, pfp.quantity as qty, pfp.fk_availability, pfp.ref_fourn';
  161. $sql .= ' FROM '.MAIN_DB_PREFIX.'product_fournisseur_price as pfp, '.MAIN_DB_PREFIX.'societe as s';
  162. $sql .= ' WHERE pfp.fk_soc = s.rowid AND pfp.entity IN ('.getEntity('product').')';
  163. $sql .= " AND tva_tx = '".$db->escape($oldvatrate)."'";
  164. if ($vat_src_code_old) {
  165. $sql .= " AND default_vat_code = '".$db->escape($vat_src_code_old)."'";
  166. } else {
  167. " AND default_vat_code = IS NULL";
  168. }
  169. $sql .= " AND s.fk_pays = ".((int) $country_id);
  170. //print $sql;
  171. $resql = $db->query($sql);
  172. if ($resql) {
  173. $num = $db->num_rows($resql);
  174. $i = 0;
  175. while ($i < $num) {
  176. $obj = $db->fetch_object($resql);
  177. $objectstatic2 = new ProductFournisseur($db); // Object init must be into loop to avoid to get value of previous step
  178. $ret = $objectstatic2->fetch_product_fournisseur_price($obj->rowid);
  179. if ($ret > 0) {
  180. $ret = 0; $retm = 0; $updatelevel1 = false;
  181. $price_base_type = 'HT';
  182. //$price_base_type = $objectstatic2->price_base_type; // Get price_base_type of product/service to keep the same for update
  183. //if ($price_base_type == 'TTC')
  184. //{
  185. // $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)
  186. // $newminprice=$objectstatic2->price_min_ttc;
  187. //}
  188. //else
  189. //{
  190. $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)
  191. //$newminprice=$objectstatic2->fourn_price_min;
  192. //}
  193. //if ($newminprice > $newprice) $newminprice=$newprice;
  194. $newvat = str_replace('*', '', $newvatrate);
  195. $localtaxes_type = getLocalTaxesFromRate($newvat, 0, $mysoc, $mysoc);
  196. //$newnpr=$objectstatic2->recuperableonly;
  197. $newnpr = 0;
  198. $newdefaultvatcode = $vat_src_code_new;
  199. $newpercent = $objectstatic2->fourn_remise_percent;
  200. $newdeliverydelay = $objectstatic2->delivery_time_days;
  201. $newsupplierreputation = $objectstatic2->supplier_reputation;
  202. $newlevel = 0;
  203. if (!empty($price_base_type) && !$updatelevel1) {
  204. //print "$objectstatic2->id $newprice, $price_base_type, $newvat, $newminprice, $newlevel, $newnpr<br>\n";
  205. $fourn->id = $obj->fk_soc;
  206. $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);
  207. }
  208. if ($ret < 0 || $retm < 0) {
  209. $error++;
  210. } else {
  211. $nbrecordsmodified++;
  212. }
  213. }
  214. unset($objectstatic2);
  215. $i++;
  216. }
  217. } else {
  218. dol_print_error($db);
  219. }
  220. if (!$error) {
  221. $db->commit();
  222. } else {
  223. $db->rollback();
  224. }
  225. // Output result
  226. if (!$error) {
  227. if ($nbrecordsmodified > 0) {
  228. setEventMessages($langs->trans("RecordsModified", $nbrecordsmodified), null, 'mesgs');
  229. } else {
  230. setEventMessages($langs->trans("NoRecordFound"), null, 'warnings');
  231. }
  232. } else {
  233. setEventMessages($langs->trans("Error"), null, 'errors');
  234. }
  235. }
  236. }
  237. /*
  238. * View
  239. */
  240. $form = new Form($db);
  241. $title = $langs->trans('ProductVatMassChange');
  242. llxHeader('', $title);
  243. print load_fiche_titre($title, '', 'title_setup');
  244. print $langs->trans("ProductVatMassChangeDesc").'<br><br>';
  245. if (empty($mysoc->country_code)) {
  246. $langs->load("errors");
  247. $warnpicto = img_error($langs->trans("WarningMandatorySetupNotComplete"));
  248. print '<br><a href="'.DOL_URL_ROOT.'/admin/company.php?mainmenu=home">'.$warnpicto.' '.$langs->trans("WarningMandatorySetupNotComplete").'</a>';
  249. } else {
  250. print '<form method="POST" action="'.$_SERVER['PHP_SELF'].'">';
  251. print '<input type="hidden" name="token" value="'.newToken().'" />';
  252. print '<input type="hidden" name="action" value="convert" />';
  253. print '<table class="noborder centpercent">';
  254. print '<tr class="liste_titre">';
  255. print '<td>'.$langs->trans("Parameters").'</td>'."\n";
  256. print '<td class="right" width="60">'.$langs->trans("Value").'</td>'."\n";
  257. print '</tr>'."\n";
  258. print '<tr class="oddeven">'."\n";
  259. print '<td>'.$langs->trans("OldVATRates").'</td>'."\n";
  260. print '<td width="60" class="right">'."\n";
  261. print $form->load_tva('oldvatrate', $oldvatrate, $mysoc, null, 0, 0, '', false, 1);
  262. print '</td>'."\n";
  263. print '</tr>'."\n";
  264. print '<tr class="oddeven">'."\n";
  265. print '<td>'.$langs->trans("NewVATRates").'</td>'."\n";
  266. print '<td width="60" class="right">'."\n";
  267. print $form->load_tva('newvatrate', $newvatrate, $mysoc, null, 0, 0, '', false, 1);
  268. print '</td>'."\n";
  269. print '</tr>'."\n";
  270. /*
  271. print '<tr class="oddeven">'."\n";
  272. print '<td>'.$langs->trans("PriceBaseTypeToChange").'</td>'."\n";
  273. print '<td width="60" class="right">'."\n";
  274. print $form->selectPriceBaseType($price_base_type);
  275. print '</td>'."\n";
  276. print '</tr>'."\n";
  277. */
  278. print '</table>';
  279. print '<br>';
  280. // Buttons for actions
  281. print '<div class="center">';
  282. print '<input type="submit" id="convert_vatrate" name="convert_vatrate" value="'.$langs->trans("MassConvert").'" class="button" />';
  283. print '</div>';
  284. print '</form>';
  285. }
  286. // End of page
  287. llxFooter();
  288. $db->close();