loan.lib.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /* Copyright (C) 2014-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
  3. * Copyright (C) 2015-2020 Frederic France <frederic.france@netlogic.fr>
  4. * Copyright (C) 2020 Maxime DEMAREST <maxime@indelog.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/lib/loan.lib.php
  21. * \ingroup loan
  22. * \brief Library for loan module
  23. */
  24. /**
  25. * Prepare array with list of tabs
  26. *
  27. * @param Object $object Object related to tabs
  28. * @return array Array of tabs to show
  29. */
  30. function loan_prepare_head($object)
  31. {
  32. global $db, $langs, $conf;
  33. $tab = 0;
  34. $head = array();
  35. $head[$tab][0] = DOL_URL_ROOT.'/loan/card.php?id='.$object->id;
  36. $head[$tab][1] = $langs->trans('Card');
  37. $head[$tab][2] = 'card';
  38. $tab++;
  39. $head[$tab][0] = DOL_URL_ROOT.'/loan/schedule.php?loanid='.$object->id;
  40. $head[$tab][1] = $langs->trans('FinancialCommitment');
  41. $head[$tab][2] = 'FinancialCommitment';
  42. $tab++;
  43. // Show more tabs from modules
  44. // Entries must be declared in modules descriptor with line
  45. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  46. // $this->tabs = array('entity:-tabname); to remove a tab
  47. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'add', 'core');
  48. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  50. $upload_dir = $conf->loan->dir_output."/".dol_sanitizeFileName($object->ref);
  51. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  52. $nbLinks = Link::count($db, $object->element, $object->id);
  53. $head[$tab][0] = DOL_URL_ROOT.'/loan/document.php?id='.$object->id;
  54. $head[$tab][1] = $langs->trans("Documents");
  55. if (($nbFiles + $nbLinks) > 0) {
  56. $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  57. }
  58. $head[$tab][2] = 'documents';
  59. $tab++;
  60. if (empty($conf->global->MAIN_DISABLE_NOTES_TAB)) {
  61. $nbNote = (empty($object->note_private) ? 0 : 1) + (empty($object->note_public) ? 0 : 1);
  62. $head[$tab][0] = DOL_URL_ROOT."/loan/note.php?id=".$object->id;
  63. $head[$tab][1] = $langs->trans("Notes");
  64. if ($nbNote > 0) {
  65. $head[$tab][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
  66. }
  67. $head[$tab][2] = 'note';
  68. $tab++;
  69. }
  70. $head[$tab][0] = DOL_URL_ROOT.'/loan/info.php?id='.$object->id;
  71. $head[$tab][1] = $langs->trans("Info");
  72. $head[$tab][2] = 'info';
  73. $tab++;
  74. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'add', 'external');
  75. complete_head_from_modules($conf, $langs, $object, $head, $tab, 'loan', 'remove');
  76. return $head;
  77. }
  78. /**
  79. * Calculate remaining loan mensuality and interests
  80. *
  81. * @param float $mens Value of this mensuality (interests include, set 0 if we don't paid interests for this mensuality)
  82. * @param float $capital Remaining capital for this mensuality
  83. * @param float $rate Loan rate
  84. * @param int $numactualloadterm Actual loan term
  85. * @param int $nbterm Total number of term for this loan
  86. * @return array Array with remaining capital, interest, and mensuality for each remaining terms
  87. */
  88. function loanCalcMonthlyPayment($mens, $capital, $rate, $numactualloadterm, $nbterm)
  89. {
  90. global $conf, $db;
  91. require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
  92. $object = new LoanSchedule($db);
  93. $output = array();
  94. // Sanitize data in case of
  95. $mens = price2num($mens);
  96. $capital = price2num($capital);
  97. $rate = price2num($rate);
  98. $numactualloadterm = ((int) $numactualloadterm);
  99. $nbterm = ((int) $nbterm);
  100. // If mensuality is 0 we don't pay interests and remaining capital not modified
  101. if ($mens == 0) {
  102. $int = 0;
  103. $cap_rest = $capital;
  104. } else {
  105. $int = ($capital * ($rate / 12));
  106. $int = round($int, 2, PHP_ROUND_HALF_UP);
  107. $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
  108. }
  109. $output[$numactualloadterm] = array('cap_rest'=>$cap_rest, 'cap_rest_str'=>price($cap_rest, 0, '', 1, -1, -1, $conf->currency), 'interet'=>$int, 'interet_str'=>price($int, 0, '', 1, -1, -1, $conf->currency), 'mens'=>$mens);
  110. $numactualloadterm++;
  111. $capital = $cap_rest;
  112. while ($numactualloadterm <= $nbterm) {
  113. $mens = round($object->calcMonthlyPayments($capital, $rate, $nbterm - $numactualloadterm + 1), 2, PHP_ROUND_HALF_UP);
  114. $int = ($capital * ($rate / 12));
  115. $int = round($int, 2, PHP_ROUND_HALF_UP);
  116. $cap_rest = round($capital - ($mens - $int), 2, PHP_ROUND_HALF_UP);
  117. $output[$numactualloadterm] = array(
  118. 'cap_rest' => $cap_rest,
  119. 'cap_rest_str' => price($cap_rest, 0, '', 1, -1, -1, $conf->currency),
  120. 'interet' => $int,
  121. 'interet_str' => price($int, 0, '', 1, -1, -1, $conf->currency),
  122. 'mens' => $mens,
  123. );
  124. $capital = $cap_rest;
  125. $numactualloadterm++;
  126. }
  127. return $output;
  128. }