/** * @return Twig_SimpleFunction */ public function formatAccountPerspective() : Twig_SimpleFunction { return new Twig_SimpleFunction('formatAccountPerspective', function (TransactionJournal $journal, Account $account) { $cache = new CacheProperties(); $cache->addProperty('formatAccountPerspective'); $cache->addProperty($journal->id); $cache->addProperty($account->id); if ($cache->has()) { return $cache->get(); } // get the account amount: $transactions = $journal->transactions()->where('transactions.account_id', $account->id)->get(['transactions.*']); $amount = '0'; foreach ($transactions as $transaction) { $amount = bcadd($amount, strval($transaction->amount)); } if ($journal->isTransfer()) { $amount = bcmul($amount, '-1'); } // check if this sum is the same as the journal: $journalSum = TransactionJournal::amount($journal); $full = Amount::formatJournal($journal); if (bccomp($journalSum, $amount) === 0 || bccomp(bcmul($journalSum, '-1'), $amount) === 0) { $cache->store($full); return $full; } $formatted = Amount::format($amount, true); if ($journal->isTransfer()) { $formatted = '<span class="text-info">' . Amount::format($amount) . '</span>'; } $str = $formatted . ' (' . $full . ')'; $cache->store($str); return $str; }); }
/** * Handle the event. * * @param TransactionJournalUpdated $event * @SuppressWarnings(PHPMD.CyclomaticComplexity) // it's exactly 5. * * @return bool */ public function handle(TransactionJournalUpdated $event) : bool { $journal = $event->journal; if (!$journal->isTransfer()) { return true; } // get the event connected to this journal: /** @var PiggyBankEvent $event */ $event = PiggyBankEvent::where('transaction_journal_id', $journal->id)->first(); if (is_null($event)) { return false; } $piggyBank = $event->piggyBank()->first(); $repetition = null; if (!is_null($piggyBank)) { /** @var PiggyBankRepetition $repetition */ $repetition = $piggyBank->piggyBankRepetitions()->relevantOnDate($journal->date)->first(); } if (is_null($repetition)) { return false; } $amount = TransactionJournal::amount($journal); $diff = bcsub($amount, $event->amount); // update current repetition $repetition->currentamount = bcadd($repetition->currentamount, $diff); $repetition->save(); $event->amount = $amount; $event->save(); return true; }
/** * @param TransactionJournal $journal * * @return Entry */ public static function fromJournal(TransactionJournal $journal) { $entry = new self(); $entry->description = $journal->description; $entry->date = $journal->date->format('Y-m-d'); $entry->amount = TransactionJournal::amount($journal); $entry->budget = new EntryBudget($journal->budgets->first()); $entry->category = new EntryCategory($journal->categories->first()); $entry->bill = new EntryBill($journal->bill); $sources = TransactionJournal::sourceAccountList($journal); $destinations = TransactionJournal::destinationAccountList($journal); $entry->sourceAccount = new EntryAccount($sources->first()); $entry->destinationAccount = new EntryAccount($destinations->first()); foreach ($sources as $source) { $entry->sourceAccounts->push(new EntryAccount($source)); } foreach ($destinations as $destination) { $entry->destinationAccounts->push(new EntryAccount($destination)); } return $entry; }
/** * * @param \FireflyIII\Models\TransactionJournal $journal * @param bool $coloured * * @return string */ public function formatJournal(TransactionJournal $journal, bool $coloured = true) : string { $locale = setlocale(LC_MONETARY, 0); $float = round(TransactionJournal::amount($journal), 2); $formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY); $currencyCode = $journal->transaction_currency_code ?? $journal->transactionCurrency->code; $result = $formatter->formatCurrency($float, $currencyCode); if ($coloured === true && $float === 0.0) { return '<span style="color:#999">' . $result . '</span>'; // always grey. } if (!$coloured) { return $result; } if (!$journal->isTransfer()) { if ($float > 0) { return '<span class="text-success">' . $result . '</span>'; } return '<span class="text-danger">' . $result . '</span>'; } else { return '<span class="text-info">' . $result . '</span>'; } }
/** * Get a full report on the users incomes during the period for the given accounts. * * @param Carbon $start * @param Carbon $end * @param Collection $accounts * * @return Income */ public function getIncomeReport(Carbon $start, Carbon $end, Collection $accounts) : Income { $object = new Income(); /** @var AccountRepositoryInterface $repos */ $repos = app(AccountRepositoryInterface::class); $journals = $repos->incomesInPeriod($accounts, $start, $end); foreach ($journals as $entry) { $amount = TransactionJournal::amount($entry); $object->addToTotal($amount); $object->addOrCreateIncome($entry); } return $object; }
/** * @param Tag $tag * * @return string */ public static function tagSum(Tag $tag) : string { $sum = '0'; /** @var TransactionJournal $journal */ foreach ($tag->transactionjournals as $journal) { bcadd($sum, TransactionJournal::amount($journal)); } return $sum; }
/** * @param BudgetRepositoryInterface $repository * @param Carbon $start * @param Carbon $end * * @return array */ private function spentInPeriodWithout(BudgetRepositoryInterface $repository, Carbon $start, Carbon $end) : array { $list = $repository->journalsInPeriodWithoutBudget(new Collection(), $start, $end); $sum = '0'; /** @var TransactionJournal $entry */ foreach ($list as $entry) { $sum = bcadd(TransactionJournal::amount($entry), $sum); } return [trans('firefly.no_budget'), '0', '0', $sum, '0', '0']; }
/** * @param Tag $tag * @param TagRepositoryInterface $repository * * @return View */ public function show(Tag $tag, TagRepositoryInterface $repository) { $subTitle = $tag->tag; $subTitleIcon = 'fa-tag'; $journals = $repository->getJournals($tag); $sum = $journals->sum(function (TransactionJournal $journal) { return TransactionJournal::amount($journal); }); return view('tags.show', compact('tag', 'subTitle', 'subTitleIcon', 'journals', 'sum')); }
/** * @return Twig_SimpleFunction */ private function getAmountFromJournal() { return new Twig_SimpleFunction('getAmount', function (TransactionJournal $journal) : string { return TransactionJournal::amount($journal); }); }