205 lines
4.6 KiB
PHP
Raw Normal View History

2024-09-03 15:15:57 -07:00
<?php
namespace App\Models;
use App\Enums\OrderStatus;
2024-11-09 15:13:04 -05:00
use App\Enums\OrderType;
2024-09-18 16:00:06 -07:00
use Database\Factories\OrderFactory;
2024-09-03 15:15:57 -07:00
use DateTimeInterface;
2024-09-18 16:00:06 -07:00
use Illuminate\Database\Eloquent\Builder;
2024-09-03 15:15:57 -07:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
2024-09-09 15:29:31 -07:00
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
2024-09-03 15:15:57 -07:00
use Illuminate\Database\Eloquent\SoftDeletes;
2024-09-20 12:12:45 -07:00
use Illuminate\Support\Carbon;
2024-09-03 15:15:57 -07:00
class Order extends Model
{
2024-09-18 16:00:06 -07:00
/** @use HasFactory<OrderFactory> */
2024-09-09 15:29:31 -07:00
use HasFactory, SoftDeletes;
2024-09-03 15:15:57 -07:00
protected $fillable = [
'customer_id',
2024-09-09 15:29:31 -07:00
'contact_id',
2024-09-03 15:15:57 -07:00
'internal_po',
'customer_po',
'invoice_id',
2024-09-03 15:15:57 -07:00
'order_date',
2024-09-09 15:29:31 -07:00
'order_type',
2024-09-03 15:15:57 -07:00
'status',
2024-09-09 15:29:31 -07:00
'due_date',
2024-09-03 15:15:57 -07:00
'notes',
2024-10-22 12:48:05 -04:00
'rush',
'repeat',
'new_art',
'event',
'digitizing',
'garments',
'supplied_file',
'printed',
'pre_production',
2024-09-03 15:15:57 -07:00
];
2024-11-05 11:35:23 -05:00
protected $appends = [
'total_service_price',
2024-11-07 12:22:20 -05:00
'total_product_quantity',
2024-11-05 11:35:23 -05:00
];
2024-10-10 15:15:30 -07:00
protected $casts = [
2024-11-09 15:13:04 -05:00
'status' => OrderStatus::class,
'order_type' => OrderType::class,
2024-09-03 15:15:57 -07:00
];
2024-09-18 16:00:06 -07:00
public static function boot(): void
2024-09-09 15:29:31 -07:00
{
parent::boot();
static::created(function ($model) {
$model->attributes['internal_po'] = $model->generateInternalPo($model->id);
$model->save();
});
}
2024-09-20 12:12:45 -07:00
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');
}
2024-09-18 16:00:06 -07:00
public function generateInternalPo(int $id): string
2024-09-09 15:29:31 -07:00
{
2024-09-18 16:00:06 -07:00
$po = str_pad(strval($id), 4, '0', STR_PAD_LEFT);
2024-09-09 15:29:31 -07:00
$year = date('y');
return 'TN'.$year.'-'.$po;
}
2024-11-07 12:22:20 -05:00
public function getTotalProductQuantityAttribute(): int
2024-09-19 11:52:04 -07:00
{
$total = 0;
foreach ($this->orderProducts as $product) {
$total += $product->totalQuantity();
}
return $total;
}
2024-11-19 15:54:02 -05:00
public function getTotalServicePriceAttribute(): float
2024-09-19 11:52:04 -07:00
{
$total = 0;
foreach ($this->productServices as $service) {
$total += $service->amount * $service->amount_price;
}
return number_format($total, 2);
}
2024-09-09 15:29:31 -07:00
public function active(): bool
2024-09-03 15:15:57 -07:00
{
2024-09-09 15:29:31 -07:00
if ($this->status == OrderStatus::APPROVED
|| $this->status == OrderStatus::PRODUCTION) {
return true;
2024-09-03 15:15:57 -07:00
}
2024-09-09 15:29:31 -07:00
return false;
}
2024-09-18 16:00:06 -07:00
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeActive(Builder $query): Builder
2024-09-09 15:29:31 -07:00
{
return $query->where('status', 'approved')
->orWhere('status', 'production');
}
2024-09-18 16:00:06 -07:00
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeFinished(Builder $query): Builder
2024-09-09 15:29:31 -07:00
{
return $query->where('status', 'shipped')
->orWhere('status', 'completed');
}
2024-09-18 16:00:06 -07:00
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeInvoiced(Builder $query): Builder
2024-09-09 15:29:31 -07:00
{
return $query->where('status', 'invoiced');
}
2024-09-18 16:00:06 -07:00
/**
* @param Builder<Order> $query
* @return Builder<Order>
*/
public function scopeRush(Builder $query): Builder
2024-09-09 15:29:31 -07:00
{
return $query->where('rush', true);
2024-09-03 15:15:57 -07:00
}
2024-09-18 16:00:06 -07:00
/**
* @return BelongsTo<Customer, self>
*/
2024-09-03 15:15:57 -07:00
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
2024-09-19 11:52:04 -07:00
/**
* @return BelongsTo<Contact, self>
*/
public function contact(): BelongsTo
{
return $this->belongsTo(Contact::class);
}
2024-09-18 16:00:06 -07:00
/**
* @return HasMany<OrderProduct>
*/
2024-09-09 15:29:31 -07:00
public function orderProducts(): HasMany
{
return $this->hasMany(OrderProduct::class);
}
2024-09-18 16:00:06 -07:00
/**
* @return HasMany<ProductService>
*/
2024-09-09 15:29:31 -07:00
public function productServices(): HasMany
{
return $this->hasMany(ProductService::class);
}
2024-10-10 15:15:30 -07:00
public function packingSlips(): HasMany
{
return $this->hasMany(PackingSlip::class);
}
public function quote(): HasOne
{
return $this->hasOne(Quote::class);
}
2024-11-05 11:35:23 -05:00
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}
2024-09-03 15:15:57 -07:00
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d');
}
}