/** * 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 Request $request * @param JournalRepositoryInterface $repository * @param string $what * * @return View */ public function index(Request $request, JournalRepositoryInterface $repository, string $what) { $pageSize = intval(Preferences::get('transactionPageSize', 50)->data); $subTitleIcon = config('firefly.transactionIconsByWhat.' . $what); $types = config('firefly.transactionTypesByWhat.' . $what); $subTitle = trans('firefly.title_' . $what); $page = intval($request->get('page')); $journals = $repository->getJournals($types, $page, $pageSize); $journals->setPath('transactions/' . $what); return view('transactions.index', compact('subTitle', 'what', 'subTitleIcon', 'journals')); }
/** * @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); }