diff --git a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php index 28504bf..4d964d8 100644 --- a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php +++ b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php @@ -32,7 +32,7 @@ class CreateOrder extends CreateRecord $order = Order::create($data); - // Order Products + // Create Order Products foreach ($data['order_products'] as $product) { $orderProduct = OrderProduct::create([ 'sku' => $product['sku'], @@ -58,10 +58,10 @@ class CreateOrder extends CreateRecord foreach ($data['services'] as $service) { $serviceFile = ServiceFile::create([ 'name' => $service['serviceFileName'] ?? '', - 'width' => $service['serviceFileWidth'] ?? '', - 'height' => $service['serviceFileHeight'] ?? '', + 'width' => $service['serviceFileWidth'] ?? null, + 'height' => $service['serviceFileHeight'] ?? null, 'code' => $service['serviceFileCode'] ?? '', - 'setup_number' => $service['serviceFileSetupNumber'] ?? '', + 'setup_number' => $service['serviceFileSetupNumber'] ?? null, ]); ProductService::create([ diff --git a/app/Filament/Resources/OrderResource/Pages/EditOrder.php b/app/Filament/Resources/OrderResource/Pages/EditOrder.php index 00636a1..423068f 100644 --- a/app/Filament/Resources/OrderResource/Pages/EditOrder.php +++ b/app/Filament/Resources/OrderResource/Pages/EditOrder.php @@ -5,9 +5,14 @@ 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 { @@ -59,8 +64,9 @@ class EditOrder extends EditRecord return $data; } - protected function mutateFormDataBeforeSave(array $data): array + public function handleRecordUpdate(Model $record, array $data): Model { + // Correctly set attribute booleans foreach (OrderAttributes::cases() as $case) { $data[$case->name] = false; } @@ -73,7 +79,65 @@ class EditOrder extends EditRecord unset($data['order_attributes']); - return $data; + $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