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/Order.php

179 lines
4.0 KiB
PHTML

3 months ago
<?php
namespace App\Models;
use App\Enums\OrderStatus;
use Database\Factories\OrderFactory;
3 months ago
use DateTimeInterface;
use Illuminate\Database\Eloquent\Builder;
3 months ago
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
3 months ago
use Illuminate\Database\Eloquent\Relations\HasMany;
3 months ago
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Carbon;
3 months ago
class Order extends Model
{
/** @use HasFactory<OrderFactory> */
3 months ago
use HasFactory, SoftDeletes;
3 months ago
protected $fillable = [
'customer_id',
3 months ago
'contact_id',
3 months ago
'internal_po',
'customer_po',
'order_date',
3 months ago
'order_type',
3 months ago
'status',
3 months ago
'due_date',
3 months ago
'notes',
2 months ago
'order_attributes',
3 months ago
];
2 months ago
protected $casts = [
'status' => OrderStatus::class,
'order_attributes' => 'array',
3 months ago
];
public static function boot(): void
3 months ago
{
parent::boot();
static::created(function ($model) {
$model->attributes['internal_po'] = $model->generateInternalPo($model->id);
$model->save();
});
}
public function dueDatePdf(): string
{
return Carbon::createFromDate($this->due_date)->format('M d, Y');
}
public function orderDatePdf(): string
{
return Carbon::createFromDate($this->order_date)->format('M d, Y');
}
public function generateInternalPo(int $id): string
3 months ago
{
$po = str_pad(strval($id), 4, '0', STR_PAD_LEFT);
3 months ago
$year = date('y');
return 'TN'.$year.'-'.$po;
}
2 months ago
public function totalProductQuantity(): int
{
$total = 0;
foreach ($this->orderProducts as $product) {
$total += $product->totalQuantity();
}
return $total;
}
public function totalServicePrice(): string
{
$total = 0;
foreach ($this->productServices as $service) {
$total += $service->amount * $service->amount_price;
}
return number_format($total, 2);
}
3 months ago
public function active(): bool
3 months ago
{
3 months ago
if ($this->status == OrderStatus::APPROVED
|| $this->status == OrderStatus::PRODUCTION) {
return true;
3 months ago
}
3 months ago
return false;
}
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeActive(Builder $query): Builder
3 months ago
{
return $query->where('status', 'approved')
->orWhere('status', 'production');
}
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeFinished(Builder $query): Builder
3 months ago
{
return $query->where('status', 'shipped')
->orWhere('status', 'completed');
}
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeInvoiced(Builder $query): Builder
3 months ago
{
return $query->where('status', 'invoiced');
}
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeRush(Builder $query): Builder
3 months ago
{
return $query->where('rush', true);
3 months ago
}
/**
* @return BelongsTo<Customer, self>
*/
3 months ago
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
2 months ago
/**
* @return BelongsTo<Contact, self>
*/
public function contact(): BelongsTo
{
return $this->belongsTo(Contact::class);
}
/**
* @return HasMany<OrderProduct>
*/
3 months ago
public function orderProducts(): HasMany
{
return $this->hasMany(OrderProduct::class);
}
/**
* @return HasMany<ProductService>
*/
3 months ago
public function productServices(): HasMany
{
return $this->hasMany(ProductService::class);
}
2 months ago
public function packingSlips(): HasMany
{
return $this->hasMany(PackingSlip::class);
}
3 months ago
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d');
}
}