schedule.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. <?php
  2. /* Copyright (C) 2017 Franck Moreau <franck.moreau@theobald.com>
  3. * Copyright (C) 2018-2023 Alexandre Spangaro <aspangaro@easya.solutions>
  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/loan/schedule.php
  21. * \ingroup loan
  22. * \brief Schedule card
  23. */
  24. // Load Dolibarr environment
  25. require '../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/loan/class/paymentloan.class.php';
  31. $loanid = GETPOST('loanid', 'int');
  32. $action = GETPOST('action', 'aZ09');
  33. // Security check
  34. $socid = 0;
  35. if (GETPOSTISSET('socid')) {
  36. $socid = GETPOST('socid', 'int');
  37. }
  38. if ($user->socid) {
  39. $socid = $user->socid;
  40. }
  41. if (!$user->hasRight('loan', 'calc')) {
  42. accessforbidden();
  43. }
  44. // Load translation files required by the page
  45. $langs->loadLangs(array("compta", "bills", "loan"));
  46. $object = new Loan($db);
  47. $object->fetch($loanid);
  48. $echeances = new LoanSchedule($db);
  49. $echeances->fetchAll($object->id);
  50. if ($object->paid > 0 && count($echeances->lines) == 0) {
  51. $pay_without_schedule = 1;
  52. } else {
  53. $pay_without_schedule = 0;
  54. }
  55. /*
  56. * Actions
  57. */
  58. if ($action == 'createecheancier' && empty($pay_without_schedule)) {
  59. $db->begin();
  60. $i = 1;
  61. while ($i < $object->nbterm + 1) {
  62. $date = GETPOST('hi_date'.$i, 'int');
  63. $mens = price2num(GETPOST('mens'.$i));
  64. $int = price2num(GETPOST('hi_interets'.$i));
  65. $insurance = price2num(GETPOST('hi_insurance'.$i));
  66. $new_echeance = new LoanSchedule($db);
  67. $new_echeance->fk_loan = $object->id;
  68. $new_echeance->datec = dol_now();
  69. $new_echeance->tms = dol_now();
  70. $new_echeance->datep = $date;
  71. $new_echeance->amount_capital = $mens - $int;
  72. $new_echeance->amount_insurance = $insurance;
  73. $new_echeance->amount_interest = $int;
  74. $new_echeance->fk_typepayment = 3;
  75. $new_echeance->fk_bank = 0;
  76. $new_echeance->fk_user_creat = $user->id;
  77. $new_echeance->fk_user_modif = $user->id;
  78. $result = $new_echeance->create($user);
  79. if ($result < 0) {
  80. setEventMessages($new_echeance->error, $echeance->errors, 'errors');
  81. $db->rollback();
  82. unset($echeances->lines);
  83. break;
  84. }
  85. $echeances->lines[] = $new_echeance;
  86. $i++;
  87. }
  88. if ($result > 0) {
  89. $db->commit();
  90. }
  91. }
  92. if ($action == 'updateecheancier' && empty($pay_without_schedule)) {
  93. $db->begin();
  94. $i = 1;
  95. while ($i < $object->nbterm + 1) {
  96. $mens = price2num(GETPOST('mens'.$i));
  97. $int = price2num(GETPOST('hi_interets'.$i));
  98. $id = GETPOST('hi_rowid'.$i);
  99. $insurance = price2num(GETPOST('hi_insurance'.$i));
  100. $new_echeance = new LoanSchedule($db);
  101. $new_echeance->fetch($id);
  102. $new_echeance->tms = dol_now();
  103. $new_echeance->amount_capital = $mens - $int;
  104. $new_echeance->amount_insurance = $insurance;
  105. $new_echeance->amount_interest = $int;
  106. $new_echeance->fk_user_modif = $user->id;
  107. $result = $new_echeance->update($user, 0);
  108. if ($result < 0) {
  109. setEventMessages(null, $new_echeance->errors, 'errors');
  110. $db->rollback();
  111. $echeances->fetchAll($object->id);
  112. break;
  113. }
  114. $echeances->lines[$i - 1] = $new_echeance;
  115. $i++;
  116. }
  117. if ($result > 0) {
  118. $db->commit();
  119. }
  120. }
  121. /*
  122. * View
  123. */
  124. $title = $langs->trans("Loan").' - '.$langs->trans("Card");
  125. $help_url = 'EN:Module_Loan|FR:Module_Emprunt';
  126. llxHeader("", $title, $help_url);
  127. $head = loan_prepare_head($object);
  128. print dol_get_fiche_head($head, 'FinancialCommitment', $langs->trans("Loan"), -1, 'money-bill-alt');
  129. $linkback = '<a href="'.DOL_URL_ROOT.'/loan/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  130. $morehtmlref = '<div class="refidno">';
  131. // Ref loan
  132. $morehtmlref .= $form->editfieldkey("Label", 'label', $object->label, $object, 0, 'string', '', 0, 1);
  133. $morehtmlref .= $form->editfieldval("Label", 'label', $object->label, $object, 0, 'string', '', null, null, '', 1);
  134. // Project
  135. if (isModEnabled('project')) {
  136. $langs->loadLangs(array("projects"));
  137. $morehtmlref .= '<br>'.$langs->trans('Project').' : ';
  138. if ($user->hasRight('loan', 'write')) {
  139. if ($action != 'classify') {
  140. //$morehtmlref .= '<a class="editfielda" href="'.$_SERVER['PHP_SELF'].'?action=classify&token='.newToken().'&id='.$object->id.'">'.img_edit($langs->transnoentitiesnoconv('SetProject')).'</a> : ';
  141. if ($action == 'classify') {
  142. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  143. $morehtmlref .= '<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  144. $morehtmlref .= '<input type="hidden" name="action" value="classin">';
  145. $morehtmlref .= '<input type="hidden" name="token" value="'.newToken().'">';
  146. $morehtmlref .= $formproject->select_projects(-1, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  147. $morehtmlref .= '<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  148. $morehtmlref .= '</form>';
  149. } else {
  150. $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, -1, $object->fk_project, 'none', 0, 0, 0, 1, '', 'maxwidth300');
  151. }
  152. }
  153. } else {
  154. if (!empty($object->fk_project)) {
  155. $proj = new Project($db);
  156. $proj->fetch($object->fk_project);
  157. $morehtmlref .= ' : '.$proj->getNomUrl(1);
  158. if ($proj->title) {
  159. $morehtmlref .= ' - '.$proj->title;
  160. }
  161. } else {
  162. $morehtmlref .= '';
  163. }
  164. }
  165. }
  166. $morehtmlref .= '</div>';
  167. $morehtmlright = '';
  168. dol_banner_tab($object, 'loanid', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlright);
  169. ?>
  170. <script type="text/javascript">
  171. $(document).ready(function() {
  172. $('[name^="mens"]').focusout(function() {
  173. var echeance=$(this).attr('ech');
  174. var mens=price2numjs($(this).val());
  175. var idcap=echeance-1;
  176. idcap = '#hi_capital'+idcap;
  177. var capital=price2numjs($(idcap).val());
  178. console.log("Change montly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
  179. $.ajax({
  180. method: "GET",
  181. dataType: 'json',
  182. url: 'calcmens.php',
  183. data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate / 100; ?>, nbterm: <?php echo $object->nbterm; ?>, token: '<?php echo currentToken(); ?>' },
  184. success: function(data) {
  185. $.each(data, function(index, element) {
  186. var idcap_res='#hi_capital'+index;
  187. var idcap_res_srt='#capital'+index;
  188. var interet_res='#hi_interets'+index;
  189. var interet_res_str='#interets'+index;
  190. var men_res='#mens'+index;
  191. $(idcap_res).val(element.cap_rest);
  192. $(idcap_res_srt).text(element.cap_rest_str);
  193. $(interet_res).val(element.interet);
  194. $(interet_res_str).text(element.interet_str);
  195. $(men_res).val(element.mens);
  196. });
  197. }
  198. });
  199. });
  200. });
  201. </script>
  202. <?php
  203. if ($pay_without_schedule == 1) {
  204. print '<div class="warning">'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'</div>'."\n";
  205. }
  206. print '<form name="createecheancier" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
  207. print '<input type="hidden" name="token" value="'.newToken().'">';
  208. print '<input type="hidden" name="loanid" value="'.$loanid.'">';
  209. if (count($echeances->lines) > 0) {
  210. print '<input type="hidden" name="action" value="updateecheancier">';
  211. } else {
  212. print '<input type="hidden" name="action" value="createecheancier">';
  213. }
  214. //print_fiche_titre($langs->trans("FinancialCommitment"));
  215. print '<br>';
  216. print '<div class="div-table-responsive-no-min">';
  217. print '<table class="border centpercent">';
  218. $colspan = 6;
  219. if (count($echeances->lines) > 0) {
  220. $colspan++;
  221. }
  222. print '<tr class="liste_titre">';
  223. print '<th class="center">'.$langs->trans("Term").'</th>';
  224. print '<th class="center">'.$langs->trans("Date").'</th>';
  225. print '<th class="center">'.$langs->trans("Insurance");
  226. print '<th class="center">'.$langs->trans("InterestAmount").'</th>';
  227. print '<th class="center">'.$langs->trans("Amount").'</th>';
  228. print '<th class="center">'.$langs->trans("CapitalRemain");
  229. print '<br>('.price($object->capital, 0, '', 1, -1, -1, $conf->currency).')';
  230. print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
  231. print '</th>';
  232. if (count($echeances->lines) > 0) {
  233. print '<th class="center">'.$langs->trans('DoPayment').'</th>';
  234. }
  235. print '</tr>'."\n";
  236. if ($object->nbterm > 0 && count($echeances->lines) == 0) {
  237. $i = 1;
  238. $capital = $object->capital;
  239. $insurance = $object->insurance_amount / $object->nbterm;
  240. $insurance = price2num($insurance, 'MT');
  241. $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
  242. while ($i < $object->nbterm + 1) {
  243. $mens = price2num($echeances->calcMonthlyPayments($capital, $object->rate / 100, $object->nbterm - $i + 1), 'MT');
  244. $int = ($capital * ($object->rate / 12)) / 100;
  245. $int = price2num($int, 'MT');
  246. $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
  247. $cap_rest = price2num($capital - ($mens - $int), 'MT');
  248. print '<tr>';
  249. print '<td class="center" id="n'.$i.'">'.$i.'</td>';
  250. print '<td class="center" id ="date'.$i.'"><input type="hidden" name="hi_date'.$i.'" id ="hi_date'.$i.'" value="'.dol_time_plus_duree($object->datestart, $i - 1, 'm').'">'.dol_print_date(dol_time_plus_duree($object->datestart, $i - 1, 'm'), 'day').'</td>';
  251. print '<td class="center amount" id="insurance'.$i.'">'.price($insurance + (($i == 1) ? $regulInsurance : 0), 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_insurance'.$i.'" id ="hi_insurance'.$i.'" value="'.($insurance + (($i == 1) ? $regulInsurance : 0)).'">';
  252. print '<td class="center amount" id="interets'.$i.'">'.price($int, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_interets'.$i.'" id ="hi_interets'.$i.'" value="'.$int.'">';
  253. print '<td class="center"><input class="width75 right" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
  254. print '<td class="center amount" id="capital'.$i.'">'.price($cap_rest).'</td><input type="hidden" name="hi_capital'.$i.'" id ="hi_capital'.$i.'" value="'.$cap_rest.'">';
  255. print '</tr>'."\n";
  256. $i++;
  257. $capital = $cap_rest;
  258. }
  259. } elseif (count($echeances->lines) > 0) {
  260. $i = 1;
  261. $capital = $object->capital;
  262. $insurance = $object->insurance_amount / $object->nbterm;
  263. $insurance = price2num($insurance, 'MT');
  264. $regulInsurance = price2num($object->insurance_amount - ($insurance * $object->nbterm));
  265. $printed = false;
  266. foreach ($echeances->lines as $line) {
  267. $mens = $line->amount_capital + $line->amount_interest;
  268. $int = $line->amount_interest;
  269. $insu = ($insurance + (($i == 1) ? $regulInsurance : 0));
  270. $cap_rest = price2num($capital - ($mens - $int), 'MT');
  271. print '<tr>';
  272. print '<td class="center" id="n'.$i.'"><input type="hidden" name="hi_rowid'.$i.'" id ="hi_rowid'.$i.'" value="'.$line->id.'">'.$i.'</td>';
  273. print '<td class="center" id ="date'.$i.'"><input type="hidden" name="hi_date'.$i.'" id ="hi_date'.$i.'" value="'.$line->datep.'">'.dol_print_date($line->datep, 'day').'</td>';
  274. print '<td class="center amount" id="insurance'.$i.'">'.price($insu, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_insurance'.$i.'" id ="hi_insurance'.$i.'" value="'.$insu.'">';
  275. print '<td class="center amount" id="interets'.$i.'">'.price($int, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_interets'.$i.'" id ="hi_interets'.$i.'" value="'.$int.'">';
  276. if (empty($line->fk_bank)) {
  277. print '<td class="center"><input class="right width75" name="mens'.$i.'" id="mens'.$i.'" value="'.$mens.'" ech="'.$i.'"></td>';
  278. } else {
  279. print '<td class="center amount">'.price($mens, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="mens'.$i.'" id ="mens'.$i.'" value="'.$mens.'">';
  280. }
  281. print '<td class="center amount" id="capital'.$i.'">'.price($cap_rest, 0, '', 1, -1, -1, $conf->currency).'</td><input type="hidden" name="hi_capital'.$i.'" id ="hi_capital'.$i.'" value="'.$cap_rest.'">';
  282. print '<td class="center">';
  283. if (!empty($line->fk_bank)) {
  284. print $langs->trans('Paid');
  285. if (!empty($line->fk_payment_loan)) {
  286. print '&nbsp;<a href="'.DOL_URL_ROOT.'/loan/payment/card.php?id='.$line->fk_payment_loan.'">('.img_object($langs->trans("Payment"), "payment").' '.$line->fk_payment_loan.')</a>';
  287. }
  288. } elseif (!$printed) {
  289. print '<a class="butAction smallpaddingimp" href="'.DOL_URL_ROOT.'/loan/payment/payment.php?id='.$object->id.'&action=create">'.$langs->trans('DoPayment').'</a>';
  290. $printed = true;
  291. }
  292. print '</td>';
  293. print '</tr>'."\n";
  294. $i++;
  295. $capital = $cap_rest;
  296. }
  297. }
  298. print '</table>';
  299. print '</div>';
  300. print '</br>';
  301. if (count($echeances->lines) == 0) {
  302. $label = $langs->trans("Create");
  303. } else {
  304. $label = $langs->trans("Save");
  305. }
  306. print '<div class="center"><input type="submit" class="button button-add" value="'.$label.'" '.(($pay_without_schedule == 1) ? 'disabled title="'.$langs->trans('CantUseScheduleWithLoanStartedToPaid').'"' : '').'title=""></div>';
  307. print '</form>';
  308. // End of page
  309. llxFooter();
  310. $db->close();