2017-09-14 22:21:00 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models\Income;
|
|
|
|
|
|
|
|
use App\Models\Model;
|
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
2018-02-08 17:41:12 +03:00
|
|
|
use App\Traits\Media;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
class InvoicePayment extends Model
|
|
|
|
{
|
2018-02-08 17:41:12 +03:00
|
|
|
use Currencies, DateTime, Media;
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
protected $table = 'invoice_payments';
|
|
|
|
|
|
|
|
protected $dates = ['deleted_at', 'paid_at'];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Attributes that should be mass-assignable.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2018-01-09 01:10:43 +03:00
|
|
|
protected $fillable = ['company_id', 'invoice_id', 'account_id', 'paid_at', 'amount', 'currency_code', 'currency_rate', 'description', 'payment_method', 'reference'];
|
2017-09-14 22:21:00 +03:00
|
|
|
|
|
|
|
public function account()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Banking\Account');
|
|
|
|
}
|
|
|
|
|
2018-02-09 16:18:56 +03:00
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:21:00 +03:00
|
|
|
public function invoice()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Income\Invoice');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function item()
|
|
|
|
{
|
2018-06-10 02:48:51 +03:00
|
|
|
return $this->belongsTo('App\Models\Common\Item');
|
2017-09-14 22:21:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function tax()
|
|
|
|
{
|
|
|
|
return $this->belongsTo('App\Models\Setting\Tax');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopeLatest($query)
|
|
|
|
{
|
|
|
|
return $query->orderBy('paid_at', 'desc');
|
|
|
|
}
|
2017-10-21 14:23:57 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert amount to double.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setAmountAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['amount'] = (double) $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert currency rate to double.
|
|
|
|
*
|
|
|
|
* @param string $value
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function setCurrencyRateAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['currency_rate'] = (double) $value;
|
|
|
|
}
|
2017-12-07 00:13:15 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Scope paid invoice.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query
|
|
|
|
* @return \Illuminate\Database\Eloquent\Builder
|
|
|
|
*/
|
|
|
|
public function scopePaid($query)
|
|
|
|
{
|
|
|
|
return $query->sum('amount');
|
|
|
|
}
|
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
|
|
|
}
|