dolibarr 24.0.0-beta
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-2025 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2021 Maxime DEMAREST <maxime@indelog.fr>
11 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
33require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
35
36
40class Don extends CommonObject
41{
42 use CommonPeople;
43
47 public $element = 'don';
48
52 public $table_element = 'don';
53
57 public $fk_element = 'fk_donation';
58
62 public $picto = 'donation';
63
67 public $date;
68
72 public $datec;
73
77 public $datem;
78
82 public $date_valid;
83
88 public $amount;
89
93 public $socid;
94
98 public $societe;
99
103 public $address;
104
108 public $zip;
109
113 public $town;
114
118 public $email;
119
123 public $phone;
124
128 public $phone_mobile;
129
133 public $mode_reglement;
134
138 public $mode_reglement_code;
139
143 public $public;
144
148 public $fk_project;
149
153 public $fk_typepayment;
154
159 public $num_payment;
160
164 public $modepaymentid = 0;
165
169 public $paid;
170
174 public $ip;
175
176 const STATUS_DRAFT = 0;
177 const STATUS_VALIDATED = 1;
178 const STATUS_PAID = 2;
179 const STATUS_CANCELED = -1;
180
181
187 public function __construct($db)
188 {
189 $this->db = $db;
190
191 $this->ismultientitymanaged = 1;
192 }
193
194
201 public function getLibStatut($mode = 0)
202 {
203 return $this->LibStatut($this->status, $mode);
204 }
205
206 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
214 public function LibStatut($status, $mode = 0)
215 {
216 // phpcs:enable
217 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
218 global $langs;
219 $langs->load("donations");
220 $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
221 $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
222 $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
223 $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
224 $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
225 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
226 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
227 $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
228 }
229
230 $statusType = 'status'.$status;
231 if ($status == self::STATUS_CANCELED) {
232 $statusType = 'status9';
233 }
234 if ($status == self::STATUS_PAID) {
235 $statusType = 'status6';
236 }
237
238 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
239 }
240
241
249 public function initAsSpecimen()
250 {
251 global $conf;
252
253 $now = dol_now();
254
255 // Charge tableau des id de societe socids
256 $socids = array();
257
258 $sql = "SELECT rowid";
259 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
260 $sql .= " WHERE client IN (1, 3)";
261 $sql .= " AND entity = ".$conf->entity;
262 $sql .= " LIMIT 10";
263
264 $resql = $this->db->query($sql);
265 if ($resql) {
266 $num_socs = $this->db->num_rows($resql);
267 $i = 0;
268 while ($i < $num_socs) {
269 $row = $this->db->fetch_row($resql);
270 $socids[$i] = $row[0];
271
272 $i++;
273 }
274 }
275
276 // Initialise parameters
277 $this->id = 0;
278 $this->ref = 'SPECIMEN';
279 $this->specimen = 1;
280 $this->lastname = 'Doe';
281 $this->firstname = 'John';
282 $this->socid = empty($socids[0]) ? 0 : $socids[0];
283 $this->date = $now;
284 $this->date_valid = $now;
285 $this->amount = 100.90;
286 $this->public = 1;
287 $this->societe = 'The Company';
288 $this->address = 'Twist road';
289 $this->zip = '99999';
290 $this->town = 'Town';
291 $this->note_private = 'Private note';
292 $this->note_public = 'Public note';
293 $this->email = 'email@email.com';
294 $this->phone = '0123456789';
295 $this->phone_mobile = '0606060606';
296 $this->status = 1;
297
298 return 1;
299 }
300
301
309 public function check($minimum = 0)
310 {
311 global $langs;
312 $langs->load('main');
313 $langs->load('companies');
314
315 $error_string = array();
316 $err = 0;
317 $amount_invalid = 0;
318
319 if (dol_strlen(trim($this->societe)) == 0) {
320 if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
321 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
322 $err++;
323 }
324 }
325
326 if (dol_strlen(trim($this->address)) == 0) {
327 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
328 $err++;
329 }
330
331 if (dol_strlen(trim($this->zip)) == 0) {
332 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
333 $err++;
334 }
335
336 if (dol_strlen(trim($this->town)) == 0) {
337 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
338 $err++;
339 }
340
341 if (dol_strlen(trim($this->email)) == 0) {
342 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
343 $err++;
344 }
345
346 $this->amount = (float) $this->amount;
347
348 $map = range(0, 9);
349 $len = dol_strlen((string) $this->amount);
350 for ($i = 0; $i < $len; $i++) {
351 if (!isset($map[substr((string) $this->amount, $i, 1)])) {
352 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
353 $err++;
354 $amount_invalid = 1;
355 break;
356 }
357 }
358
359 if (!$amount_invalid) {
360 if ($this->amount == 0) {
361 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
362 $err++;
363 } else {
364 if ($this->amount < $minimum && $minimum > 0) {
365 $error_string[] = $langs->trans('MinimumAmount', $minimum);
366 $err++;
367 }
368 }
369 }
370
371 if ($err) {
372 $this->errors = $error_string;
373 return 0;
374 } else {
375 return 1;
376 }
377 }
378
387 public function create($user, $notrigger = 0)
388 {
389 global $conf, $langs;
390
391 $error = 0;
392 $ret = 0;
393 $now = dol_now();
394
395 // Clean parameters
396 // $this->address = ($this->address > 0 ? $this->address : $this->address);
397 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
398 // $this->town = ($this->town > 0 ? $this->town : $this->town);
399 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
400 // $this->country = ($this->country ? $this->country : $this->country);
401 $this->amount = (float) price2num($this->amount);
402
403 // Check parameters
404 if ($this->amount < 0) {
405 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
406 return -1;
407 }
408
409 $this->db->begin();
410
411 $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
412 $sql .= "datec";
413 $sql .= ", entity";
414 $sql .= ", amount";
415 $sql .= ", fk_payment";
416 $sql .= ", fk_soc";
417 $sql .= ", firstname";
418 $sql .= ", lastname";
419 $sql .= ", societe";
420 $sql .= ", address";
421 $sql .= ", zip";
422 $sql .= ", town";
423 $sql .= ", fk_country";
424 $sql .= ", public";
425 $sql .= ", fk_projet";
426 $sql .= ", note_private";
427 $sql .= ", note_public";
428 $sql .= ", fk_user_author";
429 $sql .= ", fk_user_valid";
430 $sql .= ", datedon";
431 $sql .= ", email";
432 $sql .= ", phone";
433 $sql .= ", phone_mobile";
434 $sql .= ", ip";
435 $sql .= ") VALUES (";
436 $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
437 $sql .= ", ".((int) $conf->entity);
438 $sql .= ", ".((float) $this->amount);
439 $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
440 $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
441 $sql .= ", '".$this->db->escape($this->firstname)."'";
442 $sql .= ", '".$this->db->escape($this->lastname)."'";
443 $sql .= ", '".$this->db->escape($this->societe)."'";
444 $sql .= ", '".$this->db->escape($this->address)."'";
445 $sql .= ", '".$this->db->escape($this->zip)."'";
446 $sql .= ", '".$this->db->escape($this->town)."'";
447 $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
448 $sql .= ", ".(int) $this->public;
449 $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
450 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
451 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
452 $sql .= ", ".((int) $user->id);
453 $sql .= ", null";
454 $sql .= ", '".$this->db->idate($this->date)."'";
455 $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
456 $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
457 $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
458 $sql .= ", '".(!empty($this->ip) ? $this->db->escape($this->ip) : "null")."'";
459 $sql .= ")";
460
461 $resql = $this->db->query($sql);
462 if ($resql) {
463 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
464 $ret = $this->id;
465
466 if (!$notrigger) {
467 // Call trigger
468 $result = $this->call_trigger('DON_CREATE', $user);
469 if ($result < 0) {
470 $error++;
471 }
472 // End call triggers
473 }
474 } else {
475 $this->error = $this->db->lasterror();
476 $error++;
477 }
478
479 // Update extrafield
480 if (!$error) {
481 $result = $this->insertExtraFields();
482 if ($result < 0) {
483 $error++;
484 }
485 }
486
487 //if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
488 //$res = $this->setValid($user);
489 //if ($res < 0) $error++;
490 //}
491
492 if (!$error) {
493 $this->db->commit();
494 return $ret;
495 } else {
496 $this->db->rollback();
497 return -1;
498 }
499 }
500
508 public function update($user, $notrigger = 0)
509 {
510 global $langs;
511
512 $error = 0;
513
514 // Clean parameters
515 // $this->address = ($this->address > 0 ? $this->address : $this->address);
516 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
517 // $this->town = ($this->town > 0 ? $this->town : $this->town);
518 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
519 // $this->country = ($this->country ? $this->country : $this->country);
520 $this->amount = (float) price2num($this->amount);
521
522 // Check parameters
523 if ($this->amount < 0) {
524 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
525 return -1;
526 }
527
528 $this->db->begin();
529
530 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
531 $sql .= " amount = ".((float) $this->amount);
532 $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
533 $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
534 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
535 $sql .= ", societe='".$this->db->escape($this->societe)."'";
536 $sql .= ", address='".$this->db->escape($this->address)."'";
537 $sql .= ", zip='".$this->db->escape($this->zip)."'";
538 $sql .= ", town='".$this->db->escape($this->town)."'";
539 $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
540 $sql .= ", public=".((int) $this->public);
541 $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
542 $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
543 $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
544 $sql .= ", datedon='".$this->db->idate($this->date)."'";
545 $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
546 $sql .= ", email='".$this->db->escape(trim($this->email))."'";
547 $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
548 $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
549 $sql .= ", fk_statut=".((int) $this->status);
550 $sql .= " WHERE rowid = ".((int) $this->id);
551
552 dol_syslog(get_class($this)."::Update", LOG_DEBUG);
553 $resql = $this->db->query($sql);
554 if ($resql) {
555 if (!$notrigger) {
556 // Call trigger
557 $result = $this->call_trigger('DON_MODIFY', $user);
558 if ($result < 0) {
559 $error++;
560 }
561 // End call triggers
562 }
563
564 // Update extrafield
565 if (!$error) {
566 $result = $this->insertExtraFields();
567 if ($result < 0) {
568 $error++;
569 }
570 }
571
572 if (!$error) {
573 $this->db->commit();
574 $result = 1;
575 } else {
576 $this->db->rollback();
577 $result = -1;
578 }
579 } else {
580 $this->error = $this->db->lasterror();
581 $this->errors[] = $this->error;
582 $this->db->rollback();
583 dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
584 $result = -2;
585 }
586 return $result;
587 }
588
596 public function delete($user, $notrigger = 0)
597 {
598 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
599
600 $error = 0;
601
602 $this->db->begin();
603
604 if (!$notrigger) {
605 // Call trigger
606 $result = $this->call_trigger('DON_DELETE', $user);
607
608 if ($result < 0) {
609 $error++;
610 }
611 // End call triggers
612 }
613
614 // Delete donation
615 if (!$error) {
616 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
617 $sql .= " WHERE fk_object = ".((int) $this->id);
618
619 $resql = $this->db->query($sql);
620 if (!$resql) {
621 $this->errors[] = $this->db->lasterror();
622 $error++;
623 }
624 }
625
626 if (!$error) {
627 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
628 $sql .= " WHERE rowid=".((int) $this->id);
629
630 $resql = $this->db->query($sql);
631 if (!$resql) {
632 $this->errors[] = $this->db->lasterror();
633 $error++;
634 } else {
635 // we delete file with dol_delete_dir_recursive
636 $this->deleteEcmFiles(1);
637
638 $dir = DOL_DATA_ROOT.'/'.$this->element.'/'.$this->ref;
639 // For remove dir
640 if (dol_is_dir($dir)) {
641 if (!dol_delete_dir_recursive($dir)) {
642 $this->errors[] = 'ErrorFailToDeleteDir';
643 }
644 }
645 }
646 }
647
648 if (!$error) {
649 $this->db->commit();
650 return 1;
651 } else {
652 foreach ($this->errors as $errmsg) {
653 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
654 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
655 }
656 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
657 $this->db->rollback();
658 return -1;
659 }
660 }
661
669 public function fetch($id, $ref = '')
670 {
671 $sql = "SELECT d.rowid, d.entity, d.datec, d.date_valid, d.tms as datem, d.datedon,";
672 $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, ";
673 $sql .= " d.fk_country, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
674 $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
675 $sql .= " p.ref as project_ref,";
676 $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
677 $sql .= " c.code as country_code, c.label as country";
678 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
679 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
680 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
681 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
682 $sql .= " WHERE d.entity IN (".getEntity('donation').")";
683 if (!empty($id)) {
684 $sql .= " AND d.rowid=".((int) $id);
685 } elseif (!empty($ref)) {
686 $sql .= " AND d.ref='".$this->db->escape($ref)."'";
687 }
688
689 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
690 $resql = $this->db->query($sql);
691 if ($resql) {
692 if ($this->db->num_rows($resql)) {
693 $obj = $this->db->fetch_object($resql);
694
695 $this->id = $obj->rowid;
696 $this->entity = $obj->entity;
697 $this->ref = $obj->rowid;
698 $this->date_creation = $this->db->jdate($obj->datec);
699 $this->datec = $this->db->jdate($obj->datec);
700 $this->date_validation = $this->db->jdate($obj->date_valid);
701 $this->date_valid = $this->db->jdate($obj->date_valid);
702 $this->date_modification = $this->db->jdate($obj->datem);
703 $this->datem = $this->db->jdate($obj->datem);
704 $this->date = $this->db->jdate($obj->datedon);
705 $this->socid = $obj->socid;
706 $this->firstname = $obj->firstname;
707 $this->lastname = $obj->lastname;
708 $this->societe = $obj->societe;
709 $this->status = $obj->status;
710 $this->statut = $obj->status;
711 $this->address = $obj->address;
712 $this->zip = $obj->zip;
713 $this->town = $obj->town;
714 $this->country_id = $obj->fk_country;
715 $this->country_code = $obj->country_code;
716 $this->country = $obj->country;
717 $this->email = $obj->email;
718 $this->phone = $obj->phone;
719 $this->phone_mobile = $obj->phone_mobile;
720 $this->project = $obj->project_ref;
721 $this->fk_projet = $obj->fk_project; // deprecated
722 $this->fk_project = $obj->fk_project;
723 $this->public = $obj->public;
724 $this->mode_reglement_id = $obj->fk_payment;
725 $this->mode_reglement_code = $obj->payment_code;
726 $this->mode_reglement = $obj->payment_label;
727 $this->paid = $obj->paid;
728 $this->amount = $obj->amount;
729 $this->note_private = $obj->note_private;
730 $this->note_public = $obj->note_public;
731 $this->model_pdf = $obj->model_pdf;
732
733 // Retrieve all extrafield
734 // fetch optionals attributes and labels
735 $this->fetch_optionals();
736 }
737 return 1;
738 } else {
739 dol_print_error($this->db);
740 return -1;
741 }
742 }
743
751 public function setValid($user, $notrigger = 0)
752 {
753 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
754 return $this->valid_promesse($this->id, $user->id, $notrigger);
755 }
756
757 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
766 public function valid_promesse($id, $userid, $notrigger = 0)
767 {
768 // phpcs:enable
769 global $user;
770
771 $error = 0;
772
773 $this->db->begin();
774
775 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
776
777 $resql = $this->db->query($sql);
778 if ($resql) {
779 if ($this->db->affected_rows($resql)) {
780 if (!$notrigger) {
781 // Call trigger
782 $result = $this->call_trigger('DON_VALIDATE', $user);
783 if ($result < 0) {
784 $error++;
785 }
786 // End call triggers
787 }
788 }
789 } else {
790 $error++;
791 $this->error = $this->db->lasterror();
792 }
793
794 if (!$error) {
795 $this->status = 1;
796 $this->db->commit();
797 return 1;
798 } else {
799 $this->db->rollback();
800 return -1;
801 }
802 }
803
811 public function setPaid($id, $modepayment = 0)
812 {
813 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
814 if ($modepayment) {
815 $sql .= ", fk_payment = ".((int) $modepayment);
816 }
817 $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
818
819 $resql = $this->db->query($sql);
820 if ($resql) {
821 if ($this->db->affected_rows($resql)) {
822 $this->status = 2;
823 $this->paid = 1;
824 return 1;
825 } else {
826 return 0;
827 }
828 } else {
829 dol_print_error($this->db);
830 return -1;
831 }
832 }
833
834 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
841 public function set_cancel($id)
842 {
843 // phpcs:enable
844 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
845
846 $resql = $this->db->query($sql);
847 if ($resql) {
848 if ($this->db->affected_rows($resql)) {
849 $this->status = -1;
850 return 1;
851 } else {
852 return 0;
853 }
854 } else {
855 dol_print_error($this->db);
856 return -1;
857 }
858 }
859
867 public function reopen($user, $notrigger = 0)
868 {
869 // Protection
870 if ($this->status != self::STATUS_CANCELED && $this->status != self::STATUS_PAID) {
871 return 0;
872 }
873 if ($this->statut == self::STATUS_PAID) {
874 $sql = "UPDATE " . MAIN_DB_PREFIX . "don SET paid = 0 WHERE rowid = " . ((int) $this->id);
875
876 $resql = $this->db->query($sql);
877 if ($resql) {
878 if ($this->db->affected_rows($resql)) {
879 $this->paid = 0;
880 } else {
881 dol_print_error($this->db);
882 return -1;
883 }
884 }
885 }
886
887 return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
888 }
889
890 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
897 public function sum_donations($param)
898 {
899 // phpcs:enable
900 global $conf;
901
902 $result = 0;
903
904 $sql = "SELECT sum(amount) as total";
905 $sql .= " FROM ".MAIN_DB_PREFIX."don";
906 $sql .= " WHERE fk_statut = ".((int) $param);
907 $sql .= " AND entity = ".$conf->entity;
908
909 $resql = $this->db->query($sql);
910 if ($resql) {
911 $obj = $this->db->fetch_object($resql);
912 $result = $obj->total;
913 }
914
915 return $result;
916 }
917
923 public function loadStateBoard()
924 {
925 $this->nb = array();
926
927 $sql = "SELECT count(d.rowid) as nb";
928 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
929 $sql .= " WHERE d.fk_statut > 0";
930 $sql .= " AND d.entity IN (".getEntity('donation').")";
931
932 $resql = $this->db->query($sql);
933 if ($resql) {
934 while ($obj = $this->db->fetch_object($resql)) {
935 $this->nb["donations"] = $obj->nb;
936 }
937 $this->db->free($resql);
938 return 1;
939 } else {
940 dol_print_error($this->db);
941 $this->error = $this->db->error();
942 return -1;
943 }
944 }
945
955 public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
956 {
957 global $conf, $langs, $hookmanager;
958
959 if (!empty($conf->dol_no_mouse_hover)) {
960 $notooltip = 1; // Force disable tooltips
961 }
962
963 $result = '';
964 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
965 if (isset($this->status)) {
966 $label .= ' '.$this->getLibStatut(5);
967 }
968 if (!empty($this->id)) {
969 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
970 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
971 }
972 if ($moretitle) {
973 $label .= ' - '.$moretitle;
974 }
975
976 $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
977
978 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
979 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
980 $add_save_lastsearch_values = 1;
981 }
982 if ($add_save_lastsearch_values) {
983 $url .= '&save_lastsearch_values=1';
984 }
985
986 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
987 $linkend = '</a>';
988
989 $result .= $linkstart;
990 if ($withpicto) {
991 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
992 }
993 if ($withpicto != 2) {
994 $result .= $this->ref;
995 }
996 $result .= $linkend;
997 global $action;
998 $hookmanager->initHooks(array($this->element . 'dao'));
999 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1000 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1001 if ($reshook > 0) {
1002 $result = $hookmanager->resPrint;
1003 } else {
1004 $result .= $hookmanager->resPrint;
1005 }
1006 return $result;
1007 }
1008
1015 public function info($id)
1016 {
1017 $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
1018 $sql .= ' d.tms as datem';
1019 $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
1020 $sql .= ' WHERE d.rowid = '.((int) $id);
1021
1022 dol_syslog(get_class($this).'::info', LOG_DEBUG);
1023 $result = $this->db->query($sql);
1024
1025 if ($result) {
1026 if ($this->db->num_rows($result)) {
1027 $obj = $this->db->fetch_object($result);
1028 $this->id = $obj->rowid;
1029
1030 $this->user_creation_id = $obj->fk_user_author;
1031 $this->user_validation_id = $obj->fk_user_valid;
1032 $this->date_creation = $this->db->jdate($obj->datec);
1033 $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
1034 }
1035 $this->db->free($result);
1036 } else {
1037 dol_print_error($this->db);
1038 }
1039 }
1040
1041
1052 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1053 {
1054 global $conf, $langs;
1055
1056 $langs->load("bills");
1057
1058 if (!dol_strlen($modele)) {
1059 $modele = 'html_cerfafr';
1060
1061 if ($this->model_pdf) {
1062 $modele = $this->model_pdf;
1063 } elseif (getDolGlobalString('DON_ADDON_MODEL')) {
1064 $modele = getDolGlobalString('DON_ADDON_MODEL');
1065 }
1066 }
1067
1068 //$modelpath = "core/modules/dons/";
1069
1070 // TODO Restore use of commonGenerateDocument instead of dedicated code here
1071 //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1072
1073 // Increase limit for PDF build
1074 $err = error_reporting();
1075 error_reporting(0);
1076 @set_time_limit(120);
1077 error_reporting($err);
1078
1079 $srctemplatepath = '';
1080
1081 // If selected modele is a filename template (then $modele="modelname:filename")
1082 $tmp = explode(':', $modele, 2);
1083 if (!empty($tmp[1])) {
1084 $modele = $tmp[0];
1085 $srctemplatepath = $tmp[1];
1086 }
1087
1088 // Search template files
1089 $file = '';
1090 $classname = '';
1091 $filefound = 0;
1092 $dirmodels = array_merge(['/'], (array) $conf->modules_parts['models']);
1093 foreach ($dirmodels as $reldir) {
1094 foreach (array('html', 'doc', 'pdf') as $prefix) {
1095 $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1096
1097 // Verify the path for the module
1098 $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1099 if (file_exists($file)) {
1100 $filefound = 1;
1101 $classname = $prefix.'_'.$modele;
1102 break;
1103 }
1104 }
1105 if ($filefound) {
1106 break;
1107 }
1108 }
1109
1110 // Charge le modele
1111 if ($filefound) {
1112 require_once $file;
1113
1114 $object = $this;
1115
1116 $classname = $modele;
1117 $obj = new $classname($this->db);
1119 '@phan-var-force ModeleDon $obj';
1120
1121 // We save charset_output to restore it because write_file can change it if needed for
1122 // output format that does not support UTF8.
1123 $sav_charset_output = $outputlangs->charset_output;
1124 if ($obj->write_file($object, $outputlangs /*, $currency */) > 0) {
1125 $outputlangs->charset_output = $sav_charset_output;
1126
1127 // we delete preview files
1128 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1130 return 1;
1131 } else {
1132 $outputlangs->charset_output = $sav_charset_output;
1133 dol_syslog("Erreur dans don_create");
1134 dol_print_error($this->db, $obj->error);
1135 return 0;
1136 }
1137 } else {
1138 print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1139 return 0;
1140 }
1141 }
1142
1151 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1152 {
1153 $tables = array(
1154 'don'
1155 );
1156
1157 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1158 }
1159
1165 public function getRemainToPay()
1166 {
1167 dol_syslog(__METHOD__, LOG_DEBUG);
1168
1169 if (empty($this->id)) {
1170 $this->error = 'Missing object id';
1171 $this->errors[] = $this->error;
1172 dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1173 return -1;
1174 }
1175
1176 $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1177 $resql = $this->db->query($sql);
1178 if (!$resql) {
1179 dol_print_error($this->db);
1180 return -2;
1181 } else {
1182 $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1183 return (float) ($this->amount - $sum_amount);
1184 }
1185 }
1186
1194 public function getKanbanView($option = '', $arraydata = null)
1195 {
1196 global $conf, $langs;
1197
1198 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1199
1200 $return = '<div class="box-flex-item box-flex-grow-zero">';
1201 $return .= '<div class="info-box info-box-sm">';
1202 $return .= '<span class="info-box-icon bg-infobox-action">';
1203 $return .= img_picto('', $this->picto);
1204 $return .= '</span>';
1205 $return .= '<div class="info-box-content">';
1206 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(1) . '</span>';
1207 if ($selected >= 0) {
1208 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1209 }
1210 if (isDolTms($this->date)) {
1211 $return .= ' &nbsp; | &nbsp; <span class="info-box-label">'.dol_print_date($this->date, 'day', 'tzuserrel').'</span>';
1212 }
1213 if (!empty($this->societe)) {
1214 $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1215 }
1216 if (!empty($this->amount)) {
1217 $return .= '<br><span class="info-box-label amount">'.price($this->amount, 1, $langs, 1, -1, -1, getDolCurrency()).'</span>';
1218 }
1219 if (isset($this->status)) {
1220 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1221 }
1222 $return .= '</div>';
1223 $return .= '</div>';
1224 $return .= '</div>';
1225 return $return;
1226 }
1227}
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
$object ref
Definition info.php:90
Parent class of all other business classes (invoices, contracts, proposals, orders,...
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...
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:41
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)
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:168
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").
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
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_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.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
isDolTms($timestamp)
isDolTms check if a timestamp is valid.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
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).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
div refaddress div address
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:489
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:487