/** * 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); }
/** * Shows all bills and whether or not they've been paid this month (pie chart). * * @param BillRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BillRepositoryInterface $repository) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); $paid = $repository->getBillsPaidInRange($start, $end); // will be a negative amount. $unpaid = $repository->getBillsUnpaidInRange($start, $end); // will be a positive amount. $creditCardDue = $repository->getCreditCardBill($start, $end); if ($creditCardDue < 0) { // expenses are negative (bill not yet paid), $creditCardDue = bcmul($creditCardDue, '-1'); $unpaid = bcadd($unpaid, $creditCardDue); } else { // if more than zero, the bill has been paid: (transfer = positive). // amount must be negative to be added to $paid: $paid = bcadd($paid, $creditCardDue); } // build chart: $data = $this->generator->frontpage($paid, $unpaid); return Response::json($data); }
/** * Shows all bills and whether or not theyve been paid this month (pie chart). * * @param BillRepositoryInterface $repository * * @return \Symfony\Component\HttpFoundation\Response */ public function frontpage(BillRepositoryInterface $repository) { $start = Session::get('start', Carbon::now()->startOfMonth()); $end = Session::get('end', Carbon::now()->endOfMonth()); $cache = new CacheProperties(); // chart properties for cache: $cache->addProperty($start); $cache->addProperty($end); $cache->addProperty('bills'); $cache->addProperty('frontpage'); if ($cache->has()) { return Response::json($cache->get()); // @codeCoverageIgnore } $set = $repository->getBillsForChart($start, $end); // optionally expand this set with credit card data $set = $repository->getCreditCardInfoForChart($set, $start, $end); $paid = $set->get('paid'); $unpaid = $set->get('unpaid'); // build chart: $data = $this->generator->frontpage($paid, $unpaid); $cache->store($data); return Response::json($data); }