Created
April 13, 2022 21:36
-
-
Save malgany/3cf78f9ad787166572c0fb66f35a2b18 to your computer and use it in GitHub Desktop.
Split balance amount between statements
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function calculateInstallmentAmounts( | |
int $installments, | |
string $description, | |
float $amount | |
): Collection { | |
$diff_sum = (round((pow(10, 2) * $amount)) % $installments) / pow(10, 2); | |
$installment_amount = ($amount - $diff_sum) / $installments; | |
$installment = array_fill(1, $installments, [$description => $installment_amount]); | |
return $this->distributeBalanceBetweenInstallments($diff_sum, $installment, $description, $installments); | |
} | |
private function distributeBalanceBetweenInstallments( | |
float $diff_sum, | |
array $installment, | |
string $description, | |
int $installments | |
): Collection { | |
$idx = 1; | |
for ($i = $diff_sum * 100; $i > 0; $i--) { | |
$installment[$idx++][$description] += 0.01; | |
$idx = ($idx > $installments) ? 1 : $idx; | |
} | |
return collect($installment); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment