schedule.php 13 KB

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