1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2007-2012 Laurent Destailleur <[email protected]> |
4
|
|
|
* Copyright (C) 2024 Frédéric France <[email protected]> |
5
|
|
|
* Copyright (C) 2024 Rafael San José <[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 <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace Dolibarr\Code\Compta\Classes; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* \file compta/facture/class/paymentterm.class.php |
25
|
|
|
* \ingroup invoice |
26
|
|
|
* \brief This file is an example for a CRUD class file (Create/Read/Update/Delete) |
27
|
|
|
*/ |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Class to manage payment terms records in dictionary |
32
|
|
|
*/ |
33
|
|
|
class PaymentTerm // extends CommonObject |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var DoliDB Database handler. |
|
|
|
|
37
|
|
|
*/ |
38
|
|
|
public $db; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var string Error code (or message) |
42
|
|
|
*/ |
43
|
|
|
public $error = ''; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @var string[] Error codes (or messages) |
47
|
|
|
*/ |
48
|
|
|
public $errors = array(); |
49
|
|
|
|
50
|
|
|
//public $element='c_payment_term'; //!< Id that identify managed objects |
51
|
|
|
//public $table_element='c_payment_term'; //!< Name of table without prefix where object is stored |
52
|
|
|
public $context = array(); |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var int ID |
56
|
|
|
*/ |
57
|
|
|
public $id; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @var int Entity ID |
62
|
|
|
*/ |
63
|
|
|
public $entity; |
64
|
|
|
|
65
|
|
|
public $code; |
66
|
|
|
public $sortorder; |
67
|
|
|
public $active; |
68
|
|
|
public $libelle; |
69
|
|
|
public $libelle_facture; |
70
|
|
|
public $type_cdr; |
71
|
|
|
public $nbjour; |
72
|
|
|
public $decalage; |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
|
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Constructor |
79
|
|
|
* |
80
|
|
|
* @param DoliDB $db Database handler |
81
|
|
|
*/ |
82
|
|
|
public function __construct(DoliDB $db) |
83
|
|
|
{ |
84
|
|
|
$this->db = $db; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Create in database |
90
|
|
|
* |
91
|
|
|
* @param User $user User that create |
92
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
93
|
|
|
* @return int Return integer <0 if KO, Id of created object if OK |
94
|
|
|
*/ |
95
|
|
|
public function create($user, $notrigger = 0) |
96
|
|
|
{ |
97
|
|
|
global $conf, $langs; |
98
|
|
|
$error = 0; |
99
|
|
|
|
100
|
|
|
// Clean parameters |
101
|
|
|
|
102
|
|
|
if (isset($this->code)) { |
103
|
|
|
$this->code = trim($this->code); |
104
|
|
|
} |
105
|
|
|
if (isset($this->sortorder)) { |
106
|
|
|
$this->sortorder = trim($this->sortorder); |
107
|
|
|
} |
108
|
|
|
if (isset($this->active)) { |
109
|
|
|
$this->active = trim($this->active); |
110
|
|
|
} |
111
|
|
|
if (isset($this->libelle)) { |
112
|
|
|
$this->libelle = trim($this->libelle); |
113
|
|
|
} |
114
|
|
|
if (isset($this->libelle_facture)) { |
115
|
|
|
$this->libelle_facture = trim($this->libelle_facture); |
116
|
|
|
} |
117
|
|
|
if (isset($this->type_cdr)) { |
118
|
|
|
$this->type_cdr = trim($this->type_cdr); |
119
|
|
|
} |
120
|
|
|
if (isset($this->nbjour)) { |
121
|
|
|
$this->nbjour = trim($this->nbjour); |
122
|
|
|
} |
123
|
|
|
if (isset($this->decalage)) { |
124
|
|
|
$this->decalage = trim($this->decalage); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
|
128
|
|
|
// Check parameters |
129
|
|
|
// Put here code to add control on parameters values |
130
|
|
|
|
131
|
|
|
// Insert request |
132
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "c_payment_term("; |
133
|
|
|
$sql .= "entity,"; |
134
|
|
|
$sql .= "code,"; |
135
|
|
|
$sql .= "sortorder,"; |
136
|
|
|
$sql .= "active,"; |
137
|
|
|
$sql .= "libelle,"; |
138
|
|
|
$sql .= "libelle_facture,"; |
139
|
|
|
$sql .= "type_cdr,"; |
140
|
|
|
$sql .= "nbjour,"; |
141
|
|
|
$sql .= "decalage"; |
142
|
|
|
$sql .= ") VALUES ("; |
143
|
|
|
$sql .= " " . (!isset($this->entity) ? getEntity('c_payment_term') : "'" . $this->db->escape($this->entity) . "'") . ","; |
144
|
|
|
$sql .= " " . (!isset($this->code) ? 'NULL' : "'" . $this->db->escape($this->code) . "'") . ","; |
145
|
|
|
$sql .= " " . (!isset($this->sortorder) ? 'NULL' : "'" . $this->db->escape($this->sortorder) . "'") . ","; |
146
|
|
|
$sql .= " " . (!isset($this->active) ? 'NULL' : "'" . $this->db->escape($this->active) . "'") . ","; |
147
|
|
|
$sql .= " " . (!isset($this->libelle) ? 'NULL' : "'" . $this->db->escape($this->libelle) . "'") . ","; |
148
|
|
|
$sql .= " " . (!isset($this->libelle_facture) ? 'NULL' : "'" . $this->db->escape($this->libelle_facture) . "'") . ","; |
149
|
|
|
$sql .= " " . (!isset($this->type_cdr) ? 'NULL' : "'" . $this->db->escape($this->type_cdr) . "'") . ","; |
150
|
|
|
$sql .= " " . (!isset($this->nbjour) ? 'NULL' : "'" . $this->db->escape($this->nbjour) . "'") . ","; |
151
|
|
|
$sql .= " " . (!isset($this->decalage) ? 'NULL' : "'" . $this->db->escape($this->decalage) . "'"); |
152
|
|
|
$sql .= ")"; |
153
|
|
|
|
154
|
|
|
$this->db->begin(); |
155
|
|
|
|
156
|
|
|
dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
157
|
|
|
$resql = $this->db->query($sql); |
158
|
|
|
if (!$resql) { |
159
|
|
|
$error++; |
160
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
if (!$error) { |
164
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "c_payment_term"); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
// Commit or rollback |
168
|
|
|
if ($error) { |
169
|
|
|
foreach ($this->errors as $errmsg) { |
170
|
|
|
dol_syslog(get_class($this) . "::create " . $errmsg, LOG_ERR); |
171
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
172
|
|
|
} |
173
|
|
|
$this->db->rollback(); |
174
|
|
|
return -1 * $error; |
175
|
|
|
} else { |
176
|
|
|
$this->db->commit(); |
177
|
|
|
return $this->id; |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Load object in memory from database |
184
|
|
|
* |
185
|
|
|
* @param int $id Id object |
186
|
|
|
* @param string $code Code object |
187
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
188
|
|
|
*/ |
189
|
|
|
public function fetch($id, $code = '') |
190
|
|
|
{ |
191
|
|
|
$sql = "SELECT"; |
192
|
|
|
$sql .= " t.rowid,"; |
193
|
|
|
$sql .= " t.entity,"; |
194
|
|
|
$sql .= " t.code,"; |
195
|
|
|
$sql .= " t.sortorder,"; |
196
|
|
|
$sql .= " t.active,"; |
197
|
|
|
$sql .= " t.libelle,"; |
198
|
|
|
$sql .= " t.libelle_facture,"; |
199
|
|
|
$sql .= " t.type_cdr,"; |
200
|
|
|
$sql .= " t.nbjour,"; |
201
|
|
|
$sql .= " t.decalage"; |
202
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "c_payment_term as t"; |
203
|
|
|
if ($id) { |
204
|
|
|
$sql .= " WHERE t.rowid = " . ((int) $id); |
205
|
|
|
} |
206
|
|
|
if ($code) { |
207
|
|
|
$sql .= " WHERE t.code='" . $this->db->escape($code) . "' AND t.entity IN (" . getEntity('payment_term') . ")"; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
211
|
|
|
$resql = $this->db->query($sql); |
212
|
|
|
if ($resql) { |
213
|
|
|
if ($this->db->num_rows($resql)) { |
214
|
|
|
$obj = $this->db->fetch_object($resql); |
215
|
|
|
|
216
|
|
|
$this->id = $obj->rowid; |
217
|
|
|
|
218
|
|
|
$this->code = $obj->code; |
219
|
|
|
$this->sortorder = $obj->sortorder; |
220
|
|
|
$this->active = $obj->active; |
221
|
|
|
$this->libelle = $obj->libelle; |
222
|
|
|
$this->libelle_facture = $obj->libelle_facture; |
223
|
|
|
$this->type_cdr = $obj->type_cdr; |
224
|
|
|
$this->nbjour = $obj->nbjour; |
225
|
|
|
$this->decalage = $obj->decalage; |
226
|
|
|
} |
227
|
|
|
$this->db->free($resql); |
228
|
|
|
|
229
|
|
|
return 1; |
230
|
|
|
} else { |
231
|
|
|
$this->error = "Error " . $this->db->lasterror(); |
232
|
|
|
return -1; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Return id of default payment term |
239
|
|
|
* |
240
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
241
|
|
|
*/ |
242
|
|
|
public function getDefaultId() |
243
|
|
|
{ |
244
|
|
|
global $langs; |
245
|
|
|
|
246
|
|
|
$ret = 0; |
247
|
|
|
|
248
|
|
|
$sql = "SELECT"; |
249
|
|
|
$sql .= " t.rowid"; |
250
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "c_payment_term as t"; |
251
|
|
|
$sql .= " WHERE t.code = 'RECEP'"; |
252
|
|
|
$sql .= " AND t.entity IN (" . getEntity('c_payment_term') . ")"; |
253
|
|
|
|
254
|
|
|
dol_syslog(get_class($this) . "::getDefaultId", LOG_DEBUG); |
255
|
|
|
$resql = $this->db->query($sql); |
256
|
|
|
if ($resql) { |
257
|
|
|
if ($this->db->num_rows($resql)) { |
258
|
|
|
$obj = $this->db->fetch_object($resql); |
259
|
|
|
if ($obj) { |
260
|
|
|
$ret = $obj->rowid; |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
$this->db->free($resql); |
264
|
|
|
return $ret; |
265
|
|
|
} else { |
266
|
|
|
$this->error = "Error " . $this->db->lasterror(); |
267
|
|
|
return -1; |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Update database |
274
|
|
|
* |
275
|
|
|
* @param User $user User that modify |
276
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
277
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
278
|
|
|
*/ |
279
|
|
|
public function update($user = null, $notrigger = 0) |
280
|
|
|
{ |
281
|
|
|
global $conf, $langs; |
282
|
|
|
|
283
|
|
|
$error = 0; |
284
|
|
|
|
285
|
|
|
// Clean parameters |
286
|
|
|
|
287
|
|
|
if (isset($this->code)) { |
288
|
|
|
$this->code = trim($this->code); |
289
|
|
|
} |
290
|
|
|
if (isset($this->sortorder)) { |
291
|
|
|
$this->sortorder = trim($this->sortorder); |
292
|
|
|
} |
293
|
|
|
if (isset($this->active)) { |
294
|
|
|
$this->active = trim($this->active); |
295
|
|
|
} |
296
|
|
|
if (isset($this->libelle)) { |
297
|
|
|
$this->libelle = trim($this->libelle); |
298
|
|
|
} |
299
|
|
|
if (isset($this->libelle_facture)) { |
300
|
|
|
$this->libelle_facture = trim($this->libelle_facture); |
301
|
|
|
} |
302
|
|
|
if (isset($this->type_cdr)) { |
303
|
|
|
$this->type_cdr = trim($this->type_cdr); |
304
|
|
|
} |
305
|
|
|
if (isset($this->nbjour)) { |
306
|
|
|
$this->nbjour = trim($this->nbjour); |
307
|
|
|
} |
308
|
|
|
if (isset($this->decalage)) { |
309
|
|
|
$this->decalage = trim($this->decalage); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
|
313
|
|
|
|
314
|
|
|
// Check parameters |
315
|
|
|
// Put here code to add control on parameters values |
316
|
|
|
|
317
|
|
|
// Update request |
318
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "c_payment_term SET"; |
319
|
|
|
$sql .= " code=" . (isset($this->code) ? "'" . $this->db->escape($this->code) . "'" : "null") . ","; |
320
|
|
|
$sql .= " sortorder=" . (isset($this->sortorder) ? $this->sortorder : "null") . ","; |
321
|
|
|
$sql .= " active=" . (isset($this->active) ? $this->active : "null") . ","; |
322
|
|
|
$sql .= " libelle=" . (isset($this->libelle) ? "'" . $this->db->escape($this->libelle) . "'" : "null") . ","; |
323
|
|
|
$sql .= " libelle_facture=" . (isset($this->libelle_facture) ? "'" . $this->db->escape($this->libelle_facture) . "'" : "null") . ","; |
324
|
|
|
$sql .= " type_cdr=" . (isset($this->type_cdr) ? $this->type_cdr : "null") . ","; |
325
|
|
|
$sql .= " nbjour=" . (isset($this->nbjour) ? $this->nbjour : "null") . ","; |
326
|
|
|
$sql .= " decalage=" . (isset($this->decalage) ? $this->decalage : "null"); |
327
|
|
|
$sql .= " WHERE rowid = " . ((int) $this->id); |
328
|
|
|
|
329
|
|
|
$this->db->begin(); |
330
|
|
|
|
331
|
|
|
dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
332
|
|
|
$resql = $this->db->query($sql); |
333
|
|
|
if (!$resql) { |
334
|
|
|
$error++; |
335
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
// Commit or rollback |
339
|
|
|
if ($error) { |
340
|
|
|
foreach ($this->errors as $errmsg) { |
341
|
|
|
dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
342
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
343
|
|
|
} |
344
|
|
|
$this->db->rollback(); |
345
|
|
|
return -1 * $error; |
346
|
|
|
} else { |
347
|
|
|
$this->db->commit(); |
348
|
|
|
return 1; |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
|
353
|
|
|
/** |
354
|
|
|
* Delete object in database |
355
|
|
|
* |
356
|
|
|
* @param User $user User that delete |
357
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
358
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
359
|
|
|
*/ |
360
|
|
|
public function delete($user, $notrigger = 0) |
361
|
|
|
{ |
362
|
|
|
global $conf, $langs; |
363
|
|
|
$error = 0; |
364
|
|
|
|
365
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "c_payment_term"; |
366
|
|
|
$sql .= " WHERE rowid = " . ((int) $this->id); |
367
|
|
|
|
368
|
|
|
$this->db->begin(); |
369
|
|
|
|
370
|
|
|
dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
371
|
|
|
$resql = $this->db->query($sql); |
372
|
|
|
if (!$resql) { |
373
|
|
|
$error++; |
374
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
// Commit or rollback |
378
|
|
|
if ($error) { |
379
|
|
|
foreach ($this->errors as $errmsg) { |
380
|
|
|
dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
381
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
382
|
|
|
} |
383
|
|
|
$this->db->rollback(); |
384
|
|
|
return -1 * $error; |
385
|
|
|
} else { |
386
|
|
|
$this->db->commit(); |
387
|
|
|
return 1; |
388
|
|
|
} |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
|
392
|
|
|
|
393
|
|
|
/** |
394
|
|
|
* Load an object from its id and create a new one in database |
395
|
|
|
* |
396
|
|
|
* @param User $user User making the clone |
397
|
|
|
* @param int $fromid Id of object to clone |
398
|
|
|
* @return int New id of clone |
399
|
|
|
*/ |
400
|
|
|
public function createFromClone(User $user, $fromid) |
401
|
|
|
{ |
402
|
|
|
$error = 0; |
403
|
|
|
|
404
|
|
|
$object = new PaymentTerm($this->db); |
405
|
|
|
|
406
|
|
|
$this->db->begin(); |
407
|
|
|
|
408
|
|
|
// Load source object |
409
|
|
|
$object->fetch($fromid); |
410
|
|
|
$object->id = 0; |
411
|
|
|
|
412
|
|
|
// Create clone |
413
|
|
|
$object->context['createfromclone'] = 'createfromclone'; |
414
|
|
|
$result = $object->create($user); |
415
|
|
|
|
416
|
|
|
// Other options |
417
|
|
|
if ($result < 0) { |
418
|
|
|
$this->error = $object->error; |
419
|
|
|
$error++; |
420
|
|
|
} |
421
|
|
|
|
422
|
|
|
unset($object->context['createfromclone']); |
423
|
|
|
|
424
|
|
|
// End |
425
|
|
|
if (!$error) { |
426
|
|
|
$this->db->commit(); |
427
|
|
|
return $object->id; |
428
|
|
|
} else { |
429
|
|
|
$this->db->rollback(); |
430
|
|
|
return -1; |
431
|
|
|
} |
432
|
|
|
} |
433
|
|
|
|
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* Initialise an instance with random values. |
437
|
|
|
* Used to build previews or test instances. |
438
|
|
|
* id must be 0 if object instance is a specimen. |
439
|
|
|
* |
440
|
|
|
* @return int |
441
|
|
|
*/ |
442
|
|
|
public function initAsSpecimen() |
443
|
|
|
{ |
444
|
|
|
$this->id = 0; |
445
|
|
|
|
446
|
|
|
$this->code = ''; |
447
|
|
|
$this->sortorder = ''; |
448
|
|
|
$this->active = ''; |
449
|
|
|
$this->libelle = ''; |
450
|
|
|
$this->libelle_facture = ''; |
451
|
|
|
$this->type_cdr = ''; |
452
|
|
|
$this->nbjour = ''; |
453
|
|
|
$this->decalage = ''; |
454
|
|
|
|
455
|
|
|
return 1; |
456
|
|
|
} |
457
|
|
|
} |
458
|
|
|
|