loanschedule.class.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. <?php
  2. /* Copyright (C) 2017 Florian HENRY <florian.henry@atm-consulting.fr>
  3. * Copyright (C) 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/loanschedule.class.php
  20. * \ingroup loan
  21. * \brief File of class to manage schedule of loans
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
  24. /**
  25. * Class to manage Schedule of loans
  26. */
  27. class LoanSchedule extends CommonObject
  28. {
  29. /**
  30. * @var string ID to identify managed object
  31. */
  32. public $element = 'loan_schedule';
  33. /**
  34. * @var string Name of table without prefix where object is stored
  35. */
  36. public $table_element = 'loan_schedule';
  37. /**
  38. * @var int Loan ID
  39. */
  40. public $fk_loan;
  41. /**
  42. * @var string Create date
  43. */
  44. public $datec;
  45. public $tms;
  46. /**
  47. * @var string Payment date
  48. */
  49. public $datep;
  50. public $amounts = array(); // Array of amounts
  51. public $amount_capital; // Total amount of payment
  52. public $amount_insurance;
  53. public $amount_interest;
  54. /**
  55. * @var int Payment Type ID
  56. */
  57. public $fk_typepayment;
  58. /**
  59. * @var int Payment ID
  60. */
  61. public $num_payment;
  62. /**
  63. * @var int Bank ID
  64. */
  65. public $fk_bank;
  66. /**
  67. * @var int Loan Payment ID
  68. */
  69. public $fk_payment_loan;
  70. /**
  71. * @var int Bank ID
  72. */
  73. public $fk_user_creat;
  74. /**
  75. * @var int User ID
  76. */
  77. public $fk_user_modif;
  78. public $lines = array();
  79. /**
  80. * @deprecated
  81. * @see $amount, $amounts
  82. */
  83. public $total;
  84. public $type_code;
  85. public $type_label;
  86. /**
  87. * Constructor
  88. *
  89. * @param DoliDB $db Database handler
  90. */
  91. public function __construct($db)
  92. {
  93. $this->db = $db;
  94. }
  95. /**
  96. * Create payment of loan into database.
  97. * Use this->amounts to have list of lines for the payment
  98. *
  99. * @param User $user User making payment
  100. * @return int <0 if KO, id of payment if OK
  101. */
  102. public function create($user)
  103. {
  104. global $conf, $langs;
  105. $error = 0;
  106. $now = dol_now();
  107. // Validate parameters
  108. if (!$this->datep) {
  109. $this->error = 'ErrorBadValueForParameter';
  110. return -1;
  111. }
  112. // Clean parameters
  113. if (isset($this->fk_loan)) {
  114. $this->fk_loan = (int) $this->fk_loan;
  115. }
  116. if (isset($this->amount_capital)) {
  117. $this->amount_capital = trim($this->amount_capital ? $this->amount_capital : 0);
  118. }
  119. if (isset($this->amount_insurance)) {
  120. $this->amount_insurance = trim($this->amount_insurance ? $this->amount_insurance : 0);
  121. }
  122. if (isset($this->amount_interest)) {
  123. $this->amount_interest = trim($this->amount_interest ? $this->amount_interest : 0);
  124. }
  125. if (isset($this->fk_typepayment)) {
  126. $this->fk_typepayment = (int) $this->fk_typepayment;
  127. }
  128. if (isset($this->fk_bank)) {
  129. $this->fk_bank = (int) $this->fk_bank;
  130. }
  131. if (isset($this->fk_user_creat)) {
  132. $this->fk_user_creat = (int) $this->fk_user_creat;
  133. }
  134. if (isset($this->fk_user_modif)) {
  135. $this->fk_user_modif = (int) $this->fk_user_modif;
  136. }
  137. $totalamount = $this->amount_capital + $this->amount_insurance + $this->amount_interest;
  138. $totalamount = price2num($totalamount);
  139. // Check parameters
  140. if ($totalamount == 0) {
  141. $this->errors[] = 'step1';
  142. return -1; // Negative amounts are accepted for reject prelevement but not null
  143. }
  144. $this->db->begin();
  145. if ($totalamount != 0) {
  146. $sql = "INSERT INTO ".MAIN_DB_PREFIX.$this->table_element." (fk_loan, datec, datep, amount_capital, amount_insurance, amount_interest,";
  147. $sql .= " fk_typepayment, fk_user_creat, fk_bank)";
  148. $sql .= " VALUES (".$this->fk_loan.", '".$this->db->idate($now)."',";
  149. $sql .= " '".$this->db->idate($this->datep)."',";
  150. $sql .= " ".price2num($this->amount_capital).",";
  151. $sql .= " ".price2num($this->amount_insurance).",";
  152. $sql .= " ".price2num($this->amount_interest).",";
  153. $sql .= " ".price2num($this->fk_typepayment).", ";
  154. $sql .= " ".((int) $user->id).",";
  155. $sql .= " ".((int) $this->fk_bank).")";
  156. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  157. $resql = $this->db->query($sql);
  158. if ($resql) {
  159. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_loan");
  160. } else {
  161. $this->error = $this->db->lasterror();
  162. $error++;
  163. }
  164. }
  165. if ($totalamount != 0 && !$error) {
  166. $this->amount_capital = $totalamount;
  167. $this->db->commit();
  168. return $this->id;
  169. } else {
  170. $this->errors[] = $this->db->lasterror();
  171. $this->db->rollback();
  172. return -1;
  173. }
  174. }
  175. /**
  176. * Load object in memory from database
  177. *
  178. * @param int $id Id object
  179. * @return int <0 if KO, >0 if OK
  180. */
  181. public function fetch($id)
  182. {
  183. global $langs;
  184. $sql = "SELECT";
  185. $sql .= " t.rowid,";
  186. $sql .= " t.fk_loan,";
  187. $sql .= " t.datec,";
  188. $sql .= " t.tms,";
  189. $sql .= " t.datep,";
  190. $sql .= " t.amount_capital,";
  191. $sql .= " t.amount_insurance,";
  192. $sql .= " t.amount_interest,";
  193. $sql .= " t.fk_typepayment,";
  194. $sql .= " t.num_payment,";
  195. $sql .= " t.note_private,";
  196. $sql .= " t.note_public,";
  197. $sql .= " t.fk_bank,";
  198. $sql .= " t.fk_payment_loan,";
  199. $sql .= " t.fk_user_creat,";
  200. $sql .= " t.fk_user_modif,";
  201. $sql .= " pt.code as type_code, pt.libelle as type_label,";
  202. $sql .= ' b.fk_account';
  203. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  204. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
  205. $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
  206. $sql .= " WHERE t.rowid = ".((int) $id);
  207. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  208. $resql = $this->db->query($sql);
  209. if ($resql) {
  210. if ($this->db->num_rows($resql)) {
  211. $obj = $this->db->fetch_object($resql);
  212. $this->id = $obj->rowid;
  213. $this->ref = $obj->rowid;
  214. $this->fk_loan = $obj->fk_loan;
  215. $this->datec = $this->db->jdate($obj->datec);
  216. $this->tms = $this->db->jdate($obj->tms);
  217. $this->datep = $this->db->jdate($obj->datep);
  218. $this->amount_capital = $obj->amount_capital;
  219. $this->amount_insurance = $obj->amount_insurance;
  220. $this->amount_interest = $obj->amount_interest;
  221. $this->fk_typepayment = $obj->fk_typepayment;
  222. $this->num_payment = $obj->num_payment;
  223. $this->note_private = $obj->note_private;
  224. $this->note_public = $obj->note_public;
  225. $this->fk_bank = $obj->fk_bank;
  226. $this->fk_payment_loan = $obj->fk_payment_loan;
  227. $this->fk_user_creat = $obj->fk_user_creat;
  228. $this->fk_user_modif = $obj->fk_user_modif;
  229. $this->type_code = $obj->type_code;
  230. $this->type_label = $obj->type_label;
  231. $this->bank_account = $obj->fk_account;
  232. $this->bank_line = $obj->fk_bank;
  233. }
  234. $this->db->free($resql);
  235. return 1;
  236. } else {
  237. $this->error = "Error ".$this->db->lasterror();
  238. return -1;
  239. }
  240. }
  241. /**
  242. * Update database
  243. *
  244. * @param User $user User that modify
  245. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  246. * @return int <0 if KO, >0 if OK
  247. */
  248. public function update($user = 0, $notrigger = 0)
  249. {
  250. global $conf, $langs;
  251. $error = 0;
  252. // Clean parameters
  253. if (isset($this->amount_capital)) {
  254. $this->amount_capital = trim($this->amount_capital);
  255. }
  256. if (isset($this->amount_insurance)) {
  257. $this->amount_insurance = trim($this->amount_insurance);
  258. }
  259. if (isset($this->amount_interest)) {
  260. $this->amount_interest = trim($this->amount_interest);
  261. }
  262. if (isset($this->num_payment)) {
  263. $this->num_payment = trim($this->num_payment);
  264. }
  265. if (isset($this->note_private)) {
  266. $this->note_private = trim($this->note_private);
  267. }
  268. if (isset($this->note_public)) {
  269. $this->note_public = trim($this->note_public);
  270. }
  271. if (isset($this->fk_bank)) {
  272. $this->fk_bank = trim($this->fk_bank);
  273. }
  274. if (isset($this->fk_payment_loan)) {
  275. $this->fk_payment_loan = (int) $this->fk_payment_loan;
  276. }
  277. // Check parameters
  278. // Put here code to add control on parameters values
  279. // Update request
  280. $sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
  281. $sql .= " fk_loan=".(isset($this->fk_loan) ? $this->fk_loan : "null").",";
  282. $sql .= " datec=".(dol_strlen($this->datec) != 0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  283. $sql .= " tms=".(dol_strlen($this->tms) != 0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  284. $sql .= " datep=".(dol_strlen($this->datep) != 0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
  285. $sql .= " amount_capital=".(isset($this->amount_capital) ? $this->amount_capital : "null").",";
  286. $sql .= " amount_insurance=".(isset($this->amount_insurance) ? $this->amount_insurance : "null").",";
  287. $sql .= " amount_interest=".(isset($this->amount_interest) ? $this->amount_interest : "null").",";
  288. $sql .= " fk_typepayment=".(isset($this->fk_typepayment) ? $this->fk_typepayment : "null").",";
  289. $sql .= " num_payment=".(isset($this->num_payment) ? "'".$this->db->escape($this->num_payment)."'" : "null").",";
  290. $sql .= " note_private=".(isset($this->note_private) ? "'".$this->db->escape($this->note_private)."'" : "null").",";
  291. $sql .= " note_public=".(isset($this->note_public) ? "'".$this->db->escape($this->note_public)."'" : "null").",";
  292. $sql .= " fk_bank=".(isset($this->fk_bank) ? $this->fk_bank : "null").",";
  293. $sql .= " fk_payment_loan=".(isset($this->fk_payment_loan) ? $this->fk_payment_loan : "null").",";
  294. $sql .= " fk_user_creat=".(isset($this->fk_user_creat) ? $this->fk_user_creat : "null").",";
  295. $sql .= " fk_user_modif=".(isset($this->fk_user_modif) ? $this->fk_user_modif : "null")."";
  296. $sql .= " WHERE rowid=".((int) $this->id);
  297. $this->db->begin();
  298. dol_syslog(get_class($this)."::update", LOG_DEBUG);
  299. $resql = $this->db->query($sql);
  300. if (!$resql) {
  301. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  302. }
  303. // Commit or rollback
  304. if ($error) {
  305. $this->db->rollback();
  306. return -1 * $error;
  307. } else {
  308. $this->db->commit();
  309. return 1;
  310. }
  311. }
  312. /**
  313. * Delete object in database
  314. *
  315. * @param User $user User that delete
  316. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  317. * @return int <0 if KO, >0 if OK
  318. */
  319. public function delete($user, $notrigger = 0)
  320. {
  321. global $conf, $langs;
  322. $error = 0;
  323. $this->db->begin();
  324. if (!$error) {
  325. $sql = "DELETE FROM ".MAIN_DB_PREFIX.$this->table_element;
  326. $sql .= " WHERE rowid=".((int) $this->id);
  327. dol_syslog(get_class($this)."::delete", LOG_DEBUG);
  328. $resql = $this->db->query($sql);
  329. if (!$resql) {
  330. $error++; $this->errors[] = "Error ".$this->db->lasterror();
  331. }
  332. }
  333. // Commit or rollback
  334. if ($error) {
  335. foreach ($this->errors as $errmsg) {
  336. dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
  337. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  338. }
  339. $this->db->rollback();
  340. return -1 * $error;
  341. } else {
  342. $this->db->commit();
  343. return 1;
  344. }
  345. }
  346. /**
  347. * Calculate Monthly Payments
  348. *
  349. * @param double $capital Capital
  350. * @param double $rate rate
  351. * @param int $nbterm nb term
  352. * @return double mensuality
  353. */
  354. public function calcMonthlyPayments($capital, $rate, $nbterm)
  355. {
  356. $result = '';
  357. if (!empty($capital) && !empty($rate) && !empty($nbterm)) {
  358. $result = ($capital * ($rate / 12)) / (1 - pow((1 + ($rate / 12)), ($nbterm * -1)));
  359. }
  360. return $result;
  361. }
  362. /**
  363. * Load all object in memory from database
  364. *
  365. * @param int $loanid Id object
  366. * @return int <0 if KO, >0 if OK
  367. */
  368. public function fetchAll($loanid)
  369. {
  370. global $langs;
  371. $sql = "SELECT";
  372. $sql .= " t.rowid,";
  373. $sql .= " t.fk_loan,";
  374. $sql .= " t.datec,";
  375. $sql .= " t.tms,";
  376. $sql .= " t.datep,";
  377. $sql .= " t.amount_capital,";
  378. $sql .= " t.amount_insurance,";
  379. $sql .= " t.amount_interest,";
  380. $sql .= " t.fk_typepayment,";
  381. $sql .= " t.num_payment,";
  382. $sql .= " t.note_private,";
  383. $sql .= " t.note_public,";
  384. $sql .= " t.fk_bank,";
  385. $sql .= " t.fk_payment_loan,";
  386. $sql .= " t.fk_user_creat,";
  387. $sql .= " t.fk_user_modif";
  388. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as t";
  389. $sql .= " WHERE t.fk_loan = ".((int) $loanid);
  390. dol_syslog(get_class($this)."::fetchAll", LOG_DEBUG);
  391. $resql = $this->db->query($sql);
  392. if ($resql) {
  393. while ($obj = $this->db->fetch_object($resql)) {
  394. $line = new LoanSchedule($this->db);
  395. $line->id = $obj->rowid;
  396. $line->ref = $obj->rowid;
  397. $line->fk_loan = $obj->fk_loan;
  398. $line->datec = $this->db->jdate($obj->datec);
  399. $line->tms = $this->db->jdate($obj->tms);
  400. $line->datep = $this->db->jdate($obj->datep);
  401. $line->amount_capital = $obj->amount_capital;
  402. $line->amount_insurance = $obj->amount_insurance;
  403. $line->amount_interest = $obj->amount_interest;
  404. $line->fk_typepayment = $obj->fk_typepayment;
  405. $line->num_payment = $obj->num_payment;
  406. $line->note_private = $obj->note_private;
  407. $line->note_public = $obj->note_public;
  408. $line->fk_bank = $obj->fk_bank;
  409. $line->fk_payment_loan = $obj->fk_payment_loan;
  410. $line->fk_user_creat = $obj->fk_user_creat;
  411. $line->fk_user_modif = $obj->fk_user_modif;
  412. $this->lines[] = $line;
  413. }
  414. $this->db->free($resql);
  415. return 1;
  416. } else {
  417. $this->error = "Error ".$this->db->lasterror();
  418. return -1;
  419. }
  420. }
  421. /**
  422. * transPayment
  423. *
  424. * @return void
  425. */
  426. private function transPayment()
  427. {
  428. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  429. require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
  430. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  431. $toinsert = array();
  432. $sql = "SELECT l.rowid";
  433. $sql .= " FROM ".MAIN_DB_PREFIX."loan as l";
  434. $sql .= " WHERE l.paid = 0";
  435. $resql = $this->db->query($sql);
  436. if ($resql) {
  437. while ($obj = $this->db->fetch_object($resql)) {
  438. $lastrecorded = $this->lastPayment($obj->rowid);
  439. $toinsert = $this->paimenttorecord($obj->rowid, $lastrecorded);
  440. if (count($toinsert) > 0) {
  441. foreach ($toinsert as $echid) {
  442. $this->db->begin();
  443. $sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_loan ";
  444. $sql .= "(fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif) ";
  445. $sql .= "SELECT fk_loan,datec,tms,datep,amount_capital,amount_insurance,amount_interest,fk_typepayment,num_payment,note_private,note_public,fk_bank,fk_user_creat,fk_user_modif";
  446. $sql .= " FROM ".MAIN_DB_PREFIX."loan_schedule WHERE rowid =".((int) $echid);
  447. $res = $this->db->query($sql);
  448. if ($res) {
  449. $this->db->commit();
  450. } else {
  451. $this->db->rollback();
  452. }
  453. }
  454. }
  455. }
  456. }
  457. }
  458. /**
  459. * lastpayment
  460. *
  461. * @param int $loanid Loan id
  462. * @return int < 0 if KO, Date > 0 if OK
  463. */
  464. private function lastPayment($loanid)
  465. {
  466. $sql = "SELECT p.datep";
  467. $sql .= " FROM ".MAIN_DB_PREFIX."payment_loan as p ";
  468. $sql .= " WHERE p.fk_loan = ".((int) $loanid);
  469. $sql .= " ORDER BY p.datep DESC ";
  470. $sql .= " LIMIT 1 ";
  471. $resql = $this->db->query($sql);
  472. if ($resql) {
  473. $obj = $this->db->fetch_object($resql);
  474. return $this->db->jdate($obj->datep);
  475. } else {
  476. return -1;
  477. }
  478. }
  479. /**
  480. * paimenttorecord
  481. *
  482. * @param int $loanid Loan id
  483. * @param int $datemax Date max
  484. * @return array Array of id
  485. */
  486. public function paimenttorecord($loanid, $datemax)
  487. {
  488. $result = array();
  489. $sql = "SELECT p.rowid";
  490. $sql .= " FROM ".MAIN_DB_PREFIX.$this->table_element." as p ";
  491. $sql .= " WHERE p.fk_loan = ".((int) $loanid);
  492. if (!empty($datemax)) {
  493. $sql .= " AND p.datep > '".$this->db->idate($datemax)."'";
  494. }
  495. $sql .= " AND p.datep <= '".$this->db->idate(dol_now())."'";
  496. $resql = $this->db->query($sql);
  497. if ($resql) {
  498. while ($obj = $this->db->fetch_object($resql)) {
  499. $result[] = $obj->rowid;
  500. }
  501. }
  502. return $result;
  503. }
  504. }