2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Setting;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
use App\Abstracts\Model;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Builders\Category as Builder;
|
2020-12-24 01:28:38 +03:00
|
|
|
use App\Models\Document\Document;
|
2022-06-01 10:15:55 +03:00
|
|
|
use App\Relations\HasMany\Category as HasMany;
|
|
|
|
use App\Scopes\Category as Scope;
|
2022-07-21 01:07:55 +03:00
|
|
|
use App\Traits\Categories;
|
2022-07-21 16:36:34 +03:00
|
|
|
use App\Traits\Tailwind;
|
2020-08-26 15:14:16 +03:00
|
|
|
use App\Traits\Transactions;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
|
2020-10-14 17:07:59 +03:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
2022-06-01 10:15:55 +03:00
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Category extends Model
|
|
|
|
{
|
2022-07-21 16:36:34 +03:00
|
|
|
use Categories, HasFactory, Tailwind, Transactions;
|
2020-08-26 15:14:16 +03:00
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
public const INCOME_TYPE = 'income';
|
|
|
|
public const EXPENSE_TYPE = 'expense';
|
|
|
|
public const ITEM_TYPE = 'item';
|
|
|
|
public const OTHER_TYPE = 'other';
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
protected $table = 'categories';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2022-06-01 10:15:55 +03:00
|
|
|
protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled', 'created_from', 'created_by', 'parent_id'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
2020-11-13 15:15:27 +03:00
|
|
|
/**
|
|
|
|
* The attributes that should be cast.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $casts = [
|
|
|
|
'enabled' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['name', 'type', 'enabled'];
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
/**
|
|
|
|
* The "booted" method of the model.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected static function booted()
|
|
|
|
{
|
|
|
|
static::addGlobalScope(new Scope);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new Eloquent query builder for the model.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Query\Builder $query
|
|
|
|
* @return \App\Builders\Category
|
|
|
|
*/
|
|
|
|
public function newEloquentBuilder($query)
|
|
|
|
{
|
|
|
|
return new Builder($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instantiate a new HasMany relationship.
|
|
|
|
*
|
|
|
|
* @param EloquentBuilder $query
|
|
|
|
* @param EloquentModel $parent
|
|
|
|
* @param string $foreignKey
|
|
|
|
* @param string $localKey
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
protected function newHasMany(EloquentBuilder $query, EloquentModel $parent, $foreignKey, $localKey)
|
|
|
|
{
|
|
|
|
return new HasMany($query, $parent, $foreignKey, $localKey);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieve the model for a bound value.
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string|null $field
|
|
|
|
* @return \Illuminate\Database\Eloquent\Model|null
|
|
|
|
*/
|
|
|
|
public function resolveRouteBinding($value, $field = null)
|
|
|
|
{
|
|
|
|
return $this->resolveRouteBindingQuery($this, $value, $field)
|
|
|
|
->withoutGlobalScope(Scope::class)
|
2022-10-10 17:46:50 +03:00
|
|
|
->getWithoutChildren()
|
2022-06-01 10:15:55 +03:00
|
|
|
->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function categories()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Category::class, 'parent_id')->withSubCategory();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sub_categories()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Category::class, 'parent_id')->withSubCategory()->with('categories')->orderBy('name');
|
|
|
|
}
|
|
|
|
|
2020-12-24 01:28:38 +03:00
|
|
|
public function documents()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Document\Document');
|
|
|
|
}
|
|
|
|
|
2018-04-23 22:17:20 +03:00
|
|
|
public function bills()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2022-06-10 19:26:32 +03:00
|
|
|
return $this->documents()->where('documents.type', Document::BILL_TYPE);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function expense_transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2022-06-10 19:26:32 +03:00
|
|
|
return $this->transactions()->whereIn('transactions.type', (array) $this->getExpenseTypes());
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 09:02:25 +01:00
|
|
|
public function income_transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2022-06-10 19:26:32 +03:00
|
|
|
return $this->transactions()->whereIn('transactions.type', (array) $this->getIncomeTypes());
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function invoices()
|
2018-04-23 22:17:20 +03:00
|
|
|
{
|
2022-06-10 19:26:32 +03:00
|
|
|
return $this->documents()->where('documents.type', Document::INVOICE_TYPE);
|
2018-04-23 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function items()
|
2018-04-23 22:17:20 +03:00
|
|
|
{
|
2019-11-27 12:08:36 +03:00
|
|
|
return $this->hasMany('App\Models\Common\Item');
|
2019-11-16 10:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function transactions()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Banking\Transaction');
|
2018-04-23 22:17:20 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Scope to only include categories of a given type.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
2019-11-16 10:21:14 +03:00
|
|
|
* @param mixed $types
|
2017-09-14 22:21:00 +03:00
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
2019-11-16 10:21:14 +03:00
|
|
|
public function scopeType($query, $types)
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
if (empty($types)) {
|
|
|
|
return $query;
|
|
|
|
}
|
|
|
|
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->whereIn($this->qualifyColumn('type'), (array) $types);
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
2017-12-05 15:56:33 +03:00
|
|
|
|
2020-05-03 11:15:56 +03:00
|
|
|
/**
|
|
|
|
* Scope to include only income.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeIncome($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', 'income');
|
2020-05-03 11:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope to include only expense.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeExpense($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', 'expense');
|
2020-05-03 11:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope to include only item.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeItem($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', 'item');
|
2020-05-03 11:15:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope to include only other.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeOther($query)
|
|
|
|
{
|
2021-09-10 09:41:15 +03:00
|
|
|
return $query->where($this->qualifyColumn('type'), '=', 'other');
|
2020-05-03 11:15:56 +03:00
|
|
|
}
|
|
|
|
|
2020-01-20 22:58:49 +03:00
|
|
|
public function scopeName($query, $name)
|
|
|
|
{
|
|
|
|
return $query->where('name', '=', $name);
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
/**
|
|
|
|
* Scope gets only parent categories.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeWithSubCategory($query)
|
|
|
|
{
|
|
|
|
return $query->withoutGlobalScope(new Scope);
|
|
|
|
}
|
|
|
|
|
2022-07-21 16:36:34 +03:00
|
|
|
/**
|
|
|
|
* Get the hex code of the color.
|
|
|
|
*/
|
|
|
|
public function getColorHexCodeAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->getHexCodeOfTailwindClass($this->color);
|
|
|
|
}
|
|
|
|
|
2022-06-01 10:15:55 +03:00
|
|
|
/**
|
|
|
|
* Get the line actions.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getLineActionsAttribute()
|
|
|
|
{
|
|
|
|
$actions = [];
|
|
|
|
|
|
|
|
$actions[] = [
|
|
|
|
'title' => trans('general.edit'),
|
|
|
|
'icon' => 'create',
|
|
|
|
'url' => route('categories.edit', $this->id),
|
|
|
|
'permission' => 'update-settings-categories',
|
2022-09-06 13:54:56 +03:00
|
|
|
'attributes' => [
|
|
|
|
'id' => 'index-line-actions-edit-category-' . $this->id,
|
|
|
|
],
|
2022-06-01 10:15:55 +03:00
|
|
|
];
|
|
|
|
|
2022-07-21 01:07:55 +03:00
|
|
|
if ($this->isTransferCategory()) {
|
2022-06-01 10:15:55 +03:00
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
|
|
|
$actions[] = [
|
|
|
|
'type' => 'delete',
|
|
|
|
'icon' => 'delete',
|
|
|
|
'route' => 'categories.destroy',
|
|
|
|
'permission' => 'delete-settings-categories',
|
2022-09-06 13:54:56 +03:00
|
|
|
'attributes' => [
|
|
|
|
'id' => 'index-line-actions-delete-category-' . $this->id,
|
|
|
|
],
|
2022-06-01 10:15:55 +03:00
|
|
|
'model' => $this,
|
|
|
|
];
|
|
|
|
|
|
|
|
return $actions;
|
|
|
|
}
|
|
|
|
|
2020-10-14 17:07:59 +03:00
|
|
|
/**
|
|
|
|
* Create a new factory instance for the model.
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Database\Eloquent\Factories\Factory
|
|
|
|
*/
|
|
|
|
protected static function newFactory()
|
|
|
|
{
|
|
|
|
return \Database\Factories\Category::new();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|