1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* Copyright (C) 2014-2018 Alexandre Spangaro <[email protected]> |
4
|
|
|
* Copyright (C) 2015-2024 Frédéric France <[email protected]> |
5
|
|
|
* Copyright (C) 2020 Maxime DEMAREST <[email protected]> |
6
|
|
|
* Copyright (C) 2024 MDW <[email protected]> |
7
|
|
|
* Copyright (C) 2024 Rafael San José <[email protected]> |
8
|
|
|
* |
9
|
|
|
* This program is free software; you can redistribute it and/or modify |
10
|
|
|
* it under the terms of the GNU General Public License as published by |
11
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
12
|
|
|
* (at your option) any later version. |
13
|
|
|
* |
14
|
|
|
* This program is distributed in the hope that it will be useful, |
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17
|
|
|
* GNU General Public License for more details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU General Public License |
20
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace Dolibarr\Code\Loan\Classes; |
24
|
|
|
|
25
|
|
|
use Dolibarr\Core\Base\CommonObject; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* \file htdocs/loan/class/paymentloan.class.php |
29
|
|
|
* \ingroup loan |
30
|
|
|
* \brief File of class to manage payment of loans |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class to manage payments of loans |
35
|
|
|
*/ |
36
|
|
|
class PaymentLoan extends CommonObject |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var string ID to identify managed object |
40
|
|
|
*/ |
41
|
|
|
public $element = 'payment_loan'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string Name of table without prefix where object is stored |
45
|
|
|
*/ |
46
|
|
|
public $table_element = 'payment_loan'; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string String with name of icon for PaymentLoan |
50
|
|
|
*/ |
51
|
|
|
public $picto = 'money-bill-alt'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var int Loan ID |
55
|
|
|
*/ |
56
|
|
|
public $fk_loan; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @var string Create date |
60
|
|
|
*/ |
61
|
|
|
public $datec = ''; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @var string Payment date |
65
|
|
|
*/ |
66
|
|
|
public $datep = ''; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @var array<float|int> Array of amounts |
70
|
|
|
*/ |
71
|
|
|
public $amounts = array(); |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @var float|int Total amount of payment |
75
|
|
|
*/ |
76
|
|
|
public $amount_capital; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @var float|int |
80
|
|
|
*/ |
81
|
|
|
public $amount_insurance; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @var float|int |
85
|
|
|
*/ |
86
|
|
|
public $amount_interest; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @var int Payment mode ID |
90
|
|
|
*/ |
91
|
|
|
public $fk_typepayment; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @var string Payment reference |
95
|
|
|
* (Cheque or bank transfer reference. Can be "ABC123") |
96
|
|
|
*/ |
97
|
|
|
public $num_payment; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var int Bank ID |
101
|
|
|
*/ |
102
|
|
|
public $fk_bank; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var int User ID |
106
|
|
|
*/ |
107
|
|
|
public $fk_user_creat; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var int user ID |
111
|
|
|
*/ |
112
|
|
|
public $fk_user_modif; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var string |
116
|
|
|
*/ |
117
|
|
|
public $type_code; |
118
|
|
|
/** |
119
|
|
|
* @var string |
120
|
|
|
*/ |
121
|
|
|
public $type_label; |
122
|
|
|
public $chid; |
123
|
|
|
/** |
124
|
|
|
* @var string |
125
|
|
|
*/ |
126
|
|
|
public $label; |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @var int |
130
|
|
|
*/ |
131
|
|
|
public $paymenttype; |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @var int |
135
|
|
|
*/ |
136
|
|
|
public $bank_account; |
137
|
|
|
public $bank_line; |
138
|
|
|
|
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Constructor |
142
|
|
|
* |
143
|
|
|
* @param DoliDB $db Database handler |
|
|
|
|
144
|
|
|
*/ |
145
|
|
|
public function __construct($db) |
146
|
|
|
{ |
147
|
|
|
$this->db = $db; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Create payment of loan into database. |
152
|
|
|
* Use this->amounts to have list of lines for the payment |
153
|
|
|
* |
154
|
|
|
* @param User $user User making payment |
|
|
|
|
155
|
|
|
* @return int Return integer <0 if KO, id of payment if OK |
156
|
|
|
*/ |
157
|
|
|
public function create($user) |
158
|
|
|
{ |
159
|
|
|
global $conf, $langs; |
160
|
|
|
|
161
|
|
|
$error = 0; |
162
|
|
|
|
163
|
|
|
$now = dol_now(); |
164
|
|
|
|
165
|
|
|
// Validate parameters |
166
|
|
|
if (!$this->datep) { |
167
|
|
|
$this->error = 'ErrorBadValueForParameter'; |
168
|
|
|
return -1; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
// Clean parameters |
172
|
|
|
if (isset($this->fk_loan)) { |
173
|
|
|
$this->fk_loan = (int) $this->fk_loan; |
174
|
|
|
} |
175
|
|
|
if (isset($this->amount_capital)) { |
176
|
|
|
$this->amount_capital = (float) price2num($this->amount_capital ? $this->amount_capital : 0); |
177
|
|
|
} |
178
|
|
|
if (isset($this->amount_insurance)) { |
179
|
|
|
$this->amount_insurance = (float) price2num($this->amount_insurance ? $this->amount_insurance : 0); |
180
|
|
|
} |
181
|
|
|
if (isset($this->amount_interest)) { |
182
|
|
|
$this->amount_interest = (float) price2num($this->amount_interest ? $this->amount_interest : 0); |
183
|
|
|
} |
184
|
|
|
if (isset($this->fk_typepayment)) { |
185
|
|
|
$this->fk_typepayment = (int) $this->fk_typepayment; |
186
|
|
|
} |
187
|
|
|
if (isset($this->num_payment)) { |
188
|
|
|
$this->num_payment = trim($this->num_payment); |
189
|
|
|
} |
190
|
|
|
if (isset($this->note_private)) { |
191
|
|
|
$this->note_private = trim($this->note_private); |
192
|
|
|
} |
193
|
|
|
if (isset($this->note_public)) { |
194
|
|
|
$this->note_public = trim($this->note_public); |
195
|
|
|
} |
196
|
|
|
if (isset($this->fk_bank)) { |
197
|
|
|
$this->fk_bank = (int) $this->fk_bank; |
198
|
|
|
} |
199
|
|
|
if (isset($this->fk_user_creat)) { |
200
|
|
|
$this->fk_user_creat = (int) $this->fk_user_creat; |
201
|
|
|
} |
202
|
|
|
if (isset($this->fk_user_modif)) { |
203
|
|
|
$this->fk_user_modif = (int) $this->fk_user_modif; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
$totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest; |
207
|
|
|
$totalamount = (float) price2num($totalamount); |
208
|
|
|
|
209
|
|
|
// Check parameters |
210
|
|
|
if ($totalamount == 0) { |
211
|
|
|
return -1; // Negative amounts are accepted for reject prelevement but not null |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
|
215
|
|
|
$this->db->begin(); |
216
|
|
|
|
217
|
|
|
if ($totalamount != 0) { |
218
|
|
|
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "payment_loan (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,"; |
219
|
|
|
$sql .= " fk_typepayment, num_payment, note_private, note_public, fk_user_creat, fk_bank)"; |
220
|
|
|
$sql .= " VALUES (" . $this->chid . ", '" . $this->db->idate($now) . "',"; |
221
|
|
|
$sql .= " '" . $this->db->idate($this->datep) . "',"; |
222
|
|
|
$sql .= " " . price2num($this->amount_capital) . ","; |
223
|
|
|
$sql .= " " . price2num($this->amount_insurance) . ","; |
224
|
|
|
$sql .= " " . price2num($this->amount_interest) . ","; |
225
|
|
|
$sql .= " " . ((int) $this->paymenttype) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note_private) . "', '" . $this->db->escape($this->note_public) . "', " . $user->id . ","; |
226
|
|
|
$sql .= " 0)"; |
227
|
|
|
|
228
|
|
|
dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
229
|
|
|
$resql = $this->db->query($sql); |
230
|
|
|
if ($resql) { |
231
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "payment_loan"); |
232
|
|
|
} else { |
233
|
|
|
$this->error = $this->db->lasterror(); |
234
|
|
|
$error++; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
if ($totalamount != 0 && !$error) { |
239
|
|
|
$this->amount_capital = $totalamount; |
240
|
|
|
$this->db->commit(); |
241
|
|
|
return $this->id; |
242
|
|
|
} else { |
243
|
|
|
$this->error = $this->db->lasterror(); |
244
|
|
|
$this->db->rollback(); |
245
|
|
|
return -1; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
/** |
250
|
|
|
* Load object in memory from database |
251
|
|
|
* |
252
|
|
|
* @param int $id Id object |
253
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
254
|
|
|
*/ |
255
|
|
|
public function fetch($id) |
256
|
|
|
{ |
257
|
|
|
global $langs; |
258
|
|
|
$sql = "SELECT"; |
259
|
|
|
$sql .= " t.rowid,"; |
260
|
|
|
$sql .= " t.fk_loan,"; |
261
|
|
|
$sql .= " t.datec,"; |
262
|
|
|
$sql .= " t.tms,"; |
263
|
|
|
$sql .= " t.datep,"; |
264
|
|
|
$sql .= " t.amount_capital,"; |
265
|
|
|
$sql .= " t.amount_insurance,"; |
266
|
|
|
$sql .= " t.amount_interest,"; |
267
|
|
|
$sql .= " t.fk_typepayment,"; |
268
|
|
|
$sql .= " t.num_payment,"; |
269
|
|
|
$sql .= " t.note_private,"; |
270
|
|
|
$sql .= " t.note_public,"; |
271
|
|
|
$sql .= " t.fk_bank,"; |
272
|
|
|
$sql .= " t.fk_user_creat,"; |
273
|
|
|
$sql .= " t.fk_user_modif,"; |
274
|
|
|
$sql .= " pt.code as type_code, pt.libelle as type_label,"; |
275
|
|
|
$sql .= ' b.fk_account'; |
276
|
|
|
$sql .= " FROM " . MAIN_DB_PREFIX . "payment_loan as t"; |
277
|
|
|
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepayment = pt.id"; |
278
|
|
|
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid'; |
279
|
|
|
$sql .= " WHERE t.rowid = " . ((int) $id); |
280
|
|
|
|
281
|
|
|
dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
282
|
|
|
$resql = $this->db->query($sql); |
283
|
|
|
if ($resql) { |
284
|
|
|
if ($this->db->num_rows($resql)) { |
285
|
|
|
$obj = $this->db->fetch_object($resql); |
286
|
|
|
|
287
|
|
|
$this->id = $obj->rowid; |
288
|
|
|
$this->ref = $obj->rowid; |
289
|
|
|
|
290
|
|
|
$this->fk_loan = $obj->fk_loan; |
291
|
|
|
$this->datec = $this->db->jdate($obj->datec); |
292
|
|
|
$this->tms = $this->db->jdate($obj->tms); |
|
|
|
|
293
|
|
|
$this->datep = $this->db->jdate($obj->datep); |
294
|
|
|
$this->amount_capital = $obj->amount_capital; |
295
|
|
|
$this->amount_insurance = $obj->amount_insurance; |
296
|
|
|
$this->amount_interest = $obj->amount_interest; |
297
|
|
|
$this->fk_typepayment = $obj->fk_typepayment; |
298
|
|
|
$this->num_payment = $obj->num_payment; |
299
|
|
|
$this->note_private = $obj->note_private; |
300
|
|
|
$this->note_public = $obj->note_public; |
301
|
|
|
$this->fk_bank = $obj->fk_bank; |
302
|
|
|
$this->fk_user_creat = $obj->fk_user_creat; |
303
|
|
|
$this->fk_user_modif = $obj->fk_user_modif; |
304
|
|
|
|
305
|
|
|
$this->type_code = $obj->type_code; |
306
|
|
|
$this->type_label = $obj->type_label; |
307
|
|
|
|
308
|
|
|
$this->bank_account = $obj->fk_account; |
309
|
|
|
$this->bank_line = $obj->fk_bank; |
310
|
|
|
} |
311
|
|
|
$this->db->free($resql); |
312
|
|
|
|
313
|
|
|
return 1; |
314
|
|
|
} else { |
315
|
|
|
$this->error = "Error " . $this->db->lasterror(); |
316
|
|
|
return -1; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
|
321
|
|
|
/** |
322
|
|
|
* Update database |
323
|
|
|
* |
324
|
|
|
* @param User $user User that modify |
325
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
326
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
327
|
|
|
*/ |
328
|
|
|
public function update($user = null, $notrigger = 0) |
329
|
|
|
{ |
330
|
|
|
global $conf, $langs; |
331
|
|
|
$error = 0; |
332
|
|
|
|
333
|
|
|
// Clean parameters |
334
|
|
|
if (isset($this->fk_loan)) { |
335
|
|
|
$this->fk_loan = (int) $this->fk_loan; |
336
|
|
|
} |
337
|
|
|
if (isset($this->amount_capital)) { |
338
|
|
|
$this->amount_capital = (float) $this->amount_capital; |
339
|
|
|
} |
340
|
|
|
if (isset($this->amount_insurance)) { |
341
|
|
|
$this->amount_insurance = (float) $this->amount_insurance; |
342
|
|
|
} |
343
|
|
|
if (isset($this->amount_interest)) { |
344
|
|
|
$this->amount_interest = (float) $this->amount_interest; |
345
|
|
|
} |
346
|
|
|
if (isset($this->fk_typepayment)) { |
347
|
|
|
$this->fk_typepayment = (int) $this->fk_typepayment; |
348
|
|
|
} |
349
|
|
|
if (isset($this->num_payment)) { |
350
|
|
|
$this->num_payment = trim($this->num_payment); |
351
|
|
|
} |
352
|
|
|
if (isset($this->note_private)) { |
353
|
|
|
$this->note = trim($this->note_private); |
354
|
|
|
} |
355
|
|
|
if (isset($this->note_public)) { |
356
|
|
|
$this->note = trim($this->note_public); |
357
|
|
|
} |
358
|
|
|
if (isset($this->fk_bank)) { |
359
|
|
|
$this->fk_bank = (int) $this->fk_bank; |
360
|
|
|
} |
361
|
|
|
if (isset($this->fk_user_creat)) { |
362
|
|
|
$this->fk_user_creat = (int) $this->fk_user_creat; |
363
|
|
|
} |
364
|
|
|
if (isset($this->fk_user_modif)) { |
365
|
|
|
$this->fk_user_modif = (int) $this->fk_user_modif; |
366
|
|
|
} |
367
|
|
|
|
368
|
|
|
// Check parameters |
369
|
|
|
|
370
|
|
|
// Update request |
371
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "payment_loan SET"; |
372
|
|
|
$sql .= " fk_loan=" . (isset($this->fk_loan) ? $this->fk_loan : "null") . ","; |
373
|
|
|
$sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ","; |
374
|
|
|
$sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ","; |
375
|
|
|
$sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ","; |
376
|
|
|
$sql .= " amount_capital=" . (isset($this->amount_capital) ? $this->amount_capital : "null") . ","; |
377
|
|
|
$sql .= " amount_insurance=" . (isset($this->amount_insurance) ? $this->amount_insurance : "null") . ","; |
378
|
|
|
$sql .= " amount_interest=" . (isset($this->amount_interest) ? $this->amount_interest : "null") . ","; |
379
|
|
|
$sql .= " fk_typepayment=" . (isset($this->fk_typepayment) ? $this->fk_typepayment : "null") . ","; |
380
|
|
|
$sql .= " num_payment=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ","; |
381
|
|
|
$sql .= " note_private=" . (isset($this->note_private) ? "'" . $this->db->escape($this->note_private) . "'" : "null") . ","; |
382
|
|
|
$sql .= " note_public=" . (isset($this->note_public) ? "'" . $this->db->escape($this->note_public) . "'" : "null") . ","; |
383
|
|
|
$sql .= " fk_bank=" . (isset($this->fk_bank) ? ((int) $this->fk_bank) : "null") . ","; |
384
|
|
|
$sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null") . ","; |
385
|
|
|
$sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null"); |
386
|
|
|
$sql .= " WHERE rowid=" . ((int) $this->id); |
387
|
|
|
|
388
|
|
|
$this->db->begin(); |
389
|
|
|
|
390
|
|
|
dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
391
|
|
|
$resql = $this->db->query($sql); |
392
|
|
|
if (!$resql) { |
393
|
|
|
$error++; |
394
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
// Commit or rollback |
398
|
|
|
if ($error) { |
399
|
|
|
foreach ($this->errors as $errmsg) { |
400
|
|
|
dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
401
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
402
|
|
|
} |
403
|
|
|
$this->db->rollback(); |
404
|
|
|
return -1 * $error; |
405
|
|
|
} else { |
406
|
|
|
$this->db->commit(); |
407
|
|
|
return 1; |
408
|
|
|
} |
409
|
|
|
} |
410
|
|
|
|
411
|
|
|
|
412
|
|
|
/** |
413
|
|
|
* Delete object in database |
414
|
|
|
* |
415
|
|
|
* @param User $user User that delete |
416
|
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers |
417
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
418
|
|
|
*/ |
419
|
|
|
public function delete($user, $notrigger = 0) |
420
|
|
|
{ |
421
|
|
|
global $conf, $langs; |
422
|
|
|
$error = 0; |
423
|
|
|
|
424
|
|
|
$this->db->begin(); |
425
|
|
|
|
426
|
|
|
if (!$error) { |
427
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_url"; |
428
|
|
|
$sql .= " WHERE type='payment_loan' AND url_id=" . ((int) $this->id); |
429
|
|
|
|
430
|
|
|
dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
431
|
|
|
$resql = $this->db->query($sql); |
432
|
|
|
if (!$resql) { |
433
|
|
|
$error++; |
434
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
if (!$error) { |
439
|
|
|
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_loan"; |
440
|
|
|
$sql .= " WHERE rowid=" . ((int) $this->id); |
441
|
|
|
|
442
|
|
|
dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
443
|
|
|
$resql = $this->db->query($sql); |
444
|
|
|
if (!$resql) { |
445
|
|
|
$error++; |
446
|
|
|
$this->errors[] = "Error " . $this->db->lasterror(); |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|
450
|
|
|
// Set loan unpaid if loan has no other payment |
451
|
|
|
if (!$error) { |
452
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/loan/class/loan.class.php'; |
453
|
|
|
$loan = new Loan($this->db); |
454
|
|
|
$loan->fetch($this->fk_loan); |
455
|
|
|
$sum_payment = $loan->getSumPayment(); |
456
|
|
|
if ($sum_payment == 0) { |
457
|
|
|
dol_syslog(get_class($this) . "::delete : set loan to unpaid", LOG_DEBUG); |
458
|
|
|
if ($loan->setUnpaid($user) < 1) { |
459
|
|
|
$error++; |
460
|
|
|
dol_print_error($this->db); |
461
|
|
|
} |
462
|
|
|
} |
463
|
|
|
} |
464
|
|
|
|
465
|
|
|
//if (! $error) |
466
|
|
|
//{ |
467
|
|
|
// if (! $notrigger) |
468
|
|
|
// { |
469
|
|
|
// Uncomment this and change MYOBJECT to your own tag if you |
470
|
|
|
// want this action call a trigger. |
471
|
|
|
|
472
|
|
|
//// Call triggers |
473
|
|
|
//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; |
474
|
|
|
//$interface=new Interfaces($this->db); |
475
|
|
|
//$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf); |
476
|
|
|
//if ($result < 0) { $error++; $this->errors=$interface->errors; } |
477
|
|
|
//// End call triggers |
478
|
|
|
// } |
479
|
|
|
//} |
480
|
|
|
|
481
|
|
|
// Commit or rollback |
482
|
|
|
if ($error) { |
483
|
|
|
foreach ($this->errors as $errmsg) { |
484
|
|
|
dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
485
|
|
|
$this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
486
|
|
|
} |
487
|
|
|
$this->db->rollback(); |
488
|
|
|
return -1 * $error; |
489
|
|
|
} else { |
490
|
|
|
$this->db->commit(); |
491
|
|
|
return 1; |
492
|
|
|
} |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
/** |
496
|
|
|
* Return the label of the status |
497
|
|
|
* |
498
|
|
|
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto |
499
|
|
|
* @return string Label of status |
500
|
|
|
*/ |
501
|
|
|
public function getLibStatut($mode = 0) |
502
|
|
|
{ |
503
|
|
|
return $this->LibStatut($this->statut, $mode); |
|
|
|
|
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
507
|
|
|
/** |
508
|
|
|
* Renvoi le libelle d'un statut donne |
509
|
|
|
* |
510
|
|
|
* @param int $status Statut |
511
|
|
|
* @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
512
|
|
|
* @return string Libelle du statut |
513
|
|
|
*/ |
514
|
|
|
public function LibStatut($status, $mode = 0) |
515
|
|
|
{ |
516
|
|
|
// phpcs:enable |
517
|
|
|
return ''; |
518
|
|
|
} |
519
|
|
|
|
520
|
|
|
/** |
521
|
|
|
* Add record into bank for payment with links between this bank record and invoices of payment. |
522
|
|
|
* All payment properties must have been set first like after a call to create(). |
523
|
|
|
* |
524
|
|
|
* @param User $user Object of user making payment |
525
|
|
|
* @param int $fk_loan Id of fk_loan to do link with this payment |
526
|
|
|
* @param string $mode 'payment_loan' |
527
|
|
|
* @param string $label Label to use in bank record |
528
|
|
|
* @param int $accountid Id of bank account to do link with |
529
|
|
|
* @param string $emetteur_nom Name of transmitter |
530
|
|
|
* @param string $emetteur_banque Name of bank |
531
|
|
|
* @return int Return integer <0 if KO, >0 if OK |
532
|
|
|
*/ |
533
|
|
|
public function addPaymentToBank($user, $fk_loan, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque) |
534
|
|
|
{ |
535
|
|
|
global $conf; |
536
|
|
|
|
537
|
|
|
$error = 0; |
538
|
|
|
$this->db->begin(); |
539
|
|
|
|
540
|
|
|
if (isModEnabled("bank")) { |
541
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/compta/bank/class/account.class.php'; |
542
|
|
|
|
543
|
|
|
$acc = new Account($this->db); |
|
|
|
|
544
|
|
|
$acc->fetch($accountid); |
545
|
|
|
|
546
|
|
|
$total = $this->amount_capital; |
547
|
|
|
if ($mode == 'payment_loan') { |
548
|
|
|
$total = -$total; |
549
|
|
|
} |
550
|
|
|
|
551
|
|
|
// Insert payment into llx_bank |
552
|
|
|
$bank_line_id = $acc->addline( |
553
|
|
|
$this->datep, |
554
|
|
|
$this->paymenttype, // Payment mode ID or code ("CHQ or VIR for example") it's integer in db |
555
|
|
|
$label, |
556
|
|
|
$total, |
557
|
|
|
$this->num_payment, |
558
|
|
|
'', |
559
|
|
|
$user, |
560
|
|
|
$emetteur_nom, |
561
|
|
|
$emetteur_banque |
562
|
|
|
); |
563
|
|
|
|
564
|
|
|
// Update fk_bank into llx_paiement. |
565
|
|
|
// We know the payment who generated the account write |
566
|
|
|
if ($bank_line_id > 0) { |
567
|
|
|
$result = $this->update_fk_bank($bank_line_id); |
568
|
|
|
if ($result <= 0) { |
569
|
|
|
$error++; |
570
|
|
|
dol_print_error($this->db); |
571
|
|
|
} |
572
|
|
|
|
573
|
|
|
// Add link 'payment_loan' in bank_url between payment and bank transaction |
574
|
|
|
$url = ''; |
575
|
|
|
if ($mode == 'payment_loan') { |
576
|
|
|
$url = constant('BASE_URL') . '/loan/payment/card.php?id='; |
577
|
|
|
} |
578
|
|
|
if ($url) { |
579
|
|
|
$result = $acc->add_url_line($bank_line_id, $this->id, $url, '(payment)', $mode); |
580
|
|
|
if ($result <= 0) { |
581
|
|
|
$error++; |
582
|
|
|
dol_print_error($this->db); |
583
|
|
|
} |
584
|
|
|
} |
585
|
|
|
|
586
|
|
|
|
587
|
|
|
// Add link 'loan' in bank_url between invoice and bank transaction (for each invoice concerned by payment) |
588
|
|
|
if ($mode == 'payment_loan') { |
589
|
|
|
$result = $acc->add_url_line($bank_line_id, $fk_loan, constant('BASE_URL') . '/loan/card.php?id=', ($this->label ? $this->label : ''), 'loan'); |
590
|
|
|
if ($result <= 0) { |
591
|
|
|
dol_print_error($this->db); |
592
|
|
|
} |
593
|
|
|
} |
594
|
|
|
} else { |
595
|
|
|
$this->error = $acc->error; |
596
|
|
|
$error++; |
597
|
|
|
} |
598
|
|
|
} |
599
|
|
|
|
600
|
|
|
|
601
|
|
|
// Set loan payment started if no set |
602
|
|
|
if (!$error) { |
603
|
|
|
require_once constant('DOL_DOCUMENT_ROOT') . '/loan/class/loan.class.php'; |
604
|
|
|
$loan = new Loan($this->db); |
605
|
|
|
$loan->fetch($fk_loan); |
606
|
|
|
if ($loan->paid == $loan::STATUS_UNPAID) { |
607
|
|
|
dol_syslog(get_class($this) . "::addPaymentToBank : set loan payment to started", LOG_DEBUG); |
608
|
|
|
if ($loan->setStarted($user) < 1) { |
609
|
|
|
$error++; |
610
|
|
|
dol_print_error($this->db); |
611
|
|
|
} |
612
|
|
|
} |
613
|
|
|
} |
614
|
|
|
|
615
|
|
|
if (!$error) { |
616
|
|
|
$this->db->commit(); |
617
|
|
|
return 1; |
618
|
|
|
} else { |
619
|
|
|
$this->db->rollback(); |
620
|
|
|
return -1; |
621
|
|
|
} |
622
|
|
|
} |
623
|
|
|
|
624
|
|
|
|
625
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps |
626
|
|
|
/** |
627
|
|
|
* Update link between loan's payment and the line generate in llx_bank |
628
|
|
|
* |
629
|
|
|
* @param int $id_bank Id if bank |
630
|
|
|
* @return int >0 if OK, <=0 if KO |
631
|
|
|
*/ |
632
|
|
|
public function update_fk_bank($id_bank) |
633
|
|
|
{ |
634
|
|
|
// phpcs:enable |
635
|
|
|
$sql = "UPDATE " . MAIN_DB_PREFIX . "payment_loan SET fk_bank = " . ((int) $id_bank) . " WHERE rowid = " . ((int) $this->id); |
636
|
|
|
|
637
|
|
|
dol_syslog(get_class($this) . "::update_fk_bank", LOG_DEBUG); |
638
|
|
|
$result = $this->db->query($sql); |
639
|
|
|
if ($result) { |
640
|
|
|
$this->fk_bank = ((int) $id_bank); |
641
|
|
|
return 1; |
642
|
|
|
} else { |
643
|
|
|
$this->error = $this->db->error(); |
644
|
|
|
return 0; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
/** |
649
|
|
|
* Return clicable name (with eventually a picto) |
650
|
|
|
* |
651
|
|
|
* @param int $withpicto 0=No picto, 1=Include picto into link, 2=No picto |
652
|
|
|
* @param int $maxlen Max length label |
653
|
|
|
* @param int $notooltip 1=Disable tooltip |
654
|
|
|
* @param string $moretitle Add more text to title tooltip |
655
|
|
|
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
656
|
|
|
* @return string String with URL |
657
|
|
|
*/ |
658
|
|
|
public function getNomUrl($withpicto = 0, $maxlen = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1) |
659
|
|
|
{ |
660
|
|
|
global $langs, $conf, $hookmanager; |
661
|
|
|
|
662
|
|
|
if (!empty($conf->dol_no_mouse_hover)) { |
663
|
|
|
$notooltip = 1; // Force disable tooltips |
664
|
|
|
} |
665
|
|
|
|
666
|
|
|
$result = ''; |
667
|
|
|
$label = '<u>' . $langs->trans("Loan") . '</u>'; |
668
|
|
|
if (!empty($this->id)) { |
669
|
|
|
$label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->id; |
670
|
|
|
} |
671
|
|
|
if ($moretitle) { |
672
|
|
|
$label .= ' - ' . $moretitle; |
673
|
|
|
} |
674
|
|
|
|
675
|
|
|
$url = constant('BASE_URL') . '/loan/payment/card.php?id=' . $this->id; |
676
|
|
|
|
677
|
|
|
$add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0); |
678
|
|
|
if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { |
679
|
|
|
$add_save_lastsearch_values = 1; |
680
|
|
|
} |
681
|
|
|
if ($add_save_lastsearch_values) { |
682
|
|
|
$url .= '&save_lastsearch_values=1'; |
683
|
|
|
} |
684
|
|
|
|
685
|
|
|
$linkstart = '<a href="' . $url . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">'; |
686
|
|
|
$linkend = '</a>'; |
687
|
|
|
|
688
|
|
|
$result .= $linkstart; |
689
|
|
|
if ($withpicto) { |
690
|
|
|
$result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1); |
691
|
|
|
} |
692
|
|
|
if ($withpicto != 2) { |
693
|
|
|
$result .= $this->ref; |
694
|
|
|
} |
695
|
|
|
$result .= $linkend; |
696
|
|
|
|
697
|
|
|
global $action; |
698
|
|
|
$hookmanager->initHooks(array($this->element . 'dao')); |
699
|
|
|
$parameters = array('id' => $this->id, 'getnomurl' => &$result); |
700
|
|
|
$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks |
701
|
|
|
if ($reshook > 0) { |
702
|
|
|
$result = $hookmanager->resPrint; |
703
|
|
|
} else { |
704
|
|
|
$result .= $hookmanager->resPrint; |
705
|
|
|
} |
706
|
|
|
return $result; |
707
|
|
|
} |
708
|
|
|
} |
709
|
|
|
|