apply not recurring and split scopes by default
This commit is contained in:
parent
3ac8186d4a
commit
2cdeaad00d
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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) {
|
||||
|
@ -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,
|
||||
|
@ -20,6 +20,6 @@ class Contact implements Scope
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
$this->applyTypeScope($builder, $model);
|
||||
//
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,6 @@ class Document implements Scope
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
$this->applyTypeScope($builder, $model);
|
||||
$this->applyNotRecurringScope($builder, $model);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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();
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user