23if (!defined(
'SUBTOTALS_SPECIAL_CODE')) {
24 define(
'SUBTOTALS_SPECIAL_CODE', 81);
39 public static $PRODUCT_TYPE = 9;
45 public static $TITLE_OPTIONS = [
'titleshowuponpdf',
'titleshowtotalexludingvatonpdf',
'titleforcepagebreak'];
51 public static $SUBTOTAL_OPTIONS = [
'subtotalshowtotalexludingvatonpdf'];
56 public static $ALLOWED_TYPES = [
78 public function addSubtotalLine($langs, $desc, $depth, $options = array(), $parent_line = 0)
81 $this->errors[] = $langs->trans(
"TitleNeedDesc");
84 $current_module = $this->element;
86 if (!in_array($current_module, self::$ALLOWED_TYPES)) {
87 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
96 if ($depth < 0 && $current_module !=
'shipping') {
97 foreach ($this->lines as $line) {
98 if (!$next_line && $line->desc == $desc && $line->qty == -$depth) {
102 if ($next_line && $line->desc == $desc && $line->qty == $depth) {
106 if ($next_line && $line->special_code == SUBTOTALS_SPECIAL_CODE && abs($line->qty) <= abs($depth)) {
113 if ($depth > 0 && $current_module !=
'shipping') {
114 $max_existing_level = 0;
116 foreach ($this->lines as $line) {
117 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > $max_existing_level) {
118 $max_existing_level = $line->qty;
122 if ($max_existing_level+1 < $depth) {
123 $depth = $max_existing_level+1;
124 $this->errors[] = $langs->trans(
"TitleAddedLevelTooHigh", $depth);
131 if ($current_module ==
'facture' && $this instanceof
Facture) {
132 $result = $this->addline(
150 SUBTOTALS_SPECIAL_CODE
152 } elseif ($current_module ==
'propal' && $this instanceof
Propal) {
153 $result = $this->addline(
167 SUBTOTALS_SPECIAL_CODE
169 } elseif ($current_module ==
'commande' && $this instanceof
Commande) {
170 $result = $this->addline(
187 SUBTOTALS_SPECIAL_CODE
189 } elseif ($current_module ==
'shipping' && $this instanceof
Expedition) {
190 $result = $this->addline(
195 } elseif ($current_module ==
'facturerec' && $this instanceof
FactureRec) {
196 $rang = $rang == -1 ? $rang : $rang-1;
197 $result = $this->addline(
212 SUBTOTALS_SPECIAL_CODE
214 $this->fetch_lines();
215 } elseif ($current_module ==
'fichinter' && $this instanceof
Fichinter) {
217 $result = $this->addline(
226 SUBTOTALS_SPECIAL_CODE
231 if ($current_module !=
'shipping') {
232 foreach ($this->lines as $line) {
233 '@phan-var-force CommonObjectLine $line';
235 if ($line->id == $result) {
236 $line->extraparams[
"subtotal"] = $options;
237 $line->setExtraParameters();
246 return $error > 0 ? 0 : $result;
262 public function deleteSubtotalLine($langs,
$id, $correspondingstline =
false, $user =
null)
264 $current_module = $this->element;
266 if (!in_array($current_module, self::$ALLOWED_TYPES)) {
267 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
273 if ($correspondingstline) {
276 foreach ($this->lines as $line) {
277 if ($line->id ==
$id) {
278 $oldDesc = $line->desc;
279 $oldDepth = $line->qty;
281 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
282 $this->deleteSubtotalLine($langs, $line->id,
false, $user);
289 if ($current_module ==
'facture' && $this instanceof
Facture) {
291 $result = $this->deleteLine($rowid);
292 } elseif ($current_module ==
'propal' && $this instanceof
Propal) {
294 $result = $this->deleteLine($rowid);
295 } elseif ($current_module ==
'commande' && $this instanceof
Commande) {
297 $result = $this->deleteLine($user, $lineid);
298 } elseif ($current_module ==
'facturerec') {
301 $result = $line->delete($user);
302 } elseif ($current_module ==
'shipping') {
305 $result = $line->delete($user);
308 return $result >= 0 ? $result : -1;
326 public function updateSubtotalLine($langs, $lineid, $desc, $depth, $options)
328 $current_module = $this->element;
330 if (!in_array($current_module, self::$ALLOWED_TYPES)) {
331 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
338 $max_existing_level = 0;
341 foreach ($this->lines as $line) {
342 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > $max_existing_level && $line->id != $lineid) {
343 $max_existing_level = $line->qty;
348 if ($max_existing_level+1 < $depth) {
349 $depth = $max_existing_level+1;
350 $this->errors[] = $langs->trans(
"TitleEditedLevelTooHigh");
357 foreach ($this->lines as $line) {
358 if ($line->id == $lineid) {
359 $oldDesc = $line->desc;
360 $oldDepth = $line->qty;
362 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
363 $this->updateSubtotalLine($langs, $line->id, $desc, -$depth, !empty($line->extraparams[
"subtotal"]) ? $line->extraparams[
"subtotal"] : array());
370 if ($current_module ==
'facture' && $this instanceof
Facture) {
371 $result = $this->updateline(
390 SUBTOTALS_SPECIAL_CODE
392 } elseif ($current_module ==
'propal' && $this instanceof
Propal) {
393 $result = $this->updateline(
404 SUBTOTALS_SPECIAL_CODE,
412 } elseif ($current_module ==
'commande' && $this instanceof
Commande) {
413 $result = $this->updateline(
432 SUBTOTALS_SPECIAL_CODE
434 } elseif ($current_module ==
'facturerec' && $this instanceof
FactureRec) {
436 $objectline->fetch($lineid);
437 $line_rang = $objectline->rang;
438 $result = $this->updateline(
454 SUBTOTALS_SPECIAL_CODE
458 foreach ($this->lines as $line) {
459 '@phan-var-force CommonObjectLine $line';
461 if ($line->id == $lineid) {
462 $line->extraparams[
"subtotal"] = $options;
463 $line->setExtraParameters();
471 return $error > 0 ? 0 : $result;
486 public function updateSubtotalLineBlockLines($langs, $linerang, $mode, $value)
488 $current_module = $this->element;
490 if (!in_array($current_module, self::$ALLOWED_TYPES)) {
491 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
498 $nb_lines = count($this->lines)+1;
500 for ($i = $linerang+1; $i < $nb_lines; $i++) {
501 if ($this->lines[$i]->special_code == SUBTOTALS_SPECIAL_CODE) {
502 if (abs($this->lines[$i]->qty) <= (
int) $this->lines[$linerang]->qty) {
506 if ($current_module ==
'facture' && $this instanceof
Facture) {
507 $result = $this->updateline(
508 $this->lines[$i]->
id,
509 $this->lines[$i]->desc,
510 $this->lines[$i]->subprice,
511 $this->lines[$i]->qty,
512 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
513 $this->lines[$i]->date_start,
514 $this->lines[$i]->date_end,
515 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
516 $this->lines[$i]->localtax1_tx,
517 $this->lines[$i]->localtax2_tx,
519 $this->lines[$i]->info_bits,
520 $this->lines[$i]->product_type,
521 $this->lines[$i]->fk_parent_line, 0,
522 $this->lines[$i]->fk_fournprice,
523 $this->lines[$i]->pa_ht,
524 $this->lines[$i]->label,
525 $this->lines[$i]->special_code,
526 $this->lines[$i]->array_options,
527 $this->lines[$i]->situation_percent,
528 $this->lines[$i]->fk_unit,
529 $this->lines[$i]->multicurrency_subprice
531 } elseif ($current_module ==
'commande' && $this instanceof
Commande) {
532 $result = $this->updateline(
533 $this->lines[$i]->
id,
534 $this->lines[$i]->desc,
535 $this->lines[$i]->subprice,
536 $this->lines[$i]->qty,
537 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
538 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
539 $this->lines[$i]->localtax1_rate,
540 $this->lines[$i]->localtax2_rate,
542 $this->lines[$i]->info_bits,
543 $this->lines[$i]->date_start,
544 $this->lines[$i]->date_end,
545 $this->lines[$i]->product_type,
546 $this->lines[$i]->fk_parent_line, 0,
547 $this->lines[$i]->fk_fournprice,
548 $this->lines[$i]->pa_ht,
549 $this->lines[$i]->label,
550 $this->lines[$i]->special_code,
551 $this->lines[$i]->array_options,
552 $this->lines[$i]->fk_unit,
553 $this->lines[$i]->multicurrency_subprice
555 } elseif ($current_module ==
'propal' && $this instanceof
Propal) {
556 $result = $this->updateline(
557 $this->lines[$i]->
id,
558 $this->lines[$i]->subprice,
559 $this->lines[$i]->qty,
560 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
561 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
562 $this->lines[$i]->localtax1_rate,
563 $this->lines[$i]->localtax2_rate,
564 $this->lines[$i]->desc,
566 $this->lines[$i]->info_bits,
567 $this->lines[$i]->special_code,
568 $this->lines[$i]->fk_parent_line, 0,
569 $this->lines[$i]->fk_fournprice,
570 $this->lines[$i]->pa_ht,
571 $this->lines[$i]->label,
572 $this->lines[$i]->product_type,
573 $this->lines[$i]->date_start,
574 $this->lines[$i]->date_end,
575 $this->lines[$i]->array_options,
576 $this->lines[$i]->fk_unit,
577 $this->lines[$i]->multicurrency_subprice
597 public function getSubtotalLineAmount($line)
600 for ($i = $line->rang-1; $i > 0; $i--) {
601 if (is_null($this->lines[$i-1]) || $this->lines[$i-1]->rang >= $line->rang) {
604 if ($this->lines[$i-1]->special_code == SUBTOTALS_SPECIAL_CODE && $this->lines[$i-1]->qty > 0) {
605 if ($this->lines[$i-1]->qty <= abs($line->qty)) {
606 return price($final_amount);
609 $final_amount += $this->lines[$i-1]->total_ht;
612 return price($final_amount);
624 public function getSubtotalLineMulticurrencyAmount($line)
627 for ($i = $line->rang-1; $i > 0; $i--) {
628 if (is_null($this->lines[$i-1]) || $this->lines[$i-1]->rang >= $line->rang) {
631 if ($this->lines[$i-1]->special_code == SUBTOTALS_SPECIAL_CODE && $this->lines[$i-1]->qty>0) {
632 if ($this->lines[$i-1]->qty <= abs($line->qty)) {
633 return price($final_amount);
636 $final_amount += $this->lines[$i-1]->multicurrency_total_ht;
639 return price($final_amount);
648 public function getSubtotalColors($level)
660 public function getPossibleTitles()
663 foreach ($this->lines as $line) {
664 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > 0) {
665 $titles[$line->desc] = $line->desc;
667 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty < 0) {
668 unset($titles[$line->desc]);
682 public function getPossibleLevels($langs)
684 $depth_array = array();
685 $max_depth =
getDolGlobalString(
'SUBTOTAL_'.strtoupper($this->element).
'_MAX_DEPTH', 2);
686 for ($i = 0; $i < $max_depth; $i++) {
687 $depth_array[$i + 1] = $langs->trans(
"SubtotalLevel", $i + 1);
699 public function getDisabledShippmentSubtotalLines()
701 $toDisableLines = array();
706 foreach ($this->lines as $titleLine) {
707 if ($titleLine->special_code != SUBTOTALS_SPECIAL_CODE || $titleLine->qty <= 0) {
710 foreach ($this->lines as $line) {
711 if ($line->id == $titleLine->id) {
712 $oldDesc = $line->desc;
713 $oldDepth = $line->qty;
715 if ($line->special_code != SUBTOTALS_SPECIAL_CODE && $line->fk_product_type == 0 && !empty($oldDesc) && !empty($oldDepth)) {
718 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
720 $toDisableLines = array_merge($toDisableLines, array($titleLine->id, $line->id));
729 return $toDisableLines;
$id
Support class for third parties, contacts, members, users or resources.
Class to manage customers orders.
Class to manage lines of shipment.
Class to manage invoices.
Class to manage invoice lines of templates.
Class to manage invoice templates.
Class to manage proposals.
dol_html_entity_decode($a, $b, $c='UTF-8', $keepsomeentities=0)
Replace html_entity_decode functions to manage errors.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.