File: /home/cafsindia/lifemaze_in/lp_lib/lp_calculation.php
<?php
/**********************************************************
Filename: lp_calculation.php
Description: Object for all calculation related operation
Author: uday
Created on: AUG, 01 2018
Approved on:
Reviewed on:
------------------------------------------------------------
Modification Details
Changed by:
------------------------------------------------------------
**********************************************************/
class lp_calculation{
/*** GET TOTAL DAYS - START ***/
public static function days_difference($end_date, $begin_date){
$begin_date = new DateTime($begin_date);
$begin_date = $begin_date->format("Y-m-d");
$end_date = new DateTime($end_date);
$end_date = $end_date->format("Y-m-d");
$date_parts1 = explode("-", $begin_date);
$date_parts2 = explode("-", $end_date);
$start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
$diff = abs($end_date - $start_date);
return $diff;
}
/*** GET TOTAL DAYS - END ***/
/*** GET TOTAL YEAR - START ***/
public static function total_year($end_date, $begin_date){
$begin_date = new DateTime($begin_date);
$begin_date = $begin_date->format("Y-m-d");
$end_date = new DateTime($end_date);
$end_date = $end_date->format("Y-m-d");
$date_parts1 = explode("-", $begin_date);
$date_parts2 = explode("-", $end_date);
$start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
$end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
$diff = abs($end_date - $start_date);
if((int)$diff === 365){
$years = 1;
}else{
$years = floor($diff / 365.25);
}
return $years;
}
/*** GET TOTAL YEAR - END ***/
/*** GET TOTAL MONTH - START ***/
public static function total_month($start_date, $end_date){
$start_date = new DateTime($start_date);
$end_date = new DateTime($end_date);
$tot_month = $start_date->diff($end_date)->m + ($start_date->diff($end_date)->y*12);
return $tot_month;
}
/*** GET TOTAL MONTH - END ***/
/*** GET MATURITY DATE - START ***/
public static function maturity_date($start_date, $tenure,$tenure_type){
$tenure_array = array("1"=>"day","2"=>"months","3"=>"year");
$maturity = $tenure_array[$tenure_type];
$maturity_date = "+ $tenure $maturity";
$start_date = new DateTime($start_date);
$bank_maturity_date = $start_date->modify("$maturity_date");
$bank_maturity_date = $bank_maturity_date->format("d-M-Y");
return json_encode(array('sts' => true,'maturity_date' =>$bank_maturity_date));
}
/*** GET MATURITY DATE - END ***/
/*** SIMPLE INTEREST - START ***/
public static function simple_interest($start_date,$end_date,$tenure,$tenure_type,$ror,$amount){
$maturity_amt = 0;
$maturity_date = "";
$maturity_info = self::maturity_date($start_date,$tenure,$tenure_type);
$maturity_info = json_decode($maturity_info);
$maturity_date = $maturity_info->maturity_date;
$tot_month = self::total_month($start_date,$maturity_date);
$ror1 = $ror/100;
$ror = $ror1/12;
$maturity_amt = $amount*pow(1 + $ror,$tot_month);
$maturity_amt = round($maturity_amt);
return json_encode(array('sts' => true, 'maturity_amt' =>$maturity_amt,'maturity_date' =>$maturity_date));
}
/*** SIMPLE INTEREST - END ***/
/*** MONTHLY INV CALCULATION - START***/
function pmt($interest, $years, $loan) {
/*
$months = $months;
$interest = $interest / 1200;
$amount = ($interest * -$loan * pow((1 + $interest), $months)) / (1 - pow((1 + $interest), $months));
return number_format($amount, 2);
*/
$tv = $loan; // total value
$er = $interest; // expected rate of return
$Y = $years;
$n = 12;
$totalCost = round($tv * pow(1 + (0 / 100), $Y));
$c1 = ($er / (100 * $n));
$c2 = pow(1 + $c1, $n * $Y);
$c3 = $c2 - 1;
return round(($totalCost * $c1) / $c3);
}
/*** MONTHLY INV CALCULATION - END***/
function lumpsum($amount,$interest,$months){
$i = $interest/100;
$n = $months/12;
$lumpsum = $amount * (1/pow(1+$i, $n));
return round($lumpsum);
}
/*** COMPOUND INTEREST - START ***/
public static function compound_interest($present_value,$interst_rate,$number_periods){
$future_value = round($present_value * pow(1 + ($interst_rate/100),$number_periods), 2);
return json_encode(array('sts' => true, 'future_value' =>$future_value));
}
/*** COMPOUND INTEREST - END ***/
/***
RD MATURITY - START
## $compounding in Monthly:12 Quarterly:4 Half-yearly:2 Yearly:1
## $months in months
***/
function rd_maturity($interst_rate,$compounding,$months,$principal){
$irate = $interst_rate/$compounding;
$year = $months/12;
$maturity_amount =($principal *(pow((1+ $irate/100),($year)*$compounding)-1)/(1-pow((1+$irate/100),-$compounding/12)));
$maturity_amount = round($maturity_amount);
return json_encode(array('sts' => true, 'maturity_amount' => $maturity_amount));
}
/*** RD MATURITY - END ***/
/***
FD MATURITY - START
## $compounding in Monthly:12 Quarterly:4 Half-yearly:2 Yearly:1
## $years in years
***/
function fd_maturity($interst_rate,$compounding,$years,$principal){
$interst_rate = $interst_rate/100;
$years = $years/365;
$temp = (1+$interst_rate/$compounding);
$amount = $principal*pow($temp,($compounding*$years));
$maturity_amount = round($amount);
$earned_amount = $maturity_amount-$principal;
return json_encode(array('sts' => true, 'fd_maturity_amount' => $maturity_amount, 'earned_amount' => $earned_amount));
}
/*** FD MATURITY - END ***/
/*** LOAN INTEREST - START ***/
public static function loan_interest($total_month,$emi,$outstanding){
$numPay = $total_month;
$payment = $emi;
$amount = $outstanding;
$error = pow(10,-5);
$approx = 0.05/12;
$prev_approx;
for ($k=0;$k<20; $k++) {
$prev_approx = $approx;
$approx = $prev_approx - (self::f($prev_approx,$numPay,$payment,$amount)/self::f_prime($prev_approx,$numPay,$amount));
$diff = abs($approx-$prev_approx);
if ($diff < $error) break;
}
$apr = round($approx * 12 * 10000 /100, 1);
if($apr < 0){
$msg = "Can't cover loan amount, Please eneter valid amount";
return json_encode(array('sts'=> false,'ror' =>$apr,'info'=>$msg));
}else{
$msg = "Your Loan Interest Rate $apr%";
return json_encode(array('sts'=> true,'ror' =>$apr,'info'=>$msg));
}
}
public static function f($x,$numPay,$payment,$amount) {
global $error;
return $amount * $x * (pow(1 + $x,$numPay)/(pow(1 + $x, $numPay) - 1)) - $payment;
}
public static function f_prime($x,$numPay,$amount) {
global $error;
return $amount * (pow(1 + $x,$numPay)/(-1 + pow(1 + $x,$numPay)) - $numPay * $x * pow(1 + $x,-1 + 2*$numPay)/pow(-1 + pow(1 + $x,$numPay),2) + $numPay * $x * pow(1 + $x,-1 + $numPay)/(-1 + pow(1 + $x,$numPay)));
}
/*** LOAN INTEREST - END ***/
}
?>