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

This commit is contained in:
James Cole 2018-02-06 18:11:33 +01:00
parent 9a0672e359
commit c4507a7f75
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 78 additions and 43 deletions

View File

@ -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'));
}
/**

View File

@ -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.
*

View File

@ -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
*

View File

@ -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
*/

View File

@ -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 '';
}
}