dolibarr  13.0.2
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  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
31 require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
32 
33 
37 class Don extends CommonObject
38 {
42  public $element = 'don';
43 
47  public $table_element = 'don';
48 
52  public $fk_element = 'fk_donation';
53 
58  public $ismultientitymanaged = 1;
59 
63  public $picto = 'donation';
64 
68  public $date;
69 
74  public $amount;
75 
79  public $societe;
80 
84  public $address;
85 
89  public $zip;
90 
94  public $town;
95 
99  public $email;
100 
104  public $public;
105 
109  public $fk_project;
110 
114  public $fk_typepayment;
115 
116  public $num_payment;
117  public $date_valid;
118 
122  public $modepaymentid = 0;
123 
127  public $labelStatus;
128 
132  public $labelStatusShort;
133 
134 
135  const STATUS_DRAFT = 0;
136  const STATUS_VALIDATED = 1;
137  const STATUS_PAID = 2;
138  const STATUS_CANCELED = -1;
139 
140 
146  public function __construct($db)
147  {
148  $this->db = $db;
149  }
150 
151 
158  public function getLibStatut($mode = 0)
159  {
160  return $this->LibStatut($this->statut, $mode);
161  }
162 
163  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
171  public function LibStatut($status, $mode = 0)
172  {
173  // phpcs:enable
174  if (empty($this->labelStatus) || empty($this->labelStatusShort))
175  {
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) $statusType = 'status9';
190  if ($status == self::STATUS_PAID) $statusType = 'status6';
191 
192  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
193  }
194 
195 
203  public function initAsSpecimen()
204  {
205  global $conf, $user, $langs;
206 
207  $now = dol_now();
208 
209  // Charge tableau des id de societe socids
210  $socids = array();
211 
212  $sql = "SELECT rowid";
213  $sql .= " FROM ".MAIN_DB_PREFIX."societe";
214  $sql .= " WHERE client IN (1, 3)";
215  $sql .= " AND entity = ".$conf->entity;
216  $sql .= " LIMIT 10";
217 
218  $resql = $this->db->query($sql);
219  if ($resql)
220  {
221  $num_socs = $this->db->num_rows($resql);
222  $i = 0;
223  while ($i < $num_socs)
224  {
225  $i++;
226 
227  $row = $this->db->fetch_row($resql);
228  $socids[$i] = $row[0];
229  }
230  }
231 
232  // Initialise parametres
233  $this->id = 0;
234  $this->ref = 'SPECIMEN';
235  $this->specimen = 1;
236  $this->lastname = 'Doe';
237  $this->firstname = 'John';
238  $this->socid = 1;
239  $this->date = $now;
240  $this->date_valid = $now;
241  $this->amount = 100.90;
242  $this->public = 1;
243  $this->societe = 'The Company';
244  $this->address = 'Twist road';
245  $this->zip = '99999';
246  $this->town = 'Town';
247  $this->note_private = 'Private note';
248  $this->note_public = 'Public note';
249  $this->email = 'email@email.com';
250  $this->phone = '0123456789';
251  $this->phone_mobile = '0606060606';
252  $this->statut = 1;
253  }
254 
255 
263  public function check($minimum = 0)
264  {
265  global $langs;
266  $langs->load('main');
267  $langs->load('companies');
268 
269  $error_string = array();
270  $err = 0;
271 
272  if (dol_strlen(trim($this->societe)) == 0)
273  {
274  if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0)
275  {
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  {
283  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Address'));
284  $err++;
285  }
286 
287  if (dol_strlen(trim($this->zip)) == 0)
288  {
289  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Zip'));
290  $err++;
291  }
292 
293  if (dol_strlen(trim($this->town)) == 0)
294  {
295  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Town'));
296  $err++;
297  }
298 
299  if (dol_strlen(trim($this->email)) == 0)
300  {
301  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('EMail'));
302  $err++;
303  }
304 
305  $this->amount = trim($this->amount);
306 
307  $map = range(0, 9);
308  $len = dol_strlen($this->amount);
309  for ($i = 0; $i < $len; $i++)
310  {
311  if (!isset($map[substr($this->amount, $i, 1)]))
312  {
313  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
314  $err++;
315  $amount_invalid = 1;
316  break;
317  }
318  }
319 
320  if (!$amount_invalid)
321  {
322  if ($this->amount == 0)
323  {
324  $error_string[] = $langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv('Amount'));
325  $err++;
326  } else {
327  if ($this->amount < $minimum && $minimum > 0)
328  {
329  $error_string[] = $langs->trans('MinimumAmount', $langs->transnoentitiesnoconv('$minimum'));
330  $err++;
331  }
332  }
333  }
334 
335  if ($err)
336  {
337  $this->errors = $error_string;
338  return 0;
339  } else {
340  return 1;
341  }
342  }
343 
352  public function create($user, $notrigger = 0)
353  {
354  global $conf, $langs;
355 
356  $error = 0;
357  $ret = 0;
358  $now = dol_now();
359 
360  // Clean parameters
361  $this->address = ($this->address > 0 ? $this->address : $this->address);
362  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
363  $this->town = ($this->town > 0 ? $this->town : $this->town);
364  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
365  $this->country = ($this->country ? $this->country : $this->country);
366 
367  $this->db->begin();
368 
369  $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
370  $sql .= "datec";
371  $sql .= ", entity";
372  $sql .= ", amount";
373  $sql .= ", fk_payment";
374  $sql .= ", fk_soc";
375  $sql .= ", firstname";
376  $sql .= ", lastname";
377  $sql .= ", societe";
378  $sql .= ", address";
379  $sql .= ", zip";
380  $sql .= ", town";
381  $sql .= ", fk_country";
382  $sql .= ", public";
383  $sql .= ", fk_projet";
384  $sql .= ", note_private";
385  $sql .= ", note_public";
386  $sql .= ", fk_user_author";
387  $sql .= ", fk_user_valid";
388  $sql .= ", datedon";
389  $sql .= ", email";
390  $sql .= ", phone";
391  $sql .= ", phone_mobile";
392  $sql .= ") VALUES (";
393  $sql .= "'".$this->db->idate($now)."'";
394  $sql .= ", ".$conf->entity;
395  $sql .= ", ".price2num($this->amount);
396  $sql .= ", ".($this->modepaymentid ? $this->modepaymentid : "null");
397  $sql .= ", ".($this->socid > 0 ? $this->socid : "null");
398  $sql .= ", '".$this->db->escape($this->firstname)."'";
399  $sql .= ", '".$this->db->escape($this->lastname)."'";
400  $sql .= ", '".$this->db->escape($this->societe)."'";
401  $sql .= ", '".$this->db->escape($this->address)."'";
402  $sql .= ", '".$this->db->escape($this->zip)."'";
403  $sql .= ", '".$this->db->escape($this->town)."'";
404  $sql .= ", ".(int) ($this->country_id > 0 ? $this->country_id : 0);
405  $sql .= ", ".(int) $this->public;
406  $sql .= ", ".($this->fk_project > 0 ? (int) $this->fk_project : "null");
407  $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
408  $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
409  $sql .= ", ".$user->id;
410  $sql .= ", null";
411  $sql .= ", '".$this->db->idate($this->date)."'";
412  $sql .= ", '".$this->db->escape(trim($this->email))."'";
413  $sql .= ", '".$this->db->escape(trim($this->phone))."'";
414  $sql .= ", '".$this->db->escape(trim($this->phone_mobile))."'";
415  $sql .= ")";
416 
417  $resql = $this->db->query($sql);
418  if ($resql)
419  {
420  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
421  $ret = $this->id;
422 
423  if (!$notrigger)
424  {
425  // Call trigger
426  $result = $this->call_trigger('DON_CREATE', $user);
427  if ($result < 0) { $error++; }
428  // End call triggers
429  }
430  } else {
431  $this->error = $this->db->lasterror();
432  $this->errno = $this->db->lasterrno();
433  $error++;
434  }
435 
436  // Update extrafield
437  if (!$error) {
438  $result = $this->insertExtraFields();
439  if ($result < 0) {
440  $error++;
441  }
442  }
443 
444  if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
445  {
446  //$res = $this->setValid($user);
447  //if ($res < 0) $error++;
448  }
449 
450  if (!$error)
451  {
452  $this->db->commit();
453  return $ret;
454  } else {
455  $this->db->rollback();
456  return -1;
457  }
458  }
459 
467  public function update($user, $notrigger = 0)
468  {
469  global $langs, $conf;
470 
471  $error = 0;
472 
473  // Clean parameters
474  $this->address = ($this->address > 0 ? $this->address : $this->address);
475  $this->zip = ($this->zip > 0 ? $this->zip : $this->zip);
476  $this->town = ($this->town > 0 ? $this->town : $this->town);
477  $this->country_id = ($this->country_id > 0 ? $this->country_id : $this->country_id);
478  $this->country = ($this->country ? $this->country : $this->country);
479 
480  $this->db->begin();
481 
482  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET ";
483  $sql .= "amount = ".price2num($this->amount);
484  $sql .= ",fk_payment = ".($this->modepaymentid ? $this->modepaymentid : "null");
485  $sql .= ",firstname = '".$this->db->escape($this->firstname)."'";
486  $sql .= ",lastname='".$this->db->escape($this->lastname)."'";
487  $sql .= ",societe='".$this->db->escape($this->societe)."'";
488  $sql .= ",address='".$this->db->escape($this->address)."'";
489  $sql .= ",zip='".$this->db->escape($this->zip)."'";
490  $sql .= ",town='".$this->db->escape($this->town)."'";
491  $sql .= ",fk_country = ".($this->country_id > 0 ? $this->country_id : '0');
492  $sql .= ",public=".$this->public;
493  $sql .= ",fk_projet=".($this->fk_project > 0 ? $this->fk_project : 'null');
494  $sql .= ",note_private=".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
495  $sql .= ",note_public=".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
496  $sql .= ",datedon='".$this->db->idate($this->date)."'";
497  $sql .= ",date_valid=".($this->date_valid ? "'".$this->db->idate($this->date)."'" : "null");
498  $sql .= ",email='".$this->db->escape(trim($this->email))."'";
499  $sql .= ",phone='".$this->db->escape(trim($this->phone))."'";
500  $sql .= ",phone_mobile='".$this->db->escape(trim($this->phone_mobile))."'";
501  $sql .= ",fk_statut=".$this->statut;
502  $sql .= " WHERE rowid = ".$this->id;
503 
504  dol_syslog(get_class($this)."::Update", LOG_DEBUG);
505  $resql = $this->db->query($sql);
506  if ($resql)
507  {
508  if (!$notrigger)
509  {
510  // Call trigger
511  $result = $this->call_trigger('DON_MODIFY', $user);
512  if ($result < 0) { $error++; }
513  // End call triggers
514  }
515 
516  // Update extrafield
517  if (!$error)
518  {
519  $result = $this->insertExtraFields();
520  if ($result < 0)
521  {
522  $error++;
523  }
524  }
525 
526  if (!$error)
527  {
528  $this->db->commit();
529  $result = 1;
530  } else {
531  $this->db->rollback();
532  $result = -1;
533  }
534  } else {
535  $this->error = $this->db->lasterror();
536  $this->errors[] = $this->error;
537  $this->db->rollback();
538  dol_syslog(get_class($this)."::Update error -2 ".$this->error, LOG_ERR);
539  $result = -2;
540  }
541  return $result;
542  }
543 
551  public function delete($user, $notrigger = 0)
552  {
553  global $user, $conf, $langs;
554  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
555 
556  $error = 0;
557 
558  $this->db->begin();
559 
560  if (!$error && !$notrigger) {
561  // Call trigger
562  $result = $this->call_trigger('DON_DELETE', $user);
563 
564  if ($result < 0) {
565  $error++;
566  }
567  // End call triggers
568  }
569 
570  // Delete donation
571  if (!$error)
572  {
573  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don_extrafields";
574  $sql .= " WHERE fk_object=".$this->id;
575 
576  $resql = $this->db->query($sql);
577  if (!$resql)
578  {
579  $this->errors[] = $this->db->lasterror();
580  $error++;
581  }
582  }
583 
584  if (!$error)
585  {
586  $sql = "DELETE FROM ".MAIN_DB_PREFIX."don";
587  $sql .= " WHERE rowid=".$this->id;
588 
589  $resql = $this->db->query($sql);
590  if (!$resql)
591  {
592  $this->errors[] = $this->db->lasterror();
593  $error++;
594  }
595  }
596 
597  if (!$error)
598  {
599  $this->db->commit();
600  return 1;
601  } else {
602  foreach ($this->errors as $errmsg)
603  {
604  dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
605  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
606  }
607  dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
608  $this->db->rollback();
609  return -1;
610  }
611  }
612 
620  public function fetch($id, $ref = '')
621  {
622  global $conf;
623 
624  $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,";
625  $sql .= " d.fk_soc as socid,d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
626  $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, ";
627  $sql .= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,";
628  $sql .= " p.ref as project_ref,";
629  $sql .= " cp.libelle as payment_label, cp.code as payment_code,";
630  $sql .= " c.code as country_code, c.label as country";
631  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
632  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
633  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
634  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
635  $sql .= " WHERE d.entity IN (".getEntity('donation').")";
636  if (!empty($id))
637  {
638  $sql .= " AND d.rowid=".$id;
639  } elseif (!empty($ref))
640  {
641  $sql .= " AND d.ref='".$this->db->escape($ref)."'";
642  }
643 
644  dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
645  $resql = $this->db->query($sql);
646  if ($resql)
647  {
648  if ($this->db->num_rows($resql))
649  {
650  $obj = $this->db->fetch_object($resql);
651 
652  $this->id = $obj->rowid;
653  $this->ref = $obj->rowid;
654  $this->date_creation = $this->db->jdate($obj->datec);
655  $this->datec = $this->db->jdate($obj->datec);
656  $this->date_validation = $this->db->jdate($obj->date_valid);
657  $this->date_valid = $this->db->jdate($obj->date_valid);
658  $this->date_modification = $this->db->jdate($obj->datem);
659  $this->datem = $this->db->jdate($obj->datem);
660  $this->date = $this->db->jdate($obj->datedon);
661  $this->socid = $obj->socid;
662  $this->firstname = $obj->firstname;
663  $this->lastname = $obj->lastname;
664  $this->societe = $obj->societe;
665  $this->statut = $obj->fk_statut;
666  $this->address = $obj->address;
667  $this->zip = $obj->zip;
668  $this->town = $obj->town;
669  $this->country_id = $obj->fk_country;
670  $this->country_code = $obj->country_code;
671  $this->country = $obj->country;
672  $this->country_olddata = $obj->country_olddata; // deprecated
673  $this->email = $obj->email;
674  $this->phone = $obj->phone;
675  $this->phone_mobile = $obj->phone_mobile;
676  $this->project = $obj->project_ref;
677  $this->fk_projet = $obj->fk_project; // deprecated
678  $this->fk_project = $obj->fk_project;
679  $this->public = $obj->public;
680  $this->mode_reglement_id = $obj->fk_payment;
681  $this->mode_reglement_code = $obj->payment_code;
682  $this->mode_reglement = $obj->payment_label;
683  $this->paid = $obj->paid;
684  $this->amount = $obj->amount;
685  $this->note_private = $obj->note_private;
686  $this->note_public = $obj->note_public;
687  $this->model_pdf = $obj->model_pdf;
688  $this->modelpdf = $obj->model_pdf; // deprecated
689 
690  // Retrieve all extrafield
691  // fetch optionals attributes and labels
692  $this->fetch_optionals();
693  }
694  return 1;
695  } else {
696  dol_print_error($this->db);
697  return -1;
698  }
699  }
700 
708  public function setValid($user, $notrigger = 0)
709  {
710  return $this->valid_promesse($this->id, $user->id, $notrigger);
711  }
712 
713  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
722  public function valid_promesse($id, $userid, $notrigger = 0)
723  {
724  // phpcs:enable
725  global $langs, $user;
726 
727  $error = 0;
728 
729  $this->db->begin();
730 
731  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".$userid." WHERE rowid = ".$id." AND fk_statut = 0";
732 
733  $resql = $this->db->query($sql);
734  if ($resql)
735  {
736  if ($this->db->affected_rows($resql))
737  {
738  if (!$notrigger) {
739  // Call trigger
740  $result = $this->call_trigger('DON_VALIDATE', $user);
741  if ($result < 0) { $error++; }
742  // End call triggers
743  }
744  }
745  } else {
746  $error++;
747  $this->error = $this->db->lasterror();
748  }
749 
750  if (!$error) {
751  $this->statut = 1;
752  $this->db->commit();
753  return 1;
754  } else {
755  $this->db->rollback();
756  return -1;
757  }
758  }
759 
760  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
768  public function set_paid($id, $modepayment = 0)
769  {
770  // phpcs:enable
771  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2";
772  if ($modepayment)
773  {
774  $sql .= ", fk_payment=".$modepayment;
775  }
776  $sql .= " WHERE rowid = ".$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  return 1;
783  } else {
784  return 0;
785  }
786  } else {
787  dol_print_error($this->db);
788  return -1;
789  }
790  }
791 
792  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
799  public function set_cancel($id)
800  {
801  // phpcs:enable
802  $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".$id;
803 
804  $resql = $this->db->query($sql);
805  if ($resql) {
806  if ($this->db->affected_rows($resql)) {
807  $this->statut = -1;
808  return 1;
809  } else {
810  return 0;
811  }
812  } else {
813  dol_print_error($this->db);
814  return -1;
815  }
816  }
817 
825  public function reopen($user, $notrigger = 0)
826  {
827  // Protection
828  if ($this->statut != self::STATUS_CANCELED) {
829  return 0;
830  }
831 
832  /*if (! ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->write))
833  || (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->bom->bom_advance->validate))))
834  {
835  $this->error='Permission denied';
836  return -1;
837  }*/
838 
839  return $this->setStatusCommon($user, self::STATUS_VALIDATED, $notrigger, 'DON_REOPEN');
840  }
841 
842  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
849  public function sum_donations($param)
850  {
851  // phpcs:enable
852  global $conf;
853 
854  $result = 0;
855 
856  $sql = "SELECT sum(amount) as total";
857  $sql .= " FROM ".MAIN_DB_PREFIX."don";
858  $sql .= " WHERE fk_statut = ".$param;
859  $sql .= " AND entity = ".$conf->entity;
860 
861  $resql = $this->db->query($sql);
862  if ($resql)
863  {
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  global $conf;
881 
882  $this->nb = array();
883 
884  $sql = "SELECT count(d.rowid) as nb";
885  $sql .= " FROM ".MAIN_DB_PREFIX."don as d";
886  $sql .= " WHERE d.fk_statut > 0";
887  $sql .= " AND d.entity IN (".getEntity('donation').")";
888 
889  $resql = $this->db->query($sql);
890  if ($resql)
891  {
892  while ($obj = $this->db->fetch_object($resql))
893  {
894  $this->nb["donations"] = $obj->nb;
895  }
896  $this->db->free($resql);
897  return 1;
898  } else {
899  dol_print_error($this->db);
900  $this->error = $this->db->error();
901  return -1;
902  }
903  }
904 
914  public function getNomUrl($withpicto = 0, $notooltip = 0, $moretitle = '', $save_lastsearch_value = -1)
915  {
916  global $conf, $langs;
917 
918  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
919 
920  $result = '';
921  $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("Donation").'</u>';
922  if (isset($this->status)) {
923  $label .= ' '.$this->getLibStatut(5);
924  }
925  if (!empty($this->id)) {
926  $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->id;
927  $label .= '<br><b>'.$langs->trans('Date').':</b> '.dol_print_date($this->date, 'day');
928  }
929  if ($moretitle) $label .= ' - '.$moretitle;
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"])) $add_save_lastsearch_values = 1;
935  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
936 
937  $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
938  $linkend = '</a>';
939 
940  $result .= $linkstart;
941  if ($withpicto) $result .= img_object(($notooltip ? '' : $label), $this->picto, ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
942  if ($withpicto != 2) $result .= $this->ref;
943  $result .= $linkend;
944 
945  return $result;
946  }
947 
954  public function info($id)
955  {
956  $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
957  $sql .= ' d.tms';
958  $sql .= ' FROM '.MAIN_DB_PREFIX.'don as d';
959  $sql .= ' WHERE d.rowid = '.$id;
960 
961  dol_syslog(get_class($this).'::info', LOG_DEBUG);
962  $result = $this->db->query($sql);
963 
964  if ($result)
965  {
966  if ($this->db->num_rows($result))
967  {
968  $obj = $this->db->fetch_object($result);
969  $this->id = $obj->rowid;
970  if ($obj->fk_user_author)
971  {
972  $cuser = new User($this->db);
973  $cuser->fetch($obj->fk_user_author);
974  $this->user_creation = $cuser;
975  }
976  if ($obj->fk_user_valid)
977  {
978  $vuser = new User($this->db);
979  $vuser->fetch($obj->fk_user_valid);
980  $this->user_modification = $vuser;
981  }
982  $this->date_creation = $this->db->jdate($obj->datec);
983  $this->date_modification = $this->db->jdate($obj->tms);
984  }
985  $this->db->free($result);
986  } else {
987  dol_print_error($this->db);
988  }
989  }
990 
991 
1002  public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0)
1003  {
1004  global $conf, $langs;
1005 
1006  $langs->load("bills");
1007 
1008  if (!dol_strlen($modele)) {
1009  $modele = 'html_cerfafr';
1010 
1011  if ($this->model_pdf) {
1012  $modele = $this->model_pdf;
1013  } elseif (!empty($conf->global->DON_ADDON_MODEL)) {
1014  $modele = $conf->global->DON_ADDON_MODEL;
1015  }
1016  }
1017 
1018  $modelpath = "core/modules/dons/";
1019 
1020  // TODO Restore use of commonGenerateDocument instead of dedicated code here
1021  //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref);
1022 
1023  // Increase limit for PDF build
1024  $err = error_reporting();
1025  error_reporting(0);
1026  @set_time_limit(120);
1027  error_reporting($err);
1028 
1029  $srctemplatepath = '';
1030 
1031  // If selected modele is a filename template (then $modele="modelname:filename")
1032  $tmp = explode(':', $modele, 2);
1033  if (!empty($tmp[1]))
1034  {
1035  $modele = $tmp[0];
1036  $srctemplatepath = $tmp[1];
1037  }
1038 
1039  // Search template files
1040  $file = ''; $classname = ''; $filefound = 0;
1041  $dirmodels = array('/');
1042  if (is_array($conf->modules_parts['models'])) $dirmodels = array_merge($dirmodels, $conf->modules_parts['models']);
1043  foreach ($dirmodels as $reldir)
1044  {
1045  foreach (array('html', 'doc', 'pdf') as $prefix)
1046  {
1047  $file = $prefix."_".preg_replace('/^html_/', '', $modele).".modules.php";
1048 
1049  // On verifie l'emplacement du modele
1050  $file = dol_buildpath($reldir."core/modules/dons/".$file, 0);
1051  if (file_exists($file))
1052  {
1053  $filefound = 1;
1054  $classname = $prefix.'_'.$modele;
1055  break;
1056  }
1057  }
1058  if ($filefound) break;
1059  }
1060 
1061  // Charge le modele
1062  if ($filefound)
1063  {
1064  require_once $file;
1065 
1066  $object = $this;
1067 
1068  $classname = $modele;
1069  $obj = new $classname($this->db);
1070 
1071  // We save charset_output to restore it because write_file can change it if needed for
1072  // output format that does not support UTF8.
1073  $sav_charset_output = $outputlangs->charset_output;
1074  if ($obj->write_file($object, $outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0)
1075  {
1076  $outputlangs->charset_output = $sav_charset_output;
1077 
1078  // we delete preview files
1079  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1080  dol_delete_preview($object);
1081  return 1;
1082  } else {
1083  $outputlangs->charset_output = $sav_charset_output;
1084  dol_syslog("Erreur dans don_create");
1085  dol_print_error($this->db, $obj->error);
1086  return 0;
1087  }
1088  } else {
1089  print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists", $file);
1090  return 0;
1091  }
1092  }
1093 
1102  public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
1103  {
1104  $tables = array(
1105  'don'
1106  );
1107 
1108  return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
1109  }
1110 }
update($user, $notrigger=0)
Update a donation record.
Definition: don.class.php:467
valid_promesse($id, $userid, $notrigger=0)
Validate a promise of donation.
Definition: don.class.php:722
if(!empty($arrayfields['u.datec']['checked'])) print_liste_field_titre("DateCreationShort"u if(!empty($arrayfields['u.tms']['checked'])) print_liste_field_titre("DateModificationShort"u if(!empty($arrayfields['u.statut']['checked'])) print_liste_field_titre("Status"u statut
Definition: list.php:632
getNomUrl($withpicto=0, $notooltip=0, $moretitle= '', $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
Definition: don.class.php:914
set_cancel($id)
Set donation to status cancelled.
Definition: don.class.php:799
set_paid($id, $modepayment=0)
Classify the donation as paid, the donation was received.
Definition: don.class.php:768
dol_now($mode= 'auto')
Return date for now.
dol_delete_preview($object)
Delete all preview files linked to object instance.
Definition: files.lib.php:1335
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
load_state_board()
Charge indicateurs this-&gt;nb pour le tableau de bord.
Definition: don.class.php:877
fetch($id, $ref= '')
Load donation from database.
Definition: don.class.php:620
dol_buildpath($path, $type=0, $returnemptyifnotfound=0)
Return path of url or filesystem.
$conf db
API class for accounts.
Definition: inc.php:54
LibStatut($status, $mode=0)
Return the label of a given status.
Definition: don.class.php:171
insertExtraFields($trigger= '', $userused=null)
Add/Update all extra fields values for the current object.
getLibStatut($mode=0)
Returns the donation status label (draft, valid, abandoned, paid)
Definition: don.class.php:158
static replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
Definition: don.class.php:1102
info($id)
Information on record.
Definition: don.class.php:954
dol_strlen($string, $stringencoding= 'UTF-8')
Make a strlen call.
generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0)
Create a document onto disk according to template module.
Definition: don.class.php:1002
__construct($db)
Constructor.
Definition: don.class.php:146
initAsSpecimen()
Initialise an instance with random values.
Definition: don.class.php:203
static commonReplaceThirdparty(DoliDB $db, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
create($user, $notrigger=0)
Create donation record into database.
Definition: don.class.php:352
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt= '', $morecss= '', $marginleftonlyshort=2)
Show picto whatever it&#39;s its name (generic function)
setValid($user, $notrigger=0)
Validate a intervention.
Definition: don.class.php:708
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this-&gt;array_options This method is in most cases call...
print $_SERVER["PHP_SELF"]
Edit parameters.
print
Draft customers invoices.
Definition: index.php:89
Class to manage donations.
Definition: don.class.php:37
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
setStatusCommon($user, $status, $notrigger=0, $triggercode= '')
Set to a status.
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
reopen($user, $notrigger=0)
Set cancel status.
Definition: don.class.php:825
check($minimum=0)
Check params and init -&gt;errors array.
Definition: don.class.php:263
sum_donations($param)
Sum of donations.
Definition: don.class.php:849
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $keepmoretags= '', $escapeonlyhtmltags=0)
Returns text escaped for inclusion in HTML alt or title tags, or into values of HTML input fields...