Completed
Branch develop (9ce07b)
by
unknown
26:31
created

CashControl::getNomUrl()   F

Complexity

Conditions 16
Paths 864

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 16
nc 864
nop 5
dl 0
loc 62
rs 1.5888
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) 2008 Laurent Destailleur  <[email protected]>
3
 * Copyright (C) 2009 Regis Houssin        <[email protected]>
4
 * Copyright (C) 2016 Marcos García        <[email protected]>
5
 * Copyright (C) 2018 Andreu Bisquerra     <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19
 */
20
21
/**
22
 * \file       cashcontrol/class/cashcontrol.class.php
23
 * \ingroup    bank
24
 * \brief      This file is CRUD class file (Create/Read/Update/Delete) for cash fence table
25
 */
26
27
/**
28
 *    Class to manage cash fence
29
 */
30
class CashControl extends CommonObject
31
{
32
	/**
33
	 * @var string ID to identify managed object
34
	 */
35
	public $element = 'CashControl';
36
37
	/**
38
	 * @var string Name of table without prefix where object is stored
39
	 */
40
	public $table_element = 'pos_cash_fence';
41
42
	/**
43
	 * @var int  Does pos_cash_fence support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
44
	 */
45
	public $ismultientitymanaged = 1;
46
47
	/**
48
	 * @var int  Does pos_cash_fence support extrafields ? 0=No, 1=Yes
49
	 */
50
	public $isextrafieldmanaged = 0;
51
52
	/**
53
	 * @var string String with name of icon for pos_cash_fence. Must be the part after the 'object_' into object_pos_cash_fence.png
54
	 */
55
	public $picto = 'bank';
56
57
	public $fields=array(
58
		'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>10),
59
		'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>15),
60
		'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>18),
61
		'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>19),
62
		'posnumber' =>array('type'=>'varchar(30)', 'label'=>'CashDesk', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>20),
63
		'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>24),
64
		'opening' =>array('type'=>'double(24,8)', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25),
65
		'cash' =>array('type'=>'double(24,8)', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30),
66
		'cheque' =>array('type'=>'double(24,8)', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>33),
67
		'card' =>array('type'=>'double(24,8)', 'label'=>'CreditCard', 'enabled'=>1, 'visible'=>1, 'position'=>36),
68
		'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>50),
69
		'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55),
70
		'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>60),
71
		'date_creation' =>array('type'=>'datetime', 'label'=>'Date creation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
72
		'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>505),
73
		'import_key' =>array('type'=>'varchar(14)', 'label'=>'Import key', 'enabled'=>1, 'visible'=>0, 'position'=>510),
74
		'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Brouillon', '1'=>'Validated')),
75
	);
76
77
	public $id;
78
	public $opening;
79
	public $status;
80
	public $date_creation;
81
	public $year_close;
82
	public $month_close;
83
	public $day_close;
84
	public $posmodule;
85
	public $posnumber;
86
87
88
89
	/**
90
	 * Constructor
91
	 *
92
	 * @param DoliDB $db Database handler
93
	 */
94
	public function __construct(DoliDB $db)
95
	{
96
		$this->db = $db;
97
	}
98
99
100
	/**
101
	 *  Create in database
102
	 *
103
	 * @param  User $user User that create
104
	 * @param  int $notrigger 0=launch triggers after, 1=disable triggers
105
	 * @return int <0 if KO, Id of created object if OK
106
	 */
107
	public function create(User $user, $notrigger = 0)
108
	{
109
		global $conf;
110
111
		$error = 0;
112
113
		// Insert request
114
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
115
		$sql .= "entity";
116
		//$sql .= ", ref";
117
		$sql .= ", opening";
118
        $sql .= ", status";
119
		$sql .= ", date_creation";
120
		$sql .= ", posmodule";
121
		$sql .= ", posnumber";
122
		$sql .= ") VALUES (";
123
		//$sql .= "'(PROV)', ";
124
		$sql .= $conf->entity;
125
		$sql .= ", ".$this->opening;
126
        $sql .= ", 0";										// Draft by default
127
		$sql .= ", '".$this->db->idate(dol_now())."'";
128
		$sql .= ", '".$this->db->escape($this->posmodule)."'";
129
		$sql .= ", '".$this->db->escape($this->posnumber)."'";
130
		$sql .= ")";
131
132
		$this->db->begin();
133
134
		dol_syslog(get_class($this)."::create", LOG_DEBUG);
135
		$resql = $this->db->query($sql);
136
		if (!$resql) {
137
			$error++;
138
			$this->errors[] = "Error ".$this->db->lasterror();
139
		}
140
141
		if (!$error) {
142
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
143
144
			$sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.$this->id;
145
			$this->db->query($sql);
146
		}
147
148
		// Commit or rollback
149
		if ($error) {
150
			foreach ($this->errors as $errmsg) {
151
				dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
152
				$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
153
			}
154
			$this->db->rollback();
155
			return -1 * $error;
156
		} else {
157
			$this->db->commit();
158
			return $this->id;
159
		}
160
	}
161
162
	/**
163
	 * close
164
	 *
165
	 * @param 	User 		$user		User
166
	 * @param 	number 		$notrigger	No trigger
167
	 * @return 	int						<0 if KO, >0 if OK
168
	 */
169
	public function close(User $user, $notrigger = 0)
170
	{
171
		global $conf;
172
173
		$error = 0;
174
175
		// Update request
176
		$sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence ";
177
		$sql.= "SET";
178
		$sql.= " day_close=DAYOFMONTH(NOW())";
179
		$sql.= ", month_close=MONTH(NOW())";
180
		$sql.= ", year_close=YEAR(NOW())";
181
        $sql.= ", status=2";
182
		$sql.= " where rowid=".$this->id;
183
		$this->db->begin();
184
185
		dol_syslog(get_class($this)."::create", LOG_DEBUG);
186
		$resql = $this->db->query($sql);
187
		if (!$resql) {
188
			$error++;
189
			$this->errors[] = "Error ".$this->db->lasterror();
190
		}
191
192
		if (!$error) {
193
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
194
		}
195
196
		// Commit or rollback
197
		if ($error) {
198
			foreach ($this->errors as $errmsg) {
199
				dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
200
				$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
201
			}
202
			$this->db->rollback();
203
			return -1 * $error;
204
		} else {
205
			$this->db->commit();
206
			return $this->id;
207
		}
208
	}
209
210
211
	/**
212
	 * Load object in memory from database
213
	 *
214
	 * @param  int 	$id 		Id object
215
	 * @return int 				<0 if KO, >0 if OK
216
	 */
217
	public function fetch($id)
218
	{
219
		global $conf;
220
221
		$sql = "SELECT";
222
		$sql .= " *";
223
		$sql .= " FROM ".MAIN_DB_PREFIX."pos_cash_fence";
224
		$sql .= " WHERE rowid = ".$id;
225
		$sql .= " AND entity = ".$conf->entity;
226
227
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
228
		$resql = $this->db->query($sql);
229
		if ($resql) {
230
			if ($this->db->num_rows($resql)) {
231
				$obj = $this->db->fetch_object($resql);
232
				$this->ref = $obj->id;
233
				$this->label = $obj->label;
234
				$this->opening = $obj->opening;
235
				$this->status = $obj->status;
236
				$this->year_close = $obj->year_close;
237
				$this->month_close = $obj->month_close;
238
				$this->day_close = $obj->day_close;
239
				$this->posmodule = $obj->posmodule;
240
				$this->posnumber = $obj->posnumber;
241
				$this->date_creation = $obj->date_creation;
242
				$this->tms = $obj->tms;
243
				$this->id=$id;
244
			}
245
			$this->db->free($resql);
246
247
			return 1;
248
		} else {
249
			$this->error = "Error ".$this->db->lasterror();
250
			return -1;
251
		}
252
	}
253
254
255
	/**
256
	 *    Return clicable link of object (with eventually picto)
257
	 *
258
	 * @param  int    $withpicto             Add picto into link
259
	 * @param  string $option                Where point the link ('stock', 'composition', 'category', 'supplier', '')
260
	 * @param  int    $maxlength             Maxlength of ref
261
	 * @param  int    $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
262
     * @param  int    $notooltip			 No tooltip
263
	 * @return string                                String with URL
264
	 */
265
	public function getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0)
266
	{
267
		global $conf, $langs, $hookmanager;
268
		include_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
269
270
		$result='';
271
		$newref=($this->ref?$this->ref:$this->id);
272
		if ($maxlength) { $newref=dol_trunc($newref, $maxlength, 'middle'); }
273
274
		$label = '<u>' . $langs->trans("ShowCashFence") . '</u>';
275
		$label .= '<br><b>' . $langs->trans('ProductRef') . ':</b> ' . ($this->ref?$this->ref:$this->id);
276
277
		$linkclose='';
278
		if (empty($notooltip)) {
279
			if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
280
				$label=$langs->trans("ShowCashFence");
281
				$linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
282
			}
283
284
			$linkclose.= ' title="'.dol_escape_htmltag($label, 1, 1).'"';
285
			$linkclose.= ' class="classfortooltip"';
286
287
			/*
288
			 $hookmanager->initHooks(array('productdao'));
289
			 $parameters=array('id'=>$this->id);
290
			 $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action);    // Note that $action and $object may have been modified by some hooks
291
			 if ($reshook > 0) $linkclose = $hookmanager->resPrint;
292
			 */
293
		}
294
295
		$url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
296
297
		if ($option !== 'nolink') {
298
			// Add param to save lastsearch_values or not
299
			$add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
300
			if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { $add_save_lastsearch_values=1;
301
			}
302
			if ($add_save_lastsearch_values) { $url.='&save_lastsearch_values=1';
303
			}
304
		}
305
306
		$linkstart = '<a href="'.$url.'"';
307
		$linkstart.=$linkclose.'>';
308
		$linkend='</a>';
309
310
		$result.=$linkstart;
311
		if ($withpicto) {
312
			$result.=(img_object(($notooltip?'':$label), 'bank', ($notooltip?'class="paddingright"':'class="paddingright classfortooltip"'), 0, 0, $notooltip?0:1));
313
		}
314
		$result.= $newref;
315
		$result.= $linkend;
316
317
		global $action;
318
		$hookmanager->initHooks(array('cashfencedao'));
319
		$parameters=array('id'=>$this->id, 'getnomurl'=>$result);
320
		$reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action);    // Note that $action and $object may have been modified by some hooks
321
		if ($reshook > 0) { $result = $hookmanager->resPrint;
322
		} else { $result .= $hookmanager->resPrint;
323
		}
324
325
		return $result;
326
	}
327
}