dolibarr  18.0.0-alpha
don.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2002 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2009 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
6  * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
7  * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
8  * Copyright (C) 2019 Thibault FOUCART <support@ptibogxiv.net>
9  * Copyright (C) 2019-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 
71  public $datec;
72  public $datem;
73 
78  public $amount;
79 
83  public $socid;
84 
88  public $societe;
89 
93  public $address;
94 
98  public $zip;
99 
103  public $town;
104 
108  public $email;
109 
110  public $phone;
111  public $phone_mobile;
112 
116  public $public;
117 
121  public $fk_project;
122 
126  public $fk_typepayment;
127 
128  public $num_payment;
129  public $date_valid;
130 
134  public $modepaymentid = 0;
135 
136  public $paid;
137 
138 
142  public $labelStatus;
143 
147  public $labelStatusShort;
148 
149 
150  const STATUS_DRAFT = 0;
151  const STATUS_VALIDATED = 1;
152  const STATUS_PAID = 2;
153  const STATUS_CANCELED = -1;
154 
155 
161  public function __construct($db)
162  {
163  $this->db = $db;
164  }
165 
166 
173  public function getLibStatut($mode = 0)
174  {
175  return $this->LibStatut($this->statut, $mode);
176  }
177 
178  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
186  public function LibStatut($status, $mode = 0)
187  {
188  // phpcs:enable
189  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
190  global $langs;
191  $langs->load("donations");
192  $this->labelStatus[-1] = $langs->transnoentitiesnoconv("Canceled");
193  $this->labelStatus[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidated");
194  $this->labelStatus[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidated");
195  $this->labelStatus[2] = $langs->transnoentitiesnoconv("DonationStatusPaid");
196  $this->labelStatusShort[-1] = $langs->transnoentitiesnoconv("Canceled");
197  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv("DonationStatusPromiseNotValidatedShort");
198  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv("DonationStatusPromiseValidatedShort");
199  $this->labelStatusShort[2] = $langs->transnoentitiesnoconv("DonationStatusPaidShort");
200  }
201 
202  $statusType = 'status'.$status;
203  if ($status == self::STATUS_CANCELED) {
204  $statusType = 'status9';
205  }
206  if ($status == self::STATUS_PAID) {
207  $statusType = 'status6';
208  }
209 
210  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
211  }
212 
213 
221  public function initAsSpecimen()
222  {
223  global $conf;
224 
225  $now = dol_now();
226 
227  // Charge tableau des id de societe socids
228  $socids = array();
229 
230  $sql = "SELECT rowid";
231  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
232  $sql .= " WHERE client IN (1, 3)";
233  $sql .= " AND entity = ".$conf->entity;
234  $sql .= " LIMIT 10";
235 
236  $resql = $this->db->query($sql);
237  if ($resql) {
238  $num_socs = $this->db->num_rows($resql);
239  $i = 0;
240  while ($i < $num_socs) {
241  $row = $this->db->fetch_row($resql);
242  $socids[$i] = $row[0];
243 
244  $i++;
245  }
246  }
247 
248  // Initialise parametres
249  $this->id = 0;
250  $this->ref = 'SPECIMEN';
251  $this->specimen = 1;
252  $this->lastname = 'Doe';
253  $this->firstname = 'John';
254  $this->socid = empty($socids[0]) ? 0 : $socids[0];
255  $this->date = $now;
256  $this->date_valid = $now;
257  $this->amount = 100.90;
258  $this->public = 1;
259  $this->societe = 'The Company';
260  $this->address = 'Twist road';
261  $this->zip = '99999';
262  $this->town = 'Town';
263  $this->note_private = 'Private note';
264  $this->note_public = 'Public note';
265  $this->email = 'email@email.com';
266  $this->phone = '0123456789';
267  $this->phone_mobile = '0606060606';
268  $this->statut = 1;
269  }
270 
271 
279  public function check($minimum = 0)
280  {
281  global $langs;
282  $langs->load('main');
283  $langs->load('companies');
284 
285  $error_string = array();
286  $err = 0;
287 
288  if (dol_strlen(trim($this->societe)) == 0) {
289  if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) {
290  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Company').'/'.$langs->transnoentitiesnoconv('Firstname').'-'.$langs->transnoentitiesnoconv('Lastname'));
291  $err++;
292  }
293  }
294 
295  if (dol_strlen(trim($this->address)) == 0) {
296  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
297  $err++;
298  }
299 
300  if (dol_strlen(trim($this->zip)) == 0) {
301  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
302  $err++;
303  }
304 
305  if (dol_strlen(trim($this->town)) == 0) {
306  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
307  $err++;
308  }
309 
310  if (dol_strlen(trim($this->email)) == 0) {
311  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
312  $err++;
313  }
314 
315  $this->amount = trim($this->amount);
316 
317  $map = range(0, 9);
318  $len = dol_strlen($this->amount);
319  for ($i = 0; $i < $len; $i++) {
320  if (!isset($map[substr($this->amount, $i, 1)])) {
321  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
322  $err++;
323  $amount_invalid = 1;
324  break;
325  }
326  }
327 
328  if (!$amount_invalid) {
329  if ($this->amount == 0) {
330  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
331  $err++;
332  } else {
333  if ($this->amount < $minimum && $minimum > 0) {
334  $error_string[] = $langs->trans('MinimumAmount', $langs->transnoentitiesnoconv('$minimum'));
335  $err++;
336  }
337  }
338  }
339 
340  if ($err) {
341  $this->errors = $error_string;
342  return 0;
343  } else {
344  return 1;
345  }
346  }
347 
356  public function create($user, $notrigger = 0)
357  {
358  global $conf, $langs;
359 
360  $error = 0;
361  $ret = 0;
362  $now = dol_now();
363 
364  // Clean parameters
365  $this->address = ($this->address > 0 ? $this->address : $this->address);
366  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
367  $this->town = ($this->town > 0 ? $this->town : $this->town);
368  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
369  $this->country = ($this->country ? $this->country : $this->country);
370  $this->amount = price2num($this->amount);
371 
372  // Check parameters
373  if ($this->amount < 0) {
374  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
375  return -1;
376  }
377 
378  $this->db->begin();
379 
380  $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
381  $sql .= "datec";
382  $sql .= ", entity";
383  $sql .= ", amount";
384  $sql .= ", fk_payment";
385  $sql .= ", fk_soc";
386  $sql .= ", firstname";
387  $sql .= ", lastname";
388  $sql .= ", societe";
389  $sql .= ", address";
390  $sql .= ", zip";
391  $sql .= ", town";
392  $sql .= ", fk_country";
393  $sql .= ", public";
394  $sql .= ", fk_projet";
395  $sql .= ", note_private";
396  $sql .= ", note_public";
397  $sql .= ", fk_user_author";
398  $sql .= ", fk_user_valid";
399  $sql .= ", datedon";
400  $sql .= ", email";
401  $sql .= ", phone";
402  $sql .= ", phone_mobile";
403  $sql .= ") VALUES (";
404  $sql .= "'".$this->db->idate($this->date ? $this->date : $now)."'";
405  $sql .= ", ".((int) $conf->entity);
406  $sql .= ", ".((float) $this->amount);
407  $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
408  $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
409  $sql .= ", '".$this->db->escape($this->firstname)."'";
410  $sql .= ", '".$this->db->escape($this->lastname)."'";
411  $sql .= ", '".$this->db->escape($this->societe)."'";
412  $sql .= ", '".$this->db->escape($this->address)."'";
413  $sql .= ", '".$this->db->escape($this->zip)."'";
414  $sql .= ", '".$this->db->escape($this->town)."'";
415  $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
416  $sql .= ", ".(int) $this->public;
417  $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
418  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
419  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
420  $sql .= ", ".((int) $user->id);
421  $sql .= ", null";
422  $sql .= ", '".$this->db->idate($this->date)."'";
423  $sql .= ", '".(!empty($this->email) ? $this->db->escape(trim($this->email)) : "")."'";
424  $sql .= ", '".(!empty($this->phone) ? $this->db->escape(trim($this->phone)) : "")."'";
425  $sql .= ", '".(!empty($this->phone_mobile) ? $this->db->escape(trim($this->phone_mobile)) : "")."'";
426  $sql .= ")";
427 
428  $resql = $this->db->query($sql);
429  if ($resql) {
430  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
431  $ret = $this->id;
432 
433  if (!$notrigger) {
434  // Call trigger
435  $result = $this->call_trigger('DON_CREATE', $user);
436  if ($result < 0) {
437  $error++;
438  }
439  // End call triggers
440  }
441  } else {
442  $this->error = $this->db->lasterror();
443  $this->errno = $this->db->lasterrno();
444  $error++;
445  }
446 
447  // Update extrafield
448  if (!$error) {
449  $result = $this->insertExtraFields();
450  if ($result < 0) {
451  $error++;
452  }
453  }
454 
455  if (!$error && (getDolGlobalString('MAIN_DISABLEDRAFTSTATUS') || getDolGlobalString('MAIN_DISABLEDRAFTSTATUS_DONATION'))) {
456  //$res = $this->setValid($user);
457  //if ($res < 0) $error++;
458  }
459 
460  if (!$error) {
461  $this->db->commit();
462  return $ret;
463  } else {
464  $this->db->rollback();
465  return -1;
466  }
467  }
468 
476  public function update($user, $notrigger = 0)
477  {
478  global $langs, $conf;
479 
480  $error = 0;
481 
482  // Clean parameters
483  $this->address = ($this->address > 0 ? $this->address : $this->address);
484  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
485  $this->town = ($this->town > 0 ? $this->town : $this->town);
486  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
487  $this->country = ($this->country ? $this->country : $this->country);
488  $this->amount = price2num($this->amount);
489 
490  // Check parameters
491  if ($this->amount < 0) {
492  $this->error = $langs->trans('FieldCannotBeNegative', $langs->transnoentitiesnoconv("Amount"));
493  return -1;
494  }
495 
496  $this->db->begin();
497 
498  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET";
499  $sql .= " amount = ".((float) $this->amount);
500  $sql .= ", fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
501  $sql .= ", firstname = '".$this->db->escape($this->firstname)."'";
502  $sql .= ", lastname='".$this->db->escape($this->lastname)."'";
503  $sql .= ", societe='".$this->db->escape($this->societe)."'";
504  $sql .= ", address='".$this->db->escape($this->address)."'";
505  $sql .= ", zip='".$this->db->escape($this->zip)."'";
506  $sql .= ", town='".$this->db->escape($this->town)."'";
507  $sql .= ", fk_country = ".($this->country_id > 0 ? ((int) $this->country_id) : '0');
508  $sql .= ", public=".((int) $this->public);
509  $sql .= ", fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
510  $sql .= ", note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
511  $sql .= ", note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
512  $sql .= ", datedon='".$this->db->idate($this->date)."'";
513  $sql .= ", date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
514  $sql .= ", email='".$this->db->escape(trim($this->email))."'";
515  $sql .= ", phone='".$this->db->escape(trim($this->phone))."'";
516  $sql .= ", phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
517  $sql .= ", fk_statut=".((int) $this->statut);
518  $sql .= " WHERE rowid = ".((int) $this->id);
519 
520  dol_syslog(get_class($this)."::Update", LOG_DEBUG);
521  $resql = $this->db->query($sql);
522  if ($resql) {
523  if (!$notrigger) {
524  // Call trigger
525  $result = $this->call_trigger('DON_MODIFY', $user);
526  if ($result < 0) {
527  $error++;
528  }
529  // End call triggers
530  }
531 
532  // Update extrafield
533  if (!$error) {
534  $result = $this->insertExtraFields();
535  if ($result < 0) {
536  $error++;
537  }
538  }
539 
540  if (!$error) {
541  $this->db->commit();
542  $result = 1;
543  } else {
544  $this->db->rollback();
545  $result = -1;
546  }
547  } else {
548  $this->error = $this->db->lasterror();
549  $this->errors[] = $this->error;
550  $this->db->rollback();
551  dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
552  $result = -2;
553  }
554  return $result;
555  }
556 
564  public function delete($user, $notrigger = 0)
565  {
566  global $user, $conf, $langs;
567  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
568 
569  $error = 0;
570 
571  $this->db->begin();
572 
573  if (!$error && !$notrigger) {
574  // Call trigger
575  $result = $this->call_trigger('DON_DELETE', $user);
576 
577  if ($result < 0) {
578  $error++;
579  }
580  // End call triggers
581  }
582 
583  // Delete donation
584  if (!$error) {
585  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
586  $sql .= " WHERE fk_object = ".((int) $this->id);
587 
588  $resql = $this->db->query($sql);
589  if (!$resql) {
590  $this->errors[] = $this->db->lasterror();
591  $error++;
592  }
593  }
594 
595  if (!$error) {
596  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
597  $sql .= " WHERE rowid=".((int) $this->id);
598 
599  $resql = $this->db->query($sql);
600  if (!$resql) {
601  $this->errors[] = $this->db->lasterror();
602  $error++;
603  }
604  }
605 
606  if (!$error) {
607  $this->db->commit();
608  return 1;
609  } else {
610  foreach ($this->errors as $errmsg) {
611  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
612  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
613  }
614  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
615  $this->db->rollback();
616  return -1;
617  }
618  }
619 
627  public function fetch($id, $ref = '')
628  {
629  global $conf;
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 $langs, $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 
763  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
773  public function set_paid($id, $modepayment = 0)
774  {
775  // phpcs:enable
776  dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
777  return $this->setPaid($id, $modepayment);
778  }
779 
787  public function setPaid($id, $modepayment = 0)
788  {
789  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2, paid = 1";
790  if ($modepayment) {
791  $sql .= ", fk_payment = ".((int) $modepayment);
792  }
793  $sql .= " WHERE rowid = ".((int) $id)." AND fk_statut = 1";
794 
795  $resql = $this->db->query($sql);
796  if ($resql) {
797  if ($this->db->affected_rows($resql)) {
798  $this->statut = 2;
799  $this->paid = 1;
800  return 1;
801  } else {
802  return 0;
803  }
804  } else {
805  dol_print_error($this->db);
806  return -1;
807  }
808  }
809 
810  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
817  public function set_cancel($id)
818  {
819  // phpcs:enable
820  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".((int) $id);
821 
822  $resql = $this->db->query($sql);
823  if ($resql) {
824  if ($this->db->affected_rows($resql)) {
825  $this->statut = -1;
826  return 1;
827  } else {
828  return 0;
829  }
830  } else {
831  dol_print_error($this->db);
832  return -1;
833  }
834  }
835 
843  public function reopen($user, $notrigger = 0)
844  {
845  // Protection
846  if ($this->statut != self::STATUS_CANCELED) {
847  return 0;
848  }
849 
850  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->bom->write))
851  || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && !empty($user->rights->bom->bom_advance->validate))))
852  {
853  $this->error='Permission denied';
854  return -1;
855  }*/
856 
857  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
858  }
859 
860  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
867  public function sum_donations($param)
868  {
869  // phpcs:enable
870  global $conf;
871 
872  $result = 0;
873 
874  $sql = "SELECT sum(amount) as total";
875  $sql .= " FROM ".MAIN_DB_PREFIX."don";
876  $sql .= " WHERE fk_statut = ".((int) $param);
877  $sql .= " AND entity = ".$conf->entity;
878 
879  $resql = $this->db->query($sql);
880  if ($resql) {
881  $obj = $this->db->fetch_object($resql);
882  $result = $obj->total;
883  }
884 
885  return $result;
886  }
887 
888  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
894  public function load_state_board()
895  {
896  // phpcs:enable
897  global $conf;
898 
899  $this->nb = array();
900 
901  $sql = "SELECT count(d.rowid) as nb";
902  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
903  $sql .= " WHERE d.fk_statut > 0";
904  $sql .= " AND d.entity IN (".getEntity('donation').")";
905 
906  $resql = $this->db->query($sql);
907  if ($resql) {
908  while ($obj = $this->db->fetch_object($resql)) {
909  $this->nb["donations"] = $obj->nb;
910  }
911  $this->db->free($resql);
912  return 1;
913  } else {
914  dol_print_error($this->db);
915  $this->error = $this->db->error();
916  return -1;
917  }
918  }
919 
929  public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
930  {
931  global $conf, $langs, $hookmanager;
932 
933  if (!empty($conf->dol_no_mouse_hover)) {
934  $notooltip = 1; // Force disable tooltips
935  }
936 
937  $result = '';
938  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
939  if (isset($this->status)) {
940  $label .= ' '.$this->getLibStatut(5);
941  }
942  if (!empty($this->id)) {
943  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
944  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
945  }
946  if ($moretitle) {
947  $label .= ' - '.$moretitle;
948  }
949 
950  $url = DOL_URL_ROOT.'/don/card.php?id='.$this->id;
951 
952  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
953  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
954  $add_save_lastsearch_values = 1;
955  }
956  if ($add_save_lastsearch_values) {
957  $url .= '&save_lastsearch_values=1';
958  }
959 
960  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
961  $linkend = '</a>';
962 
963  $result .= $linkstart;
964  if ($withpicto) {
965  $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
966  }
967  if ($withpicto != 2) {
968  $result .= $this->ref;
969  }
970  $result .= $linkend;
971  global $action;
972  $hookmanager->initHooks(array($this->element . 'dao'));
973  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
974  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
975  if ($reshook > 0) {
976  $result = $hookmanager->resPrint;
977  } else {
978  $result .= $hookmanager->resPrint;
979  }
980  return $result;
981  }
982 
989  public function info($id)
990  {
991  $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
992  $sql .= ' d.tms as datem';
993  $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
994  $sql .= ' WHERE d.rowid = '.((int) $id);
995 
996  dol_syslog(get_class($this).'::info', LOG_DEBUG);
997  $result = $this->db->query($sql);
998 
999  if ($result) {
1000  if ($this->db->num_rows($result)) {
1001  $obj = $this->db->fetch_object($result);
1002  $this->id = $obj->rowid;
1003 
1004  $this->user_creation_id = $obj->fk_user_author;
1005  $this->user_validation_id = $obj->fk_user_valid;
1006  $this->date_creation = $this->db->jdate($obj->datec);
1007  $this->date_modification = (!empty($obj->tms) ? $this->db->jdate($obj->tms) : "");
1008  }
1009  $this->db->free($result);
1010  } else {
1011  dol_print_error($this->db);
1012  }
1013  }
1014 
1015 
1026  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1027  {
1028  global $conf, $langs;
1029 
1030  $langs->load("bills");
1031 
1032  if (!dol_strlen($modele)) {
1033  $modele = 'html_cerfafr';
1034 
1035  if ($this->model_pdf) {
1036  $modele = $this->model_pdf;
1037  } elseif (!empty($conf->global->DON_ADDON_MODEL)) {
1038  $modele = $conf->global->DON_ADDON_MODEL;
1039  }
1040  }
1041 
1042  $modelpath = "core/modules/dons/";
1043 
1044  // TODO Restore use of commonGenerateDocument instead of dedicated code here
1045  //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1046 
1047  // Increase limit for PDF build
1048  $err = error_reporting();
1049  error_reporting(0);
1050  @set_time_limit(120);
1051  error_reporting($err);
1052 
1053  $srctemplatepath = '';
1054 
1055  // If selected modele is a filename template (then $modele="modelname:filename")
1056  $tmp = explode(':', $modele, 2);
1057  if (!empty($tmp[1])) {
1058  $modele = $tmp[0];
1059  $srctemplatepath = $tmp[1];
1060  }
1061 
1062  // Search template files
1063  $file = ''; $classname = ''; $filefound = 0;
1064  $dirmodels = array('/');
1065  if (is_array($conf->modules_parts['models'])) {
1066  $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1067  }
1068  foreach ($dirmodels as $reldir) {
1069  foreach (array('html', 'doc', 'pdf') as $prefix) {
1070  $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1071 
1072  // On verifie l'emplacement du modele
1073  $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1074  if (file_exists($file)) {
1075  $filefound = 1;
1076  $classname = $prefix.'_'.$modele;
1077  break;
1078  }
1079  }
1080  if ($filefound) {
1081  break;
1082  }
1083  }
1084 
1085  // Charge le modele
1086  if ($filefound) {
1087  require_once $file;
1088 
1089  $object = $this;
1090 
1091  $classname = $modele;
1092  $obj = new $classname($this->db);
1093 
1094  // We save charset_output to restore it because write_file can change it if needed for
1095  // output format that does not support UTF8.
1096  $sav_charset_output = $outputlangs->charset_output;
1097  if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) {
1098  $outputlangs->charset_output = $sav_charset_output;
1099 
1100  // we delete preview files
1101  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1102  dol_delete_preview($object);
1103  return 1;
1104  } else {
1105  $outputlangs->charset_output = $sav_charset_output;
1106  dol_syslog("Erreur dans don_create");
1107  dol_print_error($this->db, $obj->error);
1108  return 0;
1109  }
1110  } else {
1111  print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1112  return 0;
1113  }
1114  }
1115 
1124  public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1125  {
1126  $tables = array(
1127  'don'
1128  );
1129 
1130  return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1131  }
1132 
1138  public function getRemainToPay()
1139  {
1140  dol_syslog(__METHOD__, LOG_DEBUG);
1141 
1142  if (empty($this->id)) {
1143  $this->error = 'Missing object id';
1144  $this->errors[] = $this->error;
1145  dol_syslog(__METHOD__.' : '.$this->error, LOG_ERR);
1146  return -1;
1147  }
1148 
1149  $sql = "SELECT SUM(amount) as sum_amount FROM ".MAIN_DB_PREFIX."payment_donation WHERE fk_donation = ".((int) $this->id);
1150  $resql = $this->db->query($sql);
1151  if (!$resql) {
1152  dol_print_error($this->db);
1153  return -2;
1154  } else {
1155  $sum_amount = (float) $this->db->fetch_object($resql)->sum_amount;
1156  return (float) $this->amount - $sum_amount;
1157  }
1158  }
1159 
1167  public function getKanbanView($option = '', $arraydata = null)
1168  {
1169  global $langs;
1170 
1171  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
1172 
1173  $return = '<div class="box-flex-item box-flex-grow-zero">';
1174  $return .= '<div class="info-box info-box-sm">';
1175  $return .= '<span class="info-box-icon bg-infobox-action">';
1176  $return .= img_picto('', $this->picto);
1177  $return .= '</span>';
1178  $return .= '<div class="info-box-content">';
1179  $return .= '<span class="info-box-ref">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref).'</span>';
1180  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
1181  if (property_exists($this, 'date')) {
1182  $return .= ' | <span class="opacitymedium" >'.$langs->trans("Date").'</span> : <span class="info-box-label">'.dol_print_date($this->date).'</span>';
1183  }
1184  if (property_exists($this, 'societe') && !empty($this->societe)) {
1185  $return .= '<br><span class="opacitymedium">'.$langs->trans("Company").'</span> : <span class="info-box-label">'.$this->societe.'</span>';
1186  }
1187  if (property_exists($this, 'amount')) {
1188  $return .= '<br><span class="opacitymedium" >'.$langs->trans("Amount").'</span> : <span class="info-box-label amount">'.price($this->amount).'</span>';
1189  }
1190  if (method_exists($this, 'LibStatut')) {
1191  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
1192  }
1193  $return .= '</div>';
1194  $return .= '</div>';
1195  $return .= '</div>';
1196  return $return;
1197  }
1198 }
Don\setPaid
setPaid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:787
Don\create
create($user, $notrigger=0)
Create donation record into database.
Definition: don.class.php:356
CommonObject\setStatusCommon
setStatusCommon($user, $status, $notrigger=0, $triggercode='')
Set to a status.
Definition: commonobject.class.php:10054
Don\LibStatut
LibStatut($status, $mode=0)
Return the label of a given status.
Definition: don.class.php:186
dol_escape_htmltag
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=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:1554
Don\getKanbanView
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
Definition: don.class.php:1167
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:711
$sql
if(isModEnabled('facture') &&!empty($user->rights->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') &&!empty($user->rights->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:745
Don\info
info($id)
Information on record.
Definition: don.class.php:989
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:5052
Don\getRemainToPay
getRemainToPay()
Function to get reamain to pay for a donation.
Definition: don.class.php:1138
dol_buildpath
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
Definition: functions.lib.php:1117
Don\replaceThirdparty
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: don.class.php:1124
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:45
Don\check
check($minimum=0)
Check params and init ->errors array.
Definition: don.class.php:279
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5897
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:2622
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:4082
Don\fetch
fetch($id, $ref='')
Load donation from database.
Definition: don.class.php:627
Don\sum_donations
sum_donations($param)
Sum of donations.
Definition: don.class.php:867
Don\reopen
reopen($user, $notrigger=0)
Set cancel status.
Definition: don.class.php:843
CommonObject\insertExtraFields
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
Definition: commonobject.class.php:6348
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1689
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:817
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:6197
dol_strlen
dol_strlen($string, $stringencoding='UTF-8')
Make a strlen call.
Definition: functions.lib.php:3944
Don\update
update($user, $notrigger=0)
Update a donation record.
Definition: don.class.php:476
ref
$object ref
Definition: info.php:78
getDolGlobalString
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:102
Don\set_paid
set_paid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:773
dol_delete_preview
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1535
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10863
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:4419
Don\getNomUrl
getNomUrl($withpicto=0, $notooltip=0, $moretitle='', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: don.class.php:929
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:3003
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:5771
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5981
Don\getLibStatut
getLibStatut($mode=0)
Returns the donation status label (draft, valid, abandoned, paid)
Definition: don.class.php:173
Don\__construct
__construct($db)
Constructor.
Definition: don.class.php:161
Don\initAsSpecimen
initAsSpecimen()
Initialise an instance with random values.
Definition: don.class.php:221
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:8705
Don\load_state_board
load_state_board()
Charge indicateurs this->nb pour le tableau de bord.
Definition: don.class.php:894
Don\generateDocument
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: don.class.php:1026
float
div float
Buy price without taxes.
Definition: style.css.php:921