createschedule.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. /* Copyright (C) 2017 Franck Moreau <franck.moreau@theobald.com>
  3. * Copyright (C) 2018 Alexandre Spangaro <aspangaro@zendsi.com>
  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 <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/loan/createschedule.php
  20. * \ingroup loan
  21. * \brief Schedule card
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/loan/class/loan.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/loan.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/loan/class/loanschedule.class.php';
  28. $loanid = GETPOST('loanid', 'int');
  29. $action = GETPOST('action','aZ09');
  30. $object = new Loan($db);
  31. $object->fetch($loanid);
  32. // Load translation files required by the page
  33. $langs->loadLangs(array("loan"));
  34. if ($action == 'createecheancier') {
  35. $i=1;
  36. while($i <$object->nbterm+1){
  37. $date = GETPOST('hi_date'.$i,'int');
  38. $mens = GETPOST('mens'.$i);
  39. $int = GETPOST('hi_interets'.$i);
  40. $echeance = new LoanSchedule($db);
  41. $echeance->fk_loan = $object->id;
  42. $echeance->datec = dol_now();
  43. $echeance->tms = dol_now();
  44. $echeance->datep = $date;
  45. $echeance->amount_capital = $mens-$int;
  46. $echeance->amount_insurance = 0;
  47. $echeance->amount_interest = $int;
  48. $echeance->fk_typepayment = 3;
  49. $echeance->fk_bank = 1;
  50. $echeance->fk_user_creat = $user->id;
  51. $echeance->fk_user_modif = $user->id;
  52. $result=$echeance->create($user);
  53. if ($result<0) {
  54. setEventMessages($echeance->error, $echeance->errors,'errors');
  55. }
  56. $i++;
  57. }
  58. }
  59. if ($action == 'updateecheancier') {
  60. $i=1;
  61. while($i <$object->nbterm+1){
  62. $mens = GETPOST('mens'.$i);
  63. $int = GETPOST('hi_interets'.$i);
  64. $id = GETPOST('hi_rowid'.$i);
  65. $echeance = new LoanSchedule($db);
  66. $echeance->fetch($id);
  67. $echeance->tms = dol_now();
  68. $echeance->amount_capital = $mens-$int;
  69. $echeance->amount_insurance = 0;
  70. $echeance->amount_interest = $int;
  71. $echeance->fk_user_modif = $user->id;
  72. $result= $echeance->update($user,0);
  73. if ($result<0) {
  74. setEventMessages(null, $echeance->errors,'errors');
  75. }
  76. $i++;
  77. }
  78. }
  79. $echeance = new LoanSchedule($db);
  80. $echeance->fetchAll($object->id);
  81. top_htmlhead('', '');
  82. $var = ! $var;
  83. ?>
  84. <script type="text/javascript" language="javascript">
  85. $(document).ready(function() {
  86. $('[name^="mens"]').focusout(function() {
  87. var echeance=$(this).attr('ech');
  88. var mens=$(this).val();
  89. var idcap=echeance-1;
  90. idcap = '#hi_capital'+idcap;
  91. var capital=$(idcap).val();
  92. console.log("Change montly amount echeance="+echeance+" idcap="+idcap+" capital="+capital);
  93. $.ajax({
  94. dataType: 'json',
  95. url: 'calcmens.php',
  96. data: { echeance: echeance, mens: mens, capital:capital, rate:<?php echo $object->rate/100;?> , nbterm : <?php echo $object->nbterm;?>},
  97. success: function(data) {
  98. $.each(data, function(index, element) {
  99. var idcap_res='#hi_capital'+index;
  100. var idcap_res_srt='#capital'+index;
  101. var interet_res='#hi_interets'+index;
  102. var interet_res_str='#interets'+index;
  103. var men_res='#mens'+index;
  104. $(idcap_res).val(element.cap_rest);
  105. $(idcap_res_srt).text(element.cap_rest_str+' €');
  106. $(interet_res).val(element.interet);
  107. $(interet_res_str).text(element.interet_str+' €');
  108. $(men_res).val(element.mens);
  109. });
  110. }
  111. });
  112. });
  113. });
  114. </script>
  115. <?php
  116. print '<form name="createecheancier" action="' . $_SERVER["PHP_SELF"] . '" method="POST">';
  117. print '<input type="hidden" name="token" value="' . $_SESSION['newtoken'] . '">';
  118. print '<input type="hidden" name="loanid" value="' . $loanid . '">';
  119. if(count($echeance->lines)>0)
  120. {
  121. print '<input type="hidden" name="action" value="updateecheancier">';
  122. }else{
  123. print '<input type="hidden" name="action" value="createecheancier">';
  124. }
  125. print '<table class="border" width="100%">';
  126. print '<tr class="liste_titre">';
  127. print '<th align="center" colspan="5">';
  128. print $langs->trans("FinancialCommitment");
  129. print '</th>';
  130. print '</tr>';
  131. print '<tr class="liste_titre">';
  132. Print '<th width="10%" align="center">'.$langs->trans("Term").'</th>';
  133. Print '<th width="10%" align="center">'.$langs->trans("Date").'</th>';
  134. Print '<th width="10%" align="center">'.$langs->trans("Amount").'</th>';
  135. Print '<th width="20%" align="center">'.$langs->trans("InterestAmount").'</th>';
  136. Print '<th width="40%" align="center">'.$langs->trans("CapitalRemain");
  137. print ' ('.price2num($object->capital).')';
  138. print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="'.$object->capital.'">';
  139. print '</th>';
  140. print '</tr>'."\n";
  141. if ($object->nbterm > 0 && count($echeance->lines)==0)
  142. {
  143. $i=1;
  144. $capital = $object->capital;
  145. while($i <$object->nbterm+1)
  146. {
  147. $mens = price2num($echeance->calcMonthlyPayments($capital, $object->rate/100, $object->nbterm-$i+1), 'MT');
  148. $int = ($capital*($object->rate/12))/100;
  149. $int = price2num($int, 'MT');
  150. $cap_rest = price2num($capital - ($mens-$int), 'MT');
  151. print '<tr>';
  152. print '<td align="center" id="n'.$i.'">' . $i .'</td>';
  153. print '<td align="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>';
  154. print '<td align="center"><input name="mens'.$i.'" id="mens'.$i.'" size="5" value="'.$mens.'" ech="'.$i.'"> €</td>';
  155. print '<td align="center" id="interets'.$i.'">'.price($int,0,'',1).' €</td><input type="hidden" name="hi_interets' .$i .'" id ="hi_interets' .$i .'" value="' . $int . '">';
  156. print '<td align="center" id="capital'.$i.'">'.price($cap_rest).' €</td><input type="hidden" name="hi_capital' .$i .'" id ="hi_capital' .$i .'" value="' . $cap_rest . '">';
  157. print '</tr>'."\n";
  158. $i++;
  159. $capital = $cap_rest;
  160. }
  161. }
  162. elseif(count($echeance->lines)>0)
  163. {
  164. $i=1;
  165. $capital = $object->capital;
  166. foreach ($echeance->lines as $line){
  167. $mens = $line->amount_capital+$line->amount_insurance+$line->amount_interest;
  168. $int = $line->amount_interest;
  169. $cap_rest = price2num($capital - ($mens-$int), 'MT');
  170. print '<tr>';
  171. print '<td align="center" id="n'.$i.'"><input type="hidden" name="hi_rowid' .$i .'" id ="hi_rowid' .$i .'" value="' . $line->id . '">' . $i .'</td>';
  172. print '<td align="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>';
  173. if($line->datep > dol_now()){
  174. print '<td align="center"><input name="mens'.$i.'" id="mens'.$i.'" size="5" value="'.$mens.'" ech="'.$i.'"> €</td>';
  175. }else{
  176. print '<td align="center">' . price($mens) . ' €</td><input type="hidden" name="mens' .$i .'" id ="mens' .$i .'" value="' . $mens . '">';
  177. }
  178. print '<td align="center" id="interets'.$i.'">'.price($int,0,'',1).' €</td><input type="hidden" name="hi_interets' .$i .'" id ="hi_interets' .$i .'" value="' . $int . '">';
  179. print '<td align="center" id="capital'.$i.'">'.price($cap_rest).' €</td><input type="hidden" name="hi_capital' .$i .'" id ="hi_capital' .$i .'" value="' . $cap_rest . '">';
  180. print '</tr>'."\n";
  181. $i++;
  182. $capital = $cap_rest;
  183. }
  184. }
  185. print '</table>';
  186. print '</br>';
  187. print '</br>';
  188. print '<div align="center"><input class="button" type="submit" value="'.$langs->trans("Save").'"></div>';
  189. print '</form>';
  190. // End of page
  191. llxFooter();
  192. $db->close();