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