loan.class.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. <?php
  2. /* Copyright (C) 2014-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
  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 <https://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. /**
  39. * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
  40. */
  41. public $picto = 'money-bill-alt';
  42. /**
  43. * @var int ID
  44. */
  45. public $rowid;
  46. public $datestart;
  47. public $dateend;
  48. /**
  49. * @var string Loan label
  50. */
  51. public $label;
  52. public $capital;
  53. public $nbterm;
  54. public $rate;
  55. public $paid;
  56. public $account_capital;
  57. public $account_insurance;
  58. public $account_interest;
  59. /**
  60. * @var integer|string date_creation
  61. */
  62. public $date_creation;
  63. /**
  64. * @var integer|string date_modification
  65. */
  66. public $date_modification;
  67. /**
  68. * @var integer|string date_validation
  69. */
  70. public $date_validation;
  71. public $insurance_amount;
  72. /**
  73. * @var int Bank ID
  74. */
  75. public $fk_bank;
  76. /**
  77. * @var int ID
  78. */
  79. public $fk_user_creat;
  80. /**
  81. * @var int ID
  82. */
  83. public $fk_user_modif;
  84. /**
  85. * @var int ID
  86. */
  87. public $fk_project;
  88. /**
  89. * @var int totalpaid
  90. */
  91. public $totalpaid;
  92. const STATUS_UNPAID = 0;
  93. const STATUS_PAID = 1;
  94. const STATUS_STARTED = 2;
  95. /**
  96. * Constructor
  97. *
  98. * @param DoliDB $db Database handler
  99. */
  100. public function __construct($db)
  101. {
  102. $this->db = $db;
  103. }
  104. /**
  105. * Load object in memory from database
  106. *
  107. * @param int $id id object
  108. * @return int <0 error , >=0 no error
  109. */
  110. public function fetch($id)
  111. {
  112. $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,";
  113. $sql .= " l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
  114. $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
  115. $sql .= " WHERE l.rowid = ".((int) $id);
  116. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  117. $resql = $this->db->query($sql);
  118. if ($resql) {
  119. if ($this->db->num_rows($resql)) {
  120. $obj = $this->db->fetch_object($resql);
  121. $this->id = $obj->rowid;
  122. $this->ref = $obj->rowid;
  123. $this->datestart = $this->db->jdate($obj->datestart);
  124. $this->dateend = $this->db->jdate($obj->dateend);
  125. $this->label = $obj->label;
  126. $this->capital = $obj->capital;
  127. $this->nbterm = $obj->nbterm;
  128. $this->rate = $obj->rate;
  129. $this->note_private = $obj->note_private;
  130. $this->note_public = $obj->note_public;
  131. $this->insurance_amount = $obj->insurance_amount;
  132. $this->paid = $obj->paid;
  133. $this->fk_bank = $obj->fk_bank;
  134. $this->account_capital = $obj->accountancy_account_capital;
  135. $this->account_insurance = $obj->accountancy_account_insurance;
  136. $this->account_interest = $obj->accountancy_account_interest;
  137. $this->fk_project = $obj->fk_project;
  138. $this->db->free($resql);
  139. return 1;
  140. } else {
  141. $this->db->free($resql);
  142. return 0;
  143. }
  144. } else {
  145. $this->error = $this->db->lasterror();
  146. return -1;
  147. }
  148. }
  149. /**
  150. * Create a loan into database
  151. *
  152. * @param User $user User making creation
  153. * @return int <0 if KO, id if OK
  154. */
  155. public function create($user)
  156. {
  157. global $conf, $langs;
  158. $error = 0;
  159. $now = dol_now();
  160. // clean parameters
  161. $newcapital = price2num($this->capital, 'MT');
  162. if (empty($this->insurance_amount)) {
  163. $this->insurance_amount = 0;
  164. }
  165. $newinsuranceamount = price2num($this->insurance_amount, 'MT');
  166. if (isset($this->note_private)) {
  167. $this->note_private = trim($this->note_private);
  168. }
  169. if (isset($this->note_public)) {
  170. $this->note_public = trim($this->note_public);
  171. }
  172. if (isset($this->account_capital)) {
  173. $this->account_capital = trim($this->account_capital);
  174. }
  175. if (isset($this->account_insurance)) {
  176. $this->account_insurance = trim($this->account_insurance);
  177. }
  178. if (isset($this->account_interest)) {
  179. $this->account_interest = trim($this->account_interest);
  180. }
  181. if (isset($this->fk_bank)) {
  182. $this->fk_bank = (int) $this->fk_bank;
  183. }
  184. if (isset($this->fk_user_creat)) {
  185. $this->fk_user_creat = (int) $this->fk_user_creat;
  186. }
  187. if (isset($this->fk_user_modif)) {
  188. $this->fk_user_modif = (int) $this->fk_user_modif;
  189. }
  190. if (isset($this->fk_project)) {
  191. $this->fk_project = (int) $this->fk_project;
  192. }
  193. // Check parameters
  194. if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
  195. $this->error = "ErrorBadParameter";
  196. return -2;
  197. }
  198. if (isModEnabled('accounting') && empty($this->account_capital)) {
  199. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
  200. return -2;
  201. }
  202. if (isModEnabled('accounting') && empty($this->account_insurance)) {
  203. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
  204. return -2;
  205. }
  206. if (isModEnabled('accounting') && empty($this->account_interest)) {
  207. $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
  208. return -2;
  209. }
  210. $this->db->begin();
  211. $sql = "INSERT INTO ".MAIN_DB_PREFIX."loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
  212. $sql .= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
  213. $sql .= " datec, fk_projet, fk_user_author, insurance_amount)";
  214. $sql .= " VALUES ('".$this->db->escape($this->label)."',";
  215. $sql .= " '".$this->db->escape($this->fk_bank)."',";
  216. $sql .= " '".price2num($newcapital)."',";
  217. $sql .= " '".$this->db->idate($this->datestart)."',";
  218. $sql .= " '".$this->db->idate($this->dateend)."',";
  219. $sql .= " '".$this->db->escape($this->nbterm)."',";
  220. $sql .= " '".$this->db->escape($this->rate)."',";
  221. $sql .= " '".$this->db->escape($this->note_private)."',";
  222. $sql .= " '".$this->db->escape($this->note_public)."',";
  223. $sql .= " '".$this->db->escape($this->account_capital)."',";
  224. $sql .= " '".$this->db->escape($this->account_insurance)."',";
  225. $sql .= " '".$this->db->escape($this->account_interest)."',";
  226. $sql .= " ".$conf->entity.",";
  227. $sql .= " '".$this->db->idate($now)."',";
  228. $sql .= " ".(empty($this->fk_project) ? 'NULL' : $this->fk_project).",";
  229. $sql .= " ".$user->id.",";
  230. $sql .= " '".price2num($newinsuranceamount)."'";
  231. $sql .= ")";
  232. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  233. $resql = $this->db->query($sql);
  234. if ($resql) {
  235. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."loan");
  236. //dol_syslog("Loans::create this->id=".$this->id);
  237. $this->db->commit();
  238. return $this->id;
  239. } else {
  240. $this->error = $this->db->error();
  241. $this->db->rollback();
  242. return -1;
  243. }
  244. }
  245. /**
  246. * Delete a loan
  247. *
  248. * @param User $user Object user making delete
  249. * @return int <0 if KO, >0 if OK
  250. */
  251. public function delete($user)
  252. {
  253. $error = 0;
  254. $this->db->begin();
  255. // Get bank transaction lines for this loan
  256. include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  257. $account = new Account($this->db);
  258. $lines_url = $account->get_url('', $this->id, 'loan');
  259. // Delete bank urls
  260. foreach ($lines_url as $line_url) {
  261. if (!$error) {
  262. $accountline = new AccountLine($this->db);
  263. $accountline->fetch($line_url['fk_bank']);
  264. $result = $accountline->delete_urls($user);
  265. if ($result < 0) {
  266. $error++;
  267. }
  268. }
  269. }
  270. // Delete payments
  271. if (!$error) {
  272. $sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_loan where fk_loan=".((int) $this->id);
  273. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  274. $resql = $this->db->query($sql);
  275. if (!$resql) {
  276. $error++;
  277. $this->error = $this->db->lasterror();
  278. }
  279. }
  280. if (!$error) {
  281. $sql = "DELETE FROM ".MAIN_DB_PREFIX."loan where rowid=".((int) $this->id);
  282. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  283. $resql = $this->db->query($sql);
  284. if (!$resql) {
  285. $error++;
  286. $this->error = $this->db->lasterror();
  287. }
  288. }
  289. if (!$error) {
  290. $this->db->commit();
  291. return 1;
  292. } else {
  293. $this->db->rollback();
  294. return -1;
  295. }
  296. }
  297. /**
  298. * Update loan
  299. *
  300. * @param User $user User who modified
  301. * @return int <0 if error, >0 if ok
  302. */
  303. public function update($user)
  304. {
  305. $this->db->begin();
  306. if (!is_numeric($this->nbterm)) {
  307. $this->error = 'BadValueForParameterForNbTerm';
  308. return -1;
  309. }
  310. $sql = "UPDATE ".MAIN_DB_PREFIX."loan";
  311. $sql .= " SET label='".$this->db->escape($this->label)."',";
  312. $sql .= " capital='".price2num($this->db->escape($this->capital))."',";
  313. $sql .= " datestart='".$this->db->idate($this->datestart)."',";
  314. $sql .= " dateend='".$this->db->idate($this->dateend)."',";
  315. $sql .= " nbterm=".((float) $this->nbterm).",";
  316. $sql .= " rate=".((float) $this->rate).",";
  317. $sql .= " accountancy_account_capital = '".$this->db->escape($this->account_capital)."',";
  318. $sql .= " accountancy_account_insurance = '".$this->db->escape($this->account_insurance)."',";
  319. $sql .= " accountancy_account_interest = '".$this->db->escape($this->account_interest)."',";
  320. $sql .= " fk_projet=".(empty($this->fk_project) ? 'NULL' : ((int) $this->fk_project)).",";
  321. $sql .= " fk_user_modif = ".$user->id.",";
  322. $sql .= " insurance_amount = '".price2num($this->db->escape($this->insurance_amount))."'";
  323. $sql .= " WHERE rowid=".((int) $this->id);
  324. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  325. $resql = $this->db->query($sql);
  326. if ($resql) {
  327. $this->db->commit();
  328. return 1;
  329. } else {
  330. $this->error = $this->db->error();
  331. $this->db->rollback();
  332. return -1;
  333. }
  334. }
  335. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  336. /**
  337. * Tag loan as paid completely
  338. *
  339. * @deprecated
  340. * @see setPaid()
  341. * @param User $user Object user making change
  342. * @return int <0 if KO, >0 if OK
  343. */
  344. public function set_paid($user)
  345. {
  346. // phpcs:enable
  347. dol_syslog(get_class($this)."::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
  348. return $this->setPaid($user);
  349. }
  350. /**
  351. * Tag loan as paid completely
  352. *
  353. * @param User $user Object user making change
  354. * @return int <0 if KO, >0 if OK
  355. */
  356. public function setPaid($user)
  357. {
  358. $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
  359. $sql .= " paid = ".$this::STATUS_PAID;
  360. $sql .= " WHERE rowid = ".((int) $this->id);
  361. $return = $this->db->query($sql);
  362. if ($return) {
  363. return 1;
  364. } else {
  365. $this->error = $this->db->lasterror();
  366. return -1;
  367. }
  368. }
  369. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  370. /**
  371. * Tag loan as payment started
  372. *
  373. * @deprecated
  374. * @see setStarted()
  375. * @param User $user Object user making change
  376. * @return int <0 if KO, >0 if OK
  377. */
  378. public function set_started($user)
  379. {
  380. // phpcs:enable
  381. dol_syslog(get_class($this)."::set_started is deprecated, use setStarted instead", LOG_NOTICE);
  382. return $this->setStarted($user);
  383. }
  384. /**
  385. * Tag loan as payment started
  386. *
  387. * @param User $user Object user making change
  388. * @return int <0 if KO, >0 if OK
  389. */
  390. public function setStarted($user)
  391. {
  392. $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
  393. $sql .= " paid = ".$this::STATUS_STARTED;
  394. $sql .= " WHERE rowid = ".((int) $this->id);
  395. $return = $this->db->query($sql);
  396. if ($return) {
  397. return 1;
  398. } else {
  399. $this->error = $this->db->lasterror();
  400. return -1;
  401. }
  402. }
  403. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  404. /**
  405. * Tag loan as payment as unpaid
  406. * @deprecated
  407. * @see setUnpaid()
  408. * @param User $user Object user making change
  409. * @return int <0 if KO, >0 if OK
  410. */
  411. public function set_unpaid($user)
  412. {
  413. // phpcs:enable
  414. dol_syslog(get_class($this)."::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
  415. return $this->setUnpaid($user);
  416. }
  417. /**
  418. * Tag loan as payment as unpaid
  419. *
  420. * @param User $user Object user making change
  421. * @return int <0 if KO, >0 if OK
  422. */
  423. public function setUnpaid($user)
  424. {
  425. $sql = "UPDATE ".MAIN_DB_PREFIX."loan SET";
  426. $sql .= " paid = ".$this::STATUS_UNPAID;
  427. $sql .= " WHERE rowid = ".((int) $this->id);
  428. $return = $this->db->query($sql);
  429. if ($return) {
  430. return 1;
  431. } else {
  432. $this->error = $this->db->lasterror();
  433. return -1;
  434. }
  435. }
  436. /**
  437. * Return label of loan status (unpaid, paid)
  438. *
  439. * @param int $mode 0=label, 1=short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label
  440. * @param integer $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount paid if you have it, 1 otherwise)
  441. * @return string Label
  442. */
  443. public function getLibStatut($mode = 0, $alreadypaid = -1)
  444. {
  445. return $this->LibStatut($this->paid, $mode, $alreadypaid);
  446. }
  447. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  448. /**
  449. * Return label for given status
  450. *
  451. * @param int $status Id status
  452. * @param int $mode 0=Label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label, 5=Short label + Picto
  453. * @param integer $alreadypaid 0=No payment already done, >0=Some payments were already done (we recommand to put here amount paid if you have it, 1 otherwise)
  454. * @return string Label
  455. */
  456. public function LibStatut($status, $mode = 0, $alreadypaid = -1)
  457. {
  458. // phpcs:enable
  459. global $langs;
  460. // Load translation files required by the page
  461. $langs->loadLangs(array("customers", "bills"));
  462. unset($this->labelStatus); // Force to reset the array of status label, because label can change depending on parameters
  463. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  464. global $langs;
  465. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
  466. $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
  467. $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
  468. if ($status == 0 && $alreadypaid > 0) {
  469. $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  470. }
  471. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
  472. $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Enabled');
  473. $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
  474. if ($status == 0 && $alreadypaid > 0) {
  475. $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
  476. }
  477. }
  478. $statusType = 'status1';
  479. if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
  480. $statusType = 'status3';
  481. }
  482. if ($status == 1) {
  483. $statusType = 'status6';
  484. }
  485. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  486. }
  487. /**
  488. * Return clicable name (with eventually the picto)
  489. *
  490. * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
  491. * @param int $maxlen Label max length
  492. * @param string $option On what the link point to ('nolink', ...)
  493. * @param int $notooltip 1=Disable tooltip
  494. * @param string $morecss Add more css on link
  495. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  496. * @return string Chaine with URL
  497. */
  498. public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  499. {
  500. global $conf, $langs, $hookmanager;
  501. $result = '';
  502. $label = '<u>'.$langs->trans("ShowLoan").'</u>';
  503. if (!empty($this->ref)) {
  504. $label .= '<br><strong>'.$langs->trans('Ref').':</strong> '.$this->ref;
  505. }
  506. if (!empty($this->label)) {
  507. $label .= '<br><strong>'.$langs->trans('Label').':</strong> '.$this->label;
  508. }
  509. $url = DOL_URL_ROOT.'/loan/card.php?id='.$this->id;
  510. if ($option != 'nolink') {
  511. // Add param to save lastsearch_values or not
  512. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  513. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  514. $add_save_lastsearch_values = 1;
  515. }
  516. if ($add_save_lastsearch_values) {
  517. $url .= '&save_lastsearch_values=1';
  518. }
  519. }
  520. $linkclose = '';
  521. if (empty($notooltip)) {
  522. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  523. $label = $langs->trans("ShowMyObject");
  524. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  525. }
  526. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  527. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  528. } else {
  529. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  530. }
  531. $linkstart = '<a href="'.$url.'"';
  532. $linkstart .= $linkclose.'>';
  533. $linkend = '</a>';
  534. $result .= $linkstart;
  535. if ($withpicto) {
  536. $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);
  537. }
  538. if ($withpicto != 2) {
  539. $result .= ($maxlen ?dol_trunc($this->ref, $maxlen) : $this->ref);
  540. }
  541. $result .= $linkend;
  542. global $action;
  543. $hookmanager->initHooks(array($this->element . 'dao'));
  544. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  545. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  546. if ($reshook > 0) {
  547. $result = $hookmanager->resPrint;
  548. } else {
  549. $result .= $hookmanager->resPrint;
  550. }
  551. return $result;
  552. }
  553. /**
  554. * Initialise an instance with random values.
  555. * Used to build previews or test instances.
  556. * id must be 0 if object instance is a specimen.
  557. *
  558. * @return void
  559. */
  560. public function initAsSpecimen()
  561. {
  562. global $user, $langs, $conf;
  563. $now = dol_now();
  564. // Initialise parameters
  565. $this->id = 0;
  566. $this->fk_bank = 1;
  567. $this->label = 'SPECIMEN';
  568. $this->specimen = 1;
  569. $this->socid = 1;
  570. $this->account_capital = 16;
  571. $this->account_insurance = 616;
  572. $this->account_interest = 518;
  573. $this->datestart = $now;
  574. $this->dateend = $now + (3600 * 24 * 365);
  575. $this->note_public = 'SPECIMEN';
  576. $this->capital = 20000;
  577. $this->nbterm = 48;
  578. $this->rate = 4.3;
  579. }
  580. /**
  581. * Return amount of payments already done
  582. *
  583. * @return int Amount of payment already done, <0 if KO
  584. */
  585. public function getSumPayment()
  586. {
  587. $table = 'payment_loan';
  588. $field = 'fk_loan';
  589. $sql = 'SELECT sum(amount_capital) as amount';
  590. $sql .= ' FROM '.MAIN_DB_PREFIX.$table;
  591. $sql .= " WHERE ".$field." = ".((int) $this->id);
  592. dol_syslog(get_class($this)."::getSumPayment", LOG_DEBUG);
  593. $resql = $this->db->query($sql);
  594. if ($resql) {
  595. $amount = 0;
  596. $obj = $this->db->fetch_object($resql);
  597. if ($obj) {
  598. $amount = $obj->amount ? $obj->amount : 0;
  599. }
  600. $this->db->free($resql);
  601. return $amount;
  602. } else {
  603. $this->error = $this->db->lasterror();
  604. return -1;
  605. }
  606. }
  607. /**
  608. * Information on record
  609. *
  610. * @param int $id Id of record
  611. * @return integer|null
  612. */
  613. public function info($id)
  614. {
  615. $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
  616. $sql .= ' l.tms as datem';
  617. $sql .= ' WHERE l.rowid = '.((int) $id);
  618. dol_syslog(get_class($this).'::info', LOG_DEBUG);
  619. $result = $this->db->query($sql);
  620. if ($result) {
  621. if ($this->db->num_rows($result)) {
  622. $obj = $this->db->fetch_object($result);
  623. $this->id = $obj->rowid;
  624. $this->user_creation_id = $obj->fk_user_author;
  625. $this->user_modification_id = $obj->fk_user_modif;
  626. $this->date_creation = $this->db->jdate($obj->datec);
  627. $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
  628. $this->db->free($result);
  629. return 1;
  630. } else {
  631. $this->db->free($result);
  632. return 0;
  633. }
  634. } else {
  635. $this->error = $this->db->lasterror();
  636. return -1;
  637. }
  638. }
  639. }