Completed
Branch develop (edb367)
by
unknown
28:40
created

Cpaiement   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 341
rs 8.8798
c 0
b 0
f 0
wmc 44
lcom 1
cbo 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B fetch() 0 51 5
F create() 0 91 17
F update() 0 73 16
A delete() 0 41 4
A initAsSpecimen() 0 11 1

How to fix   Complexity   

Complex Class

Complex classes like Cpaiement often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Cpaiement, and based on these observations, apply Extract Interface, too.

1
<?php
2
/* Copyright (C) 2007-2012  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2014       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Florian Henry       <[email protected]>
5
 * Copyright (C) 2015       Raphaël Doursenaud  <[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    htdocs/compat/paiement/class/cpaiement.class.php
23
 * \ingroup facture
24
 * \brief   This file is to manage CRUD function of type of payments
25
 */
26
27
28
/**
29
 * Class Cpaiement
30
 */
31
class Cpaiement
32
{
33
	/**
34
	 * @var string Id to identify managed objects
35
	 */
36
	public $element = 'cpaiement';
37
	
38
	/**
39
	 * @var string Name of table without prefix where object is stored
40
	 */
41
	public $table_element = 'c_paiement';
42
43
	public $code;
44
	public $libelle;
45
	public $type;
46
	public $active;
47
	public $accountancy_code;
48
	public $module;
49
50
51
	/**
52
	 * Constructor
53
	 *
54
	 * @param DoliDb $db Database handler
55
	 */
56
	public function __construct(DoliDB $db)
57
	{
58
		$this->db = $db;
59
	}
60
61
	/**
62
	 * Create object into database
63
	 *
64
	 * @param  User $user      User that creates
65
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
66
	 *
67
	 * @return int <0 if KO, Id of created object if OK
68
	 */
69
	public function create(User $user, $notrigger = false)
70
	{
71
		dol_syslog(__METHOD__, LOG_DEBUG);
72
73
		$error = 0;
74
75
		// Clean parameters
76
77
		if (isset($this->code)) {
78
			 $this->code = trim($this->code);
79
		}
80
		if (isset($this->libelle)) {
81
			 $this->libelle = trim($this->libelle);
82
		}
83
		if (isset($this->type)) {
84
			 $this->type = trim($this->type);
85
		}
86
		if (isset($this->active)) {
87
			 $this->active = trim($this->active);
88
		}
89
		if (isset($this->accountancy_code)) {
90
			 $this->accountancy_code = trim($this->accountancy_code);
91
		}
92
		if (isset($this->module)) {
93
			 $this->module = trim($this->module);
94
		}
95
96
97
98
		// Check parameters
99
		// Put here code to add control on parameters values
100
101
		// Insert request
102
		$sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
103
104
		$sql.= 'entity,';
105
		$sql.= 'code,';
106
		$sql.= 'libelle,';
107
		$sql.= 'type,';
108
		$sql.= 'active,';
109
		$sql.= 'accountancy_code,';
110
		$sql.= 'module';
111
112
113
		$sql .= ') VALUES (';
114
115
		$sql .= ' '.(! isset($this->entity)?getEntity('c_paiement'):$this->entity).',';
116
		$sql .= ' '.(! isset($this->code)?'NULL':"'".$this->db->escape($this->code)."'").',';
117
		$sql .= ' '.(! isset($this->libelle)?'NULL':"'".$this->db->escape($this->libelle)."'").',';
118
		$sql .= ' '.(! isset($this->type)?'NULL':$this->type).',';
119
		$sql .= ' '.(! isset($this->active)?'NULL':$this->active).',';
120
		$sql .= ' '.(! isset($this->accountancy_code)?'NULL':"'".$this->db->escape($this->accountancy_code)."'").',';
121
		$sql .= ' '.(! isset($this->module)?'NULL':"'".$this->db->escape($this->module)."'");
122
123
124
		$sql .= ')';
125
126
		$this->db->begin();
127
128
		$resql = $this->db->query($sql);
129
		if (!$resql) {
130
			$error ++;
131
			$this->errors[] = 'Error ' . $this->db->lasterror();
132
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
133
		}
134
135
		if (!$error) {
136
			$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
137
138
			// Uncomment this and change MYOBJECT to your own tag if you
139
			// want this action to call a trigger.
140
			//if (!$notrigger) {
141
142
			//  // Call triggers
143
			//  $result=$this->call_trigger('MYOBJECT_CREATE',$user);
144
			//  if ($result < 0) $error++;
145
			//  // End call triggers
146
			//}
147
		}
148
149
		// Commit or rollback
150
		if ($error) {
151
			$this->db->rollback();
152
153
			return - 1 * $error;
154
		} else {
155
			$this->db->commit();
156
157
			return $this->id;
158
		}
159
	}
160
161
	/**
162
	 * Load object in memory from the database
163
	 *
164
	 * @param int    $id  Id object
165
	 * @param string $ref Ref
166
	 *
167
	 * @return int <0 if KO, 0 if not found, >0 if OK
168
	 */
169
	public function fetch($id, $ref = null)
170
	{
171
		dol_syslog(__METHOD__, LOG_DEBUG);
172
173
		$sql = 'SELECT';
174
		$sql .= ' t.id,';
175
		$sql .= " t.code,";
176
		$sql .= " t.libelle,";
177
		$sql .= " t.type,";
178
		$sql .= " t.active,";
179
		$sql .= " t.accountancy_code,";
180
		$sql .= " t.module";
181
		$sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
182
		if (null !== $ref) {
183
			$sql .= ' WHERE t.entity IN ('.getEntity('c_paiement').')';
184
			$sql .= ' AND t.code = ' . '\'' . $ref . '\'';
185
		} else {
186
			$sql .= ' WHERE t.id = ' . $id;
187
		}
188
189
		$resql = $this->db->query($sql);
190
		if ($resql) {
191
			$numrows = $this->db->num_rows($resql);
192
			if ($numrows) {
193
				$obj = $this->db->fetch_object($resql);
194
195
				$this->id = $obj->id;
196
197
				$this->code = $obj->code;
198
				$this->libelle = $obj->libelle;
199
				$this->type = $obj->type;
200
				$this->active = $obj->active;
201
				$this->accountancy_code = $obj->accountancy_code;
202
				$this->module = $obj->module;
203
204
205
			}
206
			$this->db->free($resql);
207
208
			if ($numrows) {
209
				return 1;
210
			} else {
211
				return 0;
212
			}
213
		} else {
214
			$this->errors[] = 'Error ' . $this->db->lasterror();
215
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
216
217
			return - 1;
218
		}
219
	}
220
221
	/**
222
	 * Update object into database
223
	 *
224
	 * @param  User $user      User that modifies
225
	 * @param  bool $notrigger false=launch triggers after, true=disable triggers
226
	 *
227
	 * @return int <0 if KO, >0 if OK
228
	 */
229
	public function update(User $user, $notrigger = false)
230
	{
231
		$error = 0;
232
233
		dol_syslog(__METHOD__, LOG_DEBUG);
234
235
		// Clean parameters
236
237
		if (isset($this->code)) {
238
			 $this->code = trim($this->code);
239
		}
240
		if (isset($this->libelle)) {
241
			 $this->libelle = trim($this->libelle);
242
		}
243
		if (isset($this->type)) {
244
			 $this->type = trim($this->type);
245
		}
246
		if (isset($this->active)) {
247
			 $this->active = trim($this->active);
248
		}
249
		if (isset($this->accountancy_code)) {
250
			 $this->accountancy_code = trim($this->accountancy_code);
251
		}
252
		if (isset($this->module)) {
253
			 $this->module = trim($this->module);
254
		}
255
256
257
258
		// Check parameters
259
		// Put here code to add a control on parameters values
260
261
		// Update request
262
		$sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
263
		$sql .= ' id = '.(isset($this->id)?$this->id:"null").',';
264
		$sql .= ' code = '.(isset($this->code)?"'".$this->db->escape($this->code)."'":"null").',';
265
		$sql .= ' libelle = '.(isset($this->libelle)?"'".$this->db->escape($this->libelle)."'":"null").',';
266
		$sql .= ' type = '.(isset($this->type)?$this->type:"null").',';
267
		$sql .= ' active = '.(isset($this->active)?$this->active:"null").',';
268
		$sql .= ' accountancy_code = '.(isset($this->accountancy_code)?"'".$this->db->escape($this->accountancy_code)."'":"null").',';
269
		$sql .= ' module = '.(isset($this->module)?"'".$this->db->escape($this->module)."'":"null");
270
		$sql .= ' WHERE id=' . $this->id;
271
272
		$this->db->begin();
273
274
		$resql = $this->db->query($sql);
275
		if (!$resql) {
276
			$error ++;
277
			$this->errors[] = 'Error ' . $this->db->lasterror();
278
			dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
279
		}
280
281
		// Uncomment this and change MYOBJECT to your own tag if you
282
		// want this action calls a trigger.
283
		//if (!$error && !$notrigger) {
284
285
		//  // Call triggers
286
		//  $result=$this->call_trigger('MYOBJECT_MODIFY',$user);
287
		//  if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
288
		//  // End call triggers
289
		//}
290
291
		// Commit or rollback
292
		if ($error) {
293
			$this->db->rollback();
294
295
			return - 1 * $error;
296
		} else {
297
			$this->db->commit();
298
299
			return 1;
300
		}
301
	}
302
303
	/**
304
	 * Delete object in database
305
	 *
306
	 * @param User $user      User that deletes
307
	 * @param bool $notrigger false=launch triggers after, true=disable triggers
308
	 *
309
	 * @return int <0 if KO, >0 if OK
310
	 */
311
	public function delete(User $user, $notrigger = false)
312
	{
313
		dol_syslog(__METHOD__, LOG_DEBUG);
314
315
		$error = 0;
316
317
		$this->db->begin();
318
319
		// Uncomment this and change MYOBJECT to your own tag if you
320
		// want this action calls a trigger.
321
		//if (!$error && !$notrigger) {
322
323
		//  // Call triggers
324
		//  $result=$this->call_trigger('MYOBJECT_DELETE',$user);
325
		//  if ($result < 0) { $error++; //Do also what you must do to rollback action if trigger fail}
326
		//  // End call triggers
327
		//}
328
329
		if (!$error) {
330
			$sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
331
			$sql .= ' WHERE id=' . $this->id;
332
333
			$resql = $this->db->query($sql);
334
			if (!$resql) {
335
				$error ++;
336
				$this->errors[] = 'Error ' . $this->db->lasterror();
337
				dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
338
			}
339
		}
340
341
		// Commit or rollback
342
		if ($error) {
343
			$this->db->rollback();
344
345
			return - 1 * $error;
346
		} else {
347
			$this->db->commit();
348
349
			return 1;
350
		}
351
	}
352
353
354
	/**
355
	 * Initialise object with example values
356
	 * Id must be 0 if object instance is a specimen
357
	 *
358
	 * @return void
359
	 */
360
	public function initAsSpecimen()
361
	{
362
		$this->id = 0;
363
364
		$this->code = '';
365
		$this->libelle = '';
366
		$this->type = '';
367
		$this->active = '';
368
		$this->accountancy_code = '';
369
		$this->module = '';
370
	}
371
}
372