2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Income;
|
|
|
|
|
|
|
|
use App\Models\Model;
|
2018-03-06 18:26:55 +03:00
|
|
|
use App\Models\Setting\Category;
|
2017-09-14 22:21:00 +03:00
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
2018-04-27 16:01:31 +03:00
|
|
|
use App\Traits\Media;
|
|
|
|
use App\Traits\Recurring;
|
2017-11-26 15:20:17 +03:00
|
|
|
use Bkwld\Cloner\Cloneable;
|
2017-09-14 22:21:00 +03:00
|
|
|
use Sofa\Eloquence\Eloquence;
|
|
|
|
|
|
|
|
class Revenue extends Model
|
|
|
|
{
|
2018-04-27 16:01:31 +03:00
|
|
|
use Cloneable, Currencies, DateTime, Eloquence, Media, Recurring;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'revenues';
|
|
|
|
|
2018-02-09 15:38:33 +03:00
|
|
|
protected $dates = ['deleted_at', 'paid_at'];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-09 01:10:43 +03:00
|
|
|
protected $fillable = ['company_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'description', 'category_id', 'payment_method', 'reference'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sortable columns.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public $sortable = ['paid_at', 'amount','category_id', 'account', 'payment_method'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searchable rules.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $searchableColumns = [
|
|
|
|
'invoice_number' => 10,
|
|
|
|
'order_number' => 10,
|
|
|
|
'customer_name' => 10,
|
|
|
|
'customer_email' => 5,
|
|
|
|
'notes' => 2,
|
|
|
|
];
|
|
|
|
|
2018-04-26 18:40:04 +03:00
|
|
|
/**
|
|
|
|
* Clonable relationships.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $cloneable_relations = ['recurring'];
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function user()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Auth\User', 'customer_id', 'id');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Banking\Account');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function category()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Category');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function customer()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Income\Customer');
|
|
|
|
}
|
|
|
|
|
2018-04-26 18:40:04 +03:00
|
|
|
public function recurring()
|
|
|
|
{
|
|
|
|
return $this->morphOne('App\Models\Common\Recurring', 'recurrable');
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function transfers()
|
|
|
|
{
|
|
|
|
return $this->hasMany('App\Models\Banking\Transfer');
|
|
|
|
}
|
|
|
|
|
2018-03-06 18:26:55 +03:00
|
|
|
/**
|
|
|
|
* Get only transfers.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeIsTransfer($query)
|
|
|
|
{
|
|
|
|
return $query->where('category_id', '=', Category::transfer());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Skip transfers.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopeIsNotTransfer($query)
|
|
|
|
{
|
|
|
|
return $query->where('category_id', '<>', Category::transfer());
|
|
|
|
}
|
|
|
|
|
2017-10-04 01:25:03 +03:00
|
|
|
/**
|
2017-10-21 14:23:57 +03:00
|
|
|
* Convert amount to double.
|
2017-10-04 01:25:03 +03:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAmountAttribute($value)
|
|
|
|
{
|
2017-10-21 14:23:57 +03:00
|
|
|
$this->attributes['amount'] = (double) $value;
|
2017-10-04 01:25:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-10-21 14:23:57 +03:00
|
|
|
* Convert currency rate to double.
|
2017-10-04 01:25:03 +03:00
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setCurrencyRateAttribute($value)
|
|
|
|
{
|
2017-10-21 14:23:57 +03:00
|
|
|
$this->attributes['currency_rate'] = (double) $value;
|
2017-10-04 01:25:03 +03:00
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function scopeLatest($query)
|
|
|
|
{
|
|
|
|
return $query->orderBy('paid_at', 'desc');
|
|
|
|
}
|
2018-01-02 18:20:52 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current balance.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2018-01-03 20:08:56 +03:00
|
|
|
public function getAttachmentAttribute($value)
|
2018-01-02 18:20:52 +03:00
|
|
|
{
|
2018-01-10 18:21:12 +03:00
|
|
|
if (!empty($value) && !$this->hasMedia('attachment')) {
|
2018-01-03 20:08:56 +03:00
|
|
|
return $value;
|
2018-01-10 18:21:12 +03:00
|
|
|
} elseif (!$this->hasMedia('attachment')) {
|
2018-01-02 18:20:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->getMedia('attachment')->last();
|
|
|
|
}
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|