dolibarr  20.0.0-beta
don.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6  * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
8  * Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
9  * Copyright (C) 2019-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 
33 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
35 
36 
40 class 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 
69  public $datec;
70  public $datem;
71 
76  public $amount;
77 
81  public $socid;
82 
86  public $societe;
87 
91  public $address;
92 
96  public $zip;
97 
101  public $town;
102 
106  public $email;
107 
108  public $phone;
109  public $phone_mobile;
110 
111 
115  public $mode_reglement;
116 
120  public $mode_reglement_code;
121 
125  public $public;
126 
130  public $fk_project;
131 
135  public $fk_typepayment;
136 
141  public $num_payment;
142  public $date_valid;
143 
147  public $modepaymentid = 0;
148 
149  public $paid;
150 
151  const STATUS_DRAFT = 0;
152  const STATUS_VALIDATED = 1;
153  const STATUS_PAID = 2;
154  const STATUS_CANCELED = -1;
155 
156 
162  public function __construct($db)
163  {
164  $this->db = $db;
165 
166  $this->ismultientitymanaged = 1;
167  }
168 
169 
176  public function getLibStatut($mode = 0)
177  {
178  return $this->LibStatut($this->statut, $mode);
179  }
180 
181  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
189  public function LibStatut($status, $mode = 0)
190  {
191  // phpcs:enable
192  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
193  global $langs;
194  $langs->load("donations");
195  $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
196  $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
197  $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
198  $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
199  $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
200  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
201  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
202  $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
203  }
204 
205  $statusType = 'status'.$status;
206  if ($status == self::STATUS_CANCELED) {
207  $statusType = 'status9';
208  }
209  if ($status == self::STATUS_PAID) {
210  $statusType = 'status6';
211  }
212 
213  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
214  }
215 
216 
224  public function initAsSpecimen()
225  {
226  global $conf;
227 
228  $now = dol_now();
229 
230  // Charge tableau des id de societe socids
231  $socids = array();
232 
233  $sql = "SELECT rowid";
234  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
235  $sql .= " WHERE client IN (1, 3)";
236  $sql .= " AND entity = ".$conf->entity;
237  $sql .= " LIMIT 10";
238 
239  $resql = $this->db->query($sql);
240  if ($resql) {
241  $num_socs = $this->db->num_rows($resql);
242  $i = 0;
243  while ($i < $num_socs) {
244  $row = $this->db->fetch_row($resql);
245  $socids[$i] = $row[0];
246 
247  $i++;
248  }
249  }
250 
251  // Initialise parameters
252  $this->id = 0;
253  $this->ref = 'SPECIMEN';
254  $this->specimen = 1;
255  $this->lastname = 'Doe';
256  $this->firstname = 'John';
257  $this->socid = empty($socids[0]) ? 0 : $socids[0];
258  $this->date = $now;
259  $this->date_valid = $now;
260  $this->amount = 100.90;
261  $this->public = 1;
262  $this->societe = 'The Company';
263  $this->address = 'Twist road';
264  $this->zip = '99999';
265  $this->town = 'Town';
266  $this->note_private = 'Private note';
267  $this->note_public = 'Public note';
268  $this->email = 'email@email.com';
269  $this->phone = '0123456789';
270  $this->phone_mobile = '0606060606';
271  $this->status = 1;
272 
273  return 1;
274  }
275 
276 
284  public function check($minimum = 0)
285  {
286  global $langs;
287  $langs->load('main');
288  $langs->load('companies');
289 
290  $error_string = array();
291  $err = 0;
292  $amount_invalid = 0;
293 
294  if (dol_strlen(trim($this->societe)) == 0) {
295  if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
296  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
297  $err++;
298  }
299  }
300 
301  if (dol_strlen(trim($this->address)) == 0) {
302  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
303  $err++;
304  }
305 
306  if (dol_strlen(trim($this->zip)) == 0) {
307  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
308  $err++;
309  }
310 
311  if (dol_strlen(trim($this->town)) == 0) {
312  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
313  $err++;
314  }
315 
316  if (dol_strlen(trim($this->email)) == 0) {
317  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
318  $err++;
319  }
320 
321  $this->amount = (float) $this->amount;
322 
323  $map = range(0, 9);
324  $len = dol_strlen((string) $this->amount);
325  for ($i = 0; $i < $len; $i++) {
326  if (!isset($map[substr((string) $this->amount, $i, 1)])) {
327  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
328  $err++;
329  $amount_invalid = 1;
330  break;
331  }
332  }
333 
334  if (!$amount_invalid) {
335  if ($this->amount == 0) {
336  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
337  $err++;
338  } else {
339  if ($this->amount < $minimum && $minimum > 0) {
340  $error_string[] = $langs->trans('MinimumAmount', $minimum);
341  $err++;
342  }
343  }
344  }
345 
346  if ($err) {
347  $this->errors = $error_string;
348  return 0;
349  } else {
350  return 1;
351  }
352  }
353 
362  public function create($user, $notrigger = 0)
363  {
364  global $conf, $langs;
365 
366  $error = 0;
367  $ret = 0;
368  $now = dol_now();
369 
370  // Clean parameters
371  // $this->address = ($this->address > 0 ? $this->address : $this->address);
372  // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
373  // $this->town = ($this->town > 0 ? $this->town : $this->town);
374  // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
375  // $this->country = ($this->country ? $this->country : $this->country);
376  $this->amount = (float) price2num($this->amount);
377 
378  // Check parameters
379  if ($this->amount < 0) {
380  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
381  return -1;
382  }
383 
384  $this->db->begin();
385 
386  $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
387  $sql .= "datec";
388  $sql .= ", entity";
389  $sql .= ", amount";
390  $sql .= ", fk_payment";
391  $sql .= ", fk_soc";
392  $sql .= ", firstname";
393  $sql .= ", lastname";
394  $sql .= ", societe";
395  $sql .= ", address";
396  $sql .= ", zip";
397  $sql .= ", town";
398  $sql .= ", fk_country";
399  $sql .= ", public";
400  $sql .= ", fk_projet";
401  $sql .= ", note_private";
402  $sql .= ", note_public";
403  $sql .= ", fk_user_author";
404  $sql .= ", fk_user_valid";
405  $sql .= ", datedon";
406  $sql .= ", email";
407  $sql .= ", phone";
408  $sql .= ", phone_mobile";
409  $sql .= ") VALUES (";
410  $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
411  $sql .= ", ".((int) $conf->entity);
412  $sql .= ", ".((float) $this->amount);
413  $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
414  $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
415  $sql .= ", '".$this->db->escape($this->firstname)."'";
416  $sql .= ", '".$this->db->escape($this->lastname)."'";
417  $sql .= ", '".$this->db->escape($this->societe)."'";
418  $sql .= ", '".$this->db->escape($this->address)."'";
419  $sql .= ", '".$this->db->escape($this->zip)."'";
420  $sql .= ", '".$this->db->escape($this->town)."'";
421  $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
422  $sql .= ", ".(int) $this->public;
423  $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
424  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
425  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
426  $sql .= ", ".((int) $user->id);
427  $sql .= ", null";
428  $sql .= ", '".$this->db->idate($this->date)."'";
429  $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
430  $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
431  $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
432  $sql .= ")";
433 
434  $resql = $this->db->query($sql);
435  if ($resql) {
436  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
437  $ret = $this->id;
438 
439  if (!$notrigger) {
440  // Call trigger
441  $result = $this->call_trigger('DON_CREATE', $user);
442  if ($result < 0) {
443  $error++;
444  }
445  // End call triggers
446  }
447  } else {
448  $this->error = $this->db->lasterror();
449  $error++;
450  }
451 
452  // Update extrafield
453  if (!$error) {
454  $result = $this->insertExtraFields();
455  if ($result < 0) {
456  $error++;
457  }
458  }
459 
460  if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
461  //$res = $this->setValid($user);
462  //if ($res < 0) $error++;
463  }
464 
465  if (!$error) {
466  $this->db->commit();
467  return $ret;
468  } else {
469  $this->db->rollback();
470  return -1;
471  }
472  }
473 
481  public function update($user, $notrigger = 0)
482  {
483  global $langs;
484 
485  $error = 0;
486 
487  // Clean parameters
488  // $this->address = ($this->address > 0 ? $this->address : $this->address);
489  // $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
490  // $this->town = ($this->town > 0 ? $this->town : $this->town);
491  // $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
492  // $this->country = ($this->country ? $this->country : $this->country);
493  $this->amount = (float) price2num($this->amount);
494 
495  // Check parameters
496  if ($this->amount < 0) {
497  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
498  return -1;
499  }
500 
501  $this->db->begin();
502 
503  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
504  $sql .= " amount = ".((float) $this->amount);
505  $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
506  $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
507  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
508  $sql .= ", societe='".$this->db->escape($this->societe)."'";
509  $sql .= ", address='".$this->db->escape($this->address)."'";
510  $sql .= ", zip='".$this->db->escape($this->zip)."'";
511  $sql .= ", town='".$this->db->escape($this->town)."'";
512  $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
513  $sql .= ", public=".((int) $this->public);
514  $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
515  $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
516  $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
517  $sql .= ", datedon='".$this->db->idate($this->date)."'";
518  $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
519  $sql .= ", email='".$this->db->escape(trim($this->email))."'";
520  $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
521  $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
522  $sql .= ", fk_statut=".((int) $this->statut);
523  $sql .= " WHERE rowid = ".((int) $this->id);
524 
525  dol_syslog(get_class($this)."::Update", LOG_DEBUG);
526  $resql = $this->db->query($sql);
527  if ($resql) {
528  if (!$notrigger) {
529  // Call trigger
530  $result = $this->call_trigger('DON_MODIFY', $user);
531  if ($result < 0) {
532  $error++;
533  }
534  // End call triggers
535  }
536 
537  // Update extrafield
538  if (!$error) {
539  $result = $this->insertExtraFields();
540  if ($result < 0) {
541  $error++;
542  }
543  }
544 
545  if (!$error) {
546  $this->db->commit();
547  $result = 1;
548  } else {
549  $this->db->rollback();
550  $result = -1;
551  }
552  } else {
553  $this->error = $this->db->lasterror();
554  $this->errors[] = $this->error;
555  $this->db->rollback();
556  dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
557  $result = -2;
558  }
559  return $result;
560  }
561 
569  public function delete($user, $notrigger = 0)
570  {
571  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
572 
573  $error = 0;
574 
575  $this->db->begin();
576 
577  if (!$error && !$notrigger) {
578  // Call trigger
579  $result = $this->call_trigger('DON_DELETE', $user);
580 
581  if ($result < 0) {
582  $error++;
583  }
584  // End call triggers
585  }
586 
587  // Delete donation
588  if (!$error) {
589  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
590  $sql .= " WHERE fk_object = ".((int) $this->id);
591 
592  $resql = $this->db->query($sql);
593  if (!$resql) {
594  $this->errors[] = $this->db->lasterror();
595  $error++;
596  }
597  }
598 
599  if (!$error) {
600  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
601  $sql .= " WHERE rowid=".((int) $this->id);
602 
603  $resql = $this->db->query($sql);
604  if (!$resql) {
605  $this->errors[] = $this->db->lasterror();
606  $error++;
607  }
608  }
609 
610  if (!$error) {
611  $this->db->commit();
612  return 1;
613  } else {
614  foreach ($this->errors as $errmsg) {
615  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
616  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
617  }
618  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
619  $this->db->rollback();
620  return -1;
621  }
622  }
623 
631  public function fetch($id, $ref = '')
632  {
633  $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
634  $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, ";
635  $sql .= " d.fk_country, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
636  $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
637  $sql .= " p.ref as project_ref,";
638  $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
639  $sql .= " c.code as country_code, c.label as country";
640  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
641  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
642  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
643  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
644  $sql .= " WHERE d.entity IN (".getEntity('donation').")";
645  if (!empty($id)) {
646  $sql .= " AND d.rowid=".((int) $id);
647  } elseif (!empty($ref)) {
648  $sql .= " AND d.ref='".$this->db->escape($ref)."'";
649  }
650 
651  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
652  $resql = $this->db->query($sql);
653  if ($resql) {
654  if ($this->db->num_rows($resql)) {
655  $obj = $this->db->fetch_object($resql);
656 
657  $this->id = $obj->rowid;
658  $this->ref = $obj->rowid;
659  $this->date_creation = $this->db->jdate($obj->datec);
660  $this->datec = $this->db->jdate($obj->datec);
661  $this->date_validation = $this->db->jdate($obj->date_valid);
662  $this->date_valid = $this->db->jdate($obj->date_valid);
663  $this->date_modification = $this->db->jdate($obj->datem);
664  $this->datem = $this->db->jdate($obj->datem);
665  $this->date = $this->db->jdate($obj->datedon);
666  $this->socid = $obj->socid;
667  $this->firstname = $obj->firstname;
668  $this->lastname = $obj->lastname;
669  $this->societe = $obj->societe;
670  $this->status = $obj->status;
671  $this->statut = $obj->status;
672  $this->address = $obj->address;
673  $this->zip = $obj->zip;
674  $this->town = $obj->town;
675  $this->country_id = $obj->fk_country;
676  $this->country_code = $obj->country_code;
677  $this->country = $obj->country;
678  $this->email = $obj->email;
679  $this->phone = $obj->phone;
680  $this->phone_mobile = $obj->phone_mobile;
681  $this->project = $obj->project_ref;
682  $this->fk_projet = $obj->fk_project; // deprecated
683  $this->fk_project = $obj->fk_project;
684  $this->public = $obj->public;
685  $this->mode_reglement_id = $obj->fk_payment;
686  $this->mode_reglement_code = $obj->payment_code;
687  $this->mode_reglement = $obj->payment_label;
688  $this->paid = $obj->paid;
689  $this->amount = $obj->amount;
690  $this->note_private = $obj->note_private;
691  $this->note_public = $obj->note_public;
692  $this->model_pdf = $obj->model_pdf;
693 
694  // Retrieve all extrafield
695  // fetch optionals attributes and labels
696  $this->fetch_optionals();
697  }
698  return 1;
699  } else {
700  dol_print_error($this->db);
701  return -1;
702  }
703  }
704 
712  public function setValid($user, $notrigger = 0)
713  {
714  // @phan-suppress-next-line PhanPluginSuspiciousParamPosition
715  return $this->valid_promesse($this->id, $user->id, $notrigger);
716  }
717 
718  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
727  public function valid_promesse($id, $userid, $notrigger = 0)
728  {
729  // phpcs:enable
730  global $user;
731 
732  $error = 0;
733 
734  $this->db->begin();
735 
736  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
737 
738  $resql = $this->db->query($sql);
739  if ($resql) {
740  if ($this->db->affected_rows($resql)) {
741  if (!$notrigger) {
742  // Call trigger
743  $result = $this->call_trigger('DON_VALIDATE', $user);
744  if ($result < 0) {
745  $error++;
746  }
747  // End call triggers
748  }
749  }
750  } else {
751  $error++;
752  $this->error = $this->db->lasterror();
753  }
754 
755  if (!$error) {
756  $this->statut = 1;
757  $this->db->commit();
758  return 1;
759  } else {
760  $this->db->rollback();
761  return -1;
762  }
763  }
764 
772  public function setPaid($id, $modepayment = 0)
773  {
774  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
775  if ($modepayment) {
776  $sql .= ", fk_payment = ".((int) $modepayment);
777  }
778  $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
779 
780  $resql = $this->db->query($sql);
781  if ($resql) {
782  if ($this->db->affected_rows($resql)) {
783  $this->statut = 2;
784  $this->paid = 1;
785  return 1;
786  } else {
787  return 0;
788  }
789  } else {
790  dol_print_error($this->db);
791  return -1;
792  }
793  }
794 
795  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
802  public function set_cancel($id)
803  {
804  // phpcs:enable
805  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
806 
807  $resql = $this->db->query($sql);
808  if ($resql) {
809  if ($this->db->affected_rows($resql)) {
810  $this->statut = -1;
811  return 1;
812  } else {
813  return 0;
814  }
815  } else {
816  dol_print_error($this->db);
817  return -1;
818  }
819  }
820 
828  public function reopen($user, $notrigger = 0)
829  {
830  // Protection
831  if ($this->statut != self::STATUS_CANCELED) {
832  return 0;
833  }
834 
835  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
836  }
837 
838  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
845  public function sum_donations($param)
846  {
847  // phpcs:enable
848  global $conf;
849 
850  $result = 0;
851 
852  $sql = "SELECT sum(amount) as total";
853  $sql .= " FROM ".MAIN_DB_PREFIX."don";
854  $sql .= " WHERE fk_statut = ".((int) $param);
855  $sql .= " AND entity = ".$conf->entity;
856 
857  $resql = $this->db->query($sql);
858  if ($resql) {
859  $obj = $this->db->fetch_object($resql);
860  $result = $obj->total;
861  }
862 
863  return $result;
864  }
865 
871  public function loadStateBoard()
872  {
873  $this->nb = array();
874 
875  $sql = "SELECT count(d.rowid) as nb";
876  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
877  $sql .= " WHERE d.fk_statut > 0";
878  $sql .= " AND d.entity IN (".getEntity('donation').")";
879 
880  $resql = $this->db->query($sql);
881  if ($resql) {
882  while ($obj = $this->db->fetch_object($resql)) {
883  $this->nb["donations"] = $obj->nb;
884  }
885  $this->db->free($resql);
886  return 1;
887  } else {
888  dol_print_error($this->db);
889  $this->error = $this->db->error();
890  return -1;
891  }
892  }
893 
903  public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
904  {
905  global $conf, $langs, $hookmanager;
906 
907  if (!empty($conf->dol_no_mouse_hover)) {
908  $notooltip = 1; // Force disable tooltips
909  }
910 
911  $result = '';
912  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
913  if (isset($this->status)) {
914  $label .= ' '.$this->getLibStatut(5);
915  }
916  if (!empty($this->id)) {
917  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
918  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
919  }
920  if ($moretitle) {
921  $label .= ' - '.$moretitle;
922  }
923 
924  $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
925 
926  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
927  if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
928  $add_save_lastsearch_values = 1;
929  }
930  if ($add_save_lastsearch_values) {
931  $url .= '&save_lastsearch_values=1';
932  }
933 
934  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
935  $linkend = '</a>';
936 
937  $result .= $linkstart;
938  if ($withpicto) {
939  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
940  }
941  if ($withpicto != 2) {
942  $result .= $this->ref;
943  }
944  $result .= $linkend;
945  global $action;
946  $hookmanager->initHooks(array($this->element . 'dao'));
947  $parameters = array('id' => $this->id, 'getnomurl' => &$result);
948  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
949  if ($reshook > 0) {
950  $result = $hookmanager->resPrint;
951  } else {
952  $result .= $hookmanager->resPrint;
953  }
954  return $result;
955  }
956 
963  public function info($id)
964  {
965  $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
966  $sql .= ' d.tms as datem';
967  $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
968  $sql .= ' WHERE d.rowid = '.((int) $id);
969 
970  dol_syslog(get_class($this).'::info', LOG_DEBUG);
971  $result = $this->db->query($sql);
972 
973  if ($result) {
974  if ($this->db->num_rows($result)) {
975  $obj = $this->db->fetch_object($result);
976  $this->id = $obj->rowid;
977 
978  $this->user_creation_id = $obj->fk_user_author;
979  $this->user_validation_id = $obj->fk_user_valid;
980  $this->date_creation = $this->db->jdate($obj->datec);
981  $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
982  }
983  $this->db->free($result);
984  } else {
985  dol_print_error($this->db);
986  }
987  }
988 
989 
1000  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1001  {
1002  global $conf, $langs;
1003 
1004  $langs->load("bills");
1005 
1006  if (!dol_strlen($modele)) {
1007  $modele = 'html_cerfafr';
1008 
1009  if ($this->model_pdf) {
1010  $modele = $this->model_pdf;
1011  } elseif (getDolGlobalString('DON_ADDON_MODEL')) {
1012  $modele = getDolGlobalString('DON_ADDON_MODEL');
1013  }
1014  }
1015 
1016  //$modelpath = "core/modules/dons/";
1017 
1018  // TODO Restore use of commonGenerateDocument instead of dedicated code here
1019  //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1020 
1021  // Increase limit for PDF build
1022  $err = error_reporting();
1023  error_reporting(0);
1024  @set_time_limit(120);
1025  error_reporting($err);
1026 
1027  $srctemplatepath = '';
1028 
1029  // If selected modele is a filename template (then $modele="modelname:filename")
1030  $tmp = explode(':', $modele, 2);
1031  if (!empty($tmp[1])) {
1032  $modele = $tmp[0];
1033  $srctemplatepath = $tmp[1];
1034  }
1035 
1036  // Search template files
1037  $file = '';
1038  $classname = '';
1039  $filefound = 0;
1040  $dirmodels = array('/');
1041  if (is_array($conf->modules_parts['models'])) {
1042  $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1043  }
1044  foreach ($dirmodels as $reldir) {
1045  foreach (array('html', 'doc', 'pdf') as $prefix) {
1046  $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1047 
1048  // Verify the path for the module
1049  $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1050  if (file_exists($file)) {
1051  $filefound = 1;
1052  $classname = $prefix.'_'.$modele;
1053  break;
1054  }
1055  }
1056  if ($filefound) {
1057  break;
1058  }
1059  }
1060 
1061  // Charge le modele
1062  if ($filefound) {
1063  require_once $file;
1064 
1065  $object = $this;
1066 
1067  $classname = $modele;
1068  $obj = new $classname($this->db);
1069 
1070  // We save charset_output to restore it because write_file can change it if needed for
1071  // output format that does not support UTF8.
1072  $sav_charset_output = $outputlangs->charset_output;
1073  if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1074  $outputlangs->charset_output = $sav_charset_output;
1075 
1076  // we delete preview files
1077  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1079  return 1;
1080  } else {
1081  $outputlangs->charset_output = $sav_charset_output;
1082  dol_syslog("Erreur dans don_create");
1083  dol_print_error($this->db, $obj->error);
1084  return 0;
1085  }
1086  } else {
1087  print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1088  return 0;
1089  }
1090  }
1091 
1100  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1101  {
1102  $tables = array(
1103  'don'
1104  );
1105 
1106  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1107  }
1108 
1114  public function getRemainToPay()
1115  {
1116  dol_syslog(__METHOD__, LOG_DEBUG);
1117 
1118  if (empty($this->id)) {
1119  $this->error = 'Missing object id';
1120  $this->errors[] = $this->error;
1121  dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1122  return -1;
1123  }
1124 
1125  $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1126  $resql = $this->db->query($sql);
1127  if (!$resql) {
1128  dol_print_error($this->db);
1129  return -2;
1130  } else {
1131  $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1132  return (float) ($this->amount - $sum_amount);
1133  }
1134  }
1135 
1143  public function getKanbanView($option = '', $arraydata = null)
1144  {
1145  global $conf, $langs;
1146 
1147  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1148 
1149  $return = '<div class="box-flex-item box-flex-grow-zero">';
1150  $return .= '<div class="info-box info-box-sm">';
1151  $return .= '<span class="info-box-icon bg-infobox-action">';
1152  $return .= img_picto('', $this->picto);
1153  $return .= '</span>';
1154  $return .= '<div class="info-box-content">';
1155  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
1156  if ($selected >= 0) {
1157  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1158  }
1159  if (property_exists($this, 'date')) {
1160  $return .= ' &nbsp; | &nbsp; <span class="info-box-label">'.dol_print_date($this->date, 'day', 'tzuserrel').'</span>';
1161  }
1162  if (property_exists($this, 'societe') && !empty($this->societe)) {
1163  $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1164  }
1165  if (property_exists($this, 'amount')) {
1166  $return .= '<br><span class="info-box-label amount">'.price($this->amount, 1, $langs, 1, -1, -1, $conf->currency).'</span>';
1167  }
1168  if (method_exists($this, 'LibStatut')) {
1169  $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
1170  }
1171  $return .= '</div>';
1172  $return .= '</div>';
1173  $return .= '</div>';
1174  return $return;
1175  }
1176 }
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:607
$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...
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)
Definition: don.class.php:176
__construct($db)
Constructor.
Definition: don.class.php:162
initAsSpecimen()
Initialise an instance with random values.
Definition: don.class.php:224
create($user, $notrigger=0)
Create donation record into database.
Definition: don.class.php:362
getRemainToPay()
Function to get remaining amount to pay for a donation.
Definition: don.class.php:1114
check($minimum=0)
Check params and init ->errors array.
Definition: don.class.php:284
setValid($user, $notrigger=0)
Validate a intervention.
Definition: don.class.php:712
valid_promesse($id, $userid, $notrigger=0)
Validate a promise of donation.
Definition: don.class.php:727
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: don.class.php:1143
LibStatut($status, $mode=0)
Return the label of a given status.
Definition: don.class.php:189
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: don.class.php:1100
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: don.class.php:1000
fetch($id, $ref='')
Load donation from database.
Definition: don.class.php:631
reopen($user, $notrigger=0)
Set cancel status.
Definition: don.class.php:828
sum_donations($param)
Sum of donations.
Definition: don.class.php:845
info($id)
Information on record.
Definition: don.class.php:963
update($user, $notrigger=0)
Update a donation record.
Definition: don.class.php:481
set_cancel($id)
Set donation to status cancelled.
Definition: don.class.php:802
loadStateBoard()
Load the indicators this->nb for the state board.
Definition: don.class.php:871
setPaid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:772
getNomUrl($withpicto=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: don.class.php:903
trait CommonPeople
Support class for thirdparties, contacts, members, users or resources.
if(isModEnabled('invoice') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&!getDolGlobalString('MAIN_USE_NEW_SUPPLIERMOD') && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') && $user->hasRight('tax', 'charges', 'lire')) if(isModEnabled('invoice') &&isModEnabled('order') && $user->hasRight("commande", "lire") &&!getDolGlobalString('WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER')) $sql
Social contributions to pay.
Definition: index.php:745
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1661
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).
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 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...
td amount
Definition: global.inc.php:554
div float
Buy price without taxes.
Definition: style.css.php:960