dolibarr  16.0.5
don.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6  * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
8  * Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
9  * Copyright (C) 2019-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 
34 
38 class Don extends CommonObject
39 {
43  public $element = 'don';
44 
48  public $table_element = 'don';
49 
53  public $fk_element = 'fk_donation';
54 
59  public $ismultientitymanaged = 1;
60 
64  public $picto = 'donation';
65 
69  public $date;
70 
75  public $amount;
76 
80  public $societe;
81 
85  public $address;
86 
90  public $zip;
91 
95  public $town;
96 
100  public $email;
101 
105  public $public;
106 
110  public $fk_project;
111 
115  public $fk_typepayment;
116 
117  public $num_payment;
118  public $date_valid;
119 
123  public $modepaymentid = 0;
124 
128  public $labelStatus;
129 
133  public $labelStatusShort;
134 
135 
136  const STATUS_DRAFT = 0;
137  const STATUS_VALIDATED = 1;
138  const STATUS_PAID = 2;
139  const STATUS_CANCELED = -1;
140 
141 
147  public function __construct($db)
148  {
149  $this->db = $db;
150  }
151 
152 
159  public function getLibStatut($mode = 0)
160  {
161  return $this->LibStatut($this->statut, $mode);
162  }
163 
164  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
172  public function LibStatut($status, $mode = 0)
173  {
174  // phpcs:enable
175  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
176  global $langs;
177  $langs->load("donations");
178  $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
179  $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
180  $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
181  $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
182  $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
183  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
184  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
185  $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
186  }
187 
188  $statusType = 'status'.$status;
189  if ($status == self::STATUS_CANCELED) {
190  $statusType = 'status9';
191  }
192  if ($status == self::STATUS_PAID) {
193  $statusType = 'status6';
194  }
195 
196  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
197  }
198 
199 
207  public function initAsSpecimen()
208  {
209  global $conf, $user, $langs;
210 
211  $now = dol_now();
212 
213  // Charge tableau des id de societe socids
214  $socids = array();
215 
216  $sql = "SELECT rowid";
217  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
218  $sql .= " WHERE client IN (1, 3)";
219  $sql .= " AND entity = ".$conf->entity;
220  $sql .= " LIMIT 10";
221 
222  $resql = $this->db->query($sql);
223  if ($resql) {
224  $num_socs = $this->db->num_rows($resql);
225  $i = 0;
226  while ($i < $num_socs) {
227  $i++;
228 
229  $row = $this->db->fetch_row($resql);
230  $socids[$i] = $row[0];
231  }
232  }
233 
234  // Initialise parametres
235  $this->id = 0;
236  $this->ref = 'SPECIMEN';
237  $this->specimen = 1;
238  $this->lastname = 'Doe';
239  $this->firstname = 'John';
240  $this->socid = 1;
241  $this->date = $now;
242  $this->date_valid = $now;
243  $this->amount = 100.90;
244  $this->public = 1;
245  $this->societe = 'The Company';
246  $this->address = 'Twist road';
247  $this->zip = '99999';
248  $this->town = 'Town';
249  $this->note_private = 'Private note';
250  $this->note_public = 'Public note';
251  $this->email = 'email@email.com';
252  $this->phone = '0123456789';
253  $this->phone_mobile = '0606060606';
254  $this->statut = 1;
255  }
256 
257 
265  public function check($minimum = 0)
266  {
267  global $langs;
268  $langs->load('main');
269  $langs->load('companies');
270 
271  $error_string = array();
272  $err = 0;
273 
274  if (dol_strlen(trim($this->societe)) == 0) {
275  if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
276  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
277  $err++;
278  }
279  }
280 
281  if (dol_strlen(trim($this->address)) == 0) {
282  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
283  $err++;
284  }
285 
286  if (dol_strlen(trim($this->zip)) == 0) {
287  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
288  $err++;
289  }
290 
291  if (dol_strlen(trim($this->town)) == 0) {
292  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
293  $err++;
294  }
295 
296  if (dol_strlen(trim($this->email)) == 0) {
297  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
298  $err++;
299  }
300 
301  $this->amount = trim($this->amount);
302 
303  $map = range(0, 9);
304  $len = dol_strlen($this->amount);
305  for ($i = 0; $i < $len; $i++) {
306  if (!isset($map[substr($this->amount, $i, 1)])) {
307  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
308  $err++;
309  $amount_invalid = 1;
310  break;
311  }
312  }
313 
314  if (!$amount_invalid) {
315  if ($this->amount == 0) {
316  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
317  $err++;
318  } else {
319  if ($this->amount < $minimum && $minimum > 0) {
320  $error_string[] = $langs->trans('MinimumAmount', $langs->transnoentitiesnoconv('$minimum'));
321  $err++;
322  }
323  }
324  }
325 
326  if ($err) {
327  $this->errors = $error_string;
328  return 0;
329  } else {
330  return 1;
331  }
332  }
333 
342  public function create($user, $notrigger = 0)
343  {
344  global $conf, $langs;
345 
346  $error = 0;
347  $ret = 0;
348  $now = dol_now();
349 
350  // Clean parameters
351  $this->address = ($this->address > 0 ? $this->address : $this->address);
352  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
353  $this->town = ($this->town > 0 ? $this->town : $this->town);
354  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
355  $this->country = ($this->country ? $this->country : $this->country);
356  $this->amount = price2num($this->amount);
357 
358  // Check parameters
359  if ($this->amount < 0) {
360  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
361  return -1;
362  }
363 
364  $this->db->begin();
365 
366  $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
367  $sql .= "datec";
368  $sql .= ", entity";
369  $sql .= ", amount";
370  $sql .= ", fk_payment";
371  $sql .= ", fk_soc";
372  $sql .= ", firstname";
373  $sql .= ", lastname";
374  $sql .= ", societe";
375  $sql .= ", address";
376  $sql .= ", zip";
377  $sql .= ", town";
378  $sql .= ", fk_country";
379  $sql .= ", public";
380  $sql .= ", fk_projet";
381  $sql .= ", note_private";
382  $sql .= ", note_public";
383  $sql .= ", fk_user_author";
384  $sql .= ", fk_user_valid";
385  $sql .= ", datedon";
386  $sql .= ", email";
387  $sql .= ", phone";
388  $sql .= ", phone_mobile";
389  $sql .= ") VALUES (";
390  $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
391  $sql .= ", ".((int) $conf->entity);
392  $sql .= ", ".((float) $this->amount);
393  $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
394  $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
395  $sql .= ", '".$this->db->escape($this->firstname)."'";
396  $sql .= ", '".$this->db->escape($this->lastname)."'";
397  $sql .= ", '".$this->db->escape($this->societe)."'";
398  $sql .= ", '".$this->db->escape($this->address)."'";
399  $sql .= ", '".$this->db->escape($this->zip)."'";
400  $sql .= ", '".$this->db->escape($this->town)."'";
401  $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
402  $sql .= ", ".(int) $this->public;
403  $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
404  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
405  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
406  $sql .= ", ".((int) $user->id);
407  $sql .= ", null";
408  $sql .= ", '".$this->db->idate($this->date)."'";
409  $sql .= ", '".$this->db->escape(trim($this->email))."'";
410  $sql .= ", '".$this->db->escape(trim($this->phone))."'";
411  $sql .= ", '".$this->db->escape(trim($this->phone_mobile))."'";
412  $sql .= ")";
413 
414  $resql = $this->db->query($sql);
415  if ($resql) {
416  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
417  $ret = $this->id;
418 
419  if (!$notrigger) {
420  // Call trigger
421  $result = $this->call_trigger('DON_CREATE', $user);
422  if ($result < 0) {
423  $error++;
424  }
425  // End call triggers
426  }
427  } else {
428  $this->error = $this->db->lasterror();
429  $this->errno = $this->db->lasterrno();
430  $error++;
431  }
432 
433  // Update extrafield
434  if (!$error) {
435  $result = $this->insertExtraFields();
436  if ($result < 0) {
437  $error++;
438  }
439  }
440 
441  if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS)) {
442  //$res = $this->setValid($user);
443  //if ($res < 0) $error++;
444  }
445 
446  if (!$error) {
447  $this->db->commit();
448  return $ret;
449  } else {
450  $this->db->rollback();
451  return -1;
452  }
453  }
454 
462  public function update($user, $notrigger = 0)
463  {
464  global $langs, $conf;
465 
466  $error = 0;
467 
468  // Clean parameters
469  $this->address = ($this->address > 0 ? $this->address : $this->address);
470  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
471  $this->town = ($this->town > 0 ? $this->town : $this->town);
472  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
473  $this->country = ($this->country ? $this->country : $this->country);
474  $this->amount = price2num($this->amount);
475 
476  // Check parameters
477  if ($this->amount < 0) {
478  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
479  return -1;
480  }
481 
482  $this->db->begin();
483 
484  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
485  $sql .= " amount = ".((float) $this->amount);
486  $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
487  $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
488  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
489  $sql .= ", societe='".$this->db->escape($this->societe)."'";
490  $sql .= ", address='".$this->db->escape($this->address)."'";
491  $sql .= ", zip='".$this->db->escape($this->zip)."'";
492  $sql .= ", town='".$this->db->escape($this->town)."'";
493  $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
494  $sql .= ", public=".((int) $this->public);
495  $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
496  $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
497  $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
498  $sql .= ", datedon='".$this->db->idate($this->date)."'";
499  $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
500  $sql .= ", email='".$this->db->escape(trim($this->email))."'";
501  $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
502  $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
503  $sql .= ", fk_statut=".((int) $this->statut);
504  $sql .= " WHERE rowid = ".((int) $this->id);
505 
506  dol_syslog(get_class($this)."::Update", LOG_DEBUG);
507  $resql = $this->db->query($sql);
508  if ($resql) {
509  if (!$notrigger) {
510  // Call trigger
511  $result = $this->call_trigger('DON_MODIFY', $user);
512  if ($result < 0) {
513  $error++;
514  }
515  // End call triggers
516  }
517 
518  // Update extrafield
519  if (!$error) {
520  $result = $this->insertExtraFields();
521  if ($result < 0) {
522  $error++;
523  }
524  }
525 
526  if (!$error) {
527  $this->db->commit();
528  $result = 1;
529  } else {
530  $this->db->rollback();
531  $result = -1;
532  }
533  } else {
534  $this->error = $this->db->lasterror();
535  $this->errors[] = $this->error;
536  $this->db->rollback();
537  dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
538  $result = -2;
539  }
540  return $result;
541  }
542 
550  public function delete($user, $notrigger = 0)
551  {
552  global $user, $conf, $langs;
553  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
554 
555  $error = 0;
556 
557  $this->db->begin();
558 
559  if (!$error && !$notrigger) {
560  // Call trigger
561  $result = $this->call_trigger('DON_DELETE', $user);
562 
563  if ($result < 0) {
564  $error++;
565  }
566  // End call triggers
567  }
568 
569  // Delete donation
570  if (!$error) {
571  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
572  $sql .= " WHERE fk_object = ".((int) $this->id);
573 
574  $resql = $this->db->query($sql);
575  if (!$resql) {
576  $this->errors[] = $this->db->lasterror();
577  $error++;
578  }
579  }
580 
581  if (!$error) {
582  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
583  $sql .= " WHERE rowid=".((int) $this->id);
584 
585  $resql = $this->db->query($sql);
586  if (!$resql) {
587  $this->errors[] = $this->db->lasterror();
588  $error++;
589  }
590  }
591 
592  if (!$error) {
593  $this->db->commit();
594  return 1;
595  } else {
596  foreach ($this->errors as $errmsg) {
597  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
598  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
599  }
600  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
601  $this->db->rollback();
602  return -1;
603  }
604  }
605 
613  public function fetch($id, $ref = '')
614  {
615  global $conf;
616 
617  $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
618  $sql .= " d.fk_soc as socid,d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
619  $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, ";
620  $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
621  $sql .= " p.ref as project_ref,";
622  $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
623  $sql .= " c.code as country_code, c.label as country";
624  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
625  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
626  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
627  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
628  $sql .= " WHERE d.entity IN (".getEntity('donation').")";
629  if (!empty($id)) {
630  $sql .= " AND d.rowid=".((int) $id);
631  } elseif (!empty($ref)) {
632  $sql .= " AND d.ref='".$this->db->escape($ref)."'";
633  }
634 
635  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
636  $resql = $this->db->query($sql);
637  if ($resql) {
638  if ($this->db->num_rows($resql)) {
639  $obj = $this->db->fetch_object($resql);
640 
641  $this->id = $obj->rowid;
642  $this->ref = $obj->rowid;
643  $this->date_creation = $this->db->jdate($obj->datec);
644  $this->datec = $this->db->jdate($obj->datec);
645  $this->date_validation = $this->db->jdate($obj->date_valid);
646  $this->date_valid = $this->db->jdate($obj->date_valid);
647  $this->date_modification = $this->db->jdate($obj->datem);
648  $this->datem = $this->db->jdate($obj->datem);
649  $this->date = $this->db->jdate($obj->datedon);
650  $this->socid = $obj->socid;
651  $this->firstname = $obj->firstname;
652  $this->lastname = $obj->lastname;
653  $this->societe = $obj->societe;
654  $this->statut = $obj->fk_statut;
655  $this->address = $obj->address;
656  $this->zip = $obj->zip;
657  $this->town = $obj->town;
658  $this->country_id = $obj->fk_country;
659  $this->country_code = $obj->country_code;
660  $this->country = $obj->country;
661  $this->country_olddata = $obj->country_olddata; // deprecated
662  $this->email = $obj->email;
663  $this->phone = $obj->phone;
664  $this->phone_mobile = $obj->phone_mobile;
665  $this->project = $obj->project_ref;
666  $this->fk_projet = $obj->fk_project; // deprecated
667  $this->fk_project = $obj->fk_project;
668  $this->public = $obj->public;
669  $this->mode_reglement_id = $obj->fk_payment;
670  $this->mode_reglement_code = $obj->payment_code;
671  $this->mode_reglement = $obj->payment_label;
672  $this->paid = $obj->paid;
673  $this->amount = $obj->amount;
674  $this->note_private = $obj->note_private;
675  $this->note_public = $obj->note_public;
676  $this->model_pdf = $obj->model_pdf;
677  $this->modelpdf = $obj->model_pdf; // deprecated
678 
679  // Retrieve all extrafield
680  // fetch optionals attributes and labels
681  $this->fetch_optionals();
682  }
683  return 1;
684  } else {
685  dol_print_error($this->db);
686  return -1;
687  }
688  }
689 
697  public function setValid($user, $notrigger = 0)
698  {
699  return $this->valid_promesse($this->id, $user->id, $notrigger);
700  }
701 
702  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
711  public function valid_promesse($id, $userid, $notrigger = 0)
712  {
713  // phpcs:enable
714  global $langs, $user;
715 
716  $error = 0;
717 
718  $this->db->begin();
719 
720  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".((int) $userid)." WHERE rowid = ".((int) $id)." AND fk_statut = 0";
721 
722  $resql = $this->db->query($sql);
723  if ($resql) {
724  if ($this->db->affected_rows($resql)) {
725  if (!$notrigger) {
726  // Call trigger
727  $result = $this->call_trigger('DON_VALIDATE', $user);
728  if ($result < 0) {
729  $error++;
730  }
731  // End call triggers
732  }
733  }
734  } else {
735  $error++;
736  $this->error = $this->db->lasterror();
737  }
738 
739  if (!$error) {
740  $this->statut = 1;
741  $this->db->commit();
742  return 1;
743  } else {
744  $this->db->rollback();
745  return -1;
746  }
747  }
748 
749  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
759  public function set_paid($id, $modepayment = 0)
760  {
761  // phpcs:enable
762  dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
763  return $this->setPaid($id, $modepayment);
764  }
765 
773  public function setPaid($id, $modepayment = 0)
774  {
775  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
776  if ($modepayment) {
777  $sql .= ", fk_payment = ".((int) $modepayment);
778  }
779  $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
780 
781  $resql = $this->db->query($sql);
782  if ($resql) {
783  if ($this->db->affected_rows($resql)) {
784  $this->statut = 2;
785  $this->paid = 1;
786  return 1;
787  } else {
788  return 0;
789  }
790  } else {
791  dol_print_error($this->db);
792  return -1;
793  }
794  }
795 
796  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
803  public function set_cancel($id)
804  {
805  // phpcs:enable
806  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
807 
808  $resql = $this->db->query($sql);
809  if ($resql) {
810  if ($this->db->affected_rows($resql)) {
811  $this->statut = -1;
812  return 1;
813  } else {
814  return 0;
815  }
816  } else {
817  dol_print_error($this->db);
818  return -1;
819  }
820  }
821 
829  public function reopen($user, $notrigger = 0)
830  {
831  // Protection
832  if ($this->statut != self::STATUS_CANCELED) {
833  return 0;
834  }
835 
836  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write))
837  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
838  {
839  $this->error='Permission denied';
840  return -1;
841  }*/
842 
843  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
844  }
845 
846  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
853  public function sum_donations($param)
854  {
855  // phpcs:enable
856  global $conf;
857 
858  $result = 0;
859 
860  $sql = "SELECT sum(amount) as total";
861  $sql .= " FROM ".MAIN_DB_PREFIX."don";
862  $sql .= " WHERE fk_statut = ".((int) $param);
863  $sql .= " AND entity = ".$conf->entity;
864 
865  $resql = $this->db->query($sql);
866  if ($resql) {
867  $obj = $this->db->fetch_object($resql);
868  $result = $obj->total;
869  }
870 
871  return $result;
872  }
873 
874  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
880  public function load_state_board()
881  {
882  // phpcs:enable
883  global $conf;
884 
885  $this->nb = array();
886 
887  $sql = "SELECT count(d.rowid) as nb";
888  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
889  $sql .= " WHERE d.fk_statut > 0";
890  $sql .= " AND d.entity IN (".getEntity('donation').")";
891 
892  $resql = $this->db->query($sql);
893  if ($resql) {
894  while ($obj = $this->db->fetch_object($resql)) {
895  $this->nb["donations"] = $obj->nb;
896  }
897  $this->db->free($resql);
898  return 1;
899  } else {
900  dol_print_error($this->db);
901  $this->error = $this->db->error();
902  return -1;
903  }
904  }
905 
915  public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
916  {
917  global $conf, $langs, $hookmanager;
918 
919  if (!empty($conf->dol_no_mouse_hover)) {
920  $notooltip = 1; // Force disable tooltips
921  }
922 
923  $result = '';
924  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
925  if (isset($this->status)) {
926  $label .= ' '.$this->getLibStatut(5);
927  }
928  if (!empty($this->id)) {
929  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
930  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
931  }
932  if ($moretitle) {
933  $label .= ' - '.$moretitle;
934  }
935 
936  $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
937 
938  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
939  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
940  $add_save_lastsearch_values = 1;
941  }
942  if ($add_save_lastsearch_values) {
943  $url .= '&save_lastsearch_values=1';
944  }
945 
946  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
947  $linkend = '</a>';
948 
949  $result .= $linkstart;
950  if ($withpicto) {
951  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
952  }
953  if ($withpicto != 2) {
954  $result .= $this->ref;
955  }
956  $result .= $linkend;
957  global $action;
958  $hookmanager->initHooks(array($this->element . 'dao'));
959  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
960  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
961  if ($reshook > 0) {
962  $result = $hookmanager->resPrint;
963  } else {
964  $result .= $hookmanager->resPrint;
965  }
966  return $result;
967  }
968 
975  public function info($id)
976  {
977  $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
978  $sql .= ' d.tms as datem';
979  $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
980  $sql .= ' WHERE d.rowid = '.((int) $id);
981 
982  dol_syslog(get_class($this).'::info', LOG_DEBUG);
983  $result = $this->db->query($sql);
984 
985  if ($result) {
986  if ($this->db->num_rows($result)) {
987  $obj = $this->db->fetch_object($result);
988  $this->id = $obj->rowid;
989 
990  $this->user_creation_id = $obj->fk_user_author;
991  $this->user_validation_id = $obj->fk_user_valid;
992  $this->date_creation = $this->db->jdate($obj->datec);
993  $this->date_modification = $this->db->jdate($obj->tms);
994  }
995  $this->db->free($result);
996  } else {
997  dol_print_error($this->db);
998  }
999  }
1000 
1001 
1012  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1013  {
1014  global $conf, $langs;
1015 
1016  $langs->load("bills");
1017 
1018  if (!dol_strlen($modele)) {
1019  $modele = 'html_cerfafr';
1020 
1021  if ($this->model_pdf) {
1022  $modele = $this->model_pdf;
1023  } elseif (!empty($conf->global->DON_ADDON_MODEL)) {
1024  $modele = $conf->global->DON_ADDON_MODEL;
1025  }
1026  }
1027 
1028  $modelpath = "core/modules/dons/";
1029 
1030  // TODO Restore use of commonGenerateDocument instead of dedicated code here
1031  //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1032 
1033  // Increase limit for PDF build
1034  $err = error_reporting();
1035  error_reporting(0);
1036  @set_time_limit(120);
1037  error_reporting($err);
1038 
1039  $srctemplatepath = '';
1040 
1041  // If selected modele is a filename template (then $modele="modelname:filename")
1042  $tmp = explode(':', $modele, 2);
1043  if (!empty($tmp[1])) {
1044  $modele = $tmp[0];
1045  $srctemplatepath = $tmp[1];
1046  }
1047 
1048  // Search template files
1049  $file = ''; $classname = ''; $filefound = 0;
1050  $dirmodels = array('/');
1051  if (is_array($conf->modules_parts['models'])) {
1052  $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1053  }
1054  foreach ($dirmodels as $reldir) {
1055  foreach (array('html', 'doc', 'pdf') as $prefix) {
1056  $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1057 
1058  // On verifie l'emplacement du modele
1059  $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1060  if (file_exists($file)) {
1061  $filefound = 1;
1062  $classname = $prefix.'_'.$modele;
1063  break;
1064  }
1065  }
1066  if ($filefound) {
1067  break;
1068  }
1069  }
1070 
1071  // Charge le modele
1072  if ($filefound) {
1073  require_once $file;
1074 
1075  $object = $this;
1076 
1077  $classname = $modele;
1078  $obj = new $classname($this->db);
1079 
1080  // We save charset_output to restore it because write_file can change it if needed for
1081  // output format that does not support UTF8.
1082  $sav_charset_output = $outputlangs->charset_output;
1083  if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1084  $outputlangs->charset_output = $sav_charset_output;
1085 
1086  // we delete preview files
1087  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1088  dol_delete_preview($object);
1089  return 1;
1090  } else {
1091  $outputlangs->charset_output = $sav_charset_output;
1092  dol_syslog("Erreur dans don_create");
1093  dol_print_error($this->db, $obj->error);
1094  return 0;
1095  }
1096  } else {
1097  print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1098  return 0;
1099  }
1100  }
1101 
1110  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1111  {
1112  $tables = array(
1113  'don'
1114  );
1115 
1116  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1117  }
1118 
1124  public function getRemainToPay()
1125  {
1126  dol_syslog(__METHOD__, LOG_DEBUG);
1127 
1128  if (empty($this->id)) {
1129  $this->error = 'Missing object id';
1130  $this->errors[] = $this->error;
1131  dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1132  return -1;
1133  }
1134 
1135  $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1136  $resql = $this->db->query($sql);
1137  if (!$resql) {
1138  dol_print_error($this->db);
1139  return -2;
1140  } else {
1141  $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1142  return (float) $this->amount - $sum_amount;
1143  }
1144  }
1145 }
Don\setPaid
setPaid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:773
Don\create
create($user, $notrigger=0)
Create donation record into database.
Definition: don.class.php:342
CommonObject\setStatusCommon
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
Definition: commonobject.class.php:9683
db
$conf db
API class for accounts.
Definition: inc.php:41
Don\LibStatut
LibStatut($status, $mode=0)
Return the label of a given status.
Definition: don.class.php:172
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields.
Definition: functions.lib.php:1468
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
Don\setValid
setValid($user, $notrigger=0)
Validate a intervention.
Definition: don.class.php:697
Don\info
info($id)
Information on record.
Definition: don.class.php:975
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:4844
Don\getRemainToPay
getRemainToPay()
Function to get reamain to pay for a donation.
Definition: don.class.php:1124
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1062
ref
$object ref
Definition: info.php:77
Don
Class to manage donations.
Definition: don.class.php:38
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
Don\check
check($minimum=0)
Check params and init ->errors array.
Definition: don.class.php:265
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
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:2514
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:3880
Don\fetch
fetch($id, $ref='')
Load donation from database.
Definition: don.class.php:613
Don\sum_donations
sum_donations($param)
Sum of donations.
Definition: don.class.php:853
Don\reopen
reopen($user, $notrigger=0)
Set cancel status.
Definition: don.class.php:829
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6156
CommonObject\commonReplaceThirdparty
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
Definition: commonobject.class.php:8347
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
Don\valid_promesse
valid_promesse($id, $userid, $notrigger=0)
Validate a promise of donation.
Definition: don.class.php:711
Don\set_cancel
set_cancel($id)
Set donation to status cancelled.
Definition: don.class.php:803
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:6007
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3747
Don\update
update($user, $notrigger=0)
Update a donation record.
Definition: don.class.php:462
Don\set_paid
set_paid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:759
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1435
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
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:4211
Don\getNomUrl
getNomUrl($withpicto=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: don.class.php:915
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
Don\replaceThirdparty
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: don.class.php:1110
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791
Don\getLibStatut
getLibStatut($mode=0)
Returns the donation status label (draft, valid, abandoned, paid)
Definition: don.class.php:159
Don\__construct
__construct($db)
Constructor.
Definition: don.class.php:147
Don\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: don.class.php:207
Don\load_state_board
load_state_board()
Charge indicateurs this->nb pour le tableau de bord.
Definition: don.class.php:880
Don\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: don.class.php:1012
float
div float
Buy price without taxes.
Definition: style.css.php:809