1 | <?php |
||
2 | /* Copyright (C) 2015-2017 Alexandre Spangaro <[email protected]> |
||
3 | * Copyright (C) 2018 Nicolas ZABOURI <[email protected]> |
||
4 | * Copyright (C) 2019 Alxarafe <[email protected]> |
||
5 | * |
||
6 | * This program is free software; you can redistribute it and/or modify |
||
7 | * it under the terms of the GNU General Public License as published by |
||
8 | * the Free Software Foundation; either version 3 of the License, or |
||
9 | * (at your option) any later version. |
||
10 | * |
||
11 | * This program is distributed in the hope that it will be useful, |
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
14 | * GNU General Public License for more details. |
||
15 | * |
||
16 | * You should have received a copy of the GNU General Public License |
||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
18 | */ |
||
19 | defined('BASE_PATH') or die('Single entry point through the index.php of the main folder'); |
||
20 | |||
21 | /** |
||
22 | * \file htdocs/expensereport/class/paymentexpensereport.class.php |
||
23 | * \ingroup Expense Report |
||
24 | * \brief File of class to manage payment of expense report |
||
25 | */ |
||
26 | require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php'; |
||
27 | |||
28 | /** \class PaymentExpenseReport |
||
29 | * \brief Class to manage payments of expense report |
||
30 | */ |
||
31 | class PaymentExpenseReport extends CommonObject |
||
32 | { |
||
33 | |||
34 | /** |
||
35 | * @var string ID to identify managed object |
||
36 | */ |
||
37 | public $element = 'payment_expensereport'; |
||
38 | |||
39 | /** |
||
40 | * @var string Name of table without prefix where object is stored |
||
41 | */ |
||
42 | public $table_element = 'payment_expensereport'; |
||
43 | |||
44 | /** |
||
45 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
46 | */ |
||
47 | public $picto = 'payment'; |
||
48 | |||
49 | /** |
||
50 | * @var int ID |
||
51 | */ |
||
52 | public $rowid; |
||
53 | |||
54 | /** |
||
55 | * @var int ID |
||
56 | */ |
||
57 | public $fk_expensereport; |
||
58 | public $datec = ''; |
||
59 | public $tms = ''; |
||
60 | public $datep = ''; |
||
61 | public $amount; // Total amount of payment |
||
62 | public $amounts = array(); // Array of amounts |
||
63 | |||
64 | /** |
||
65 | * @var int ID |
||
66 | */ |
||
67 | public $fk_typepayment; |
||
68 | public $num_payment; |
||
69 | |||
70 | /** |
||
71 | * @var int ID |
||
72 | */ |
||
73 | public $fk_bank; |
||
74 | |||
75 | /** |
||
76 | * @var int ID |
||
77 | */ |
||
78 | public $fk_user_creat; |
||
79 | |||
80 | /** |
||
81 | * @var int ID |
||
82 | */ |
||
83 | public $fk_user_modif; |
||
84 | //Unknow field |
||
85 | public $chid; |
||
86 | public $total; |
||
87 | |||
88 | /** |
||
89 | * Constructor |
||
90 | * |
||
91 | * @param DoliDB $db Database handler |
||
92 | */ |
||
93 | function __construct($db) |
||
94 | { |
||
95 | $this->db = $db; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Create payment of expense report into database. |
||
100 | * Use this->amounts to have list of lines for the payment |
||
101 | * |
||
102 | * @param User $user User making payment |
||
103 | * @return int <0 if KO, id of payment if OK |
||
104 | */ |
||
105 | function create($user) |
||
106 | { |
||
107 | global $conf, $langs; |
||
108 | |||
109 | $error = 0; |
||
110 | |||
111 | $now = dol_now(); |
||
112 | |||
113 | // Validate parameters |
||
114 | if (!$this->datepaid) { |
||
115 | $this->error = 'ErrorBadValueForParameterCreatePaymentExpenseReport'; |
||
116 | return -1; |
||
117 | } |
||
118 | |||
119 | // Clean parameters |
||
120 | if (isset($this->fk_expensereport)) { |
||
121 | $this->fk_expensereport = trim($this->fk_expensereport); |
||
0 ignored issues
–
show
|
|||
122 | } |
||
123 | if (isset($this->amount)) { |
||
124 | $this->amount = trim($this->amount); |
||
125 | } |
||
126 | if (isset($this->fk_typepayment)) { |
||
127 | $this->fk_typepayment = trim($this->fk_typepayment); |
||
0 ignored issues
–
show
The property
$fk_typepayment was declared of type integer , but trim($this->fk_typepayment) is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
128 | } |
||
129 | if (isset($this->num_payment)) { |
||
130 | $this->num_payment = trim($this->num_payment); |
||
131 | } |
||
132 | if (isset($this->note)) { |
||
133 | $this->note = trim($this->note); |
||
134 | } |
||
135 | if (isset($this->fk_bank)) { |
||
136 | $this->fk_bank = trim($this->fk_bank); |
||
137 | } |
||
138 | if (isset($this->fk_user_creat)) { |
||
139 | $this->fk_user_creat = trim($this->fk_user_creat); |
||
0 ignored issues
–
show
The property
$fk_user_creat was declared of type integer , but trim($this->fk_user_creat) is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
140 | } |
||
141 | if (isset($this->fk_user_modif)) { |
||
142 | $this->fk_user_modif = trim($this->fk_user_modif); |
||
143 | } |
||
144 | if (!empty($this->fk_expensereport)) { |
||
145 | $this->chid = $this->fk_expensereport; |
||
146 | } |
||
147 | |||
148 | $totalamount = 0; |
||
149 | foreach ($this->amounts as $key => $value) { // How payment is dispatch |
||
150 | $newvalue = price2num($value, 'MT'); |
||
151 | $this->amounts[$key] = $newvalue; |
||
152 | $totalamount += $newvalue; |
||
153 | } |
||
154 | $totalamount = price2num($totalamount); |
||
155 | |||
156 | // Check parameters |
||
157 | if ($totalamount == 0) { |
||
158 | return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null |
||
159 | } |
||
160 | |||
161 | $this->db->begin(); |
||
162 | |||
163 | if ($totalamount != 0) { |
||
164 | $sql = "INSERT INTO " . MAIN_DB_PREFIX . "payment_expensereport (fk_expensereport, datec, datep, amount,"; |
||
165 | $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)"; |
||
166 | $sql .= " VALUES ($this->chid, '" . $this->db->idate($now) . "',"; |
||
167 | $sql .= " '" . $this->db->idate($this->datepaid) . "',"; |
||
168 | $sql .= " " . $totalamount . ","; |
||
169 | $sql .= " " . $this->fk_typepayment . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note) . "', " . $user->id . ","; |
||
170 | $sql .= " 0)"; |
||
171 | |||
172 | dol_syslog(get_class($this) . "::create", LOG_DEBUG); |
||
173 | $resql = $this->db->query($sql); |
||
174 | if ($resql) { |
||
175 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "payment_expensereport"); |
||
176 | } else { |
||
177 | $error++; |
||
178 | } |
||
179 | } |
||
180 | |||
181 | if ($totalamount != 0 && !$error) { |
||
182 | $this->amount = $totalamount; |
||
183 | $this->db->commit(); |
||
184 | return $this->id; |
||
185 | } else { |
||
186 | $this->error = $this->db->error(); |
||
187 | $this->db->rollback(); |
||
188 | return -1; |
||
189 | } |
||
190 | } |
||
191 | |||
192 | /** |
||
193 | * Load object in memory from database |
||
194 | * |
||
195 | * @param int $id Id object |
||
196 | * @return int <0 if KO, >0 if OK |
||
197 | */ |
||
198 | function fetch($id) |
||
199 | { |
||
200 | global $langs; |
||
201 | $sql = "SELECT"; |
||
202 | $sql .= " t.rowid,"; |
||
203 | $sql .= " t.fk_expensereport,"; |
||
204 | $sql .= " t.datec,"; |
||
205 | $sql .= " t.tms,"; |
||
206 | $sql .= " t.datep,"; |
||
207 | $sql .= " t.amount,"; |
||
208 | $sql .= " t.fk_typepayment,"; |
||
209 | $sql .= " t.num_payment,"; |
||
210 | $sql .= " t.note,"; |
||
211 | $sql .= " t.fk_bank,"; |
||
212 | $sql .= " t.fk_user_creat,"; |
||
213 | $sql .= " t.fk_user_modif,"; |
||
214 | $sql .= " pt.code as type_code, pt.libelle as type_libelle,"; |
||
215 | $sql .= ' b.fk_account'; |
||
216 | $sql .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as t"; |
||
217 | $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepayment = pt.id"; |
||
218 | $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid'; |
||
219 | $sql .= " WHERE t.rowid = " . $id; |
||
220 | |||
221 | dol_syslog(get_class($this) . "::fetch", LOG_DEBUG); |
||
222 | $resql = $this->db->query($sql); |
||
223 | if ($resql) { |
||
224 | if ($this->db->num_rows($resql)) { |
||
225 | $obj = $this->db->fetch_object($resql); |
||
226 | |||
227 | $this->id = $obj->rowid; |
||
228 | $this->ref = $obj->rowid; |
||
229 | |||
230 | $this->fk_expensereport = $obj->fk_expensereport; |
||
231 | $this->datec = $this->db->jdate($obj->datec); |
||
232 | $this->tms = $this->db->jdate($obj->tms); |
||
233 | $this->datep = $this->db->jdate($obj->datep); |
||
234 | $this->amount = $obj->amount; |
||
235 | $this->fk_typepayment = $obj->fk_typepayment; |
||
236 | $this->num_payment = $obj->num_payment; |
||
237 | $this->note = $obj->note; |
||
238 | $this->fk_bank = $obj->fk_bank; |
||
239 | $this->fk_user_creat = $obj->fk_user_creat; |
||
240 | $this->fk_user_modif = $obj->fk_user_modif; |
||
241 | |||
242 | $this->type_code = $obj->type_code; |
||
243 | $this->type_libelle = $obj->type_libelle; |
||
244 | |||
245 | $this->bank_account = $obj->fk_account; |
||
246 | $this->bank_line = $obj->fk_bank; |
||
247 | } |
||
248 | $this->db->free($resql); |
||
249 | |||
250 | return 1; |
||
251 | } else { |
||
252 | $this->error = "Error " . $this->db->lasterror(); |
||
253 | return -1; |
||
254 | } |
||
255 | } |
||
256 | |||
257 | /** |
||
258 | * Update database |
||
259 | * |
||
260 | * @param User $user User that modify |
||
261 | * @param int $notrigger 0=launch triggers after, 1=disable triggers |
||
262 | * @return int <0 if KO, >0 if OK |
||
263 | */ |
||
264 | function update($user = null, $notrigger = 0) |
||
265 | { |
||
266 | global $conf, $langs; |
||
267 | $error = 0; |
||
268 | |||
269 | // Clean parameters |
||
270 | |||
271 | if (isset($this->fk_expensereport)) { |
||
272 | $this->fk_expensereport = trim($this->fk_expensereport); |
||
0 ignored issues
–
show
The property
$fk_expensereport was declared of type integer , but trim($this->fk_expensereport) is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
273 | } |
||
274 | if (isset($this->amount)) { |
||
275 | $this->amount = trim($this->amount); |
||
276 | } |
||
277 | if (isset($this->fk_typepayment)) { |
||
278 | $this->fk_typepayment = trim($this->fk_typepayment); |
||
0 ignored issues
–
show
The property
$fk_typepayment was declared of type integer , but trim($this->fk_typepayment) is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
279 | } |
||
280 | if (isset($this->num_payment)) { |
||
281 | $this->num_payment = trim($this->num_payment); |
||
282 | } |
||
283 | if (isset($this->note)) { |
||
284 | $this->note = trim($this->note); |
||
285 | } |
||
286 | if (isset($this->fk_bank)) { |
||
287 | $this->fk_bank = trim($this->fk_bank); |
||
288 | } |
||
289 | if (isset($this->fk_user_creat)) { |
||
290 | $this->fk_user_creat = trim($this->fk_user_creat); |
||
0 ignored issues
–
show
The property
$fk_user_creat was declared of type integer , but trim($this->fk_user_creat) is of type string . Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
291 | } |
||
292 | if (isset($this->fk_user_modif)) { |
||
293 | $this->fk_user_modif = trim($this->fk_user_modif); |
||
294 | } |
||
295 | |||
296 | |||
297 | // Check parameters |
||
298 | // Put here code to add control on parameters values |
||
299 | // Update request |
||
300 | $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_expensereport SET"; |
||
301 | |||
302 | $sql .= " fk_expensereport=" . (isset($this->fk_expensereport) ? $this->fk_expensereport : "null") . ","; |
||
303 | $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ","; |
||
304 | $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ","; |
||
305 | $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ","; |
||
306 | $sql .= " amount=" . (isset($this->amount) ? $this->amount : "null") . ","; |
||
307 | $sql .= " fk_typepayment=" . (isset($this->fk_typepayment) ? $this->fk_typepayment : "null") . ","; |
||
308 | $sql .= " num_payment=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ","; |
||
309 | $sql .= " note=" . (isset($this->note) ? "'" . $this->db->escape($this->note) . "'" : "null") . ","; |
||
310 | $sql .= " fk_bank=" . (isset($this->fk_bank) ? $this->fk_bank : "null") . ","; |
||
311 | $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? $this->fk_user_creat : "null") . ","; |
||
312 | $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? $this->fk_user_modif : "null") . ""; |
||
313 | |||
314 | |||
315 | $sql .= " WHERE rowid=" . $this->id; |
||
316 | |||
317 | $this->db->begin(); |
||
318 | |||
319 | dol_syslog(get_class($this) . "::update", LOG_DEBUG); |
||
320 | $resql = $this->db->query($sql); |
||
321 | if (!$resql) { |
||
322 | $error++; |
||
323 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
324 | } |
||
325 | |||
326 | if (!$error) { |
||
327 | if (!$notrigger) { |
||
328 | // Uncomment this and change MYOBJECT to your own tag if you |
||
329 | // want this action call a trigger. |
||
330 | //// Call triggers |
||
331 | //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; |
||
332 | //$interface=new Interfaces($this->db); |
||
333 | //$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf); |
||
334 | //if ($result < 0) { $error++; $this->errors=$interface->errors; } |
||
335 | //// End call triggers |
||
336 | } |
||
337 | } |
||
338 | |||
339 | // Commit or rollback |
||
340 | if ($error) { |
||
341 | foreach ($this->errors as $errmsg) { |
||
342 | dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR); |
||
343 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
344 | } |
||
345 | $this->db->rollback(); |
||
346 | return -1 * $error; |
||
347 | } else { |
||
348 | $this->db->commit(); |
||
349 | return 1; |
||
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 <0 if KO, >0 if OK |
||
359 | */ |
||
360 | function delete($user, $notrigger = 0) |
||
361 | { |
||
362 | global $conf, $langs; |
||
363 | $error = 0; |
||
364 | |||
365 | $this->db->begin(); |
||
366 | |||
367 | if (!$error) { |
||
368 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_url"; |
||
369 | $sql .= " WHERE type='payment_expensereport' AND url_id=" . $this->id; |
||
370 | |||
371 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
372 | $resql = $this->db->query($sql); |
||
373 | if (!$resql) { |
||
374 | $error++; |
||
375 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
376 | } |
||
377 | } |
||
378 | |||
379 | if (!$error) { |
||
380 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_expensereport"; |
||
381 | $sql .= " WHERE rowid=" . $this->id; |
||
382 | |||
383 | dol_syslog(get_class($this) . "::delete", LOG_DEBUG); |
||
384 | $resql = $this->db->query($sql); |
||
385 | if (!$resql) { |
||
386 | $error++; |
||
387 | $this->errors[] = "Error " . $this->db->lasterror(); |
||
388 | } |
||
389 | } |
||
390 | |||
391 | if (!$error) { |
||
392 | if (!$notrigger) { |
||
393 | // Uncomment this and change MYOBJECT to your own tag if you |
||
394 | // want this action call a trigger. |
||
395 | //// Call triggers |
||
396 | //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; |
||
397 | //$interface=new Interfaces($this->db); |
||
398 | //$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf); |
||
399 | //if ($result < 0) { $error++; $this->errors=$interface->errors; } |
||
400 | //// End call triggers |
||
401 | } |
||
402 | } |
||
403 | |||
404 | // Commit or rollback |
||
405 | if ($error) { |
||
406 | foreach ($this->errors as $errmsg) { |
||
407 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
408 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
409 | } |
||
410 | $this->db->rollback(); |
||
411 | return -1 * $error; |
||
412 | } else { |
||
413 | $this->db->commit(); |
||
414 | return 1; |
||
415 | } |
||
416 | } |
||
417 | |||
418 | /** |
||
419 | * Load an object from its id and create a new one in database |
||
420 | * |
||
421 | * @param int $fromid Id of object to clone |
||
422 | * @return int New id of clone |
||
423 | */ |
||
424 | function createFromClone($fromid) |
||
425 | { |
||
426 | global $user, $langs; |
||
427 | |||
428 | $error = 0; |
||
429 | |||
430 | $object = new PaymentExpenseReport($this->db); |
||
431 | |||
432 | $object->context['createfromclone'] = 'createfromclone'; |
||
433 | |||
434 | $this->db->begin(); |
||
435 | |||
436 | // Load source object |
||
437 | $object->fetch($fromid); |
||
438 | $object->id = 0; |
||
439 | $object->statut = 0; |
||
440 | |||
441 | // Clear fields |
||
442 | // ... |
||
443 | // Create clone |
||
444 | $result = $object->create($user); |
||
445 | |||
446 | // Other options |
||
447 | if ($result < 0) { |
||
448 | $this->error = $object->error; |
||
449 | $error++; |
||
450 | } |
||
451 | |||
452 | if (!$error) { |
||
453 | |||
454 | } |
||
455 | |||
456 | unset($this->context['createfromclone']); |
||
457 | |||
458 | // End |
||
459 | if (!$error) { |
||
460 | $this->db->commit(); |
||
461 | return $object->id; |
||
462 | } else { |
||
463 | $this->db->rollback(); |
||
464 | return -1; |
||
465 | } |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee) |
||
470 | * |
||
471 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long |
||
472 | * @return string Libelle |
||
473 | */ |
||
474 | function getLibStatut($mode = 0) |
||
475 | { |
||
476 | return ''; |
||
477 | } |
||
478 | |||
479 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
480 | /** |
||
481 | * Renvoi le libelle d'un statut donne |
||
482 | * |
||
483 | * @param int $statut Id statut |
||
484 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
485 | * @return string Libelle du statut |
||
486 | */ |
||
487 | function LibStatut($statut, $mode = 0) |
||
488 | { |
||
489 | // phpcs:enable |
||
490 | global $langs; |
||
491 | |||
492 | return ''; |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Initialise an instance with random values. |
||
497 | * Used to build previews or test instances. |
||
498 | * id must be 0 if object instance is a specimen. |
||
499 | * |
||
500 | * @return void |
||
501 | */ |
||
502 | function initAsSpecimen() |
||
503 | { |
||
504 | $this->id = 0; |
||
505 | |||
506 | $this->fk_expensereport = ''; |
||
507 | $this->datec = ''; |
||
508 | $this->tms = ''; |
||
509 | $this->datep = ''; |
||
510 | $this->amount = ''; |
||
511 | $this->fk_typepayment = ''; |
||
512 | $this->num_payment = ''; |
||
513 | $this->note = ''; |
||
514 | $this->fk_bank = ''; |
||
515 | $this->fk_user_creat = ''; |
||
516 | $this->fk_user_modif = ''; |
||
517 | } |
||
518 | |||
519 | /** |
||
520 | * Add record into bank for payment with links between this bank record and invoices of payment. |
||
521 | * All payment properties must have been set first like after a call to create(). |
||
522 | * |
||
523 | * @param User $user Object of user making payment |
||
524 | * @param string $mode 'payment_expensereport' |
||
525 | * @param string $label Label to use in bank record |
||
526 | * @param int $accountid Id of bank account to do link with |
||
527 | * @param string $emetteur_nom Name of transmitter |
||
528 | * @param string $emetteur_banque Name of bank |
||
529 | * @return int <0 if KO, >0 if OK |
||
530 | */ |
||
531 | function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque) |
||
532 | { |
||
533 | global $langs, $conf; |
||
534 | |||
535 | $error = 0; |
||
536 | |||
537 | if (!empty($conf->banque->enabled)) { |
||
538 | include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php'; |
||
539 | |||
540 | $acc = new Account($this->db); |
||
541 | $acc->fetch($accountid); |
||
542 | |||
543 | //Fix me field |
||
544 | $this->total = $this->amount; |
||
545 | $total = $this->total; |
||
546 | |||
547 | if ($mode == 'payment_expensereport') { |
||
548 | $amount = $total; |
||
549 | } |
||
550 | |||
551 | // Insert payment into llx_bank |
||
552 | $bank_line_id = $acc->addline( |
||
553 | $this->datepaid, $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example") |
||
0 ignored issues
–
show
|
|||
554 | $label, -$amount, $this->num_payment, '', $user, $emetteur_nom, $emetteur_banque |
||
555 | ); |
||
556 | |||
557 | // Update fk_bank in llx_paiement. |
||
558 | // On connait ainsi le paiement qui a genere l'ecriture bancaire |
||
559 | if ($bank_line_id > 0) { |
||
560 | $result = $this->update_fk_bank($bank_line_id); |
||
561 | if ($result <= 0) { |
||
562 | $error++; |
||
563 | dol_print_error($this->db); |
||
564 | } |
||
565 | |||
566 | // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction |
||
567 | $url = ''; |
||
568 | if ($mode == 'payment_expensereport') { |
||
569 | // $url = DOL_URL_ROOT . '/expensereport/payment/card.php?rowid='; |
||
570 | $url = BASE_URI . '?controller=expensereport/payment&method=card&rowid='; |
||
571 | } |
||
572 | if ($url) { |
||
573 | $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode); |
||
574 | if ($result <= 0) { |
||
575 | $error++; |
||
576 | dol_print_error($this->db); |
||
577 | } |
||
578 | } |
||
579 | |||
580 | // Add link 'user' in bank_url between user and bank transaction |
||
581 | if (!$error) { |
||
582 | foreach ($this->amounts as $key => $value) { // We should have always same user but we loop in case of. |
||
583 | if ($mode == 'payment_expensereport') { |
||
584 | $fuser = new User($this->db); |
||
585 | $fuser->fetch($key); |
||
586 | |||
587 | $result = $acc->add_url_line( |
||
588 | $bank_line_id, $fuser->id, |
||
589 | //DOL_URL_ROOT.'/user/card.php?id=', |
||
590 | BASE_URI . '?controller=user&method=card&id=', $fuser->getFullName($langs), 'user' |
||
591 | ); |
||
592 | if ($result <= 0) { |
||
593 | $this->error = $this->db->lasterror(); |
||
594 | dol_syslog(get_class($this) . '::addPaymentToBank ' . $this->error); |
||
595 | $error++; |
||
596 | } |
||
597 | } |
||
598 | } |
||
599 | } |
||
600 | } else { |
||
601 | $this->error = $acc->error; |
||
602 | $error++; |
||
603 | } |
||
604 | } |
||
605 | |||
606 | if (!$error) { |
||
607 | return 1; |
||
608 | } else { |
||
609 | return -1; |
||
610 | } |
||
611 | } |
||
612 | |||
613 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
614 | /** |
||
615 | * Update link between the expense report payment and the generated line in llx_bank |
||
616 | * |
||
617 | * @param int $id_bank Id if bank |
||
618 | * @return int >0 if OK, <=0 if KO |
||
619 | */ |
||
620 | function update_fk_bank($id_bank) |
||
621 | { |
||
622 | // phpcs:enable |
||
623 | $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_expensereport SET fk_bank = " . $id_bank . " WHERE rowid = " . $this->id; |
||
624 | |||
625 | dol_syslog(get_class($this) . "::update_fk_bank", LOG_DEBUG); |
||
626 | $result = $this->db->query($sql); |
||
627 | if ($result) { |
||
628 | return 1; |
||
629 | } else { |
||
630 | $this->error = $this->db->error(); |
||
631 | return 0; |
||
632 | } |
||
633 | } |
||
634 | |||
635 | /** |
||
636 | * Return clicable name (with picto eventually) |
||
637 | * |
||
638 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
639 | * @param int $maxlen Longueur max libelle |
||
640 | * @return string Chaine avec URL |
||
641 | */ |
||
642 | function getNomUrl($withpicto = 0, $maxlen = 0) |
||
643 | { |
||
644 | global $langs; |
||
645 | |||
646 | $result = ''; |
||
647 | |||
648 | if (empty($this->ref)) { |
||
649 | $this->ref = $this->lib; |
||
0 ignored issues
–
show
|
|||
650 | } |
||
651 | $label = $langs->trans("ShowPayment") . ': ' . $this->ref; |
||
652 | |||
653 | if (!empty($this->id)) { |
||
654 | $link = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">'; |
||
655 | $linkend = '</a>'; |
||
656 | |||
657 | if ($withpicto) { |
||
658 | $result .= ($link . img_object($label, 'payment', 'class="classfortooltip"') . $linkend . ' '); |
||
659 | } |
||
660 | if ($withpicto && $withpicto != 2) { |
||
661 | $result .= ' '; |
||
662 | } |
||
663 | if ($withpicto != 2) { |
||
664 | $result .= $link . ($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref) . $linkend; |
||
665 | } |
||
666 | } |
||
667 | |||
668 | return $result; |
||
669 | } |
||
670 | |||
671 | /** |
||
672 | * Tab information on object |
||
673 | * |
||
674 | * @param int $id Payment id |
||
675 | * @return void |
||
676 | */ |
||
677 | function info($id) |
||
678 | { |
||
679 | $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms'; |
||
680 | $sql .= ' FROM ' . MAIN_DB_PREFIX . 'payment_expensereport as e'; |
||
681 | $sql .= ' WHERE e.rowid = ' . $id; |
||
682 | |||
683 | dol_syslog(get_class($this) . '::info', LOG_DEBUG); |
||
684 | $result = $this->db->query($sql); |
||
685 | |||
686 | if ($result) { |
||
687 | if ($this->db->num_rows($result)) { |
||
688 | $obj = $this->db->fetch_object($result); |
||
689 | $this->id = $obj->rowid; |
||
690 | if ($obj->fk_user_creat) { |
||
691 | $cuser = new User($this->db); |
||
692 | $cuser->fetch($obj->fk_user_creat); |
||
693 | $this->user_creation = $cuser; |
||
694 | } |
||
695 | if ($obj->fk_user_modif) { |
||
696 | $muser = new User($this->db); |
||
697 | $muser->fetch($obj->fk_user_modif); |
||
698 | $this->user_modification = $muser; |
||
699 | } |
||
700 | $this->date_creation = $this->db->jdate($obj->datec); |
||
701 | $this->date_modification = $this->db->jdate($obj->tms); |
||
702 | } |
||
703 | $this->db->free($result); |
||
704 | } else { |
||
705 | dol_print_error($this->db); |
||
706 | } |
||
707 | } |
||
708 | } |
||
709 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.