PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/lib/price.lib.php

https://github.com/asterix14/dolibarr
PHP | 164 lines | 101 code | 22 blank | 41 comment | 8 complexity | fbd6e6074663cf01cddddfdc09e2ec0f MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2002-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2006-2008 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2010 Juanjo Menent <jmenent@2byte.es>
  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 2 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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/lib/price.lib.php
  21. * \brief Librairie contenant les fonctions pour calculer un prix.
  22. */
  23. /**
  24. * Calculate totals (net, vat, ...) of a line.
  25. * @param qty Quantity
  26. * @param pu Unit price (HT or TTC selon price_base_type)
  27. * @param remise_percent_ligne Discount for line
  28. * @param txtva Vat rate
  29. * @param txlocaltax1 Localtax1 rate (used for some countries only, like spain)
  30. * @param txlocaltax2 Localtax2 rate (used for some countries only, like spain)
  31. * @param remise_percent_global 0
  32. * @param price_base_type HT=on calcule sur le HT, TTC=on calcule sur le TTC
  33. * @param info_bits Miscellanous informations on line
  34. * @return result[0,1,2,3,4,5,6,7,8] (total_ht, total_vat, total_ttc, pu_ht, pu_tva, pu_ttc, total_ht_without_discount, total_vat_without_discount, total_ttc_without_discount)
  35. */
  36. function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $txlocaltax1=0, $txlocaltax2=0, $remise_percent_global=0, $price_base_type='HT', $info_bits=0)
  37. {
  38. global $conf,$mysoc;
  39. $result=array();
  40. //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits");
  41. if ($price_base_type == 'HT')
  42. {
  43. // We work to define prices using the price without tax
  44. $tot_sans_remise = $pu * $qty;
  45. $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100));
  46. $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
  47. $result[6] = price2num($tot_sans_remise, 'MT');
  48. $result[8] = price2num($tot_sans_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  49. $result8bis= price2num($tot_sans_remise * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  50. $result[7] = $result8bis - $result[6];
  51. $result[0] = price2num($tot_avec_remise, 'MT');
  52. $result[2] = price2num($tot_avec_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  53. $result2bis= price2num($tot_avec_remise * (1 + ( $txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  54. $result[1] = $result2bis - $result[0]; // Total VAT = TTC - HT
  55. $result[3] = price2num($pu, 'MU');
  56. $result[5] = price2num($pu * (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non
  57. $result5bis= price2num($pu * (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
  58. $result[4] = $result5bis - $result[3];
  59. }
  60. else
  61. {
  62. // We work to define prices using the price with tax
  63. $tot_sans_remise = $pu * $qty;
  64. $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100));
  65. $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
  66. $result[8] = price2num($tot_sans_remise, 'MT');
  67. $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  68. $result6bis= price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  69. $result[7] = $result[8] - $result6bis;
  70. $result[2] = price2num($tot_avec_remise, 'MT');
  71. $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  72. $result0bis= price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  73. $result[1] = $result[2] - $result0bis; // Total VAT = TTC - HT
  74. $result[5] = price2num($pu, 'MU');
  75. $result[3] = price2num($pu / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non
  76. $result3bis= price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
  77. $result[4] = $result[5] - $result3bis;
  78. }
  79. //Local taxes
  80. if ($txlocaltax1>0)
  81. {
  82. $result[14] = price2num(($tot_sans_remise * (1 + ( $txlocaltax1 / 100))) - $tot_sans_remise, 'MT');
  83. $result[8] = $result[8] + $result[14];
  84. $result[9] = price2num(($tot_avec_remise * (1 + ( $txlocaltax1 / 100))) - $tot_avec_remise, 'MT');
  85. $result[2] = $result[2] + $result[9];
  86. $result[11] = price2num(($pu * (1 + ( $txlocaltax1 / 100))) - $pu, 'MT');
  87. $result[5] = $result[5] + $result[11];
  88. }
  89. else
  90. {
  91. $result[14] = 0;
  92. $result[9] = 0;
  93. $result[11] = 0;
  94. }
  95. if ($txlocaltax2>0)
  96. {
  97. $result[15] = price2num(($tot_sans_remise * (1 + ( $txlocaltax2 / 100))) - $tot_sans_remise, 'MT');
  98. $result[10] = price2num(($tot_avec_remise * (1 + ( $txlocaltax2 / 100))) - $tot_avec_remise, 'MT');
  99. $result[12] = price2num(($pu * (1 + ( $txlocaltax2 / 100))) - $pu, 'MT');
  100. //If Country is Spain, localtax2 (IRPF) will be subtracted
  101. if ($mysoc->pays_code=='ES')
  102. {
  103. $result[8] = $result[8] - $result[15];
  104. $result[2] = $result[2] - $result[10];
  105. $result[5] = $result[5] - $result[12];
  106. }
  107. else
  108. {
  109. $result[8] = $result[8] + $result[15];
  110. $result[2] = $result[2] + $result[10];
  111. $result[5] = $result[5] + $result[12];
  112. }
  113. }
  114. else
  115. {
  116. $result[15] = 0;
  117. $result[10] = 0;
  118. $result[12] = 0;
  119. }
  120. // If rounding is not using base 10 (rare)
  121. if (! empty($conf->global->MAIN_ROUNDING_RULE_TOT))
  122. {
  123. if ($price_base_type == 'HT')
  124. {
  125. $result[0]=round($result[0]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  126. $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  127. $result[2]=$result[0]+$result[1];
  128. $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  129. $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  130. }
  131. else
  132. {
  133. $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  134. $result[2]=round($result[2]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  135. $result[0]=$result[2]-$result[0];
  136. $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  137. $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  138. }
  139. }
  140. //print "Price.lib::calcul_price_total ".$result[0]."-".$result[1]."-".$result[2];
  141. return $result;
  142. }