From 2361ec0b8823d4eb3f00983261ac6717aa067f04 Mon Sep 17 00:00:00 2001 From: Nisse Lommerde Date: Fri, 25 Oct 2024 11:30:20 -0400 Subject: [PATCH] Orders are once again writeable --- app/Filament/Resources/OrderResource.php | 19 +++-- .../OrderResource/Pages/CreateOrder.php | 71 ++++++++++++++++++- .../OrderResource/Pages/EditOrder.php | 35 +++++++++ app/Models/OrderProduct.php | 9 --- app/Models/ProductService.php | 43 ++--------- ..._10_224439_create_order_products_table.php | 1 + ...0_224947_create_product_services_table.php | 4 +- database/seeders/CustomerSeeder.php | 3 +- .../table-repeater.blade.php | 10 +-- 9 files changed, 128 insertions(+), 67 deletions(-) diff --git a/app/Filament/Resources/OrderResource.php b/app/Filament/Resources/OrderResource.php index af448ce..6c6fb92 100644 --- a/app/Filament/Resources/OrderResource.php +++ b/app/Filament/Resources/OrderResource.php @@ -90,12 +90,12 @@ class OrderResource extends Resource ])->columnSpan(1), ])->columns(2), - TableRepeater::make('Order Products') - ->relationship('orderProducts') + TableRepeater::make('order_products') ->label('Garments') ->schema([ TextInput::make('sku'), - TextInput::make('product_name'), + TextInput::make('product_name') + ->required(), TextInput::make('color'), Cluster::make([ TextInput::make('xs') @@ -120,8 +120,7 @@ class OrderResource extends Resource ->reorderable() ->cloneable(), - Repeater::make('Services') - ->relationship('productServices') + Repeater::make('services') ->label('Production Details') ->schema([ Grid::make(19) @@ -137,18 +136,18 @@ class OrderResource extends Resource ->columnSpan(2), TextInput::make('placement') ->columnSpan(3), - TextInput::make('service_file_name') + TextInput::make('serviceFileName') ->columnSpan(3) ->label('Logo Name'), - TextInput::make('service_file_setup_number') + TextInput::make('serviceFileSetupNumber') ->label('Setup') ->columnSpan(1), Cluster::make([ - TextInput::make('service_file_width') + TextInput::make('serviceFileWidth') ->prefix('w') ->placeholder('Width'), - TextInput::make('service_file_height') + TextInput::make('serviceFileHeight') ->prefix('h') ->placeholder('Height'), ]) @@ -170,7 +169,7 @@ class OrderResource extends Resource Grid::make(9) ->schema([ - TextInput::make('service_file_code') + TextInput::make('serviceFileCode') ->label('Code') ->columnSpan(1) ->placeholder('A1234'), diff --git a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php index a9e137a..28504bf 100644 --- a/app/Filament/Resources/OrderResource/Pages/CreateOrder.php +++ b/app/Filament/Resources/OrderResource/Pages/CreateOrder.php @@ -2,11 +2,80 @@ namespace App\Filament\Resources\OrderResource\Pages; +use App\Enums\OrderAttributes; use App\Filament\Resources\OrderResource; -use Filament\Actions; +use App\Models\Order; +use App\Models\OrderProduct; +use App\Models\ProductService; +use App\Models\ProductSize; +use App\Models\ServiceFile; use Filament\Resources\Pages\CreateRecord; class CreateOrder extends CreateRecord { protected static string $resource = OrderResource::class; + + protected function handleRecordCreation(array $data): Order + { + // Attributes + 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']); + + $order = Order::create($data); + + // Order Products + foreach ($data['order_products'] as $product) { + $orderProduct = OrderProduct::create([ + 'sku' => $product['sku'], + 'product_name' => $product['product_name'], + 'color' => $product['color'], + 'order_id' => $order->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, + ]); + } + } + } + + // ProductServices and ServiceFiles + foreach ($data['services'] as $service) { + $serviceFile = ServiceFile::create([ + 'name' => $service['serviceFileName'] ?? '', + 'width' => $service['serviceFileWidth'] ?? '', + 'height' => $service['serviceFileHeight'] ?? '', + 'code' => $service['serviceFileCode'] ?? '', + 'setup_number' => $service['serviceFileSetupNumber'] ?? '', + ]); + + 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' => $order->id, + ]); + } + + return $order; + } } diff --git a/app/Filament/Resources/OrderResource/Pages/EditOrder.php b/app/Filament/Resources/OrderResource/Pages/EditOrder.php index f57895a..00636a1 100644 --- a/app/Filament/Resources/OrderResource/Pages/EditOrder.php +++ b/app/Filament/Resources/OrderResource/Pages/EditOrder.php @@ -15,6 +15,41 @@ class EditOrder extends EditRecord 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; diff --git a/app/Models/OrderProduct.php b/app/Models/OrderProduct.php index 7809d61..b00f3ea 100644 --- a/app/Models/OrderProduct.php +++ b/app/Models/OrderProduct.php @@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; -use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\SoftDeletes; class OrderProduct extends Model @@ -35,14 +34,6 @@ class OrderProduct extends Model return $this->belongsTo(Order::class); } - /** - * @return HasOne - */ - // public function serviceFile(): HasOne - // { - // return $this->hasOne(ServiceFile::class); - // } - /** * @return HasMany */ diff --git a/app/Models/ProductService.php b/app/Models/ProductService.php index a390292..e0762c6 100644 --- a/app/Models/ProductService.php +++ b/app/Models/ProductService.php @@ -18,49 +18,16 @@ class ProductService extends Model 'service_file_id', 'service_type', 'placement', + 'width', + 'height', + 'code', + 'setup_amount', + 'logo_name', 'amount', 'amount_price', 'notes', ]; - protected $appends = [ - 'service_file_name', - 'service_file_setup_number', - 'service_file_width', - 'service_file_height', - 'service_file_code', - ]; - - public function getServiceFileNameAttribute(): string - { - return $this->serviceFile->name ?? ''; - } - - public function getServiceFileSetupNumberAttribute(): string - { - return $this->serviceFile->setup_number ?? ''; - } - - public function getServiceFileWidthAttribute(): string - { - return $this->serviceFile->width ?? ''; - } - - public function getServiceFileHeightAttribute(): string - { - return $this->serviceFile->height ?? ''; - } - - public function getServiceFileCodeAttribute(): string - { - return $this->serviceFile->code ?? ''; - } - - public function setServiceFileName(string $name): void - { - $this->serviceFile->name = $name; - } - /** * @return BelongsTo */ diff --git a/database/migrations/2024_09_10_224439_create_order_products_table.php b/database/migrations/2024_09_10_224439_create_order_products_table.php index 4e1ccd6..a9ec916 100644 --- a/database/migrations/2024_09_10_224439_create_order_products_table.php +++ b/database/migrations/2024_09_10_224439_create_order_products_table.php @@ -14,6 +14,7 @@ return new class extends Migration $table->string('sku')->nullable(); $table->string('product_name'); $table->string('color')->nullable(); + $table->softDeletes(); $table->timestamps(); }); diff --git a/database/migrations/2024_09_10_224947_create_product_services_table.php b/database/migrations/2024_09_10_224947_create_product_services_table.php index d369349..ae6f271 100644 --- a/database/migrations/2024_09_10_224947_create_product_services_table.php +++ b/database/migrations/2024_09_10_224947_create_product_services_table.php @@ -12,8 +12,8 @@ return new class extends Migration $table->id(); $table->foreignId('order_id'); $table->foreignId('service_file_id')->nullable(); - $table->string('service_type'); - $table->string('placement'); + $table->string('service_type')->nullable(); + $table->string('placement')->nullable(); $table->string('amount')->nullable(); $table->string('amount_price')->nullable(); $table->string('notes')->nullable(); diff --git a/database/seeders/CustomerSeeder.php b/database/seeders/CustomerSeeder.php index e6b7f82..cd82314 100644 --- a/database/seeders/CustomerSeeder.php +++ b/database/seeders/CustomerSeeder.php @@ -22,8 +22,7 @@ class CustomerSeeder extends Seeder ->has(Contact::factory(rand(1, 5))) ->has(ShippingEntry::factory(rand(1, 3))) ->has(Order::factory(rand(5, 10)) - ->has(OrderProduct::factory(rand(1, 3)) - ->has(productSize::factory(rand(1, 3)))) + ->has(OrderProduct::factory(rand(1, 3))) ->has(ProductService::factory(rand(1, 5), [ 'service_file_id' => ServiceFile::factory(), ])) diff --git a/resources/views/vendor/filament-table-repeater/table-repeater.blade.php b/resources/views/vendor/filament-table-repeater/table-repeater.blade.php index 6a0edd4..ed05b09 100644 --- a/resources/views/vendor/filament-table-repeater/table-repeater.blade.php +++ b/resources/views/vendor/filament-table-repeater/table-repeater.blade.php @@ -37,17 +37,17 @@ {{ $attributes ->merge($getExtraAttributes(), escape: false) - ->class(['bg-white border border-gray-300 shadow-sm rounded-xl relative dark:bg-gray-900 dark:border-gray-700']) + ->class(['bg-white border border-gray-150 rounded-xl relative dark:bg-gray-900 dark:border-gray-700']) }} >
-
+
@if ($isCollapsible)