akaunting/app/Models/Setting/Category.php

82 lines
1.7 KiB
PHP
Raw Normal View History

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;
2017-09-14 22:21:00 +03:00
class Category extends Model
{
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 22:17:20 +03:00
public function bills()
2017-09-14 22:21:00 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Purchase\Bill');
2017-09-14 22:21:00 +03:00
}
public function expense_transactions()
2017-09-14 22:21:00 +03:00
{
return $this->transactions()->where('type', 'expense');
2017-09-14 22:21:00 +03:00
}
public function income_transacions()
2017-09-14 22:21:00 +03:00
{
return $this->transactions()->where('type', 'income');
2017-09-14 22:21:00 +03:00
}
public function invoices()
2018-04-23 22:17:20 +03:00
{
2019-12-31 15:49:09 +03:00
return $this->hasMany('App\Models\Sale\Invoice');
2018-04-23 22:17:20 +03:00
}
public function items()
2018-04-23 22:17:20 +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;
}
return $query->whereIn('type', (array) $types);
2017-09-14 22:21:00 +03:00
}
2017-12-05 15:56:33 +03:00
/**
* Scope transfer category.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeTransfer($query)
{
return $query->where('type', 'other')->pluck('id')->first();
}
2017-09-14 22:21:00 +03:00
}