This commit is contained in:
James Cole 2020-06-20 18:57:20 +02:00
parent 1a154a8d45
commit 45eb758583
3 changed files with 85 additions and 6 deletions

View File

@ -86,7 +86,7 @@ class IndexController extends Controller
/** @var PiggyBank $piggy */
foreach ($collection as $piggy) {
$array = $transformer->transform($piggy);
$groupOrder = $array['object_group_order'];
$groupOrder = (int) $array['object_group_order'];
// make group array if necessary:
$piggyBanks[$groupOrder] = $piggyBanks[$groupOrder] ?? [
'object_group_id' => $array['object_group_id'],
@ -109,19 +109,61 @@ class IndexController extends Controller
}
// calculate new interesting fields:
$accounts[$accountId]['left'] -= $array['current_amount'];
$accounts[$accountId]['saved'] += $array['current_amount'];
$accounts[$accountId]['target'] += $array['target_amount'];
$accounts[$accountId]['to_save'] += ($array['target_amount'] - $array['current_amount']);
$array['account_name'] = $account['name'];
$accounts[$accountId]['left'] -= $array['current_amount'];
$accounts[$accountId]['saved'] += $array['current_amount'];
$accounts[$accountId]['target'] += $array['target_amount'];
$accounts[$accountId]['to_save'] += ($array['target_amount'] - $array['current_amount']);
$array['account_name'] = $account['name'];
$piggyBanks[$groupOrder]['piggy_banks'][] = $array;
}
// do a bunch of summaries.
$piggyBanks = $this->makeSums($piggyBanks);
ksort($piggyBanks);
return view('piggy-banks.index', compact('piggyBanks', 'accounts'));
}
/**
* @param array $piggyBanks
*
* @return array
*/
private function makeSums(array $piggyBanks): array
{
$sums = [];
foreach ($piggyBanks as $groupOrder => $group) {
$groupId = $group['object_group_id'];
foreach ($group['piggy_banks'] as $piggy) {
$currencyId = $piggy['currency_id'];
$sums[$groupId][$currencyId] = $sums[$groupId][$currencyId] ?? [
'target' => '0',
'saved' => '0',
'left_to_save' => '0',
'save_per_month' => '0',
'currency_id' => $currencyId,
'currency_code' => $piggy['currency_code'],
'currency_symbol' => $piggy['currency_symbol'],
'currency_decimal_places' => $piggy['currency_decimal_places'],
];
// target_amount
// current_amount
// left_to_save
// save_per_month
$sums[$groupId][$currencyId]['target'] = bcadd($sums[$groupId][$currencyId]['target'], (string) $piggy['target_amount']);
$sums[$groupId][$currencyId]['saved'] = bcadd($sums[$groupId][$currencyId]['saved'], (string) $piggy['current_amount']);
$sums[$groupId][$currencyId]['left_to_save'] = bcadd($sums[$groupId][$currencyId]['left_to_save'], (string) $piggy['left_to_save']);
$sums[$groupId][$currencyId]['save_per_month'] = bcadd($sums[$groupId][$currencyId]['save_per_month'], (string) $piggy['save_per_month']);
}
}
foreach ($piggyBanks as $groupOrder => $group) {
$groupId = $group['object_group_id'];
$piggyBanks[$groupOrder]['sums'] = $sums[$groupId];
}
return $piggyBanks;
}
/**
* Set the order of a piggy bank.
*

View File

@ -5,6 +5,7 @@ namespace FireflyIII\Repositories\ObjectGroup;
use DB;
use FireflyIII\Models\ObjectGroup;
use FireflyIII\Models\PiggyBank;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@ -123,6 +124,12 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
*/
public function destroy(ObjectGroup $objectGroup): void
{
$list = $objectGroup->piggyBanks;
/** @var PiggyBank $piggy */
foreach($list as $piggy) {
$piggy->objectGroups()->sync([]);
$piggy->save();
}
$objectGroup->delete();
}

View File

@ -96,6 +96,36 @@
</td>
</tr>
{% endfor %}
<tr>
<td class="visible-xs visible-sm hidden-md hidden-lg">&nbsp;</td>
<td class="visible-xs visible-sm hidden-md hidden-lg">&nbsp;</td>
<td>&nbsp;</td> {# handle #}
<td class="hidden-sm hidden-xs">&nbsp;</td> {# buttons #}
<td>&nbsp;</td>
<td style="text-align: right;">
{% for sum in objectGroup.sums %}
{{ formatAmountBySymbol(sum.saved, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
<td class="hidden-sm hidden-xs">&nbsp;</td> {# remove money #}
<td class="hidden-sm hidden-xs">&nbsp;</td> {# progress#}
<td class="hidden-sm hidden-xs">&nbsp;</td> {# add money #}
<td class="hidden-sm hidden-xs" style="text-align: right;">
{% for sum in objectGroup.sums %}
{{ formatAmountBySymbol(sum.target, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
<td class="hidden-sm hidden-xs" style="text-align: right;">
{% for sum in objectGroup.sums %}
{{ formatAmountBySymbol(sum.left_to_save, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
<td class="hidden-sm hidden-xs" style="text-align: right;">
{% for sum in objectGroup.sums %}
{{ formatAmountBySymbol(sum.save_per_month, sum.currency_symbol, sum.currency_decimal_places) }}<br />
{% endfor %}
</td>
</tr>
</tbody>
{% endif %}
{% endfor %}