Completed
Branch develop (fb88ea)
by
unknown
42:49
created

box_services_contracts::loadBox()   C

Complexity

Conditions 12
Paths 45

Size

Total Lines 117
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 12
eloc 76
nc 45
nop 1
dl 0
loc 117
rs 5.034
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
/* Copyright (C) 2003      Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2005-2011 Laurent Destailleur  <[email protected]>
4
 * Copyright (C) 2005-2011 Regis Houssin        <[email protected]>
5
 *
6
 * This program is free software; you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation; either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
/**
21
 *      \file       htdocs/core/boxes/box_services_contracts.php
22
 *		\ingroup    produits,services
23
 *      \brief      Module de generation de l'affichage de la box services_vendus
24
 */
25
26
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';
27
28
29
/**
30
 * Class to manage the box to show last services lines
31
 */
32
class box_services_contracts extends ModeleBoxes
33
{
34
	var $boxcode="lastproductsincontract";
35
	var $boximg="object_product";
36
	var $boxlabel="BoxLastProductsInContract";
37
	var $depends = array("service","contrat");
38
39
	var $db;
40
	var $param;
41
42
	var $info_box_head = array();
43
	var $info_box_contents = array();
44
45
46
	/**
47
	 *  Load data into info_box_contents array to show array later.
48
	 *
49
	 *  @param	int		$max        Maximum number of records to load
50
     *  @return	void
51
	 */
52
	function loadBox($max=5)
53
	{
54
		global $user, $langs, $db, $conf;
55
56
		$this->max=$max;
57
58
		include_once DOL_DOCUMENT_ROOT.'/contrat/class/contrat.class.php';
59
60
		$this->info_box_head = array('text' => $langs->trans("BoxLastProductsInContract",$max));
61
62
		if ($user->rights->service->lire && $user->rights->contrat->lire)
63
		{
64
		    $contractstatic=new Contrat($db);
65
		    $contratlignestatic=new ContratLigne($db);
66
		    $thirdpartytmp = new Societe($db);
67
68
			$sql = "SELECT s.nom as name, s.rowid as socid,";
69
			$sql.= " c.rowid, c.ref, c.statut as contract_status,";
70
			$sql.= " cd.rowid as cdid, cd.tms as datem, cd.statut, cd.label, cd.description, cd.product_type as type,";
71
			$sql.= " p.rowid as product_id, p.ref as product_ref";
72
			$sql.= " FROM (".MAIN_DB_PREFIX."societe as s";
73
			$sql.= " INNER JOIN ".MAIN_DB_PREFIX."contrat as c ON s.rowid = c.fk_soc";
74
			$sql.= " INNER JOIN ".MAIN_DB_PREFIX."contratdet as cd ON c.rowid = cd.fk_contrat";
75
			$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON cd.fk_product = p.rowid";
76
			if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= "INNER JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
77
			$sql.= ")";
78
			$sql.= " WHERE c.entity = ".$conf->entity;
79
			if($user->societe_id) $sql.= " AND s.rowid = ".$user->societe_id;
80
			$sql.= $db->order("c.tms","DESC");
81
			$sql.= $db->plimit($max, 0);
82
83
			$result = $db->query($sql);
84
			if ($result)
85
			{
86
				$num = $db->num_rows($result);
87
				$now=dol_now();
88
89
				$i = 0;
90
91
				while ($i < $num)
92
				{
93
					$objp = $db->fetch_object($result);
94
					$datem=$db->jdate($objp->datem);
95
96
					$contratlignestatic->id=$objp->cdid;
97
					$contratlignestatic->fk_contrat=$objp->rowid;
98
					$contratlignestatic->label=$objp->label;
99
					$contratlignestatic->description=$objp->description;
100
					$contratlignestatic->type=$objp->type;
101
					$contratlignestatic->product_id=$objp->product_id;
102
					$contratlignestatic->product_ref=$objp->product_ref;
103
104
                    $contractstatic->statut=$objp->contract_status;
105
					$contractstatic->id=$objp->rowid;
106
					$contractstatic->ref=$objp->ref;
107
108
					$thirdpartytmp->name = $objp->name;
109
					$thirdpartytmp->id = $objp->socid;
110
111
					// Multilangs
112
					if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active
113
					{
114
						$sqld = "SELECT label";
115
						$sqld.= " FROM ".MAIN_DB_PREFIX."product_lang";
116
						$sqld.= " WHERE fk_product=".$objp->product_id;
117
						$sqld.= " AND lang='". $langs->getDefaultLang() ."'";
118
						$sqld.= " LIMIT 1";
119
120
						$resultd = $db->query($sqld);
121
						if ($resultd)
122
						{
123
							$objtp = $db->fetch_object($resultd);
124
							if ($objtp->label != '') $contratlignestatic->label = $objtp->label;
125
						}
126
					}
127
128
					$this->info_box_contents[$i][] = array('td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"',
129
                    'text' => $contratlignestatic->getNomUrl(1),
130
					'asis' => 1
131
                    );
132
133
					$this->info_box_contents[$i][] = array('td' => '',
134
                    'text' => $contractstatic->getNomUrl(1),
135
					'asis' => 1
136
                    );
137
138
					$this->info_box_contents[$i][] = array('td' => '',
139
                    'text' => $thirdpartytmp->getNomUrl(1),
140
					'asis' => 1
141
                    );
142
143
					$this->info_box_contents[$i][] = array('td' => '',
144
                    'text' => dol_print_date($datem,'day'));
145
146
					$this->info_box_contents[$i][] = array('td' => 'align="right" width="18"',
147
                    'text' => $contratlignestatic->LibStatut($objp->statut,3)
148
					);
149
150
					$i++;
151
				}
152
				if ($num==0) $this->info_box_contents[$i][0] = array('td' => 'align="center"','text'=>$langs->trans("NoContractedProducts"));
153
154
				$db->free($result);
155
			}
156
			else
157
			{
158
				$this->info_box_contents[0][0] = array(	'td' => 'align="left"',
159
    	        										'maxlength'=>500,
160
	            										'text' => ($db->error().' sql='.$sql));
161
			}
162
		}
163
		else {
164
			$this->info_box_contents[0][0] = array('td' => 'align="left"',
165
            'text' => $langs->trans("ReadPermissionNotAllowed"));
166
		}
167
168
	}
169
170
	/**
171
	 *	Method to show box
172
	 *
173
	 *	@param	array	$head       Array with properties of box title
174
	 *	@param  array	$contents   Array with properties of box lines
175
	 *  @param	int		$nooutput	No print, only return string
176
	 *	@return	void
177
	 */
178
    function showBox($head = null, $contents = null, $nooutput=0)
179
    {
180
		parent::showBox($this->info_box_head, $this->info_box_contents, $nooutput);
181
	}
182
183
}
184
185