Show bill average.

This commit is contained in:
James Cole
2016-07-05 08:57:45 +02:00
parent e8e7ab01d2
commit f1e8d1cf1e
5 changed files with 83 additions and 4 deletions

View File

@@ -11,6 +11,7 @@ declare(strict_types = 1);
namespace FireflyIII\Http\Controllers; namespace FireflyIII\Http\Controllers;
use Carbon\Carbon;
use FireflyIII\Http\Requests\BillFormRequest; use FireflyIII\Http\Requests\BillFormRequest;
use FireflyIII\Models\Bill; use FireflyIII\Models\Bill;
use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionJournal;
@@ -190,15 +191,20 @@ class BillController extends Controller
*/ */
public function show(BillRepositoryInterface $repository, Bill $bill) public function show(BillRepositoryInterface $repository, Bill $bill)
{ {
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page')); /** @var Carbon $date */
$pageSize = Preferences::get('transactionPageSize', 50)->data; $date = session('start');
$journals = $repository->getJournals($bill, $page, $pageSize); $year = $date->year;
$page = intval(Input::get('page')) == 0 ? 1 : intval(Input::get('page'));
$pageSize = Preferences::get('transactionPageSize', 50)->data;
$journals = $repository->getJournals($bill, $page, $pageSize);
$yearAverage = $repository->getYearAverage($bill, $date);
$overallAverage = $repository->getOverallAverage($bill);
$journals->setPath('/bills/show/' . $bill->id); $journals->setPath('/bills/show/' . $bill->id);
$bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill);
$hideBill = true; $hideBill = true;
$subTitle = e($bill->name); $subTitle = e($bill->name);
return view('bills.show', compact('journals', 'hideBill', 'bill', 'subTitle')); return view('bills.show', compact('journals', 'yearAverage', 'overallAverage', 'year', 'hideBill', 'bill', 'subTitle'));
} }
/** /**

View File

@@ -557,4 +557,52 @@ class BillRepository implements BillRepositoryInterface
return $wordMatch; return $wordMatch;
} }
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string
{
$journals = $bill->transactionjournals()
->where('date', '>=', $date->year . '-01-01')
->where('date', '<=', $date->year . '-12-31')
->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string
{
$journals = $bill->transactionjournals()->get();
$sum = '0';
$count = strval($journals->count());
/** @var TransactionJournal $journal */
foreach ($journals as $journal) {
$sum = bcadd($sum, TransactionJournal::amountPositive($journal));
}
$avg = '0';
if ($journals->count() > 0) {
$avg = bcdiv($sum, $count);
}
return $avg;
}
} }

View File

@@ -32,6 +32,21 @@ interface BillRepositoryInterface
*/ */
public function destroy(Bill $bill): bool; public function destroy(Bill $bill): bool;
/**
* @param Bill $bill
* @param Carbon $date
*
* @return string
*/
public function getYearAverage(Bill $bill, Carbon $date): string;
/**
* @param $bill
*
* @return string
*/
public function getOverallAverage($bill): string;
/** /**
* Find a bill by ID. * Find a bill by ID.
* *

View File

@@ -415,6 +415,8 @@ return [
'cannot_scan_inactive_bill' => 'Inactive bills cannot be scanned.', 'cannot_scan_inactive_bill' => 'Inactive bills cannot be scanned.',
'rescanned_bill' => 'Rescanned everything.', 'rescanned_bill' => 'Rescanned everything.',
'bill_date_little_relevance' => 'The only part of this date used by Firefly is the day. It is only useful when your bill arrives at exactly the same date every month. If the payment date of your bills varies, simply use the first of the month.', 'bill_date_little_relevance' => 'The only part of this date used by Firefly is the day. It is only useful when your bill arrives at exactly the same date every month. If the payment date of your bills varies, simply use the first of the month.',
'average_bill_amount_year' => 'Average bill amount (:year)',
'average_bill_amount_overall' => 'Average bill amount (overall)',
// accounts: // accounts:
'details_for_asset' => 'Details for asset account ":name"', 'details_for_asset' => 'Details for asset account ":name"',

View File

@@ -60,6 +60,14 @@
{% endif %} {% endif %}
</td> </td>
</tr> </tr>
<tr>
<td>{{ trans('firefly.average_bill_amount_year', {year: year}) }}</td>
<td>{{ '0'|formatAmount }}</td>
</tr>
<tr>
<td>{{ 'average_bill_amount_overall'|_ }}</td>
<td>{{ '0'|formatAmount }}</td>
</tr>
</table> </table>
</div> </div>
</div> </div>