loan.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@zendsi.com>
  3. * Copyright (C) 2015-2018 Frédéric France <frederic.france@netlogic.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/loan/class/loan.class.php
  20. * \ingroup loan
  21. * \brief Class for loan module
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. /**
  25. * Loan
  26. */
  27. class Loan extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element='loan';
  33. public $table='loan';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element='loan';
  38. public $picto = 'bill';
  39. /**
  40. * @var int ID
  41. */
  42. public $rowid;
  43. public $datestart;
  44. public $dateend;
  45. public $label;
  46. public $capital;
  47. public $nbterm;
  48. public $rate;
  49. public $paid;
  50. public $account_capital;
  51. public $account_insurance;
  52. public $account_interest;
  53. public $date_creation;
  54. public $date_modification;
  55. public $date_validation;
  56. public $fk_bank;
  57. public $fk_user_creat;
  58. public $fk_user_modif;
  59. public $fk_project;
  60. /**
  61. * Constructor
  62. *
  63. * @param DoliDB $db Database handler
  64. */
  65. function __construct($db)
  66. {
  67. $this->db = $db;
  68. }
  69. /**
  70. * Load object in memory from database
  71. *
  72. * @param int $id id object
  73. * @return int <0 error , >=0 no error
  74. */
  75. function fetch($id)
  76. {
  77. $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public,";
  78. $sql.= " l.paid, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
  79. $sql.= " FROM ".MAIN_DB_PREFIX."loan as l";
  80. $sql.= " WHERE l.rowid = ".$id;
  81. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  82. $resql=$this->db->query($sql);
  83. if ($resql)
  84. {
  85. if ($this->db->num_rows($resql))
  86. {
  87. $obj = $this->db->fetch_object($resql);
  88. $this->id = $obj->rowid;
  89. $this->ref = $obj->rowid;
  90. $this->datestart = $this->db->jdate($obj->datestart);
  91. $this->dateend = $this->db->jdate($obj->dateend);
  92. $this->label = $obj->label;
  93. $this->capital = $obj->capital;
  94. $this->nbterm = $obj->nbterm;
  95. $this->rate = $obj->rate;
  96. $this->note_private = $obj->note_private;
  97. $this->note_public = $obj->note_public;
  98. $this->paid = $obj->paid;
  99. $this->account_capital = $obj->accountancy_account_capital;
  100. $this->account_insurance = $obj->accountancy_account_insurance;
  101. $this->account_interest = $obj->accountancy_account_interest;
  102. $this->fk_project = $obj->fk_project;
  103. $this->db->free($resql);
  104. return 1;
  105. }
  106. else
  107. {
  108. $this->db->free($resql);
  109. return 0;
  110. }
  111. }
  112. else
  113. {
  114. $this->error=$this->db->lasterror();
  115. return -1;
  116. }
  117. }
  118. /**
  119. * Create a loan into database
  120. *
  121. * @param User $user User making creation
  122. * @return int <0 if KO, id if OK
  123. */
  124. function create($user)
  125. {
  126. global $conf, $langs;
  127. $error=0;
  128. $now=dol_now();
  129. // clean parameters
  130. $newcapital=price2num($this->capital,'MT');
  131. if (isset($this->note_private)) $this->note_private = trim($this->note_private);
  132. if (isset($this->note_public)) $this->note_public = trim($this->note_public);
  133. if (isset($this->account_capital)) $this->account_capital = trim($this->account_capital);
  134. if (isset($this->account_insurance)) $this->account_insurance = trim($this->account_insurance);
  135. if (isset($this->account_interest)) $this->account_interest = trim($this->account_interest);
  136. if (isset($this->fk_bank)) $this->fk_bank=trim($this->fk_bank);
  137. if (isset($this->fk_user_creat)) $this->fk_user_creat=trim($this->fk_user_creat);
  138. if (isset($this->fk_user_modif)) $this->fk_user_modif=trim($this->fk_user_modif);
  139. if (isset($this->fk_project)) $this->fk_project=trim($this->fk_project);
  140. // Check parameters
  141. if (! $newcapital > 0 || empty($this->datestart) || empty($this->dateend))
  142. {
  143. $this->error="ErrorBadParameter";
  144. return -2;
  145. }
  146. if (($conf->accounting->enabled) && empty($this->account_capital))
  147. {
  148. $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
  149. return -2;
  150. }
  151. if (($conf->accounting->enabled) && empty($this->account_insurance))
  152. {
  153. $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
  154. return -2;
  155. }
  156. if (($conf->accounting->enabled) && empty($this->account_interest))
  157. {
  158. $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
  159. return -2;
  160. }
  161. $this->db->begin();
  162. $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
  163. $sql.= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
  164. $sql.= " datec, fk_projet, fk_user_author)";
  165. $sql.= " VALUES ('".$this->db->escape($this->label)."',";
  166. $sql.= " '".$this->db->escape($this->fk_bank)."',";
  167. $sql.= " '".price2num($newcapital)."',";
  168. $sql.= " '".$this->db->idate($this->datestart)."',";
  169. $sql.= " '".$this->db->idate($this->dateend)."',";
  170. $sql.= " '".$this->db->escape($this->nbterm)."',";
  171. $sql.= " '".$this->db->escape($this->rate)."',";
  172. $sql.= " '".$this->db->escape($this->note_private)."',";
  173. $sql.= " '".$this->db->escape($this->note_public)."',";
  174. $sql.= " '".$this->db->escape($this->account_capital)."',";
  175. $sql.= " '".$this->db->escape($this->account_insurance)."',";
  176. $sql.= " '".$this->db->escape($this->account_interest)."',";
  177. $sql.= " ".$conf->entity.",";
  178. $sql.= " '".$this->db->idate($now)."',";
  179. $sql.= " ".(empty($this->fk_project)?'NULL':$this->fk_project).",";
  180. $sql.= " ".$user->id;
  181. $sql.= ")";
  182. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  183. $resql=$this->db->query($sql);
  184. if ($resql)
  185. {
  186. $this->id=$this->db->last_insert_id(MAIN_DB_PREFIX."loan");
  187. //dol_syslog("Loans::create this->id=".$this->id);
  188. $this->db->commit();
  189. return $this->id;
  190. }
  191. else
  192. {
  193. $this->error=$this->db->error();
  194. $this->db->rollback();
  195. return -1;
  196. }
  197. }
  198. /**
  199. * Delete a loan
  200. *
  201. * @param User $user Object user making delete
  202. * @return int <0 if KO, >0 if OK
  203. */
  204. function delete($user)
  205. {
  206. $error=0;
  207. $this->db->begin();
  208. // Get bank transaction lines for this loan
  209. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  210. $account=new Account($this->db);
  211. $lines_url=$account->get_url('',$this->id,'loan');
  212. // Delete bank urls
  213. foreach ($lines_url as $line_url)
  214. {
  215. if (! $error)
  216. {
  217. $accountline=new AccountLine($this->db);
  218. $accountline->fetch($line_url['fk_bank']);
  219. $result=$accountline->delete_urls($user);
  220. if ($result < 0)
  221. {
  222. $error++;
  223. }
  224. }
  225. }
  226. // Delete payments
  227. if (! $error)
  228. {
  229. $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".$this->id;
  230. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  231. $resql=$this->db->query($sql);
  232. if (! $resql)
  233. {
  234. $error++;
  235. $this->error=$this->db->lasterror();
  236. }
  237. }
  238. if (! $error)
  239. {
  240. $sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".$this->id;
  241. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  242. $resql=$this->db->query($sql);
  243. if (! $resql)
  244. {
  245. $error++;
  246. $this->error=$this->db->lasterror();
  247. }
  248. }
  249. if (! $error)
  250. {
  251. $this->db->commit();
  252. return 1;
  253. }
  254. else
  255. {
  256. $this->db->rollback();
  257. return -1;
  258. }
  259. }
  260. /**
  261. * Update loan
  262. *
  263. * @param User $user User who modified
  264. * @return int <0 if error, >0 if ok
  265. */
  266. function update($user)
  267. {
  268. $this->db->begin();
  269. if (! is_numeric($this->nbterm))
  270. {
  271. $this->error='BadValueForParameterForNbTerm';
  272. return -1;
  273. }
  274. $sql = "UPDATE ".MAIN_DB_PREFIX."loan";
  275. $sql.= " SET label='".$this->db->escape($this->label)."',";
  276. $sql.= " capital='".price2num($this->db->escape($this->capital))."',";
  277. $sql.= " datestart='".$this->db->idate($this->datestart)."',";
  278. $sql.= " dateend='".$this->db->idate($this->dateend)."',";
  279. $sql.= " nbterm=".$this->nbterm.",";
  280. $sql.= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
  281. $sql.= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
  282. $sql.= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
  283. $sql.= " fk_projet=".(empty($this->fk_project)?'NULL':$this->fk_project).",";
  284. $sql.= " fk_user_modif = ".$user->id;
  285. $sql.= " WHERE rowid=".$this->id;
  286. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  287. $resql=$this->db->query($sql);
  288. if ($resql)
  289. {
  290. $this->db->commit();
  291. return 1;
  292. }
  293. else
  294. {
  295. $this->error=$this->db->error();
  296. $this->db->rollback();
  297. return -1;
  298. }
  299. }
  300. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  301. /**
  302. * Tag loan as payed completely
  303. *
  304. * @param User $user Object user making change
  305. * @return int <0 if KO, >0 if OK
  306. */
  307. function set_paid($user)
  308. {
  309. // phpcs:enable
  310. $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
  311. $sql.= " paid = 1";
  312. $sql.= " WHERE rowid = ".$this->id;
  313. $return = $this->db->query($sql);
  314. if ($return) {
  315. return 1;
  316. } else {
  317. $this->error=$this->db->lasterror();
  318. return -1;
  319. }
  320. }
  321. /**
  322. * Return label of loan status (unpaid, paid)
  323. *
  324. * @param int $mode 0=label, 1=short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label
  325. * @param integer $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  326. * @return string Label
  327. */
  328. function getLibStatut($mode=0,$alreadypaid=-1)
  329. {
  330. return $this->LibStatut($this->paid,$mode,$alreadypaid);
  331. }
  332. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
  333. /**
  334. * Return label for given status
  335. *
  336. * @param int $statut Id statut
  337. * @param int $mode 0=Label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label, 5=Short label + Picto
  338. * @param integer $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount payed if you have it, 1 otherwise)
  339. * @return string Label
  340. */
  341. function LibStatut($statut,$mode=0,$alreadypaid=-1)
  342. {
  343. // phpcs:enable
  344. global $langs;
  345. $langs->loadLangs(array("customers","bills"));
  346. if ($mode == 0 || $mode == 1)
  347. {
  348. if ($statut == 0) return $langs->trans("Unpaid");
  349. elseif ($statut == 1) return $langs->trans("Paid");
  350. }
  351. elseif ($mode == 2)
  352. {
  353. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  354. elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
  355. elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  356. }
  357. elseif ($mode == 3)
  358. {
  359. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1');
  360. elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3');
  361. elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6');
  362. }
  363. elseif ($mode == 4)
  364. {
  365. if ($statut == 0 && $alreadypaid <= 0) return img_picto($langs->trans("Unpaid"), 'statut1').' '.$langs->trans("Unpaid");
  366. elseif ($statut == 0 && $alreadypaid > 0) return img_picto($langs->trans("BillStatusStarted"), 'statut3').' '.$langs->trans("BillStatusStarted");
  367. elseif ($statut == 1) return img_picto($langs->trans("Paid"), 'statut6').' '.$langs->trans("Paid");
  368. }
  369. elseif ($mode == 5)
  370. {
  371. if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
  372. elseif ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
  373. elseif ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
  374. }
  375. elseif ($mode == 6)
  376. {
  377. if ($statut == 0 && $alreadypaid <= 0) return $langs->trans("Unpaid").' '.img_picto($langs->trans("Unpaid"), 'statut1');
  378. elseif ($statut == 0 && $alreadypaid > 0) return $langs->trans("BillStatusStarted").' '.img_picto($langs->trans("BillStatusStarted"), 'statut3');
  379. elseif ($statut == 1) return $langs->trans("Paid").' '.img_picto($langs->trans("Paid"), 'statut6');
  380. }
  381. else return "Error, mode/status not found";
  382. }
  383. /**
  384. * Return clicable name (with eventually the picto)
  385. *
  386. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  387. * @param int $maxlen Label max length
  388. * @return string Chaine with URL
  389. */
  390. function getNomUrl($withpicto=0,$maxlen=0)
  391. {
  392. global $langs;
  393. $result='';
  394. $tooltip = '<u>' . $langs->trans("ShowLoan") . '</u>';
  395. if (! empty($this->ref))
  396. $tooltip .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
  397. if (! empty($this->label))
  398. $tooltip .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label;
  399. $linkstart = '<a href="'.DOL_URL_ROOT.'/loan/card.php?id='.$this->id.'" title="'.str_replace('\n', '', dol_escape_htmltag($tooltip, 1)).'" class="classfortooltip">';
  400. $linkend = '</a>';
  401. $result .= $linkstart;
  402. 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);
  403. if ($withpicto != 2) $result.= ($maxlen?dol_trunc($this->ref,$maxlen):$this->ref);
  404. $result .= $linkend;
  405. return $result;
  406. }
  407. /**
  408. * Return amount of payments already done
  409. *
  410. * @return int Amount of payment already done, <0 if KO
  411. */
  412. function getSumPayment()
  413. {
  414. $table='payment_loan';
  415. $field='fk_loan';
  416. $sql = 'SELECT sum(amount) as amount';
  417. $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
  418. $sql.= ' WHERE '.$field.' = '.$this->id;
  419. dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
  420. $resql=$this->db->query($sql);
  421. if ($resql)
  422. {
  423. $amount=0;
  424. $obj = $this->db->fetch_object($resql);
  425. if ($obj) $amount=$obj->amount?$obj->amount:0;
  426. $this->db->free($resql);
  427. return $amount;
  428. }
  429. else
  430. {
  431. $this->error=$this->db->lasterror();
  432. return -1;
  433. }
  434. }
  435. /**
  436. * Information on record
  437. *
  438. * @param int $id Id of record
  439. * @return integer|null
  440. */
  441. function info($id)
  442. {
  443. $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
  444. $sql.= ' l.tms';
  445. $sql.= ' FROM '.MAIN_DB_PREFIX.'loan as l';
  446. $sql.= ' WHERE l.rowid = '.$id;
  447. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  448. $result = $this->db->query($sql);
  449. if ($result)
  450. {
  451. if ($this->db->num_rows($result))
  452. {
  453. $obj = $this->db->fetch_object($result);
  454. $this->id = $obj->rowid;
  455. if ($obj->fk_user_author)
  456. {
  457. $cuser = new User($this->db);
  458. $cuser->fetch($obj->fk_user_author);
  459. $this->user_creation = $cuser;
  460. }
  461. if ($obj->fk_user_modif)
  462. {
  463. $muser = new User($this->db);
  464. $muser->fetch($obj->fk_user_modif);
  465. $this->user_modification = $muser;
  466. }
  467. $this->date_creation = $this->db->jdate($obj->datec);
  468. if (empty($obj->fk_user_modif)) $obj->tms = "";
  469. $this->date_modification = $this->db->jdate($obj->tms);
  470. $this->db->free($result);
  471. return 1;
  472. }
  473. else
  474. {
  475. $this->db->free($result);
  476. return 0;
  477. }
  478. }
  479. else
  480. {
  481. $this->error=$this->db->lasterror();
  482. return -1;
  483. }
  484. }
  485. }