Orders are once again writeable

orders
Nisse Lommerde 1 month ago
parent b9346c4466
commit 2361ec0b88

@ -90,12 +90,12 @@ class OrderResource extends Resource
])->columnSpan(1), ])->columnSpan(1),
])->columns(2), ])->columns(2),
TableRepeater::make('Order Products') TableRepeater::make('order_products')
->relationship('orderProducts')
->label('Garments') ->label('Garments')
->schema([ ->schema([
TextInput::make('sku'), TextInput::make('sku'),
TextInput::make('product_name'), TextInput::make('product_name')
->required(),
TextInput::make('color'), TextInput::make('color'),
Cluster::make([ Cluster::make([
TextInput::make('xs') TextInput::make('xs')
@ -120,8 +120,7 @@ class OrderResource extends Resource
->reorderable() ->reorderable()
->cloneable(), ->cloneable(),
Repeater::make('Services') Repeater::make('services')
->relationship('productServices')
->label('Production Details') ->label('Production Details')
->schema([ ->schema([
Grid::make(19) Grid::make(19)
@ -137,18 +136,18 @@ class OrderResource extends Resource
->columnSpan(2), ->columnSpan(2),
TextInput::make('placement') TextInput::make('placement')
->columnSpan(3), ->columnSpan(3),
TextInput::make('service_file_name') TextInput::make('serviceFileName')
->columnSpan(3) ->columnSpan(3)
->label('Logo Name'), ->label('Logo Name'),
TextInput::make('service_file_setup_number') TextInput::make('serviceFileSetupNumber')
->label('Setup') ->label('Setup')
->columnSpan(1), ->columnSpan(1),
Cluster::make([ Cluster::make([
TextInput::make('service_file_width') TextInput::make('serviceFileWidth')
->prefix('w') ->prefix('w')
->placeholder('Width'), ->placeholder('Width'),
TextInput::make('service_file_height') TextInput::make('serviceFileHeight')
->prefix('h') ->prefix('h')
->placeholder('Height'), ->placeholder('Height'),
]) ])
@ -170,7 +169,7 @@ class OrderResource extends Resource
Grid::make(9) Grid::make(9)
->schema([ ->schema([
TextInput::make('service_file_code') TextInput::make('serviceFileCode')
->label('Code') ->label('Code')
->columnSpan(1) ->columnSpan(1)
->placeholder('A1234'), ->placeholder('A1234'),

@ -2,11 +2,80 @@
namespace App\Filament\Resources\OrderResource\Pages; namespace App\Filament\Resources\OrderResource\Pages;
use App\Enums\OrderAttributes;
use App\Filament\Resources\OrderResource; 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; use Filament\Resources\Pages\CreateRecord;
class CreateOrder extends CreateRecord class CreateOrder extends CreateRecord
{ {
protected static string $resource = OrderResource::class; 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;
}
} }

@ -15,6 +15,41 @@ class EditOrder extends EditRecord
protected function mutateFormDataBeforeFill(array $data): array 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) { foreach (OrderAttributes::cases() as $case) {
if ($data[$case->name]) { if ($data[$case->name]) {
$data['order_attributes'][] = $case->value ?? null; $data['order_attributes'][] = $case->value ?? null;

@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
class OrderProduct extends Model class OrderProduct extends Model
@ -35,14 +34,6 @@ class OrderProduct extends Model
return $this->belongsTo(Order::class); return $this->belongsTo(Order::class);
} }
/**
* @return HasOne<ServiceFile>
*/
// public function serviceFile(): HasOne
// {
// return $this->hasOne(ServiceFile::class);
// }
/** /**
* @return HasMany<ProductSize> * @return HasMany<ProductSize>
*/ */

@ -18,49 +18,16 @@ class ProductService extends Model
'service_file_id', 'service_file_id',
'service_type', 'service_type',
'placement', 'placement',
'width',
'height',
'code',
'setup_amount',
'logo_name',
'amount', 'amount',
'amount_price', 'amount_price',
'notes', '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<Order, self> * @return BelongsTo<Order, self>
*/ */

@ -14,6 +14,7 @@ return new class extends Migration
$table->string('sku')->nullable(); $table->string('sku')->nullable();
$table->string('product_name'); $table->string('product_name');
$table->string('color')->nullable(); $table->string('color')->nullable();
$table->softDeletes(); $table->softDeletes();
$table->timestamps(); $table->timestamps();
}); });

@ -12,8 +12,8 @@ return new class extends Migration
$table->id(); $table->id();
$table->foreignId('order_id'); $table->foreignId('order_id');
$table->foreignId('service_file_id')->nullable(); $table->foreignId('service_file_id')->nullable();
$table->string('service_type'); $table->string('service_type')->nullable();
$table->string('placement'); $table->string('placement')->nullable();
$table->string('amount')->nullable(); $table->string('amount')->nullable();
$table->string('amount_price')->nullable(); $table->string('amount_price')->nullable();
$table->string('notes')->nullable(); $table->string('notes')->nullable();

@ -22,8 +22,7 @@ class CustomerSeeder extends Seeder
->has(Contact::factory(rand(1, 5))) ->has(Contact::factory(rand(1, 5)))
->has(ShippingEntry::factory(rand(1, 3))) ->has(ShippingEntry::factory(rand(1, 3)))
->has(Order::factory(rand(5, 10)) ->has(Order::factory(rand(5, 10))
->has(OrderProduct::factory(rand(1, 3)) ->has(OrderProduct::factory(rand(1, 3)))
->has(productSize::factory(rand(1, 3))))
->has(ProductService::factory(rand(1, 5), [ ->has(ProductService::factory(rand(1, 5), [
'service_file_id' => ServiceFile::factory(), 'service_file_id' => ServiceFile::factory(),
])) ]))

@ -37,17 +37,17 @@
{{ {{
$attributes $attributes
->merge($getExtraAttributes(), escape: false) ->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'])
}} }}
> >
<div @class([ <div @class([
'filament-tables-header', // 'filament-tables-header',
'flex items-center h-10 overflow-hidden border-b bg-gray-50 rounded-t-xl', // 'flex items-center h-1 pt-2 overflow-hidden bg-white',
'dark:bg-gray-900 dark:border-gray-700', // 'dark:bg-gray-900 dark:border-gray-700',
])> ])>
<div class="flex-1"></div> <div class="flex-1 pt-2"></div>
@if ($isCollapsible) @if ($isCollapsible)
<div> <div>
<button <button

Loading…
Cancel
Save