Order CRUD complete

orders
Nisse Lommerde 1 month ago
parent e79d1839fe
commit b3868e8b0a

@ -32,7 +32,7 @@ class CreateOrder extends CreateRecord
$order = Order::create($data); $order = Order::create($data);
// Order Products // Create Order Products
foreach ($data['order_products'] as $product) { foreach ($data['order_products'] as $product) {
$orderProduct = OrderProduct::create([ $orderProduct = OrderProduct::create([
'sku' => $product['sku'], 'sku' => $product['sku'],
@ -58,10 +58,10 @@ class CreateOrder extends CreateRecord
foreach ($data['services'] as $service) { foreach ($data['services'] as $service) {
$serviceFile = ServiceFile::create([ $serviceFile = ServiceFile::create([
'name' => $service['serviceFileName'] ?? '', 'name' => $service['serviceFileName'] ?? '',
'width' => $service['serviceFileWidth'] ?? '', 'width' => $service['serviceFileWidth'] ?? null,
'height' => $service['serviceFileHeight'] ?? '', 'height' => $service['serviceFileHeight'] ?? null,
'code' => $service['serviceFileCode'] ?? '', 'code' => $service['serviceFileCode'] ?? '',
'setup_number' => $service['serviceFileSetupNumber'] ?? '', 'setup_number' => $service['serviceFileSetupNumber'] ?? null,
]); ]);
ProductService::create([ ProductService::create([

@ -5,9 +5,14 @@ namespace App\Filament\Resources\OrderResource\Pages;
use App\Enums\OrderAttributes; use App\Enums\OrderAttributes;
use App\Filament\Resources\OrderResource; use App\Filament\Resources\OrderResource;
use App\Models\Order; 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;
use Filament\Actions\Action; use Filament\Actions\Action;
use Filament\Resources\Pages\EditRecord; use Filament\Resources\Pages\EditRecord;
use Illuminate\Database\Eloquent\Model;
class EditOrder extends EditRecord class EditOrder extends EditRecord
{ {
@ -59,8 +64,9 @@ class EditOrder extends EditRecord
return $data; 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) { foreach (OrderAttributes::cases() as $case) {
$data[$case->name] = false; $data[$case->name] = false;
} }
@ -73,7 +79,65 @@ class EditOrder extends EditRecord
unset($data['order_attributes']); 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 protected function getHeaderActions(): array

Loading…
Cancel
Save