osb/app/Models/Charge.php

85 lines
1.7 KiB
PHP
Raw Normal View History

2018-08-01 07:09:38 +00:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
2018-08-01 07:09:38 +00:00
2022-06-11 07:39:25 +00:00
use App\Traits\SiteID;
/**
* CLEANUP NOTES:
* + Charge Date should not be null
* + Attributes should be a collection array
* + type should not be null
* + It would be useful, given an array of Charges to call a function that renders them into invoice format. This may provide consistence and be the single view of how charges do look on an invoice.
*/
2018-08-01 07:09:38 +00:00
class Charge extends Model
{
use SiteID;
2022-06-11 07:39:25 +00:00
protected $casts = [
'attributes' => 'json',
];
2022-06-11 07:39:25 +00:00
protected $dates = [
'start_at',
'stop_at',
2022-06-13 04:34:45 +00:00
'charge_at', // The date the charge applies - since it can be different to created_at
2022-06-11 07:39:25 +00:00
];
2018-08-01 07:09:38 +00:00
public const sweep = [
// 0 => 'Daily',
// 1 => 'Weekly',
// 2 => 'Monthly',
// 3 => 'Quarterly',
// 4 => 'Semi-Annually',
// 5 => 'Annually',
6 => 'Service Rebill',
];
/* RELATIONS */
public function account()
{
return $this->belongsTo(Account::class);
}
2022-06-11 07:39:25 +00:00
public function product()
{
return $this->belongsTo(Product::class);
}
public function service()
{
return $this->belongsTo(Service::class);
}
/* SCOPES */
/** @deprecated use pending */
public function scopeUnprocessed($query)
{
return $this->scopePending();
}
public function scopePending($query) {
return $query
->active()
2022-07-25 13:59:28 +00:00
->whereNotNull('charge_at')
->whereNotNull('type')
->where(fn($query)=>$query->where('processed',FALSE)->orWhereNull('processed'));
}
/* ATTRIBUTES */
2018-08-01 07:09:38 +00:00
public function getNameAttribute()
{
return sprintf('%s %s',$this->description,$this->getAttribute('attributes') ? join('|',unserialize($this->getAttribute('attributes'))) : '');
2018-08-01 07:09:38 +00:00
}
public function getTypeNameAttribute(): string
{
return Arr::get(InvoiceItem::type,$this->type);
}
2018-08-01 07:09:38 +00:00
}