getSupplierPrices.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  3. * Copyright (C) 2015 Francis Appels <francis.appels@z-application.com>
  4. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file /htdocs/fourn/ajax/getSupplierPrices.php
  21. * \brief File to return an Ajax response to get list of possible prices for margin calculation
  22. */
  23. if (!defined('NOTOKENRENEWAL')) {
  24. define('NOTOKENRENEWAL', '1'); // Disables token renewal
  25. }
  26. if (!defined('NOREQUIREMENU')) {
  27. define('NOREQUIREMENU', '1');
  28. }
  29. if (!defined('NOREQUIREAJAX')) {
  30. define('NOREQUIREAJAX', '1');
  31. }
  32. if (!defined('NOREQUIRESOC')) {
  33. define('NOREQUIRESOC', '1');
  34. }
  35. // Load Dolibarr environment
  36. require '../../main.inc.php';
  37. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  38. $idprod = GETPOST('idprod', 'int');
  39. $prices = array();
  40. // Load translation files required by the page
  41. $langs->loadLangs(array("stocks", "margins", "products"));
  42. /*
  43. * View
  44. */
  45. top_httphead();
  46. //print '<!-- Ajax page called with url '.dol_escape_htmltag($_SERVER["PHP_SELF"]).'?'.dol_escape_htmltag($_SERVER["QUERY_STRING"]).' -->'."\n";
  47. if ($idprod > 0) {
  48. $producttmp = new ProductFournisseur($db);
  49. $producttmp->fetch($idprod);
  50. $sorttouse = 's.nom, pfp.quantity, pfp.price';
  51. if (GETPOST('bestpricefirst')) {
  52. $sorttouse = 'pfp.unitprice, s.nom, pfp.quantity, pfp.price';
  53. }
  54. $productSupplierArray = $producttmp->list_product_fournisseur_price($idprod, $sorttouse); // We list all price per supplier, and then firstly with the lower quantity. So we can choose first one with enough quantity into list.
  55. if (is_array($productSupplierArray)) {
  56. foreach ($productSupplierArray as $productSupplier) {
  57. $price = $productSupplier->fourn_price * (1 - $productSupplier->fourn_remise_percent / 100);
  58. $unitprice = $productSupplier->fourn_unitprice * (1 - $productSupplier->fourn_remise_percent / 100);
  59. $title = $productSupplier->fourn_name.' - '.$productSupplier->fourn_ref.' - ';
  60. if ($productSupplier->fourn_qty == 1) {
  61. $title .= price($price, 0, $langs, 0, 0, -1, $conf->currency)."/";
  62. }
  63. $title .= $productSupplier->fourn_qty.' '.($productSupplier->fourn_qty == 1 ? $langs->trans("Unit") : $langs->trans("Units"));
  64. if ($productSupplier->fourn_qty > 1) {
  65. $title .= " - ";
  66. $title .= price($unitprice, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
  67. $price = $unitprice;
  68. }
  69. $label = price($price, 0, $langs, 0, 0, -1, $conf->currency)."/".$langs->trans("Unit");
  70. if ($productSupplier->fourn_ref) {
  71. $label .= ' ('.$productSupplier->fourn_ref.')';
  72. }
  73. $prices[] = array("id" => $productSupplier->product_fourn_price_id, "price" => price2num($price, '', 0), "label" => $label, "title" => $title); // For price field, we must use price2num(), for label or title, price()
  74. }
  75. }
  76. // After best supplier prices and before costprice
  77. if (isModEnabled('stock')) {
  78. // Add price for pmp
  79. $price = $producttmp->pmp;
  80. if (empty($price) && !empty($conf->global->PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY)) {
  81. // get pmp for subproducts if any
  82. $producttmp->get_sousproduits_arbo();
  83. $prods_arbo=$producttmp->get_arbo_each_prod();
  84. if (!empty($prods_arbo)) {
  85. $price = 0;
  86. foreach ($prods_arbo as $child) {
  87. $sousprod = new Product($db);
  88. $sousprod->fetch($child['id']);
  89. $price += $sousprod->pmp;
  90. }
  91. }
  92. }
  93. $prices[] = array("id" => 'pmpprice', "price" => price2num($price), "label" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
  94. }
  95. // Add price for costprice (at end)
  96. $price = $producttmp->cost_price;
  97. if (empty($price) && !empty($conf->global->PRODUCT_USE_SUB_COST_PRICES_IF_COST_PRICE_EMPTY)) {
  98. // get costprice for subproducts if any
  99. $producttmp->get_sousproduits_arbo();
  100. $prods_arbo=$producttmp->get_arbo_each_prod();
  101. if (!empty($prods_arbo)) {
  102. $price = 0;
  103. foreach ($prods_arbo as $child) {
  104. $sousprod = new Product($db);
  105. $sousprod->fetch($child['id']);
  106. $price += $sousprod->cost_price;
  107. }
  108. }
  109. }
  110. $prices[] = array("id" => 'costprice', "price" => price2num($price), "label" => $langs->trans("CostPrice").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency), "title" => $langs->trans("PMPValueShort").': '.price($price, 0, $langs, 0, 0, -1, $conf->currency)); // For price field, we must use price2num(), for label or title, price()
  111. }
  112. echo json_encode($prices);