dolibarr 25.0.0-alpha
don.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6 * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
7 * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
8 * Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
9 * Copyright (C) 2019-2026 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2021 Maxime DEMAREST <maxime@indelog.fr>
11 * Copyright (C) 2024-2026 MDW <mdeweerd@users.noreply.github.com>
12 * Copyright (C) 2026 Philippe Grand <philippe.grand@atoo-net.com>
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program. If not, see <https://www.gnu.org/licenses/>.
26 */
27
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
36
37
41class Don extends CommonObject
42{
43 use CommonPeople;
44
48 public $element = 'don';
49
53 public $table_element = 'don';
54
58 public $fk_element = 'fk_donation';
59
63 public $picto = 'donation';
64
68 public $date;
69
73 public $datec;
74
78 public $datem;
79
83 public $date_valid;
84
89 public $amount;
90
94 public $socid;
95
99 public $societe;
100
104 public $address;
105
109 public $zip;
110
114 public $town;
115
119 public $email;
120
124 public $phone;
125
129 public $phone_mobile;
130
134 public $mode_reglement;
135
139 public $mode_reglement_code;
140
144 public $public;
145
149 public $fk_project;
150
154 public $fk_typepayment;
155
160 public $num_payment;
161
165 public $modepaymentid = 0;
166
170 public $paid;
171
175 public $ip;
176
177 const STATUS_DRAFT = 0;
178 const STATUS_VALIDATED = 1;
179 const STATUS_PAID = 2;
180 const STATUS_CANCELED = -1;
181
182
188 public function __construct($db)
189 {
190 $this->db = $db;
191
192 $this->ismultientitymanaged = 1;
193 }
194
195
202 public function getLibStatut($mode = 0)
203 {
204 return $this->LibStatut($this->status, $mode);
205 }
206
207 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
215 public function LibStatut($status, $mode = 0)
216 {
217 // phpcs:enable
218 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
219 global $langs;
220 $langs->load("donations");
221 $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
222 $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
223 $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
224 $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
225 $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
226 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
227 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
228 $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
229 }
230
231 $statusType = 'status'.$status;
232 if ($status == self::STATUS_CANCELED) {
233 $statusType = 'status9';
234 }
235 if ($status == self::STATUS_PAID) {
236 $statusType = 'status6';
237 }
238
239 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
240 }
241
242
250 public function initAsSpecimen()
251 {
252 global $conf;
253
254 $now = dol_now();
255
256 // Load array of company ids socids
257 $socids = array();
258
259 $sql = "SELECT rowid";
260 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
261 $sql .= " WHERE client IN (1, 3)";
262 $sql .= " AND entity = ".((int) $conf->entity);
263 $sql .= " LIMIT 10";
264
265 $resql = $this->db->query($sql);
266 if ($resql) {
267 $num_socs = $this->db->num_rows($resql);
268 $i = 0;
269 while ($i < $num_socs) {
270 $row = $this->db->fetch_row($resql);
271 $socids[$i] = $row[0];
272
273 $i++;
274 }
275 }
276
277 // Initialise parameters
278 $this->id = 0;
279 $this->ref = 'SPECIMEN';
280 $this->specimen = 1;
281 $this->lastname = 'Doe';
282 $this->firstname = 'John';
283 $this->socid = empty($socids[0]) ? 0 : $socids[0];
284 $this->date = $now;
285 $this->date_valid = $now;
286 $this->amount = 100.90;
287 $this->public = 1;
288 $this->societe = 'The Company';
289 $this->address = 'Twist road';
290 $this->zip = '99999';
291 $this->town = 'Town';
292 $this->note_private = 'Private note';
293 $this->note_public = 'Public note';
294 $this->email = 'email@email.com';
295 $this->phone = '0123456789';
296 $this->phone_mobile = '0606060606';
297 $this->status = 1;
298
299 return 1;
300 }
301
302
310 public function check($minimum = 0)
311 {
312 global $langs;
313 $langs->load('main');
314 $langs->load('companies');
315
316 $error_string = array();
317 $err = 0;
318 $amount_invalid = 0;
319
320 if (dol_strlen(trim((string) $this->societe)) == 0) {
321 if ((dol_strlen(trim((string) $this->lastname)) + dol_strlen(trim((string) $this->firstname))) == 0) {
322 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
323 $err++;
324 }
325 }
326
327 if (dol_strlen(trim((string) $this->address)) == 0) {
328 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
329 $err++;
330 }
331
332 if (dol_strlen(trim((string) $this->zip)) == 0) {
333 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
334 $err++;
335 }
336
337 if (dol_strlen(trim((string) $this->town)) == 0) {
338 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
339 $err++;
340 }
341
342 if (dol_strlen(trim((string) $this->email)) == 0) {
343 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
344 $err++;
345 }
346
347 $this->amount = (float) $this->amount;
348
349 $map = range(0, 9);
350 $len = dol_strlen((string) $this->amount);
351 for ($i = 0; $i < $len; $i++) {
352 if (!isset($map[substr((string) $this->amount, $i, 1)])) {
353 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
354 $err++;
355 $amount_invalid = 1;
356 break;
357 }
358 }
359
360 if (!$amount_invalid) {
361 if ($this->amount == 0) {
362 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
363 $err++;
364 } else {
365 if ($this->amount < $minimum && $minimum > 0) {
366 $error_string[] = $langs->trans('ErrorMinimumAmount', $minimum);
367 $err++;
368 }
369 }
370 }
371
372 if ($err) {
373 $this->errors = $error_string;
374 return 0;
375 } else {
376 return 1;
377 }
378 }
379
388 public function create($user, $notrigger = 0)
389 {
390 global $conf, $langs;
391
392 $error = 0;
393 $ret = 0;
394 $now = dol_now();
395
396 // Clean parameters
397 // $this->address = ($this->address > 0 ? $this->address : $this->address);
398 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
399 // $this->town = ($this->town > 0 ? $this->town : $this->town);
400 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
401 // $this->country = ($this->country ? $this->country : $this->country);
402 $this->amount = (float) price2num($this->amount);
403
404 // Check parameters
405 if ($this->amount < 0) {
406 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
407 return -1;
408 }
409
410 $this->db->begin();
411
412 $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
413 $sql .= "datec";
414 $sql .= ", entity";
415 $sql .= ", amount";
416 $sql .= ", fk_payment";
417 $sql .= ", fk_soc";
418 $sql .= ", firstname";
419 $sql .= ", lastname";
420 $sql .= ", societe";
421 $sql .= ", address";
422 $sql .= ", zip";
423 $sql .= ", town";
424 $sql .= ", fk_country";
425 $sql .= ", public";
426 $sql .= ", fk_projet";
427 $sql .= ", note_private";
428 $sql .= ", note_public";
429 $sql .= ", fk_user_author";
430 $sql .= ", fk_user_valid";
431 $sql .= ", datedon";
432 $sql .= ", email";
433 $sql .= ", phone";
434 $sql .= ", phone_mobile";
435 $sql .= ", ip";
436 $sql .= ") VALUES (";
437 $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
438 $sql .= ", ".((int) $conf->entity);
439 $sql .= ", ".((float) $this->amount);
440 $sql .= ", ".($this->modepaymentid ? ((int) $this->modepaymentid) : "null");
441 $sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : "null");
442 $sql .= ", '".$this->db->escape($this->firstname)."'";
443 $sql .= ", '".$this->db->escape($this->lastname)."'";
444 $sql .= ", '".$this->db->escape($this->societe)."'";
445 $sql .= ", '".$this->db->escape((string) $this->address)."'";
446 $sql .= ", '".$this->db->escape((string) $this->zip)."'";
447 $sql .= ", '".$this->db->escape((string) $this->town)."'";
448 $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
449 $sql .= ", ".(int) $this->public;
450 $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
451 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
452 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
453 $sql .= ", ".((int) $user->id);
454 $sql .= ", null";
455 $sql .= ", '".$this->db->idate($this->date)."'";
456 $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
457 $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
458 $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
459 $sql .= ", '".(!empty($this->ip) ? $this->db->escape($this->ip) : "null")."'";
460 $sql .= ")";
461
462 $resql = $this->db->query($sql);
463 if ($resql) {
464 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
465 $ret = $this->id;
466
467 if (!$notrigger) {
468 // Call trigger
469 $result = $this->call_trigger('DON_CREATE', $user);
470 if ($result < 0) {
471 $error++;
472 }
473 // End call triggers
474 }
475 } else {
476 $this->error = $this->db->lasterror();
477 $error++;
478 }
479
480 // Update extrafield
481 if (!$error) {
482 $result = $this->insertExtraFields();
483 if ($result < 0) {
484 $error++;
485 }
486 }
487
488 //if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
489 //$res = $this->setValid($user);
490 //if ($res < 0) $error++;
491 //}
492
493 if (!$error) {
494 $this->db->commit();
495 return $ret;
496 } else {
497 $this->db->rollback();
498 return -1;
499 }
500 }
501
509 public function update($user, $notrigger = 0)
510 {
511 global $langs;
512
513 $error = 0;
514
515 // Clean parameters
516 // $this->address = ($this->address > 0 ? $this->address : $this->address);
517 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
518 // $this->town = ($this->town > 0 ? $this->town : $this->town);
519 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
520 // $this->country = ($this->country ? $this->country : $this->country);
521 $this->amount = (float) price2num($this->amount);
522
523 // Check parameters
524 if ($this->amount < 0) {
525 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
526 return -1;
527 }
528
529 $this->db->begin();
530
531 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
532 $sql .= " amount = ".((float) $this->amount);
533 $sql .= ", fk_payment = ".($this->modepaymentid ? ((int) $this->modepaymentid) : "null");
534 $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
535 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
536 $sql .= ", societe='".$this->db->escape($this->societe)."'";
537 $sql .= ", address='".$this->db->escape((string) $this->address)."'";
538 $sql .= ", zip='".$this->db->escape((string) $this->zip)."'";
539 $sql .= ", town='".$this->db->escape((string) $this->town)."'";
540 $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
541 $sql .= ", public=".((int) $this->public);
542 $sql .= ", fk_projet=".($this->fk_project > 0 ? ((int) $this->fk_project) : 'null');
543 $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
544 $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
545 $sql .= ", datedon='".$this->db->idate($this->date)."'";
546 $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
547 $sql .= ", email='".$this->db->escape(trim((string) $this->email))."'";
548 $sql .= ", phone='".$this->db->escape(trim((string) $this->phone))."'";
549 $sql .= ", phone_mobile='".$this->db->escape(trim((string) $this->phone_mobile))."'";
550 $sql .= ", fk_statut=".((int) $this->status);
551 $sql .= " WHERE rowid = ".((int) $this->id);
552
553 dol_syslog(get_class($this)."::Update", LOG_DEBUG);
554 $resql = $this->db->query($sql);
555 if ($resql) {
556 if (!$notrigger) {
557 // Call trigger
558 $result = $this->call_trigger('DON_MODIFY', $user);
559 if ($result < 0) {
560 $error++;
561 }
562 // End call triggers
563 }
564
565 // Update extrafield
566 if (!$error) {
567 $result = $this->insertExtraFields();
568 if ($result < 0) {
569 $error++;
570 }
571 }
572
573 if (!$error) {
574 $this->db->commit();
575 $result = 1;
576 } else {
577 $this->db->rollback();
578 $result = -1;
579 }
580 } else {
581 $this->error = $this->db->lasterror();
582 $this->errors[] = $this->error;
583 $this->db->rollback();
584 dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
585 $result = -2;
586 }
587 return $result;
588 }
589
597 public function delete($user, $notrigger = 0)
598 {
599 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
600
601 $error = 0;
602
603 $this->db->begin();
604
605 if (!$notrigger) {
606 // Call trigger
607 $result = $this->call_trigger('DON_DELETE', $user);
608
609 if ($result < 0) {
610 $error++;
611 }
612 // End call triggers
613 }
614
615 // Delete donation
616 if (!$error) {
617 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
618 $sql .= " WHERE fk_object = ".((int) $this->id);
619
620 $resql = $this->db->query($sql);
621 if (!$resql) {
622 $this->errors[] = $this->db->lasterror();
623 $error++;
624 }
625 }
626
627 if (!$error) {
628 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
629 $sql .= " WHERE rowid=".((int) $this->id);
630
631 $resql = $this->db->query($sql);
632 if (!$resql) {
633 $this->errors[] = $this->db->lasterror();
634 $error++;
635 } else {
636 // we delete file with dol_delete_dir_recursive
637 $this->deleteEcmFiles(1);
638
639 $dir = DOL_DATA_ROOT.'/'.$this->element.'/'.$this->ref;
640 // For remove dir
641 if (dol_is_dir($dir)) {
642 if (!dol_delete_dir_recursive($dir)) {
643 $this->errors[] = 'ErrorFailToDeleteDir';
644 }
645 }
646 }
647 }
648
649 if (!$error) {
650 $this->db->commit();
651 return 1;
652 } else {
653 foreach ($this->errors as $errmsg) {
654 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
655 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
656 }
657 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
658 $this->db->rollback();
659 return -1;
660 }
661 }
662
670 public function fetch($id, $ref = '')
671 {
672 $sql = "SELECT d.rowid, d.entity, d.datec, d.date_valid, d.tms as datem, d.datedon,";
673 $sql .= " d.fk_soc as socid, d.firstname, d.lastname, d.societe, d.amount, d.fk_statut as status, d.address, d.zip, d.town, ";
674 $sql .= " d.fk_country, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
675 $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
676 $sql .= " p.ref as project_ref,";
677 $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
678 $sql .= " c.code as country_code, c.label as country";
679 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
680 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
681 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
682 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
683 $sql .= " WHERE d.entity IN (".getEntity('donation').")";
684 if (!empty($id)) {
685 $sql .= " AND d.rowid=".((int) $id);
686 } elseif (!empty($ref)) {
687 $sql .= " AND d.ref='".$this->db->escape($ref)."'";
688 }
689
690 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
691 $resql = $this->db->query($sql);
692 if ($resql) {
693 if ($this->db->num_rows($resql)) {
694 $obj = $this->db->fetch_object($resql);
695
696 $this->id = $obj->rowid;
697 $this->entity = $obj->entity;
698 $this->ref = $obj->rowid;
699 $this->date_creation = $this->db->jdate($obj->datec);
700 $this->datec = $this->db->jdate($obj->datec);
701 $this->date_validation = $this->db->jdate($obj->date_valid);
702 $this->date_valid = $this->db->jdate($obj->date_valid);
703 $this->date_modification = $this->db->jdate($obj->datem);
704 $this->datem = $this->db->jdate($obj->datem);
705 $this->date = $this->db->jdate($obj->datedon);
706 $this->socid = $obj->socid;
707 $this->firstname = $obj->firstname;
708 $this->lastname = $obj->lastname;
709 $this->societe = $obj->societe;
710 $this->status = $obj->status;
711 $this->statut = $obj->status;
712 $this->address = $obj->address;
713 $this->zip = $obj->zip;
714 $this->town = $obj->town;
715 $this->country_id = $obj->fk_country;
716 $this->country_code = $obj->country_code;
717 $this->country = $obj->country;
718 $this->email = $obj->email;
719 $this->phone = $obj->phone;
720 $this->phone_mobile = $obj->phone_mobile;
721 $this->project = $obj->project_ref;
722 $this->fk_projet = $obj->fk_project; // deprecated
723 $this->fk_project = $obj->fk_project;
724 $this->public = $obj->public;
725 $this->mode_reglement_id = $obj->fk_payment;
726 $this->mode_reglement_code = $obj->payment_code;
727 $this->mode_reglement = $obj->payment_label;
728 $this->paid = $obj->paid;
729 $this->amount = $obj->amount;
730 $this->note_private = $obj->note_private;
731 $this->note_public = $obj->note_public;
732 $this->model_pdf = $obj->model_pdf;
733
734 // Retrieve all extrafield
735 // fetch optionals attributes and labels
736 $this->fetch_optionals();
737 }
738 return 1;
739 } else {
740 dol_print_error($this->db);
741 return -1;
742 }
743 }
744
752 public function setValid($user, $notrigger = 0)
753 {
754 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
755 return $this->valid_promesse($this->id, $user->id, $notrigger);
756 }
757
758 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
767 public function valid_promesse($id, $userid, $notrigger = 0)
768 {
769 // phpcs:enable
770 global $user;
771
772 $error = 0;
773
774 $this->db->begin();
775
776 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
777
778 $resql = $this->db->query($sql);
779 if ($resql) {
780 if ($this->db->affected_rows($resql)) {
781 if (!$notrigger) {
782 // Call trigger
783 $result = $this->call_trigger('DON_VALIDATE', $user);
784 if ($result < 0) {
785 $error++;
786 }
787 // End call triggers
788 }
789 }
790 } else {
791 $error++;
792 $this->error = $this->db->lasterror();
793 }
794
795 if (!$error) {
796 $this->status = 1;
797 $this->db->commit();
798 return 1;
799 } else {
800 $this->db->rollback();
801 return -1;
802 }
803 }
804
812 public function setPaid($id, $modepayment = 0)
813 {
814 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
815 if ($modepayment) {
816 $sql .= ", fk_payment = ".((int) $modepayment);
817 }
818 $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
819
820 $resql = $this->db->query($sql);
821 if ($resql) {
822 if ($this->db->affected_rows($resql)) {
823 $this->status = 2;
824 $this->paid = 1;
825 return 1;
826 } else {
827 return 0;
828 }
829 } else {
830 dol_print_error($this->db);
831 return -1;
832 }
833 }
834
835 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
843 public function set_cancel($id, $notrigger = 0)
844 {
845 // phpcs:enable
846 global $user;
847
848 $error = 0;
849
850 $this->db->begin();
851
852 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
853
854 $resql = $this->db->query($sql);
855 if ($resql) {
856 if ($this->db->affected_rows($resql)) {
857 $this->status = -1;
858
859 if (!$notrigger) {
860 // Call trigger
861 $result = $this->call_trigger('DON_CANCEL', $user);
862 if ($result < 0) {
863 $error++;
864 }
865 // End call triggers
866 }
867 } else {
868 $this->db->commit();
869 return 0;
870 }
871 } else {
872 $this->error = $this->db->error();
873 $error++;
874 }
875
876 if (!$error) {
877 $this->db->commit();
878 return 1;
879 } else {
880 $this->db->rollback();
881 return -1;
882 }
883 }
884
892 public function reopen($user, $notrigger = 0)
893 {
894 // Protection
895 if ($this->status != self::STATUS_CANCELED && $this->status != self::STATUS_PAID) {
896 return 0;
897 }
898 if ($this->statut == self::STATUS_PAID) {
899 $sql = "UPDATE " . MAIN_DB_PREFIX . "don SET paid = 0 WHERE rowid = " . ((int) $this->id);
900
901 $resql = $this->db->query($sql);
902 if ($resql) {
903 if ($this->db->affected_rows($resql)) {
904 $this->paid = 0;
905 } else {
906 dol_print_error($this->db);
907 return -1;
908 }
909 }
910 }
911
912 return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
913 }
914
915 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
922 public function sum_donations($param)
923 {
924 // phpcs:enable
925 global $conf;
926
927 $result = 0;
928
929 $sql = "SELECT sum(amount) as total";
930 $sql .= " FROM ".MAIN_DB_PREFIX."don";
931 $sql .= " WHERE fk_statut = ".((int) $param);
932 $sql .= " AND entity = ".((int) $conf->entity);
933
934 $resql = $this->db->query($sql);
935 if ($resql) {
936 $obj = $this->db->fetch_object($resql);
937 $result = $obj->total;
938 }
939
940 return $result;
941 }
942
948 public function loadStateBoard()
949 {
950 $this->nb = array();
951
952 $sql = "SELECT count(d.rowid) as nb";
953 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
954 $sql .= " WHERE d.fk_statut > 0";
955 $sql .= " AND d.entity IN (".getEntity('donation').")";
956
957 $resql = $this->db->query($sql);
958 if ($resql) {
959 while ($obj = $this->db->fetch_object($resql)) {
960 $this->nb["donations"] = $obj->nb;
961 }
962 $this->db->free($resql);
963 return 1;
964 } else {
965 dol_print_error($this->db);
966 $this->error = $this->db->error();
967 return -1;
968 }
969 }
970
980 public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
981 {
982 global $conf, $langs, $hookmanager;
983
984 if (!empty($conf->dol_no_mouse_hover)) {
985 $notooltip = 1; // Force disable tooltips
986 }
987
988 $result = '';
989 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
990 if (isset($this->status)) {
991 $label .= ' '.$this->getLibStatut(5);
992 }
993 if (!empty($this->id)) {
994 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
995 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
996 }
997 if ($moretitle) {
998 $label .= ' - '.$moretitle;
999 }
1000
1001 $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
1002
1003 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1004 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1005 $add_save_lastsearch_values = 1;
1006 }
1007 if ($add_save_lastsearch_values) {
1008 $url .= '&save_lastsearch_values=1';
1009 }
1010
1011 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1012 $linkend = '</a>';
1013
1014 $result .= $linkstart;
1015 if ($withpicto) {
1016 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1017 }
1018 if ($withpicto != 2) {
1019 $result .= $this->ref;
1020 }
1021 $result .= $linkend;
1022 global $action;
1023 $hookmanager->initHooks(array($this->element . 'dao'));
1024 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1025 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1026 if ($reshook > 0) {
1027 $result = $hookmanager->resPrint;
1028 } else {
1029 $result .= $hookmanager->resPrint;
1030 }
1031 return $result;
1032 }
1033
1040 public function info($id)
1041 {
1042 $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
1043 $sql .= ' d.tms as datem';
1044 $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
1045 $sql .= ' WHERE d.rowid = '.((int) $id);
1046
1047 dol_syslog(get_class($this).'::info', LOG_DEBUG);
1048 $result = $this->db->query($sql);
1049
1050 if ($result) {
1051 if ($this->db->num_rows($result)) {
1052 $obj = $this->db->fetch_object($result);
1053 $this->id = $obj->rowid;
1054
1055 $this->user_creation_id = $obj->fk_user_author;
1056 $this->user_validation_id = $obj->fk_user_valid;
1057 $this->date_creation = $this->db->jdate($obj->datec);
1058 $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
1059 }
1060 $this->db->free($result);
1061 } else {
1062 dol_print_error($this->db);
1063 }
1064 }
1065
1066
1077 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1078 {
1079 global $conf, $langs;
1080
1081 $langs->load("bills");
1082
1083 if (!dol_strlen($modele)) {
1084 $modele = 'html_cerfafr';
1085
1086 if ($this->model_pdf) {
1087 $modele = $this->model_pdf;
1088 } elseif (getDolGlobalString('DON_ADDON_MODEL')) {
1089 $modele = getDolGlobalString('DON_ADDON_MODEL');
1090 }
1091 }
1092
1093 //$modelpath = "core/modules/dons/";
1094
1095 // TODO Restore use of commonGenerateDocument instead of dedicated code here
1096 //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1097
1098 // Increase limit for PDF build
1099 $err = error_reporting();
1100 error_reporting(0);
1101 @set_time_limit(120);
1102 error_reporting($err);
1103
1104 $srctemplatepath = '';
1105
1106 // If selected modele is a filename template (then $modele="modelname:filename")
1107 $tmp = explode(':', $modele, 2);
1108 if (!empty($tmp[1])) {
1109 $modele = $tmp[0];
1110 $srctemplatepath = $tmp[1];
1111 }
1112
1113 // Search template files
1114 // The model name stored in llx_document_model already holds its prefix (html_cerfafr, pdf_xxx...),
1115 // but a model name without prefix is also accepted.
1116 $file = '';
1117 $filefound = 0;
1118 $modelebasename = preg_replace('/^(html|doc|pdf)_/', '', $modele);
1119 $dirmodels = array_merge(['/'], (array) $conf->modules_parts['models']);
1120 foreach ($dirmodels as $reldir) {
1121 foreach (array('html', 'doc', 'pdf') as $prefix) {
1122 if ($modelebasename != $modele && strpos($modele, $prefix.'_') !== 0) {
1123 continue; // The prefix is part of the model name, so do not load a template of another type
1124 }
1125 $file = dol_sanitizeFileName($prefix."_".$modelebasename.".modules.php");
1126
1127 // Verify the path for the module
1128 $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1129 if (file_exists($file)) {
1130 $filefound = 1;
1131 $modele = $prefix.'_'.$modelebasename; // Name of the class of the template
1132 break;
1133 }
1134 }
1135 if ($filefound) {
1136 break;
1137 }
1138 }
1139
1140 // Charge le modele
1141 if ($filefound) {
1142 require_once $file;
1143
1144 $object = $this;
1145
1146 $classname = $modele;
1147 $obj = new $classname($this->db);
1149 '@phan-var-force ModeleDon $obj';
1150
1151 // We save charset_output to restore it because write_file can change it if needed for
1152 // output format that does not support UTF8.
1153 $sav_charset_output = $outputlangs->charset_output;
1154 if ($obj->write_file($object, $outputlangs /*, $currency */) > 0) {
1155 $outputlangs->charset_output = $sav_charset_output;
1156
1157 // we delete preview files
1158 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1160
1161 // Index file in database, as commonGenerateDocument() does
1162 if (!empty($obj->result['fullpath'])) {
1163 // Check that the file exists, before indexing it (it does not exist if we create a PDF and auto delete the ODT file)
1164 if (dol_is_file($obj->result['fullpath'])) {
1165 $this->indexFile($obj->result['fullpath'], (empty($obj->update_main_doc_field) ? 0 : 1));
1166 }
1167 } else {
1168 dol_syslog('Method ->write_file was called on object '.get_class($obj).' and return a success but the return array ->result["fullpath"] was not set.', LOG_WARNING);
1169 }
1170
1171 return 1;
1172 } else {
1173 $outputlangs->charset_output = $sav_charset_output;
1174 dol_syslog("Erreur dans don_create");
1175 dol_print_error($this->db, $obj->error);
1176 return 0;
1177 }
1178 } else {
1179 print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1180 return 0;
1181 }
1182 }
1183
1192 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1193 {
1194 $tables = array(
1195 'don'
1196 );
1197
1198 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1199 }
1200
1206 public function getRemainToPay()
1207 {
1208 dol_syslog(__METHOD__, LOG_DEBUG);
1209
1210 if (empty($this->id)) {
1211 $this->error = 'Missing object id';
1212 $this->errors[] = $this->error;
1213 dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1214 return -1;
1215 }
1216
1217 $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1218 $resql = $this->db->query($sql);
1219 if (!$resql) {
1220 dol_print_error($this->db);
1221 return -2;
1222 } else {
1223 $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1224 return (float) ($this->amount - $sum_amount);
1225 }
1226 }
1227
1235 public function getKanbanView($option = '', $arraydata = null)
1236 {
1237 global $conf, $langs;
1238
1239 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1240
1241 $return = '<div class="box-flex-item box-flex-grow-zero">';
1242 $return .= '<div class="info-box info-box-sm">';
1243 $return .= '<span class="info-box-icon bg-infobox-action">';
1244 $return .= img_picto('', $this->picto);
1245 $return .= '</span>';
1246 $return .= '<div class="info-box-content">';
1247 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(1) . '</span>';
1248 if ($selected >= 0) {
1249 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1250 }
1251 if (isDolTms($this->date)) {
1252 $return .= ' &nbsp; | &nbsp; <span class="info-box-label">'.dol_print_date($this->date, 'day', 'tzuserrel').'</span>';
1253 }
1254 if (!empty($this->societe)) {
1255 $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1256 }
1257 if (!empty($this->amount)) {
1258 $return .= '<br><span class="info-box-label amount">'.price($this->amount, 1, $langs, 1, -1, -1, getDolCurrency()).'</span>';
1259 }
1260 if (isset($this->status)) {
1261 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1262 }
1263 $return .= '</div>';
1264 $return .= '</div>';
1265 $return .= '</div>';
1266 return $return;
1267 }
1268}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
indexFile($destfull, $update_main_doc_field)
Index a file into the ECM database.
deleteEcmFiles($mode=0)
Delete related files of object in database.
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Class to manage Dolibarr database access.
Class to manage donations.
Definition don.class.php:42
getLibStatut($mode=0)
Returns the donation status label (draft, valid, abandoned, paid)
__construct($db)
Constructor.
initAsSpecimen()
Initialise an instance with random values.
create($user, $notrigger=0)
Create donation record into database.
getRemainToPay()
Function to get remaining amount to pay for a donation.
check($minimum=0)
Check params and init ->errors array.
setValid($user, $notrigger=0)
Validate a intervention.
valid_promesse($id, $userid, $notrigger=0)
Validate a promise of donation.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
LibStatut($status, $mode=0)
Return the label of a given status.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
fetch($id, $ref='')
Load donation from database.
reopen($user, $notrigger=0)
Set cancel status.
sum_donations($param)
Sum of donations.
info($id)
Information on record.
update($user, $notrigger=0)
Update a donation record.
set_cancel($id, $notrigger=0)
Set donation to status cancelled.
loadStateBoard()
Load the indicators this->nb for the state board.
setPaid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
getNomUrl($withpicto=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clickable name (with picto eventually)
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:169
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as p label as s rowid as s nom as s email
Sender: Who sends the email ("Sender" has sent emails on behalf of "From").
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=0, $level=0)
Remove a directory $dir and its subdirectories (or only files and subdirectories)
dol_is_file($pathoffile)
Return if path is a file.
dol_is_dir($folder)
Test if filename is a directory.
dol_delete_preview($object)
Delete all preview files linked to object instance.
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0, $currencycode='')
Function that return a number with universal decimal format (decimal separator is '.
dol_strlen($string, $stringencoding='UTF-8', $binarycount=0)
Make a strlen call.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
dol_sanitizeFileName($str, $newstr='_', $unaccent=1, $includequotes=0, $allowdash=0)
Clean a string to use it as a file name.
getDolCurrency()
Return the main currency ('EUR', 'USD', ...)
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
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)
Definition html.lib.php:183
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as p p num_paiement as f pf amount as amount
Definition receipt.php:494
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
Definition receipt.php:492
div refaddress div address