mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-29 06:34:37 +00:00 
			
		
		
		
	Solved group thing.
This commit is contained in:
		| @@ -7,8 +7,11 @@ namespace FireflyIII\Helpers\Collector; | |||||||
| use Carbon\Carbon; | use Carbon\Carbon; | ||||||
| use Crypt; | use Crypt; | ||||||
| use FireflyIII\Exceptions\FireflyException; | use FireflyIII\Exceptions\FireflyException; | ||||||
|  | use FireflyIII\Models\AccountType; | ||||||
| use FireflyIII\Models\Category; | use FireflyIII\Models\Category; | ||||||
| use FireflyIII\Models\Transaction; | use FireflyIII\Models\Transaction; | ||||||
|  | use FireflyIII\Models\TransactionType; | ||||||
|  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||||
| use FireflyIII\User; | use FireflyIII\User; | ||||||
| use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||||||
| use Illuminate\Pagination\LengthAwarePaginator; | use Illuminate\Pagination\LengthAwarePaginator; | ||||||
| @@ -50,8 +53,22 @@ class JournalCollector | |||||||
|             'accounts.name as account_name', |             'accounts.name as account_name', | ||||||
|             'accounts.encrypted as account_encrypted', |             'accounts.encrypted as account_encrypted', | ||||||
|             'account_types.type as account_type', |             'account_types.type as account_type', | ||||||
|  |  | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|  |     /** @var array */ | ||||||
|  |     private $group | ||||||
|  |         = [ | ||||||
|  |             'transaction_journals.id', | ||||||
|  |             'transaction_journals.description', | ||||||
|  |             'firefly-iii.transaction_journals.date', | ||||||
|  |             'transaction_journals.encrypted', | ||||||
|  |             'transaction_currencies.code', | ||||||
|  |             'transaction_types.type', | ||||||
|  |             'transaction_journals.bill_id', | ||||||
|  |             'bills.name', | ||||||
|  |             'transactions.amount', | ||||||
|  |         ]; | ||||||
|  |  | ||||||
|     /** @var  bool */ |     /** @var  bool */ | ||||||
|     private $joinedCategory = false; |     private $joinedCategory = false; | ||||||
|     /** @var  int */ |     /** @var  int */ | ||||||
| @@ -107,79 +124,19 @@ class JournalCollector | |||||||
|     public function getJournals(): Collection |     public function getJournals(): Collection | ||||||
|     { |     { | ||||||
|         $this->run = true; |         $this->run = true; | ||||||
|         $set       = $this->query->get($this->fields); |         $set       = $this->query->get(array_values($this->fields)); | ||||||
|  |  | ||||||
|         // loop for decryption. |         // filter out transfers: | ||||||
|         $set->each( |         $set = $set->filter( | ||||||
|             function (Transaction $transaction) { |             function (Transaction $transaction) { | ||||||
|                 $transaction->date        = new Carbon($transaction->date); |                 if (!($transaction->transaction_type_type === TransactionType::TRANSFER && bccomp($transaction->transaction_amount, '0') === -1)) { | ||||||
|                 $transaction->description = intval($transaction->encrypted) === 1 ? Crypt::decrypt($transaction->description) : $transaction->description; |                     return $transaction; | ||||||
|                 $transaction->bill_name   = !is_null($transaction->bill_name) ? Crypt::decrypt($transaction->bill_name) : ''; |                 } | ||||||
|  |  | ||||||
|  |                 return false; | ||||||
|             } |             } | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|         return $set; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * It might be worth it to expand this query to include all account information required. |  | ||||||
|      * |  | ||||||
|      * @param Collection $accounts |  | ||||||
|      * @param array      $types |  | ||||||
|      * @param Carbon     $start |  | ||||||
|      * @param Carbon     $end |  | ||||||
|      * |  | ||||||
|      * @return Collection |  | ||||||
|      */ |  | ||||||
|     public function getJournalsInPeriod(Collection $accounts, array $types, Carbon $start, Carbon $end): Collection |  | ||||||
|     { |  | ||||||
|         $accountIds = $accounts->pluck('id')->toArray(); |  | ||||||
|         $query      = Transaction |  | ||||||
|             ::leftJoin('transaction_journals', 'transaction_journals.id', '=', 'transactions.transaction_journal_id') |  | ||||||
|             ->leftJoin('transaction_currencies', 'transaction_currencies.id', 'transaction_journals.transaction_currency_id') |  | ||||||
|             ->leftJoin('transaction_types', 'transaction_types.id', 'transaction_journals.transaction_type_id') |  | ||||||
|             ->leftJoin('bills', 'bills.id', 'transaction_journals.bill_id') |  | ||||||
|             ->leftJoin('accounts', 'accounts.id', '=', 'transactions.account_id') |  | ||||||
|             ->leftJoin('account_types', 'accounts.account_type_id', 'account_types.id') |  | ||||||
|             ->whereIn('transactions.account_id', $accountIds) |  | ||||||
|             ->whereNull('transactions.deleted_at') |  | ||||||
|             ->whereNull('transaction_journals.deleted_at') |  | ||||||
|             ->where('transaction_journals.date', '>=', $start->format('Y-m-d')) |  | ||||||
|             ->where('transaction_journals.date', '<=', $end->format('Y-m-d')) |  | ||||||
|             ->where('transaction_journals.user_id', $this->user->id) |  | ||||||
|             ->orderBy('transaction_journals.date', 'DESC') |  | ||||||
|             ->orderBy('transaction_journals.order', 'ASC') |  | ||||||
|             ->orderBy('transaction_journals.id', 'DESC'); |  | ||||||
|  |  | ||||||
|         if (count($types) > 0) { |  | ||||||
|             $query->whereIn('transaction_types.type', $types); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         $set = $query->get( |  | ||||||
|             [ |  | ||||||
|                 'transaction_journals.id as journal_id', |  | ||||||
|                 'transaction_journals.description', |  | ||||||
|                 'transaction_journals.date', |  | ||||||
|                 'transaction_journals.encrypted', |  | ||||||
|                 //'transaction_journals.transaction_currency_id', |  | ||||||
|                 'transaction_currencies.code as transaction_currency_code', |  | ||||||
|                 //'transaction_currencies.symbol as transaction_currency_symbol', |  | ||||||
|                 'transaction_types.type as transaction_type_type', |  | ||||||
|                 'transaction_journals.bill_id', |  | ||||||
|                 'bills.name as bill_name', |  | ||||||
|                 'transactions.id as id', |  | ||||||
|                 'transactions.amount as transaction_amount', |  | ||||||
|                 'transactions.description as transaction_description', |  | ||||||
|                 'transactions.account_id', |  | ||||||
|                 'transactions.identifier', |  | ||||||
|                 'transactions.transaction_journal_id', |  | ||||||
|                 'accounts.name as account_name', |  | ||||||
|                 'accounts.encrypted as account_encrypted', |  | ||||||
|                 'account_types.type as account_type', |  | ||||||
|  |  | ||||||
|             ] |  | ||||||
|         ); |  | ||||||
|  |  | ||||||
|         // loop for decryption. |         // loop for decryption. | ||||||
|         $set->each( |         $set->each( | ||||||
|             function (Transaction $transaction) { |             function (Transaction $transaction) { | ||||||
| @@ -223,6 +180,22 @@ class JournalCollector | |||||||
|         return $this; |         return $this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @return JournalCollector | ||||||
|  |      */ | ||||||
|  |     public function setAllAssetAccounts(): JournalCollector | ||||||
|  |     { | ||||||
|  |         /** @var AccountRepositoryInterface $repository */ | ||||||
|  |         $repository = app(AccountRepositoryInterface::class, [$this->user]); | ||||||
|  |         $accounts   = $repository->getAccountsByType([AccountType::ASSET, AccountType::DEFAULT]); | ||||||
|  |         if ($accounts->count() > 0) { | ||||||
|  |             $accountIds = $accounts->pluck('id')->toArray(); | ||||||
|  |             $this->query->whereIn('transactions.account_id', $accountIds); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         return $this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Category $category |      * @param Category $category | ||||||
|      * |      * | ||||||
| @@ -247,22 +220,6 @@ class JournalCollector | |||||||
|         return $this; |         return $this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * @param Collection $accounts |  | ||||||
|      * |  | ||||||
|      * @return JournalCollector |  | ||||||
|      */ |  | ||||||
|     public function setDestinationAccounts(Collection $accounts): JournalCollector |  | ||||||
|     { |  | ||||||
|         if ($accounts->count() > 0) { |  | ||||||
|             $accountIds = $accounts->pluck('id')->toArray(); |  | ||||||
|             $this->query->whereIn('transactions.account_id', $accountIds); |  | ||||||
|             $this->query->where('transactions.amount', '>', 0); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return $this; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param int $limit |      * @param int $limit | ||||||
|      * |      * | ||||||
| @@ -330,22 +287,6 @@ class JournalCollector | |||||||
|         return $this; |         return $this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |  | ||||||
|      * @param Collection $accounts |  | ||||||
|      * |  | ||||||
|      * @return JournalCollector |  | ||||||
|      */ |  | ||||||
|     public function setSourceAccounts(Collection $accounts): JournalCollector |  | ||||||
|     { |  | ||||||
|         if ($accounts->count() > 0) { |  | ||||||
|             $accountIds = $accounts->pluck('id')->toArray(); |  | ||||||
|             $this->query->whereIn('transactions.account_id', $accountIds); |  | ||||||
|             $this->query->where('transactions.amount', '<', 0); |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return $this; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param array $types |      * @param array $types | ||||||
|      * |      * | ||||||
| @@ -360,6 +301,28 @@ class JournalCollector | |||||||
|         return $this; |         return $this; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     /** | ||||||
|  |      * @return JournalCollector | ||||||
|  |      */ | ||||||
|  |     public function withoutCategory(): JournalCollector | ||||||
|  |     { | ||||||
|  |         if (!$this->joinedCategory) { | ||||||
|  |             // join some extra tables: | ||||||
|  |             $this->joinedCategory = true; | ||||||
|  |             $this->query->leftJoin('category_transaction_journal', 'category_transaction_journal.transaction_journal_id', '=', 'transaction_journals.id'); | ||||||
|  |             $this->query->leftJoin('category_transaction', 'category_transaction.transaction_id', '=', 'transactions.id'); | ||||||
|  |         } | ||||||
|  |  | ||||||
|  |         $this->query->where( | ||||||
|  |             function (EloquentBuilder $q) { | ||||||
|  |                 $q->whereNull('category_transaction.category_id'); | ||||||
|  |                 $q->whereNull('category_transaction_journal.category_id'); | ||||||
|  |             } | ||||||
|  |         ); | ||||||
|  |  | ||||||
|  |         return $this; | ||||||
|  |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @return EloquentBuilder |      * @return EloquentBuilder | ||||||
|      */ |      */ | ||||||
|   | |||||||
| @@ -157,13 +157,20 @@ class CategoryController extends Controller | |||||||
|         $start = session('start', Carbon::now()->startOfMonth()); |         $start = session('start', Carbon::now()->startOfMonth()); | ||||||
|         /** @var Carbon $end */ |         /** @var Carbon $end */ | ||||||
|         $end      = session('end', Carbon::now()->startOfMonth()); |         $end      = session('end', Carbon::now()->startOfMonth()); | ||||||
|         $list     = $repository->journalsInPeriodWithoutCategory(new Collection(), [], $start, $end); // category |  | ||||||
|  |         // new collector: | ||||||
|  |         $collector = new JournalCollector(auth()->user()); | ||||||
|  |         $collector->setAllAssetAccounts()->setRange($start, $end)->withoutCategory();//->groupJournals(); | ||||||
|  |         $journals = $collector->getJournals(); | ||||||
|  | //        $list     = $repository->journalsInPeriodWithoutCategory(new Collection(), [], $start, $end); // category | ||||||
|         $subTitle = trans( |         $subTitle = trans( | ||||||
|             'firefly.without_category_between', |             'firefly.without_category_between', | ||||||
|             ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] |             ['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)] | ||||||
|         ); |         ); | ||||||
|  |  | ||||||
|         return view('categories.noCategory', compact('list', 'subTitle')); |  | ||||||
|  |  | ||||||
|  |         return view('categories.no-category', compact('journals', 'subTitle')); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     /** |     /** | ||||||
| @@ -189,7 +196,7 @@ class CategoryController extends Controller | |||||||
|  |  | ||||||
|         // use journal collector |         // use journal collector | ||||||
|         $collector = new JournalCollector(auth()->user()); |         $collector = new JournalCollector(auth()->user()); | ||||||
|         $collector->setPage($page)->setLimit($pageSize)->setAccounts($accounts)->setRange($start, $end)->setCategory($category); |         $collector->setPage($page)->setLimit($pageSize)->setAllAssetAccounts()->setRange($start, $end)->setCategory($category); | ||||||
|         $journals = $collector->getPaginatedJournals(); |         $journals = $collector->getPaginatedJournals(); | ||||||
|         $journals->setPath('categories/show/' . $category->id); |         $journals->setPath('categories/show/' . $category->id); | ||||||
|  |  | ||||||
| @@ -245,7 +252,7 @@ class CategoryController extends Controller | |||||||
|      * |      * | ||||||
|      * @return View |      * @return View | ||||||
|      */ |      */ | ||||||
|     public function showWithDate(AccountRepositoryInterface $repository, Category $category, string $date) |     public function showWithDate(Category $category, string $date) | ||||||
|     { |     { | ||||||
|         $carbon       = new Carbon($date); |         $carbon       = new Carbon($date); | ||||||
|         $range        = Preferences::get('viewRange', '1M')->data; |         $range        = Preferences::get('viewRange', '1M')->data; | ||||||
| @@ -255,11 +262,10 @@ class CategoryController extends Controller | |||||||
|         $hideCategory = true; // used in list. |         $hideCategory = true; // used in list. | ||||||
|         $page         = intval(Input::get('page')); |         $page         = intval(Input::get('page')); | ||||||
|         $pageSize     = intval(Preferences::get('transactionPageSize', 50)->data); |         $pageSize     = intval(Preferences::get('transactionPageSize', 50)->data); | ||||||
|         $accounts     = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |  | ||||||
|  |  | ||||||
|         // new collector: |         // new collector: | ||||||
|         $collector = new JournalCollector(auth()->user()); |         $collector = new JournalCollector(auth()->user()); | ||||||
|         $collector->setPage($page)->setLimit($pageSize)->setAccounts($accounts)->setRange($start, $end)->setCategory($category); |         $collector->setPage($page)->setLimit($pageSize)->setAllAssetAccounts()->setRange($start, $end)->setCategory($category); | ||||||
|         $journals = $collector->getPaginatedJournals(); |         $journals = $collector->getPaginatedJournals(); | ||||||
|         $journals->setPath('categories/show/' . $category->id . '/' . $date); |         $journals->setPath('categories/show/' . $category->id . '/' . $date); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -53,32 +53,19 @@ class TransactionController extends Controller | |||||||
|  |  | ||||||
|     /** |     /** | ||||||
|      * @param Request                    $request |      * @param Request                    $request | ||||||
|      * @param AccountRepositoryInterface $repository |  | ||||||
|      * @param string                     $what |      * @param string                     $what | ||||||
|      * |      * | ||||||
|      * @return View |      * @return View | ||||||
|      */ |      */ | ||||||
|     public function index(Request $request, AccountRepositoryInterface $repository, string $what) |     public function index(Request $request, string $what) | ||||||
|     { |     { | ||||||
|         $pageSize      = intval(Preferences::get('transactionPageSize', 50)->data); |         $pageSize      = intval(Preferences::get('transactionPageSize', 50)->data); | ||||||
|         $subTitleIcon  = config('firefly.transactionIconsByWhat.' . $what); |         $subTitleIcon  = config('firefly.transactionIconsByWhat.' . $what); | ||||||
|         $types         = config('firefly.transactionTypesByWhat.' . $what); |         $types         = config('firefly.transactionTypesByWhat.' . $what); | ||||||
|         $subTitle      = trans('firefly.title_' . $what); |         $subTitle      = trans('firefly.title_' . $what); | ||||||
|         $page          = intval($request->get('page')); |         $page          = intval($request->get('page')); | ||||||
|         $assetAccounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); |  | ||||||
|         $collector     = new JournalCollector(auth()->user()); |         $collector     = new JournalCollector(auth()->user()); | ||||||
|         $collector->setTypes($types)->setLimit($pageSize)->setPage($page); |         $collector->setTypes($types)->setLimit($pageSize)->setPage($page)->setAllAssetAccounts(); | ||||||
|  |  | ||||||
|         // depending on the view, we filter the collector to grab the right stuff. |  | ||||||
|         switch ($what) { |  | ||||||
|             default: |  | ||||||
|                 $collector->setAccounts($assetAccounts); |  | ||||||
|                 break; |  | ||||||
|             case 'transfer': |  | ||||||
|             case 'transfers': |  | ||||||
|                 $collector->setDestinationAccounts($assetAccounts); |  | ||||||
|                 break; |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         $journals = $collector->getPaginatedJournals(); |         $journals = $collector->getPaginatedJournals(); | ||||||
|         $journals->setPath('transactions/' . $what); |         $journals->setPath('transactions/' . $what); | ||||||
|   | |||||||
| @@ -12,7 +12,7 @@ | |||||||
|                     {{ subTitle }} |                     {{ subTitle }} | ||||||
|                 </div> |                 </div> | ||||||
|                 <div class="box-body"> |                 <div class="box-body"> | ||||||
|                     {% include 'list.journals' with {'journals': list} %} |                     {% include 'list.journals-tasker' %} | ||||||
|                 </div> |                 </div> | ||||||
|             </div> |             </div> | ||||||
|         </div> |         </div> | ||||||
		Reference in New Issue
	
	Block a user