/**
  * This method will search the user's transaction journal (with an upper limit of $range) for
  * transaction journals matching the given $triggers. This is accomplished by trying to fire these
  * triggers onto each transaction journal until enough matches are found ($limit).
  *
  * @return Collection
  *
  */
 public function findMatchingTransactions() : Collection
 {
     if (count($this->triggers) === 0) {
         return new Collection();
     }
     $pagesize = min($this->range / 2, $this->limit * 2);
     // Variables used within the loop
     $processed = 0;
     $page = 1;
     $result = new Collection();
     $processor = Processor::makeFromStringArray($this->triggers);
     // Start a loop to fetch batches of transactions. The loop will finish if:
     //   - all transactions have been fetched from the database
     //   - the maximum number of transactions to return has been found
     //   - the maximum number of transactions to search in have been searched
     do {
         // Fetch a batch of transactions from the database
         $paginator = $this->repository->getJournals($this->transactionTypes, $page, $pagesize);
         $set = $paginator->getCollection();
         // Filter transactions that match the given triggers.
         $filtered = $set->filter(function (TransactionJournal $journal) use($processor) {
             Log::debug(sprintf('Test these triggers on #%d', $journal->id));
             return $processor->handleTransactionJournal($journal);
         });
         // merge:
         /** @var Collection $result */
         $result = $result->merge($filtered);
         // Update counters
         $page++;
         $processed += count($set);
         // Check for conditions to finish the loop
         $reachedEndOfList = $set->count() < $pagesize;
         $foundEnough = $result->count() >= $this->limit;
         $searchedEnough = $processed >= $this->range;
     } while (!$reachedEndOfList && !$foundEnough && !$searchedEnough);
     // If the list of matchingTransactions is larger than the maximum number of results
     // (e.g. if a large percentage of the transactions match), truncate the list
     $result = $result->slice(0, $this->limit);
     return $result;
 }
 /**
  * @param JournalFormRequest         $request
  * @param JournalRepositoryInterface $repository
  * @param AttachmentHelperInterface  $att
  * @param TransactionJournal         $journal
  *
  * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
  */
 public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att, TransactionJournal $journal)
 {
     $journalData = $request->getJournalData();
     $repository->update($journal, $journalData);
     // save attachments:
     $att->saveAttachmentsForModel($journal);
     // flash errors
     if (count($att->getErrors()->get('attachments')) > 0) {
         Session::flash('error', $att->getErrors()->get('attachments'));
     }
     // flash messages
     if (count($att->getMessages()->get('attachments')) > 0) {
         Session::flash('info', $att->getMessages()->get('attachments'));
     }
     event(new TransactionJournalUpdated($journal));
     // update, get events by date and sort DESC
     Session::flash('success', 'Transaction "' . e($journalData['description']) . '" updated.');
     Preferences::mark();
     if (intval(Input::get('return_to_edit')) === 1) {
         // set value so edit routine will not overwrite URL:
         Session::put('transactions.edit.fromUpdate', true);
         return redirect(route('transactions.edit', [$journal->id]))->withInput(['return_to_edit' => 1]);
     }
     // redirect to previous URL.
     return redirect(Session::get('transactions.edit.url'));
 }
 /**
  * @param JournalRepositoryInterface $repository
  * @param                            $what
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function transactionJournals(JournalRepositoryInterface $repository, $what)
 {
     $descriptions = [];
     $dbType = $repository->getTransactionType($what);
     $journals = $repository->getJournalsOfType($dbType);
     foreach ($journals as $j) {
         $descriptions[] = $j->description;
     }
     $descriptions = array_unique($descriptions);
     sort($descriptions);
     return Response::json($descriptions);
 }
 /**
  * @param JournalRepositoryInterface $repository
  * @param                            $what
  *
  * @return \Symfony\Component\HttpFoundation\Response
  */
 public function transactionJournals(JournalRepositoryInterface $repository, $what)
 {
     $descriptions = [];
     $type = config('firefly.transactionTypesByWhat.' . $what);
     $types = [$type];
     $journals = $repository->getJournals($types, 1, 50);
     foreach ($journals as $j) {
         $descriptions[] = $j->description;
     }
     $descriptions = array_unique($descriptions);
     sort($descriptions);
     return Response::json($descriptions);
 }
 /**
  * @param MassEditJournalRequest     $request
  * @param JournalRepositoryInterface $repository
  *
  * @return mixed
  */
 public function massUpdate(MassEditJournalRequest $request, JournalRepositoryInterface $repository)
 {
     $journalIds = $request->get('journals');
     $count = 0;
     if (is_array($journalIds)) {
         foreach ($journalIds as $journalId) {
             $journal = $repository->find(intval($journalId));
             if ($journal) {
                 // get optional fields:
                 $what = strtolower(TransactionJournal::transactionTypeStr($journal));
                 $sourceAccountId = $request->get('source_account_id')[$journal->id] ?? 0;
                 $sourceAccountName = $request->get('source_account_name')[$journal->id] ?? '';
                 $destAccountId = $request->get('destination_account_id')[$journal->id] ?? 0;
                 $destAccountName = $request->get('destination_account_name')[$journal->id] ?? '';
                 $budgetId = $journal->budgets->first() ? $journal->budgets->first()->id : 0;
                 $category = $request->get('category')[$journal->id];
                 $tags = $journal->tags->pluck('tag')->toArray();
                 // build data array
                 $data = ['id' => $journal->id, 'what' => $what, 'description' => $request->get('description')[$journal->id], 'source_account_id' => intval($sourceAccountId), 'source_account_name' => $sourceAccountName, 'destination_account_id' => intval($destAccountId), 'destination_account_name' => $destAccountName, 'amount' => round($request->get('amount')[$journal->id], 4), 'user' => auth()->user()->id, 'amount_currency_id_amount' => intval($request->get('amount_currency_id_amount_' . $journal->id)), 'date' => new Carbon($request->get('date')[$journal->id]), 'interest_date' => $journal->interest_date, 'book_date' => $journal->book_date, 'process_date' => $journal->process_date, 'budget_id' => $budgetId, 'category' => $category, 'tags' => $tags];
                 // call repository update function.
                 $repository->update($journal, $data);
                 $count++;
             }
         }
     }
     Preferences::mark();
     Session::flash('success', trans('firefly.mass_edited_transactions_success', ['amount' => $count]));
     // redirect to previous URL:
     return redirect(session('transactions.mass-edit.url'));
 }
 /**
  * @param JournalFormRequest         $request
  * @param JournalRepositoryInterface $repository
  * @param TransactionJournal         $journal
  *
  * @return \Illuminate\Http\RedirectResponse
  */
 public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, TransactionJournal $journal)
 {
     $journalData = $request->getJournalData();
     $repository->update($journal, $journalData);
     event(new JournalSaved($journal));
     // update, get events by date and sort DESC
     Session::flash('success', 'Transaction "' . e($journalData['description']) . '" updated.');
     Preferences::mark();
     if (intval(Input::get('return_to_edit')) === 1) {
         // set value so edit routine will not overwrite URL:
         Session::put('transactions.edit.fromUpdate', true);
         return Redirect::route('transactions.edit', [$journal->id])->withInput(['return_to_edit' => 1]);
     }
     // redirect to previous URL.
     return Redirect::to(Session::get('transactions.edit.url'));
 }