/** * @param BillRepositoryInterface $repository * * @param AccountRepositoryInterface $accountRepository * * @return \Symfony\Component\HttpFoundation\Response */ public function boxBillsPaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); // works for json too! $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('box-bills-paid'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $amount = 0; // these two functions are the same as the chart $bills = $repository->getActiveBills(); /** @var Bill $bill */ foreach ($bills as $bill) { $amount += $repository->billPaymentsInRange($bill, $start, $end); } unset($bill, $bills); /** * Find credit card accounts and possibly unpaid credit card bills. */ $creditCards = $accountRepository->getCreditCards(); // if the balance is not zero, the monthly payment is still underway. /** @var Account $creditCard */ foreach ($creditCards as $creditCard) { $balance = Steam::balance($creditCard, $end, true); if ($balance == 0) { // find a transfer TO the credit card which should account for // anything paid. If not, the CC is not yet used. $amount += $accountRepository->getTransfersInRange($creditCard, $start, $end)->sum('amount'); } } $data = ['box' => 'bills-paid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; $cache->store($data); return Response::json($data); }