2018-08-01 07:09:38 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2021-10-01 04:59:04 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2024-07-25 04:08:26 +00:00
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
use Leenooks\Traits\ScopeActive;
|
2018-08-01 07:09:38 +00:00
|
|
|
|
2024-07-07 09:10:00 +00:00
|
|
|
use App\Casts\CollectionOrNull;
|
2022-06-11 07:39:25 +00:00
|
|
|
|
2021-10-01 04:59:04 +00:00
|
|
|
/**
|
|
|
|
* CLEANUP NOTES:
|
2022-06-12 08:32:54 +00:00
|
|
|
* + 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.
|
2021-10-01 04:59:04 +00:00
|
|
|
*/
|
2018-08-01 07:09:38 +00:00
|
|
|
class Charge extends Model
|
|
|
|
{
|
2024-07-25 04:08:26 +00:00
|
|
|
use ScopeActive;
|
2022-06-11 07:39:25 +00:00
|
|
|
|
|
|
|
protected $casts = [
|
2024-07-07 09:10:00 +00:00
|
|
|
'attributes' => CollectionOrNull::class,
|
2024-07-25 04:08:26 +00:00
|
|
|
'start_at' => 'datetime:Y-m-d',
|
|
|
|
'stop_at' => 'datetime:Y-m-d',
|
|
|
|
'charge_at' => 'datetime:Y-m-d', // 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
|
|
|
|
2021-10-01 04:59:04 +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);
|
|
|
|
}
|
|
|
|
|
2021-10-01 04:59:04 +00:00
|
|
|
public function service()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Service::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SCOPES */
|
|
|
|
|
2024-07-05 02:04:08 +00:00
|
|
|
/** @deprecated use pending */
|
2021-10-01 04:59:04 +00:00
|
|
|
public function scopeUnprocessed($query)
|
|
|
|
{
|
2024-07-25 04:08:26 +00:00
|
|
|
Log::alert('UMO:! Deprecated function scopeUnprocessed()');
|
2024-07-05 02:04:08 +00:00
|
|
|
return $this->scopePending();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function scopePending($query) {
|
2021-10-01 04:59:04 +00:00
|
|
|
return $query
|
2024-07-05 02:04:08 +00:00
|
|
|
->active()
|
2022-07-25 13:59:28 +00:00
|
|
|
->whereNotNull('charge_at')
|
2021-10-01 04:59:04 +00:00
|
|
|
->whereNotNull('type')
|
2024-07-05 02:04:08 +00:00
|
|
|
->where(fn($query)=>$query->where('processed',FALSE)->orWhereNull('processed'));
|
2021-10-01 04:59:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* ATTRIBUTES */
|
|
|
|
|
2018-08-01 07:09:38 +00:00
|
|
|
public function getNameAttribute()
|
|
|
|
{
|
2024-07-25 04:08:26 +00:00
|
|
|
return sprintf('%s %s',
|
|
|
|
$this->description,
|
|
|
|
$this->getAttribute('attributes')
|
|
|
|
? join('|',unserialize($this->getAttribute('attributes')))
|
|
|
|
: '');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getSubTotalAttribute(): float
|
|
|
|
{
|
|
|
|
return $this->quantity*$this->amount;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getTotalAttribute(): float
|
|
|
|
{
|
|
|
|
return $this->account->taxed($this->getSubTotalAttribute());
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|
2021-10-01 04:59:04 +00:00
|
|
|
|
2021-12-16 00:55:59 +00:00
|
|
|
public function getTypeNameAttribute(): string
|
2021-10-01 04:59:04 +00:00
|
|
|
{
|
2022-06-12 08:32:54 +00:00
|
|
|
return Arr::get(InvoiceItem::type,$this->type);
|
2021-10-01 04:59:04 +00:00
|
|
|
}
|
2018-08-01 07:09:38 +00:00
|
|
|
}
|