Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Better sum for bill view.
James Colee5e3797d9c71
diff --git a/app/Api/V1/Controllers/BillController.php b/app/Api/V1/Controllers/BillController.phpindex f31b7206c4..05ca73eb33 100644--- a/app/Api/V1/Controllers/BillController.php+++ b/app/Api/V1/Controllers/BillController.php@@ -38,7 +38,6 @@ use FireflyIII\User;use Illuminate\Http\JsonResponse;use Illuminate\Http\Request;use Illuminate\Pagination\LengthAwarePaginator;-use Illuminate\Support\Collection;use League\Fractal\Manager;use League\Fractal\Pagination\IlluminatePaginatorAdapter;use League\Fractal\Resource\Collection as FractalCollection;@@ -82,7 +81,7 @@ class BillController extends Controller* Display a listing of the resource.** @param Request $request- * @param Bill $bill+ * @param Bill $bill** @return JsonResponse* @codeCoverageIgnore@@ -141,11 +140,7 @@ class BillController extends Controller*/public function index(Request $request): JsonResponse{- $pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;- $paginator = $this->repository->getPaginator($pageSize);- /** @var Collection $bills */- $bills = $paginator->getCollection();-+ $bills = $this->repository->getBills();$manager = new Manager();$baseUrl = $request->getSchemeAndHttpHost() . '/api/v1';$manager->setSerializer(new JsonApiSerializer($baseUrl));@@ -164,7 +159,7 @@ class BillController extends Controller* List all of them.** @param Request $request- * @param Bill $bill+ * @param Bill $bill** @return JsonResponse* @codeCoverageIgnore@@ -206,7 +201,7 @@ class BillController extends Controller* Show the specified bill.** @param Request $request- * @param Bill $bill+ * @param Bill $bill** @return JsonResponse* @codeCoverageIgnore@@ -259,7 +254,7 @@ class BillController extends Controller** @param Request $request*- * @param Bill $bill+ * @param Bill $bill** @return JsonResponse* @codeCoverageIgnore@@ -318,7 +313,7 @@ class BillController extends Controller* Update a bill.** @param BillRequest $request- * @param Bill $bill+ * @param Bill $bill** @return JsonResponse*/diff --git a/app/Api/V1/Controllers/CurrencyController.php b/app/Api/V1/Controllers/CurrencyController.phpindex 67fbe22ac3..28a4f96a7f 100644--- a/app/Api/V1/Controllers/CurrencyController.php+++ b/app/Api/V1/Controllers/CurrencyController.php@@ -223,9 +223,7 @@ class CurrencyController extends Controller/** @var BillRepositoryInterface $repository */$repository = app(BillRepositoryInterface::class);$pageSize = (int)app('preferences')->getForUser(auth()->user(), 'listPageSize', 50)->data;- $paginator = $repository->getPaginator($pageSize);- /** @var Collection $bills */- $unfiltered = $paginator->getCollection();+ $unfiltered = $repository->getBills();// filter and paginate list:$collection = $unfiltered->filter(diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.phpindex 304aef10d9..51ee610ca0 100644--- a/app/Http/Controllers/BillController.php+++ b/app/Http/Controllers/BillController.php@@ -28,6 +28,7 @@ use FireflyIII\Helpers\Collector\GroupCollectorInterface;use FireflyIII\Http\Requests\BillFormRequest;use FireflyIII\Models\Attachment;use FireflyIII\Models\Bill;+use FireflyIII\Models\TransactionCurrency;use FireflyIII\Repositories\Bill\BillRepositoryInterface;use FireflyIII\TransactionRules\TransactionMatcher;use FireflyIII\Transformers\AttachmentTransformer;@@ -194,8 +195,7 @@ class BillController extends Controller{$start = session('start');$end = session('end');- $pageSize = (int)app('preferences')->get('listPageSize', 50)->data;- $paginator = $this->billRepository->getPaginator($pageSize);+ $unfiltered = $this->billRepository->getBills();$defaultCurrency = app('amount')->getDefaultCurrency();$parameters = new ParameterBag();$parameters->set('start', $start);@@ -206,28 +206,34 @@ class BillController extends Controller$transformer->setParameters($parameters);/** @var Collection $bills */- $bills = $paginator->getCollection()->map(+ $bills = $unfiltered->map(static function (Bill $bill) use ($transformer, $defaultCurrency) {- $return = $transformer->transform($bill);- $return['currency'] = $bill->transactionCurrency ?? $defaultCurrency;+ $return = $transformer->transform($bill);+ $currency = $bill->transactionCurrency ?? $defaultCurrency;+ $return['currency_id'] = $currency->id;+ $return['currency_name'] = $currency->name;+ $return['currency_symbol'] = $currency->symbol;+ $return['currency_code'] = $currency->code;+ $return['currency_decimal_places'] = $currency->decimal_places;return $return;});// add info about rules:- $rules = $this->billRepository->getRulesForBills($paginator->getCollection());+ $rules = $this->billRepository->getRulesForBills($unfiltered);$bills = $bills->map(- function (array $bill) use ($rules) {+ static function (array $bill) use ($rules) {$bill['rules'] = $rules[$bill['id']] ?? [];return $bill;});- $paginator->setPath(route('bills.index'));+ // summarise per currency:+ $sums = $this->getSums($bills);- return view('bills.index', compact('bills', 'paginator'));+ return view('bills.index', compact('bills', 'sums'));}/**@@ -413,4 +419,35 @@ class BillController extends Controllerreturn $redirect;}++ /**+ * @param Collection $bills+ *+ * @return array+ */+ private function getSums(Collection $bills): array+ {+ $sums = [];++ /** @var array $bill */+ foreach ($bills as $bill) {+ if (false === $bill['active']) {+ continue;+ }+ /** @var TransactionCurrency $currency */+ $currencyId = $bill['currency_id'];+ $sums[$currencyId] = $sums[$currencyId] ?? [+ 'currency_id' => $currencyId,+ 'currency_code' => $bill['currency_code'],+ 'currency_name' => $bill['currency_name'],+ 'currency_symbol' => $bill['currency_symbol'],+ 'currency_decimal_places' => $bill['currency_decimal_places'],+ 'avg' => '0',+ ];+ $avg = bcdiv(bcadd((string)$bill['amount_min'], (string)$bill['amount_max']), '2');+ $sums[$currencyId]['avg'] = bcadd($sums[$currencyId]['avg'], $avg);+ }++ return $sums;+ }}diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.phpindex ffaba94d46..17e09784ef 100644--- a/app/Repositories/Bill/BillRepository.php+++ b/app/Repositories/Bill/BillRepository.php@@ -172,9 +172,7 @@ class BillRepository implements BillRepositoryInterfacepublic function getBills(): Collection{/** @var Collection $set */- $set = $this->user->bills()->orderBy('active', 'DESC')->orderBy('name', 'ASC')->get();-- return $set;+ return $this->user->bills()->orderBy('active', 'DESC')->orderBy('name', 'ASC')->get();}/**diff --git a/resources/views/v1/list/bills.twig b/resources/views/v1/list/bills.twigindex aab57f54e4..6471a32c44 100644--- a/resources/views/v1/list/bills.twig+++ b/resources/views/v1/list/bills.twig@@ -14,18 +14,7 @@</tr></thead><tbody>- {% set sum_min =0 %}- {% set sum_max =0 %}- {% set expected_total = 0 %}- {% set count = 0 %}-{% for entry in bills %}- {% if entry.active %}- {% set count = count + 1 %}- {% set sum_min = sum_min + entry.amount_min %}- {% set sum_max = sum_max + entry.amount_max %}- {% set expected_total = expected_total + ((entry.amount_min + entry.amount_max) / 2) %}- {% endif %}<tr{% if not entry.active %} data-disablesort="true"{% endif %}><td class="hidden-sm hidden-xs"><div class="btn-group btn-group-xs edit_tr_buttons"><a href="{{ route('bills.edit',entry.id) }}" class="btn btn-default btn-xs"><i@@ -58,8 +47,10 @@{% endif %}</td><td data-value="{{ entry.amount_min }}" style="text-align: right;">- <span style="margin-right:5px;">- ~ {{ formatAmountByCurrency(entry.currency, (entry.amount_max + entry.amount_min)/2) }}+ <span style="margin-right:5px;"+ title="{{ formatAmountBySymbol(entry.amount_min, entry.currency_symbol, entry.currency_decimal_places, false)|escape }} -- {{ formatAmountBySymbol(entry.amount_max, entry.currency_symbol, entry.currency_decimal_places, false)|escape }}"+ >+ ~ {{ formatAmountBySymbol((entry.amount_max + entry.amount_min)/2, entry.currency_symbol, entry.currency_decimal_places) }}</span></td>@@ -125,53 +116,19 @@{% endfor %}</tbody><tfoot>- <tr>- <td style="text-align:right;" colspan="3">{{ 'sum'|_ }} ({{ 'active_bills_only'|_ }})</td>- <td style="text-align:right;" colspan="1">- <span style="margin-right:5px;">- {{ formatAmountBySymbol(sum_min,'¤') }}- </span>- </td>- <td style="text-align:right;" colspan="1">- <span style="margin-right:5px;">- {{ formatAmountBySymbol(sum_max,'¤') }}- </span>- </td>- <td colspan="5"> </td>- </tr>-- {# calculate total#}- {% if count > 0 %}- {% set avg_min = (sum_min / count) %}- {% set avg_max = (sum_max / count) %}- {% else %}- {% set avg_min = 0 %}- {% set avg_max = 0 %}- {% endif %}-- <tr>- <td style="text-align:right;" colspan="3">{{ 'average_per_bill'|_ }} ({{ 'active_bills_only'|_ }})</td>- <td style="text-align:right;" colspan="1">- <span style="margin-right:5px;">- {{ formatAmountBySymbol(avg_min,'¤') }}- </span>- </td>- <td style="text-align:right;" colspan="1">- <span style="margin-right:5px;">- {{ formatAmountBySymbol(avg_max,'¤') }}- </span>- </td>- <td colspan="6"> </td>- </tr>- <tr>- <td style="text-align:right;" colspan="3">{{ 'expected_total'|_ }} ({{ 'active_bills_only'|_ }})</td>- <td style="text-align:right;" colspan="1">+ {% for sum in sums %}+ <tr>+ <td style="text-align:right;" colspan="3">+ {{ 'sum'|_ }} ({{ sum.currency_name }}) ({{ 'active_bills_only'|_ }})<br />+ </td>+ <td style="text-align:right;" colspan="1"><span style="margin-right:5px;">- ~ {{ formatAmountBySymbol(expected_total,'¤') }}+ {{ formatAmountBySymbol(sum.avg, sum.currency_symbol, sum.currency_decimal_places) }}</span>- </td>- <td colspan="6"> </td>- </tr>+ </td>+ <td colspan="6"> </td>+ </tr>+ {% endfor %}</tfoot></table><div style="padding-left:8px;">