Skip to content

Commit

Permalink
apply not recurring and split scopes by default
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Jun 5, 2022
1 parent 3ac8186 commit 2cdeaad
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 33 deletions.
2 changes: 1 addition & 1 deletion app/Exports/Banking/Transactions.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/Exports/Purchases/Sheets/Bills.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/Exports/Sales/Sheets/Invoices.php
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Banking/Accounts.php
Expand Up @@ -36,7 +36,7 @@ public function index()
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) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Banking/Transactions.php
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/Scopes/Contact.php
Expand Up @@ -20,6 +20,6 @@ class Contact implements Scope
*/
public function apply(Builder $builder, Model $model)
{
$this->applyTypeScope($builder, $model);
//
}
}
2 changes: 1 addition & 1 deletion app/Scopes/Document.php
Expand Up @@ -20,6 +20,6 @@ class Document implements Scope
*/
public function apply(Builder $builder, Model $model)
{
$this->applyTypeScope($builder, $model);
$this->applyNotRecurringScope($builder, $model);
}
}
4 changes: 3 additions & 1 deletion app/Scopes/Transaction.php
Expand Up @@ -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);
}
}
83 changes: 60 additions & 23 deletions app/Traits/Scopes.php
Expand Up @@ -4,7 +4,6 @@

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

trait Scopes
{
Expand All @@ -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();
}

/**
* 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;
}

if (empty($type)) {
// Skip if scope is already applied
if ($this->scopeEquals($builder, 'type', 'not like', '%-split')) {
return;
}

// Apply type scope
$builder->where($model->getTable() . '.type', '=', $type);
// Apply not split scope
$builder->isNotSplit();
}

/**
Expand Down Expand Up @@ -73,18 +85,43 @@ public function scopeExists($builder, $column)
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']);

$type = $request->get('type') ?: Str::singular((string) $request->segment(3));
$where['column'] = $whr[1];
}

if ($where['column'] != $column) {
continue;
}

return $type;
if ($where['operator'] != $operator) {
continue;
}

if ($where['value'] != $value) {
continue;
}

return true;
}

return false;
}
}
4 changes: 2 additions & 2 deletions app/View/Components/Contacts/Show/Content.php
Expand Up @@ -34,7 +34,7 @@ public function render()
$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();

Expand All @@ -61,7 +61,7 @@ public function render()
}

// 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();

Expand Down

0 comments on commit 2cdeaad

Please sign in to comment.