You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.8 KiB
PHP
101 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Database\Factories\CustomerFactory;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
|
|
|
class Customer extends Model
|
|
{
|
|
/** @use HasFactory<CustomerFactory> */
|
|
use HasFactory, SoftDeletes;
|
|
|
|
protected $fillable = [
|
|
'company_name',
|
|
'internal_name',
|
|
'shipping_address',
|
|
'billing_address',
|
|
'phone',
|
|
];
|
|
|
|
protected $appends = [
|
|
'subtotal',
|
|
'gst',
|
|
'pst',
|
|
'total',
|
|
];
|
|
|
|
public function getSubtotalAttribute($created_at = null, $created_until = null): float
|
|
{
|
|
return $this->invoices()
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
->sum('subtotal');
|
|
}
|
|
|
|
public function getGstAttribute($created_at = null, $created_until = null): float
|
|
{
|
|
return $this->invoices()
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
->sum('total') * 0.05;
|
|
}
|
|
|
|
public function getPstAttribute($created_at = null, $created_until = null): float
|
|
{
|
|
return $this->invoices()
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
->where('pst', true)
|
|
->sum('total') * 0.07;
|
|
}
|
|
|
|
public function getTotalAttribute($created_at = null, $created_until = null): float
|
|
{
|
|
return $this->invoices()
|
|
->when($created_at, fn ($query) => $query->whereDate('created_at', '>=', $created_at))
|
|
->when($created_until, fn ($query) => $query->whereDate('created_at', '<=', $created_until))
|
|
->sum('total');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Contact>
|
|
*/
|
|
public function contacts(): HasMany
|
|
{
|
|
return $this->hasMany(Contact::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<PackingSlip>
|
|
*/
|
|
public function packingSlips(): HasMany
|
|
{
|
|
return $this->hasMany(PackingSlip::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<ShippingEntry>
|
|
*/
|
|
public function shippingEntries(): HasMany
|
|
{
|
|
return $this->hasMany(ShippingEntry::class);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<Order>
|
|
*/
|
|
public function orders(): HasMany
|
|
{
|
|
return $this->hasMany(Order::class);
|
|
}
|
|
|
|
public function invoices(): HasMany
|
|
{
|
|
return $this->hasMany(Invoice::class);
|
|
}
|
|
}
|