akaunting/app/Models/Expense/Payment.php

137 lines
3.1 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Models\Expense;
use App\Models\Model;
use App\Models\Setting\Category;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2017-11-26 15:20:17 +03:00
use Bkwld\Cloner\Cloneable;
2017-09-14 22:21:00 +03:00
use Sofa\Eloquence\Eloquence;
use App\Traits\Media;
2017-09-14 22:21:00 +03:00
class Payment extends Model
{
use Cloneable, Currencies, DateTime, Eloquence, Media;
2017-09-14 22:21:00 +03:00
protected $table = 'payments';
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', 'vendor_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.name', 'account.name'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'accounts.name',
'categories.name',
'vendors.name' ,
'description' ,
];
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 vendor()
{
return $this->belongsTo('App\Models\Expense\Vendor');
}
public function transfers()
{
return $this->hasMany('App\Models\Banking\Transfer');
}
/**
* 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 static function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
2018-01-02 18:20:52 +03:00
/**
* Get the current balance.
*
* @return string
*/
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')) {
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
}