*/ use HasFactory, SoftDeletes; protected $fillable = [ 'customer_id', 'contact_id', 'internal_po', 'customer_po', 'invoice_id', 'order_date', 'order_type', 'status', 'due_date', 'notes', 'rush', 'repeat', 'new_art', 'event', 'digitizing', 'garments', 'supplied_file', 'printed', 'pre_production', ]; protected $appends = [ 'total_service_price', 'total_product_quantity', 'is_alert_warning', 'is_alert_danger', ]; protected $casts = [ 'status' => OrderStatus::class, 'order_type' => OrderType::class, ]; public static function boot(): void { 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 { $po = str_pad(strval($id), 4, '0', STR_PAD_LEFT); $year = date('y'); return 'TN'.$year.'-'.$po; } public function getIsAlertWarningAttribute(): bool { if ($this->rush) { return ! ($this->status === OrderStatus::INVOICED || $this->status === OrderStatus::SHIPPED); } return false; } public function getIsAlertDangerAttribute(): bool { return $this->due_date <= today() && $this->status !== OrderStatus::INVOICED && $this->status !== OrderStatus::SHIPPED; } public function getTotalProductQuantityAttribute(): int { $total = 0; foreach ($this->orderProducts as $product) { $total += $product->totalQuantity(); } return $total; } public function getTotalServicePriceAttribute(): float { $total = 0; foreach ($this->productServices as $service) { $total += $service->amount * $service->amount_price; } return number_format($total, 2); } public function active(): bool { if ($this->status == OrderStatus::APPROVED || $this->status == OrderStatus::PRODUCTION) { return true; } return false; } /** * @param Builder $query * @return Builder */ public function scopeActive(Builder $query): Builder { return $query->where('status', 'approved') ->orWhere('status', 'production'); } /** * @param Builder $query * @return Builder */ public function scopeFinished(Builder $query): Builder { return $query->where('status', 'shipped') ->orWhere('status', 'completed'); } /** * @param Builder $query * @return Builder */ public function scopeInvoiced(Builder $query): Builder { return $query->where('status', 'invoiced'); } /** * @param Builder $query * @return Builder */ public function scopeRush(Builder $query): Builder { return $query->where('rush', true); } /** * @return BelongsTo */ public function customer(): BelongsTo { return $this->belongsTo(Customer::class); } /** * @return BelongsTo */ public function contact(): BelongsTo { return $this->belongsTo(Contact::class); } /** * @return HasMany */ public function orderProducts(): HasMany { return $this->hasMany(OrderProduct::class); } /** * @return HasMany */ public function productServices(): HasMany { return $this->hasMany(ProductService::class); } public function packingSlips(): HasMany { return $this->hasMany(PackingSlip::class); } public function quote(): HasOne { return $this->hasOne(Quote::class); } public function invoice(): BelongsTo { return $this->belongsTo(Invoice::class); } protected function serializeDate(DateTimeInterface $date): string { return $date->format('Y-m-d'); } }