2018-10-18 17:16:38 +02:00
< ? php
/* Copyright ( C ) 2017 Franck Moreau < franck . moreau @ theobald . com >
2019-01-28 21:39:22 +01:00
* Copyright ( C ) 2018 Alexandre Spangaro < aspangaro @ open - dsi . fr >
2018-10-18 17:16:38 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2018-10-18 17:16:38 +02:00
*/
/**
* \file htdocs / loan / createschedule . php
* \ingroup loan
* \brief Schedule card
*/
require '../main.inc.php' ;
require_once DOL_DOCUMENT_ROOT . '/loan/class/loan.class.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/loan.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/loan/class/loanschedule.class.php' ;
$loanid = GETPOST ( 'loanid' , 'int' );
2019-01-27 11:55:16 +01:00
$action = GETPOST ( 'action' , 'aZ09' );
2018-10-18 17:16:38 +02:00
$object = new Loan ( $db );
$object -> fetch ( $loanid );
// Load translation files required by the page
2019-11-12 00:15:34 +01:00
$langs -> loadLangs ( array ( " compta " , " bills " , " loan " ));
2018-10-18 17:16:38 +02:00
2019-11-12 00:15:34 +01:00
$title = $langs -> trans ( " Loan " ) . ' - ' . $langs -> trans ( " Card " );
2018-10-18 17:16:38 +02:00
$help_url = 'EN:Module_Loan|FR:Module_Emprunt' ;
2019-01-27 11:55:16 +01:00
llxHeader ( " " , $title , $help_url );
2018-10-18 17:16:38 +02:00
2019-11-12 00:15:34 +01:00
$head = loan_prepare_head ( $object );
2018-10-18 17:16:38 +02:00
dol_fiche_head ( $head , 'FinancialCommitment' , $langs -> trans ( " Loan " ), - 1 , 'bill' );
if ( $action == 'createecheancier' ) {
2019-11-12 00:15:34 +01:00
$i = 1 ;
while ( $i < $object -> nbterm + 1 ) {
$date = GETPOST ( 'hi_date' . $i , 'int' );
2018-10-18 17:16:38 +02:00
$mens = GETPOST ( 'mens' . $i );
$int = GETPOST ( 'hi_interets' . $i );
$insurance = GETPOST ( 'hi_insurance' . $i );
2019-03-03 08:55:27 +01:00
2018-10-18 17:16:38 +02:00
$echeance = new LoanSchedule ( $db );
2019-03-03 08:55:27 +01:00
2018-10-18 17:16:38 +02:00
$echeance -> fk_loan = $object -> id ;
$echeance -> datec = dol_now ();
$echeance -> tms = dol_now ();
$echeance -> datep = $date ;
2019-11-12 00:15:34 +01:00
$echeance -> amount_capital = $mens - $int ;
2018-10-18 17:16:38 +02:00
$echeance -> amount_insurance = $insurance ;
$echeance -> amount_interest = $int ;
$echeance -> fk_typepayment = 3 ;
$echeance -> fk_bank = 0 ;
$echeance -> fk_user_creat = $user -> id ;
$echeance -> fk_user_modif = $user -> id ;
2019-11-12 00:15:34 +01:00
$result = $echeance -> create ( $user );
if ( $result < 0 ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( $echeance -> error , $echeance -> errors , 'errors' );
2018-10-18 17:16:38 +02:00
}
$i ++ ;
}
}
if ( $action == 'updateecheancier' ) {
2019-11-12 00:15:34 +01:00
$i = 1 ;
while ( $i < $object -> nbterm + 1 ) {
2018-10-18 17:16:38 +02:00
$mens = GETPOST ( 'mens' . $i );
$int = GETPOST ( 'hi_interets' . $i );
$id = GETPOST ( 'hi_rowid' . $i );
$insurance = GETPOST ( 'hi_insurance' . $i );
2019-03-03 08:55:27 +01:00
2018-10-18 17:16:38 +02:00
$echeance = new LoanSchedule ( $db );
$echeance -> fetch ( $id );
$echeance -> tms = dol_now ();
2019-11-12 00:15:34 +01:00
$echeance -> amount_capital = $mens - $int ;
2018-10-18 17:16:38 +02:00
$echeance -> amount_insurance = $insurance ;
$echeance -> amount_interest = $int ;
$echeance -> fk_user_modif = $user -> id ;
2019-11-12 00:15:34 +01:00
$result = $echeance -> update ( $user , 0 );
if ( $result < 0 ) {
2019-01-27 11:55:16 +01:00
setEventMessages ( null , $echeance -> errors , 'errors' );
2018-10-18 17:16:38 +02:00
}
$i ++ ;
}
}
$echeance = new LoanSchedule ( $db );
$echeance -> fetchAll ( $object -> id );
2019-11-12 00:15:34 +01:00
$var = ! $var ;
2018-10-18 17:16:38 +02:00
?>
< script type = " text/javascript " language = " javascript " >
$ ( document ) . ready ( function () {
$ ( '[name^="mens"]' ) . focusout ( function () {
var echeance = $ ( this ) . attr ( 'ech' );
var mens = $ ( this ) . val ();
var idcap = echeance - 1 ;
idcap = '#hi_capital' + idcap ;
var capital = $ ( idcap ) . val ();
console . log ( " Change montly amount echeance= " + echeance + " idcap= " + idcap + " capital= " + capital );
$ . ajax ({
dataType : 'json' ,
url : 'calcmens.php' ,
2019-11-12 00:15:34 +01:00
data : { echeance : echeance , mens : mens , capital : capital , rate :< ? php echo $object -> rate / 100 ; ?> , nbterm : <?php echo $object->nbterm; ?>},
2018-10-18 17:16:38 +02:00
success : function ( data ) {
$ . each ( data , function ( index , element ) {
var idcap_res = '#hi_capital' + index ;
var idcap_res_srt = '#capital' + index ;
var interet_res = '#hi_interets' + index ;
var interet_res_str = '#interets' + index ;
var men_res = '#mens' + index ;
$ ( idcap_res ) . val ( element . cap_rest );
$ ( idcap_res_srt ) . text ( element . cap_rest_str + ' €' );
$ ( interet_res ) . val ( element . interet );
$ ( interet_res_str ) . text ( element . interet_str + ' €' );
$ ( men_res ) . val ( element . mens );
});
}
});
});
});
</ script >
< ? php
2019-11-12 00:15:34 +01:00
print '<form name="createecheancier" action="' . $_SERVER [ " PHP_SELF " ] . '" method="POST">' ;
print '<input type="hidden" name="token" value="' . $_SESSION [ 'newtoken' ] . '">' ;
print '<input type="hidden" name="loanid" value="' . $loanid . '">' ;
if ( count ( $echeance -> lines ) > 0 )
2018-10-18 17:16:38 +02:00
{
print '<input type="hidden" name="action" value="updateecheancier">' ;
2019-11-12 00:15:34 +01:00
} else {
2018-10-18 17:16:38 +02:00
print '<input type="hidden" name="action" value="createecheancier">' ;
}
2019-11-05 21:24:41 +01:00
print '<table class="border centpercent">' ;
2018-10-18 17:16:38 +02:00
print '<tr class="liste_titre">' ;
$colspan = 6 ;
2019-11-12 00:15:34 +01:00
if ( count ( $echeance -> lines ) > 0 ) $colspan ++ ;
2019-03-03 08:55:27 +01:00
print '<th class="center" colspan="' . $colspan . '">' ;
2018-10-18 17:16:38 +02:00
print $langs -> trans ( " FinancialCommitment " );
print '</th>' ;
print '</tr>' ;
print '<tr class="liste_titre">' ;
2019-03-09 01:02:05 +01:00
print '<th width="5%" class="center">' . $langs -> trans ( " Term " ) . '</th>' ;
print '<th width="5%" class="center">' . $langs -> trans ( " Date " ) . '</th>' ;
2019-03-03 08:55:27 +01:00
print '<th width="15%" class="center">' . $langs -> trans ( " Insurance " );
2019-03-09 01:02:05 +01:00
print '<th width="15%" class="center">' . $langs -> trans ( " InterestAmount " ) . '</th>' ;
print '<th width="10%" class="center">' . $langs -> trans ( " Amount " ) . '</th>' ;
print '<th width="40%" class="center">' . $langs -> trans ( " CapitalRemain " );
2018-10-18 17:16:38 +02:00
print ' (' . price2num ( $object -> capital ) . ')' ;
print '<input type="hidden" name="hi_capital0" id ="hi_capital0" value="' . $object -> capital . '">' ;
print '</th>' ;
2019-11-12 00:15:34 +01:00
if ( count ( $echeance -> lines ) > 0 ) print '<th>' . $langs -> trans ( 'DoPayment' ) . '</th>' ;
2018-10-18 17:16:38 +02:00
print '</tr>' . " \n " ;
2019-11-12 00:15:34 +01:00
if ( $object -> nbterm > 0 && count ( $echeance -> lines ) == 0 )
2018-10-18 17:16:38 +02:00
{
2019-11-12 00:15:34 +01:00
$i = 1 ;
2018-10-18 17:16:38 +02:00
$capital = $object -> capital ;
2019-11-12 00:15:34 +01:00
$insurance = $object -> insurance_amount / $object -> nbterm ;
2018-10-18 17:16:38 +02:00
$insurance = price2num ( $insurance , 'MT' );
$regulInsurance = price2num ( $object -> insurance_amount - ( $insurance * $object -> nbterm ));
2019-11-12 00:15:34 +01:00
while ( $i < $object -> nbterm + 1 )
2018-10-18 17:16:38 +02:00
{
2019-11-12 00:15:34 +01:00
$mens = price2num ( $echeance -> calcMonthlyPayments ( $capital , $object -> rate / 100 , $object -> nbterm - $i + 1 ), 'MT' );
$int = ( $capital * ( $object -> rate / 12 )) / 100 ;
2018-10-18 17:16:38 +02:00
$int = price2num ( $int , 'MT' );
2019-11-12 00:15:34 +01:00
$insu = ( $insurance + (( $i == 1 ) ? $regulInsurance : 0 ));
$cap_rest = price2num ( $capital - ( $mens - $int ), 'MT' );
2018-10-18 17:16:38 +02:00
print '<tr>' ;
2019-11-12 00:15:34 +01:00
print '<td class="center" id="n' . $i . '">' . $i . '</td>' ;
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>' ;
print '<td class="center" id="insurance' . $i . '">' . price ( $insurance + (( $i == 1 ) ? $regulInsurance : 0 ), 0 , '' , 1 ) . ' €</td><input type="hidden" name="hi_insurance' . $i . '" id ="hi_insurance' . $i . '" value="' . ( $insurance + (( $i == 1 ) ? $regulInsurance : 0 )) . '">' ;
print '<td class="center" id="interets' . $i . '">' . price ( $int , 0 , '' , 1 ) . ' €</td><input type="hidden" name="hi_interets' . $i . '" id ="hi_interets' . $i . '" value="' . $int . '">' ;
2019-03-03 08:55:27 +01:00
print '<td class="center"><input name="mens' . $i . '" id="mens' . $i . '" size="5" value="' . $mens . '" ech="' . $i . '"> €</td>' ;
2019-11-12 00:15:34 +01:00
print '<td class="center" id="capital' . $i . '">' . price ( $cap_rest ) . ' €</td><input type="hidden" name="hi_capital' . $i . '" id ="hi_capital' . $i . '" value="' . $cap_rest . '">' ;
2018-10-18 17:16:38 +02:00
print '</tr>' . " \n " ;
$i ++ ;
$capital = $cap_rest ;
}
}
2019-11-12 00:15:34 +01:00
elseif ( count ( $echeance -> lines ) > 0 )
2018-10-18 17:16:38 +02:00
{
2019-11-12 00:15:34 +01:00
$i = 1 ;
2018-10-18 17:16:38 +02:00
$capital = $object -> capital ;
2019-11-12 00:15:34 +01:00
$insurance = $object -> insurance_amount / $object -> nbterm ;
2018-10-18 17:16:38 +02:00
$insurance = price2num ( $insurance , 'MT' );
$regulInsurance = price2num ( $object -> insurance_amount - ( $insurance * $object -> nbterm ));
2018-10-19 15:36:13 +02:00
$printed = false ;
2019-11-12 00:15:34 +01:00
foreach ( $echeance -> lines as $line ) {
$mens = $line -> amount_capital + $line -> amount_interest ;
2018-10-18 17:16:38 +02:00
$int = $line -> amount_interest ;
2019-11-12 00:15:34 +01:00
$insu = ( $insurance + (( $i == 1 ) ? $regulInsurance : 0 ));
$cap_rest = price2num ( $capital - ( $mens - $int ), 'MT' );
2019-03-03 08:55:27 +01:00
2018-10-18 17:16:38 +02:00
print '<tr>' ;
2019-11-12 00:15:34 +01:00
print '<td class="center" id="n' . $i . '"><input type="hidden" name="hi_rowid' . $i . '" id ="hi_rowid' . $i . '" value="' . $line -> id . '">' . $i . '</td>' ;
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>' ;
print '<td class="center" id="insurance' . $i . '">' . price ( $insu , 0 , '' , 1 ) . ' €</td><input type="hidden" name="hi_insurance' . $i . '" id ="hi_insurance' . $i . '" value="' . $insu . '">' ;
print '<td class="center" id="interets' . $i . '">' . price ( $int , 0 , '' , 1 ) . ' €</td><input type="hidden" name="hi_interets' . $i . '" id ="hi_interets' . $i . '" value="' . $int . '">' ;
if ( $line -> datep > dol_now () && empty ( $line -> fk_bank )) {
2019-03-03 08:55:27 +01:00
print '<td class="center"><input name="mens' . $i . '" id="mens' . $i . '" size="5" value="' . $mens . '" ech="' . $i . '"> €</td>' ;
2019-11-12 00:15:34 +01:00
} else {
print '<td class="center">' . price ( $mens ) . ' €</td><input type="hidden" name="mens' . $i . '" id ="mens' . $i . '" value="' . $mens . '">' ;
2018-10-18 17:16:38 +02:00
}
2019-03-03 08:55:27 +01:00
2019-11-12 00:15:34 +01:00
print '<td class="center" id="capital' . $i . '">' . price ( $cap_rest ) . ' €</td><input type="hidden" name="hi_capital' . $i . '" id ="hi_capital' . $i . '" value="' . $cap_rest . '">' ;
2019-03-03 08:55:27 +01:00
print '<td class="center">' ;
2018-10-19 15:36:13 +02:00
if ( ! empty ( $line -> fk_bank )) print $langs -> trans ( 'Paid' );
elseif ( ! $printed )
{
print '<a class="butAction" href="' . DOL_URL_ROOT . '/loan/payment/payment.php?id=' . $object -> id . '&action=create&line_id=' . $line -> id . '">' . $langs -> trans ( 'DoPayment' ) . '</a>' ;
$printed = true ;
}
2018-10-18 17:16:38 +02:00
print '</td>' ;
print '</tr>' . " \n " ;
$i ++ ;
$capital = $cap_rest ;
}
}
print '</table>' ;
print '</br>' ;
print '</br>' ;
2019-11-12 00:15:34 +01:00
if ( count ( $echeance -> lines ) == 0 ) $label = $langs -> trans ( " Create " );
2018-10-18 17:16:38 +02:00
else $label = $langs -> trans ( " Save " );
2019-03-03 08:55:27 +01:00
print '<div class="center"><input class="button" type="submit" value="' . $label . '"></div>' ;
2018-10-18 17:16:38 +02:00
print '</form>' ;
// End of page
llxFooter ();
$db -> close ();