2018-05-20 12:53:14 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2020-01-11 11:05:05 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-02-06 09:31:43 +00:00
|
|
|
use Leenooks\Carbon;
|
2018-05-20 12:53:14 +00:00
|
|
|
|
|
|
|
class InvoiceItem extends Model
|
|
|
|
{
|
2018-08-01 07:09:38 +00:00
|
|
|
protected $dates = ['date_start','date_stop'];
|
2019-07-04 04:55:05 +00:00
|
|
|
public $dateFormat = 'U';
|
2018-05-20 12:53:14 +00:00
|
|
|
protected $table = 'ab_invoice_item';
|
|
|
|
protected $with = ['taxes'];
|
|
|
|
|
|
|
|
private $_tax = 0;
|
|
|
|
|
2018-08-07 04:26:33 +00:00
|
|
|
public function invoice()
|
2018-08-01 07:09:38 +00:00
|
|
|
{
|
2018-08-07 04:26:33 +00:00
|
|
|
return $this->belongsTo(Invoice::class);
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
|
2018-08-07 04:26:33 +00:00
|
|
|
public function product()
|
2018-05-20 12:53:14 +00:00
|
|
|
{
|
2018-08-07 04:26:33 +00:00
|
|
|
return $this->belongsTo(Product::class);
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function service()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function taxes()
|
|
|
|
{
|
|
|
|
return $this->hasMany(InvoiceItemTax::class);
|
|
|
|
}
|
|
|
|
|
2019-07-04 04:55:05 +00:00
|
|
|
/** ATTRIBUTES **/
|
|
|
|
|
2020-02-06 09:31:43 +00:00
|
|
|
/**
|
|
|
|
* Start date for the invoice item line
|
|
|
|
*
|
|
|
|
* We need cast this value to a Leenooks\Carbon for access to startOfHalf() endOfHalf() methods
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
* @return Carbon
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getDateStartAttribute($value)
|
|
|
|
{
|
|
|
|
if (! is_null($value))
|
|
|
|
return Carbon::createFromTimestamp($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* End date for the invoice item line
|
|
|
|
*
|
|
|
|
* We need cast this value to a Leenooks\Carbon for access to startOfHalf() endOfHalf() methods
|
|
|
|
*
|
|
|
|
* @param $value
|
|
|
|
* @return Carbon
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function getDateStopAttribute($value)
|
|
|
|
{
|
|
|
|
if (! is_null($value))
|
|
|
|
return Carbon::createFromTimestamp($value);
|
|
|
|
}
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getItemTypeNameAttribute()
|
|
|
|
{
|
|
|
|
$types = [
|
|
|
|
1=>'Hardware', // *
|
|
|
|
2=>'Service Relocation Fee', // * Must have corresponding SERVICE_ID
|
|
|
|
3=>'Service Change', // * Must have corresponding SERVICE_ID
|
|
|
|
4=>'Service Connection', // * Must have corresponding SERVICE_ID
|
|
|
|
6=>'Service Cancellation', // * Must have corresponding SERVICE_ID
|
|
|
|
7=>'Extra Product/Service Charge', // * Service Billing in advance, Must have corresponding SERVICE_ID
|
|
|
|
8=>'Product Addition', // * Additional Product Customisation, Must have corresponding SERVICE_ID
|
|
|
|
120=>'Credit/Debit Transfer', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL : INVOICE_ID is NOT NULL
|
|
|
|
123=>'Shipping',
|
|
|
|
124=>'Late Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE,
|
|
|
|
125=>'Payment Fee', // * SERVICE_ID is NULL, MODULE_ID = CHECKOUT MODULE, MODULE_REF = CHECKOUT NAME
|
|
|
|
126=>'Other', // * MODEL_ID should be a module
|
|
|
|
127=>'Rounding', // * SERVICE_ID is NULL, MODULE_ID is NULL, MODULE_REF is NULL
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ($this->item_type)
|
|
|
|
{
|
|
|
|
// * Line Charge Topic on Invoice.
|
|
|
|
case 0:
|
|
|
|
return sprintf('%s [%s]','Product/Service',
|
|
|
|
$this->date_start == $this->date_stop ? $this->date_start->format('Y-m-d') : sprintf('%s -> %s',$this->date_start->format('Y-m-d'),$this->date_stop->format('Y-m-d')));
|
|
|
|
|
|
|
|
// * Excess Service Item, of item 0, must have corresponding SERVICE_ID
|
|
|
|
case 5:
|
|
|
|
return sprintf('%s [%s] (%s)','Excess Usage',
|
|
|
|
$this->date_start == $this->date_stop ? $this->date_start->format('Y-m-d') : sprintf('%s -> %s',$this->date_start->format('Y-m-d'),$this->date_stop->format('Y-m-d')),
|
|
|
|
$this->module_text()
|
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
2020-01-11 11:05:05 +00:00
|
|
|
return Arr::get($types,$this->item_type,'Unknown');
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-20 12:53:14 +00:00
|
|
|
public function getSubTotalAttribute()
|
|
|
|
{
|
|
|
|
return $this->quantity * $this->price_base;
|
|
|
|
}
|
|
|
|
public function getTaxAttribute()
|
|
|
|
{
|
|
|
|
if (! $this->_tax)
|
|
|
|
{
|
|
|
|
foreach ($this->taxes as $o)
|
|
|
|
{
|
|
|
|
$this->_tax += $o->amount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->_tax;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTotalAttribute()
|
|
|
|
{
|
|
|
|
return $this->tax + $this->sub_total;
|
|
|
|
}
|
2019-07-04 04:55:05 +00:00
|
|
|
|
|
|
|
/** FUNCTIONS **/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add taxes to this record
|
|
|
|
*/
|
|
|
|
public function addTaxes()
|
|
|
|
{
|
|
|
|
// Refuse to change an existing record
|
|
|
|
if ($this->exists)
|
|
|
|
throw new \Exception('Refusing to add Taxes to existing record');
|
|
|
|
|
|
|
|
foreach($this->service->account->country->taxes as $to)
|
|
|
|
{
|
|
|
|
$iit = new InvoiceItemTax;
|
|
|
|
$iit->tax_id = $to->id;
|
|
|
|
$iit->amount = round($this->quantity*$this->price_base*$to->rate,3);
|
|
|
|
$this->taxes->push($iit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function module_text()
|
|
|
|
{
|
|
|
|
switch ($this->module_id)
|
|
|
|
{
|
|
|
|
// Charges Module
|
|
|
|
case 30: return Charge::findOrFail($this->module_ref)->name;
|
|
|
|
|
|
|
|
default: abort(500,'Unable to handle '.$this->module_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-05-20 12:53:14 +00:00
|
|
|
}
|