21if (!defined(
'SUBTOTALS_SPECIAL_CODE')) {
22 define(
'SUBTOTALS_SPECIAL_CODE', 81);
37 public static $PRODUCT_TYPE = 9;
43 public static $TITLE_OPTIONS = [
'titleshowuponpdf',
'titleshowtotalexludingvatonpdf',
'titleforcepagebreak'];
49 public static $SUBTOTAL_OPTIONS = [
'subtotalshowtotalexludingvatonpdf'];
65 public function addSubtotalLine($langs, $desc, $depth, $options = array(), $parent_line = 0)
68 if (isset($this->errors)) {
69 $this->errors[] = $langs->trans(
"TitleNeedDesc");
73 $current_module = $this->element;
75 $allowed_types = array(
'propal',
'commande',
'facture',
'facturerec',
'shipping');
76 if (!in_array($current_module, $allowed_types)) {
77 if (isset($this->errors)) {
78 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
88 if ($depth < 0 && $current_module !=
'shipping') {
89 foreach ($this->lines as $line) {
90 if (!$next_line && $line->desc == $desc && $line->qty == -$depth) {
94 if ($next_line && $line->desc == $desc && $line->qty == $depth) {
98 if ($next_line && $line->special_code == SUBTOTALS_SPECIAL_CODE && abs($line->qty) <= abs($depth)) {
105 if ($depth > 0 && $current_module !=
'shipping') {
106 $max_existing_level = 0;
108 foreach ($this->lines as $line) {
109 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > $max_existing_level) {
110 $max_existing_level = $line->qty;
114 if ($max_existing_level+1 < $depth) {
115 $depth = $max_existing_level+1;
116 if (isset($this->errors)) {
117 $this->errors[] = $langs->trans(
"TitleAddedLevelTooHigh", $depth);
125 if ($current_module ==
'facture') {
126 $result = $this->addline(
144 SUBTOTALS_SPECIAL_CODE
146 } elseif ($current_module ==
'propal') {
147 $result = $this->addline(
161 SUBTOTALS_SPECIAL_CODE
163 } elseif ($current_module ==
'commande') {
164 $result = $this->addline(
181 SUBTOTALS_SPECIAL_CODE
183 } elseif ($current_module ==
'shipping') {
184 $result = $this->addline(
189 } elseif ($current_module ==
'facturerec') {
190 $rang = $rang == -1 ? $rang : $rang-1;
191 $result = $this->addline(
206 SUBTOTALS_SPECIAL_CODE
208 $this->fetch_lines();
211 if ($current_module !=
'shipping') {
212 foreach ($this->lines as $line) {
213 '@phan-var-force CommonObjectLine $line';
214 if ($line->id == $result) {
215 $line->extraparams[
"subtotal"] = $options;
216 $line->setExtraParameters();
225 return $error > 0 ? 0 : $result;
241 public function deleteSubtotalLine($langs,
$id, $correspondingstline =
false, $user =
null)
243 $current_module = $this->element;
245 $allowed_types = array(
'propal',
'commande',
'facture',
'facturerec',
'shipping');
246 if (!in_array($current_module, $allowed_types)) {
247 if (isset($this->errors)) {
248 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
255 if ($correspondingstline) {
258 foreach ($this->lines as $line) {
259 if ($line->id ==
$id) {
260 $oldDesc = $line->desc;
261 $oldDepth = $line->qty;
263 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
264 $this->deleteSubtotalLine($langs, $line->id,
false, $user);
271 if ($current_module ==
'facture') {
272 $result = $this->deleteLine(
$id);
273 } elseif ($current_module ==
'propal') {
274 $result = $this->deleteLine(
$id);
275 } elseif ($current_module ==
'commande') {
276 $result = $this->deleteLine($user,
$id);
277 } elseif ($current_module ==
'facturerec') {
280 $result = $line->delete($user);
281 } elseif ($current_module ==
'shipping') {
284 $result = $line->delete($user);
287 return $result >= 0 ? $result : -1;
305 public function updateSubtotalLine($langs, $lineid, $desc, $depth, $options)
307 $current_module = $this->element;
309 $allowed_types = array(
'propal',
'commande',
'facture',
'facturerec',
'shipping');
310 if (!in_array($current_module, $allowed_types)) {
311 if (isset($this->errors)) {
312 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
320 $max_existing_level = 0;
323 foreach ($this->lines as $line) {
324 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > $max_existing_level && $line->id != $lineid) {
325 $max_existing_level = $line->qty;
330 if ($max_existing_level+1 < $depth) {
331 $depth = $max_existing_level+1;
332 if (isset($this->errors)) {
333 $this->errors[] = $langs->trans(
"TitleEditedLevelTooHigh");
341 foreach ($this->lines as $line) {
342 if ($line->id == $lineid) {
343 $oldDesc = $line->desc;
344 $oldDepth = $line->qty;
346 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
347 $this->updateSubtotalLine($langs, $line->id, $desc, -$depth, !empty($line->extraparams[
"subtotal"]) ? $line->extraparams[
"subtotal"] : array());
354 if ($current_module ==
'facture') {
355 $result = $this->updateline(
374 SUBTOTALS_SPECIAL_CODE
376 } elseif ($current_module ==
'propal') {
377 $result = $this->updateline(
388 SUBTOTALS_SPECIAL_CODE,
396 } elseif ($current_module ==
'commande') {
397 $result = $this->updateline(
416 SUBTOTALS_SPECIAL_CODE
418 } elseif ($current_module ==
'facturerec') {
420 $objectline->fetch($lineid);
421 $line_rang = $objectline->rang;
422 $result = $this->updateline(
438 SUBTOTALS_SPECIAL_CODE
442 foreach ($this->lines as $line) {
443 '@phan-var-force CommonObjectLine $line';
445 if ($line->id == $lineid) {
446 $line->extraparams[
"subtotal"] = $options;
447 $line->setExtraParameters();
455 return $error > 0 ? 0 : $result;
470 public function updateSubtotalLineBlockLines($langs, $linerang, $mode, $value)
472 $current_module = $this->element;
474 $allowed_types = array(
'propal',
'commande',
'facture',
'facturerec',
'shipping');
475 if (!in_array($current_module, $allowed_types)) {
476 if (isset($this->errors)) {
477 $this->errors[] = $langs->trans(
"UnsupportedModuleError");
485 $nb_lines = count($this->lines)+1;
487 for ($i = $linerang+1; $i < $nb_lines; $i++) {
488 if ($this->lines[$i]->special_code == SUBTOTALS_SPECIAL_CODE) {
489 if (abs($this->lines[$i]->qty) <= (
int) $this->lines[$linerang]->qty) {
493 if ($current_module ==
'facture') {
494 $result = $this->updateline(
495 $this->lines[$i]->
id,
496 $this->lines[$i]->desc,
497 $this->lines[$i]->subprice,
498 $this->lines[$i]->qty,
499 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
500 $this->lines[$i]->date_start,
501 $this->lines[$i]->date_end,
502 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
503 $this->lines[$i]->localtax1_tx,
504 $this->lines[$i]->localtax2_tx,
506 $this->lines[$i]->info_bits,
507 $this->lines[$i]->product_type,
508 $this->lines[$i]->fk_parent_line, 0,
509 $this->lines[$i]->fk_fournprice,
510 $this->lines[$i]->pa_ht,
511 $this->lines[$i]->label,
512 $this->lines[$i]->special_code,
513 $this->lines[$i]->array_options,
514 $this->lines[$i]->situation_percent,
515 $this->lines[$i]->fk_unit,
516 $this->lines[$i]->multicurrency_subprice
518 } elseif ($current_module ==
'commande') {
519 $result = $this->updateline(
520 $this->lines[$i]->
id,
521 $this->lines[$i]->desc,
522 $this->lines[$i]->subprice,
523 $this->lines[$i]->qty,
524 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
525 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
526 $this->lines[$i]->localtax1_rate,
527 $this->lines[$i]->localtax2_rate,
529 $this->lines[$i]->info_bits,
530 $this->lines[$i]->date_start,
531 $this->lines[$i]->date_end,
532 $this->lines[$i]->product_type,
533 $this->lines[$i]->fk_parent_line, 0,
534 $this->lines[$i]->fk_fournprice,
535 $this->lines[$i]->pa_ht,
536 $this->lines[$i]->label,
537 $this->lines[$i]->special_code,
538 $this->lines[$i]->array_options,
539 $this->lines[$i]->fk_unit,
540 $this->lines[$i]->multicurrency_subprice
542 } elseif ($current_module ==
'propal') {
543 $result = $this->updateline(
544 $this->lines[$i]->
id,
545 $this->lines[$i]->subprice,
546 $this->lines[$i]->qty,
547 $mode ==
'discount' ? $value : $this->lines[$i]->remise_percent,
548 $mode ==
'tva' ? $value : $this->lines[$i]->tva_tx,
549 $this->lines[$i]->localtax1_rate,
550 $this->lines[$i]->localtax2_rate,
551 $this->lines[$i]->desc,
553 $this->lines[$i]->info_bits,
554 $this->lines[$i]->special_code,
555 $this->lines[$i]->fk_parent_line, 0,
556 $this->lines[$i]->fk_fournprice,
557 $this->lines[$i]->pa_ht,
558 $this->lines[$i]->label,
559 $this->lines[$i]->product_type,
560 $this->lines[$i]->date_start,
561 $this->lines[$i]->date_end,
562 $this->lines[$i]->array_options,
563 $this->lines[$i]->fk_unit,
564 $this->lines[$i]->multicurrency_subprice
584 public function getSubtotalLineAmount($line)
587 for ($i = $line->rang-1; $i > 0; $i--) {
588 if (is_null($this->lines[$i-1]) || $this->lines[$i-1]->rang >= $line->rang) {
591 if ($this->lines[$i-1]->special_code == SUBTOTALS_SPECIAL_CODE && $this->lines[$i-1]->qty > 0) {
592 if ($this->lines[$i-1]->qty <= abs($line->qty)) {
593 return price($final_amount);
596 $final_amount += $this->lines[$i-1]->total_ht;
599 return price($final_amount);
611 public function getSubtotalLineMulticurrencyAmount($line)
614 for ($i = $line->rang-1; $i > 0; $i--) {
615 if (is_null($this->lines[$i-1]) || $this->lines[$i-1]->rang >= $line->rang) {
618 if ($this->lines[$i-1]->special_code == SUBTOTALS_SPECIAL_CODE && $this->lines[$i-1]->qty>0) {
619 if ($this->lines[$i-1]->qty <= abs($line->qty)) {
620 return price($final_amount);
623 $final_amount += $this->lines[$i-1]->multicurrency_total_ht;
626 return price($final_amount);
635 public function getSubtotalColors($level)
647 public function getPossibleTitles()
650 foreach ($this->lines as $line) {
651 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty > 0) {
652 $titles[$line->desc] = $line->desc;
654 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty < 0) {
655 unset($titles[$line->desc]);
669 public function getPossibleLevels($langs)
671 $depth_array = array();
672 $max_depth =
getDolGlobalString(
'SUBTOTAL_'.strtoupper($this->element).
'_MAX_DEPTH', 2);
673 for ($i = 0; $i < $max_depth; $i++) {
674 $depth_array[$i + 1] = $langs->trans(
"SubtotalLevel", $i + 1);
686 public function getDisabledShippmentSubtotalLines()
688 $toDisableLines = array();
693 foreach ($this->lines as $titleLine) {
694 if ($titleLine->special_code != SUBTOTALS_SPECIAL_CODE || $titleLine->qty <= 0) {
697 foreach ($this->lines as $line) {
698 if ($line->id == $titleLine->id) {
699 $oldDesc = $line->desc;
700 $oldDepth = $line->qty;
702 if ($line->special_code != SUBTOTALS_SPECIAL_CODE && $line->fk_product_type == 0 && !empty($oldDesc) && !empty($oldDepth)) {
705 if ($line->special_code == SUBTOTALS_SPECIAL_CODE && $line->qty == -$oldDepth && $line->desc == $oldDesc) {
707 $toDisableLines = array_merge($toDisableLines, array($titleLine->id, $line->id));
716 return $toDisableLines;
$id
Support class for third parties, contacts, members, users or resources.
Class to manage lines of shipment.
Class to manage invoice lines of templates.
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.