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