/** * Shows all bills and whether or not theyve been paid this month (pie chart). * * @param BillRepositoryInterface $repository * @param AccountRepositoryInterface $accounts * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BillRepositoryInterface $repository, AccountRepositoryInterface $accounts) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); // chart properties for cache: $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('bills'); $cache->addProperty('frontpage'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $bills = $repository->getActiveBills(); $paid = new Collection(); // journals. $unpaid = new Collection(); // bills /** @var Bill $bill */ foreach ($bills as $bill) { $ranges = $repository->getRanges($bill, $start, $end); foreach ($ranges as $range) { // paid a bill in this range? $journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']); if ($journals->count() == 0) { $unpaid->push([$bill, $range['start']]); } else { $paid = $paid->merge($journals); } } } $creditCards = $accounts->getCreditCards(); foreach ($creditCards as $creditCard) { $balance = Steam::balance($creditCard, $end, true); $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate')); if ($balance < 0) { // unpaid! create a fake bill that matches the amount. $description = $creditCard->name; $amount = $balance * -1; $fakeBill = $repository->createFakeBill($description, $date, $amount); unset($description, $amount); $unpaid->push([$fakeBill, $date]); } if ($balance == 0) { // find transfer(s) TO the credit card which should account for // anything paid. If not, the CC is not yet used. $journals = $accounts->getTransfersInRange($creditCard, $start, $end); $paid = $paid->merge($journals); } } // build chart: $data = $this->generator->frontpage($paid, $unpaid); $cache->store($data); return Response::json($data); }
/** * @param BillRepositoryInterface $repository * @param AccountRepositoryInterface $accountRepository * * @return \Symfony\Component\HttpFoundation\Response */ public function boxBillsUnpaid(BillRepositoryInterface $repository, AccountRepositoryInterface $accountRepository) { $amount = 0; $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); bcscale(2); // works for json too! $cache = new CacheProperties(); $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('box-bills-unpaid'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $bills = $repository->getActiveBills(); $unpaid = new Collection(); // bills /** @var Bill $bill */ foreach ($bills as $bill) { $ranges = $repository->getRanges($bill, $start, $end); foreach ($ranges as $range) { $journals = $repository->getJournalsInRange($bill, $range['start'], $range['end']); if ($journals->count() == 0) { $unpaid->push([$bill, $range['start']]); } } } unset($bill, $bills, $range, $ranges); $creditCards = $accountRepository->getCreditCards(); foreach ($creditCards as $creditCard) { $balance = Steam::balance($creditCard, $end, true); $date = new Carbon($creditCard->getMeta('ccMonthlyPaymentDate')); if ($balance < 0) { // unpaid! create a fake bill that matches the amount. $description = $creditCard->name; $fakeAmount = $balance * -1; $fakeBill = $repository->createFakeBill($description, $date, $fakeAmount); $unpaid->push([$fakeBill, $date]); } } /** @var Bill $entry */ foreach ($unpaid as $entry) { $current = bcdiv(bcadd($entry[0]->amount_max, $entry[0]->amount_min), 2); $amount = bcadd($amount, $current); } $data = ['box' => 'bills-unpaid', 'amount' => Amount::format($amount, false), 'amount_raw' => $amount]; $cache->store($data); return Response::json($data); }
/** * @param BillRepositoryInterface $repository * * @return View */ public function index(BillRepositoryInterface $repository) { /** @var Carbon $start */ $start = session('start'); /** @var Carbon $end */ $end = session('end'); $bills = $repository->getBills(); $bills->each(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; }); return view('bills.index', compact('bills')); }