PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/product/price.php

https://bitbucket.org/speedealing/speedealing
PHP | 506 lines | 359 code | 81 blank | 66 comment | 113 complexity | ec4a23b32804c1004b4ca2c8e3369708 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2001-2007 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005 Eric Seigne <eric.seigne@ryxeo.com>
  5. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  6. * Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
  7. * Copyright (C) 2011-2012 Herve Prot <herve.prot@symeos.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. require '../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT . '/product/lib/product.lib.php';
  24. require_once DOL_DOCUMENT_ROOT . '/product/class/product.class.php';
  25. $langs->load("products");
  26. $langs->load("bills");
  27. $id = GETPOST('id', 'alpha');
  28. $action = GETPOST('action', 'alpha');
  29. // Security check
  30. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  31. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  32. if ($user->societe_id)
  33. $socid = $user->societe_id;
  34. $result = restrictedArea($user, 'produit|service', $fieldvalue, 'product&product', '', '', $fieldtype);
  35. $object = new Product($db);
  36. /*
  37. * Actions
  38. */
  39. if ($action == 'update_price' && !$_POST["cancel"] && ($user->rights->produit->creer || $user->rights->service->creer)) {
  40. $result = $object->fetch($id);
  41. // MultiPrix
  42. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  43. $newprice = '';
  44. $newprice_min = '';
  45. $newpricebase = '';
  46. $newvat = '';
  47. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  48. if (isset($_POST["price_" . $i])) {
  49. $level = $i;
  50. $newprice = price2num($_POST["price_" . $i], 'MU');
  51. $newprice_min = price2num($_POST["price_min_" . $i], 'MU');
  52. $newpricebase = $_POST["multiprices_base_type_" . $i];
  53. $newnpr = (preg_match('/\*/', $_POST["tva_tx_" . $i]) ? 1 : 0);
  54. $newvat = str_replace('*', '', $_POST["tva_tx_" . $i]);
  55. $newecotax = price2num($_POST["ecotax" . $i], 'MU');
  56. break; // We found submited price
  57. }
  58. }
  59. } else {
  60. $level = 0;
  61. $newprice = price2num($_POST["price"], 'MU');
  62. $newprice_min = price2num($_POST["price_min"], 'MU');
  63. $newpricebase = $_POST["price_base_type"];
  64. $newnpr = (preg_match('/\*/', $_POST["tva_tx"]) ? 1 : 0);
  65. $newvat = str_replace('*', '', $_POST["tva_tx"]);
  66. $newecotax = price2num($_POST["ecotax"], 'MU');
  67. }
  68. if ($object->updatePrice($object->id, $newprice, $newpricebase, $user, $newvat, $newprice_min, $newecotax, $level, $newnpr) > 0) {
  69. $action = '';
  70. $mesg = '<div class="ok">' . $langs->trans("RecordSaved") . '</div>';
  71. } else {
  72. $action = 'edit_price';
  73. $mesg = '<div class="error">' . $object->error . '</div>';
  74. }
  75. } else if ($action == 'delete' && $user->rights->produit->supprimer) {
  76. $result = $object->log_price_delete($user, $_GET["lineid"]);
  77. if ($result < 0)
  78. $mesg = '<div class="error">' . $object->error . '</div>';
  79. }
  80. /*
  81. * View
  82. */
  83. $form = new Form($db);
  84. if (!empty($id))
  85. $result = $object->fetch($id);
  86. llxHeader("", "", $langs->trans("CardProduct" . $object->type));
  87. $head = product_prepare_head($object, $user);
  88. $titre = $langs->trans("CardProduct" . $object->type);
  89. $picto = ($object->type == 1 ? 'service' : 'product');
  90. dol_fiche_head($head, 'price', $titre, 0, $picto);
  91. print '<table class="border" width="100%">';
  92. // Ref
  93. print '<tr>';
  94. print '<td width="15%">' . $langs->trans("Ref") . '</td><td colspan="2">';
  95. print $form->showrefnav($object, 'ref', '', 1, 'ref');
  96. print '</td>';
  97. print '</tr>';
  98. // Label
  99. print '<tr><td>' . $langs->trans("Label") . '</td><td>' . $object->libelle . '</td>';
  100. $isphoto = $object->is_photo_available($conf->product->multidir_output[$object->entity]);
  101. $nblignes = 5;
  102. if ($isphoto) {
  103. // Photo
  104. print '<td valign="middle" align="center" width="30%" rowspan="' . $nblignes . '">';
  105. print $object->show_photos($conf->product->multidir_output[$object->entity], 1, 1, 0, 0, 0, 80);
  106. print '</td>';
  107. }
  108. print '</tr>';
  109. // MultiPrix
  110. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  111. if (!empty($socid)) {
  112. $soc = new Societe($db);
  113. $soc->id = $socid;
  114. $soc->fetch($socid);
  115. print '<tr><td>' . $langs->trans("SellingPrice") . '</td>';
  116. if ($object->multiprices_base_type["$soc->price_level"] == 'TTC') {
  117. print '<td>' . price($object->multiprices_ttc["$soc->price_level"]);
  118. } else {
  119. print '<td>' . price($object->multiprices["$soc->price_level"]);
  120. }
  121. if ($object->multiprices_base_type["$soc->price_level"]) {
  122. print ' ' . $langs->trans($object->multiprices_base_type["$soc->price_level"]);
  123. } else {
  124. print ' ' . $langs->trans($object->price_base_type);
  125. }
  126. print '</td></tr>';
  127. // Prix mini
  128. print '<tr><td>' . $langs->trans("MinPrice") . '</td><td>';
  129. if ($object->multiprices_base_type["$soc->price_level"] == 'TTC') {
  130. print price($object->multiprices_min_ttc["$soc->price_level"]) . ' ' . $langs->trans($object->multiprices_base_type["$soc->price_level"]);
  131. } else {
  132. print price($object->multiprices_min["$soc->price_level"]) . ' ' . $langs->trans($object->multiprices_base_type["$soc->price_level"]);
  133. }
  134. print '</td></tr>';
  135. // TVA
  136. print '<tr><td>' . $langs->trans("VATRate") . '</td><td>' . vatrate($object->multiprices_tva_tx["$soc->price_level"], true) . '</td></tr>';
  137. } else {
  138. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  139. // TVA
  140. if ($i == 1) { // We show only price for level 1
  141. print '<tr><td>' . $langs->trans("VATRate") . '</td><td>' . vatrate($object->multiprices_tva_tx[1], true) . '</td></tr>';
  142. }
  143. print '<tr><td>' . $langs->trans("SellingPrice") . ' ' . $i . '</td>';
  144. if ($object->multiprices_base_type["$i"] == 'TTC') {
  145. print '<td>' . price($object->multiprices_ttc["$i"]);
  146. } else {
  147. print '<td>' . price($object->multiprices["$i"]);
  148. }
  149. if ($object->multiprices_base_type["$i"]) {
  150. print ' ' . $langs->trans($object->multiprices_base_type["$i"]);
  151. } else {
  152. print ' ' . $langs->trans($object->price_base_type);
  153. }
  154. print '</td></tr>';
  155. // Ecotaxe
  156. if ($conf->global->PRODUCT_USE_ECOTAX && $product->type == 0) {
  157. print '<tr><td>' . $langs->trans("Ecotax") . '</td><td>';
  158. if ($product->multiprices_base_type["$i"] == 'TTC') {
  159. print price($product->ecotax["$i"]) . ' ' . $langs->trans($product->multiprices_base_type["$i"]);
  160. } else {
  161. print price($product->ecotax_ttc["$i"]) . ' ' . $langs->trans($product->multiprices_base_type["$i"]);
  162. }
  163. print '</td></tr>';
  164. }
  165. // Prix mini
  166. print '<tr><td>' . $langs->trans("MinPrice") . ' ' . $i . '</td><td>';
  167. if ($object->multiprices_base_type["$i"] == 'TTC') {
  168. print price($object->multiprices_min_ttc["$i"]) . ' ' . $langs->trans($object->multiprices_base_type["$i"]);
  169. } else {
  170. print price($object->multiprices_min["$i"]) . ' ' . $langs->trans($object->multiprices_base_type["$i"]);
  171. }
  172. print '</td></tr>';
  173. }
  174. }
  175. } else {
  176. // TVA
  177. print '<tr><td>' . $langs->trans("VATRate") . '</td><td>' . vatrate($object->tva_tx . ($object->tva_npr ? '*' : ''), true) . '</td></tr>';
  178. // Price
  179. print '<tr><td>' . $langs->trans("SellingPrice") . '</td><td>';
  180. if ($object->price_base_type == 'TTC') {
  181. print price($object->price_ttc) . ' ' . $langs->trans($object->price_base_type);
  182. } else {
  183. print price($object->price) . ' ' . $langs->trans($object->price_base_type);
  184. }
  185. print '</td></tr>';
  186. // Ecotaxe
  187. if ($conf->global->PRODUCT_USE_ECOTAX && $product->type == 0) {
  188. print '<tr><td>' . $langs->trans("Ecotax") . '</td><td>';
  189. if ($product->price_base_type == 'TTC') {
  190. print price($product->ecotax_ttc) . ' ' . $langs->trans($product->price_base_type);
  191. } else {
  192. print price($product->ecotax) . ' ' . $langs->trans($product->price_base_type);
  193. }
  194. print '</td></tr>';
  195. }
  196. // Price minimum
  197. print '<tr><td>' . $langs->trans("MinPrice") . '</td><td>';
  198. if ($object->price_base_type == 'TTC') {
  199. print price($object->price_min_ttc) . ' ' . $langs->trans($object->price_base_type);
  200. } else {
  201. print price($object->price_min) . ' ' . $langs->trans($object->price_base_type);
  202. }
  203. print '</td></tr>';
  204. }
  205. // Status (to sell)
  206. print '<tr><td>' . $langs->trans("Status") . ' (' . $langs->trans("Sell") . ')</td><td>';
  207. print $object->getLibStatus();
  208. print '</td></tr>';
  209. print "</table>\n";
  210. print "</div>\n";
  211. if (!empty($mesg)) {
  212. dol_htmloutput_mesg($mesg);
  213. }
  214. /* * ************************************************************************* */
  215. /* */
  216. /* Barre d'action */
  217. /* */
  218. /* * ************************************************************************* */
  219. if (!$action || $action == 'delete') {
  220. print "\n" . '<div class="tabsAction">' . "\n";
  221. if ($user->rights->produit->creer || $user->rights->service->creer) {
  222. print '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?action=edit_price&amp;id=' . $object->id . '">' . $langs->trans("UpdatePrice") . '</a>';
  223. }
  224. print "\n</div>\n";
  225. }
  226. /*
  227. * Edition du prix
  228. */
  229. if ($action == 'edit_price' && ($user->rights->produit->creer || $user->rights->service->creer)) {
  230. print_fiche_titre($langs->trans("NewPrice"), '', '');
  231. if (empty($conf->global->PRODUIT_MULTIPRICES)) {
  232. print '<form action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="POST">';
  233. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  234. print '<input type="hidden" name="action" value="update_price">';
  235. print '<input type="hidden" name="id" value="' . $object->id . '">';
  236. print '<table class="border" width="100%">';
  237. // VAT
  238. print '<tr><td>' . $langs->trans("VATRate") . '</td><td>';
  239. print $form->load_tva("tva_tx", $object->tva_tx, $mysoc, '', $object->id, $object->tva_npr);
  240. print '</td></tr>';
  241. // Price base
  242. print '<tr><td width="15%">';
  243. print $langs->trans('PriceBase');
  244. print '</td>';
  245. print '<td>';
  246. print $form->select_PriceBaseType($object->price_base_type, "price_base_type");
  247. print '</td>';
  248. print '</tr>';
  249. // Price
  250. print '<tr><td width="20%">';
  251. $text = $langs->trans('SellingPrice');
  252. print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
  253. print '</td><td>';
  254. if ($object->price_base_type == 'TTC') {
  255. print '<input name="price" size="10" value="' . price($object->price_ttc) . '">';
  256. } else {
  257. print '<input name="price" size="10" value="' . price($object->price) . '">';
  258. }
  259. print '</td></tr>';
  260. // EcoTax
  261. if ($conf->global->PRODUCT_USE_ECOTAX && $product->type == 0) {
  262. print '<tr><td>';
  263. $text = $langs->trans("Ecotax");
  264. print $html->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), $direction = 1, $usehelpcursor = 1);
  265. print '</td><td>';
  266. if ($product->price_base_type == 'TTC') {
  267. print '<input name="ecotax" size="10" value="' . price($product->ecotax_ttc) . '">';
  268. } else {
  269. print '<input name="ecotax" size="10" value="' . price($product->ecotax) . '">';
  270. }
  271. print '</td></tr>';
  272. }
  273. // Price minimum
  274. print '<tr><td>';
  275. $text = $langs->trans('MinPrice');
  276. print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
  277. if ($object->price_base_type == 'TTC') {
  278. print '<td><input name="price_min" size="10" value="' . price($object->price_min_ttc) . '">';
  279. } else {
  280. print '<td><input name="price_min" size="10" value="' . price($object->price_min) . '">';
  281. }
  282. print '</td></tr>';
  283. print '</table>';
  284. print '<center><br><input type="submit" class="button" value="' . $langs->trans("Save") . '">&nbsp;';
  285. print '<input type="submit" class="button" name="cancel" value="' . $langs->trans("Cancel") . '"></center>';
  286. print '<br></form>';
  287. } else {
  288. for ($i = 1; $i <= $conf->global->PRODUIT_MULTIPRICES_LIMIT; $i++) {
  289. print '<form action="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '" method="POST">';
  290. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  291. print '<input type="hidden" name="action" value="update_price">';
  292. print '<input type="hidden" name="id" value="' . $object->id . '">';
  293. print '<table class="border" width="100%">';
  294. // VAT
  295. if ($i == 1) {
  296. print '<tr><td>' . $langs->trans("VATRate") . '</td><td>';
  297. print $form->load_tva("tva_tx_" . $i, $object->multiprices_tva_tx["$i"], $mysoc, '', $object->id);
  298. print '</td></tr>';
  299. } else { // We always use the vat rate of price level 1 (A vat rate does not depends on customer)
  300. print '<input type="hidden" name="tva_tx_' . $i . '" value="' . $object->multiprices_tva_tx[1] . '">';
  301. }
  302. // Selling price
  303. print '<tr><td width="20%">';
  304. $text = $langs->trans('SellingPrice') . ' ' . $i;
  305. print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
  306. print '</td><td>';
  307. if ($object->multiprices_base_type["$i"] == 'TTC') {
  308. print '<input name="price_' . $i . '" size="10" value="' . price($object->multiprices_ttc["$i"]) . '">';
  309. } else {
  310. print '<input name="price_' . $i . '" size="10" value="' . price($object->multiprices["$i"]) . '">';
  311. }
  312. print $form->select_PriceBaseType($object->multiprices_base_type["$i"], "multiprices_base_type_" . $i);
  313. print '</td></tr>';
  314. // Min price
  315. print '<tr><td>';
  316. $text = $langs->trans('MinPrice') . ' ' . $i;
  317. print $form->textwithpicto($text, $langs->trans("PrecisionUnitIsLimitedToXDecimals", $conf->global->MAIN_MAX_DECIMALS_UNIT), 1, 1);
  318. if ($object->multiprices_base_type["$i"] == 'TTC') {
  319. print '<td><input name="price_min_' . $i . '" size="10" value="' . price($object->multiprices_min_ttc["$i"]) . '">';
  320. } else {
  321. print '<td><input name="price_min_' . $i . '" size="10" value="' . price($object->multiprices_min["$i"]) . '">';
  322. }
  323. print '</td></tr>';
  324. print '<tr><td colspan="2" align="center"><input type="submit" class="button" value="' . $langs->trans("Save") . '">&nbsp;';
  325. print '<input type="submit" class="button" name="cancel" value="' . $langs->trans("Cancel") . '"></td></tr>';
  326. print '</table>';
  327. print '</form>';
  328. }
  329. }
  330. }
  331. // Liste des evolutions du prix
  332. $sql = "SELECT p.rowid, p.price, p.price_ttc, p.price_base_type, p.tva_tx, p.recuperableonly,";
  333. $sql.= " p.price_level, p.price_min, p.price_min_ttc, p.ecotax, p.ecotax_ttc,";
  334. $sql.= " p.date_price as dp, u.rowid as user_id, u.login";
  335. $sql.= " FROM " . MAIN_DB_PREFIX . "product_price as p,";
  336. $sql.= " " . MAIN_DB_PREFIX . "user as u";
  337. $sql.= " WHERE fk_product = " . $object->id;
  338. $sql.= " AND p.fk_user_author = u.rowid";
  339. if (!empty($socid) && !empty($conf->global->PRODUIT_MULTIPRICES))
  340. $sql.= " AND p.price_level = " . $soc->price_level;
  341. $sql.= " ORDER BY p.date_price DESC, p.price_level ASC";
  342. //$sql .= $db->plimit();
  343. $result = $db->query($sql);
  344. if ($result) {
  345. $num = $db->num_rows($result);
  346. if (!$num) {
  347. $db->free($result);
  348. // Il doit au moins y avoir la ligne de prix initial.
  349. // On l'ajoute donc pour remettre a niveau (pb vieilles versions)
  350. $object->updatePrice($object->id, $object->price, 'HT', $user, $newprice_min);
  351. $result = $db->query($sql);
  352. $num = $db->num_rows($result);
  353. }
  354. if ($num > 0) {
  355. print '<br>';
  356. print '<table class="noborder" width="100%">';
  357. print '<tr class="liste_titre">';
  358. print '<td>' . $langs->trans("AppliedPricesFrom") . '</td>';
  359. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  360. print '<td>' . $langs->trans("MultiPriceLevelsName") . '</td>';
  361. }
  362. print '<td align="center">' . $langs->trans("PriceBase") . '</td>';
  363. print '<td align="right">' . $langs->trans("VAT") . '</td>';
  364. print '<td align="right">' . $langs->trans("HT") . '</td>';
  365. print '<td align="right">' . $langs->trans("TTC") . '</td>';
  366. if ($conf->global->PRODUCT_USE_ECOTAX && $product->type == 0) {
  367. print '<td align="right">' . $langs->trans("Ecotax") . '</td>';
  368. print '<td align="right">' . $langs->trans("EcotaxTTC") . '</td>';
  369. }
  370. print '<td align="right">' . $langs->trans("MinPrice") . ' ' . $langs->trans("HT") . '</td>';
  371. print '<td align="right">' . $langs->trans("MinPrice") . ' ' . $langs->trans("TTC") . '</td>';
  372. print '<td align="right">' . $langs->trans("ChangedBy") . '</td>';
  373. if ($user->rights->produit->supprimer)
  374. print '<td align="right">&nbsp;</td>';
  375. print '</tr>';
  376. $var = True;
  377. $i = 0;
  378. while ($i < $num) {
  379. $objp = $db->fetch_object($result);
  380. $var = !$var;
  381. print "<tr $bc[$var]>";
  382. // Date
  383. print "<td>" . dol_print_date($db->jdate($objp->dp), "dayhour") . "</td>";
  384. // Price level
  385. if (!empty($conf->global->PRODUIT_MULTIPRICES)) {
  386. print '<td align="center">' . $objp->price_level . "</td>";
  387. }
  388. print '<td align="center">' . $langs->trans($objp->price_base_type) . "</td>";
  389. print '<td align="right">' . vatrate($objp->tva_tx, true, $objp->recuperableonly) . "</td>";
  390. print '<td align="right">' . price($objp->price) . "</td>";
  391. print '<td align="right">' . price($objp->price_ttc) . "</td>";
  392. if ($conf->global->PRODUCT_USE_ECOTAX && $product->type == 0) {
  393. print '<td align="right">' . price($objp->ecotax) . "</td>";
  394. print '<td align="right">' . price($objp->ecotax_ttc) . "</td>";
  395. }
  396. print '<td align="right">' . price($objp->price_min) . '</td>';
  397. print '<td align="right">' . price($objp->price_min_ttc) . '</td>';
  398. // User
  399. print '<td align="right"><a href="' . DOL_URL_ROOT . '/user/fiche.php?id=' . $objp->user_id . '">' . img_object($langs->trans("ShowUser"), 'user') . ' ' . $objp->login . '</a></td>';
  400. // Action
  401. if ($user->rights->produit->supprimer) {
  402. print '<td align="right">';
  403. if ($i > 0) {
  404. print '<a href="' . $_SERVER["PHP_SELF"] . '?action=delete&amp;id=' . $object->id . '&amp;lineid=' . $objp->rowid . '">';
  405. print img_delete();
  406. print '</a>';
  407. }
  408. else
  409. print '&nbsp;'; // Can not delete last price (it's current price)
  410. print '</td>';
  411. }
  412. print "</tr>\n";
  413. $i++;
  414. }
  415. $db->free($result);
  416. print "</table>";
  417. print "<br>";
  418. }
  419. }
  420. else {
  421. dol_print_error($db);
  422. }
  423. llxFooter();
  424. $db->close();
  425. ?>