2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
use Carbon\Carbon;
|
2018-05-20 12:53:14 +00:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
2020-04-01 12:35:06 +00:00
|
|
|
use App\Traits\NextKey;
|
|
|
|
use App\Traits\PushNew;
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
class Invoice extends Model
|
|
|
|
{
|
2020-04-01 12:35:06 +00:00
|
|
|
use NextKey,PushNew;
|
|
|
|
const RECORD_ID = 'invoice';
|
|
|
|
public $incrementing = FALSE;
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
protected $table = 'ab_invoice';
|
2020-04-01 12:35:06 +00:00
|
|
|
const CREATED_AT = 'date_orig';
|
|
|
|
const UPDATED_AT = 'date_last';
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
protected $dates = ['date_orig','due_date'];
|
2020-04-01 12:35:06 +00:00
|
|
|
public $dateFormat = 'U';
|
2018-06-19 12:31:49 +00:00
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'date_due',
|
|
|
|
'due',
|
|
|
|
'invoice_id_url',
|
|
|
|
'total',
|
|
|
|
];
|
|
|
|
|
|
|
|
protected $visible = [
|
|
|
|
'date_due',
|
|
|
|
'due',
|
|
|
|
'id',
|
|
|
|
'invoice_id_url',
|
|
|
|
'total',
|
|
|
|
];
|
2018-05-20 12:53:14 +00:00
|
|
|
|
2020-02-10 11:07:46 +00:00
|
|
|
protected $with = [
|
|
|
|
'account.country.currency',
|
|
|
|
'items.taxes',
|
|
|
|
'paymentitems'
|
|
|
|
];
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
private $_total = 0;
|
2018-08-01 07:09:38 +00:00
|
|
|
private $_total_tax = 0;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
|
|
|
public function account()
|
|
|
|
{
|
2018-07-13 04:53:44 +00:00
|
|
|
return $this->belongsTo(Account::class);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function items()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceItem::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function paymentitems()
|
|
|
|
{
|
|
|
|
return $this->hasMany(PaymentItem::class);
|
|
|
|
}
|
|
|
|
|
2020-01-12 12:42:32 +00:00
|
|
|
/** SCOPES **/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a record
|
|
|
|
*
|
|
|
|
* @param $query
|
|
|
|
* @param string $term
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
public function scopeSearch($query,string $term)
|
|
|
|
{
|
|
|
|
return $query->where('id','like','%'.$term.'%');
|
|
|
|
}
|
|
|
|
|
|
|
|
/** ATTRIBUTES **/
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getDueAttribute()
|
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total - $this->paid);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getDateDueAttribute()
|
|
|
|
{
|
|
|
|
return $this->due_date->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getInvoiceDateAttribute()
|
|
|
|
{
|
|
|
|
return $this->date_orig->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getInvoiceIdAttribute()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2018-08-01 07:09:38 +00:00
|
|
|
return sprintf('%06s',$this->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvoiceAccountIdAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('%02s-%04s-%06s',$this->site_id,$this->account_id,$this->invoice_id);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
public function getInvoiceIdUrlAttribute()
|
|
|
|
{
|
2018-08-01 07:09:38 +00:00
|
|
|
return sprintf('<a href="/u/invoice/%s">%s</a>',$this->id,$this->invoice_account_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getInvoiceTextAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('Thank you for using %s for your Internet Services.',config('SITE_SETUP')->site_name);
|
2018-06-19 12:31:49 +00:00
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getPaidAttribute()
|
|
|
|
{
|
|
|
|
return $this->currency()->round($this->paymentitems->sum('alloc_amt'));
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getSubTotalAttribute()
|
|
|
|
{
|
|
|
|
return sprintf('%3.'.$this->currency()->rounding.'f',$this->total-$this->tax_total);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTaxTotalAttribute()
|
|
|
|
{
|
|
|
|
if (! $this->_total_tax)
|
|
|
|
{
|
|
|
|
foreach ($this->items as $o)
|
|
|
|
{
|
|
|
|
if ($o->active)
|
|
|
|
$this->_total_tax += $this->currency()->round($o->tax);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total_tax);
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getTotalAttribute()
|
|
|
|
{
|
|
|
|
if (! $this->_total)
|
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
foreach ($this->items as $o)
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2018-06-19 12:31:49 +00:00
|
|
|
if ($o->active)
|
2018-05-20 12:53:14 +00:00
|
|
|
$this->_total += $this->currency()->round($o->total);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-19 12:31:49 +00:00
|
|
|
return sprintf('%3.'.$this->currency()->rounding.'f',$this->_total);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function currency()
|
|
|
|
{
|
|
|
|
return $this->account->country->currency;
|
|
|
|
}
|
2018-08-01 07:09:38 +00:00
|
|
|
|
|
|
|
public function products()
|
|
|
|
{
|
|
|
|
$return = collect();
|
|
|
|
|
|
|
|
foreach ($this->items->groupBy('product_id') as $o)
|
|
|
|
{
|
|
|
|
$po = $o->first()->product;
|
|
|
|
$po->count = count($o->pluck('service_id')->unique());
|
|
|
|
|
|
|
|
$return->push($po);
|
|
|
|
}
|
|
|
|
|
|
|
|
$lo = $this->account->user->language;
|
|
|
|
return $return->sortBy(function ($item) use ($lo) {
|
|
|
|
return $item->name($lo);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_services(Product $po)
|
|
|
|
{
|
|
|
|
$return = collect();
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
$this->items->load(['service']);
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
foreach ($this->items->filter(function ($item) use ($po) {
|
|
|
|
return $item->product_id == $po->id;
|
|
|
|
}) as $o)
|
|
|
|
{
|
|
|
|
$so = $o->service;
|
|
|
|
$return->push($so);
|
|
|
|
};
|
|
|
|
|
|
|
|
return $return->unique()->sortBy('name');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function product_service_items(Product $po,Service $so)
|
|
|
|
{
|
|
|
|
return $this->items->filter(function ($item) use ($po,$so) {
|
|
|
|
return $item->product_id == $po->id AND $item->service_id == $so->id;
|
|
|
|
})->filter()->sortBy('item_type');
|
|
|
|
}
|
2020-04-01 12:35:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Automatically set our due_date at save time.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = []) {
|
|
|
|
// Automatically set the date_due attribute for new records.
|
|
|
|
if (! $this->exists AND ! $this->due_date) {
|
|
|
|
$this->due_date = $this->items->min('date_start');
|
|
|
|
|
|
|
|
// @todo This 7 days should be sysetm configurable
|
|
|
|
if (($x=Carbon::now()->addDay(7)) > $this->due_date)
|
|
|
|
$this->due_date = $x;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|