price.lib.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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 <https://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 depending on price_base_type. TODO Add also mode 'INCT' when pu is price HT+VAT+LT1+LT2)
  38. * @param float $remise_percent_ligne Discount for line
  39. * @param float $txtva 0=do not apply VAT tax, VAT rate=apply (this is VAT rate only without text code, we don't need text code because we alreaydy have all tax info into $localtaxes_array)
  40. * @param float $uselocaltax1_rate 0=do not use localtax1, >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 localtax2, >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 $pu is HT, 'TTC'=Unit price parameter $pu is TTC (HT+VAT but not Localtax. TODO Add also mode 'INCT' when pu is price HT+VAT+LT1+LT2)
  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_devise Amount in currency
  51. * @param string $multicurrency_code Value of the foreign currency if multicurrency is used ('EUR', 'USD', ...). It will be used for rounding according to currency.
  52. * @return array [
  53. * 0=total_ht,
  54. * 1=total_vat, (main vat only)
  55. * 2=total_ttc, (total_ht + main vat + local taxes)
  56. * 3=pu_ht,
  57. * 4=pu_vat, (main vat only)
  58. * 5=pu_ttc,
  59. * 6=total_ht_without_discount,
  60. * 7=total_vat_without_discount, (main vat only)
  61. * 8=total_ttc_without_discount, (total_ht + main vat + local taxes)
  62. * 9=total_tax1 for total_ht,
  63. * 10=total_tax2 for total_ht,
  64. *
  65. * 11=pu_tax1 for pu_ht, !! should not be used
  66. * 12=pu_tax2 for pu_ht, !! should not be used
  67. * 13=?? !! should not be used
  68. * 14=total_tax1 for total_ht_without_discount, !! should not be used
  69. * 15=total_tax2 for total_ht_without_discount, !! should not be used
  70. *
  71. * 16=multicurrency_total_ht
  72. * 17=multicurrency_total_tva
  73. * 18=multicurrency_total_ttc
  74. * 19=multicurrency_pu_ht
  75. * 20=multicurrency_pu_vat
  76. * 21=multicurrency_pu_ttc
  77. * 22=multicurrency_total_ht_without_discount
  78. * 23=multicurrency_total_vat_without_discount
  79. * 24=multicurrency_total_ttc_without_discount
  80. * 25=multicurrency_total_tax1 for total_ht
  81. * 26=multicurrency_total_tax2 for total_ht
  82. */
  83. 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_devise = 0, $multicurrency_code = '')
  84. {
  85. global $conf, $mysoc, $db;
  86. $result = array();
  87. // Clean parameters
  88. if (empty($info_bits)) {
  89. $info_bits = 0;
  90. }
  91. if (empty($txtva)) {
  92. $txtva = 0;
  93. }
  94. if (empty($seller) || !is_object($seller)) {
  95. dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
  96. if (!is_object($mysoc)) { // mysoc may be not defined (during migration process)
  97. $mysoc = new Societe($db);
  98. $mysoc->setMysoc($conf);
  99. }
  100. $seller = $mysoc; // If sell is done to a customer, $seller is not provided, we use $mysoc
  101. //var_dump($seller->country_id);exit;
  102. }
  103. if (empty($localtaxes_array) || !is_array($localtaxes_array)) {
  104. dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing or empty", LOG_WARNING);
  105. }
  106. if (!is_numeric($txtva)) {
  107. dol_syslog("Price.lib::calcul_price_total Warning: function was called with a parameter vat rate that is not a real numeric value. There is surely a bug.", LOG_ERR);
  108. } elseif ($txtva >= 1000) {
  109. dol_syslog("Price.lib::calcul_price_total Warning: function was called with a bad value for vat rate (should be often < 100, always < 1000). There is surely a bug.", LOG_ERR);
  110. }
  111. // Too verbose. Enable for debug only
  112. //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);
  113. $countryid = $seller->country_id;
  114. if (is_numeric($uselocaltax1_rate)) {
  115. $uselocaltax1_rate = (float) $uselocaltax1_rate;
  116. }
  117. if (is_numeric($uselocaltax2_rate)) {
  118. $uselocaltax2_rate = (float) $uselocaltax2_rate;
  119. }
  120. if ($uselocaltax1_rate < 0) {
  121. $uselocaltax1_rate = $seller->localtax1_assuj;
  122. }
  123. if ($uselocaltax2_rate < 0) {
  124. $uselocaltax2_rate = $seller->localtax2_assuj;
  125. }
  126. //var_dump($uselocaltax1_rate.' - '.$uselocaltax2_rate);
  127. 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);
  128. // Now we search localtaxes information ourself (rates and types).
  129. $localtax1_type = 0;
  130. $localtax2_type = 0;
  131. if (is_array($localtaxes_array) && count($localtaxes_array)) {
  132. $localtax1_type = $localtaxes_array[0];
  133. $localtax1_rate = $localtaxes_array[1];
  134. $localtax2_type = $localtaxes_array[2];
  135. $localtax2_rate = $localtaxes_array[3];
  136. } else {
  137. // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate using the full vat rate (including text code)
  138. // also, with this method, we may get several possible values (for example with localtax2 in spain), so we take the first one.
  139. dol_syslog("Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
  140. $sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
  141. $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as cv";
  142. $sql .= " WHERE cv.taux = ".((float) $txtva);
  143. $sql .= " AND cv.fk_pays = ".((int) $countryid);
  144. $resql = $db->query($sql);
  145. if ($resql) {
  146. $obj = $db->fetch_object($resql);
  147. if ($obj) {
  148. $localtax1_rate = (float) $obj->localtax1; // Use float to force to get first numeric value when value is x:y:z
  149. $localtax2_rate = (float) $obj->localtax2; // Use float to force to get first numeric value when value is -19:-15:-9
  150. $localtax1_type = $obj->localtax1_type;
  151. $localtax2_type = $obj->localtax2_type;
  152. //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);
  153. }
  154. } else {
  155. dol_print_error($db);
  156. }
  157. }
  158. // pu calculation from pu_devise if pu empty
  159. if (empty($pu) && !empty($pu_devise)) {
  160. if (!empty($multicurrency_tx)) {
  161. $pu = $pu_devise / $multicurrency_tx;
  162. } else {
  163. dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
  164. return array();
  165. }
  166. }
  167. if ($pu === '') {
  168. $pu = 0;
  169. }
  170. // pu_devise calculation from pu
  171. if (empty($pu_devise) && !empty($multicurrency_tx)) {
  172. if (is_numeric($pu) && is_numeric($multicurrency_tx)) {
  173. $pu_devise = $pu * $multicurrency_tx;
  174. } else {
  175. dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
  176. return array();
  177. }
  178. }
  179. // initialize total (may be HT or TTC depending on price_base_type)
  180. $tot_sans_remise = $pu * $qty * $progress / 100;
  181. $tot_avec_remise_ligne = $tot_sans_remise * (1 - ((float) $remise_percent_ligne / 100));
  182. $tot_avec_remise = $tot_avec_remise_ligne * (1 - ((float) $remise_percent_global / 100));
  183. // initialize result array
  184. for ($i = 0; $i <= 15; $i++) {
  185. $result[$i] = 0;
  186. }
  187. // if there's some localtax including vat, we calculate localtaxes (we will add later)
  188. // if input unit price is 'HT', we need to have the totals with main VAT for a correct calculation
  189. if ($price_base_type != 'TTC') {
  190. $tot_sans_remise_wt = price2num($tot_sans_remise * (1 + ($txtva / 100)), 'MU');
  191. $tot_avec_remise_wt = price2num($tot_avec_remise * (1 + ($txtva / 100)), 'MU');
  192. $pu_wt = price2num($pu * (1 + ($txtva / 100)), 'MU');
  193. } else {
  194. $tot_sans_remise_wt = $tot_sans_remise;
  195. $tot_avec_remise_wt = $tot_avec_remise;
  196. $pu_wt = $pu;
  197. }
  198. //print 'rr'.$price_base_type.'-'.$txtva.'-'.$tot_sans_remise_wt."-".$pu_wt."-".$uselocaltax1_rate."-".$localtax1_rate."-".$localtax1_type."\n";
  199. $localtaxes = array(0, 0, 0);
  200. $apply_tax = false;
  201. switch ($localtax1_type) {
  202. case '2': // localtax on product or service
  203. $apply_tax = true;
  204. break;
  205. case '4': // localtax on product
  206. if ($type == 0) {
  207. $apply_tax = true;
  208. }
  209. break;
  210. case '6': // localtax on service
  211. if ($type == 1) {
  212. $apply_tax = true;
  213. }
  214. break;
  215. }
  216. if ($uselocaltax1_rate && $apply_tax) {
  217. $result[14] = price2num(($tot_sans_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_sans_remise_wt, 'MT');
  218. $localtaxes[0] += $result[14];
  219. $result[9] = price2num(($tot_avec_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_avec_remise_wt, 'MT');
  220. $localtaxes[1] += $result[9];
  221. $result[11] = price2num(($pu_wt * (1 + ($localtax1_rate / 100))) - $pu_wt, 'MU');
  222. $localtaxes[2] += $result[11];
  223. }
  224. $apply_tax = false;
  225. switch ($localtax2_type) {
  226. case '2': // localtax on product or service
  227. $apply_tax = true;
  228. break;
  229. case '4': // localtax on product
  230. if ($type == 0) {
  231. $apply_tax = true;
  232. }
  233. break;
  234. case '6': // localtax on service
  235. if ($type == 1) {
  236. $apply_tax = true;
  237. }
  238. break;
  239. }
  240. if ($uselocaltax2_rate && $apply_tax) {
  241. $result[15] = price2num(($tot_sans_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_sans_remise_wt, 'MT');
  242. $localtaxes[0] += $result[15];
  243. $result[10] = price2num(($tot_avec_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_avec_remise_wt, 'MT');
  244. $localtaxes[1] += $result[10];
  245. $result[12] = price2num(($pu_wt * (1 + ($localtax2_rate / 100))) - $pu_wt, 'MU');
  246. $localtaxes[2] += $result[12];
  247. }
  248. //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits");
  249. if ($price_base_type == 'HT') {
  250. // We work to define prices using the price without tax
  251. $result[6] = price2num($tot_sans_remise, 'MT');
  252. $result[8] = price2num($tot_sans_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[0], 'MT'); // Selon TVA NPR ou non
  253. $result8bis = price2num($tot_sans_remise * (1 + ($txtva / 100)) + $localtaxes[0], 'MT'); // Si TVA consideree normale (non NPR)
  254. $result[7] = price2num($result8bis - ($result[6] + $localtaxes[0]), 'MT');
  255. $result[0] = price2num($tot_avec_remise, 'MT');
  256. $result[2] = price2num($tot_avec_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[1], 'MT'); // Selon TVA NPR ou non
  257. $result2bis = price2num($tot_avec_remise * (1 + ($txtva / 100)) + $localtaxes[1], 'MT'); // Si TVA consideree normale (non NPR)
  258. $result[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
  259. $result[3] = price2num($pu, 'MU');
  260. $result[5] = price2num($pu * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[2], 'MU'); // Selon TVA NPR ou non
  261. $result5bis = price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2], 'MU'); // Si TVA consideree normale (non NPR)
  262. $result[4] = price2num($result5bis - ($result[3] + $localtaxes[2]), 'MU');
  263. } else {
  264. // We work to define prices using the price with tax
  265. $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT');
  266. $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  267. $result6bis = price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  268. $result[7] = price2num($result[8] - ($result6bis + $localtaxes[0]), 'MT');
  269. $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT');
  270. $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MT'); // Selon TVA NPR ou non
  271. $result0bis = price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
  272. $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
  273. $result[5] = price2num($pu + $localtaxes[2], 'MU');
  274. $result[3] = price2num($pu / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MU'); // Selon TVA NPR ou non
  275. $result3bis = price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
  276. $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU');
  277. }
  278. // if there's some localtax without vat, we calculate localtaxes (we will add them at end)
  279. //If input unit price is 'TTC', we need to have the totals without main VAT for a correct calculation
  280. if ($price_base_type == 'TTC') {
  281. $tot_sans_remise = price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MU');
  282. $tot_avec_remise = price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MU');
  283. $pu = price2num($pu / (1 + ($txtva / 100)), 'MU');
  284. }
  285. $apply_tax = false;
  286. switch ($localtax1_type) {
  287. case '1': // localtax on product or service
  288. $apply_tax = true;
  289. break;
  290. case '3': // localtax on product
  291. if ($type == 0) {
  292. $apply_tax = true;
  293. }
  294. break;
  295. case '5': // localtax on service
  296. if ($type == 1) {
  297. $apply_tax = true;
  298. }
  299. break;
  300. }
  301. if ($uselocaltax1_rate && $apply_tax) {
  302. $result[14] = price2num(($tot_sans_remise * (1 + ($localtax1_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax1 for total_ht_without_discount
  303. $result[8] += $result[14]; // total_ttc_without_discount + tax1
  304. $result[9] = price2num(($tot_avec_remise * (1 + ($localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht
  305. $result[2] += $result[9]; // total_ttc + tax1
  306. $result[11] = price2num(($pu * (1 + ($localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht
  307. $result[5] += $result[11]; // pu_ht + tax1
  308. }
  309. $apply_tax = false;
  310. switch ($localtax2_type) {
  311. case '1': // localtax on product or service
  312. $apply_tax = true;
  313. break;
  314. case '3': // localtax on product
  315. if ($type == 0) {
  316. $apply_tax = true;
  317. }
  318. break;
  319. case '5': // localtax on service
  320. if ($type == 1) {
  321. $apply_tax = true;
  322. }
  323. break;
  324. }
  325. if ($uselocaltax2_rate && $apply_tax) {
  326. $result[15] = price2num(($tot_sans_remise * (1 + ($localtax2_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax2 for total_ht_without_discount
  327. $result[8] += $result[15]; // total_ttc_without_discount + tax2
  328. $result[10] = price2num(($tot_avec_remise * (1 + ($localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht
  329. $result[2] += $result[10]; // total_ttc + tax2
  330. $result[12] = price2num(($pu * (1 + ($localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht
  331. $result[5] += $result[12]; // pu_ht + tax2
  332. }
  333. // If rounding is not using base 10 (rare)
  334. if (!empty($conf->global->MAIN_ROUNDING_RULE_TOT)) {
  335. if ($price_base_type == 'HT') {
  336. $result[0] = price2num(round($result[0] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  337. $result[1] = price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  338. $result[9] = price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  339. $result[10] = price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  340. $result[2] = price2num($result[0] + $result[1] + $result[9] + $result[10], 'MT');
  341. } else {
  342. $result[1] = price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  343. $result[2] = price2num(round($result[2] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  344. $result[9] = price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  345. $result[10] = price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
  346. $result[0] = price2num($result[2] - $result[1] - $result[9] - $result[10], 'MT');
  347. }
  348. }
  349. // Multicurrency
  350. if ($multicurrency_tx != 1) {
  351. if ($multicurrency_code) {
  352. $savMAIN_MAX_DECIMALS_UNIT = $conf->global->MAIN_MAX_DECIMALS_UNIT;
  353. $savMAIN_MAX_DECIMALS_TOT = $conf->global->MAIN_MAX_DECIMALS_TOT;
  354. $savMAIN_ROUNDING_RULE_TOT = $conf->global->MAIN_ROUNDING_RULE_TOT;
  355. // Set parameter for currency accurency according to the value of $multicurrency_code (this is because a foreign currency may have different rounding rules)
  356. $keyforforeignMAIN_MAX_DECIMALS_UNIT = 'MAIN_MAX_DECIMALS_UNIT_'.$multicurrency_code;
  357. $keyforforeignMAIN_MAX_DECIMALS_TOT = 'MAIN_MAX_DECIMALS_TOT_'.$multicurrency_code;
  358. $keyforforeignMAIN_ROUNDING_RULE_TOT = 'MAIN_ROUNDING_RULE_TOT_'.$multicurrency_code;
  359. if (!empty($conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT)) {
  360. $conf->global->MAIN_MAX_DECIMALS_UNIT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_UNIT;
  361. $conf->global->MAIN_MAX_DECIMALS_TOT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_TOT;
  362. $conf->global->MAIN_ROUNDING_RULE_TOT = $conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT;
  363. }
  364. }
  365. // Recall function using the multicurrency price as reference price. We must set param $multicurrency_tx to 1 to avoid infinite loop.
  366. $newresult = calcul_price_total($qty, $pu_devise, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller, $localtaxes_array, $progress, 1, 0, '');
  367. if ($multicurrency_code) {
  368. // Restore setup of currency accurency
  369. $conf->global->MAIN_MAX_DECIMALS_UNIT = $savMAIN_MAX_DECIMALS_UNIT;
  370. $conf->global->MAIN_MAX_DECIMALS_TOT = $savMAIN_MAX_DECIMALS_TOT;
  371. $conf->global->MAIN_ROUNDING_RULE_TOT = $savMAIN_ROUNDING_RULE_TOT;
  372. }
  373. $result[16] = $result[0];
  374. $result[17] = $result[1];
  375. $result[18] = $result[2];
  376. $result[19] = $result[3];
  377. $result[20] = $newresult[4];
  378. $result[21] = $newresult[5];
  379. $result[22] = $newresult[6];
  380. $result[23] = $newresult[7];
  381. $result[24] = $newresult[8];
  382. $result[25] = $newresult[9];
  383. $result[26] = $newresult[10];
  384. } else {
  385. $result[16] = $result[0];
  386. $result[17] = $result[1];
  387. $result[18] = $result[2];
  388. $result[19] = $result[3];
  389. $result[20] = $result[4];
  390. $result[21] = $result[5];
  391. $result[22] = $result[6];
  392. $result[23] = $result[7];
  393. $result[24] = $result[8];
  394. $result[25] = $result[9];
  395. $result[26] = $result[10];
  396. }
  397. //var_dump($result);
  398. // initialize result array
  399. //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i];
  400. dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.(empty($conf->global->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]);
  401. return $result;
  402. }