<?php

error_reporting (-1);

function calculator ($totalSum, $monthlyPayment, $percent, $commision,  $openPrice, $months) {
	$totalSum = $totalSum + $openPrice;
	$totalPayment = 0;
	for ($i = 0; $i < $months; $i++) {
		$totalSum = (($totalSum + $commision) * $percent) - $monthlyPayment;
		$totalPayment = $totalPayment + $monthlyPayment;
		if ($totalSum < $monthlyPayment) {
			$monthlyPayment = $totalSum;
			echo "{$totalSum}\n";
			$totalSum = $totalSum - $monthlyPayment;
			$totalPayment = $totalPayment + $monthlyPayment;
		}
		if ($totalSum <= 0) {
		echo "Stop it!\n";
		break;
		}
		echo "{$totalSum}\n";
	}
	return $totalPayment;
}

$totalPayment = calculator (40000, 5000, 1.04, 500, 0, 12);
echo "Всего заплатил в HomoCredit: {$totalPayment} рублей.\n";

$totalPayment = calculator (40000, 5000, 1.03, 1000, 0, 12);
echo "Всего заплатил в SoftBank: {$totalPayment} рублей.\n";

$totalPayment = calculator (40000, 5000, 1.02, 0, 7777, 12);
echo "Всего заплатил в StrawBerryBank: {$totalPayment} рублей.\n";