Piggies can have no target amount.

This commit is contained in:
James Cole
2022-03-27 18:30:46 +02:00
parent 980120f1bd
commit 8ee4c2ea11
9 changed files with 94 additions and 59 deletions

View File

@@ -79,47 +79,55 @@ class PiggyBankTransformer extends AbstractTransformer
/** @var ObjectGroup $objectGroup */
$objectGroup = $piggyBank->objectGroups->first();
if (null !== $objectGroup) {
$objectGroupId = (int)$objectGroup->id;
$objectGroupOrder = (int)$objectGroup->order;
$objectGroupId = (int) $objectGroup->id;
$objectGroupOrder = (int) $objectGroup->order;
$objectGroupTitle = $objectGroup->title;
}
// get currently saved amount:
$currentAmountStr = $this->piggyRepos->getCurrentAmount($piggyBank);
$currentAmount = number_format((float)$currentAmountStr, $currency->decimal_places, '.', '');
$currentAmount = number_format((float) $currentAmountStr, $currency->decimal_places, '.', '');
// left to save:
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
// Amounts, depending on 0.0 state of target amount
$percentage = null;
$targetAmountString = null;
$leftToSaveString = null;
$savePerMonth = null;
if (0.000 !== (float) $piggyBank->targetamount) {
$leftToSave = bcsub($piggyBank->targetamount, $currentAmountStr);
$targetAmount = (string) $piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', $targetAmount) ? '0.01' : $targetAmount;
$percentage = (int) (0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
$targetAmountString = number_format((float) $targetAmount, $currency->decimal_places, '.', '');
$leftToSaveString = number_format((float) $leftToSave, $currency->decimal_places, '.', '');
$savePerMonth = number_format((float) $this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', '');
}
$startDate = $piggyBank->startdate?->toAtomString();
$targetDate = $piggyBank->targetdate?->toAtomString();
// target and percentage:
$targetAmount = $piggyBank->targetamount;
$targetAmount = 1 === bccomp('0.01', (string)$targetAmount) ? '0.01' : $targetAmount;
$percentage = (int)(0 !== bccomp('0', $currentAmountStr) ? $currentAmountStr / $targetAmount * 100 : 0);
return [
'id' => (string)$piggyBank->id,
'id' => (string) $piggyBank->id,
'created_at' => $piggyBank->created_at->toAtomString(),
'updated_at' => $piggyBank->updated_at->toAtomString(),
'account_id' => (string)$piggyBank->account_id,
'account_id' => (string) $piggyBank->account_id,
'account_name' => $piggyBank->account->name,
'name' => $piggyBank->name,
'currency_id' => (string)$currency->id,
'currency_id' => (string) $currency->id,
'currency_code' => $currency->code,
'currency_symbol' => $currency->symbol,
'currency_decimal_places' => (int)$currency->decimal_places,
'target_amount' => number_format((float)$targetAmount, $currency->decimal_places, '.', ''),
'currency_decimal_places' => (int) $currency->decimal_places,
'target_amount' => $targetAmountString,
'percentage' => $percentage,
'current_amount' => $currentAmount,
'left_to_save' => number_format((float)$leftToSave, $currency->decimal_places, '.', ''),
'save_per_month' => number_format((float)$this->piggyRepos->getSuggestedMonthlyAmount($piggyBank), $currency->decimal_places, '.', ''),
'left_to_save' => $leftToSaveString,
'save_per_month' => $savePerMonth,
'start_date' => $startDate,
'target_date' => $targetDate,
'order' => (int)$piggyBank->order,
'order' => (int) $piggyBank->order,
'active' => true,
'notes' => $notes,
'object_group_id' => $objectGroupId ? (string)$objectGroupId : null,
'object_group_id' => $objectGroupId ? (string) $objectGroupId : null,
'object_group_order' => $objectGroupOrder,
'object_group_title' => $objectGroupTitle,
'links' => [