dolibarr 19.0.4
paymentdonation.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2015 Alexandre Spangaro <aspangaro@open-dsi.fr>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
24require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
25
26
31{
35 public $element = 'payment_donation';
36
40 public $table_element = 'payment_donation';
41
45 public $picto = 'payment';
46
50 public $rowid;
51
55 public $fk_donation;
56
57 public $datec = '';
58
59 public $tms = '';
60
61 public $datep = '';
62
63 public $amount; // Total amount of payment
64
65 public $amounts = array(); // Array of amounts
66
67 public $fk_typepayment; // Payment mode ID
68 public $paymenttype; // Payment mode ID or Code. TODO Use only the code in this field.
69
70 public $num_payment;
71
75 public $fk_bank;
76
80 public $fk_user_creat;
81
85 public $fk_user_modif;
86
91 public $total;
92
93 public $type_code;
94 public $type_label;
95 public $chid;
96 public $datepaid;
97 public $bank_account;
98 public $bank_line;
99
103 public $ext_payment_id;
104
108 public $ext_payment_site;
109
115 public function __construct($db)
116 {
117 $this->db = $db;
118 }
119
128 public function create($user, $notrigger = false)
129 {
130 global $conf, $langs;
131
132 $error = 0;
133
134 $now = dol_now();
135
136 // Validate parameters
137 if (!$this->datep) {
138 $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
139 return -1;
140 }
141
142 // Clean parameters
143 if (isset($this->chid)) {
144 $this->chid = (int) $this->chid;
145 } elseif (isset($this->fk_donation)) {
146 // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
147 // (keep priority to chid property)
148 $this->chid = (int) $this->fk_donation;
149 }
150 if (isset($this->fk_donation)) {
151 $this->fk_donation = (int) $this->fk_donation;
152 }
153 if (isset($this->amount)) {
154 $this->amount = trim($this->amount);
155 }
156 if (isset($this->fk_typepayment)) {
157 $this->fk_typepayment = trim($this->fk_typepayment);
158 }
159 if (isset($this->num_payment)) {
160 $this->num_payment = trim($this->num_payment);
161 }
162 if (isset($this->note_public)) {
163 $this->note_public = trim($this->note_public);
164 }
165 if (isset($this->fk_bank)) {
166 $this->fk_bank = (int) $this->fk_bank;
167 }
168 if (isset($this->fk_user_creat)) {
169 $this->fk_user_creat = (int) $this->fk_user_creat;
170 }
171 if (isset($this->fk_user_modif)) {
172 $this->fk_user_modif = (int) $this->fk_user_modif;
173 }
174
175 $totalamount = 0;
176 foreach ($this->amounts as $key => $value) { // How payment is dispatch
177 $newvalue = price2num($value, 'MT');
178 $this->amounts[$key] = $newvalue;
179 $totalamount += $newvalue;
180 }
181 $totalamount = price2num($totalamount);
182
183 // Check parameters
184 if ($totalamount == 0) {
185 return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
186 }
187
188
189 $this->db->begin();
190
191 if ($totalamount != 0) {
192 $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_donation (fk_donation, datec, datep, amount,";
193 $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
194 $sql .= " fk_user_creat, fk_bank)";
195 $sql .= " VALUES (".((int) $this->chid).", '".$this->db->idate($now)."',";
196 $sql .= " '".$this->db->idate($this->datep)."',";
197 $sql .= " ".((float) price2num($totalamount)).",";
198 $sql .= " ".((int) $this->paymenttype).", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note_public)."', ";
199 $sql .= " ".($this->ext_payment_id ? "'".$this->db->escape($this->ext_payment_id)."'" : "null").", ".($this->ext_payment_site ? "'".$this->db->escape($this->ext_payment_site)."'" : "null").",";
200 $sql .= " ".((int) $user->id).", 0)";
201
202 dol_syslog(get_class($this)."::create", LOG_DEBUG);
203 $resql = $this->db->query($sql);
204 if ($resql) {
205 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_donation");
206 $this->ref = $this->id;
207 } else {
208 $error++;
209 }
210 }
211
212 if (!$error && !$notrigger) {
213 // Call triggers
214 $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
215 if ($result < 0) {
216 $error++;
217 }
218 // End call triggers
219 }
220
221 if ($totalamount != 0 && !$error) {
222 $this->amount = $totalamount;
223 $this->total = $totalamount; // deprecated
224 $this->db->commit();
225 return $this->id;
226 } else {
227 $this->error = $this->db->error();
228 $this->db->rollback();
229 return -1;
230 }
231 }
232
239 public function fetch($id)
240 {
241 global $langs;
242 $sql = "SELECT";
243 $sql .= " t.rowid,";
244 $sql .= " t.fk_donation,";
245 $sql .= " t.datec,";
246 $sql .= " t.tms,";
247 $sql .= " t.datep,";
248 $sql .= " t.amount,";
249 $sql .= " t.fk_typepayment,";
250 $sql .= " t.num_payment,";
251 $sql .= " t.note as note_public,";
252 $sql .= " t.fk_bank,";
253 $sql .= " t.fk_user_creat,";
254 $sql .= " t.fk_user_modif,";
255 $sql .= " pt.code as type_code, pt.libelle as type_label,";
256 $sql .= ' b.fk_account';
257 $sql .= " FROM ".MAIN_DB_PREFIX."payment_donation as t";
258 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
259 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
260 $sql .= " WHERE t.rowid = ".((int) $id);
261
262 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
263 $resql = $this->db->query($sql);
264 if ($resql) {
265 if ($this->db->num_rows($resql)) {
266 $obj = $this->db->fetch_object($resql);
267
268 $this->id = $obj->rowid;
269 $this->ref = $obj->rowid;
270
271 $this->fk_donation = $obj->fk_donation;
272 $this->datec = $this->db->jdate($obj->datec);
273 $this->tms = $this->db->jdate($obj->tms);
274 $this->datep = $this->db->jdate($obj->datep);
275 $this->amount = $obj->amount;
276 $this->fk_typepayment = $obj->fk_typepayment; // Id on type of payent
277 $this->paymenttype = $obj->fk_typepayment; // Id on type of payment. We should store the code into paymenttype.
278 $this->num_payment = $obj->num_payment;
279 $this->note_public = $obj->note_public;
280 $this->fk_bank = $obj->fk_bank;
281 $this->fk_user_creat = $obj->fk_user_creat;
282 $this->fk_user_modif = $obj->fk_user_modif;
283
284 $this->type_code = $obj->type_code;
285 $this->type_label = $obj->type_label;
286
287 $this->bank_account = $obj->fk_account;
288 $this->bank_line = $obj->fk_bank;
289 }
290 $this->db->free($resql);
291
292 return 1;
293 } else {
294 $this->error = "Error ".$this->db->lasterror();
295 return -1;
296 }
297 }
298
299
307 public function update($user, $notrigger = 0)
308 {
309 global $conf, $langs;
310 $error = 0;
311
312 // Clean parameters
313
314 if (isset($this->fk_donation)) {
315 $this->fk_donation = (int) $this->fk_donation;
316 }
317 if (isset($this->amount)) {
318 $this->amount = trim($this->amount);
319 }
320 if (isset($this->fk_typepayment)) {
321 $this->fk_typepayment = trim($this->fk_typepayment);
322 }
323 if (isset($this->num_payment)) {
324 $this->num_payment = trim($this->num_payment);
325 }
326 if (isset($this->note_public)) {
327 $this->note_public = trim($this->note_public);
328 }
329 if (isset($this->fk_bank)) {
330 $this->fk_bank = (int) $this->fk_bank;
331 }
332 if (isset($this->fk_user_creat)) {
333 $this->fk_user_creat = (int) $this->fk_user_creat;
334 }
335 if (isset($this->fk_user_modif)) {
336 $this->fk_user_modif = (int) $this->fk_user_modif;
337 }
338
339 // Check parameters
340 // Put here code to add control on parameters values
341
342 // Update request
343 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET";
344 $sql .= " fk_donation=".(isset($this->fk_donation) ? $this->fk_donation : "null").",";
345 $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
346 $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
347 $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
348 $sql .= " amount=".(isset($this->amount) ? $this->amount : "null").",";
349 $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
350 $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
351 $sql .= " note=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
352 $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
353 $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
354 $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
355 $sql .= " WHERE rowid=".(int) $this->id;
356
357 $this->db->begin();
358
359 dol_syslog(get_class($this)."::update", LOG_DEBUG);
360 $resql = $this->db->query($sql);
361 if (!$resql) {
362 $error++;
363 $this->errors[] = "Error ".$this->db->lasterror();
364 }
365
366 if (!$error) {
367 if (!$notrigger) {
368 if (!$error && !$notrigger) {
369 // Call triggers
370 $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
371 if ($result < 0) {
372 $error++;
373 }
374 // End call triggers
375 }
376 }
377 }
378
379 // Commit or rollback
380 if ($error) {
381 foreach ($this->errors as $errmsg) {
382 dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
383 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
384 }
385 $this->db->rollback();
386 return -1 * $error;
387 } else {
388 $this->db->commit();
389 return 1;
390 }
391 }
392
393
401 public function delete($user, $notrigger = 0)
402 {
403 global $conf, $langs;
404 $error = 0;
405
406 $this->db->begin();
407
408 if (!$error) {
409 $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
410 $sql .= " WHERE type='payment_donation' AND url_id=".(int) $this->id;
411
412 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
413 $resql = $this->db->query($sql);
414 if (!$resql) {
415 $error++;
416 $this->errors[] = "Error ".$this->db->lasterror();
417 }
418 }
419
420 if (!$error) {
421 $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_donation";
422 $sql .= " WHERE rowid=".((int) $this->id);
423
424 dol_syslog(get_class($this)."::delete", LOG_DEBUG);
425 $resql = $this->db->query($sql);
426 if (!$resql) {
427 $error++;
428 $this->errors[] = "Error ".$this->db->lasterror();
429 }
430 }
431
432 if (!$error) {
433 if (!$notrigger) {
434 if (!$error && !$notrigger) {
435 // Call triggers
436 $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
437 if ($result < 0) {
438 $error++;
439 }
440 // End call triggers
441 }
442 }
443 }
444
445 // Commit or rollback
446 if ($error) {
447 foreach ($this->errors as $errmsg) {
448 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
449 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
450 }
451 $this->db->rollback();
452 return -1 * $error;
453 } else {
454 $this->db->commit();
455 return 1;
456 }
457 }
458
459
460
468 public function createFromClone(User $user, $fromid)
469 {
470 $error = 0;
471
472 $object = new PaymentDonation($this->db);
473
474 $this->db->begin();
475
476 // Load source object
477 $object->fetch($fromid);
478 $object->id = 0;
479 $object->statut = 0;
480
481 // Clear fields
482 // ...
483
484 // Create clone
485 $object->context['createfromclone'] = 'createfromclone';
486 $result = $object->create($user);
487
488 // Other options
489 if ($result < 0) {
490 $this->error = $object->error;
491 $error++;
492 }
493
494 if (!$error) {
495 }
496
497 unset($object->context['createfromclone']);
498
499 // End
500 if (!$error) {
501 $this->db->commit();
502 return $object->id;
503 } else {
504 $this->db->rollback();
505 return -1;
506 }
507 }
508
509
516 public function getLibStatut($mode = 0)
517 {
518 return '';
519 }
520
521 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
529 public function LibStatut($status, $mode = 0)
530 {
531 // phpcs:enable
532 global $langs;
533
534 return '';
535 }
536
537
545 public function initAsSpecimen()
546 {
547 $this->id = 0;
548
549 $this->fk_donation = '';
550 $this->datec = '';
551 $this->tms = '';
552 $this->datep = '';
553 $this->amount = '';
554 $this->fk_typepayment = '';
555 $this->paymenttype = '';
556 $this->num_payment = '';
557 $this->note_public = '';
558 $this->fk_bank = '';
559 $this->fk_user_creat = '';
560 $this->fk_user_modif = '';
561 }
562
563
576 public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
577 {
578 global $conf;
579
580 $error = 0;
581
582 if (isModEnabled("banque")) {
583 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
584
585 $acc = new Account($this->db);
586 $acc->fetch($accountid);
587
589 if ($mode == 'payment_donation') {
590 $amount = $total;
591 }
592 // Insert payment into llx_bank
593 $bank_line_id = $acc->addline(
594 $this->datep,
595 $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
596 $label,
597 $amount,
598 $this->num_payment,
599 '',
600 $user,
601 $emetteur_nom,
602 $emetteur_banque
603 );
604
605 // Update fk_bank in llx_paiement.
606 // On connait ainsi le paiement qui a genere l'ecriture bancaire
607 if ($bank_line_id > 0) {
608 $result = $this->update_fk_bank($bank_line_id);
609 if ($result <= 0) {
610 $error++;
611 dol_print_error($this->db);
612 }
613
614 // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
615 $url = '';
616 if ($mode == 'payment_donation') {
617 $url = DOL_URL_ROOT.'/don/payment/card.php?rowid=';
618 }
619 if ($url) {
620 $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
621 if ($result <= 0) {
622 $error++;
623 dol_print_error($this->db);
624 }
625 }
626 } else {
627 $this->error = $acc->error;
628 $error++;
629 }
630 }
631
632 if (!$error) {
633 return 1;
634 } else {
635 return -1;
636 }
637 }
638
639
640 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
647 public function update_fk_bank($id_bank)
648 {
649 // phpcs:enable
650 $sql = "UPDATE ".MAIN_DB_PREFIX."payment_donation SET fk_bank = ".(int) $id_bank." WHERE rowid = ".(int) $this->id;
651
652 dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
653 $result = $this->db->query($sql);
654 if ($result) {
655 return 1;
656 } else {
657 $this->error = $this->db->error();
658 return 0;
659 }
660 }
661
669 public function getNomUrl($withpicto = 0, $maxlen = 0)
670 {
671 global $langs, $hookmanager;
672
673 $result = '';
674
675 $label = '<u>'.$langs->trans("DonationPayment").'</u>';
676 $label .= '<br>';
677 $label .= '<b>'.$langs->trans('Ref').':</b> '.$this->ref;
678
679 if (!empty($this->id)) {
680 $link = '<a href="'.DOL_URL_ROOT.'/don/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
681 $linkend = '</a>';
682
683 if ($withpicto) {
684 $result .= ($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
685 }
686 if ($withpicto && $withpicto != 2) {
687 $result .= ' ';
688 }
689 if ($withpicto != 2) {
690 $result .= $link.($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref).$linkend;
691 }
692 }
693
694 global $action;
695 $hookmanager->initHooks(array($this->element . 'dao'));
696 $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
697 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
698 if ($reshook > 0) {
699 $result = $hookmanager->resPrint;
700 } else {
701 $result .= $hookmanager->resPrint;
702 }
703 return $result;
704 }
705}
$object ref
Definition info.php:79
Class to manage bank accounts.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage payments of donations.
addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
Add record into bank for payment with links between this bank record and invoices of payment.
create($user, $notrigger=false)
Create payment of donation into database.
initAsSpecimen()
Initialise an instance with random values.
LibStatut($status, $mode=0)
Return the label of a given status.
update_fk_bank($id_bank)
Update link between the donation payment and the generated line in llx_bank.
__construct($db)
Constructor.
getLibStatut($mode=0)
Return the label of the status.
createFromClone(User $user, $fromid)
Load an object from its id and create a new one in database.
fetch($id)
Load object in memory from database.
update($user, $notrigger=0)
Update database.
getNomUrl($withpicto=0, $maxlen=0)
Return clicable name (with picto eventually)
Class to manage Dolibarr users.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_now($mode='auto')
Return date for now.
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...