PHP 之房贷计算器、组合贷

 一、等额本金

// (等额本金)
//$loanAmount=>贷款金额
//$loanPeriod=>贷款年限
//$interestRate=>贷款利息
function calculateEqualPrincipalPayment($loanAmount, $loanPeriod, $interestRate)
{
    $monthlyPrincipal = $loanAmount / ($loanPeriod * 12);
    $monthlyInterest = $loanAmount * ($interestRate / 100) / 12;
    $remainingAmount = $loanAmount;

    $result = [];

    for ($i = 1; $i <= $loanPeriod * 12; $i++) {
        $interest = $remainingAmount * ($interestRate / 100) / 12;
        $principal = $monthlyPrincipal;
        $remainingAmount -= $monthlyPrincipal;

        $monthlyPayment = $principal + $interest;

        $result[$i] = [
            'month' => $i,//期数
            'principal' => round($principal, 2),//月供本金
            'interest' => round($interest, 2),//月供利息
            'monthlyPayment' => round($monthlyPayment, 2),//月供总额
            'remainingAmount' => round($remainingAmount, 2),//剩余本金
        ];
    }

    return $result;
}

二、等额本息

 

// (等额本息)
//$loanAmount=>贷款金额
//$loanPeriod=>贷款年限
//$loanPeriod=>贷款利率
function calculateEqualInstallmentPayment($loanAmount, $loanPeriod, $interestRate)
{
    $monthlyInterestRate = $interestRate / 12 / 100;
    $numOfMonthlyPayments = $loanPeriod * 12;

    $monthlyPayment = $loanAmount * $monthlyInterestRate * pow(1 + $monthlyInterestRate, $numOfMonthlyPayments)
        / (pow(1 + $monthlyInterestRate, $numOfMonthlyPayments) - 1);

    $result = [];

    for ($i = 1; $i <= $numOfMonthlyPayments; $i++) {
        $interest = $loanAmount * $monthlyInterestRate;
        $principal = $monthlyPayment - $interest;
        $remainingAmount = $loanAmount - $principal;

        $result[$i] = [
            'month' => $i,//期数
            'principal' => round($principal, 2),//期数
            'interest' => round($interest, 2),//月供本金
            'monthlyPayment' => round($monthlyPayment, 2),//月供总额
            'remainingAmount' => round($remainingAmount, 2),//剩余本金
        ];

        $loanAmount = $remainingAmount;
    }

    return $result;
}

三、公积金和商业贷组合贷款

// 测试组合贷款计算()等额本息
function calculateCombinationLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
{
    // 计算商业贷款的还款计划
    $commercialRepayments = calculateEqualInstallmentPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);

    // 计算公积金贷款的还款计划
    $fundRepayments = calculateEqualInstallmentPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);
    
    //判断公积金和商业贷那个贷款时间长,就以那个合并
    $arr_ti = $commercialRepayments;
    $arr_ti_duan = $fundRepayments;
    if ($fundLoanPeriod > $commercialLoanPeriod) {
        $arr_ti = $fundRepayments;
        $arr_ti_duan = $commercialRepayments;
    }
    $newArray = [];
    foreach ($arr_ti as $is => $repayment) {
        if ($fundLoanPeriod < $commercialLoanPeriod) {
            $shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
            $jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
        } else {
            $shangye_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"]?: 0;
            $jijin_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
        }

        $newArray[] = [
            "month" => $repayment["month"],//还款期数
            "jijin_monthlyPayment" => round($jijin_monthlyPayment, 2),//公积金金额
            "shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),//商贷金额
            "monthlyPayment" => round(($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"]), 2),//总还款金额
            "interest" => round(($repayment["interest"] + $arr_ti_duan[$is]["interest"]), 2),//总还款利息
        ];
    }
    return $newArray;
}

// 测试组合贷款计算(等额本金)
function calculatePrincipaltionLoan($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate, $fundLoanAmount, $fundLoanPeriod, $fundInterestRate)
{
    // 计算商业贷款的还款计划
    $commercialRepayments = calculateEqualPrincipalPayment($commercialLoanAmount, $commercialLoanPeriod, $commercialInterestRate);

    // 计算公积金贷款的还款计划
    $fundRepayments = calculateEqualPrincipalPayment($fundLoanAmount, $fundLoanPeriod, $fundInterestRate);

    //判断公积金和商业贷那个贷款时间长,就以那个合并
    $arr_ti = $commercialRepayments;
    $arr_ti_duan = $fundRepayments;

    if ($fundLoanPeriod > $commercialLoanPeriod) {
        $arr_ti = $fundRepayments;
        $arr_ti_duan = $commercialRepayments;
    }
    $newArray = [];
    foreach ($arr_ti as $is => $repayment) {
        if ($fundLoanPeriod < $commercialLoanPeriod) {
            $shangye_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
            $jijin_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"] ?: 0;
        } else {
            $shangye_monthlyPayment = $arr_ti_duan[$is]["monthlyPayment"]?: 0;
            $jijin_monthlyPayment = $repayment["monthlyPayment"] ?: 0;
        }

        $newArray[] = [
            "month" => $repayment["month"],
            "jijin_monthlyPayment" => round($jijin_monthlyPayment, 2),
            "shangye_monthlyPayment" => round($shangye_monthlyPayment, 2),
            "monthlyPayment" => round(($repayment["monthlyPayment"] + $arr_ti_duan[$is]["monthlyPayment"]), 2),
            "interest" => round(($repayment["interest"] + $arr_ti_duan[$is]["interest"]), 2),
        ];
    }
    return $newArray;
}

四、可实现效果
PHP 之房贷计算器、组合贷_第1张图片

你可能感兴趣的:(php)