doc_getlinedesc()   F
last analyzed

Complexity

Conditions 48
Paths > 20000

Size

Total Lines 114
Code Lines 72

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 48
eloc 72
nc 5529600
nop 5
dl 0
loc 114
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2006-2011  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2006		Rodolphe Quiedeville	<[email protected]>
5
 * Copyright (C) 2007		Patrick Raguin			<[email protected]>
6
 * Copyright (C) 2010-2012	Regis Houssin			<[email protected]>
7
 * Copyright (C) 2010		Juanjo Menent			<[email protected]>
8
 * Copyright (C) 2012		Christophe Battarel		<[email protected]>
9
 * Copyright (C) 2024       Rafael San José             <[email protected]>
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23
 * or see https://www.gnu.org/
24
 */
25
26
use Dolibarr\Lib\ViewMain;
27
28
/**
29
 *  \file       htdocs/core/lib/doc.lib.php
30
 *  \brief      Set of functions used for ODT generation
31
 *  \ingroup    core
32
 */
33
34
35
/**
36
 *  Return line description translated in outputlangs and encoded into UTF8
37
 *
38
 * @param Object $line Object of line
39
 * @param Translate $outputlangs Object langs for output
40
 * @param int $hideref Hide reference
41
 * @param int $hidedesc Hide description
42
 * @param int $issupplierline Is it a line for a supplier object ?
43
 * @return string                           String with line
44
 */
45
function doc_getlinedesc($line, $outputlangs, $hideref = 0, $hidedesc = 0, $issupplierline = 0)
46
{
47
    global $db, $conf, $langs;
48
49
    $idprod = $line->fk_product;
50
    $label = (!empty($line->label) ? $line->label : (!empty($line->libelle) ? $line->libelle : ''));
51
    $desc = (!empty($line->desc) ? $line->desc : (!empty($line->description) ? $line->description : ''));
52
    $ref_supplier = (!empty($line->ref_supplier) ? $line->ref_supplier : (!empty($line->ref_fourn) ? $line->ref_fourn : '')); // TODO Not yet saved for supplier invoices, only supplier orders
53
    $note = (!empty($line->note) ? $line->note : '');
54
55
    if ($issupplierline) {
56
        $prodser = new ProductFournisseur($db);
57
    } else {
58
        $prodser = new Product($db);
59
    }
60
61
    if ($idprod) {
62
        $prodser->fetch($idprod);
63
        // If a predefined product and multilang and on other lang, we renamed label with label translated
64
        if (getDolGlobalInt('MAIN_MULTILANGS') && ($outputlangs->defaultlang != $langs->defaultlang)) {
65
            if (!empty($prodser->multilangs[$outputlangs->defaultlang]["label"]) && $label == $prodser->label) {
66
                $label = $prodser->multilangs[$outputlangs->defaultlang]["label"];
67
            }
68
            if (!empty($prodser->multilangs[$outputlangs->defaultlang]["description"]) && $desc == $prodser->description) {
69
                $desc = $prodser->multilangs[$outputlangs->defaultlang]["description"];
70
            }
71
            if (!empty($prodser->multilangs[$outputlangs->defaultlang]["note"]) && $note == $prodser->note) {
72
                $note = $prodser->multilangs[$outputlangs->defaultlang]["note"];
73
            }
74
        }
75
    }
76
77
    // Description short of product line
78
    $libelleproduitservice = $label;
79
80
    // Description long of product line
81
    if ($desc && ($desc != $label)) {
82
        if ($desc == '(CREDIT_NOTE)' && $line->fk_remise_except) {
83
            $discount = new DiscountAbsolute($db);
84
            $discount->fetch($line->fk_remise_except);
85
            $sourceref = !empty($discount->discount_type) ? $discount->ref_invoice_supplier_source : $discount->ref_facture_source;
86
            $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromCreditNote", $sourceref);
87
        } elseif ($desc == '(DEPOSIT)' && $line->fk_remise_except) {
88
            $discount = new DiscountAbsolute($db);
89
            $discount->fetch($line->fk_remise_except);
90
            $sourceref = !empty($discount->discount_type) ? $discount->ref_invoice_supplier_source : $discount->ref_facture_source;
91
            $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromDeposit", $sourceref);
92
            // Add date of deposit
93
            if (getDolGlobalString('INVOICE_ADD_DEPOSIT_DATE')) {
94
                $libelleproduitservice .= ' (' . dol_print_date($discount->datec, 'day', '', $outputlangs) . ')';
95
            }
96
        } elseif ($desc == '(EXCESS RECEIVED)' && $line->fk_remise_except) {
97
            $discount = new DiscountAbsolute($db);
98
            $discount->fetch($line->fk_remise_except);
99
            $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessReceived", $discount->ref_facture_source);
100
        } elseif ($desc == '(EXCESS PAID)' && $line->fk_remise_except) {
101
            $discount = new DiscountAbsolute($db);
102
            $discount->fetch($line->fk_remise_except);
103
            $libelleproduitservice = $outputlangs->transnoentitiesnoconv("DiscountFromExcessPaid", $discount->ref_invoice_supplier_source);
104
        } else {
105
            if ($idprod) {
106
                if (empty($hidedesc)) {
107
                    $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
108
                }
109
            } else {
110
                $libelleproduitservice = dol_concatdesc($libelleproduitservice, $desc);
111
            }
112
        }
113
    }
114
115
    // If line linked to a product
116
    if ($idprod) {
117
        // On ajoute la ref
118
        if ($prodser->ref) {
119
            $prefix_prodserv = "";
120
            $ref_prodserv = "";
121
            if (getDolGlobalString('PRODUCT_ADD_TYPE_IN_DOCUMENTS')) {   // In standard mode, we do not show this
122
                if ($prodser->isService()) {
123
                    $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Service") . " ";
124
                } else {
125
                    $prefix_prodserv = $outputlangs->transnoentitiesnoconv("Product") . " ";
126
                }
127
            }
128
129
            if (empty($hideref)) {
130
                if ($issupplierline) {
131
                    $ref_prodserv = $prodser->ref . ' (' . $outputlangs->trans("SupplierRef") . ' ' . $ref_supplier . ')'; // Show local ref and supplier ref
132
                } else {
133
                    $ref_prodserv = $prodser->ref; // Show local ref only
134
                }
135
            }
136
137
            $libelleproduitservice = $prefix_prodserv . $ref_prodserv . ($libelleproduitservice ? " - " : "") . $libelleproduitservice;
138
        }
139
    }
140
141
    if (!empty($line->date_start) || !empty($line->date_end)) {
142
        $format = 'day';
143
        // Show duration if exists
144
        if ($line->date_start && $line->date_end) {
145
            $period = '(' . $outputlangs->transnoentitiesnoconv('DateFromTo', dol_print_date($line->date_start, $format, false, $outputlangs), dol_print_date($line->date_end, $format, false, $outputlangs)) . ')';
146
        }
147
        if ($line->date_start && !$line->date_end) {
148
            $period = '(' . $outputlangs->transnoentitiesnoconv('DateFrom', dol_print_date($line->date_start, $format, false, $outputlangs)) . ')';
149
        }
150
        if (!$line->date_start && $line->date_end) {
151
            $period = '(' . $outputlangs->transnoentitiesnoconv('DateUntil', dol_print_date($line->date_end, $format, false, $outputlangs)) . ')';
152
        }
153
        //print '>'.$outputlangs->charset_output.','.$period;
154
        $libelleproduitservice = dol_concatdesc($libelleproduitservice, $period);
155
        //print $libelleproduitservice;
156
    }
157
158
    return $libelleproduitservice;
159
}
160