dolibarr  18.0.0
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-2020 Frédéric France <frederic.france@netlogic.fr>
10  * Copyright (C) 2021 Maxime DEMAREST <maxime@indelog.fr>
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 3 of the License, or
15  * (at your option) any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU General Public License for more details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program. If not, see <https://www.gnu.org/licenses/>.
24  */
25 
32 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/class/commonpeople.class.php';
34 
35 
39 class Don extends CommonObject
40 {
41  use CommonPeople;
42 
46  public $element = 'don';
47 
51  public $table_element = 'don';
52 
56  public $fk_element = 'fk_donation';
57 
62  public $ismultientitymanaged = 1;
63 
67  public $picto = 'donation';
68 
72  public $date;
73 
74  public $datec;
75  public $datem;
76 
81  public $amount;
82 
86  public $socid;
87 
91  public $societe;
92 
96  public $address;
97 
101  public $zip;
102 
106  public $town;
107 
111  public $email;
112 
113  public $phone;
114  public $phone_mobile;
115 
119  public $public;
120 
124  public $fk_project;
125 
129  public $fk_typepayment;
130 
131  public $num_payment;
132  public $date_valid;
133 
137  public $modepaymentid = 0;
138 
139  public $paid;
140 
141 
145  public $labelStatus;
146 
150  public $labelStatusShort;
151 
152 
153  const STATUS_DRAFT = 0;
154  const STATUS_VALIDATED = 1;
155  const STATUS_PAID = 2;
156  const STATUS_CANCELED = -1;
157 
158 
164  public function __construct($db)
165  {
166  $this->db = $db;
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 parametres
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->statut = 1;
272  }
273 
274 
282  public function check($minimum = 0)
283  {
284  global $langs;
285  $langs->load('main');
286  $langs->load('companies');
287 
288  $error_string = array();
289  $err = 0;
290 
291  if (dol_strlen(trim($this->societe)) == 0) {
292  if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
293  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
294  $err++;
295  }
296  }
297 
298  if (dol_strlen(trim($this->address)) == 0) {
299  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
300  $err++;
301  }
302 
303  if (dol_strlen(trim($this->zip)) == 0) {
304  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
305  $err++;
306  }
307 
308  if (dol_strlen(trim($this->town)) == 0) {
309  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
310  $err++;
311  }
312 
313  if (dol_strlen(trim($this->email)) == 0) {
314  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
315  $err++;
316  }
317 
318  $this->amount = trim($this->amount);
319 
320  $map = range(0, 9);
321  $len = dol_strlen($this->amount);
322  for ($i = 0; $i < $len; $i++) {
323  if (!isset($map[substr($this->amount, $i, 1)])) {
324  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
325  $err++;
326  $amount_invalid = 1;
327  break;
328  }
329  }
330 
331  if (!$amount_invalid) {
332  if ($this->amount == 0) {
333  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
334  $err++;
335  } else {
336  if ($this->amount < $minimum && $minimum > 0) {
337  $error_string[] = $langs->trans('MinimumAmount', $langs->transnoentitiesnoconv('$minimum'));
338  $err++;
339  }
340  }
341  }
342 
343  if ($err) {
344  $this->errors = $error_string;
345  return 0;
346  } else {
347  return 1;
348  }
349  }
350 
359  public function create($user, $notrigger = 0)
360  {
361  global $conf, $langs;
362 
363  $error = 0;
364  $ret = 0;
365  $now = dol_now();
366 
367  // Clean parameters
368  $this->address = ($this->address > 0 ? $this->address : $this->address);
369  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
370  $this->town = ($this->town > 0 ? $this->town : $this->town);
371  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
372  $this->country = ($this->country ? $this->country : $this->country);
373  $this->amount = price2num($this->amount);
374 
375  // Check parameters
376  if ($this->amount < 0) {
377  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
378  return -1;
379  }
380 
381  $this->db->begin();
382 
383  $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
384  $sql .= "datec";
385  $sql .= ", entity";
386  $sql .= ", amount";
387  $sql .= ", fk_payment";
388  $sql .= ", fk_soc";
389  $sql .= ", firstname";
390  $sql .= ", lastname";
391  $sql .= ", societe";
392  $sql .= ", address";
393  $sql .= ", zip";
394  $sql .= ", town";
395  $sql .= ", fk_country";
396  $sql .= ", public";
397  $sql .= ", fk_projet";
398  $sql .= ", note_private";
399  $sql .= ", note_public";
400  $sql .= ", fk_user_author";
401  $sql .= ", fk_user_valid";
402  $sql .= ", datedon";
403  $sql .= ", email";
404  $sql .= ", phone";
405  $sql .= ", phone_mobile";
406  $sql .= ") VALUES (";
407  $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
408  $sql .= ", ".((int) $conf->entity);
409  $sql .= ", ".((float) $this->amount);
410  $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
411  $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
412  $sql .= ", '".$this->db->escape($this->firstname)."'";
413  $sql .= ", '".$this->db->escape($this->lastname)."'";
414  $sql .= ", '".$this->db->escape($this->societe)."'";
415  $sql .= ", '".$this->db->escape($this->address)."'";
416  $sql .= ", '".$this->db->escape($this->zip)."'";
417  $sql .= ", '".$this->db->escape($this->town)."'";
418  $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
419  $sql .= ", ".(int) $this->public;
420  $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
421  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
422  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
423  $sql .= ", ".((int) $user->id);
424  $sql .= ", null";
425  $sql .= ", '".$this->db->idate($this->date)."'";
426  $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
427  $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
428  $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
429  $sql .= ")";
430 
431  $resql = $this->db->query($sql);
432  if ($resql) {
433  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
434  $ret = $this->id;
435 
436  if (!$notrigger) {
437  // Call trigger
438  $result = $this->call_trigger('DON_CREATE', $user);
439  if ($result < 0) {
440  $error++;
441  }
442  // End call triggers
443  }
444  } else {
445  $this->error = $this->db->lasterror();
446  $this->errno = $this->db->lasterrno();
447  $error++;
448  }
449 
450  // Update extrafield
451  if (!$error) {
452  $result = $this->insertExtraFields();
453  if ($result < 0) {
454  $error++;
455  }
456  }
457 
458  if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
459  //$res = $this->setValid($user);
460  //if ($res < 0) $error++;
461  }
462 
463  if (!$error) {
464  $this->db->commit();
465  return $ret;
466  } else {
467  $this->db->rollback();
468  return -1;
469  }
470  }
471 
479  public function update($user, $notrigger = 0)
480  {
481  global $langs;
482 
483  $error = 0;
484 
485  // Clean parameters
486  $this->address = ($this->address > 0 ? $this->address : $this->address);
487  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
488  $this->town = ($this->town > 0 ? $this->town : $this->town);
489  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
490  $this->country = ($this->country ? $this->country : $this->country);
491  $this->amount = price2num($this->amount);
492 
493  // Check parameters
494  if ($this->amount < 0) {
495  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
496  return -1;
497  }
498 
499  $this->db->begin();
500 
501  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
502  $sql .= " amount = ".((float) $this->amount);
503  $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
504  $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
505  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
506  $sql .= ", societe='".$this->db->escape($this->societe)."'";
507  $sql .= ", address='".$this->db->escape($this->address)."'";
508  $sql .= ", zip='".$this->db->escape($this->zip)."'";
509  $sql .= ", town='".$this->db->escape($this->town)."'";
510  $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
511  $sql .= ", public=".((int) $this->public);
512  $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
513  $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
514  $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
515  $sql .= ", datedon='".$this->db->idate($this->date)."'";
516  $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
517  $sql .= ", email='".$this->db->escape(trim($this->email))."'";
518  $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
519  $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
520  $sql .= ", fk_statut=".((int) $this->statut);
521  $sql .= " WHERE rowid = ".((int) $this->id);
522 
523  dol_syslog(get_class($this)."::Update", LOG_DEBUG);
524  $resql = $this->db->query($sql);
525  if ($resql) {
526  if (!$notrigger) {
527  // Call trigger
528  $result = $this->call_trigger('DON_MODIFY', $user);
529  if ($result < 0) {
530  $error++;
531  }
532  // End call triggers
533  }
534 
535  // Update extrafield
536  if (!$error) {
537  $result = $this->insertExtraFields();
538  if ($result < 0) {
539  $error++;
540  }
541  }
542 
543  if (!$error) {
544  $this->db->commit();
545  $result = 1;
546  } else {
547  $this->db->rollback();
548  $result = -1;
549  }
550  } else {
551  $this->error = $this->db->lasterror();
552  $this->errors[] = $this->error;
553  $this->db->rollback();
554  dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
555  $result = -2;
556  }
557  return $result;
558  }
559 
567  public function delete($user, $notrigger = 0)
568  {
569  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
570 
571  $error = 0;
572 
573  $this->db->begin();
574 
575  if (!$error && !$notrigger) {
576  // Call trigger
577  $result = $this->call_trigger('DON_DELETE', $user);
578 
579  if ($result < 0) {
580  $error++;
581  }
582  // End call triggers
583  }
584 
585  // Delete donation
586  if (!$error) {
587  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
588  $sql .= " WHERE fk_object = ".((int) $this->id);
589 
590  $resql = $this->db->query($sql);
591  if (!$resql) {
592  $this->errors[] = $this->db->lasterror();
593  $error++;
594  }
595  }
596 
597  if (!$error) {
598  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
599  $sql .= " WHERE rowid=".((int) $this->id);
600 
601  $resql = $this->db->query($sql);
602  if (!$resql) {
603  $this->errors[] = $this->db->lasterror();
604  $error++;
605  }
606  }
607 
608  if (!$error) {
609  $this->db->commit();
610  return 1;
611  } else {
612  foreach ($this->errors as $errmsg) {
613  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
614  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
615  }
616  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
617  $this->db->rollback();
618  return -1;
619  }
620  }
621 
629  public function fetch($id, $ref = '')
630  {
631  $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
632  $sql .= " d.fk_soc as socid,d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
633  $sql .= " d.fk_country, d.country as country_olddata, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, d.email, d.phone, ";
634  $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
635  $sql .= " p.ref as project_ref,";
636  $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
637  $sql .= " c.code as country_code, c.label as country";
638  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
639  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
640  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
641  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
642  $sql .= " WHERE d.entity IN (".getEntity('donation').")";
643  if (!empty($id)) {
644  $sql .= " AND d.rowid=".((int) $id);
645  } elseif (!empty($ref)) {
646  $sql .= " AND d.ref='".$this->db->escape($ref)."'";
647  }
648 
649  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
650  $resql = $this->db->query($sql);
651  if ($resql) {
652  if ($this->db->num_rows($resql)) {
653  $obj = $this->db->fetch_object($resql);
654 
655  $this->id = $obj->rowid;
656  $this->ref = $obj->rowid;
657  $this->date_creation = $this->db->jdate($obj->datec);
658  $this->datec = $this->db->jdate($obj->datec);
659  $this->date_validation = $this->db->jdate($obj->date_valid);
660  $this->date_valid = $this->db->jdate($obj->date_valid);
661  $this->date_modification = $this->db->jdate($obj->datem);
662  $this->datem = $this->db->jdate($obj->datem);
663  $this->date = $this->db->jdate($obj->datedon);
664  $this->socid = $obj->socid;
665  $this->firstname = $obj->firstname;
666  $this->lastname = $obj->lastname;
667  $this->societe = $obj->societe;
668  $this->statut = $obj->fk_statut;
669  $this->address = $obj->address;
670  $this->zip = $obj->zip;
671  $this->town = $obj->town;
672  $this->country_id = $obj->fk_country;
673  $this->country_code = $obj->country_code;
674  $this->country = $obj->country;
675  $this->country_olddata = $obj->country_olddata; // deprecated
676  $this->email = $obj->email;
677  $this->phone = $obj->phone;
678  $this->phone_mobile = $obj->phone_mobile;
679  $this->project = $obj->project_ref;
680  $this->fk_projet = $obj->fk_project; // deprecated
681  $this->fk_project = $obj->fk_project;
682  $this->public = $obj->public;
683  $this->mode_reglement_id = $obj->fk_payment;
684  $this->mode_reglement_code = $obj->payment_code;
685  $this->mode_reglement = $obj->payment_label;
686  $this->paid = $obj->paid;
687  $this->amount = $obj->amount;
688  $this->note_private = $obj->note_private;
689  $this->note_public = $obj->note_public;
690  $this->model_pdf = $obj->model_pdf;
691  $this->modelpdf = $obj->model_pdf; // deprecated
692 
693  // Retrieve all extrafield
694  // fetch optionals attributes and labels
695  $this->fetch_optionals();
696  }
697  return 1;
698  } else {
699  dol_print_error($this->db);
700  return -1;
701  }
702  }
703 
711  public function setValid($user, $notrigger = 0)
712  {
713  return $this->valid_promesse($this->id, $user->id, $notrigger);
714  }
715 
716  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
725  public function valid_promesse($id, $userid, $notrigger = 0)
726  {
727  // phpcs:enable
728  global $user;
729 
730  $error = 0;
731 
732  $this->db->begin();
733 
734  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
735 
736  $resql = $this->db->query($sql);
737  if ($resql) {
738  if ($this->db->affected_rows($resql)) {
739  if (!$notrigger) {
740  // Call trigger
741  $result = $this->call_trigger('DON_VALIDATE', $user);
742  if ($result < 0) {
743  $error++;
744  }
745  // End call triggers
746  }
747  }
748  } else {
749  $error++;
750  $this->error = $this->db->lasterror();
751  }
752 
753  if (!$error) {
754  $this->statut = 1;
755  $this->db->commit();
756  return 1;
757  } else {
758  $this->db->rollback();
759  return -1;
760  }
761  }
762 
770  public function setPaid($id, $modepayment = 0)
771  {
772  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
773  if ($modepayment) {
774  $sql .= ", fk_payment = ".((int) $modepayment);
775  }
776  $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
777 
778  $resql = $this->db->query($sql);
779  if ($resql) {
780  if ($this->db->affected_rows($resql)) {
781  $this->statut = 2;
782  $this->paid = 1;
783  return 1;
784  } else {
785  return 0;
786  }
787  } else {
788  dol_print_error($this->db);
789  return -1;
790  }
791  }
792 
793  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
800  public function set_cancel($id)
801  {
802  // phpcs:enable
803  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
804 
805  $resql = $this->db->query($sql);
806  if ($resql) {
807  if ($this->db->affected_rows($resql)) {
808  $this->statut = -1;
809  return 1;
810  } else {
811  return 0;
812  }
813  } else {
814  dol_print_error($this->db);
815  return -1;
816  }
817  }
818 
826  public function reopen($user, $notrigger = 0)
827  {
828  // Protection
829  if ($this->statut != self::STATUS_CANCELED) {
830  return 0;
831  }
832 
833  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->bom->write))
834  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->bom->bom_advance->validate))))
835  {
836  $this->error='Permission denied';
837  return -1;
838  }*/
839 
840  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
841  }
842 
843  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
850  public function sum_donations($param)
851  {
852  // phpcs:enable
853  global $conf;
854 
855  $result = 0;
856 
857  $sql = "SELECT sum(amount) as total";
858  $sql .= " FROM ".MAIN_DB_PREFIX."don";
859  $sql .= " WHERE fk_statut = ".((int) $param);
860  $sql .= " AND entity = ".$conf->entity;
861 
862  $resql = $this->db->query($sql);
863  if ($resql) {
864  $obj = $this->db->fetch_object($resql);
865  $result = $obj->total;
866  }
867 
868  return $result;
869  }
870 
871  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
877  public function load_state_board()
878  {
879  // phpcs:enable
880  $this->nb = array();
881 
882  $sql = "SELECT count(d.rowid) as nb";
883  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
884  $sql .= " WHERE d.fk_statut > 0";
885  $sql .= " AND d.entity IN (".getEntity('donation').")";
886 
887  $resql = $this->db->query($sql);
888  if ($resql) {
889  while ($obj = $this->db->fetch_object($resql)) {
890  $this->nb["donations"] = $obj->nb;
891  }
892  $this->db->free($resql);
893  return 1;
894  } else {
895  dol_print_error($this->db);
896  $this->error = $this->db->error();
897  return -1;
898  }
899  }
900 
910  public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
911  {
912  global $conf, $langs, $hookmanager;
913 
914  if (!empty($conf->dol_no_mouse_hover)) {
915  $notooltip = 1; // Force disable tooltips
916  }
917 
918  $result = '';
919  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
920  if (isset($this->status)) {
921  $label .= ' '.$this->getLibStatut(5);
922  }
923  if (!empty($this->id)) {
924  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
925  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
926  }
927  if ($moretitle) {
928  $label .= ' - '.$moretitle;
929  }
930 
931  $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
932 
933  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
934  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
935  $add_save_lastsearch_values = 1;
936  }
937  if ($add_save_lastsearch_values) {
938  $url .= '&save_lastsearch_values=1';
939  }
940 
941  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
942  $linkend = '</a>';
943 
944  $result .= $linkstart;
945  if ($withpicto) {
946  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
947  }
948  if ($withpicto != 2) {
949  $result .= $this->ref;
950  }
951  $result .= $linkend;
952  global $action;
953  $hookmanager->initHooks(array($this->element . 'dao'));
954  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
955  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
956  if ($reshook > 0) {
957  $result = $hookmanager->resPrint;
958  } else {
959  $result .= $hookmanager->resPrint;
960  }
961  return $result;
962  }
963 
970  public function info($id)
971  {
972  $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
973  $sql .= ' d.tms as datem';
974  $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
975  $sql .= ' WHERE d.rowid = '.((int) $id);
976 
977  dol_syslog(get_class($this).'::info', LOG_DEBUG);
978  $result = $this->db->query($sql);
979 
980  if ($result) {
981  if ($this->db->num_rows($result)) {
982  $obj = $this->db->fetch_object($result);
983  $this->id = $obj->rowid;
984 
985  $this->user_creation_id = $obj->fk_user_author;
986  $this->user_validation_id = $obj->fk_user_valid;
987  $this->date_creation = $this->db->jdate($obj->datec);
988  $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
989  }
990  $this->db->free($result);
991  } else {
992  dol_print_error($this->db);
993  }
994  }
995 
996 
1007  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1008  {
1009  global $conf, $langs;
1010 
1011  $langs->load("bills");
1012 
1013  if (!dol_strlen($modele)) {
1014  $modele = 'html_cerfafr';
1015 
1016  if ($this->model_pdf) {
1017  $modele = $this->model_pdf;
1018  } elseif (!empty($conf->global->DON_ADDON_MODEL)) {
1019  $modele = $conf->global->DON_ADDON_MODEL;
1020  }
1021  }
1022 
1023  //$modelpath = "core/modules/dons/";
1024 
1025  // TODO Restore use of commonGenerateDocument instead of dedicated code here
1026  //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1027 
1028  // Increase limit for PDF build
1029  $err = error_reporting();
1030  error_reporting(0);
1031  @set_time_limit(120);
1032  error_reporting($err);
1033 
1034  $srctemplatepath = '';
1035 
1036  // If selected modele is a filename template (then $modele="modelname:filename")
1037  $tmp = explode(':', $modele, 2);
1038  if (!empty($tmp[1])) {
1039  $modele = $tmp[0];
1040  $srctemplatepath = $tmp[1];
1041  }
1042 
1043  // Search template files
1044  $file = ''; $classname = ''; $filefound = 0;
1045  $dirmodels = array('/');
1046  if (is_array($conf->modules_parts['models'])) {
1047  $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1048  }
1049  foreach ($dirmodels as $reldir) {
1050  foreach (array('html', 'doc', 'pdf') as $prefix) {
1051  $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1052 
1053  // On verifie l'emplacement du modele
1054  $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1055  if (file_exists($file)) {
1056  $filefound = 1;
1057  $classname = $prefix.'_'.$modele;
1058  break;
1059  }
1060  }
1061  if ($filefound) {
1062  break;
1063  }
1064  }
1065 
1066  // Charge le modele
1067  if ($filefound) {
1068  require_once $file;
1069 
1070  $object = $this;
1071 
1072  $classname = $modele;
1073  $obj = new $classname($this->db);
1074 
1075  // We save charset_output to restore it because write_file can change it if needed for
1076  // output format that does not support UTF8.
1077  $sav_charset_output = $outputlangs->charset_output;
1078  if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1079  $outputlangs->charset_output = $sav_charset_output;
1080 
1081  // we delete preview files
1082  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1083  dol_delete_preview($object);
1084  return 1;
1085  } else {
1086  $outputlangs->charset_output = $sav_charset_output;
1087  dol_syslog("Erreur dans don_create");
1088  dol_print_error($this->db, $obj->error);
1089  return 0;
1090  }
1091  } else {
1092  print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1093  return 0;
1094  }
1095  }
1096 
1105  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1106  {
1107  $tables = array(
1108  'don'
1109  );
1110 
1111  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1112  }
1113 
1119  public function getRemainToPay()
1120  {
1121  dol_syslog(__METHOD__, LOG_DEBUG);
1122 
1123  if (empty($this->id)) {
1124  $this->error = 'Missing object id';
1125  $this->errors[] = $this->error;
1126  dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1127  return -1;
1128  }
1129 
1130  $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1131  $resql = $this->db->query($sql);
1132  if (!$resql) {
1133  dol_print_error($this->db);
1134  return -2;
1135  } else {
1136  $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1137  return (float) $this->amount - $sum_amount;
1138  }
1139  }
1140 
1148  public function getKanbanView($option = '', $arraydata = null)
1149  {
1150  global $langs;
1151 
1152  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1153 
1154  $return = '<div class="box-flex-item box-flex-grow-zero">';
1155  $return .= '<div class="info-box info-box-sm">';
1156  $return .= '<span class="info-box-icon bg-infobox-action">';
1157  $return .= img_picto('', $this->picto);
1158  $return .= '</span>';
1159  $return .= '<div class="info-box-content">';
1160  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
1161  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1162  if (property_exists($this, 'date')) {
1163  $return .= ' | <span class="opacitymedium" >'.$langs->trans("Date").'</span> : <span class="info-box-label">'.dol_print_date($this->date).'</span>';
1164  }
1165  if (property_exists($this, 'societe') && !empty($this->societe)) {
1166  $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1167  }
1168  if (property_exists($this, 'amount')) {
1169  $return .= '<br><span class="opacitymedium" >'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
1170  }
1171  if (method_exists($this, 'LibStatut')) {
1172  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1173  }
1174  $return .= '</div>';
1175  $return .= '</div>';
1176  $return .= '</div>';
1177  return $return;
1178  }
1179 }
Don\setPaid
setPaid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:770
Don\create
create($user, $notrigger=0)
Create donation record into database.
Definition: don.class.php:359
CommonObject\setStatusCommon
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
Definition: commonobject.class.php:9852
Don\LibStatut
LibStatut($status, $mode=0)
Return the label of a given status.
Definition: don.class.php:189
Don\getKanbanView
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: don.class.php:1148
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
dol_escape_htmltag
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...
Definition: functions.lib.php:1600
Don\setValid
setValid($user, $notrigger=0)
Validate a intervention.
Definition: don.class.php:711
Don\info
info($id)
Information on record.
Definition: don.class.php:970
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:5107
Don\getRemainToPay
getRemainToPay()
Function to get reamain to pay for a donation.
Definition: don.class.php:1119
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1158
Don\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: don.class.php:1105
Don
Class to manage donations.
Definition: don.class.php:39
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:45
Don\check
check($minimum=0)
Check params and init ->errors array.
Definition: don.class.php:282
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5955
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2675
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:4135
Don\fetch
fetch($id, $ref='')
Load donation from database.
Definition: don.class.php:629
Don\sum_donations
sum_donations($param)
Sum of donations.
Definition: don.class.php:850
Don\reopen
reopen($user, $notrigger=0)
Set cancel status.
Definition: don.class.php:826
CommonPeople
trait CommonPeople
Superclass for thirdparties, contacts, members or users.
Definition: commonpeople.class.php:29
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6137
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1741
Don\valid_promesse
valid_promesse($id, $userid, $notrigger=0)
Validate a promise of donation.
Definition: don.class.php:725
Don\set_cancel
set_cancel($id)
Set donation to status cancelled.
Definition: don.class.php:800
CommonObject\fetch_optionals
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...
Definition: commonobject.class.php:5986
$sql
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->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') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3997
Don\update
update($user, $notrigger=0)
Update a donation record.
Definition: don.class.php:479
ref
$object ref
Definition: info.php:78
getDolGlobalString
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:142
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1537
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10967
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4473
Don\getNomUrl
getNomUrl($withpicto=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: don.class.php:910
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3056
price
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
Definition: functions.lib.php:5829
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5770
Don\getLibStatut
getLibStatut($mode=0)
Returns the donation status label (draft, valid, abandoned, paid)
Definition: don.class.php:176
Don\__construct
__construct($db)
Constructor.
Definition: don.class.php:164
Don\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: don.class.php:224
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8504
Don\load_state_board
load_state_board()
Charge indicateurs this->nb pour le tableau de bord.
Definition: don.class.php:877
Don\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: don.class.php:1007
float
div float
Buy price without taxes.
Definition: style.css.php:921