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.
155 lines
5.9 KiB
PHP
155 lines
5.9 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\OrderResource\Pages;
|
|
|
|
use App\Enums\OrderAttributes;
|
|
use App\Filament\Resources\OrderResource;
|
|
use App\Models\Order;
|
|
use App\Models\OrderProduct;
|
|
use App\Models\ProductService;
|
|
use App\Models\ProductSize;
|
|
use App\Models\ServiceFile;
|
|
use Filament\Actions;
|
|
use Filament\Actions\Action;
|
|
use Filament\Resources\Pages\EditRecord;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
class EditOrder extends EditRecord
|
|
{
|
|
protected static string $resource = OrderResource::class;
|
|
|
|
protected function mutateFormDataBeforeFill(array $data): array
|
|
{
|
|
$order = Order::findOrFail($data['id']);
|
|
|
|
// Order Products
|
|
foreach ($order->orderProducts as $key => $product) {
|
|
$data['order_products'][$key] = [
|
|
'sku' => $product->sku,
|
|
'product_name' => $product->product_name,
|
|
'color' => $product->color,
|
|
'xs' => $product->productSizes->where('size', 'xs')->first()->amount ?? null,
|
|
's' => $product->productSizes->where('size', 's')->first()->amount ?? null,
|
|
'm' => $product->productSizes->where('size', 'm')->first()->amount ?? null,
|
|
'l' => $product->productSizes->where('size', 'l')->first()->amount ?? null,
|
|
'xl' => $product->productSizes->where('size', 'xl')->first()->amount ?? null,
|
|
'2xl' => $product->productSizes->where('size', '2xl')->first()->amount ?? null,
|
|
'3xl' => $product->productSizes->where('size', '3xl')->first()->amount ?? null,
|
|
'osfa' => $product->productSizes->where('size', 'osfa')->first()->amount ?? null,
|
|
];
|
|
}
|
|
|
|
// Product Services
|
|
foreach ($order->productServices as $key => $service) {
|
|
$data['services'][$key] = [
|
|
'service_type' => $service->service_type ?? '',
|
|
'placement' => $service->placement ?? '',
|
|
'amount' => $service->amount ?? '',
|
|
'amount_price' => $service->amount_price ?? '',
|
|
'notes' => $service->notes ?? '',
|
|
'serviceFileName' => $service->serviceFile->name ?? '',
|
|
'serviceFileWidth' => $service->serviceFile->width ?? '',
|
|
'serviceFileHeight' => $service->serviceFile->height ?? '',
|
|
'serviceFileCode' => $service->serviceFile->code ?? '',
|
|
'serviceFileSetupNumber' => $service->serviceFile->setup_number ?? '',
|
|
];
|
|
}
|
|
|
|
foreach (OrderAttributes::cases() as $case) {
|
|
if ($data[$case->name]) {
|
|
$data['order_attributes'][] = $case->value ?? null;
|
|
}
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
public function handleRecordUpdate(Model $record, array $data): Model
|
|
{
|
|
// Correctly set attribute booleans
|
|
foreach (OrderAttributes::cases() as $case) {
|
|
$data[$case->name] = false;
|
|
}
|
|
|
|
$data['order_attributes'] = array_filter($data['order_attributes']);
|
|
|
|
foreach ($data['order_attributes'] as $attribute) {
|
|
$data[OrderAttributes::from($attribute)->name] = true;
|
|
}
|
|
|
|
unset($data['order_attributes']);
|
|
|
|
$record->update($data);
|
|
|
|
// Delete old and create new Order Products
|
|
foreach ($record->orderProducts as $product) {
|
|
foreach ($product->productSizes as $size) {
|
|
$size->delete();
|
|
}
|
|
|
|
$product->delete();
|
|
}
|
|
|
|
foreach ($data['order_products'] as $product) {
|
|
$orderProduct = OrderProduct::create([
|
|
'sku' => $product['sku'],
|
|
'product_name' => $product['product_name'],
|
|
'color' => $product['color'],
|
|
'order_id' => $record->id,
|
|
]);
|
|
|
|
$sizes = ['xs', 's', 'm', 'l', 'xl', '2xl', '3xl', 'osfa'];
|
|
|
|
foreach ($sizes as $size) {
|
|
if ($product[$size] > 0) {
|
|
ProductSize::create([
|
|
'amount' => $product[$size],
|
|
'size' => $size,
|
|
'order_product_id' => $orderProduct->id,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Delete old and create new services
|
|
foreach ($record->productServices as $service) {
|
|
$service->delete();
|
|
}
|
|
|
|
foreach ($data['services'] as $service) {
|
|
$serviceFile = ServiceFile::create([
|
|
'name' => $service['serviceFileName'] ?? '',
|
|
'width' => $service['serviceFileWidth'] ?? null,
|
|
'height' => $service['serviceFileHeight'] ?? null,
|
|
'code' => $service['serviceFileCode'] ?? '',
|
|
'setup_number' => $service['serviceFileSetupNumber'] ?? null,
|
|
]);
|
|
|
|
ProductService::create([
|
|
'service_type' => $service['service_type'] ?? null,
|
|
'placement' => $service['placement'] ?? null,
|
|
'amount' => $service['amount'] ?? null,
|
|
'amount_price' => $service['amount_price'] ?? null,
|
|
'total_price' => $service['total_price'] ?? null,
|
|
'notes' => $service['notes'] ?? null,
|
|
'service_file_id' => $serviceFile->id,
|
|
'order_id' => $record->id,
|
|
]);
|
|
}
|
|
|
|
return $record;
|
|
}
|
|
|
|
protected function getHeaderActions(): array
|
|
{
|
|
return [
|
|
Action::make('print')
|
|
->icon('heroicon-s-printer')
|
|
->url(fn (Order $record) => route('orders.pdf', $record))
|
|
->openUrlInNewTab(),
|
|
Actions\DeleteAction::make()
|
|
->icon('heroicon-s-trash'),
|
|
];
|
|
}
|
|
}
|