don.class.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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@capnetworks.com>
  5. * Copyright (C) 2014 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2015 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  7. * Copyright (C) 2016 Juanjo Menent <jmenent@2byte.es>
  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. * \file htdocs/don/class/don.class.php
  24. * \ingroup Donation
  25. * \brief File of class to manage donations
  26. */
  27. require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
  28. /**
  29. * \class Don
  30. * \brief Class to manage donations
  31. */
  32. class Don extends CommonObject
  33. {
  34. public $element='don'; // Id that identify managed objects
  35. public $table_element='don'; // Name of table without prefix where object is stored
  36. public $fk_element = 'fk_donation';
  37. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  38. var $date;
  39. var $amount;
  40. var $societe;
  41. var $address;
  42. var $zip;
  43. var $town;
  44. var $email;
  45. var $public;
  46. var $fk_projet;
  47. var $fk_typepayment;
  48. var $num_payment;
  49. /**
  50. * @deprecated
  51. * @see note_private, note_public
  52. */
  53. var $commentaire;
  54. /**
  55. * Constructor
  56. *
  57. * @param DoliDB $db Database handler
  58. */
  59. function __construct($db)
  60. {
  61. global $langs;
  62. $this->db = $db;
  63. $this->modepaiementid = 0;
  64. $langs->load("donations");
  65. $this->labelstatut[-1]=$langs->trans("Canceled");
  66. $this->labelstatut[0]=$langs->trans("DonationStatusPromiseNotValidated");
  67. $this->labelstatut[1]=$langs->trans("DonationStatusPromiseValidated");
  68. $this->labelstatut[2]=$langs->trans("DonationStatusPaid");
  69. $this->labelstatutshort[-1]=$langs->trans("Canceled");
  70. $this->labelstatutshort[0]=$langs->trans("DonationStatusPromiseNotValidatedShort");
  71. $this->labelstatutshort[1]=$langs->trans("DonationStatusPromiseValidatedShort");
  72. $this->labelstatutshort[2]=$langs->trans("DonationStatusPaidShort");
  73. }
  74. /**
  75. * Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee)
  76. *
  77. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
  78. * @return string Libelle
  79. */
  80. function getLibStatut($mode=0)
  81. {
  82. return $this->LibStatut($this->statut,$mode);
  83. }
  84. /**
  85. * Renvoi le libelle d'un statut donne
  86. *
  87. * @param int $statut Id statut
  88. * @param int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
  89. * @return string Libelle du statut
  90. */
  91. function LibStatut($statut,$mode=0)
  92. {
  93. global $langs;
  94. if ($mode == 0)
  95. {
  96. return $this->labelstatut[$statut];
  97. }
  98. if ($mode == 1)
  99. {
  100. return $this->labelstatutshort[$statut];
  101. }
  102. if ($mode == 2)
  103. {
  104. if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5').' '.$this->labelstatutshort[$statut];
  105. if ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0').' '.$this->labelstatutshort[$statut];
  106. if ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1').' '.$this->labelstatutshort[$statut];
  107. if ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6').' '.$this->labelstatutshort[$statut];
  108. }
  109. if ($mode == 3)
  110. {
  111. $prefix='Short';
  112. if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5');
  113. if ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0');
  114. if ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1');
  115. if ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6');
  116. }
  117. if ($mode == 4)
  118. {
  119. if ($statut == -1) return img_picto($this->labelstatut[$statut],'statut5').' '.$this->labelstatut[$statut];
  120. if ($statut == 0) return img_picto($this->labelstatut[$statut],'statut0').' '.$this->labelstatut[$statut];
  121. if ($statut == 1) return img_picto($this->labelstatut[$statut],'statut1').' '.$this->labelstatut[$statut];
  122. if ($statut == 2) return img_picto($this->labelstatut[$statut],'statut6').' '.$this->labelstatut[$statut];
  123. }
  124. if ($mode == 5)
  125. {
  126. $prefix='Short';
  127. if ($statut == -1) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut5');
  128. if ($statut == 0) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut0');
  129. if ($statut == 1) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut1');
  130. if ($statut == 2) return $this->labelstatutshort[$statut].' '.img_picto($this->labelstatut[$statut],'statut6');
  131. }
  132. }
  133. /**
  134. * Initialise an instance with random values.
  135. * Used to build previews or test instances.
  136. * id must be 0 if object instance is a specimen.
  137. *
  138. * @return void
  139. */
  140. function initAsSpecimen()
  141. {
  142. global $conf, $user,$langs;
  143. // Charge tableau des id de societe socids
  144. $socids = array();
  145. $sql = "SELECT rowid";
  146. $sql.= " FROM ".MAIN_DB_PREFIX."societe";
  147. $sql.= " WHERE client IN (1, 3)";
  148. $sql.= " AND entity = ".$conf->entity;
  149. $sql.= " LIMIT 10";
  150. $resql = $this->db->query($sql);
  151. if ($resql)
  152. {
  153. $num_socs = $this->db->num_rows($resql);
  154. $i = 0;
  155. while ($i < $num_socs)
  156. {
  157. $i++;
  158. $row = $this->db->fetch_row($resql);
  159. $socids[$i] = $row[0];
  160. }
  161. }
  162. // Initialise parametres
  163. $this->id=0;
  164. $this->ref = 'SPECIMEN';
  165. $this->specimen=1;
  166. $this->lastname = 'Doe';
  167. $this->firstname = 'John';
  168. $this->socid = 1;
  169. $this->date = dol_now();
  170. $this->amount = 100;
  171. $this->public = 1;
  172. $this->societe = 'The Company';
  173. $this->address = 'Twist road';
  174. $this->zip = '99999';
  175. $this->town = 'Town';
  176. $this->note_private='Private note';
  177. $this->note_public='Public note';
  178. $this->email='email@email.com';
  179. $this->note='';
  180. $this->statut=1;
  181. }
  182. /**
  183. * Check params and init ->errors array.
  184. * TODO This function seems to not be used by core code.
  185. *
  186. * @param int $minimum Minimum
  187. * @return int 0 if KO, >0 if OK
  188. */
  189. function check($minimum=0)
  190. {
  191. global $langs;
  192. $langs->load('main');
  193. $langs->load('companies');
  194. $error_string = array();
  195. $err = 0;
  196. if (dol_strlen(trim($this->societe)) == 0)
  197. {
  198. if ((dol_strlen(trim($this->lastname)) + dol_strlen(trim($this->firstname))) == 0)
  199. {
  200. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Company').'/'.$langs->trans('Firstname').'-'.$langs->trans('Lastname'));
  201. $err++;
  202. }
  203. }
  204. if (dol_strlen(trim($this->address)) == 0)
  205. {
  206. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Address'));
  207. $err++;
  208. }
  209. if (dol_strlen(trim($this->zip)) == 0)
  210. {
  211. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Zip'));
  212. $err++;
  213. }
  214. if (dol_strlen(trim($this->town)) == 0)
  215. {
  216. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Town'));
  217. $err++;
  218. }
  219. if (dol_strlen(trim($this->email)) == 0)
  220. {
  221. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('EMail'));
  222. $err++;
  223. }
  224. $this->amount = trim($this->amount);
  225. $map = range(0,9);
  226. $len=dol_strlen($this->amount);
  227. for ($i = 0; $i < $len; $i++)
  228. {
  229. if (!isset($map[substr($this->amount, $i, 1)] ))
  230. {
  231. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Amount'));
  232. $err++;
  233. $amount_invalid = 1;
  234. break;
  235. }
  236. }
  237. if (! $amount_invalid)
  238. {
  239. if ($this->amount == 0)
  240. {
  241. $error_string[] = $langs->trans('ErrorFieldRequired',$langs->trans('Amount'));
  242. $err++;
  243. }
  244. else
  245. {
  246. if ($this->amount < $minimum && $minimum > 0)
  247. {
  248. $error_string[] = $langs->trans('MinimumAmount',$langs->trans('$minimum'));
  249. $err++;
  250. }
  251. }
  252. }
  253. if ($err)
  254. {
  255. $this->errors = $error_string;
  256. return 0;
  257. }
  258. else
  259. {
  260. return 1;
  261. }
  262. }
  263. /**
  264. * Create donation record into database
  265. *
  266. * @param User $user User who created the donation
  267. * @param int $notrigger Disable triggers
  268. * @return int <0 if KO, id of created donation if OK
  269. * TODO add numbering module for Ref
  270. */
  271. function create($user, $notrigger=0)
  272. {
  273. global $conf, $langs;
  274. $error = 0;
  275. $ret = 0;
  276. $now=dol_now();
  277. // Clean parameters
  278. $this->address=($this->address>0?$this->address:$this->address);
  279. $this->zip=($this->zip>0?$this->zip:$this->zip);
  280. $this->town=($this->town>0?$this->town:$this->town);
  281. $this->country_id=($this->country_id>0?$this->country_id:$this->country_id);
  282. $this->country=($this->country?$this->country:$this->country);
  283. $this->db->begin();
  284. $sql = "INSERT INTO ".MAIN_DB_PREFIX."don (";
  285. $sql.= "datec";
  286. $sql.= ", entity";
  287. $sql.= ", amount";
  288. $sql.= ", fk_payment";
  289. $sql.= ", firstname";
  290. $sql.= ", lastname";
  291. $sql.= ", societe";
  292. $sql.= ", address";
  293. $sql.= ", zip";
  294. $sql.= ", town";
  295. // $sql.= ", country"; -- Deprecated
  296. $sql.= ", fk_country";
  297. $sql.= ", public";
  298. $sql.= ", fk_projet";
  299. $sql.= ", note_private";
  300. $sql.= ", note_public";
  301. $sql.= ", fk_user_author";
  302. $sql.= ", fk_user_valid";
  303. $sql.= ", datedon";
  304. $sql.= ", email";
  305. $sql.= ", phone";
  306. $sql.= ", phone_mobile";
  307. $sql.= ") VALUES (";
  308. $sql.= " '".$this->db->idate($now)."'";
  309. $sql.= ", ".$conf->entity;
  310. $sql.= ", ".price2num($this->amount);
  311. $sql.= ", ".($this->modepaiementid?$this->modepaiementid:"null");
  312. $sql.= ", '".$this->db->escape($this->firstname)."'";
  313. $sql.= ", '".$this->db->escape($this->lastname)."'";
  314. $sql.= ", '".$this->db->escape($this->societe)."'";
  315. $sql.= ", '".$this->db->escape($this->address)."'";
  316. $sql.= ", '".$this->db->escape($this->zip)."'";
  317. $sql.= ", '".$this->db->escape($this->town)."'";
  318. $sql.= ", ".$this->country_id;
  319. $sql.= ", ".$this->public;
  320. $sql.= ", ".($this->fk_projet > 0?$this->fk_projet:"null");
  321. $sql.= ", ".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL");
  322. $sql.= ", ".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL");
  323. $sql.= ", ".$user->id;
  324. $sql.= ", null";
  325. $sql.= ", '".$this->db->idate($this->date)."'";
  326. $sql.= ", '".$this->db->escape($this->email)."'";
  327. $sql.= ", '".$this->db->escape($this->phone)."'";
  328. $sql.= ", '".$this->db->escape($this->phone_mobile)."'";
  329. $sql.= ")";
  330. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  331. $resql = $this->db->query($sql);
  332. if ($resql)
  333. {
  334. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."don");
  335. $ret = $this->id;
  336. if (!$notrigger)
  337. {
  338. // Call trigger
  339. $result=$this->call_trigger('DON_CREATE',$user);
  340. if ($result < 0) { $error++; }
  341. // End call triggers
  342. }
  343. }
  344. else
  345. {
  346. $this->error = $this->db->lasterror();
  347. $this->errno = $this->db->lasterrno();
  348. $error++;
  349. }
  350. // Update extrafield
  351. if (!$error) {
  352. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  353. {
  354. $result=$this->insertExtraFields();
  355. if ($result < 0)
  356. {
  357. $error++;
  358. }
  359. }
  360. }
  361. if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
  362. {
  363. $res = $this->setValid($user);
  364. if ($res < 0) $error++;
  365. }
  366. if (!$error)
  367. {
  368. $this->db->commit();
  369. return $ret;
  370. }
  371. else
  372. {
  373. $this->db->rollback();
  374. return -1;
  375. }
  376. }
  377. /**
  378. * Update a donation record
  379. *
  380. * @param User $user Objet utilisateur qui met a jour le don
  381. * @param int $notrigger Disable triggers
  382. * @return int >0 if OK, <0 if KO
  383. */
  384. function update($user, $notrigger=0)
  385. {
  386. global $langs, $conf;
  387. $error=0;
  388. // Clean parameters
  389. $this->address=($this->address>0?$this->address:$this->address);
  390. $this->zip=($this->zip>0?$this->zip:$this->zip);
  391. $this->town=($this->town>0?$this->town:$this->town);
  392. $this->country_id=($this->country_id>0?$this->country_id:$this->country_id);
  393. $this->country=($this->country?$this->country:$this->country);
  394. $this->db->begin();
  395. $sql = "UPDATE ".MAIN_DB_PREFIX."don SET ";
  396. $sql .= "amount = " . price2num($this->amount);
  397. $sql .= ",fk_payment = ".($this->modepaymentid?$this->modepaymentid:"null");
  398. $sql .= ",firstname = '".$this->db->escape($this->firstname)."'";
  399. $sql .= ",lastname='".$this->db->escape($this->lastname)."'";
  400. $sql .= ",societe='".$this->db->escape($this->societe)."'";
  401. $sql .= ",address='".$this->db->escape($this->address)."'";
  402. $sql .= ",zip='".$this->db->escape($this->zip)."'";
  403. $sql .= ",town='".$this->db->escape($this->town)."'";
  404. $sql .= ",fk_country = ".$this->country_id;
  405. $sql .= ",public=".$this->public;
  406. $sql .= ",fk_projet=".($this->fk_projet>0?$this->fk_projet:'null');
  407. $sql .= ",note_private=".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL");
  408. $sql .= ",note_public=".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL");
  409. $sql .= ",datedon='".$this->db->idate($this->date)."'";
  410. $sql .= ",email='".$this->email."'";
  411. $sql .= ",phone='".$this->phone."'";
  412. $sql .= ",phone_mobile='".$this->phone_mobile."'";
  413. $sql .= ",fk_statut=".$this->statut;
  414. $sql .= " WHERE rowid = '".$this->id."'";
  415. dol_syslog(get_class($this)."::Update", LOG_DEBUG);
  416. $resql=$this->db->query($sql);
  417. if ($resql)
  418. {
  419. if (!$notrigger)
  420. {
  421. // Call trigger
  422. $result=$this->call_trigger('DON_MODIFY',$user);
  423. if ($result < 0) { $error++; }
  424. // End call triggers
  425. }
  426. // Update extrafield
  427. if (!$error)
  428. {
  429. if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
  430. {
  431. $result=$this->insertExtraFields();
  432. if ($result < 0)
  433. {
  434. $error++;
  435. }
  436. }
  437. }
  438. if (! $error )
  439. {
  440. $this->db->commit();
  441. $result = 1;
  442. }
  443. else
  444. {
  445. $this->db->rollback();
  446. $result = -1;
  447. }
  448. }
  449. else
  450. {
  451. $this->error = $this->db->lasterror();
  452. $this->errors[] = $this->error;
  453. $this->db->rollback();
  454. dol_syslog(get_class($this)."::Update error -2 " . $this->error, LOG_ERR);
  455. $result = -2;
  456. }
  457. return $result;
  458. }
  459. /**
  460. * Delete a donation from database
  461. *
  462. * @param User $user User
  463. * @param int $notrigger Disable triggers
  464. * @return int <0 if KO, 0 if not possible, >0 if OK
  465. */
  466. function delete($user, $notrigger=0)
  467. {
  468. global $user, $conf, $langs;
  469. require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
  470. $error = 0;
  471. $this->db->begin();
  472. if (! $error)
  473. {
  474. if (!$notrigger)
  475. {
  476. // Call trigger
  477. $result=$this->call_trigger('DON_DELETE',$user);
  478. if ($result < 0) {
  479. $error++;
  480. }
  481. // End call triggers
  482. }
  483. }
  484. // Delete donation
  485. if (! $error)
  486. {
  487. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "don_extrafields";
  488. $sql.= " WHERE fk_object=" . $this->id;
  489. $resql = $this->db->query($sql);
  490. if (! $resql)
  491. {
  492. $this->errors[] = $this->db->lasterror();
  493. $error++;
  494. }
  495. }
  496. if (! $error)
  497. {
  498. $sql = "DELETE FROM " . MAIN_DB_PREFIX . "don";
  499. $sql.= " WHERE rowid=" . $this->id;
  500. $resql = $this->db->query($sql);
  501. if (!$resql)
  502. {
  503. $this->errors[] = $this->db->lasterror();
  504. $error++;
  505. }
  506. }
  507. if (! $error)
  508. {
  509. $this->db->commit();
  510. return 1;
  511. }
  512. else
  513. {
  514. foreach($this->errors as $errmsg)
  515. {
  516. dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
  517. $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
  518. }
  519. dol_syslog(get_class($this) . "::delete " . $this->error, LOG_ERR);
  520. $this->db->rollback();
  521. return -1;
  522. }
  523. }
  524. /**
  525. * Load donation from database
  526. *
  527. * @param int $id Id of donation to load
  528. * @param string $ref Ref of donation to load
  529. * @return int <0 if KO, >0 if OK
  530. */
  531. function fetch($id, $ref='')
  532. {
  533. global $conf;
  534. $sql = "SELECT d.rowid, d.datec, d.tms as datem, d.datedon,";
  535. $sql.= " d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
  536. $sql.= " d.fk_country, d.country as country_olddata, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, cp.libelle, d.email, d.phone, ";
  537. $sql.= " d.phone_mobile, d.fk_projet, d.model_pdf,";
  538. $sql.= " p.ref as project_ref,";
  539. $sql.= " c.code as country_code, c.label as country";
  540. $sql.= " FROM ".MAIN_DB_PREFIX."don as d";
  541. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."projet as p ON p.rowid = d.fk_projet";
  542. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as cp ON cp.id = d.fk_payment";
  543. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as c ON d.fk_country = c.rowid";
  544. if (! empty($id))
  545. {
  546. $sql.= " WHERE d.rowid=".$id;
  547. }
  548. else if (! empty($ref))
  549. {
  550. $sql.= " WHERE ref='".$this->db->escape($ref)."'";
  551. }
  552. $sql.= " AND d.entity = ".$conf->entity;
  553. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  554. $resql=$this->db->query($sql);
  555. if ($resql)
  556. {
  557. if ($this->db->num_rows($resql))
  558. {
  559. $obj = $this->db->fetch_object($resql);
  560. $this->id = $obj->rowid;
  561. $this->ref = $obj->rowid;
  562. $this->datec = $this->db->jdate($obj->datec);
  563. $this->datem = $this->db->jdate($obj->datem);
  564. $this->date = $this->db->jdate($obj->datedon);
  565. $this->firstname = $obj->firstname;
  566. $this->lastname = $obj->lastname;
  567. $this->societe = $obj->societe;
  568. $this->statut = $obj->fk_statut;
  569. $this->address = $obj->address;
  570. $this->town = $obj->town;
  571. $this->zip = $obj->zip;
  572. $this->town = $obj->town;
  573. $this->country_id = $obj->fk_country;
  574. $this->country_code = $obj->country_code;
  575. $this->country = $obj->country;
  576. $this->country_olddata= $obj->country_olddata; // deprecated
  577. $this->email = $obj->email;
  578. $this->phone = $obj->phone;
  579. $this->phone_mobile = $obj->phone_mobile;
  580. $this->project = $obj->project_ref;
  581. $this->fk_projet = $obj->fk_projet;
  582. $this->public = $obj->public;
  583. $this->modepaymentid = $obj->fk_payment;
  584. $this->modepayment = $obj->libelle;
  585. $this->paid = $obj->paid;
  586. $this->amount = $obj->amount;
  587. $this->note_private = $obj->note_private;
  588. $this->note_public = $obj->note_public;
  589. $this->modelpdf = $obj->model_pdf;
  590. $this->commentaire = $obj->note; // deprecated
  591. // Retrieve all extrafield for thirdparty
  592. // fetch optionals attributes and labels
  593. require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
  594. $extrafields=new ExtraFields($this->db);
  595. $extralabels=$extrafields->fetch_name_optionals_label($this->table_element,true);
  596. $this->fetch_optionals($this->id,$extralabels);
  597. }
  598. return 1;
  599. }
  600. else
  601. {
  602. dol_print_error($this->db);
  603. return -1;
  604. }
  605. }
  606. /**
  607. * Validate a promise of donation
  608. *
  609. * @param int $id id of donation
  610. * @param int $userid User who validate the donation/promise
  611. * @return int <0 if KO, >0 if OK
  612. */
  613. function valid_promesse($id, $userid)
  614. {
  615. $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 1, fk_user_valid = ".$userid." WHERE rowid = ".$id." AND fk_statut = 0";
  616. $resql=$this->db->query($sql);
  617. if ($resql)
  618. {
  619. if ( $this->db->affected_rows($resql) )
  620. {
  621. return 1;
  622. }
  623. else
  624. {
  625. return 0;
  626. }
  627. }
  628. else
  629. {
  630. dol_print_error($this->db);
  631. return -1;
  632. }
  633. }
  634. /**
  635. * Classify the donation as paid, the donation was received
  636. *
  637. * @param int $id id of donation
  638. * @param int $modepayment mode of payment
  639. * @return int <0 if KO, >0 if OK
  640. */
  641. function set_paid($id, $modepayment='')
  642. {
  643. $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = 2";
  644. if ($modepayment)
  645. {
  646. $sql .= ", fk_payment=$modepayment";
  647. }
  648. $sql .= " WHERE rowid = $id AND fk_statut = 1";
  649. $resql=$this->db->query($sql);
  650. if ($resql)
  651. {
  652. if ($this->db->affected_rows($resql))
  653. {
  654. return 1;
  655. }
  656. else
  657. {
  658. return 0;
  659. }
  660. }
  661. else
  662. {
  663. dol_print_error($this->db);
  664. return -1;
  665. }
  666. }
  667. /**
  668. * Set donation to status cancelled
  669. *
  670. * @param int $id id of donation
  671. * @return int <0 if KO, >0 if OK
  672. */
  673. function set_cancel($id)
  674. {
  675. $sql = "UPDATE ".MAIN_DB_PREFIX."don SET fk_statut = -1 WHERE rowid = ".$id;
  676. $resql=$this->db->query($sql);
  677. if ($resql)
  678. {
  679. if ( $this->db->affected_rows($resql) )
  680. {
  681. return 1;
  682. }
  683. else
  684. {
  685. return 0;
  686. }
  687. }
  688. else
  689. {
  690. dol_print_error($this->db);
  691. return -1;
  692. }
  693. }
  694. /**
  695. * Sum of donations
  696. *
  697. * @param string $param 1=promesses de dons validees , 2=xxx, 3=encaisses
  698. * @return int Summ of donations
  699. */
  700. function sum_donations($param)
  701. {
  702. global $conf;
  703. $result=0;
  704. $sql = "SELECT sum(amount) as total";
  705. $sql.= " FROM ".MAIN_DB_PREFIX."don";
  706. $sql.= " WHERE fk_statut = ".$param;
  707. $sql.= " AND entity = ".$conf->entity;
  708. $resql=$this->db->query($sql);
  709. if ($resql)
  710. {
  711. $obj = $this->db->fetch_object($resql);
  712. $result=$obj->total;
  713. }
  714. return $result;
  715. }
  716. /**
  717. * Return clicable name (with picto eventually)
  718. *
  719. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  720. * @return string Chaine avec URL
  721. */
  722. function getNomUrl($withpicto=0)
  723. {
  724. global $langs;
  725. $result='';
  726. $label=$langs->trans("ShowDonation").': '.$this->id;
  727. $link = '<a href="'.DOL_URL_ROOT.'/don/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
  728. $linkend='</a>';
  729. $picto='generic';
  730. if ($withpicto) $result.=($link.img_object($label, $picto, 'class="classfortooltip"').$linkend);
  731. if ($withpicto && $withpicto != 2) $result.=' ';
  732. if ($withpicto != 2) $result.=$link.$this->id.$linkend;
  733. return $result;
  734. }
  735. /**
  736. * Information on record
  737. *
  738. * @param int $id Id of record
  739. * @return void
  740. */
  741. function info($id)
  742. {
  743. $sql = 'SELECT d.rowid, d.datec, d.fk_user_author, d.fk_user_valid,';
  744. $sql.= ' d.tms';
  745. $sql.= ' FROM '.MAIN_DB_PREFIX.'don as d';
  746. $sql.= ' WHERE d.rowid = '.$id;
  747. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  748. $result = $this->db->query($sql);
  749. if ($result)
  750. {
  751. if ($this->db->num_rows($result))
  752. {
  753. $obj = $this->db->fetch_object($result);
  754. $this->id = $obj->rowid;
  755. if ($obj->fk_user_author)
  756. {
  757. $cuser = new User($this->db);
  758. $cuser->fetch($obj->fk_user_author);
  759. $this->user_creation = $cuser;
  760. }
  761. if ($obj->fk_user_valid)
  762. {
  763. $vuser = new User($this->db);
  764. $vuser->fetch($obj->fk_user_valid);
  765. $this->user_modification = $vuser;
  766. }
  767. $this->date_creation = $this->db->jdate($obj->datec);
  768. $this->date_modification = $this->db->jdate($obj->tms);
  769. }
  770. $this->db->free($result);
  771. }
  772. else
  773. {
  774. dol_print_error($this->db);
  775. }
  776. }
  777. }