dolibarr 25.0.0-alpha
loan.lib.php File Reference

Library for loan module. More...

Go to the source code of this file.

Functions

 loan_prepare_head ($object)
 Prepare array with list of tabs.
 
 loanCalcMonthlyPayment ($mens, $capital, $rate, $numactualloadterm, $nbterm, $amort=null, $source='mens', $grace_period=0, $int=null, $frequency=12, $interest_basis=0, $datestart=0, $balloon=0)
 Calculate remaining loan mensuality and interests.
 
 loanFrequencies ()
 Payment frequencies of a loan.
 
 loanTermDate ($datestart, $index, $frequency=12)
 Date of a payment of a loan.
 
 loanPeriodDays ($datestart, $term, $frequency=12)
 Number of days of the period that ends with a payment (used by the daily interest basis).
 
 loanPeriodRate ($rate, $frequency=12, $interest_basis=0, $days=0)
 Interest rate of one period of a loan.
 
 loanChargeTypes ()
 Kinds of charge the "insurance" amount of a loan can be.
 
 loanChargeLabel ($charge_type, $outputlangs)
 Label of the charge of a loan (Insurance, Account-keeping fee, Other fee).
 
 loanChargePerPayment ($amount, $per_payment, $nbterm)
 Charge of each payment of a loan.
 
 loanRecalculateSchedule ($db, $user, $loan, $lines, $from, $capital, $rate, $keep, $payment=0.0)
 Recalculate the unpaid payments of a loan schedule, from one payment to the end, after a rate change or a payment that differs from the schedule.
 
 loanRecordChange ($db, $user, $loanid, $reason, $datechange, $rate_old, $rate_new, $keep, $payment_old, $payment_new, $nbterm_old, $nbterm_new, $fk_payment=0)
 Record a change of a loan (rate change, or a payment that differs from the schedule) in its history.
 
 loanFetchChanges ($db, $loanid)
 History of the changes of a loan, most recent first.
 
 loanRateAt ($history, $current, $date)
 Annual rate of a loan in force on a date, from the history of its rate changes: the rate of the most recently recorded change that applies on or before the date; before the first change, the rate the loan had before it; without any change, the current rate of the loan.
 
 loanNextPayment ($db, $loan)
 Next payment of a loan that has no schedule, worked out from the loan and the payments already made: due on the first payment date after the last payment; interest on the capital left for that period, at the rate in force on that date (daily or per period); capital so that the remaining payments repay the capital left (less the balloon), the last payment repaying all that is left; charge from the charge type of the loan.
 

Detailed Description

Library for loan module.

Definition in file loan.lib.php.

Function Documentation

◆ loan_prepare_head()

loan_prepare_head ( $object)

Prepare array with list of tabs.

Parameters
Loan$objectObject related to tabs
Returns
array<array{0:string,1:string,2:string}> Array of tabs to show

Definition at line 34 of file loan.lib.php.

References $conf, $object, complete_head_from_modules(), Link\count(), dol_dir_list(), dol_sanitizeFileName(), and getDolGlobalString().

◆ loanCalcMonthlyPayment()

loanCalcMonthlyPayment ( $mens,
$capital,
$rate,
$numactualloadterm,
$nbterm,
$amort = null,
$source = 'mens',
$grace_period = 0,
$int = null,
$frequency = 12,
$interest_basis = 0,
$datestart = 0,
$balloon = 0 )

Calculate remaining loan mensuality and interests.

Parameters
float$mensValue of this mensuality (interests include, set 0 if we don't paid interests for this mensuality)
float$capitalRemaining capital for this mensuality
float$rateLoan rate
int$numactualloadtermActual loan term
int$nbtermTotal number of term for this loan
null | float | string$amortCapital amortization for this term (optional)
string$sourceSource of modification ('mens', 'amort' or 'interet')
int$grace_periodNumber of terms with grace period (amortization = 0)
null | float | string$intInterest amount for this term (optional)
int$frequencyNumber of payments per year (52, 26, 12, 4, 2 or 1)
int$interest_basis0 = rate / number of payments per year, 1 = daily (rate x days in the period / 365)
int$datestartDate of the first payment (needed for the daily basis)
float$balloonBalloon / residual paid with the last payment (0 = none)
Returns
array<array{cap_rest:float,cap_rest_str:string,interet:float,interet_str:string,amort:string,amort_num:float,mens:string,mens_num:float}> Array with remaining capital, interest, amortization and mensuality for each remaining terms

Definition at line 111 of file loan.lib.php.

References $conf, $object, loanPeriodDays(), loanPeriodRate(), price(), and price2num().

◆ loanChargeLabel()

loanChargeLabel ( $charge_type,
$outputlangs )

Label of the charge of a loan (Insurance, Account-keeping fee, Other fee).

Parameters
int$charge_typecharge_type of the loan
Translate$outputlangsLanguage
Returns
string

Definition at line 328 of file loan.lib.php.

References loanChargeTypes().

◆ loanChargePerPayment()

loanChargePerPayment ( $amount,
$per_payment,
$nbterm )

Charge of each payment of a loan.

Parameters
float$amountinsurance_amount of the loan
int$per_payment1 = the amount is due with every payment, 0 = it is a total spread over the payments
float$nbtermNumber of payments
Returns
array{0:float,1:float} Charge of each payment, rounding difference added to the first payment

Definition at line 342 of file loan.lib.php.

References price2num().

Referenced by loanNextPayment(), and loanRecalculateSchedule().

◆ loanChargeTypes()

loanChargeTypes ( )

Kinds of charge the "insurance" amount of a loan can be.

Returns
array<int,string> charge_type => translation key

Definition at line 312 of file loan.lib.php.

Referenced by loanChargeLabel().

◆ loanFetchChanges()

loanFetchChanges ( $db,
$loanid )

History of the changes of a loan, most recent first.

Parameters
DoliDB$dbDatabase handler
int$loanidLoan id
Returns
array<int,stdClass> Records of llx_loan_change (date_change and datec as timestamps)

Definition at line 553 of file loan.lib.php.

Referenced by loanNextPayment(), and loanRecalculateSchedule().

◆ loanFrequencies()

loanFrequencies ( )

Payment frequencies of a loan.

Returns
array<int,string> Number of payments per year => translation key

Definition at line 231 of file loan.lib.php.

◆ loanNextPayment()

loanNextPayment ( $db,
$loan )

Next payment of a loan that has no schedule, worked out from the loan and the payments already made: due on the first payment date after the last payment; interest on the capital left for that period, at the rate in force on that date (daily or per period); capital so that the remaining payments repay the capital left (less the balloon), the last payment repaying all that is left; charge from the charge type of the loan.

Parameters
DoliDB$dbDatabase handler
Loan$loanLoan (fetched)
Returns
?array{date:int,term:int,capital:float,interest:float,charge:float} Null if nothing is left to repay

Definition at line 606 of file loan.lib.php.

References loanChargePerPayment(), loanFetchChanges(), loanPeriodDays(), loanPeriodRate(), loanRateAt(), loanTermDate(), and price2num().

◆ loanPeriodDays()

loanPeriodDays ( $datestart,
$term,
$frequency = 12 )

Number of days of the period that ends with a payment (used by the daily interest basis).

Parameters
int$datestartDate of the first payment
int$termPayment number, 1 for the first payment
int$frequencyNumber of payments per year
Returns
int Number of days

Definition at line 280 of file loan.lib.php.

References loanTermDate().

Referenced by loanCalcMonthlyPayment(), loanNextPayment(), and loanRecalculateSchedule().

◆ loanPeriodRate()

loanPeriodRate ( $rate,
$frequency = 12,
$interest_basis = 0,
$days = 0 )

Interest rate of one period of a loan.

Parameters
float$rateAnnual rate (as a fraction, or in percent: the result is in the same unit)
int$frequencyNumber of payments per year
int$interest_basis0 = rate / number of payments per year, 1 = daily (rate x days in the period / 365)
int$daysDays in the period, for the daily basis (0 = the average period: 7 days weekly, 14 days fortnightly, else 365 / number of payments)
Returns
float Rate of the period

Definition at line 294 of file loan.lib.php.

Referenced by LoanSchedule\calcMonthlyPayments(), loanCalcMonthlyPayment(), loanNextPayment(), and loanRecalculateSchedule().

◆ loanRateAt()

loanRateAt ( $history,
$current,
$date )

Annual rate of a loan in force on a date, from the history of its rate changes: the rate of the most recently recorded change that applies on or before the date; before the first change, the rate the loan had before it; without any change, the current rate of the loan.

Parameters
array<int,stdClass>$history From loanFetchChanges()
float$currentCurrent rate of the loan (percent)
int$dateDate
Returns
float Rate (percent)

Definition at line 577 of file loan.lib.php.

References $c.

Referenced by loanNextPayment(), and loanRecalculateSchedule().

◆ loanRecalculateSchedule()

loanRecalculateSchedule ( $db,
$user,
$loan,
$lines,
$from,
$capital,
$rate,
$keep,
$payment = 0.0 )

Recalculate the unpaid payments of a loan schedule, from one payment to the end, after a rate change or a payment that differs from the schedule.

Paid payments are never changed. The loan's number of payments and end date are updated, and its rate when a new rate is given.

Parameters
DoliDB$dbDatabase handler
User$userUser making the change
Loan$loanLoan (its rate is replaced by $rate)
LoanSchedule[]$linesAll the schedule lines of the loan, in date order
int$fromIndex (0 = first line) of the first line to recalculate; it and all the following lines must be unpaid
float$capitalCapital left to repay before that line
?float$rateAnnual rate in percent for the recalculated lines (a rate change), or null to use, for each payment, the rate in force on its date (see loanRateAt())
string$keep'term' = keep the number of payments (the repayment changes), 'payment' = keep the repayment (the number of payments changes)
float$paymentWith 'payment': repayment (capital + interest) for every recalculated payment, or 0 = each payment keeps its current repayment (added payments: the last regular one)
Returns
array{error:string,payment_old:float,payment_new:float,nbterm_old:int,nbterm_new:int} 'error' is a translation key, '' if OK

Definition at line 370 of file loan.lib.php.

References dol_now(), loanChargePerPayment(), loanFetchChanges(), loanPeriodDays(), loanPeriodRate(), loanRateAt(), loanTermDate(), and price2num().

◆ loanRecordChange()

loanRecordChange ( $db,
$user,
$loanid,
$reason,
$datechange,
$rate_old,
$rate_new,
$keep,
$payment_old,
$payment_new,
$nbterm_old,
$nbterm_new,
$fk_payment = 0 )

Record a change of a loan (rate change, or a payment that differs from the schedule) in its history.

Parameters
DoliDB$dbDatabase handler
User$userUser making the change
int$loanidLoan id
string$reason'rate' or 'payment'
int$datechangeDate from which the change applies
float$rate_oldRate before (percent)
float$rate_newRate after (percent)
string$keep'term', 'payment' or '' (no schedule recalculated)
float$payment_oldRepayment before
float$payment_newRepayment after
float$nbterm_oldNumber of payments before
float$nbterm_newNumber of payments after
int$fk_paymentPayment that caused the change (reason 'payment'), 0 if none
Returns
int Id of the record if OK, -1 if KO

Definition at line 531 of file loan.lib.php.

References $conf, dol_now(), and price2num().

◆ loanTermDate()

loanTermDate ( $datestart,
$index,
$frequency = 12 )

Date of a payment of a loan.

Parameters
int$datestartDate of the first payment
int$index0 for the first payment, 1 for the second, ...
int$frequencyNumber of payments per year (52, 26, 12, 4, 2 or 1)
Returns
int Date of the payment

Definition at line 251 of file loan.lib.php.

References dol_print_date(), and dol_time_plus_duree().

Referenced by loanNextPayment(), loanPeriodDays(), and loanRecalculateSchedule().