1 | <?php |
||
2 | /* Copyright (C) 2002 Rodolphe Quiedeville <[email protected]> |
||
3 | * Copyright (C) 2004-2008 Laurent Destailleur <[email protected]> |
||
4 | * Copyright (C) 2009 Regis Houssin <[email protected]> |
||
5 | * Copyright (C) 2014 Florian Henry <[email protected]> |
||
6 | * Copyright (C) 2015-2017 Alexandre Spangaro <[email protected]> |
||
7 | * Copyright (C) 2016 Juanjo Menent <[email protected]> |
||
8 | * |
||
9 | * This program is free software; you can redistribute it and/or modify |
||
10 | * it under the terms of the GNU General Public License as published by |
||
11 | * the Free Software Foundation; either version 3 of the License, or |
||
12 | * (at your option) any later version. |
||
13 | * |
||
14 | * This program is distributed in the hope that it will be useful, |
||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
17 | * GNU General Public License for more details. |
||
18 | * |
||
19 | * You should have received a copy of the GNU General Public License |
||
20 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
21 | */ |
||
22 | |||
23 | /** |
||
24 | * \file htdocs/don/class/don.class.php |
||
25 | * \ingroup Donation |
||
26 | * \brief File of class to manage donations |
||
27 | */ |
||
28 | |||
29 | require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php'; |
||
30 | |||
31 | |||
32 | /** |
||
33 | * Class to manage donations |
||
34 | */ |
||
35 | class Don extends CommonObject |
||
36 | { |
||
37 | /** |
||
38 | * @var string ID to identify managed object |
||
39 | */ |
||
40 | public $element='don'; |
||
41 | |||
42 | /** |
||
43 | * @var string Name of table without prefix where object is stored |
||
44 | */ |
||
45 | public $table_element='don'; |
||
46 | |||
47 | /** |
||
48 | * @var int Field with ID of parent key if this field has a parent |
||
49 | */ |
||
50 | public $fk_element = 'fk_donation'; |
||
51 | |||
52 | /** |
||
53 | * 0=No test on entity, 1=Test with field entity, 2=Test with link by societe |
||
54 | * @var int |
||
55 | */ |
||
56 | public $ismultientitymanaged = 1; |
||
57 | |||
58 | /** |
||
59 | * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
||
60 | */ |
||
61 | public $picto = 'generic'; |
||
62 | |||
63 | public $date; |
||
64 | public $amount; |
||
65 | public $societe; |
||
66 | |||
67 | /** |
||
68 | * @var string Address |
||
69 | */ |
||
70 | public $address; |
||
71 | |||
72 | public $zip; |
||
73 | public $town; |
||
74 | public $email; |
||
75 | public $public; |
||
76 | |||
77 | /** |
||
78 | * @var int ID |
||
79 | */ |
||
80 | public $fk_project; |
||
81 | |||
82 | /** |
||
83 | * @var int ID |
||
84 | */ |
||
85 | public $fk_typepayment; |
||
86 | |||
87 | public $num_payment; |
||
88 | public $date_valid; |
||
89 | public $modepaymentid = 0; |
||
90 | |||
91 | public $labelstatut; |
||
92 | public $labelstatutshort; |
||
93 | |||
94 | |||
95 | /** |
||
96 | * Constructor |
||
97 | * |
||
98 | * @param DoliDB $db Database handler |
||
99 | */ |
||
100 | function __construct($db) |
||
101 | { |
||
102 | $this->db = $db; |
||
103 | } |
||
104 | |||
105 | |||
106 | /** |
||
107 | * Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee) |
||
108 | * |
||
109 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long |
||
110 | * @return string Libelle |
||
111 | */ |
||
112 | function getLibStatut($mode=0) |
||
113 | { |
||
114 | return $this->LibStatut($this->statut,$mode); |
||
115 | } |
||
116 | |||
117 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
118 | /** |
||
119 | * Renvoi le libelle d'un statut donne |
||
120 | * |
||
121 | * @param int $statut Id statut |
||
122 | * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto |
||
123 | * @return string Libelle du statut |
||
124 | */ |
||
125 | function LibStatut($statut,$mode=0) |
||
126 | { |
||
127 | // phpcs:enable |
||
128 | if (empty($this->labelstatut) || empty($this->labelstatutshort)) |
||
129 | { |
||
130 | global $langs; |
||
131 | $langs->load("donations"); |
||
132 | $this->labelstatut[-1]=$langs->trans("Canceled"); |
||
133 | $this->labelstatut[0]=$langs->trans("DonationStatusPromiseNotValidated"); |
||
134 | $this->labelstatut[1]=$langs->trans("DonationStatusPromiseValidated"); |
||
135 | $this->labelstatut[2]=$langs->trans("DonationStatusPaid"); |
||
136 | $this->labelstatutshort[-1]=$langs->trans("Canceled"); |
||
137 | $this->labelstatutshort[0]=$langs->trans("DonationStatusPromiseNotValidatedShort"); |
||
138 | $this->labelstatutshort[1]=$langs->trans("DonationStatusPromiseValidatedShort"); |
||
139 | $this->labelstatutshort[2]=$langs->trans("DonationStatusPaidShort"); |
||
140 | } |
||
141 | |||
142 | if ($mode == 0) |
||
143 | { |
||
144 | return $this->labelstatut[$statut]; |
||
145 | } |
||
146 | elseif ($mode == 1) |
||
147 | { |
||
148 | return $this->labelstatutshort[$statut]; |
||
149 | } |
||
150 | elseif ($mode == 2) |
||
151 | { |
||
152 | if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5').' '.$this->labelstatutshort[$statut]; |
||
153 | elseif ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0').' '.$this->labelstatutshort[$statut]; |
||
154 | elseif ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1').' '.$this->labelstatutshort[$statut]; |
||
155 | elseif ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6').' '.$this->labelstatutshort[$statut]; |
||
156 | } |
||
157 | elseif ($mode == 3) |
||
158 | { |
||
159 | if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5'); |
||
160 | elseif ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0'); |
||
161 | elseif ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1'); |
||
162 | elseif ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6'); |
||
163 | } |
||
164 | elseif ($mode == 4) |
||
165 | { |
||
166 | if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5').' '.$this->labelstatut[$statut]; |
||
167 | elseif ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0').' '.$this->labelstatut[$statut]; |
||
168 | elseif ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1').' '.$this->labelstatut[$statut]; |
||
169 | elseif ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6').' '.$this->labelstatut[$statut]; |
||
170 | } |
||
171 | elseif ($mode == 5) |
||
172 | { |
||
173 | if ($statut == -1) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut5'); |
||
174 | elseif ($statut == 0) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut0'); |
||
175 | elseif ($statut == 1) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut1'); |
||
176 | elseif ($statut == 2) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut6'); |
||
177 | } |
||
178 | elseif ($mode == 6) |
||
179 | { |
||
180 | if ($statut == -1) return $this->labelstatut[$statut].' '.img_picto($this->labelstatut[$statut],'statut5'); |
||
181 | elseif ($statut == 0) return $this->labelstatut[$statut].' '.img_picto($this->labelstatut[$statut],'statut0'); |
||
182 | elseif ($statut == 1) return $this->labelstatut[$statut].' '.img_picto($this->labelstatut[$statut],'statut1'); |
||
183 | elseif ($statut == 2) return $this->labelstatut[$statut].' '.img_picto($this->labelstatut[$statut],'statut6'); |
||
184 | } |
||
185 | } |
||
186 | |||
187 | |||
188 | /** |
||
189 | * Initialise an instance with random values. |
||
190 | * Used to build previews or test instances. |
||
191 | * id must be 0 if object instance is a specimen. |
||
192 | * |
||
193 | * @return void |
||
194 | */ |
||
195 | function initAsSpecimen() |
||
196 | { |
||
197 | global $conf, $user,$langs; |
||
198 | |||
199 | $now = dol_now(); |
||
200 | |||
201 | // Charge tableau des id de societe socids |
||
202 | $socids = array(); |
||
203 | |||
204 | $sql = "SELECT rowid"; |
||
205 | $sql.= " FROM ".MAIN_DB_PREFIX."societe"; |
||
206 | $sql.= " WHERE client IN (1, 3)"; |
||
207 | $sql.= " AND entity = ".$conf->entity; |
||
208 | $sql.= " LIMIT 10"; |
||
209 | |||
210 | $resql = $this->db->query($sql); |
||
211 | if ($resql) |
||
212 | { |
||
213 | $num_socs = $this->db->num_rows($resql); |
||
214 | $i = 0; |
||
215 | while ($i < $num_socs) |
||
216 | { |
||
217 | $i++; |
||
218 | |||
219 | $row = $this->db->fetch_row($resql); |
||
220 | $socids[$i] = $row[0]; |
||
221 | } |
||
222 | } |
||
223 | |||
224 | // Initialise parametres |
||
225 | $this->id=0; |
||
226 | $this->ref = 'SPECIMEN'; |
||
227 | $this->specimen=1; |
||
228 | $this->lastname = 'Doe'; |
||
229 | $this->firstname = 'John'; |
||
230 | $this->socid = 1; |
||
231 | $this->date = $now; |
||
232 | $this->date_valid = $now; |
||
233 | $this->amount = 100; |
||
234 | $this->public = 1; |
||
235 | $this->societe = 'The Company'; |
||
236 | $this->address = 'Twist road'; |
||
237 | $this->zip = '99999'; |
||
238 | $this->town = 'Town'; |
||
239 | $this->note_private='Private note'; |
||
240 | $this->note_public='Public note'; |
||
241 | $this->email='[email protected]'; |
||
242 | $this->note=''; |
||
243 | $this->statut=1; |
||
244 | } |
||
245 | |||
246 | |||
247 | /** |
||
248 | * Check params and init ->errors array. |
||
249 | * TODO This function seems to not be used by core code. |
||
250 | * |
||
251 | * @param int $minimum Minimum |
||
252 | * @return int 0 if KO, >0 if OK |
||
253 | */ |
||
254 | function check($minimum=0) |
||
255 | { |
||
256 | global $langs; |
||
257 | $langs->load('main'); |
||
258 | $langs->load('companies'); |
||
259 | |||
260 | $error_string = array(); |
||
261 | $err = 0; |
||
262 | |||
263 | if (dol_strlen(trim($this->societe)) == 0) |
||
264 | { |
||
265 | if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0) |
||
266 | { |
||
267 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Company').'/'.$langs->trans('Firstname').'-'.$langs->trans('Lastname')); |
||
268 | $err++; |
||
269 | } |
||
270 | } |
||
271 | |||
272 | if (dol_strlen(trim($this->address)) == 0) |
||
273 | { |
||
274 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Address')); |
||
275 | $err++; |
||
276 | } |
||
277 | |||
278 | if (dol_strlen(trim($this->zip)) == 0) |
||
279 | { |
||
280 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Zip')); |
||
281 | $err++; |
||
282 | } |
||
283 | |||
284 | if (dol_strlen(trim($this->town)) == 0) |
||
285 | { |
||
286 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Town')); |
||
287 | $err++; |
||
288 | } |
||
289 | |||
290 | if (dol_strlen(trim($this->email)) == 0) |
||
291 | { |
||
292 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('EMail')); |
||
293 | $err++; |
||
294 | } |
||
295 | |||
296 | $this->amount = trim($this->amount); |
||
297 | |||
298 | $map = range(0,9); |
||
299 | $len=dol_strlen($this->amount); |
||
300 | for ($i = 0; $i < $len; $i++) |
||
301 | { |
||
302 | if (!isset($map[substr($this->amount, $i, 1)] )) |
||
303 | { |
||
304 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Amount')); |
||
305 | $err++; |
||
306 | $amount_invalid = 1; |
||
307 | break; |
||
308 | } |
||
309 | } |
||
310 | |||
311 | if (! $amount_invalid) |
||
312 | { |
||
313 | if ($this->amount == 0) |
||
314 | { |
||
315 | $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Amount')); |
||
316 | $err++; |
||
317 | } |
||
318 | else |
||
319 | { |
||
320 | if ($this->amount < $minimum && $minimum > 0) |
||
321 | { |
||
322 | $error_string[] = $langs->trans('MinimumAmount',$langs->trans('$minimum')); |
||
323 | $err++; |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | |||
328 | if ($err) |
||
329 | { |
||
330 | $this->errors = $error_string; |
||
331 | return 0; |
||
332 | } |
||
333 | else |
||
334 | { |
||
335 | return 1; |
||
336 | } |
||
337 | } |
||
338 | |||
339 | /** |
||
340 | * Create donation record into database |
||
341 | * |
||
342 | * @param User $user User who created the donation |
||
343 | * @param int $notrigger Disable triggers |
||
344 | * @return int <0 if KO, id of created donation if OK |
||
345 | * TODO add numbering module for Ref |
||
346 | */ |
||
347 | function create($user, $notrigger=0) |
||
348 | { |
||
349 | global $conf, $langs; |
||
350 | |||
351 | $error = 0; |
||
352 | $ret = 0; |
||
353 | $now=dol_now(); |
||
354 | |||
355 | // Clean parameters |
||
356 | $this->address=($this->address>0?$this->address:$this->address); |
||
357 | $this->zip=($this->zip>0?$this->zip:$this->zip); |
||
358 | $this->town=($this->town>0?$this->town:$this->town); |
||
359 | $this->country_id=($this->country_id>0?$this->country_id:$this->country_id); |
||
360 | $this->country=($this->country?$this->country:$this->country); |
||
361 | |||
362 | $this->db->begin(); |
||
363 | |||
364 | $sql = "INSERT INTO ".MAIN_DB_PREFIX."don ("; |
||
365 | $sql.= "datec"; |
||
366 | $sql.= ", entity"; |
||
367 | $sql.= ", amount"; |
||
368 | $sql.= ", fk_payment"; |
||
369 | $sql.= ", firstname"; |
||
370 | $sql.= ", lastname"; |
||
371 | $sql.= ", societe"; |
||
372 | $sql.= ", address"; |
||
373 | $sql.= ", zip"; |
||
374 | $sql.= ", town"; |
||
375 | $sql.= ", fk_country"; |
||
376 | $sql.= ", public"; |
||
377 | $sql.= ", fk_projet"; |
||
378 | $sql.= ", note_private"; |
||
379 | $sql.= ", note_public"; |
||
380 | $sql.= ", fk_user_author"; |
||
381 | $sql.= ", fk_user_valid"; |
||
382 | $sql.= ", datedon"; |
||
383 | $sql.= ", email"; |
||
384 | $sql.= ", phone"; |
||
385 | $sql.= ", phone_mobile"; |
||
386 | $sql.= ") VALUES ("; |
||
387 | $sql.= " '".$this->db->idate($now)."'"; |
||
388 | $sql.= ", ".$conf->entity; |
||
389 | $sql.= ", ".price2num($this->amount); |
||
390 | $sql.= ", ".($this->modepaymentid?$this->modepaymentid:"null"); |
||
391 | $sql.= ", '".$this->db->escape($this->firstname)."'"; |
||
392 | $sql.= ", '".$this->db->escape($this->lastname)."'"; |
||
393 | $sql.= ", '".$this->db->escape($this->societe)."'"; |
||
394 | $sql.= ", '".$this->db->escape($this->address)."'"; |
||
395 | $sql.= ", '".$this->db->escape($this->zip)."'"; |
||
396 | $sql.= ", '".$this->db->escape($this->town)."'"; |
||
397 | $sql.= ", ".($this->country_id > 0 ? $this->country_id : '0'); |
||
398 | $sql.= ", ".((int) $this->public); |
||
399 | $sql.= ", ".($this->fk_project > 0?$this->fk_project:"null"); |
||
400 | $sql.= ", ".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL"); |
||
401 | $sql.= ", ".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL"); |
||
402 | $sql.= ", ".$user->id; |
||
403 | $sql.= ", null"; |
||
404 | $sql.= ", '".$this->db->idate($this->date)."'"; |
||
405 | $sql.= ", '".$this->db->escape($this->email)."'"; |
||
406 | $sql.= ", '".$this->db->escape($this->phone)."'"; |
||
407 | $sql.= ", '".$this->db->escape($this->phone_mobile)."'"; |
||
408 | $sql.= ")"; |
||
409 | |||
410 | $resql = $this->db->query($sql); |
||
411 | if ($resql) |
||
412 | { |
||
413 | $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don"); |
||
414 | $ret = $this->id; |
||
415 | |||
416 | if (!$notrigger) |
||
417 | { |
||
418 | // Call trigger |
||
419 | $result=$this->call_trigger('DON_CREATE',$user); |
||
420 | if ($result < 0) { $error++; } |
||
421 | // End call triggers |
||
422 | } |
||
423 | } |
||
424 | else |
||
425 | { |
||
426 | $this->error = $this->db->lasterror(); |
||
427 | $this->errno = $this->db->lasterrno(); |
||
428 | $error++; |
||
429 | } |
||
430 | |||
431 | // Update extrafield |
||
432 | if (! $error) { |
||
433 | if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used |
||
434 | { |
||
435 | $result=$this->insertExtraFields(); |
||
436 | if ($result < 0) |
||
437 | { |
||
438 | $error++; |
||
439 | } |
||
440 | } |
||
441 | } |
||
442 | |||
443 | if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS)) |
||
444 | { |
||
445 | //$res = $this->setValid($user); |
||
446 | //if ($res < 0) $error++; |
||
447 | } |
||
448 | |||
449 | if (!$error) |
||
450 | { |
||
451 | $this->db->commit(); |
||
452 | return $ret; |
||
453 | } |
||
454 | else |
||
455 | { |
||
456 | $this->db->rollback(); |
||
457 | return -1; |
||
458 | } |
||
459 | } |
||
460 | |||
461 | /** |
||
462 | * Update a donation record |
||
463 | * |
||
464 | * @param User $user Objet utilisateur qui met a jour le don |
||
465 | * @param int $notrigger Disable triggers |
||
466 | * @return int >0 if OK, <0 if KO |
||
467 | */ |
||
468 | function update($user, $notrigger=0) |
||
469 | { |
||
470 | global $langs, $conf; |
||
471 | |||
472 | $error=0; |
||
473 | |||
474 | // Clean parameters |
||
475 | $this->address=($this->address>0?$this->address:$this->address); |
||
476 | $this->zip=($this->zip>0?$this->zip:$this->zip); |
||
477 | $this->town=($this->town>0?$this->town:$this->town); |
||
478 | $this->country_id=($this->country_id>0?$this->country_id:$this->country_id); |
||
479 | $this->country=($this->country?$this->country:$this->country); |
||
480 | |||
481 | $this->db->begin(); |
||
482 | |||
483 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET "; |
||
484 | $sql .= "amount = " . price2num($this->amount); |
||
485 | $sql .= ",fk_payment = ".($this->modepaymentid?$this->modepaymentid:"null"); |
||
486 | $sql .= ",firstname = '".$this->db->escape($this->firstname)."'"; |
||
487 | $sql .= ",lastname='".$this->db->escape($this->lastname)."'"; |
||
488 | $sql .= ",societe='".$this->db->escape($this->societe)."'"; |
||
489 | $sql .= ",address='".$this->db->escape($this->address)."'"; |
||
490 | $sql .= ",zip='".$this->db->escape($this->zip)."'"; |
||
491 | $sql .= ",town='".$this->db->escape($this->town)."'"; |
||
492 | $sql .= ",fk_country = ".$this->country_id; |
||
493 | $sql .= ",public=".$this->public; |
||
494 | $sql .= ",fk_projet=".($this->fk_project>0?$this->fk_project:'null'); |
||
495 | $sql .= ",note_private=".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL"); |
||
496 | $sql .= ",note_public=".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL"); |
||
497 | $sql .= ",datedon='".$this->db->idate($this->date)."'"; |
||
498 | $sql .= ",date_valid=".($this->date_valid?"'".$this->db->idate($this->date)."'":"null"); |
||
499 | $sql .= ",email='".$this->db->escape($this->email)."'"; |
||
500 | $sql .= ",phone='".$this->db->escape($this->phone)."'"; |
||
501 | $sql .= ",phone_mobile='".$this->db->escape($this->phone_mobile)."'"; |
||
502 | $sql .= ",fk_statut=".$this->statut; |
||
503 | $sql .= " WHERE rowid = ".$this->id; |
||
504 | |||
505 | dol_syslog(get_class($this)."::Update", LOG_DEBUG); |
||
506 | $resql=$this->db->query($sql); |
||
507 | if ($resql) |
||
508 | { |
||
509 | if (!$notrigger) |
||
510 | { |
||
511 | // Call trigger |
||
512 | $result=$this->call_trigger('DON_MODIFY',$user); |
||
513 | if ($result < 0) { $error++; } |
||
514 | // End call triggers |
||
515 | } |
||
516 | |||
517 | // Update extrafield |
||
518 | if (! $error) |
||
519 | { |
||
520 | if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used |
||
521 | { |
||
522 | $result=$this->insertExtraFields(); |
||
523 | if ($result < 0) |
||
524 | { |
||
525 | $error++; |
||
526 | } |
||
527 | } |
||
528 | } |
||
529 | |||
530 | if (! $error ) |
||
531 | { |
||
532 | $this->db->commit(); |
||
533 | $result = 1; |
||
534 | } |
||
535 | else |
||
536 | { |
||
537 | $this->db->rollback(); |
||
538 | $result = -1; |
||
539 | } |
||
540 | } |
||
541 | else |
||
542 | { |
||
543 | $this->error = $this->db->lasterror(); |
||
544 | $this->errors[] = $this->error; |
||
545 | $this->db->rollback(); |
||
546 | dol_syslog(get_class($this)."::Update error -2 " . $this->error, LOG_ERR); |
||
547 | $result = -2; |
||
548 | } |
||
549 | return $result; |
||
550 | } |
||
551 | |||
552 | /** |
||
553 | * Delete a donation from database |
||
554 | * |
||
555 | * @param User $user User |
||
556 | * @param int $notrigger Disable triggers |
||
557 | * @return int <0 if KO, 0 if not possible, >0 if OK |
||
558 | */ |
||
559 | function delete($user, $notrigger=0) |
||
560 | { |
||
561 | global $user, $conf, $langs; |
||
562 | require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php'; |
||
563 | |||
564 | $error = 0; |
||
565 | |||
566 | $this->db->begin(); |
||
567 | |||
568 | if (! $error) |
||
569 | { |
||
570 | if (!$notrigger) |
||
571 | { |
||
572 | // Call trigger |
||
573 | $result=$this->call_trigger('DON_DELETE',$user); |
||
574 | |||
575 | if ($result < 0) { |
||
576 | $error++; |
||
577 | } |
||
578 | // End call triggers |
||
579 | } |
||
580 | } |
||
581 | |||
582 | // Delete donation |
||
583 | if (! $error) |
||
584 | { |
||
585 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "don_extrafields"; |
||
586 | $sql.= " WHERE fk_object=" . $this->id; |
||
587 | |||
588 | $resql = $this->db->query($sql); |
||
589 | if (! $resql) |
||
590 | { |
||
591 | $this->errors[] = $this->db->lasterror(); |
||
592 | $error++; |
||
593 | } |
||
594 | } |
||
595 | |||
596 | if (! $error) |
||
597 | { |
||
598 | $sql = "DELETE FROM " . MAIN_DB_PREFIX . "don"; |
||
599 | $sql.= " WHERE rowid=" . $this->id; |
||
600 | |||
601 | $resql = $this->db->query($sql); |
||
602 | if (!$resql) |
||
603 | { |
||
604 | $this->errors[] = $this->db->lasterror(); |
||
605 | $error++; |
||
606 | } |
||
607 | } |
||
608 | |||
609 | if (! $error) |
||
610 | { |
||
611 | $this->db->commit(); |
||
612 | return 1; |
||
613 | } |
||
614 | else |
||
615 | { |
||
616 | foreach($this->errors as $errmsg) |
||
617 | { |
||
618 | dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR); |
||
619 | $this->error .= ($this->error ? ', ' . $errmsg : $errmsg); |
||
620 | } |
||
621 | dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR); |
||
622 | $this->db->rollback(); |
||
623 | return -1; |
||
624 | } |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * Load donation from database |
||
629 | * |
||
630 | * @param int $id Id of donation to load |
||
631 | * @param string $ref Ref of donation to load |
||
632 | * @return int <0 if KO, >0 if OK |
||
633 | */ |
||
634 | function fetch($id, $ref='') |
||
635 | { |
||
636 | global $conf; |
||
637 | |||
638 | $sql = "SELECT d.rowid, d.datec, d.date_valid, d.tms as datem, d.datedon,"; |
||
639 | $sql.= " d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, "; |
||
640 | $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, "; |
||
641 | $sql.= " d.phone_mobile, d.fk_projet as fk_project, d.model_pdf,"; |
||
642 | $sql.= " p.ref as project_ref,"; |
||
643 | $sql.= " cp.libelle as payment_label, cp.code as payment_code,"; |
||
644 | $sql.= " c.code as country_code, c.label as country"; |
||
645 | $sql.= " FROM ".MAIN_DB_PREFIX."don as d"; |
||
646 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet"; |
||
647 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment"; |
||
648 | $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid"; |
||
649 | $sql.= " WHERE d.entity IN (".getEntity('donation').")"; |
||
650 | if (! empty($id)) |
||
651 | { |
||
652 | $sql.= " AND d.rowid=".$id; |
||
653 | } |
||
654 | else if (! empty($ref)) |
||
655 | { |
||
656 | $sql.= " AND d.ref='".$this->db->escape($ref)."'"; |
||
657 | } |
||
658 | |||
659 | dol_syslog(get_class($this)."::fetch", LOG_DEBUG); |
||
660 | $resql=$this->db->query($sql); |
||
661 | if ($resql) |
||
662 | { |
||
663 | if ($this->db->num_rows($resql)) |
||
664 | { |
||
665 | $obj = $this->db->fetch_object($resql); |
||
666 | |||
667 | $this->id = $obj->rowid; |
||
668 | $this->ref = $obj->rowid; |
||
669 | $this->datec = $this->db->jdate($obj->datec); |
||
670 | $this->date_valid = $this->db->jdate($obj->date_valid); |
||
671 | $this->datem = $this->db->jdate($obj->datem); |
||
672 | $this->date = $this->db->jdate($obj->datedon); |
||
673 | $this->firstname = $obj->firstname; |
||
674 | $this->lastname = $obj->lastname; |
||
675 | $this->societe = $obj->societe; |
||
676 | $this->statut = $obj->fk_statut; |
||
677 | $this->address = $obj->address; |
||
678 | $this->town = $obj->town; |
||
679 | $this->zip = $obj->zip; |
||
680 | $this->town = $obj->town; |
||
681 | $this->country_id = $obj->fk_country; |
||
682 | $this->country_code = $obj->country_code; |
||
683 | $this->country = $obj->country; |
||
684 | $this->country_olddata= $obj->country_olddata; // deprecated |
||
685 | $this->email = $obj->email; |
||
686 | $this->phone = $obj->phone; |
||
687 | $this->phone_mobile = $obj->phone_mobile; |
||
688 | $this->project = $obj->project_ref; |
||
689 | $this->fk_projet = $obj->fk_project; // deprecated |
||
690 | $this->fk_project = $obj->fk_project; |
||
691 | $this->public = $obj->public; |
||
692 | $this->modepaymentid = $obj->fk_payment; |
||
693 | $this->modepaymentcode = $obj->payment_code; |
||
694 | $this->modepayment = $obj->payment_label; |
||
695 | $this->paid = $obj->paid; |
||
696 | $this->amount = $obj->amount; |
||
697 | $this->note_private = $obj->note_private; |
||
698 | $this->note_public = $obj->note_public; |
||
699 | $this->modelpdf = $obj->model_pdf; |
||
700 | |||
701 | // Retreive all extrafield |
||
702 | // fetch optionals attributes and labels |
||
703 | $this->fetch_optionals(); |
||
704 | } |
||
705 | return 1; |
||
706 | } |
||
707 | else |
||
708 | { |
||
709 | dol_print_error($this->db); |
||
710 | return -1; |
||
711 | } |
||
712 | } |
||
713 | |||
714 | /** |
||
715 | * Validate a intervention |
||
716 | * |
||
717 | * @param User $user User that validate |
||
718 | * @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
||
719 | * @return int <0 if KO, >0 if OK |
||
720 | */ |
||
721 | function setValid($user, $notrigger=0) |
||
722 | { |
||
723 | return $this->valid_promesse($this->id, $user->id, $notrigger); |
||
724 | } |
||
725 | |||
726 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
727 | /** |
||
728 | * Validate a promise of donation |
||
729 | * |
||
730 | * @param int $id id of donation |
||
731 | * @param int $userid User who validate the donation/promise |
||
732 | * @param int $notrigger Disable triggers |
||
733 | * @return int <0 if KO, >0 if OK |
||
734 | */ |
||
735 | function valid_promesse($id, $userid, $notrigger=0) |
||
736 | { |
||
737 | // phpcs:enable |
||
738 | global $langs, $user; |
||
739 | |||
740 | $error=0; |
||
741 | |||
742 | $this->db->begin(); |
||
743 | |||
744 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".$userid." WHERE rowid = ".$id." AND fk_statut = 0"; |
||
745 | |||
746 | $resql=$this->db->query($sql); |
||
747 | if ($resql) |
||
748 | { |
||
749 | if ($this->db->affected_rows($resql)) |
||
750 | { |
||
751 | if (!$notrigger) |
||
752 | { |
||
753 | // Call trigger |
||
754 | $result=$this->call_trigger('DON_VALIDATE',$user); |
||
755 | if ($result < 0) { $error++; } |
||
756 | // End call triggers |
||
757 | } |
||
758 | } |
||
759 | } |
||
760 | else |
||
761 | { |
||
762 | $error++; |
||
763 | $this->error = $this->db->lasterror(); |
||
764 | } |
||
765 | |||
766 | if (!$error) |
||
767 | { |
||
768 | $this->db->commit(); |
||
769 | return 1; |
||
770 | } |
||
771 | else |
||
772 | { |
||
773 | $this->db->rollback(); |
||
774 | return -1; |
||
775 | } |
||
776 | } |
||
777 | |||
778 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
779 | /** |
||
780 | * Classify the donation as paid, the donation was received |
||
781 | * |
||
782 | * @param int $id id of donation |
||
783 | * @param int $modepayment mode of payment |
||
784 | * @return int <0 if KO, >0 if OK |
||
785 | */ |
||
786 | function set_paid($id, $modepayment=0) |
||
787 | { |
||
788 | // phpcs:enable |
||
789 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2"; |
||
790 | if ($modepayment) |
||
791 | { |
||
792 | $sql .= ", fk_payment=".$modepayment; |
||
793 | } |
||
794 | $sql .= " WHERE rowid = ".$id." AND fk_statut = 1"; |
||
795 | |||
796 | $resql=$this->db->query($sql); |
||
797 | if ($resql) |
||
798 | { |
||
799 | if ($this->db->affected_rows($resql)) |
||
800 | { |
||
801 | return 1; |
||
802 | } |
||
803 | else |
||
804 | { |
||
805 | return 0; |
||
806 | } |
||
807 | } |
||
808 | else |
||
809 | { |
||
810 | dol_print_error($this->db); |
||
811 | return -1; |
||
812 | } |
||
813 | } |
||
814 | |||
815 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
816 | /** |
||
817 | * Set donation to status cancelled |
||
818 | * |
||
819 | * @param int $id id of donation |
||
820 | * @return int <0 if KO, >0 if OK |
||
821 | */ |
||
822 | function set_cancel($id) |
||
823 | { |
||
824 | // phpcs:enable |
||
825 | $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".$id; |
||
826 | |||
827 | $resql=$this->db->query($sql); |
||
828 | if ($resql) |
||
829 | { |
||
830 | if ( $this->db->affected_rows($resql) ) |
||
831 | { |
||
832 | return 1; |
||
833 | } |
||
834 | else |
||
835 | { |
||
836 | return 0; |
||
837 | } |
||
838 | } |
||
839 | else |
||
840 | { |
||
841 | dol_print_error($this->db); |
||
842 | return -1; |
||
843 | } |
||
844 | } |
||
845 | |||
846 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
847 | /** |
||
848 | * Sum of donations |
||
849 | * |
||
850 | * @param string $param 1=promesses de dons validees , 2=xxx, 3=encaisses |
||
851 | * @return int Summ of donations |
||
852 | */ |
||
853 | 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 = ".$param; |
||
863 | $sql.= " AND entity = ".$conf->entity; |
||
864 | |||
865 | $resql=$this->db->query($sql); |
||
866 | if ($resql) |
||
867 | { |
||
868 | $obj = $this->db->fetch_object($resql); |
||
869 | $result=$obj->total; |
||
870 | } |
||
871 | |||
872 | return $result; |
||
873 | } |
||
874 | |||
875 | // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
||
876 | /** |
||
877 | * Charge indicateurs this->nb pour le tableau de bord |
||
878 | * |
||
879 | * @return int <0 if KO, >0 if OK |
||
880 | */ |
||
881 | function load_state_board() |
||
882 | { |
||
883 | // phpcs:enable |
||
884 | global $conf; |
||
885 | |||
886 | $this->nb=array(); |
||
887 | |||
888 | $sql = "SELECT count(d.rowid) as nb"; |
||
889 | $sql.= " FROM ".MAIN_DB_PREFIX."don as d"; |
||
890 | $sql.= " WHERE d.fk_statut > 0"; |
||
891 | $sql.= " AND d.entity IN (".getEntity('donation').")"; |
||
892 | |||
893 | $resql=$this->db->query($sql); |
||
894 | if ($resql) |
||
895 | { |
||
896 | while ($obj=$this->db->fetch_object($resql)) |
||
897 | { |
||
898 | $this->nb["donations"]=$obj->nb; |
||
899 | } |
||
900 | $this->db->free($resql); |
||
901 | return 1; |
||
902 | } |
||
903 | else |
||
904 | { |
||
905 | dol_print_error($this->db); |
||
906 | $this->error=$this->db->error(); |
||
907 | return -1; |
||
908 | } |
||
909 | } |
||
910 | |||
911 | /** |
||
912 | * Return clicable name (with picto eventually) |
||
913 | * |
||
914 | * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto |
||
915 | * @param int $notooltip 1=Disable tooltip |
||
916 | * @return string Chaine avec URL |
||
917 | */ |
||
918 | function getNomUrl($withpicto=0, $notooltip=0) |
||
919 | { |
||
920 | global $langs; |
||
921 | |||
922 | $result=''; |
||
923 | $label=$langs->trans("ShowDonation").': '.$this->id; |
||
924 | |||
925 | $linkstart = '<a href="' . BASE_URL . '?controller=don&method=card&id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">'; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
926 | $linkend='</a>'; |
||
927 | |||
928 | $result .= $linkstart; |
||
929 | if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1); |
||
930 | if ($withpicto != 2) $result.= $this->ref; |
||
931 | $result .= $linkend; |
||
932 | |||
933 | return $result; |
||
934 | } |
||
935 | |||
936 | /** |
||
937 | * Information on record |
||
938 | * |
||
939 | * @param int $id Id of record |
||
940 | * @return void |
||
941 | */ |
||
942 | function info($id) |
||
943 | { |
||
944 | $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,'; |
||
945 | $sql.= ' d.tms'; |
||
946 | $sql.= ' FROM '.MAIN_DB_PREFIX.'don as d'; |
||
947 | $sql.= ' WHERE d.rowid = '.$id; |
||
948 | |||
949 | dol_syslog(get_class($this).'::info', LOG_DEBUG); |
||
950 | $result = $this->db->query($sql); |
||
951 | |||
952 | if ($result) |
||
953 | { |
||
954 | if ($this->db->num_rows($result)) |
||
955 | { |
||
956 | $obj = $this->db->fetch_object($result); |
||
957 | $this->id = $obj->rowid; |
||
958 | if ($obj->fk_user_author) |
||
959 | { |
||
960 | $cuser = new User($this->db); |
||
961 | $cuser->fetch($obj->fk_user_author); |
||
962 | $this->user_creation = $cuser; |
||
963 | } |
||
964 | if ($obj->fk_user_valid) |
||
965 | { |
||
966 | $vuser = new User($this->db); |
||
967 | $vuser->fetch($obj->fk_user_valid); |
||
968 | $this->user_modification = $vuser; |
||
969 | } |
||
970 | $this->date_creation = $this->db->jdate($obj->datec); |
||
971 | $this->date_modification = $this->db->jdate($obj->tms); |
||
972 | } |
||
973 | $this->db->free($result); |
||
974 | } |
||
975 | else |
||
976 | { |
||
977 | dol_print_error($this->db); |
||
978 | } |
||
979 | } |
||
980 | |||
981 | |||
982 | /** |
||
983 | * Create a document onto disk according to template module. |
||
984 | * |
||
985 | * @param string $modele Force template to use ('' to not force) |
||
986 | * @param Translate $outputlangs objet lang a utiliser pour traduction |
||
987 | * @param int $hidedetails Hide details of lines |
||
988 | * @param int $hidedesc Hide description |
||
989 | * @param int $hideref Hide ref |
||
990 | * @return int 0 if KO, 1 if OK |
||
991 | */ |
||
992 | public function generateDocument($modele, $outputlangs, $hidedetails=0, $hidedesc=0, $hideref=0) |
||
993 | { |
||
994 | global $conf,$langs; |
||
995 | |||
996 | $langs->load("bills"); |
||
997 | |||
998 | if (! dol_strlen($modele)) { |
||
999 | |||
1000 | $modele = 'html_cerfafr'; |
||
1001 | |||
1002 | if ($this->modelpdf) { |
||
1003 | $modele = $this->modelpdf; |
||
1004 | } elseif (! empty($conf->global->DON_ADDON_MODEL)) { |
||
1005 | $modele = $conf->global->DON_ADDON_MODEL; |
||
1006 | } |
||
1007 | } |
||
1008 | |||
1009 | $modelpath = "core/modules/dons/"; |
||
1010 | |||
1011 | // TODO Restore use of commonGenerateDocument instead of dedicated code here |
||
1012 | //return $this->commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref); |
||
1013 | |||
1014 | // Increase limit for PDF build |
||
1015 | $err=error_reporting(); |
||
1016 | error_reporting(0); |
||
1017 | @set_time_limit(120); |
||
1018 | error_reporting($err); |
||
1019 | |||
1020 | $srctemplatepath=''; |
||
1021 | |||
1022 | // If selected modele is a filename template (then $modele="modelname:filename") |
||
1023 | $tmp=explode(':',$modele,2); |
||
1024 | if (! empty($tmp[1])) |
||
1025 | { |
||
1026 | $modele=$tmp[0]; |
||
1027 | $srctemplatepath=$tmp[1]; |
||
1028 | } |
||
1029 | |||
1030 | // Search template files |
||
1031 | $file=''; $classname=''; $filefound=0; |
||
1032 | $dirmodels=array('/'); |
||
1033 | if (is_array($conf->modules_parts['models'])) $dirmodels=array_merge($dirmodels,$conf->modules_parts['models']); |
||
1034 | foreach($dirmodels as $reldir) |
||
1035 | { |
||
1036 | foreach(array('html','doc','pdf') as $prefix) |
||
1037 | { |
||
1038 | $file = $prefix."_".preg_replace('/^html_/','',$modele).".modules.php"; |
||
1039 | |||
1040 | // On verifie l'emplacement du modele |
||
1041 | $file=dol_buildpath($reldir."core/modules/dons/".$file,0); |
||
1042 | if (file_exists($file)) |
||
1043 | { |
||
1044 | $filefound=1; |
||
1045 | $classname=$prefix.'_'.$modele; |
||
1046 | break; |
||
1047 | } |
||
1048 | } |
||
1049 | if ($filefound) break; |
||
1050 | } |
||
1051 | |||
1052 | // Charge le modele |
||
1053 | if ($filefound) |
||
1054 | { |
||
1055 | require_once $file; |
||
1056 | |||
1057 | $object=$this; |
||
1058 | |||
1059 | $classname = $modele; |
||
1060 | $obj = new $classname($this->db); |
||
1061 | |||
1062 | // We save charset_output to restore it because write_file can change it if needed for |
||
1063 | // output format that does not support UTF8. |
||
1064 | $sav_charset_output=$outputlangs->charset_output; |
||
1065 | if ($obj->write_file($object,$outputlangs, $srctemplatepath, $hidedetails, $hidedesc, $hideref) > 0) |
||
1066 | { |
||
1067 | $outputlangs->charset_output=$sav_charset_output; |
||
1068 | |||
1069 | // we delete preview files |
||
1070 | require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; |
||
1071 | dol_delete_preview($object); |
||
1072 | return 1; |
||
1073 | } |
||
1074 | else |
||
1075 | { |
||
1076 | $outputlangs->charset_output=$sav_charset_output; |
||
1077 | dol_syslog("Erreur dans don_create"); |
||
1078 | dol_print_error($this->db,$obj->error); |
||
1079 | return 0; |
||
1080 | } |
||
1081 | } |
||
1082 | else |
||
1083 | { |
||
1084 | print $langs->trans("Error")." ".$langs->trans("ErrorFileDoesNotExists",$file); |
||
1085 | return 0; |
||
1086 | } |
||
1087 | } |
||
1088 | } |
||
1089 |