margins.lib.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  3. * Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
  4. * Copyright (C) 2016 Florian Henry <florian.henry@open-concept.pro>
  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/margin/lib/margins.lib.php
  21. * \ingroup margin
  22. * \brief Library for common margin functions
  23. */
  24. /**
  25. * Define head array for tabs of marges tools setup pages
  26. *
  27. * @return Array of head
  28. */
  29. function marges_admin_prepare_head()
  30. {
  31. global $langs, $conf;
  32. $h = 0;
  33. $head = array();
  34. $head[$h][0] = DOL_URL_ROOT."/margin/admin/margin.php";
  35. $head[$h][1] = $langs->trans("Parameters");
  36. $head[$h][2] = 'parameters';
  37. $h++;
  38. // Show more tabs from modules
  39. // Entries must be declared in modules descriptor with line
  40. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  41. // $this->tabs = array('entity:-tabname); to remove a tab
  42. complete_head_from_modules($conf, $langs, null, $head, $h, 'margesadmin');
  43. complete_head_from_modules($conf, $langs, null, $head, $h, 'margesadmin', 'remove');
  44. return $head;
  45. }
  46. /**
  47. * Return array of tabs to used on pages for third parties cards.
  48. *
  49. * @return array Array of tabs
  50. */
  51. function marges_prepare_head()
  52. {
  53. global $langs, $conf, $user;
  54. $langs->load("margins");
  55. $h = 0;
  56. $head = array();
  57. if ($user->hasRight('produit', 'lire')) {
  58. $head[$h][0] = DOL_URL_ROOT."/margin/productMargins.php";
  59. $head[$h][1] = $langs->trans("ProductMargins");
  60. $head[$h][2] = 'productMargins';
  61. $h++;
  62. }
  63. if ($user->hasRight('societe', 'lire')) {
  64. $head[$h][0] = DOL_URL_ROOT."/margin/customerMargins.php";
  65. $head[$h][1] = $langs->trans("CustomerMargins");
  66. $head[$h][2] = 'customerMargins';
  67. $h++;
  68. }
  69. if ($user->hasRight('margins', 'read', 'all')) {
  70. $title = 'UserMargins';
  71. } else {
  72. $title = 'SalesRepresentativeMargins';
  73. }
  74. $head[$h][0] = DOL_URL_ROOT."/margin/agentMargins.php";
  75. $head[$h][1] = $langs->trans($title);
  76. $head[$h][2] = 'agentMargins';
  77. if ($user->hasRight('margins', 'creer')) {
  78. $h++;
  79. $head[$h][0] = DOL_URL_ROOT."/margin/checkMargins.php";
  80. $head[$h][1] = $langs->trans('CheckMargins');
  81. $head[$h][2] = 'checkMargins';
  82. }
  83. complete_head_from_modules($conf, $langs, null, $head, $h, 'margins');
  84. complete_head_from_modules($conf, $langs, null, $head, $h, 'margins', 'remove');
  85. return $head;
  86. }
  87. /**
  88. * Return an array with margins information of a line
  89. *
  90. * @param float $pvht Selling price without tax
  91. * @param float $remise_percent Discount percent on line
  92. * @param float $tva_tx Vat rate (not used)
  93. * @param float $localtax1_tx Vat rate special 1 (not used)
  94. * @param float $localtax2_tx Vat rate special 2 (not used)
  95. * @param int $fk_pa Id of buying price (prefer set this to 0 and provide $paht instead. With id, buying price may have change)
  96. * @param float $paht Buying price without tax
  97. * @return array Array of margin info (buying price, marge rate, marque rate)
  98. */
  99. function getMarginInfos($pvht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $paht)
  100. {
  101. global $db, $conf;
  102. $marge_tx_ret = '';
  103. $marque_tx_ret = '';
  104. if ($fk_pa > 0 && empty($paht)) {
  105. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  106. $product = new ProductFournisseur($db);
  107. if ($product->fetch_product_fournisseur_price($fk_pa)) {
  108. $paht_ret = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
  109. } else {
  110. $paht_ret = $paht;
  111. }
  112. } else {
  113. $paht_ret = $paht;
  114. }
  115. // Calculate selling unit price including line discount
  116. // We don't use calculate_price, because this function is dedicated to calculation of total with accuracy of total. We need an accuracy of a unit price.
  117. // Also we must not apply rounding on non decimal rule defined by option MAIN_ROUNDING_RULE_TOT
  118. $pu_ht_remise = $pvht * (1 - ($remise_percent / 100));
  119. $pu_ht_remise = price2num($pu_ht_remise, 'MU');
  120. // calcul marge
  121. if ($pu_ht_remise < 0) {
  122. $marge = -1 * (abs($pu_ht_remise) - $paht_ret);
  123. } else {
  124. $marge = $pu_ht_remise - $paht_ret;
  125. }
  126. // calcul taux marge
  127. if ($paht_ret != 0) {
  128. $marge_tx_ret = (100 * $marge) / $paht_ret;
  129. }
  130. // calcul taux marque
  131. if ($pu_ht_remise != 0) {
  132. $marque_tx_ret = (100 * $marge) / $pu_ht_remise;
  133. }
  134. return array($paht_ret, $marge_tx_ret, $marque_tx_ret);
  135. }