expenses = new Collection; } /** * @param TransactionJournal $entry */ public function addOrCreateExpense(TransactionJournal $entry) { $accountId = $entry->account_id; if (!$this->expenses->has($accountId)) { $newObject = new stdClass; $newObject->amount = strval(round($entry->amount, 2)); $newObject->name = $entry->name; $newObject->count = 1; $newObject->id = $accountId; $this->expenses->put($accountId, $newObject); } else { bcscale(2); $existing = $this->expenses->get($accountId); $existing->amount = bcadd($existing->amount, $entry->amount); $existing->count++; $this->expenses->put($accountId, $existing); } } /** * @param $add */ public function addToTotal($add) { $add = strval(round($add, 2)); bcscale(2); $this->total = bcadd($this->total, $add); } /** * @return Collection */ public function getExpenses() { $set = $this->expenses->sortByDesc( function (stdClass $object) { return $object->amount; } ); return $set; } /** * @return string */ public function getTotal() { return strval(round($this->total, 2)); } }