price.lib.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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-2013 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  6. * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
  7. * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  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. /**
  23. * \file htdocs/core/lib/price.lib.php
  24. * \brief Library with functions to calculate prices
  25. */
  26. /**
  27. * Calculate totals (net, vat, ...) of a line.
  28. * Value for localtaxX_type are '0' : local tax not applied
  29. * '1' : local tax apply on products and services without vat (localtax is calculated on amount without tax)
  30. * '2' : local tax apply on products and services including vat (localtax is calculated on amount + tax)
  31. * '3' : local tax apply on products without vat (localtax is calculated on amount without tax)
  32. * '4' : local tax apply on products including vat (localtax is calculated on amount + tax)
  33. * '5' : local tax apply on services without vat (localtax is calculated on amount without tax)
  34. * '6' : local tax apply on services including vat (localtax is calculated on amount + tax)
  35. *
  36. * @param int $qty Quantity
  37. * @param float $pu Unit price (HT or TTC selon price_base_type)
  38. * @param float $remise_percent_ligne Discount for line
  39. * @param float $txtva 0=do not apply standard tax, Vat rate=apply
  40. * @param float $uselocaltax1_rate 0=do not use this localtax, >0=apply and get value from localtaxes_array (or database if empty), -1=autodetect according to seller if we must apply, get value from localtaxes_array (or database if empty). Try to always use -1.
  41. * @param float $uselocaltax2_rate 0=do not use this localtax, >0=apply and get value from localtaxes_array (or database if empty), -1=autodetect according to seller if we must apply, get value from localtaxes_array (or database if empty). Try to always use -1.
  42. * @param float $remise_percent_global 0
  43. * @param string $price_base_type HT=Unit price parameter is HT, TTC=Unit price parameter is TTC
  44. * @param int $info_bits Miscellaneous informations on line
  45. * @param int $type 0/1=Product/service
  46. * @param Societe $seller Thirdparty seller (we need $seller->country_id property). Provided only if seller is the supplier, otherwise $seller will be $mysoc.
  47. * @param array $localtaxes_array Array with localtaxes info array('0'=>type1,'1'=>rate1,'2'=>type2,'3'=>rate2) (loaded by getLocalTaxesFromRate(vatrate, 0, ...) function).
  48. * @param integer $progress Situation invoices progress (value from 0 to 100, 100 by default)
  49. * @param double $multicurrency_tx Currency rate (1 by default)
  50. * @param double $pu_ht_devise Amount in currency
  51. * @return array [
  52. * 0=total_ht,
  53. * 1=total_vat, (main vat only)
  54. * 2=total_ttc, (total_ht + main vat + local taxes)
  55. * 3=pu_ht,
  56. * 4=pu_vat, (main vat only)
  57. * 5=pu_ttc,
  58. * 6=total_ht_without_discount,
  59. * 7=total_vat_without_discount, (main vat only)
  60. * 8=total_ttc_without_discount, (total_ht + main vat + local taxes)
  61. * 9=total_tax1 for total_ht,
  62. * 10=total_tax2 for total_ht,
  63. * 11=pu_tax1 for pu_ht, !! should not be used
  64. * 12=pu_tax2 for pu_ht, !! should not be used
  65. * 13=!! should not be used
  66. * 14=total_tax1 for total_ht_without_discount,
  67. * 15=total_tax2 for total_ht_without_discount]
  68. * 16=multicurrency_total_ht
  69. * 17=multicurrency_total_tva
  70. * 18=multicurrency_total_ttc
  71. * 19=multicurrency_pu_ht
  72. */
  73. function calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller = '', $localtaxes_array='', $progress=100, $multicurrency_tx=1, $pu_ht_devise=0)
  74. {
  75. global $conf,$mysoc,$db;
  76. $result=array();
  77. // Clean parameters
  78. if (empty($info_bits)) $info_bits=0;
  79. if (empty($txtva)) $txtva=0;
  80. if (empty($seller) || ! is_object($seller))
  81. {
  82. dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
  83. if (! is_object($mysoc)) // mysoc may be not defined (during migration process)
  84. {
  85. $mysoc=new Societe($db);
  86. $mysoc->setMysoc($conf);
  87. }
  88. $seller=$mysoc; // If sell is done to a customer, $seller is not provided, we use $mysoc
  89. //var_dump($seller->country_id);exit;
  90. }
  91. if (empty($localtaxes_array) || ! is_array($localtaxes_array))
  92. {
  93. dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing", LOG_WARNING);
  94. }
  95. // Too verbose. Enable for debug only
  96. //dol_syslog("Price.lib::calcul_price_total qty=".$qty." pu=".$pu." remiserpercent_ligne=".$remise_percent_ligne." txtva=".$txtva." uselocaltax1_rate=".$uselocaltax1_rate." uselocaltax2_rate=".$uselocaltax2_rate.' remise_percent_global='.$remise_percent_global.' price_base_type='.$ice_base_type.' type='.$type.' progress='.$progress);
  97. $countryid=$seller->country_id;
  98. if (is_numeric($uselocaltax1_rate)) $uselocaltax1_rate=(float) $uselocaltax1_rate;
  99. if (is_numeric($uselocaltax2_rate)) $uselocaltax2_rate=(float) $uselocaltax2_rate;
  100. if ($uselocaltax1_rate < 0) $uselocaltax1_rate=$seller->localtax1_assuj;
  101. if ($uselocaltax2_rate < 0) $uselocaltax2_rate=$seller->localtax2_assuj;
  102. dol_syslog('Price.lib::calcul_price_total qty='.$qty.' pu='.$pu.' remise_percent_ligne='.$remise_percent_ligne.' txtva='.$txtva.' uselocaltax1_rate='.$uselocaltax1_rate.' uselocaltax2_rate='.$uselocaltax2_rate.' remise_percent_global='.$remise_percent_global.' price_base_type='.$price_base_type.' type='.$type.' progress='.$progress);
  103. // Now we search localtaxes information ourself (rates and types).
  104. $localtax1_type=0;
  105. $localtax2_type=0;
  106. if (is_array($localtaxes_array))
  107. {
  108. $localtax1_type = $localtaxes_array[0];
  109. $localtax1_rate = $localtaxes_array[1];
  110. $localtax2_type = $localtaxes_array[2];
  111. $localtax2_rate = $localtaxes_array[3];
  112. }
  113. else // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate
  114. {
  115. $sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
  116. $sql.= " FROM ".MAIN_DB_PREFIX."c_tva as cv";
  117. $sql.= " WHERE cv.taux = ".$txtva;
  118. $sql.= " AND cv.fk_pays = ".$countryid;
  119. dol_syslog("Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
  120. $resql = $db->query($sql);
  121. if ($resql)
  122. {
  123. $obj = $db->fetch_object($resql);
  124. if ($obj)
  125. {
  126. $localtax1_rate=$obj->localtax1;
  127. $localtax2_rate=$obj->localtax2;
  128. $localtax1_type=$obj->localtax1_type;
  129. $localtax2_type=$obj->localtax2_type;
  130. //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);exit;
  131. }
  132. }
  133. else dol_print_error($db);
  134. }
  135. // pu calculation from pu_devise if pu empty
  136. if(empty($pu) && !empty($pu_ht_devise)) {
  137. $pu = $pu_ht_devise / $multicurrency_tx;
  138. } else {
  139. $pu_ht_devise = $pu * $multicurrency_tx;
  140. }
  141. // initialize total (may be HT or TTC depending on price_base_type)
  142. $tot_sans_remise = $pu * $qty * $progress / 100;
  143. $tot_avec_remise_ligne = $tot_sans_remise * (1 - ($remise_percent_ligne / 100));
  144. $tot_avec_remise = $tot_avec_remise_ligne * (1 - ($remise_percent_global / 100));
  145. // initialize result array
  146. for ($i=0; $i <= 15; $i++) $result[$i] = 0;
  147. // if there's some localtax including vat, we calculate localtaxes (we will add later)
  148. //If input unit price is 'HT', we need to have the totals with main VAT for a correct calculation
  149. if ($price_base_type != 'TTC')
  150. {
  151. $tot_sans_remise_wt = price2num($tot_sans_remise * (1 + ($txtva / 100)),'MU');
  152. $tot_avec_remise_wt = price2num($tot_avec_remise * (1 + ($txtva / 100)),'MU');
  153. $pu_wt = price2num($pu * (1 + ($txtva / 100)),'MU');
  154. }
  155. else
  156. {
  157. $tot_sans_remise_wt = $tot_sans_remise;
  158. $tot_avec_remise_wt = $tot_avec_remise;
  159. $pu_wt = $pu;
  160. }
  161. //print 'rr'.$price_base_type.'-'.$txtva.'-'.$tot_sans_remise_wt."-".$pu_wt."-".$uselocaltax1_rate."-".$localtax1_rate."-".$localtax1_type."\n";
  162. $localtaxes = array(0,0,0);
  163. $apply_tax = false;
  164. switch($localtax1_type) {
  165. case '2': // localtax on product or service
  166. $apply_tax = true;
  167. break;
  168. case '4': // localtax on product
  169. if ($type == 0) $apply_tax = true;
  170. break;
  171. case '6': // localtax on service
  172. if ($type == 1) $apply_tax = true;
  173. break;
  174. }
  175. if ($uselocaltax1_rate && $apply_tax) {
  176. $result[14] = price2num(($tot_sans_remise_wt * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise_wt, 'MT');
  177. $localtaxes[0] += $result[14];
  178. $result[9] = price2num(($tot_avec_remise_wt * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise_wt, 'MT');
  179. $localtaxes[1] += $result[9];
  180. $result[11] = price2num(($pu_wt * (1 + ( $localtax1_rate / 100))) - $pu_wt, 'MU');
  181. $localtaxes[2] += $result[11];
  182. }
  183. $apply_tax = false;
  184. switch($localtax2_type) {
  185. case '2': // localtax on product or service
  186. $apply_tax = true;
  187. break;
  188. case '4': // localtax on product
  189. if ($type == 0) $apply_tax = true;
  190. break;
  191. case '6': // localtax on service
  192. if ($type == 1) $apply_tax = true;
  193. break;
  194. }
  195. if ($uselocaltax2_rate && $apply_tax) {
  196. $result[15] = price2num(($tot_sans_remise_wt * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise_wt, 'MT');
  197. $localtaxes[0] += $result[15];
  198. $result[10] = price2num(($tot_avec_remise_wt * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise_wt, 'MT');
  199. $localtaxes[1] += $result[10];
  200. $result[12] = price2num(($pu_wt * (1 + ( $localtax2_rate / 100))) - $pu_wt, 'MU');
  201. $localtaxes[2] += $result[12];
  202. }
  203. //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits");
  204. if ($price_base_type == 'HT')
  205. {
  206. // We work to define prices using the price without tax
  207. $result[6] = price2num($tot_sans_remise, 'MT');
  208. $result[8] = price2num($tot_sans_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[0], 'MT'); // Selon TVA NPR ou non
  209. $result8bis= price2num($tot_sans_remise * (1 + ( $txtva / 100)) + $localtaxes[0], 'MT'); // Si TVA consideree normale (non NPR)
  210. $result[7] = price2num($result8bis - ($result[6] + $localtaxes[0]), 'MT');
  211. $result[0] = price2num($tot_avec_remise, 'MT');
  212. $result[2] = price2num($tot_avec_remise * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[1], 'MT'); // Selon TVA NPR ou non
  213. $result2bis= price2num($tot_avec_remise * (1 + ( $txtva / 100)) + $localtaxes[1], 'MT'); // Si TVA consideree normale (non NPR)
  214. $result[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
  215. $result[3] = price2num($pu, 'MU');
  216. $result[5] = price2num($pu * (1 + ( (($info_bits & 1)?0:$txtva) / 100)) + $localtaxes[2], 'MU'); // Selon TVA NPR ou non
  217. $result5bis= price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2], 'MU'); // Si TVA consideree normale (non NPR)
  218. $result[4] = price2num($result5bis - ($result[3] + $localtaxes[2]), 'MU');
  219. }
  220. else
  221. {
  222. // We work to define prices using the price with tax
  223. $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT');
  224. $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  225. $result6bis= price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  226. $result[7] = price2num($result[8] - ($result6bis + $localtaxes[0]), 'MT');
  227. $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT');
  228. $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  229. $result0bis= price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  230. $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
  231. $result[5] = price2num($pu + $localtaxes[2], 'MU');
  232. $result[3] = price2num($pu / (1 + ((($info_bits & 1)?0:$txtva) / 100)), 'MU'); // Selon TVA NPR ou non
  233. $result3bis= price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
  234. $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU');
  235. }
  236. // if there's some localtax without vat, we calculate localtaxes (we will add them at end)
  237. //If input unit price is 'TTC', we need to have the totals without main VAT for a correct calculation
  238. if ($price_base_type == 'TTC')
  239. {
  240. $tot_sans_remise= price2num($tot_sans_remise / (1 + ($txtva / 100)),'MU');
  241. $tot_avec_remise= price2num($tot_avec_remise / (1 + ($txtva / 100)),'MU');
  242. $pu = price2num($pu / (1 + ($txtva / 100)),'MU');
  243. }
  244. $apply_tax = false;
  245. switch($localtax1_type) {
  246. case '1': // localtax on product or service
  247. $apply_tax = true;
  248. break;
  249. case '3': // localtax on product
  250. if ($type == 0) $apply_tax = true;
  251. break;
  252. case '5': // localtax on service
  253. if ($type == 1) $apply_tax = true;
  254. break;
  255. }
  256. if ($uselocaltax1_rate && $apply_tax) {
  257. $result[14] = price2num(($tot_sans_remise * (1 + ( $localtax1_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax1 for total_ht_without_discount
  258. $result[8] += $result[14]; // total_ttc_without_discount + tax1
  259. $result[9] = price2num(($tot_avec_remise * (1 + ( $localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht
  260. $result[2] += $result[9]; // total_ttc + tax1
  261. $result[11] = price2num(($pu * (1 + ( $localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht
  262. $result[5] += $result[11]; // pu_ht + tax1
  263. }
  264. $apply_tax = false;
  265. switch($localtax2_type) {
  266. case '1': // localtax on product or service
  267. $apply_tax = true;
  268. break;
  269. case '3': // localtax on product
  270. if ($type == 0) $apply_tax = true;
  271. break;
  272. case '5': // localtax on service
  273. if ($type == 1) $apply_tax = true;
  274. break;
  275. }
  276. if ($uselocaltax2_rate && $apply_tax) {
  277. $result[15] = price2num(($tot_sans_remise * (1 + ( $localtax2_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax2 for total_ht_without_discount
  278. $result[8] += $result[15]; // total_ttc_without_discount + tax2
  279. $result[10] = price2num(($tot_avec_remise * (1 + ( $localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht
  280. $result[2] += $result[10]; // total_ttc + tax2
  281. $result[12] = price2num(($pu * (1 + ( $localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht
  282. $result[5] += $result[12]; // pu_ht + tax2
  283. }
  284. // If rounding is not using base 10 (rare)
  285. if (! empty($conf->global->MAIN_ROUNDING_RULE_TOT))
  286. {
  287. if ($price_base_type == 'HT')
  288. {
  289. $result[0]=round($result[0]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  290. $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  291. $result[2]=price2num($result[0]+$result[1], 'MT');
  292. $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  293. $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  294. }
  295. else
  296. {
  297. $result[1]=round($result[1]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  298. $result[2]=round($result[2]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  299. $result[0]=price2num($result[2]-$result[1], 'MT');
  300. $result[9]=round($result[9]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  301. $result[10]=round($result[10]/$conf->global->MAIN_ROUNDING_RULE_TOT, 0)*$conf->global->MAIN_ROUNDING_RULE_TOT;
  302. }
  303. }
  304. // Multicurrency
  305. $result[16] = price2num($result[0] * $multicurrency_tx, 'MT');
  306. $result[17] = price2num($result[1] * $multicurrency_tx, 'MT');
  307. $result[18] = price2num($result[2] * $multicurrency_tx, 'MT');
  308. $result[19] = price2num($pu_ht_devise, 'MU');
  309. // initialize result array
  310. //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i];
  311. dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.$conf->global->MAIN_ROUNDING_RULE_TOT.' pu='.$pu.' qty='.$qty.' price_base_type='.$price_base_type.' total_ht='.$result[0].'-total_vat='.$result[1].'-total_ttc='.$result[2]);
  312. return $result;
  313. }