Skip to content

Firefly III: a personal finances manager

Make sure the "classic" page uses the transformer as well.

James Colec4507a7f7530
diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php
index 4591ebead3..afbbecf06f 100644
--- a/app/Http/Controllers/BillController.php
+++ b/app/Http/Controllers/BillController.php
@@ -30,8 +30,8 @@ use FireflyIII\Models\Bill;
use FireflyIII\Models\Note;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
+use FireflyIII\Transformers\Bill\BillTransformer;
use Illuminate\Http\Request;
-use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Preferences;
use URL;
@@ -167,36 +167,23 @@ class BillController extends Controller
*
* @return View
*/
- public function index(Request $request, BillRepositoryInterface $repository)
+ public function index(BillRepositoryInterface $repository)
{
- /** @var Carbon $start */
- $start = session('start');
- /** @var Carbon $end */
- $end = session('end');
- $page = 0 === intval($request->get('page')) ? 1 : intval($request->get('page'));
- $pageSize = intval(Preferences::get('listPageSize', 50)->data);
- $collection = $repository->getBills();
- $total = $collection->count();
- $collection = $collection->slice(($page - 1) * $pageSize, $pageSize);
-
- $collection->each(
- function (Bill $bill) use ($repository, $start, $end) {
- // paid in this period?
- $bill->paidDates = $repository->getPaidDatesInRange($bill, $start, $end);
- $bill->payDates = $repository->getPayDatesInRange($bill, $start, $end);
- $lastPaidDate = $this->lastPaidDate($repository->getPaidDatesInRange($bill, $start, $end), $start);
- if ($bill->paidDates->count() >= $bill->payDates->count()) {
- // if all bills have been been paid, jump to next period.
- $lastPaidDate = $end;
- }
- $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastPaidDate);
+ $start = session('start');
+ $end = session('end');
+ $pageSize = intval(Preferences::get('listPageSize', 50)->data);
+ $paginator = $repository->getPaginator($pageSize);
+ $transformer = new BillTransformer($start, $end);
+ /** @var Collection $bills */
+ $bills = $paginator->getCollection()->map(
+ function (Bill $bill) use ($transformer) {
+ return $transformer->transform($bill);
}
);
- // paginate bills
- $bills = new LengthAwarePaginator($collection, $total, $pageSize, $page);
- $bills->setPath(route('bills.index'));
- return view('bills.index', compact('bills'));
+ $paginator->setPath(route('bills.index'));
+
+ return view('bills.index', compact('bills', 'paginator'));
}
/**
@@ -235,15 +222,16 @@ class BillController extends Controller
*/
public function show(Request $request, BillRepositoryInterface $repository, Bill $bill)
{
- /** @var Carbon $date */
- $date = session('start');
- /** @var Carbon $end */
+ $subTitle = $bill->name;
+ $start = session('start');
$end = session('end');
- $year = $date->year;
+ $year = $start->year;
$page = intval($request->get('page'));
$pageSize = intval(Preferences::get('listPageSize', 50)->data);
- $yearAverage = $repository->getYearAverage($bill, $date);
+ $yearAverage = $repository->getYearAverage($bill, $start);
$overallAverage = $repository->getOverallAverage($bill);
+ $transformer = new BillTransformer($start, $end);
+ $object = $transformer->transform($bill);
// use collector:
/** @var JournalCollectorInterface $collector */
@@ -253,18 +241,8 @@ class BillController extends Controller
$transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('bills.show', [$bill->id]));
- $bill->paidDates = $repository->getPaidDatesInRange($bill, $date, $end);
- $bill->payDates = $repository->getPayDatesInRange($bill, $date, $end);
- $lastPaidDate = $this->lastPaidDate($repository->getPaidDatesInRange($bill, $date, $end), $date);
- if ($bill->paidDates->count() >= $bill->payDates->count()) {
- // if all bills have been been paid, jump to next period.
- $lastPaidDate = $end;
- }
- $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastPaidDate);
- $hideBill = true;
- $subTitle = $bill->name;
- return view('bills.show', compact('transactions', 'yearAverage', 'overallAverage', 'year', 'hideBill', 'bill', 'subTitle'));
+ return view('bills.show', compact('transactions', 'yearAverage', 'overallAverage', 'year', 'object','bill', 'subTitle'));
}
/**
diff --git a/app/Repositories/Bill/BillRepository.php b/app/Repositories/Bill/BillRepository.php
index bfa7500064..6341dba9fc 100644
--- a/app/Repositories/Bill/BillRepository.php
+++ b/app/Repositories/Bill/BillRepository.php
@@ -32,6 +32,7 @@ use FireflyIII\Models\TransactionType;
use FireflyIII\Support\CacheProperties;
use FireflyIII\User;
use Illuminate\Database\Query\JoinClause;
+use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
use Log;
use Navigation;
@@ -269,6 +270,16 @@ class BillRepository implements BillRepositoryInterface
return $avg;
}
+ /**
+ * @param int $size
+ *
+ * @return LengthAwarePaginator
+ */
+ public function getPaginator(int $size): LengthAwarePaginator
+ {
+ return $this->user->bills()->paginate($size);
+ }
+
/**
* The "paid dates" list is a list of dates of transaction journals that are linked to this bill.
*
diff --git a/app/Repositories/Bill/BillRepositoryInterface.php b/app/Repositories/Bill/BillRepositoryInterface.php
index 3a99b0fc62..dc421d2bd4 100644
--- a/app/Repositories/Bill/BillRepositoryInterface.php
+++ b/app/Repositories/Bill/BillRepositoryInterface.php
@@ -26,6 +26,7 @@ use Carbon\Carbon;
use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal;
use FireflyIII\User;
+use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
/**
@@ -33,6 +34,13 @@ use Illuminate\Support\Collection;
*/
interface BillRepositoryInterface
{
+
+ /**
+ * @param int $size
+ *
+ * @return LengthAwarePaginator
+ */
+ public function getPaginator(int $size): LengthAwarePaginator;
/**
* @param Bill $bill
*
diff --git a/app/Support/Twig/General.php b/app/Support/Twig/General.php
index d3387e31fd..db60f2e41a 100644
--- a/app/Support/Twig/General.php
+++ b/app/Support/Twig/General.php
@@ -65,6 +65,7 @@ class General extends Twig_Extension
$this->steamPositive(),
$this->activeRoutePartial(),
$this->activeRoutePartialWhat(),
+ $this->formatDate(),
];
}
@@ -179,6 +180,21 @@ class General extends Twig_Extension
);
}
+ /**
+ * @return Twig_SimpleFunction
+ */
+ protected function formatDate()
+ {
+ return new Twig_SimpleFunction(
+ 'formatDate',
+ function (string $date, string $format): string {
+ $carbon = new Carbon($date);
+
+ return $carbon->formatLocalized($format);
+ }
+ );
+ }
+
/**
* @return Twig_SimpleFilter
*/
diff --git a/app/Transformers/Bill/BillTransformer.php b/app/Transformers/Bill/BillTransformer.php
index 4431912e2f..53ffb8c4d7 100644
--- a/app/Transformers/Bill/BillTransformer.php
+++ b/app/Transformers/Bill/BillTransformer.php
@@ -25,6 +25,7 @@ namespace FireflyIII\Transformers\Bill;
use Carbon\Carbon;
use FireflyIII\Models\Bill;
+use FireflyIII\Models\Note;
use FireflyIII\Repositories\Bill\BillRepositoryInterface;
use Illuminate\Support\Collection;
use League\Fractal\TransformerAbstract;
@@ -74,6 +75,7 @@ class BillTransformer extends TransformerAbstract
'pay_dates' => $this->payDates($bill),
'paid_dates' => $paidData['paid_dates'],
'next_expected_match' => $paidData['next_expected_match'],
+ 'notes' => $this->getNote($bill),
'links' => [
[
'rel' => 'self',
@@ -82,6 +84,8 @@ class BillTransformer extends TransformerAbstract
],
];
+ // todo: attachments, journals, notes
+
return $data;
@@ -137,6 +141,13 @@ class BillTransformer extends TransformerAbstract
*/
protected function paidData(Bill $bill): array
{
+ if (is_null($this->start) || is_null($this->end)) {
+ return [
+ 'paid_dates' => [],
+ 'next_expected_match' => null,
+ ];
+ }
+
/** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class);
$repository->setUser($bill->user);
@@ -193,4 +204,15 @@ class BillTransformer extends TransformerAbstract
return $simple->toArray();
}
+
+ private function getNote($bill): string
+ {
+ /** @var Note $note */
+ $note = $bill->notes()->first();
+ if (!is_null($note)) {
+ return $note->text;
+ }
+
+ return '';
+ }
}
\ No newline at end of file