Akaunting/app/Models/Setting/Category.php

145 lines
3.4 KiB
PHP
Raw Normal View History

2017-09-14 19:21:00 +00:00
<?php
namespace App\Models\Setting;
2019-11-16 07:21:14 +00:00
use App\Abstracts\Model;
2020-08-26 12:14:16 +00:00
use App\Traits\Transactions;
2020-10-14 14:07:59 +00:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
2017-09-14 19:21:00 +00:00
class Category extends Model
{
2020-10-14 14:07:59 +00:00
use HasFactory, Transactions;
2020-08-26 12:14:16 +00:00
2017-09-14 19:21:00 +00:00
protected $table = 'categories';
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
protected $fillable = ['company_id', 'name', 'type', 'color', 'enabled'];
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['name', 'type', 'enabled'];
2018-04-23 19:17:20 +00:00
public function bills()
2017-09-14 19:21:00 +00:00
{
2019-12-31 12:49:09 +00:00
return $this->hasMany('App\Models\Purchase\Bill');
2017-09-14 19:21:00 +00:00
}
public function expense_transactions()
2017-09-14 19:21:00 +00:00
{
2020-08-26 12:14:16 +00:00
return $this->transactions()->whereIn('type', (array) $this->getExpenseTypes());
2017-09-14 19:21:00 +00:00
}
2020-03-09 08:02:25 +00:00
public function income_transactions()
2017-09-14 19:21:00 +00:00
{
2020-08-26 12:14:16 +00:00
return $this->transactions()->whereIn('type', (array) $this->getIncomeTypes());
2017-09-14 19:21:00 +00:00
}
public function invoices()
2018-04-23 19:17:20 +00:00
{
2019-12-31 12:49:09 +00:00
return $this->hasMany('App\Models\Sale\Invoice');
2018-04-23 19:17:20 +00:00
}
public function items()
2018-04-23 19:17:20 +00:00
{
return $this->hasMany('App\Models\Common\Item');
2019-11-16 07:21:14 +00:00
}
public function transactions()
{
return $this->hasMany('App\Models\Banking\Transaction');
2018-04-23 19:17:20 +00:00
}
2017-09-14 19:21:00 +00:00
/**
* Scope to only include categories of a given type.
*
* @param \Illuminate\Database\Eloquent\Builder $query
2019-11-16 07:21:14 +00:00
* @param mixed $types
2017-09-14 19:21:00 +00:00
* @return \Illuminate\Database\Eloquent\Builder
*/
2019-11-16 07:21:14 +00:00
public function scopeType($query, $types)
2017-09-14 19:21:00 +00:00
{
2019-11-16 07:21:14 +00:00
if (empty($types)) {
return $query;
}
2020-02-21 21:30:45 +00:00
return $query->whereIn($this->table . '.type', (array) $types);
2017-09-14 19:21:00 +00:00
}
2017-12-05 12:56:33 +00:00
2020-05-03 08:15:56 +00:00
/**
* Scope to include only income.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeIncome($query)
{
return $query->where($this->table . '.type', '=', 'income');
}
/**
* Scope to include only expense.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeExpense($query)
{
return $query->where($this->table . '.type', '=', 'expense');
}
/**
* Scope to include only item.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeItem($query)
{
return $query->where($this->table . '.type', '=', 'item');
}
/**
* Scope to include only other.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeOther($query)
{
return $query->where($this->table . '.type', '=', 'other');
}
2020-01-20 19:58:49 +00:00
public function scopeName($query, $name)
{
return $query->where('name', '=', $name);
}
2017-12-05 12:56:33 +00:00
/**
* Scope transfer category.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeTransfer($query)
{
2020-06-20 09:15:39 +00:00
return (int) $query->other()->pluck('id')->first();
2017-12-05 12:56:33 +00:00
}
2020-10-14 14:07:59 +00: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 19:21:00 +00:00
}