3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $date = "2016/6"; $rate = "2.90"; $summer = 7; $winter = 1; $bonus = 100000; $count = 180; $amount = 6070000; //料率 計算用 $calc_rate = $rate / 100; # ボーナス回数 $bcount = getBonusCount($date, $count, $summer, $winter); // ボーナス月テーブルの配列取得用 $bonusPattern = getBonusMonthPattern($summer, $winter); //【1】BE33 ボーナス支払残高 // = IF(★ボーナス加算希望額=0, 0, IF(【1-1】="均等", 【1-2】, IF("ボーナス月回数=奇数", 【1-3】, 【1-4】))) $bike_bonus_repay = getBikeBonusRepay($bonus, $bonusPattern, $date, $rate, $summer, $winter, $bcount); echo $bike_bonus_repay . "\r\n"; echo $amount . "\r\n"; $bike_monthly_repay = round(pmt($calc_rate, $count, $amount - $bike_bonus_repay), 0, PHP_ROUND_HALF_DOWN); echo $bike_monthly_repay; function pmt($rate, $count, $amount) { $count = $count; //支払回数 $rate = $rate / 12; //月利(%) $amount = $rate * -$amount * pow((1 + $rate), $count) / (1 - pow((1 + $rate), $count)); // return $amount; } function getBonusMonthPattern($summer, $winter) { return $summer . "-" . $winter; } /** *【1-1】AT21 間隔数利率 計算用値 均等 ot 不均等 * = VLOOKUP(★ボーナス加算月, [ボ月テーブル], 7, FALSE) */ function get1_1($pattern) { $a_table = array( "6-12" => 1, //"均等", "7-1" => 1, //"均等", "8-2" => 0, //"不均等", "7-12" => 0, //"不均等", "8-1" => 0, //"不均等", ); return $a_table[$pattern]; } /** *【1】BE33 ボーナス支払残高 * = IF(★ボーナス加算希望額=0, 0, IF(【1-1】="均等", 【1-2】, IF("ボーナス月回数=奇数", 【1-3】, 【1-4】))) */ function getBikeBonusRepay($bonus, $pattern, $date, $rate, $summer, $winter, $bcount) { $repay = 0; if(0 == $bonus) { return $repay; } if(1 == get1_1($pattern)) { // *【1-2】BE16 ①ボーナス返済間隔均等 // * = +ROUNDUP(★ボーナス加算希望額 / 【1-2-1】, 0) $repay = round($bonus / get1_2_1($date, $rate, $summer, $winter, $bcount), 0); } else { if(0 > $bcount % 2) { //支払回数 奇数 $repay = get1_3($date, $rate, $summer, $winter, $bonus, $bcount); } else { //支払回数 偶数 $repay = get1_4($date, $rate, $summer, $winter, $bonus, $bcount); } } return $repay; } function get1_4($date, $rate, $summer, $winter, $bonus, $bcount) { //【1-3-1-1-1】AQ11 「初回利率」(少数12桁) $b_first_rate = getFirstRate($date, $rate, $summer, $winter); //【1-3-1-1-2】AP4 「月利」(少数12桁) $b_month_rate = getMonthlyRate($rate); //【1-3-1-1-4】AV18 ボーナス月と支払開始月からとりだす計算用の数値 //【1-3-1-1-3】AW18  list($keyNumber1, $keyNumber2) = getKeyNumbers($date, $summer); // *【1-4-1-1】AP18 「賦金率分子」 $b_inst_numerator = (1 + $b_first_rate) * pow((1 + $b_month_rate * $keyNumber2), ($bcount / 2)) * pow((1 + $b_month_rate * $keyNumber1), ($bcount/ 2 - 1)) * ((1 + $b_month_rate * $keyNumber2) * (1 + $b_month_rate * $keyNumber1) - 1); // *【1-4-1-2】AP19 「賦金率分母」 $b_inst_denominator = (1 + (1 + $b_month_rate * $keyNumber2)) * (pow((1 + $b_month_rate * $keyNumber2), ($bcount / 2)) * pow((1 + $b_month_rate * $keyNumber1), ($bcount / 2)) - 1); // *【1-4-1】BE19  // * = +ROUNDDOWN(【1-4-1-1】/【1-4-1-2】, 12) $b_result = round($b_inst_numerator / $b_inst_denominator, 12, PHP_ROUND_HALF_DOWN); //【1-4】BE20 ②ボーナス不均・偶数 // = +ROUNDUP(★ボーナス加算希望額 / 【1-4-1】, 0) return round($bonus / $b_result, 0, PHP_ROUND_HALF_UP); } /** *【1-3-1-1-4】AV18 ボーナス月と支払開始月からとりだす計算用の数値 * = +IF(ボーナス月1 = 8, VLOOKUP(★開始月, "1~12", 2, 0), VLOOKUP(★開始月, "5 or 7", 3, 0)) * $keyNumber1 * *【1-3-1-1-3】AW18  * = +IF(【1-3-1-1-4】 = 5, 7, 5) * $keyNumber2 */ function getKeyNumbers($date, $summer) { $t_date = explode("/", $date); $month = $t_date[1]; if(8 == $summer) { $a_bonusMonthArray = array( 1 => 7, 2 => 7, 3 => 7, 4 => 7, 5 => 7, 6 => 7, 7 => 7, 8 => 5, 9 => 5, 10 => 5, 11 => 5, 12 => 5); } else { $a_bonusMonthArray = array( 1 => 5, 2 => 7, 3 => 7, 4 => 7, 5 => 7, 6 => 7, 7 => 7, 8 => 7, 9 => 5, 10 => 5, 11 => 5, 12 => 5); } $keyNumber1 = $a_bonusMonthArray[$month]; // ??? if(5 == $keyNumber1) { $keyNumber2 = 7; } else { $keyNumber2 = 5; } return array($keyNumber1, $keyNumber2); } /** *【1-3-1-1-2】AP4 「月利」(少数12桁) * = ROUND(★手数料率(年利) / 12, 12) */ function getMonthlyRate($rate) { return round($rate / 12, 12); } function getFirstRate($date, $rate, $summer, $winter) { //【1-2-1-2】AP11 「初回利率」(少数5桁) // = +ROUNDDOWN(★手数料率(年利) * 【1-2-1-2-1】 / 12, 5) // 【1-2-1-2-1】AP10 「初回期間」※支払開始後、最初のボーナス月までのヶ月 $t_date = explode("/", $date); $month = $t_date[1]; $monthCount = 0; while($monthCount < 12) { $monthCount++; $check = $month + $monthCount; if($check > 12) { $check = $check - 12; } if($check == $summer || $check == $winter) { $monthCount++; break; } } return round($rate * $monthCount / 12, 12, PHP_ROUND_HALF_DOWN); } function getBonusCount($date, $count = NULL, $summer = NULL, $winter = NULL) { # 月 $t_date = explode("/", $date); # 支払回数未設定 if (!$count) { return 0; } # ボーナス月未設定 if (!$summer && !$winter) { return 0; } # ボーナス回数 $bcount = 0; if (12 > $count) { $i = $t_date[1]; $j = $i + $count; $k = 0; $l = 0; for ($k = $i; $k < $j; $k ++) { $l = $k > 12 ? $k - 12 : $k; if ($summer == $l) { $bcount ++; } if ($winter == $l) { $bcount ++; } } } else { # 年2回 if ($summer && $winter) { $bcount = $count / 6; } # 年1回 else { $bcount = $count / 12; } } return $bcount; } /** *【1-2-1】data!AP15 「賦金率」 * = +ROUNDDOWN(【1-2-1-1】 * (1 + 【1-2-1-2】) * 【1-2-1-3】 / (【1-2-1-4】 - 1), 12) * */ function get1_2_1($date, $rate, $summer, $winter, $bcount) { //【1-2-1-1】AP7 「ボーナス利率」 // = +ROUNDDOWN(★手数料率(年利) * 6 / 12, 5) $b_rate = round($rate * 6 / 12, 5, PHP_ROUND_HALF_DOWN); //【1-2-1-2】AP11 「初回利率」(少数5桁) $b_first_rate = getFirstRate($date, $rate, $summer, $winter); //【1-2-1-3】AP8 「間隔数利率」 // = ROUNDDOWN((1 + 【1-2-1-1】)^(【1-0-1】-1),12) $b_interval_rate = round(pow((1 + $b_rate), ($bcount - 1)), 12, PHP_ROUND_HALF_DOWN); //【1-2-1-4】AP9 「回数利率」 // = ROUNDDOWN((1 + 【1-2-1-1】)^(【1-0-1】),12) $b_count_rate = round(pow(1 + $b_rate, $bcount), 12, PHP_ROUND_HALF_DOWN); //= +ROUNDDOWN(【1-2-1-1】 * (1 + 【1-2-1-2】) * 【1-2-1-3】 / (【1-2-1-4】 - 1), 12) $b_inst_rate = round($b_rate * (1 + $b_first_rate) * $b_interval_rate / ($b_count_rate - 1), 12, PHP_ROUND_HALF_DOWN); return $b_inst_rate; }
based on GiOWL
Output for 5.6.0 - 7.3.0alpha1
113909 6070000 40846