/**
  * @param TransactionJournal $journal
  *
  * @return string
  * @throws FireflyException
  */
 public static function amount(TransactionJournal $journal) : string
 {
     $cache = new CacheProperties();
     $cache->addProperty($journal->id);
     $cache->addProperty('transaction-journal');
     $cache->addProperty('amount');
     if ($cache->has()) {
         return $cache->get();
     }
     // saves on queries:
     $amount = $journal->transactions()->where('amount', '>', 0)->get()->sum('amount');
     if ($journal->isWithdrawal()) {
         $amount = $amount * -1;
     }
     $amount = strval($amount);
     $cache->store($amount);
     return $amount;
 }
예제 #2
0
 /**
  * @param Bill               $bill
  * @param TransactionJournal $journal
  *
  * @return boolean|null
  */
 public function scan(Bill $bill, TransactionJournal $journal)
 {
     /*
      * Can only support withdrawals.
      */
     if (false === $journal->isWithdrawal()) {
         return false;
     }
     $matches = explode(',', $bill->match);
     $description = strtolower($journal->description) . ' ' . strtolower($journal->destination_account->name);
     $wordMatch = $this->doWordMatch($matches, $description);
     $amountMatch = $this->doAmountMatch($journal->amount_positive, $bill->amount_min, $bill->amount_max);
     Log::debug('Journal #' . $journal->id . ' has description "' . $description . '"');
     /*
      * If both, update!
      */
     if ($wordMatch && $amountMatch) {
         $journal->bill()->associate($bill);
         $journal->save();
         return true;
     } else {
         Log::debug('Wordmatch: ' . ($wordMatch ? 'true' : 'false') . ' AmountMatch: ' . ($amountMatch ? 'true' : 'false'));
     }
     if ($bill->id == $journal->bill_id) {
         // if no match, but bill used to match, remove it:
         $journal->bill_id = null;
         $journal->save();
         return true;
     }
     return false;
 }
예제 #3
0
 /**
  * Shows the view to edit a transaction.
  *
  * @param ARI                $repository
  * @param TransactionJournal $journal
  *
  * @return $this
  */
 public function edit(ARI $repository, TransactionJournal $journal)
 {
     // cannot edit opening balance
     if ($journal->isOpeningBalance()) {
         return view('error')->with('message', 'Cannot edit this transaction. Edit the account instead!');
     }
     $maxFileSize = Steam::phpBytes(ini_get('upload_max_filesize'));
     $maxPostSize = Steam::phpBytes(ini_get('post_max_size'));
     $uploadSize = min($maxFileSize, $maxPostSize);
     $what = strtolower($journal->getTransactionType());
     $accounts = ExpandedForm::makeSelectList($repository->getAccounts(['Default account', 'Asset account']));
     $budgets = ExpandedForm::makeSelectList(Auth::user()->budgets()->get());
     $budgets[0] = trans('form.noBudget');
     $piggies = ExpandedForm::makeSelectList(Auth::user()->piggyBanks()->get());
     $piggies[0] = trans('form.noPiggybank');
     $subTitle = trans('breadcrumbs.edit_journal', ['description' => $journal->description]);
     $preFilled = ['date' => $journal->date->format('Y-m-d'), 'category' => '', 'budget_id' => 0, 'piggy_bank_id' => 0];
     // get tags:
     $preFilled['tags'] = join(',', $journal->tags->pluck('tag')->toArray());
     $category = $journal->categories()->first();
     if (!is_null($category)) {
         $preFilled['category'] = $category->name;
     }
     $budget = $journal->budgets()->first();
     if (!is_null($budget)) {
         $preFilled['budget_id'] = $budget->id;
     }
     if ($journal->piggyBankEvents()->count() > 0) {
         $preFilled['piggy_bank_id'] = $journal->piggyBankEvents()->orderBy('date', 'DESC')->first()->piggy_bank_id;
     }
     $preFilled['amount'] = $journal->amount_positive;
     if ($journal->isWithdrawal()) {
         $preFilled['account_id'] = $journal->source_account->id;
         $preFilled['expense_account'] = $journal->destination_account->name_for_editform;
     } else {
         $preFilled['account_id'] = $journal->destination_account->id;
         $preFilled['revenue_account'] = $journal->source_account->name_for_editform;
     }
     $preFilled['account_from_id'] = $journal->source_account->id;
     $preFilled['account_to_id'] = $journal->destination_account->id;
     Session::flash('preFilled', $preFilled);
     Session::flash('gaEventCategory', 'transactions');
     Session::flash('gaEventAction', 'edit-' . $what);
     // put previous url in session if not redirect from store (not "return_to_edit").
     if (Session::get('transactions.edit.fromUpdate') !== true) {
         Session::put('transactions.edit.url', URL::previous());
     }
     Session::forget('transactions.edit.fromUpdate');
     return view('transactions.edit', compact('journal', 'uploadSize', 'accounts', 'what', 'budgets', 'piggies', 'subTitle'))->with('data', $preFilled);
 }
예제 #4
0
 /**
  * @param Bill               $bill
  * @param TransactionJournal $journal
  *
  * @return bool
  */
 public function scan(Bill $bill, TransactionJournal $journal) : bool
 {
     /*
      * Can only support withdrawals.
      */
     if (false === $journal->isWithdrawal()) {
         return false;
     }
     $destinationAccounts = TransactionJournal::destinationAccountList($journal);
     $sourceAccounts = TransactionJournal::sourceAccountList($journal);
     $matches = explode(',', $bill->match);
     $description = strtolower($journal->description) . ' ';
     $description .= strtolower(join(' ', $destinationAccounts->pluck('name')->toArray()));
     $description .= strtolower(join(' ', $sourceAccounts->pluck('name')->toArray()));
     $wordMatch = $this->doWordMatch($matches, $description);
     $amountMatch = $this->doAmountMatch(TransactionJournal::amountPositive($journal), $bill->amount_min, $bill->amount_max);
     /*
      * If both, update!
      */
     if ($wordMatch && $amountMatch) {
         $journal->bill()->associate($bill);
         $journal->save();
         return true;
     }
     if ($bill->id == $journal->bill_id) {
         // if no match, but bill used to match, remove it:
         $journal->bill_id = null;
         $journal->save();
         return true;
     }
     return false;
 }