200 lines
4.7 KiB
PHP
Raw Normal View History

2024-09-03 15:15:57 -07:00
<?php
namespace App\Models;
use App\Enums\OrderStatus;
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;
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-10-23 15:45:10 -04:00
use Spatie\Browsershot\Browsershot;
use Spatie\LaravelPdf\Facades\Pdf;
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',
'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',
2024-09-03 15:15:57 -07:00
];
2024-10-10 15:15:30 -07:00
protected $casts = [
2024-10-22 12:48:05 -04:00
'status' => OrderStatus::class,
// 'order_attributes' => 'array',
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-09-19 11:52:04 -07:00
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);
}
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-10-23 15:45:10 -04:00
public function generatePdf(): \Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
{
Pdf::view('pdf.order', ['order' => $this])
->withBrowsershot(function (Browsershot $browsershot) {
$browsershot->noSandbox();
})
->margins(8, 8, 15, 8)
->footerView('pdf.order-footer', ['order' => $this])
->save('order.pdf');
return redirect('order.pdf');
}
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);
}
2024-09-03 15:15:57 -07:00
protected function serializeDate(DateTimeInterface $date): string
{
return $date->format('Y-m-d');
}
}