Passed
Branch develop (7b8a20)
by
unknown
32:48
created

CommonObjectLine::getLabelOfUnit()   B

Complexity

Conditions 7
Paths 10

Size

Total Lines 28
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 20
nc 10
nop 1
dl 0
loc 28
rs 8.6666
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2006-2008 Laurent Destailleur  <[email protected]>
3
 * Copyright (C) 2012      Cedric Salvador      <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 *	\file       htdocs/core/class/commonobjectline.class.php
21
 *  \ingroup    core
22
 *  \brief      File of the superclass of classes of lines of business objects (invoice, contract, PROPAL, commands, etc. ...)
23
 */
24
25
26
/**
27
 *  Parent class for class inheritance lines of business objects
28
 *  This class is useless for the moment so no inherit are done on it
29
 */
30
abstract class CommonObjectLine extends CommonObject
31
{
32
	/**
33
	 * Id of the line
34
	 * @var int
35
	 */
36
	public $id;
37
38
	/**
39
	 * Id of the line
40
	 * @var int
41
	 * @deprecated Try to use id property as possible (even if field into database is still rowid)
42
	 * @see $id
43
	 */
44
	public $rowid;
45
46
	/**
47
	 * Product/service unit code ('km', 'm', 'p', ...)
48
	 * @var string
49
	 */
50
	public $fk_unit;
51
52
53
	/**
54
	 *	Constructor
55
	 *
56
	 *  @param		DoliDB		$db      Database handler
57
	 */
58
	public function __construct($db)
59
	{
60
		$this->db = $db;
61
	}
62
63
	/**
64
	 *	Returns the label, shot_label or code found in units dictionary from ->fk_unit.
65
	 *  A langs->trans() must be called on result to get translated value.
66
	 *
67
	 * 	@param	string $type 	Label type ('long', 'short' or 'code'). This can be a translation key.
68
	 *	@return	string|int 		<0 if KO, label if OK (Example: 'long', 'short' or 'unitCODE')
69
	 */
70
	public function getLabelOfUnit($type = 'long')
71
	{
72
		global $langs;
73
74
		if (!$this->fk_unit) {
75
			return '';
76
		}
77
78
		$langs->load('products');
79
80
		$label_type = 'label';
81
82
		$label_type = 'label';
83
		if ($type == 'short') $label_type = 'short_label';
84
		elseif ($type == 'code') $label_type = 'code';
85
86
		$sql = 'select '.$label_type.', code from '.MAIN_DB_PREFIX.'c_units where rowid='.$this->fk_unit;
87
		$resql = $this->db->query($sql);
88
		if ($resql && $this->db->num_rows($resql) > 0) {
89
			$res = $this->db->fetch_array($resql);
90
			if ($label_type == 'code') $label = 'unit'.$res['code'];
91
			else $label = $res[$label_type];
92
			$this->db->free($resql);
93
			return $label;
94
		} else {
95
			$this->error = $this->db->error().' sql='.$sql;
96
			dol_syslog(get_class($this)."::getLabelOfUnit Error ".$this->error, LOG_ERR);
97
			return -1;
98
		}
99
	}
100
	// Currently we need function at end of file CommonObject for all object lines. Should find a way to avoid duplicate code.
101
102
	// For the moment we use the extends on CommonObject until PHP min is 5.4 so use Traits.
103
}
104