dolibarr 22.0.5
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[] = $this->error;
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.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->ref = $obj->rowid;
697 $this->date_creation = $this->db->jdate($obj->datec);
698 $this->datec = $this->db->jdate($obj->datec);
699 $this->date_validation = $this->db->jdate($obj->date_valid);
700 $this->date_valid = $this->db->jdate($obj->date_valid);
701 $this->date_modification = $this->db->jdate($obj->datem);
702 $this->datem = $this->db->jdate($obj->datem);
703 $this->date = $this->db->jdate($obj->datedon);
704 $this->socid = $obj->socid;
705 $this->firstname = $obj->firstname;
706 $this->lastname = $obj->lastname;
707 $this->societe = $obj->societe;
708 $this->status = $obj->status;
709 $this->statut = $obj->status;
710 $this->address = $obj->address;
711 $this->zip = $obj->zip;
712 $this->town = $obj->town;
713 $this->country_id = $obj->fk_country;
714 $this->country_code = $obj->country_code;
715 $this->country = $obj->country;
716 $this->email = $obj->email;
717 $this->phone = $obj->phone;
718 $this->phone_mobile = $obj->phone_mobile;
719 $this->project = $obj->project_ref;
720 $this->fk_projet = $obj->fk_project; // deprecated
721 $this->fk_project = $obj->fk_project;
722 $this->public = $obj->public;
723 $this->mode_reglement_id = $obj->fk_payment;
724 $this->mode_reglement_code = $obj->payment_code;
725 $this->mode_reglement = $obj->payment_label;
726 $this->paid = $obj->paid;
727 $this->amount = $obj->amount;
728 $this->note_private = $obj->note_private;
729 $this->note_public = $obj->note_public;
730 $this->model_pdf = $obj->model_pdf;
731
732 // Retrieve all extrafield
733 // fetch optionals attributes and labels
734 $this->fetch_optionals();
735 }
736 return 1;
737 } else {
738 dol_print_error($this->db);
739 return -1;
740 }
741 }
742
750 public function setValid($user, $notrigger = 0)
751 {
752 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
753 return $this->valid_promesse($this->id, $user->id, $notrigger);
754 }
755
756 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
765 public function valid_promesse($id, $userid, $notrigger = 0)
766 {
767 // phpcs:enable
768 global $user;
769
770 $error = 0;
771
772 $this->db->begin();
773
774 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
775
776 $resql = $this->db->query($sql);
777 if ($resql) {
778 if ($this->db->affected_rows($resql)) {
779 if (!$notrigger) {
780 // Call trigger
781 $result = $this->call_trigger('DON_VALIDATE', $user);
782 if ($result < 0) {
783 $error++;
784 }
785 // End call triggers
786 }
787 }
788 } else {
789 $error++;
790 $this->error = $this->db->lasterror();
791 }
792
793 if (!$error) {
794 $this->status = 1;
795 $this->db->commit();
796 return 1;
797 } else {
798 $this->db->rollback();
799 return -1;
800 }
801 }
802
810 public function setPaid($id, $modepayment = 0)
811 {
812 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
813 if ($modepayment) {
814 $sql .= ", fk_payment = ".((int) $modepayment);
815 }
816 $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
817
818 $resql = $this->db->query($sql);
819 if ($resql) {
820 if ($this->db->affected_rows($resql)) {
821 $this->status = 2;
822 $this->paid = 1;
823 return 1;
824 } else {
825 return 0;
826 }
827 } else {
828 dol_print_error($this->db);
829 return -1;
830 }
831 }
832
833 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
840 public function set_cancel($id)
841 {
842 // phpcs:enable
843 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
844
845 $resql = $this->db->query($sql);
846 if ($resql) {
847 if ($this->db->affected_rows($resql)) {
848 $this->status = -1;
849 return 1;
850 } else {
851 return 0;
852 }
853 } else {
854 dol_print_error($this->db);
855 return -1;
856 }
857 }
858
866 public function reopen($user, $notrigger = 0)
867 {
868 // Protection
869 if ($this->status != self::STATUS_CANCELED && $this->status != self::STATUS_PAID) {
870 return 0;
871 }
872 if ($this->statut == self::STATUS_PAID) {
873 $sql = "UPDATE " . MAIN_DB_PREFIX . "don SET paid = 0 WHERE rowid = " . ((int) $this->id);
874
875 $resql = $this->db->query($sql);
876 if ($resql) {
877 if ($this->db->affected_rows($resql)) {
878 $this->paid = 0;
879 } else {
880 dol_print_error($this->db);
881 return -1;
882 }
883 }
884 }
885
886 return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
887 }
888
889 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
896 public function sum_donations($param)
897 {
898 // phpcs:enable
899 global $conf;
900
901 $result = 0;
902
903 $sql = "SELECT sum(amount) as total";
904 $sql .= " FROM ".MAIN_DB_PREFIX."don";
905 $sql .= " WHERE fk_statut = ".((int) $param);
906 $sql .= " AND entity = ".$conf->entity;
907
908 $resql = $this->db->query($sql);
909 if ($resql) {
910 $obj = $this->db->fetch_object($resql);
911 $result = $obj->total;
912 }
913
914 return $result;
915 }
916
922 public function loadStateBoard()
923 {
924 $this->nb = array();
925
926 $sql = "SELECT count(d.rowid) as nb";
927 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
928 $sql .= " WHERE d.fk_statut > 0";
929 $sql .= " AND d.entity IN (".getEntity('donation').")";
930
931 $resql = $this->db->query($sql);
932 if ($resql) {
933 while ($obj = $this->db->fetch_object($resql)) {
934 $this->nb["donations"] = $obj->nb;
935 }
936 $this->db->free($resql);
937 return 1;
938 } else {
939 dol_print_error($this->db);
940 $this->error = $this->db->error();
941 return -1;
942 }
943 }
944
954 public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
955 {
956 global $conf, $langs, $hookmanager;
957
958 if (!empty($conf->dol_no_mouse_hover)) {
959 $notooltip = 1; // Force disable tooltips
960 }
961
962 $result = '';
963 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
964 if (isset($this->status)) {
965 $label .= ' '.$this->getLibStatut(5);
966 }
967 if (!empty($this->id)) {
968 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
969 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
970 }
971 if ($moretitle) {
972 $label .= ' - '.$moretitle;
973 }
974
975 $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
976
977 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
978 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
979 $add_save_lastsearch_values = 1;
980 }
981 if ($add_save_lastsearch_values) {
982 $url .= '&save_lastsearch_values=1';
983 }
984
985 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
986 $linkend = '</a>';
987
988 $result .= $linkstart;
989 if ($withpicto) {
990 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
991 }
992 if ($withpicto != 2) {
993 $result .= $this->ref;
994 }
995 $result .= $linkend;
996 global $action;
997 $hookmanager->initHooks(array($this->element . 'dao'));
998 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
999 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1000 if ($reshook > 0) {
1001 $result = $hookmanager->resPrint;
1002 } else {
1003 $result .= $hookmanager->resPrint;
1004 }
1005 return $result;
1006 }
1007
1014 public function info($id)
1015 {
1016 $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
1017 $sql .= ' d.tms as datem';
1018 $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
1019 $sql .= ' WHERE d.rowid = '.((int) $id);
1020
1021 dol_syslog(get_class($this).'::info', LOG_DEBUG);
1022 $result = $this->db->query($sql);
1023
1024 if ($result) {
1025 if ($this->db->num_rows($result)) {
1026 $obj = $this->db->fetch_object($result);
1027 $this->id = $obj->rowid;
1028
1029 $this->user_creation_id = $obj->fk_user_author;
1030 $this->user_validation_id = $obj->fk_user_valid;
1031 $this->date_creation = $this->db->jdate($obj->datec);
1032 $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
1033 }
1034 $this->db->free($result);
1035 } else {
1036 dol_print_error($this->db);
1037 }
1038 }
1039
1040
1051 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1052 {
1053 global $conf, $langs;
1054
1055 $langs->load("bills");
1056
1057 if (!dol_strlen($modele)) {
1058 $modele = 'html_cerfafr';
1059
1060 if ($this->model_pdf) {
1061 $modele = $this->model_pdf;
1062 } elseif (getDolGlobalString('DON_ADDON_MODEL')) {
1063 $modele = getDolGlobalString('DON_ADDON_MODEL');
1064 }
1065 }
1066
1067 //$modelpath = "core/modules/dons/";
1068
1069 // TODO Restore use of commonGenerateDocument instead of dedicated code here
1070 //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1071
1072 // Increase limit for PDF build
1073 $err = error_reporting();
1074 error_reporting(0);
1075 @set_time_limit(120);
1076 error_reporting($err);
1077
1078 $srctemplatepath = '';
1079
1080 // If selected modele is a filename template (then $modele="modelname:filename")
1081 $tmp = explode(':', $modele, 2);
1082 if (!empty($tmp[1])) {
1083 $modele = $tmp[0];
1084 $srctemplatepath = $tmp[1];
1085 }
1086
1087 // Search template files
1088 $file = '';
1089 $classname = '';
1090 $filefound = 0;
1091 $dirmodels = array('/');
1092 if (is_array($conf->modules_parts['models'])) {
1093 $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1094 }
1095 foreach ($dirmodels as $reldir) {
1096 foreach (array('html', 'doc', 'pdf') as $prefix) {
1097 $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1098
1099 // Verify the path for the module
1100 $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1101 if (file_exists($file)) {
1102 $filefound = 1;
1103 $classname = $prefix.'_'.$modele;
1104 break;
1105 }
1106 }
1107 if ($filefound) {
1108 break;
1109 }
1110 }
1111
1112 // Charge le modele
1113 if ($filefound) {
1114 require_once $file;
1115
1116 $object = $this;
1117
1118 $classname = $modele;
1119 $obj = new $classname($this->db);
1121 '@phan-var-force ModeleDon $obj';
1122
1123 // We save charset_output to restore it because write_file can change it if needed for
1124 // output format that does not support UTF8.
1125 $sav_charset_output = $outputlangs->charset_output;
1126 if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1127 $outputlangs->charset_output = $sav_charset_output;
1128
1129 // we delete preview files
1130 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1132 return 1;
1133 } else {
1134 $outputlangs->charset_output = $sav_charset_output;
1135 dol_syslog("Erreur dans don_create");
1136 dol_print_error($this->db, $obj->error);
1137 return 0;
1138 }
1139 } else {
1140 print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1141 return 0;
1142 }
1143 }
1144
1153 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1154 {
1155 $tables = array(
1156 'don'
1157 );
1158
1159 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1160 }
1161
1167 public function getRemainToPay()
1168 {
1169 dol_syslog(__METHOD__, LOG_DEBUG);
1170
1171 if (empty($this->id)) {
1172 $this->error = 'Missing object id';
1173 $this->errors[] = $this->error;
1174 dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1175 return -1;
1176 }
1177
1178 $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1179 $resql = $this->db->query($sql);
1180 if (!$resql) {
1181 dol_print_error($this->db);
1182 return -2;
1183 } else {
1184 $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1185 return (float) ($this->amount - $sum_amount);
1186 }
1187 }
1188
1196 public function getKanbanView($option = '', $arraydata = null)
1197 {
1198 global $conf, $langs;
1199
1200 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1201
1202 $return = '<div class="box-flex-item box-flex-grow-zero">';
1203 $return .= '<div class="info-box info-box-sm">';
1204 $return .= '<span class="info-box-icon bg-infobox-action">';
1205 $return .= img_picto('', $this->picto);
1206 $return .= '</span>';
1207 $return .= '<div class="info-box-content">';
1208 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . $this->getNomUrl(1) . '</span>';
1209 if ($selected >= 0) {
1210 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1211 }
1212 if (isDolTms($this->date)) {
1213 $return .= ' &nbsp; | &nbsp; <span class="info-box-label">'.dol_print_date($this->date, 'day', 'tzuserrel').'</span>';
1214 }
1215 if (!empty($this->societe)) {
1216 $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1217 }
1218 if (!empty($this->amount)) {
1219 $return .= '<br><span class="info-box-label amount">'.price($this->amount, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
1220 }
1221 if (isset($this->status)) {
1222 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1223 }
1224 $return .= '</div>';
1225 $return .= '</div>';
1226 $return .= '</div>';
1227 return $return;
1228 }
1229}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:67
$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.
call_trigger($triggerName, $user)
Call trigger based on this instance.
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:171
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_dir($folder)
Test if filename is a directory.
dol_delete_preview($object)
Delete all preview files linked to object instance.
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.
dol_now($mode='auto')
Return date for now.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
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
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79