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.
topnotch_website/app/Models/Customer.php

101 lines
2.8 KiB
PHTML

3 months ago
<?php
namespace App\Models;
use Database\Factories\CustomerFactory;
3 months ago
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
3 months ago
use Illuminate\Database\Eloquent\Relations\HasMany;
3 months ago
use Illuminate\Database\Eloquent\SoftDeletes;
class Customer extends Model
{
/** @use HasFactory<CustomerFactory> */
3 months ago
use HasFactory, SoftDeletes;
3 months ago
protected $fillable = [
'company_name',
'internal_name',
'shipping_address',
'billing_address',
'phone',
];
3 months ago
2 weeks ago
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>
*/
3 months ago
public function contacts(): HasMany
{
return $this->hasMany(Contact::class);
}
/**
* @return HasMany<PackingSlip>
*/
3 months ago
public function packingSlips(): HasMany
{
return $this->hasMany(PackingSlip::class);
}
/**
* @return HasMany<ShippingEntry>
*/
3 months ago
public function shippingEntries(): HasMany
{
return $this->hasMany(ShippingEntry::class);
}
/**
* @return HasMany<Order>
*/
3 months ago
public function orders(): HasMany
{
return $this->hasMany(Order::class);
}
3 weeks ago
public function invoices(): HasMany
{
2 weeks ago
return $this->hasMany(Invoice::class);
3 weeks ago
}
3 months ago
}