diff --git a/app/Http/Controllers/BillController.php b/app/Http/Controllers/BillController.php index a634c6aa46..87be25b6e6 100644 --- a/app/Http/Controllers/BillController.php +++ b/app/Http/Controllers/BillController.php @@ -110,11 +110,29 @@ class BillController extends Controller */ public function index(BillRepositoryInterface $repository) { + $start = session('start'); + $end = session('end'); + $bills = $repository->getBills(); $bills->each( - function (Bill $bill) use ($repository) { + function (Bill $bill) use ($repository, $start, $end) { $bill->nextExpectedMatch = $repository->nextExpectedMatch($bill); $bill->lastFoundMatch = $repository->lastFoundMatch($bill); + $journals = $repository->getJournalsInRange($bill, $start, $end); + // loop journals, find average: + $average = '0'; + $count = $journals->count(); + if ($count > 0) { + $sum = '0'; + foreach ($journals as $journal) { + $sum = bcadd($sum, TransactionJournal::amountPositive($journal)); + } + $average = bcdiv($sum, strval($count)); + } + + $bill->lastPaidAmount = $average; + $bill->paidInPeriod = ($start <= $bill->lastFoundMatch) && ($end >= $bill->lastFoundMatch); + } ); diff --git a/app/Models/Bill.php b/app/Models/Bill.php index 167ea38842..37cfdc5a6f 100644 --- a/app/Models/Bill.php +++ b/app/Models/Bill.php @@ -27,6 +27,8 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; * @property-read \FireflyIII\User $user * @property \Carbon\Carbon $nextExpectedMatch * @property \Carbon\Carbon $lastFoundMatch + * @property bool $paidInPeriod + * @property string $lastPaidAmount * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Bill whereId($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Bill whereCreatedAt($value) * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Bill whereUpdatedAt($value) @@ -47,11 +49,25 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; class Bill extends Model { + protected $dates = ['created_at', 'updated_at', 'date']; protected $fillable = ['name', 'match', 'amount_min', 'match_encrypted', 'name_encrypted', 'user_id', 'amount_max', 'date', 'repeat_freq', 'skip', 'automatch', 'active',]; - protected $hidden = ['amount_min_encrypted', 'amount_max_encrypted', 'name_encrypted', 'match_encrypted']; - protected $dates = ['created_at', 'updated_at', 'date']; + + /** + * @param Bill $value + * + * @return Bill + */ + public static function routeBinder(Bill $value) + { + if (Auth::check()) { + if ($value->user_id == Auth::user()->id) { + return $value; + } + } + throw new NotFoundHttpException; + } /** * @param $value @@ -134,20 +150,4 @@ class Bill extends Model } - /** - * @param Bill $value - * - * @return Bill - */ - public static function routeBinder(Bill $value) - { - if (Auth::check()) { - if ($value->user_id == Auth::user()->id) { - return $value; - } - } - throw new NotFoundHttpException; - } - - } diff --git a/resources/lang/en_US/list.php b/resources/lang/en_US/list.php index 29ebb0b6ca..8cab261e04 100644 --- a/resources/lang/en_US/list.php +++ b/resources/lang/en_US/list.php @@ -8,34 +8,35 @@ */ return [ - 'name' => 'Name', - 'role' => 'Role', - 'currentBalance' => 'Current balance', - 'active' => 'Is active?', - 'lastActivity' => 'Last activity', - 'balanceDiff' => 'Balance difference between :start and :end', - 'matchedOn' => 'Matched on', - 'matchesOn' => 'Matched on', - 'matchingAmount' => 'Amount', - 'lastMatch' => 'Last match', - 'expectedMatch' => 'Expected match', - 'automatch' => 'Auto match?', - 'repeat_freq' => 'Repeats', - 'description' => 'Description', - 'amount' => 'Amount', - 'date' => 'Date', - 'interest_date' => 'Interest date', - 'book_date' => 'Book date', - 'process_date' => 'Processing date', - 'from' => 'From', - 'to' => 'To', - 'budget' => 'Budget', - 'category' => 'Category', - 'bill' => 'Bill', - 'withdrawal' => 'Withdrawal', - 'deposit' => 'Deposit', - 'transfer' => 'Transfer', - 'type' => 'Type', - 'completed' => 'Completed', - 'iban' => 'IBAN', + 'name' => 'Name', + 'role' => 'Role', + 'currentBalance' => 'Current balance', + 'active' => 'Is active?', + 'lastActivity' => 'Last activity', + 'balanceDiff' => 'Balance difference between :start and :end', + 'matchedOn' => 'Matched on', + 'matchesOn' => 'Matched on', + 'matchingAmount' => 'Amount', + 'lastMatch' => 'Last match', + 'expectedMatch' => 'Expected match', + 'automatch' => 'Auto match?', + 'repeat_freq' => 'Repeats', + 'description' => 'Description', + 'amount' => 'Amount', + 'date' => 'Date', + 'interest_date' => 'Interest date', + 'book_date' => 'Book date', + 'process_date' => 'Processing date', + 'from' => 'From', + 'to' => 'To', + 'budget' => 'Budget', + 'category' => 'Category', + 'bill' => 'Bill', + 'withdrawal' => 'Withdrawal', + 'deposit' => 'Deposit', + 'transfer' => 'Transfer', + 'type' => 'Type', + 'completed' => 'Completed', + 'iban' => 'IBAN', + 'paid_current_period' => 'Paid this period', ]; diff --git a/resources/views/list/bills.twig b/resources/views/list/bills.twig index c5c0b7fa57..380878a14e 100644 --- a/resources/views/list/bills.twig +++ b/resources/views/list/bills.twig @@ -5,7 +5,7 @@