Updates to bill code for #1029

This commit is contained in:
James Cole 2017-12-02 07:10:36 +01:00
parent 8eded63055
commit efaa69cba1
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 112 additions and 25 deletions

View File

@ -42,9 +42,39 @@ class BillLine
protected $min; protected $min;
/** @var Carbon */ /** @var Carbon */
private $lastHitDate; private $lastHitDate;
/** @var Carbon */
private $payDate;
/** @var Carbon */
private $endOfPayDate;
/** @var int */ /** @var int */
private $transactionJournalId; private $transactionJournalId;
/**
* @return Carbon
*/
public function getPayDate(): Carbon
{
return $this->payDate;
}
/**
* @return Carbon
*/
public function getEndOfPayDate(): Carbon
{
return $this->endOfPayDate;
}
/**
* @param Carbon $endOfPayDate
*/
public function setEndOfPayDate(Carbon $endOfPayDate): void
{
$this->endOfPayDate = $endOfPayDate;
}
/** /**
* BillLine constructor. * BillLine constructor.
*/ */
@ -172,4 +202,12 @@ class BillLine
{ {
$this->hit = $hit; $this->hit = $hit;
} }
/**
* @param Carbon $payDate
*/
public function setPayDate(Carbon $payDate): void
{
$this->payDate = $payDate;
}
} }

View File

@ -71,34 +71,43 @@ class ReportHelper implements ReportHelperInterface
/** @var BillRepositoryInterface $repository */ /** @var BillRepositoryInterface $repository */
$repository = app(BillRepositoryInterface::class); $repository = app(BillRepositoryInterface::class);
$bills = $repository->getBillsForAccounts($accounts); $bills = $repository->getBillsForAccounts($accounts);
$collector = app(JournalCollectorInterface::class);
$collector->setAccounts($accounts)->setRange($start, $end)->setBills($bills);
$journals = $collector->getJournals();
$collection = new BillCollection; $collection = new BillCollection;
$collection->setStartDate($start); $collection->setStartDate($start);
$collection->setEndDate($end); $collection->setEndDate($end);
/** @var Bill $bill */ /** @var Bill $bill */
foreach ($bills as $bill) { foreach ($bills as $bill) {
$billLine = new BillLine; $expectedDates = $repository->getPayDatesInRange($bill, $start, $end);
$billLine->setBill($bill); foreach($expectedDates as $payDate) {
$billLine->setMin(strval($bill->amount_min)); $endOfPayPeriod = app('navigation')->endOfX($payDate, $bill->repeat_freq, null);
$billLine->setMax(strval($bill->amount_max));
$billLine->setHit(false); $collector = app(JournalCollectorInterface::class);
$entry = $journals->filter( $collector->setAccounts($accounts)->setRange($payDate, $endOfPayPeriod)->setBills($bills);
function (Transaction $transaction) use ($bill) { $journals = $collector->getJournals();
return $transaction->bill_id === $bill->id;
$billLine = new BillLine;
$billLine->setBill($bill);
$billLine->setPayDate($payDate);
$billLine->setEndOfPayDate($endOfPayPeriod);
$billLine->setMin(strval($bill->amount_min));
$billLine->setMax(strval($bill->amount_max));
$billLine->setHit(false);
$entry = $journals->filter(
function (Transaction $transaction) use ($bill) {
return $transaction->bill_id === $bill->id;
}
);
$first = $entry->first();
if (null !== $first) {
$billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount);
$billLine->setLastHitDate($first->date);
$billLine->setHit(true);
}
if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine);
} }
);
$first = $entry->first();
if (null !== $first) {
$billLine->setTransactionJournalId($first->id);
$billLine->setAmount($first->transaction_amount);
$billLine->setLastHitDate($first->date);
$billLine->setHit(true);
}
if ($billLine->isActive() || $billLine->isHit()) {
$collection->addBill($billLine);
} }
} }
$collection->filterBills(); $collection->filterBills();

View File

@ -186,11 +186,12 @@ class BillController extends Controller
// paid in this period? // paid in this period?
$bill->paidDates = $repository->getPaidDatesInRange($bill, $start, $end); $bill->paidDates = $repository->getPaidDatesInRange($bill, $start, $end);
$bill->payDates = $repository->getPayDatesInRange($bill, $start, $end); $bill->payDates = $repository->getPayDatesInRange($bill, $start, $end);
$lastDate = clone $start; $lastPaidDate = $this->lastPaidDate($repository->getPaidDatesInRange($bill, $start, $end), $start);
if ($bill->paidDates->count() >= $bill->payDates->count()) { if ($bill->paidDates->count() >= $bill->payDates->count()) {
$lastDate = $end; // if all bills have been been paid, jump to next period.
$lastPaidDate = $end;
} }
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastDate); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, $lastPaidDate);
} }
); );
@ -235,6 +236,8 @@ class BillController extends Controller
{ {
/** @var Carbon $date */ /** @var Carbon $date */
$date = session('start'); $date = session('start');
/** @var Carbon $end */
$end = session('end');
$year = $date->year; $year = $date->year;
$page = intval($request->get('page')); $page = intval($request->get('page'));
$pageSize = intval(Preferences::get('transactionPageSize', 50)->data); $pageSize = intval(Preferences::get('transactionPageSize', 50)->data);
@ -249,7 +252,15 @@ class BillController extends Controller
$transactions = $collector->getPaginatedJournals(); $transactions = $collector->getPaginatedJournals();
$transactions->setPath(route('bills.show', [$bill->id])); $transactions->setPath(route('bills.show', [$bill->id]));
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill, new Carbon);
$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; $hideBill = true;
$subTitle = e($bill->name); $subTitle = e($bill->name);
@ -325,4 +336,29 @@ class BillController extends Controller
return redirect($this->getPreviousUri('bills.edit.uri')); return redirect($this->getPreviousUri('bills.edit.uri'));
} }
/**
* Returns the latest date in the set, or start when set is empty.
*
* @param Collection $dates
* @param Carbon $default
*
* @return Carbon
*/
private function lastPaidDate(Collection $dates, Carbon $default): Carbon
{
if ($dates->count() === 0) {
return $default;
}
$latest = $dates->first();
/** @var Carbon $date */
foreach ($dates as $date) {
if ($date->gte($latest)) {
$latest = $date;
}
}
return $latest;
}
} }

View File

@ -589,6 +589,7 @@ return [
'not_or_not_yet' => 'Not (yet)', 'not_or_not_yet' => 'Not (yet)',
'not_expected_period' => 'Not expected this period', 'not_expected_period' => 'Not expected this period',
'bill_is_active' => 'Bill is active', 'bill_is_active' => 'Bill is active',
'bill_expected_between' => 'Expeced between :start and :end',
'bill_will_automatch' => 'Bill will automatically linked to matching transactions', 'bill_will_automatch' => 'Bill will automatically linked to matching transactions',
'skips_over' => 'skips over', 'skips_over' => 'skips over',

View File

@ -18,6 +18,9 @@
<tr> <tr>
<td data-value="{{ line.getBill.name }}"> <td data-value="{{ line.getBill.name }}">
<a href="{{ route('bills.show',line.getBill.id) }}">{{ line.getBill.name }}</a> <a href="{{ route('bills.show',line.getBill.id) }}">{{ line.getBill.name }}</a>
<small class="text-muted"><br />
{{ trans('firefly.bill_expected_between', {start: line.getPayDate.formatLocalized(monthAndDayFormat), end: line.getEndOfPayDate.formatLocalized(monthAndDayFormat) }) }}
</small>
</td> </td>
<td class="hidden-xs" data-value="{{ line.getMin }}" style="text-align: right;">{{ line.getMin|formatAmount }}</td> <td class="hidden-xs" data-value="{{ line.getMin }}" style="text-align: right;">{{ line.getMin|formatAmount }}</td>
<td class="hidden-xs" data-value="{{ line.getMax }}" style="text-align: right;">{{ line.getMax|formatAmount }}</td> <td class="hidden-xs" data-value="{{ line.getMax }}" style="text-align: right;">{{ line.getMax|formatAmount }}</td>