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;
|
2020-08-26 15:14:16 +03:00
|
|
|
use App\Traits\Transactions;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class Category extends Model
|
|
|
|
{
|
2020-08-26 15:14:16 +03:00
|
|
|
use Transactions;
|
|
|
|
|
2017-09-14 22:21:00 +03: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 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
|
|
|
}
|
|
|
|
|
2019-11-27 12:08:36 +03:00
|
|
|
public function expense_transactions()
|
2017-09-14 22:21:00 +03:00
|
|
|
{
|
2020-08-26 15:14:16 +03:00
|
|
|
return $this->transactions()->whereIn('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
|
|
|
{
|
2020-08-26 15:14:16 +03:00
|
|
|
return $this->transactions()->whereIn('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
|
|
|
{
|
2019-12-31 15:49:09 +03:00
|
|
|
return $this->hasMany('App\Models\Sale\Invoice');
|
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;
|
|
|
|
}
|
|
|
|
|
2020-02-22 00:30:45 +03:00
|
|
|
return $query->whereIn($this->table . '.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)
|
|
|
|
{
|
|
|
|
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 22:58:49 +03:00
|
|
|
public function scopeName($query, $name)
|
|
|
|
{
|
|
|
|
return $query->where('name', '=', $name);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2020-06-20 12:15:39 +03:00
|
|
|
return (int) $query->other()->pluck('id')->first();
|
2017-12-05 15:56:33 +03:00
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|