From 2cdeaad00dea926447ee2746fa59565b4c27f373 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Sun, 5 Jun 2022 03:33:26 +0300 Subject: [PATCH] apply not recurring and split scopes by default --- app/Exports/Banking/Transactions.php | 2 +- app/Exports/Purchases/Sheets/Bills.php | 2 +- app/Exports/Sales/Sheets/Invoices.php | 2 +- app/Http/Controllers/Banking/Accounts.php | 2 +- app/Http/Controllers/Banking/Transactions.php | 2 +- app/Scopes/Contact.php | 2 +- app/Scopes/Document.php | 2 +- app/Scopes/Transaction.php | 4 +- app/Traits/Scopes.php | 83 ++++++++++++++----- app/View/Components/Contacts/Show/Content.php | 4 +- 10 files changed, 72 insertions(+), 33 deletions(-) diff --git a/app/Exports/Banking/Transactions.php b/app/Exports/Banking/Transactions.php index a712fdc0d..78c098a87 100644 --- a/app/Exports/Banking/Transactions.php +++ b/app/Exports/Banking/Transactions.php @@ -11,7 +11,7 @@ class Transactions extends Export implements WithColumnFormatting { public function collection() { - return Model::with('account', 'category', 'contact', 'document')->isNotRecurring()->collectForExport($this->ids, ['paid_at' => 'desc']); + return Model::with('account', 'category', 'contact', 'document')->collectForExport($this->ids, ['paid_at' => 'desc']); } public function map($model): array diff --git a/app/Exports/Purchases/Sheets/Bills.php b/app/Exports/Purchases/Sheets/Bills.php index 14afd2526..d903b8cee 100644 --- a/app/Exports/Purchases/Sheets/Bills.php +++ b/app/Exports/Purchases/Sheets/Bills.php @@ -11,7 +11,7 @@ class Bills extends Export implements WithColumnFormatting { public function collection() { - return Model::with('category')->bill()->isNotRecurring()->collectForExport($this->ids, ['document_number' => 'desc']); + return Model::with('category')->bill()->collectForExport($this->ids, ['document_number' => 'desc']); } public function map($model): array diff --git a/app/Exports/Sales/Sheets/Invoices.php b/app/Exports/Sales/Sheets/Invoices.php index bf88d5537..b1f45ed8b 100644 --- a/app/Exports/Sales/Sheets/Invoices.php +++ b/app/Exports/Sales/Sheets/Invoices.php @@ -11,7 +11,7 @@ class Invoices extends Export implements WithColumnFormatting { public function collection() { - return Model::with('category')->invoice()->isNotRecurring()->collectForExport($this->ids, ['document_number' => 'desc']); + return Model::with('category')->invoice()->collectForExport($this->ids, ['document_number' => 'desc']); } public function map($model): array diff --git a/app/Http/Controllers/Banking/Accounts.php b/app/Http/Controllers/Banking/Accounts.php index b70154893..0c7f55bbe 100644 --- a/app/Http/Controllers/Banking/Accounts.php +++ b/app/Http/Controllers/Banking/Accounts.php @@ -36,7 +36,7 @@ class Accounts extends Controller public function show(Account $account) { // Handle transactions - $transactions = Transaction::with('account', 'category')->where('account_id', $account->id)->isNotRecurring()->collect('paid_at'); + $transactions = Transaction::with('account', 'category')->where('account_id', $account->id)->collect('paid_at'); $transfers = Transfer::with('expense_transaction', 'income_transaction')->get()->filter(function ($transfer) use($account) { if ($transfer->expense_account->id == $account->id || $transfer->income_account->id == $account->id) { diff --git a/app/Http/Controllers/Banking/Transactions.php b/app/Http/Controllers/Banking/Transactions.php index dee840e17..420703f4d 100644 --- a/app/Http/Controllers/Banking/Transactions.php +++ b/app/Http/Controllers/Banking/Transactions.php @@ -35,7 +35,7 @@ class Transactions extends Controller */ public function index() { - $transactions = Transaction::with('account', 'category', 'contact')->isNotRecurring()->isNotSplit()->collect(['paid_at'=> 'desc']); + $transactions = Transaction::with('account', 'category', 'contact')->collect(['paid_at'=> 'desc']); $totals = [ 'income' => 0, diff --git a/app/Scopes/Contact.php b/app/Scopes/Contact.php index 99bd2fe53..aa2b6073e 100644 --- a/app/Scopes/Contact.php +++ b/app/Scopes/Contact.php @@ -20,6 +20,6 @@ class Contact implements Scope */ public function apply(Builder $builder, Model $model) { - $this->applyTypeScope($builder, $model); + // } } diff --git a/app/Scopes/Document.php b/app/Scopes/Document.php index a3874613c..86ed58745 100644 --- a/app/Scopes/Document.php +++ b/app/Scopes/Document.php @@ -20,6 +20,6 @@ class Document implements Scope */ public function apply(Builder $builder, Model $model) { - $this->applyTypeScope($builder, $model); + $this->applyNotRecurringScope($builder, $model); } } diff --git a/app/Scopes/Transaction.php b/app/Scopes/Transaction.php index 1d8e7207c..0fea49707 100644 --- a/app/Scopes/Transaction.php +++ b/app/Scopes/Transaction.php @@ -20,6 +20,8 @@ class Transaction implements Scope */ public function apply(Builder $builder, Model $model) { - $this->applyTypeScope($builder, $model); + $this->applyNotRecurringScope($builder, $model); + + $this->applyNotSplitScope($builder, $model); } } diff --git a/app/Traits/Scopes.php b/app/Traits/Scopes.php index 9e42c30dd..83621c0d5 100644 --- a/app/Traits/Scopes.php +++ b/app/Traits/Scopes.php @@ -4,7 +4,6 @@ namespace App\Traits; use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Model; -use Illuminate\Support\Str; trait Scopes { @@ -15,30 +14,43 @@ trait Scopes * @param \Illuminate\Database\Eloquent\Model $model * @return void */ - public function applyTypeScope(Builder $builder, Model $model) + public function applyNotRecurringScope(Builder $builder, Model $model) { - // Getting type from request causes lots of issues - // @todo Try event/listener similar to Permissions trait - return; - - // Skip if already exists - if ($this->scopeExists($builder, 'type')) { + // Skip if recurring is explicitly set + if ($this->scopeEquals($builder, 'type', 'like', '%-recurring')) { return; } - // No request in console - if (app()->runningInConsole()) { + // Skip if scope is already applied + if ($this->scopeEquals($builder, 'type', 'not like', '%-recurring')) { return; } - $type = $this->getTypeFromRequest(); + // Apply not recurring scope + $builder->isNotRecurring(); + } - if (empty($type)) { + /** + * Apply the scope to a given Eloquent query builder. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @param \Illuminate\Database\Eloquent\Model $model + * @return void + */ + public function applyNotSplitScope(Builder $builder, Model $model) + { + // Skip if split is explicitly set + if ($this->scopeEquals($builder, 'type', 'like', '%-split')) { return; } - // Apply type scope - $builder->where($model->getTable() . '.type', '=', $type); + // Skip if scope is already applied + if ($this->scopeEquals($builder, 'type', 'not like', '%-split')) { + return; + } + + // Apply not split scope + $builder->isNotSplit(); } /** @@ -73,18 +85,43 @@ trait Scopes return false; } - public function getTypeFromRequest() + /** + * Check if scope has the exact value. + * + * @param \Illuminate\Database\Eloquent\Builder $builder + * @param $column + * @return boolean + */ + public function scopeEquals($builder, $column, $operator, $value) { - $type = ''; - $request = request(); + $query = $builder->getQuery(); - // Skip type scope in dashboard and reports - if ($request->routeIs('dashboards.*') || $request->routeIs('reports.*')) { - return $type; + foreach ((array) $query->wheres as $key => $where) { + if (empty($where) || empty($where['column']) || empty($where['operator']) || empty($where['value'])) { + continue; + } + + if (strstr($where['column'], '.')) { + $whr = explode('.', $where['column']); + + $where['column'] = $whr[1]; + } + + if ($where['column'] != $column) { + continue; + } + + if ($where['operator'] != $operator) { + continue; + } + + if ($where['value'] != $value) { + continue; + } + + return true; } - $type = $request->get('type') ?: Str::singular((string) $request->segment(3)); - - return $type; + return false; } } diff --git a/app/View/Components/Contacts/Show/Content.php b/app/View/Components/Contacts/Show/Content.php index eb213c6d2..8a40bfc9d 100644 --- a/app/View/Components/Contacts/Show/Content.php +++ b/app/View/Components/Contacts/Show/Content.php @@ -34,7 +34,7 @@ class Content extends Component $this->counts = []; // Handle documents - $this->documents = $this->contact->documents()->with('transactions')->isNotRecurring()->get(); + $this->documents = $this->contact->documents()->with('transactions')->get(); $this->counts['documents'] = $this->documents->count(); @@ -61,7 +61,7 @@ class Content extends Component } // Handle payments - $this->transactions = $this->contact->transactions()->with('account', 'category')->isNotRecurring()->get(); + $this->transactions = $this->contact->transactions()->with('account', 'category')->get(); $this->counts['transactions'] = $this->transactions->count();