dolibarr 21.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-2024 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2021 Maxime DEMAREST <maxime@indelog.fr>
11 * Copyright (C) 2024 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
166 public $paid;
167
168 const STATUS_DRAFT = 0;
169 const STATUS_VALIDATED = 1;
170 const STATUS_PAID = 2;
171 const STATUS_CANCELED = -1;
172
173
179 public function __construct($db)
180 {
181 $this->db = $db;
182
183 $this->ismultientitymanaged = 1;
184 }
185
186
193 public function getLibStatut($mode = 0)
194 {
195 return $this->LibStatut($this->status, $mode);
196 }
197
198 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
206 public function LibStatut($status, $mode = 0)
207 {
208 // phpcs:enable
209 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
210 global $langs;
211 $langs->load("donations");
212 $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
213 $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
214 $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
215 $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
216 $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
217 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
218 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
219 $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
220 }
221
222 $statusType = 'status'.$status;
223 if ($status == self::STATUS_CANCELED) {
224 $statusType = 'status9';
225 }
226 if ($status == self::STATUS_PAID) {
227 $statusType = 'status6';
228 }
229
230 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
231 }
232
233
241 public function initAsSpecimen()
242 {
243 global $conf;
244
245 $now = dol_now();
246
247 // Charge tableau des id de societe socids
248 $socids = array();
249
250 $sql = "SELECT rowid";
251 $sql .= " FROM ".MAIN_DB_PREFIX."societe";
252 $sql .= " WHERE client IN (1, 3)";
253 $sql .= " AND entity = ".$conf->entity;
254 $sql .= " LIMIT 10";
255
256 $resql = $this->db->query($sql);
257 if ($resql) {
258 $num_socs = $this->db->num_rows($resql);
259 $i = 0;
260 while ($i < $num_socs) {
261 $row = $this->db->fetch_row($resql);
262 $socids[$i] = $row[0];
263
264 $i++;
265 }
266 }
267
268 // Initialise parameters
269 $this->id = 0;
270 $this->ref = 'SPECIMEN';
271 $this->specimen = 1;
272 $this->lastname = 'Doe';
273 $this->firstname = 'John';
274 $this->socid = empty($socids[0]) ? 0 : $socids[0];
275 $this->date = $now;
276 $this->date_valid = $now;
277 $this->amount = 100.90;
278 $this->public = 1;
279 $this->societe = 'The Company';
280 $this->address = 'Twist road';
281 $this->zip = '99999';
282 $this->town = 'Town';
283 $this->note_private = 'Private note';
284 $this->note_public = 'Public note';
285 $this->email = 'email@email.com';
286 $this->phone = '0123456789';
287 $this->phone_mobile = '0606060606';
288 $this->status = 1;
289
290 return 1;
291 }
292
293
301 public function check($minimum = 0)
302 {
303 global $langs;
304 $langs->load('main');
305 $langs->load('companies');
306
307 $error_string = array();
308 $err = 0;
309 $amount_invalid = 0;
310
311 if (dol_strlen(trim($this->societe)) == 0) {
312 if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
313 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
314 $err++;
315 }
316 }
317
318 if (dol_strlen(trim($this->address)) == 0) {
319 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
320 $err++;
321 }
322
323 if (dol_strlen(trim($this->zip)) == 0) {
324 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
325 $err++;
326 }
327
328 if (dol_strlen(trim($this->town)) == 0) {
329 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
330 $err++;
331 }
332
333 if (dol_strlen(trim($this->email)) == 0) {
334 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
335 $err++;
336 }
337
338 $this->amount = (float) $this->amount;
339
340 $map = range(0, 9);
341 $len = dol_strlen((string) $this->amount);
342 for ($i = 0; $i < $len; $i++) {
343 if (!isset($map[substr((string) $this->amount, $i, 1)])) {
344 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
345 $err++;
346 $amount_invalid = 1;
347 break;
348 }
349 }
350
351 if (!$amount_invalid) {
352 if ($this->amount == 0) {
353 $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
354 $err++;
355 } else {
356 if ($this->amount < $minimum && $minimum > 0) {
357 $error_string[] = $langs->trans('MinimumAmount', $minimum);
358 $err++;
359 }
360 }
361 }
362
363 if ($err) {
364 $this->errors = $error_string;
365 return 0;
366 } else {
367 return 1;
368 }
369 }
370
379 public function create($user, $notrigger = 0)
380 {
381 global $conf, $langs;
382
383 $error = 0;
384 $ret = 0;
385 $now = dol_now();
386
387 // Clean parameters
388 // $this->address = ($this->address > 0 ? $this->address : $this->address);
389 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
390 // $this->town = ($this->town > 0 ? $this->town : $this->town);
391 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
392 // $this->country = ($this->country ? $this->country : $this->country);
393 $this->amount = (float) price2num($this->amount);
394
395 // Check parameters
396 if ($this->amount < 0) {
397 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
398 return -1;
399 }
400
401 $this->db->begin();
402
403 $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
404 $sql .= "datec";
405 $sql .= ", entity";
406 $sql .= ", amount";
407 $sql .= ", fk_payment";
408 $sql .= ", fk_soc";
409 $sql .= ", firstname";
410 $sql .= ", lastname";
411 $sql .= ", societe";
412 $sql .= ", address";
413 $sql .= ", zip";
414 $sql .= ", town";
415 $sql .= ", fk_country";
416 $sql .= ", public";
417 $sql .= ", fk_projet";
418 $sql .= ", note_private";
419 $sql .= ", note_public";
420 $sql .= ", fk_user_author";
421 $sql .= ", fk_user_valid";
422 $sql .= ", datedon";
423 $sql .= ", email";
424 $sql .= ", phone";
425 $sql .= ", phone_mobile";
426 $sql .= ") VALUES (";
427 $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
428 $sql .= ", ".((int) $conf->entity);
429 $sql .= ", ".((float) $this->amount);
430 $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
431 $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
432 $sql .= ", '".$this->db->escape($this->firstname)."'";
433 $sql .= ", '".$this->db->escape($this->lastname)."'";
434 $sql .= ", '".$this->db->escape($this->societe)."'";
435 $sql .= ", '".$this->db->escape($this->address)."'";
436 $sql .= ", '".$this->db->escape($this->zip)."'";
437 $sql .= ", '".$this->db->escape($this->town)."'";
438 $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
439 $sql .= ", ".(int) $this->public;
440 $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
441 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
442 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
443 $sql .= ", ".((int) $user->id);
444 $sql .= ", null";
445 $sql .= ", '".$this->db->idate($this->date)."'";
446 $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
447 $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
448 $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
449 $sql .= ")";
450
451 $resql = $this->db->query($sql);
452 if ($resql) {
453 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
454 $ret = $this->id;
455
456 if (!$notrigger) {
457 // Call trigger
458 $result = $this->call_trigger('DON_CREATE', $user);
459 if ($result < 0) {
460 $error++;
461 }
462 // End call triggers
463 }
464 } else {
465 $this->error = $this->db->lasterror();
466 $error++;
467 }
468
469 // Update extrafield
470 if (!$error) {
471 $result = $this->insertExtraFields();
472 if ($result < 0) {
473 $error++;
474 }
475 }
476
477 if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
478 //$res = $this->setValid($user);
479 //if ($res < 0) $error++;
480 }
481
482 if (!$error) {
483 $this->db->commit();
484 return $ret;
485 } else {
486 $this->db->rollback();
487 return -1;
488 }
489 }
490
498 public function update($user, $notrigger = 0)
499 {
500 global $langs;
501
502 $error = 0;
503
504 // Clean parameters
505 // $this->address = ($this->address > 0 ? $this->address : $this->address);
506 // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
507 // $this->town = ($this->town > 0 ? $this->town : $this->town);
508 // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
509 // $this->country = ($this->country ? $this->country : $this->country);
510 $this->amount = (float) price2num($this->amount);
511
512 // Check parameters
513 if ($this->amount < 0) {
514 $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
515 return -1;
516 }
517
518 $this->db->begin();
519
520 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
521 $sql .= " amount = ".((float) $this->amount);
522 $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
523 $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
524 $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
525 $sql .= ", societe='".$this->db->escape($this->societe)."'";
526 $sql .= ", address='".$this->db->escape($this->address)."'";
527 $sql .= ", zip='".$this->db->escape($this->zip)."'";
528 $sql .= ", town='".$this->db->escape($this->town)."'";
529 $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
530 $sql .= ", public=".((int) $this->public);
531 $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
532 $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
533 $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
534 $sql .= ", datedon='".$this->db->idate($this->date)."'";
535 $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
536 $sql .= ", email='".$this->db->escape(trim($this->email))."'";
537 $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
538 $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
539 $sql .= ", fk_statut=".((int) $this->status);
540 $sql .= " WHERE rowid = ".((int) $this->id);
541
542 dol_syslog(get_class($this)."::Update", LOG_DEBUG);
543 $resql = $this->db->query($sql);
544 if ($resql) {
545 if (!$notrigger) {
546 // Call trigger
547 $result = $this->call_trigger('DON_MODIFY', $user);
548 if ($result < 0) {
549 $error++;
550 }
551 // End call triggers
552 }
553
554 // Update extrafield
555 if (!$error) {
556 $result = $this->insertExtraFields();
557 if ($result < 0) {
558 $error++;
559 }
560 }
561
562 if (!$error) {
563 $this->db->commit();
564 $result = 1;
565 } else {
566 $this->db->rollback();
567 $result = -1;
568 }
569 } else {
570 $this->error = $this->db->lasterror();
571 $this->errors[] = $this->error;
572 $this->db->rollback();
573 dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
574 $result = -2;
575 }
576 return $result;
577 }
578
586 public function delete($user, $notrigger = 0)
587 {
588 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
589
590 $error = 0;
591
592 $this->db->begin();
593
594 if (!$error && !$notrigger) {
595 // Call trigger
596 $result = $this->call_trigger('DON_DELETE', $user);
597
598 if ($result < 0) {
599 $error++;
600 }
601 // End call triggers
602 }
603
604 // Delete donation
605 if (!$error) {
606 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
607 $sql .= " WHERE fk_object = ".((int) $this->id);
608
609 $resql = $this->db->query($sql);
610 if (!$resql) {
611 $this->errors[] = $this->db->lasterror();
612 $error++;
613 }
614 }
615
616 if (!$error) {
617 $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
618 $sql .= " WHERE rowid=".((int) $this->id);
619
620 $resql = $this->db->query($sql);
621 if (!$resql) {
622 $this->errors[] = $this->db->lasterror();
623 $error++;
624 } else {
625 // we delete file with dol_delete_dir_recursive
626 $this->deleteEcmFiles(1);
627
628 $dir = DOL_DATA_ROOT.'/'.$this->element.'/'.$this->ref;
629 // For remove dir
630 if (dol_is_dir($dir)) {
631 if (!dol_delete_dir_recursive($dir)) {
632 $this->errors[] = $this->error;
633 }
634 }
635 }
636 }
637
638 if (!$error) {
639 $this->db->commit();
640 return 1;
641 } else {
642 foreach ($this->errors as $errmsg) {
643 dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
644 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
645 }
646 dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
647 $this->db->rollback();
648 return -1;
649 }
650 }
651
659 public function fetch($id, $ref = '')
660 {
661 $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
662 $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, ";
663 $sql .= " d.fk_country, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
664 $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
665 $sql .= " p.ref as project_ref,";
666 $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
667 $sql .= " c.code as country_code, c.label as country";
668 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
669 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
670 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
671 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
672 $sql .= " WHERE d.entity IN (".getEntity('donation').")";
673 if (!empty($id)) {
674 $sql .= " AND d.rowid=".((int) $id);
675 } elseif (!empty($ref)) {
676 $sql .= " AND d.ref='".$this->db->escape($ref)."'";
677 }
678
679 dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
680 $resql = $this->db->query($sql);
681 if ($resql) {
682 if ($this->db->num_rows($resql)) {
683 $obj = $this->db->fetch_object($resql);
684
685 $this->id = $obj->rowid;
686 $this->ref = $obj->rowid;
687 $this->date_creation = $this->db->jdate($obj->datec);
688 $this->datec = $this->db->jdate($obj->datec);
689 $this->date_validation = $this->db->jdate($obj->date_valid);
690 $this->date_valid = $this->db->jdate($obj->date_valid);
691 $this->date_modification = $this->db->jdate($obj->datem);
692 $this->datem = $this->db->jdate($obj->datem);
693 $this->date = $this->db->jdate($obj->datedon);
694 $this->socid = $obj->socid;
695 $this->firstname = $obj->firstname;
696 $this->lastname = $obj->lastname;
697 $this->societe = $obj->societe;
698 $this->status = $obj->status;
699 $this->statut = $obj->status;
700 $this->address = $obj->address;
701 $this->zip = $obj->zip;
702 $this->town = $obj->town;
703 $this->country_id = $obj->fk_country;
704 $this->country_code = $obj->country_code;
705 $this->country = $obj->country;
706 $this->email = $obj->email;
707 $this->phone = $obj->phone;
708 $this->phone_mobile = $obj->phone_mobile;
709 $this->project = $obj->project_ref;
710 $this->fk_projet = $obj->fk_project; // deprecated
711 $this->fk_project = $obj->fk_project;
712 $this->public = $obj->public;
713 $this->mode_reglement_id = $obj->fk_payment;
714 $this->mode_reglement_code = $obj->payment_code;
715 $this->mode_reglement = $obj->payment_label;
716 $this->paid = $obj->paid;
717 $this->amount = $obj->amount;
718 $this->note_private = $obj->note_private;
719 $this->note_public = $obj->note_public;
720 $this->model_pdf = $obj->model_pdf;
721
722 // Retrieve all extrafield
723 // fetch optionals attributes and labels
724 $this->fetch_optionals();
725 }
726 return 1;
727 } else {
728 dol_print_error($this->db);
729 return -1;
730 }
731 }
732
740 public function setValid($user, $notrigger = 0)
741 {
742 // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
743 return $this->valid_promesse($this->id, $user->id, $notrigger);
744 }
745
746 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
755 public function valid_promesse($id, $userid, $notrigger = 0)
756 {
757 // phpcs:enable
758 global $user;
759
760 $error = 0;
761
762 $this->db->begin();
763
764 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
765
766 $resql = $this->db->query($sql);
767 if ($resql) {
768 if ($this->db->affected_rows($resql)) {
769 if (!$notrigger) {
770 // Call trigger
771 $result = $this->call_trigger('DON_VALIDATE', $user);
772 if ($result < 0) {
773 $error++;
774 }
775 // End call triggers
776 }
777 }
778 } else {
779 $error++;
780 $this->error = $this->db->lasterror();
781 }
782
783 if (!$error) {
784 $this->status = 1;
785 $this->db->commit();
786 return 1;
787 } else {
788 $this->db->rollback();
789 return -1;
790 }
791 }
792
800 public function setPaid($id, $modepayment = 0)
801 {
802 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
803 if ($modepayment) {
804 $sql .= ", fk_payment = ".((int) $modepayment);
805 }
806 $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
807
808 $resql = $this->db->query($sql);
809 if ($resql) {
810 if ($this->db->affected_rows($resql)) {
811 $this->status = 2;
812 $this->paid = 1;
813 return 1;
814 } else {
815 return 0;
816 }
817 } else {
818 dol_print_error($this->db);
819 return -1;
820 }
821 }
822
823 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
830 public function set_cancel($id)
831 {
832 // phpcs:enable
833 $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
834
835 $resql = $this->db->query($sql);
836 if ($resql) {
837 if ($this->db->affected_rows($resql)) {
838 $this->status = -1;
839 return 1;
840 } else {
841 return 0;
842 }
843 } else {
844 dol_print_error($this->db);
845 return -1;
846 }
847 }
848
856 public function reopen($user, $notrigger = 0)
857 {
858 // Protection
859 if ($this->status != self::STATUS_CANCELED) {
860 return 0;
861 }
862
863 return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
864 }
865
866 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
873 public function sum_donations($param)
874 {
875 // phpcs:enable
876 global $conf;
877
878 $result = 0;
879
880 $sql = "SELECT sum(amount) as total";
881 $sql .= " FROM ".MAIN_DB_PREFIX."don";
882 $sql .= " WHERE fk_statut = ".((int) $param);
883 $sql .= " AND entity = ".$conf->entity;
884
885 $resql = $this->db->query($sql);
886 if ($resql) {
887 $obj = $this->db->fetch_object($resql);
888 $result = $obj->total;
889 }
890
891 return $result;
892 }
893
899 public function loadStateBoard()
900 {
901 $this->nb = array();
902
903 $sql = "SELECT count(d.rowid) as nb";
904 $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
905 $sql .= " WHERE d.fk_statut > 0";
906 $sql .= " AND d.entity IN (".getEntity('donation').")";
907
908 $resql = $this->db->query($sql);
909 if ($resql) {
910 while ($obj = $this->db->fetch_object($resql)) {
911 $this->nb["donations"] = $obj->nb;
912 }
913 $this->db->free($resql);
914 return 1;
915 } else {
916 dol_print_error($this->db);
917 $this->error = $this->db->error();
918 return -1;
919 }
920 }
921
931 public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
932 {
933 global $conf, $langs, $hookmanager;
934
935 if (!empty($conf->dol_no_mouse_hover)) {
936 $notooltip = 1; // Force disable tooltips
937 }
938
939 $result = '';
940 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
941 if (isset($this->status)) {
942 $label .= ' '.$this->getLibStatut(5);
943 }
944 if (!empty($this->id)) {
945 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
946 $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
947 }
948 if ($moretitle) {
949 $label .= ' - '.$moretitle;
950 }
951
952 $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
953
954 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
955 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
956 $add_save_lastsearch_values = 1;
957 }
958 if ($add_save_lastsearch_values) {
959 $url .= '&save_lastsearch_values=1';
960 }
961
962 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
963 $linkend = '</a>';
964
965 $result .= $linkstart;
966 if ($withpicto) {
967 $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
968 }
969 if ($withpicto != 2) {
970 $result .= $this->ref;
971 }
972 $result .= $linkend;
973 global $action;
974 $hookmanager->initHooks(array($this->element . 'dao'));
975 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
976 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
977 if ($reshook > 0) {
978 $result = $hookmanager->resPrint;
979 } else {
980 $result .= $hookmanager->resPrint;
981 }
982 return $result;
983 }
984
991 public function info($id)
992 {
993 $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
994 $sql .= ' d.tms as datem';
995 $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
996 $sql .= ' WHERE d.rowid = '.((int) $id);
997
998 dol_syslog(get_class($this).'::info', LOG_DEBUG);
999 $result = $this->db->query($sql);
1000
1001 if ($result) {
1002 if ($this->db->num_rows($result)) {
1003 $obj = $this->db->fetch_object($result);
1004 $this->id = $obj->rowid;
1005
1006 $this->user_creation_id = $obj->fk_user_author;
1007 $this->user_validation_id = $obj->fk_user_valid;
1008 $this->date_creation = $this->db->jdate($obj->datec);
1009 $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
1010 }
1011 $this->db->free($result);
1012 } else {
1013 dol_print_error($this->db);
1014 }
1015 }
1016
1017
1028 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1029 {
1030 global $conf, $langs;
1031
1032 $langs->load("bills");
1033
1034 if (!dol_strlen($modele)) {
1035 $modele = 'html_cerfafr';
1036
1037 if ($this->model_pdf) {
1038 $modele = $this->model_pdf;
1039 } elseif (getDolGlobalString('DON_ADDON_MODEL')) {
1040 $modele = getDolGlobalString('DON_ADDON_MODEL');
1041 }
1042 }
1043
1044 //$modelpath = "core/modules/dons/";
1045
1046 // TODO Restore use of commonGenerateDocument instead of dedicated code here
1047 //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1048
1049 // Increase limit for PDF build
1050 $err = error_reporting();
1051 error_reporting(0);
1052 @set_time_limit(120);
1053 error_reporting($err);
1054
1055 $srctemplatepath = '';
1056
1057 // If selected modele is a filename template (then $modele="modelname:filename")
1058 $tmp = explode(':', $modele, 2);
1059 if (!empty($tmp[1])) {
1060 $modele = $tmp[0];
1061 $srctemplatepath = $tmp[1];
1062 }
1063
1064 // Search template files
1065 $file = '';
1066 $classname = '';
1067 $filefound = 0;
1068 $dirmodels = array('/');
1069 if (is_array($conf->modules_parts['models'])) {
1070 $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1071 }
1072 foreach ($dirmodels as $reldir) {
1073 foreach (array('html', 'doc', 'pdf') as $prefix) {
1074 $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1075
1076 // Verify the path for the module
1077 $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1078 if (file_exists($file)) {
1079 $filefound = 1;
1080 $classname = $prefix.'_'.$modele;
1081 break;
1082 }
1083 }
1084 if ($filefound) {
1085 break;
1086 }
1087 }
1088
1089 // Charge le modele
1090 if ($filefound) {
1091 require_once $file;
1092
1093 $object = $this;
1094
1095 $classname = $modele;
1096 $obj = new $classname($this->db);
1097
1098 // We save charset_output to restore it because write_file can change it if needed for
1099 // output format that does not support UTF8.
1100 $sav_charset_output = $outputlangs->charset_output;
1101 if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1102 $outputlangs->charset_output = $sav_charset_output;
1103
1104 // we delete preview files
1105 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1107 return 1;
1108 } else {
1109 $outputlangs->charset_output = $sav_charset_output;
1110 dol_syslog("Erreur dans don_create");
1111 dol_print_error($this->db, $obj->error);
1112 return 0;
1113 }
1114 } else {
1115 print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1116 return 0;
1117 }
1118 }
1119
1128 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1129 {
1130 $tables = array(
1131 'don'
1132 );
1133
1134 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1135 }
1136
1142 public function getRemainToPay()
1143 {
1144 dol_syslog(__METHOD__, LOG_DEBUG);
1145
1146 if (empty($this->id)) {
1147 $this->error = 'Missing object id';
1148 $this->errors[] = $this->error;
1149 dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1150 return -1;
1151 }
1152
1153 $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1154 $resql = $this->db->query($sql);
1155 if (!$resql) {
1156 dol_print_error($this->db);
1157 return -2;
1158 } else {
1159 $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1160 return (float) ($this->amount - $sum_amount);
1161 }
1162 }
1163
1171 public function getKanbanView($option = '', $arraydata = null)
1172 {
1173 global $conf, $langs;
1174
1175 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1176
1177 $return = '<div class="box-flex-item box-flex-grow-zero">';
1178 $return .= '<div class="info-box info-box-sm">';
1179 $return .= '<span class="info-box-icon bg-infobox-action">';
1180 $return .= img_picto('', $this->picto);
1181 $return .= '</span>';
1182 $return .= '<div class="info-box-content">';
1183 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
1184 if ($selected >= 0) {
1185 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1186 }
1187 if (property_exists($this, 'date')) {
1188 $return .= ' &nbsp; | &nbsp; <span class="info-box-label">'.dol_print_date($this->date, 'day', 'tzuserrel').'</span>';
1189 }
1190 if (property_exists($this, 'societe') && !empty($this->societe)) {
1191 $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1192 }
1193 if (property_exists($this, 'amount')) {
1194 $return .= '<br><span class="info-box-label amount">'.price($this->amount, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
1195 }
1196 if (method_exists($this, 'LibStatut')) {
1197 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1198 }
1199 $return .= '</div>';
1200 $return .= '</div>';
1201 $return .= '</div>';
1202 return $return;
1203 }
1204}
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:58
print $langs trans("AuditedSecurityEvents").'</strong >< span class="opacitymedium"></span >< br > status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition security.php:626
$object ref
Definition info.php:79
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 clicable 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.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
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 clicable name (with picto eventually)
trait CommonPeople
Support class for thirdparties, contacts, members, users or resources.
dol_delete_dir_recursive($dir, $count=0, $nophperrors=0, $onlysub=0, &$countdeleted=0, $indexdatabase=1, $nolog=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_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
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 '.
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).
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 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...
publicphonebutton2 phonegreen basiclayout basiclayout TotalHT VATCode TotalVAT TotalLT1 TotalLT2 TotalTTC TotalHT clearboth nowraponall TAKEPOS_SHOW_SUBPRICE right right right takeposterminal SELECT e e e e e statut
Definition invoice.php:1929