Skip to content

Instantly share code, notes, and snippets.

@malgany
Created April 13, 2022 21:36
Show Gist options
  • Save malgany/3cf78f9ad787166572c0fb66f35a2b18 to your computer and use it in GitHub Desktop.
Save malgany/3cf78f9ad787166572c0fb66f35a2b18 to your computer and use it in GitHub Desktop.
Split balance amount between statements
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