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

113 lines
2.4 KiB
PHTML

3 months ago
<?php
namespace App\Models;
use App\Enums\OrderStatus;
use DateTimeInterface;
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;
class Order extends Model
{
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
'rush',
'new_art',
'digitizing',
'repeat',
'purchased_garments',
'customer_supplied_file',
'notes',
];
protected $appends = [
3 months ago
'active',
3 months ago
];
3 months ago
public static function boot()
{
parent::boot();
static::created(function ($model) {
$model->attributes['internal_po'] = $model->generateInternalPo($model->id);
$model->save();
});
}
public function generateInternalPo($id): string
{
$po = str_pad($id, 4, '0', STR_PAD_LEFT);
$year = date('y');
return 'TN'.$year.'-'.$po;
}
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;
}
public function scopeActive($query)
{
return $query->where('status', 'approved')
->orWhere('status', 'production');
}
public function scopeFinished($query)
{
return $query->where('status', 'shipped')
->orWhere('status', 'completed');
}
public function scopeInvoiced($query)
{
return $query->where('status', 'invoiced');
}
public function scopeRush($query)
{
return $query->where('rush', true);
3 months ago
}
public function customer(): BelongsTo
{
return $this->belongsTo(Customer::class);
}
3 months ago
public function orderProducts(): HasMany
{
return $this->hasMany(OrderProduct::class);
}
public function productServices(): HasMany
{
return $this->hasMany(ProductService::class);
}
3 months ago
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d');
}
protected $casts = [
3 months ago
'status' => OrderStatus::class,
3 months ago
];
}