akaunting/app/Models/Income/Invoice.php

160 lines
3.6 KiB
PHP
Raw Normal View History

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;
2017-11-26 15:20:17 +03:00
use App\Traits\Incomes;
use Bkwld\Cloner\Cloneable;
2017-09-14 22:21:00 +03:00
use Sofa\Eloquence\Eloquence;
2017-12-28 17:20:16 +03:00
use Plank\Mediable\Mediable;
2017-09-14 22:21:00 +03:00
class Invoice extends Model
{
2017-12-28 17:20:16 +03:00
use Cloneable, Currencies, DateTime, Eloquence, Incomes, Mediable;
2017-09-14 22:21:00 +03:00
protected $table = 'invoices';
2017-12-28 17:20:16 +03:00
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['attachment'];
2017-09-14 22:21:00 +03:00
protected $dates = ['deleted_at', 'invoiced_at', 'due_at'];
/**
* Attributes that should be mass-assignable.
*
* @var array
*/
2018-01-09 01:10:43 +03:00
protected $fillable = ['company_id', 'invoice_number', 'order_number', 'invoice_status_code', 'invoiced_at', 'due_at', 'amount', 'currency_code', 'currency_rate', 'customer_id', 'customer_name', 'customer_email', 'customer_tax_number', 'customer_phone', 'customer_address', 'notes'];
2017-09-14 22:21:00 +03:00
/**
* Sortable columns.
*
* @var array
*/
public $sortable = ['invoice_number', 'customer_name', 'amount', 'status' , 'invoiced_at', 'due_at'];
/**
* Searchable rules.
*
* @var array
*/
protected $searchableColumns = [
'invoice_number' => 10,
'order_number' => 10,
'customer_name' => 10,
'customer_email' => 5,
'customer_phone' => 2,
'customer_address' => 1,
'notes' => 2,
];
2017-11-26 15:20:17 +03:00
/**
* Clonable relationships.
*
* @var array
*/
protected $cloneable_relations = ['items', 'totals'];
2017-11-26 15:20:17 +03:00
2017-09-14 22:21:00 +03:00
public function customer()
{
return $this->belongsTo('App\Models\Income\Customer');
}
public function currency()
{
return $this->belongsTo('App\Models\Setting\Currency', 'currency_code', 'code');
}
public function status()
{
return $this->belongsTo('App\Models\Income\InvoiceStatus', 'invoice_status_code', 'code');
}
public function items()
{
return $this->hasMany('App\Models\Income\InvoiceItem');
}
public function totals()
{
return $this->hasMany('App\Models\Income\InvoiceTotal');
}
2017-09-14 22:21:00 +03:00
public function payments()
{
return $this->hasMany('App\Models\Income\InvoicePayment');
}
public function histories()
{
return $this->hasMany('App\Models\Income\InvoiceHistory');
}
2017-11-07 05:11:03 +03:00
public function scopeDue($query, $date)
{
return $query->where('due_at', '=', $date);
}
public function scopeLatest($query)
{
return $query->orderBy('paid_at', 'desc');
}
public function scopeAccrued($query)
{
return $query->where('invoice_status_code', '!=', 'draft');
}
2017-11-26 15:20:17 +03:00
public function onCloning($src, $child = null)
{
$this->invoice_status_code = 'draft';
2017-11-26 15:20:17 +03:00
$this->invoice_number = $this->getNextInvoiceNumber();
}
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-12-28 17:20:16 +03:00
/**
* Get the current balance.
*
* @return string
*/
public function getAttachmentAttribute($value)
2017-12-28 17:20:16 +03:00
{
if (!empty($value)) {
return $value;
}
2017-12-28 17:20:16 +03:00
if (!$this->hasMedia('attachment')) {
return false;
}
2017-12-29 19:56:56 +03:00
return $this->getMedia('attachment')->last();
2017-12-28 17:20:16 +03:00
}
2017-09-14 22:21:00 +03:00
}