schedule.php 13 KB

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