dolibarr 19.0.3
price.lib.php
Go to the documentation of this file.
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
86function 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 = '')
87{
88 global $conf, $mysoc, $db;
89
90 $result = array();
91
92 // Clean parameters
93 if (empty($info_bits)) {
94 $info_bits = 0;
95 }
96 if (empty($txtva)) {
97 $txtva = 0;
98 }
99 if (empty($seller) || !is_object($seller)) {
100 dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter seller that is missing", LOG_WARNING);
101 if (!is_object($mysoc)) { // mysoc may be not defined (during migration process)
102 $mysoc = new Societe($db);
103 $mysoc->setMysoc($conf);
104 }
105 $seller = $mysoc; // If sell is done to a customer, $seller is not provided, we use $mysoc
106 //var_dump($seller->country_id);exit;
107 }
108 if (empty($localtaxes_array) || !is_array($localtaxes_array)) {
109 dol_syslog("Price.lib::calcul_price_total Warning: function is called with parameter localtaxes_array that is missing or empty", LOG_WARNING);
110 }
111 if (!is_numeric($txtva)) {
112 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);
113 } elseif ($txtva >= 1000) {
114 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);
115 }
116 // Too verbose. Enable for debug only
117 //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);
118
119 $countryid = $seller->country_id;
120
121 if (is_numeric($uselocaltax1_rate)) {
122 $uselocaltax1_rate = (float) $uselocaltax1_rate;
123 }
124 if (is_numeric($uselocaltax2_rate)) {
125 $uselocaltax2_rate = (float) $uselocaltax2_rate;
126 }
127
128 if ($uselocaltax1_rate < 0) {
129 $uselocaltax1_rate = $seller->localtax1_assuj;
130 }
131 if ($uselocaltax2_rate < 0) {
132 $uselocaltax2_rate = $seller->localtax2_assuj;
133 }
134
135 //var_dump($uselocaltax1_rate.' - '.$uselocaltax2_rate);
136 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);
137
138 // Now we search localtaxes information ourself (rates and types).
139 $localtax1_type = 0;
140 $localtax2_type = 0;
141
142 if (is_array($localtaxes_array) && count($localtaxes_array)) {
143 $localtax1_type = $localtaxes_array[0];
144 $localtax1_rate = $localtaxes_array[1];
145 $localtax2_type = $localtaxes_array[2];
146 $localtax2_rate = $localtaxes_array[3];
147 } else {
148 // deprecated method. values and type for localtaxes must be provided by caller and loaded with getLocalTaxesFromRate using the full vat rate (including text code)
149 // also, with this method, we may get several possible values (for example with localtax2 in spain), so we take the first one.
150 dol_syslog("Price.lib::calcul_price_total search vat information using old deprecated method", LOG_WARNING);
151
152 $sql = "SELECT taux, localtax1, localtax2, localtax1_type, localtax2_type";
153 $sql .= " FROM ".MAIN_DB_PREFIX."c_tva as cv";
154 $sql .= " WHERE cv.taux = ".((float) $txtva);
155 $sql .= " AND cv.fk_pays = ".((int) $countryid);
156 $sql .= " AND cv.entity IN (".getEntity('c_tva').")";
157 $resql = $db->query($sql);
158 if ($resql) {
159 $obj = $db->fetch_object($resql);
160 if ($obj) {
161 $localtax1_rate = (float) $obj->localtax1; // Use float to force to get first numeric value when value is x:y:z
162 $localtax2_rate = (float) $obj->localtax2; // Use float to force to get first numeric value when value is -19:-15:-9
163 $localtax1_type = $obj->localtax1_type;
164 $localtax2_type = $obj->localtax2_type;
165 //var_dump($localtax1_rate.' '.$localtax2_rate.' '.$localtax1_type.' '.$localtax2_type);
166 }
167 } else {
168 dol_print_error($db);
169 }
170 }
171
172 // pu calculation from pu_devise if pu empty
173 if (empty($pu) && !empty($pu_devise)) {
174 if (!empty($multicurrency_tx)) {
175 $pu = $pu_devise / $multicurrency_tx;
176 } else {
177 dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (multicurrency_tx empty when pu_devise not) ', LOG_ERR);
178 return array();
179 }
180 }
181 if ($pu === '') {
182 $pu = 0;
183 }
184 // pu_devise calculation from pu
185 if (empty($pu_devise) && !empty($multicurrency_tx)) {
186 if (is_numeric($pu) && is_numeric($multicurrency_tx)) {
187 $pu_devise = $pu * $multicurrency_tx;
188 } else {
189 dol_syslog('Price.lib::calcul_price_total function called with bad parameters combination (pu or multicurrency_tx are not numeric)', LOG_ERR);
190 return array();
191 }
192 }
193
194 // initialize total (may be HT or TTC depending on price_base_type)
195 $tot_sans_remise = $pu * $qty * $progress / 100;
196 $tot_avec_remise_ligne = $tot_sans_remise * (1 - ((float) $remise_percent_ligne / 100));
197 $tot_avec_remise = $tot_avec_remise_ligne * (1 - ((float) $remise_percent_global / 100));
198
199 // initialize result array
200 for ($i = 0; $i <= 15; $i++) {
201 $result[$i] = 0;
202 }
203
204 // if there's some localtax including vat, we calculate localtaxes (we will add later)
205
206 // if input unit price is 'HT', we need to have the totals with main VAT for a correct calculation
207 if ($price_base_type != 'TTC') {
208 $tot_sans_remise_wt = price2num($tot_sans_remise * (1 + ($txtva / 100)), 'MU');
209 $tot_avec_remise_wt = price2num($tot_avec_remise * (1 + ($txtva / 100)), 'MU');
210 $pu_wt = price2num($pu * (1 + ($txtva / 100)), 'MU');
211 } else {
212 $tot_sans_remise_wt = $tot_sans_remise;
213 $tot_avec_remise_wt = $tot_avec_remise;
214 $pu_wt = $pu;
215 }
216
217 //print 'rr'.$price_base_type.'-'.$txtva.'-'.$tot_sans_remise_wt."-".$pu_wt."-".$uselocaltax1_rate."-".$localtax1_rate."-".$localtax1_type."\n";
218
219 $localtaxes = array(0, 0, 0);
220 $apply_tax = false;
221 switch ($localtax1_type) {
222 case '2': // localtax on product or service
223 $apply_tax = true;
224 break;
225 case '4': // localtax on product
226 if ($type == 0) {
227 $apply_tax = true;
228 }
229 break;
230 case '6': // localtax on service
231 if ($type == 1) {
232 $apply_tax = true;
233 }
234 break;
235 }
236
237 if ($uselocaltax1_rate && $apply_tax) {
238 $result[14] = price2num(($tot_sans_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_sans_remise_wt, 'MT');
239 $localtaxes[0] += $result[14];
240
241 $result[9] = price2num(($tot_avec_remise_wt * (1 + ($localtax1_rate / 100))) - $tot_avec_remise_wt, 'MT');
242 $localtaxes[1] += $result[9];
243
244 $result[11] = price2num(($pu_wt * (1 + ($localtax1_rate / 100))) - $pu_wt, 'MU');
245 $localtaxes[2] += $result[11];
246 }
247
248 $apply_tax = false;
249 switch ($localtax2_type) {
250 case '2': // localtax on product or service
251 $apply_tax = true;
252 break;
253 case '4': // localtax on product
254 if ($type == 0) {
255 $apply_tax = true;
256 }
257 break;
258 case '6': // localtax on service
259 if ($type == 1) {
260 $apply_tax = true;
261 }
262 break;
263 }
264 if ($uselocaltax2_rate && $apply_tax) {
265 $result[15] = price2num(($tot_sans_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_sans_remise_wt, 'MT');
266 $localtaxes[0] += $result[15];
267
268 $result[10] = price2num(($tot_avec_remise_wt * (1 + ($localtax2_rate / 100))) - $tot_avec_remise_wt, 'MT');
269 $localtaxes[1] += $result[10];
270
271 $result[12] = price2num(($pu_wt * (1 + ($localtax2_rate / 100))) - $pu_wt, 'MU');
272 $localtaxes[2] += $result[12];
273 }
274
275 //dol_syslog("price.lib::calcul_price_total $qty, $pu, $remise_percent_ligne, $txtva, $price_base_type $info_bits");
276 if ($price_base_type == 'HT') {
277 // We work to define prices using the price without tax
278 $result[6] = price2num($tot_sans_remise, 'MT');
279 $result[8] = price2num($tot_sans_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[0], 'MT'); // Selon TVA NPR ou non
280 $result8bis = price2num($tot_sans_remise * (1 + ($txtva / 100)) + $localtaxes[0], 'MT'); // Si TVA consideree normale (non NPR)
281 $result[7] = price2num($result8bis - ($result[6] + $localtaxes[0]), 'MT');
282
283 $result[0] = price2num($tot_avec_remise, 'MT');
284 $result[2] = price2num($tot_avec_remise * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[1], 'MT'); // Selon TVA NPR ou non
285 $result2bis = price2num($tot_avec_remise * (1 + ($txtva / 100)) + $localtaxes[1], 'MT'); // Si TVA consideree normale (non NPR)
286 $result[1] = price2num($result2bis - ($result[0] + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
287
288 $result[3] = price2num($pu, 'MU');
289 $result[5] = price2num($pu * (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)) + $localtaxes[2], 'MU'); // Selon TVA NPR ou non
290 $result5bis = price2num($pu * (1 + ($txtva / 100)) + $localtaxes[2], 'MU'); // Si TVA consideree normale (non NPR)
291 $result[4] = price2num($result5bis - ($result[3] + $localtaxes[2]), 'MU');
292 } else {
293 // We work to define prices using the price with tax
294 $result[8] = price2num($tot_sans_remise + $localtaxes[0], 'MT');
295 $result[6] = price2num($tot_sans_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MT'); // Selon TVA NPR ou non
296 $result6bis = price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
297 $result[7] = price2num($result[8] - ($result6bis + $localtaxes[0]), 'MT');
298
299 $result[2] = price2num($tot_avec_remise + $localtaxes[1], 'MT');
300 $result[0] = price2num($tot_avec_remise / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MT'); // Selon TVA NPR ou non
301 $result0bis = price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MT'); // Si TVA consideree normale (non NPR)
302 $result[1] = price2num($result[2] - ($result0bis + $localtaxes[1]), 'MT'); // Total VAT = TTC - (HT + localtax)
303
304 $result[5] = price2num($pu + $localtaxes[2], 'MU');
305 $result[3] = price2num($pu / (1 + ((($info_bits & 1) ? 0 : $txtva) / 100)), 'MU'); // Selon TVA NPR ou non
306 $result3bis = price2num($pu / (1 + ($txtva / 100)), 'MU'); // Si TVA consideree normale (non NPR)
307 $result[4] = price2num($result[5] - ($result3bis + $localtaxes[2]), 'MU');
308 }
309
310 // if there's some localtax without vat, we calculate localtaxes (we will add them at end)
311
312 //If input unit price is 'TTC', we need to have the totals without main VAT for a correct calculation
313 if ($price_base_type == 'TTC') {
314 $tot_sans_remise = price2num($tot_sans_remise / (1 + ($txtva / 100)), 'MU');
315 $tot_avec_remise = price2num($tot_avec_remise / (1 + ($txtva / 100)), 'MU');
316 $pu = price2num($pu / (1 + ($txtva / 100)), 'MU');
317 }
318
319 $apply_tax = false;
320 switch ($localtax1_type) {
321 case '1': // localtax on product or service
322 $apply_tax = true;
323 break;
324 case '3': // localtax on product
325 if ($type == 0) {
326 $apply_tax = true;
327 }
328 break;
329 case '5': // localtax on service
330 if ($type == 1) {
331 $apply_tax = true;
332 }
333 break;
334 }
335 if ($uselocaltax1_rate && $apply_tax) {
336 $result[14] = price2num(($tot_sans_remise * (1 + ($localtax1_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax1 for total_ht_without_discount
337 $result[8] += $result[14]; // total_ttc_without_discount + tax1
338
339 $result[9] = price2num(($tot_avec_remise * (1 + ($localtax1_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax1 for total_ht
340 $result[2] += $result[9]; // total_ttc + tax1
341
342 $result[11] = price2num(($pu * (1 + ($localtax1_rate / 100))) - $pu, 'MU'); // amount tax1 for pu_ht
343 $result[5] += $result[11]; // pu_ht + tax1
344 }
345
346 $apply_tax = false;
347 switch ($localtax2_type) {
348 case '1': // localtax on product or service
349 $apply_tax = true;
350 break;
351 case '3': // localtax on product
352 if ($type == 0) {
353 $apply_tax = true;
354 }
355 break;
356 case '5': // localtax on service
357 if ($type == 1) {
358 $apply_tax = true;
359 }
360 break;
361 }
362 if ($uselocaltax2_rate && $apply_tax) {
363 $result[15] = price2num(($tot_sans_remise * (1 + ($localtax2_rate / 100))) - $tot_sans_remise, 'MT'); // amount tax2 for total_ht_without_discount
364 $result[8] += $result[15]; // total_ttc_without_discount + tax2
365
366 $result[10] = price2num(($tot_avec_remise * (1 + ($localtax2_rate / 100))) - $tot_avec_remise, 'MT'); // amount tax2 for total_ht
367 $result[2] += $result[10]; // total_ttc + tax2
368
369 $result[12] = price2num(($pu * (1 + ($localtax2_rate / 100))) - $pu, 'MU'); // amount tax2 for pu_ht
370 $result[5] += $result[12]; // pu_ht + tax2
371 }
372
373 // If rounding is not using base 10 (rare)
374 if (getDolGlobalString('MAIN_ROUNDING_RULE_TOT')) {
375 if ($price_base_type == 'HT') {
376 $result[0] = price2num(round($result[0] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
377 $result[1] = price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
378 $result[9] = price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
379 $result[10] = price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
380 $result[2] = price2num($result[0] + $result[1] + $result[9] + $result[10], 'MT');
381 } else {
382 $result[1] = price2num(round($result[1] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
383 $result[2] = price2num(round($result[2] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
384 $result[9] = price2num(round($result[9] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
385 $result[10] = price2num(round($result[10] / $conf->global->MAIN_ROUNDING_RULE_TOT, 0) * $conf->global->MAIN_ROUNDING_RULE_TOT, 'MT');
386 $result[0] = price2num($result[2] - $result[1] - $result[9] - $result[10], 'MT');
387 }
388 }
389
390 // Multicurrency
391 if ($multicurrency_tx != 1) {
392 if ($multicurrency_code) {
393 $savMAIN_MAX_DECIMALS_UNIT = $conf->global->MAIN_MAX_DECIMALS_UNIT;
394 $savMAIN_MAX_DECIMALS_TOT = $conf->global->MAIN_MAX_DECIMALS_TOT;
395 $savMAIN_ROUNDING_RULE_TOT = $conf->global->MAIN_ROUNDING_RULE_TOT;
396
397 // Set parameter for currency accurency according to the value of $multicurrency_code (this is because a foreign currency may have different rounding rules)
398 $keyforforeignMAIN_MAX_DECIMALS_UNIT = 'MAIN_MAX_DECIMALS_UNIT_'.$multicurrency_code;
399 $keyforforeignMAIN_MAX_DECIMALS_TOT = 'MAIN_MAX_DECIMALS_TOT_'.$multicurrency_code;
400 $keyforforeignMAIN_ROUNDING_RULE_TOT = 'MAIN_ROUNDING_RULE_TOT_'.$multicurrency_code;
401 if (!empty($conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT)) {
402 $conf->global->MAIN_MAX_DECIMALS_UNIT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_UNIT;
403 $conf->global->MAIN_MAX_DECIMALS_TOT = $conf->global->$keyforforeignMAIN_MAX_DECIMALS_TOT;
404 $conf->global->MAIN_ROUNDING_RULE_TOT = $conf->global->$keyforforeignMAIN_ROUNDING_RULE_TOT;
405 }
406 }
407
408 // Recall function using the multicurrency price as reference price. We must set param $multicurrency_tx to 1 to avoid infinite loop.
409 $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, '');
410
411 if ($multicurrency_code) {
412 // Restore setup of currency accurency
413 $conf->global->MAIN_MAX_DECIMALS_UNIT = $savMAIN_MAX_DECIMALS_UNIT;
414 $conf->global->MAIN_MAX_DECIMALS_TOT = $savMAIN_MAX_DECIMALS_TOT;
415 $conf->global->MAIN_ROUNDING_RULE_TOT = $savMAIN_ROUNDING_RULE_TOT;
416 }
417
418 $result[16] = $newresult[0];
419 $result[17] = $newresult[1];
420 $result[18] = $newresult[2];
421 $result[19] = $newresult[3];
422 $result[20] = $newresult[4];
423 $result[21] = $newresult[5];
424 $result[22] = $newresult[6];
425 $result[23] = $newresult[7];
426 $result[24] = $newresult[8];
427 $result[25] = $newresult[9];
428 $result[26] = $newresult[10];
429 } else {
430 $result[16] = $result[0];
431 $result[17] = $result[1];
432 $result[18] = $result[2];
433 $result[19] = $result[3];
434 $result[20] = $result[4];
435 $result[21] = $result[5];
436 $result[22] = $result[6];
437 $result[23] = $result[7];
438 $result[24] = $result[8];
439 $result[25] = $result[9];
440 $result[26] = $result[10];
441 }
442
443 //var_dump($result);
444 // initialize result array
445 //for ($i=0; $i <= 18; $i++) $result[$i] = (float) $result[$i];
446
447 dol_syslog('Price.lib::calcul_price_total MAIN_ROUNDING_RULE_TOT='.(!getDolGlobalString('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]);
448
449 return $result;
450}
Class to manage third parties objects (customers, suppliers, prospects...)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
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='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:86