feat(products): enhance product form data with additional fields

Added support for new product attributes including tax rate, invoicing unit, order unit, and orderable dates. Updated related queries and views to accommodate these changes.
This commit is contained in:
Jérémy Munsch 2026-11-14 16:25:59 +01:00
parent 97f6085622
commit 93e4b202da
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
8 changed files with 127 additions and 26 deletions

View file

@ -49,7 +49,7 @@ final readonly class GetProductFormDataQuery implements Query
$product = $input->productId !== null
? Product::query()
->with(['tags', 'inco'])
->with(['tags', 'inco', 'range'])
->where('id', $input->productId->value->toString())
->first()
: null;
@ -91,6 +91,7 @@ final readonly class GetProductFormDataQuery implements Query
tags: $tags,
existingProds: $existingProds,
selectedProds: $selectedProds,
product: $product !== null ? $this->assembler->toDto($product) : null,
);
}
}

View file

@ -5,11 +5,16 @@ declare(strict_types=1);
namespace App\Dto;
use App\Models\Product;
use App\Models\Tag;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\ProductWeight;
use Illuminate\Contracts\Routing\UrlRoutable;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Carbon;
use Money\Money;
/**
@ -17,6 +22,9 @@ use Money\Money;
*/
final readonly class ProductDto implements Arrayable, UrlRoutable
{
/**
* @param list<TagDto> $tags
*/
public function __construct(
public string $id,
public string $name,
@ -31,15 +39,21 @@ final readonly class ProductDto implements Arrayable, UrlRoutable
public ?string $picture_large_title = null,
public ?Money $price = null,
public ?DiscountDto $discount = null,
public ?TaxRate $tax_rate = null,
public ?InvoicingUnit $invoicing_unit = null,
public int $stock = 0,
public ?SellUnit $sell_unit = null,
public ?OrderUnit $order_unit = null,
public ?RangeDto $range = null,
public array $tags = [],
public int $order = 0,
public bool $status = false,
public bool $star = false,
public ?ProductWeight $product_weight = null,
public bool $weight_variable = false,
public bool $is_orderable = false,
public ?Carbon $orderable_start = null,
public ?Carbon $orderable_end = null,
public ?string $short_description = null,
public ?string $description = null,
) {
@ -54,6 +68,11 @@ final readonly class ProductDto implements Arrayable, UrlRoutable
? ($product->relationLoaded('validDiscount') ? $product->validDiscount : null)
: ($product->relationLoaded('discount') ? $product->discount : null);
/** @var list<TagDto> $tags */
$tags = $product->relationLoaded('tags')
? array_values($product->tags->map(static fn (Tag $tag): TagDto => TagDto::fromModel($tag))->all())
: [];
return new self(
id: (string) $product->id,
name: $product->name,
@ -68,15 +87,21 @@ final readonly class ProductDto implements Arrayable, UrlRoutable
picture_large_title: $product->picture_large_title,
price: $product->price instanceof Money ? $product->price : null,
discount: $discountModel ? DiscountDto::fromModel($discountModel) : null,
tax_rate: $product->tax_rate,
invoicing_unit: $product->invoicing_unit,
stock: (int) $product->stock,
sell_unit: $product->sell_unit,
order_unit: $product->order_unit,
range: $product->relationLoaded('range') && $product->range ? RangeDto::fromModel($product->range) : null,
tags: $tags,
order: (int) $product->order,
status: (bool) $product->status,
star: (bool) $product->star,
product_weight: $product->product_weight,
weight_variable: (bool) $product->weight_variable,
is_orderable: $isOrderable,
orderable_start: $product->orderable_start,
orderable_end: $product->orderable_end,
short_description: $product->short_description,
description: $product->description,
);
@ -124,18 +149,42 @@ final readonly class ProductDto implements Arrayable, UrlRoutable
'picture_large' => $this->picture_large,
'picture_large_alt' => $this->picture_large_alt,
'picture_large_title' => $this->picture_large_title,
'price' => $this->price?->getAmount(),
'discount' => $this->discount?->toArray(),
'stock' => $this->stock,
'sell_unit' => $this->sell_unit?->value,
...$this->pricingAndUnitsToArray(),
'range' => $this->range?->toArray(),
'tags' => array_map(static fn (TagDto $tag): array => $tag->toArray(), $this->tags),
'order' => $this->order,
'product_weight' => $this->product_weight?->value,
'weight_variable' => $this->weight_variable,
'status' => $this->status,
'star' => $this->star,
'orderable_start' => $this->orderable_start,
'orderable_end' => $this->orderable_end,
'short_description' => $this->short_description,
'description' => $this->description,
];
}
/**
* @return array{
* price: string|null,
* discount: array<string, mixed>|null,
* tax_rate: int|null,
* invoicing_unit: string|null,
* stock: int,
* sell_unit: string|null,
* order_unit: string|null
* }
*/
private function pricingAndUnitsToArray(): array
{
return [
'price' => $this->price?->getAmount(),
'discount' => $this->discount?->toArray(),
'tax_rate' => $this->tax_rate?->value,
'invoicing_unit' => $this->invoicing_unit?->value,
'stock' => $this->stock,
'sell_unit' => $this->sell_unit?->value,
'order_unit' => $this->order_unit?->value,
];
}
}

View file

@ -34,6 +34,7 @@ final readonly class ProductFormDataDto
public Collection $tags,
public Collection $existingProds,
public Collection $selectedProds,
public ?ProductDto $product = null,
) {
}
}

View file

@ -115,7 +115,7 @@ class ProductsController extends Controller implements MassActions
$formData = $this->executor->execute($query, new GetProductFormDataQueryInput(productId: $product->getId()));
return view('bo.products.edit', [
'product' => $this->productDtoAssemblerService->toDto($product),
'product' => $formData->product ?? $this->productDtoAssemblerService->toDto($product),
'brands' => $formData->brands,
'taxes' => $formData->taxes,
'invoicingUnits' => $formData->invoicingUnits,

View file

@ -23,11 +23,11 @@
</span>
</label>
<select class="form-select" id="product-brand" name="brand" aria-describedby="product-brand-help">
<option value="" @if (!old('brand', $product->brand ?? null)) selected @endif disabled>
<option value="" @if (!old('brand', ($product ?? null)?->brand?->value())) selected @endif disabled>
{{ __('Select') . ' ' . __('validation.custom.attributes.brand') }}
</option>
@foreach ($brands as $brand)
<option value="{{ $brand->value() }}" @if (old('brand', optional($product ?? null)->brand?->value() ?? null) == $brand->value()) selected @endif>{{ $brand->label() }}
<option value="{{ $brand->value() }}" @if (old('brand', ($product ?? null)?->brand?->value()) == $brand->value()) selected @endif>{{ $brand->label() }}
</option>
@endforeach
</select>
@ -41,11 +41,11 @@
<div class="col-12 col-md-6 form-group">
<label class="col-form-label" for="product-range"><b>{{ Str::ucfirst(trans_choice('models.classes.ranges', 1)) }}</b></label>
<select class="form-select" id="product-range" name="range_id" aria-describedby="product-range-help" required>
<option value="" @if (!old('range_id', $product->range_id ?? null)) selected @endif disabled>
<option value="" @if (!old('range_id', ($product ?? null)?->range?->id)) selected @endif disabled>
{{ __('Select') . ' ' . trans_choice('models.classes.ranges', 1) }}
</option>
@foreach ($ranges as $rangeId => $rangeName)
<option value="{{ $rangeId }}" @if (old('range_id', $product->range_id ?? null) == $rangeId) selected @endif>{{ $rangeName }}
<option value="{{ $rangeId }}" @if (old('range_id', ($product ?? null)?->range?->id) == $rangeId) selected @endif>{{ $rangeName }}
</option>
@endforeach
</select>
@ -62,7 +62,7 @@
$data = [
'name' => 'tags',
'allowNew' => true,
'value' => $product->tags ?? [],
'value' => old('tags', $product->tags ?? []),
'itemsList' => $tags,
'placeholder' => __('Add :name', [
'name' => trans_choice('models.classes.tags', 1),
@ -258,11 +258,11 @@
<div class="col-12 col-md-5 form-group">
<label class="col-form-label" for="product-tax"><b>{{ Str::ucfirst(__('validation.custom.attributes.tax_rate')) }}</b></label>
<select class="form-select" id="product-tax" name="tax_rate" aria-describedby="product-tax-help" required>
<option value="" @if (!old('tax_rate', $product->tax_rate ?? null)) selected @endif disabled>
<option value="" @if (!old('tax_rate', ($product ?? null)?->tax_rate?->value())) selected @endif disabled>
{{ __('Select') . ' ' . __('validation.custom.attributes.tax_rate') }}
</option>
@foreach ($taxes as $tax)
<option value="{{ $tax->value() }}" @if (old('tax_rate', optional($product ?? null)->tax_rate?->value() ?? null) == $tax->value()) selected @endif>{{ $tax->label() }}
<option value="{{ $tax->value() }}" @if (old('tax_rate', ($product ?? null)?->tax_rate?->value()) == $tax->value()) selected @endif>{{ $tax->label() }}
</option>
@endforeach
</select>
@ -287,11 +287,11 @@
</span>
</label>
<select class="form-select" id="product-sell-unit" name="sell_unit" aria-describedby="product-sell-unit-help" required>
<option value="" @if (!old('sell_unit', $product->sell_unit ?? null)) selected @endif disabled>
<option value="" @if (!old('sell_unit', ($product ?? null)?->sell_unit?->value())) selected @endif disabled>
{{ __('Select') . ' ' . __('validation.custom.attributes.sell_unit') }}
</option>
@foreach ($sellUnits as $sellUnit)
<option value="{{ $sellUnit->value() }}" @if (old('sell_unit', optional($product ?? null)->sell_unit?->value() ?? null) == $sellUnit->value()) selected @endif>{{ $sellUnit->label() }}
<option value="{{ $sellUnit->value() }}" @if (old('sell_unit', ($product ?? null)?->sell_unit?->value()) == $sellUnit->value()) selected @endif>{{ $sellUnit->label() }}
</option>
@endforeach
</select>
@ -309,11 +309,11 @@
</span>
</label>
<select class="form-select" id="product-order-unit" name="order_unit" aria-describedby="product-order-unit-help" required>
<option value="" @if (!old('order_unit', $product->order_unit ?? null)) selected @endif disabled>
<option value="" @if (!old('order_unit', ($product ?? null)?->order_unit?->value())) selected @endif disabled>
{{ __('Select') . ' ' . __('validation.custom.attributes.order_unit') }}
</option>
@foreach ($orderUnits as $orderUnit)
<option value="{{ $orderUnit->value() }}" @if (old('order_unit', optional($product ?? null)->order_unit?->value() ?? null) == $orderUnit->value()) selected @endif>{{ $orderUnit->label() }}
<option value="{{ $orderUnit->value() }}" @if (old('order_unit', ($product ?? null)?->order_unit?->value()) == $orderUnit->value()) selected @endif>{{ $orderUnit->label() }}
</option>
@endforeach
</select>
@ -332,11 +332,11 @@
</label>
<select class="form-select" id="product-invoicing-unit" name="invoicing_unit" aria-describedby="product-invoicing-unit-help"
required>
<option value="" @if (!old('invoicing_unit', $product->invoicing_unit ?? null)) selected @endif disabled>
<option value="" @if (!old('invoicing_unit', ($product ?? null)?->invoicing_unit?->value())) selected @endif disabled>
{{ __('Select') . ' ' . __('validation.custom.attributes.invoicing_unit') }}
</option>
@foreach ($invoicingUnits as $invoicingUnit)
<option value="{{ $invoicingUnit->value() }}" @if (old('invoicing_unit', optional($product ?? null)->invoicing_unit?->value() ?? null) == $invoicingUnit->value()) selected @endif>
<option value="{{ $invoicingUnit->value() }}" @if (old('invoicing_unit', ($product ?? null)?->invoicing_unit?->value()) == $invoicingUnit->value()) selected @endif>
{{ $invoicingUnit->label() }}
</option>
@endforeach
@ -358,7 +358,7 @@
<div class="col-12 col-md-4 form-group">
<label class="col-form-label" for="product-weight"><b>{{ Str::ucfirst(__('validation.custom.attributes.weight')) }} (Kg)</b></label>
<input class="form-control" id="product-weight" name="weight" type="number"
value="{{ old('weight', isset($product->weight) ? round($product->weight / 1000, 3) : '') }}" step="0.001">
value="{{ old('weight', isset($product?->product_weight) ? round($product->product_weight->toGrams() / 1000, 3) : '') }}" step="0.001">
@include('bo.modules.input-error', ['inputName' => 'weight'])
</div>

View file

@ -17,6 +17,7 @@ use Domain\Enums\Units\InvoicingUnit;
use Domain\Enums\Units\OrderUnit;
use Domain\Enums\UserRole;
use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Facades\Date;
use Money\Money;
describe('ProductsController', function () {
@ -232,23 +233,38 @@ describe('ProductsController', function () {
});
it('can access edit', function () {
/** @var \Tests\TestCase $this */
// GIVEN
$otherRange = new Range();
$otherRange->name = 'Other Range ' . uniqid();
$otherRange->status = true;
$otherRange->order = random_int(1, 4999);
$otherRange->picture_alt = 'alt';
$otherRange->picture_title = 'title';
$otherRange->save();
$range = new Range();
$range->name = 'Test Range ' . uniqid();
$range->status = true;
$range->order = random_int(1, 9999);
$range->order = random_int(5000, 9999);
$range->picture_alt = 'alt';
$range->picture_title = 'title';
$range->save();
$tag = new Tag(['label' => 'Bio ' . uniqid(), 'status' => true]);
$tag->save();
$orderableStart = Date::now()->startOfMinute()->subDay();
$orderableEnd = Date::now()->startOfMinute()->addWeek();
$product = new Product([
'range_id' => $range->id,
'name' => 'Test Product',
'brand' => ProductBrand::loue,
'invoicing_unit' => InvoicingUnit::PCE,
'sell_unit' => SellUnit::PCE,
'order_unit' => OrderUnit::PCE,
'tax_rate' => TaxRate::TVA_20,
'brand' => ProductBrand::legaulois,
'invoicing_unit' => InvoicingUnit::KGM,
'sell_unit' => SellUnit::KGM,
'order_unit' => OrderUnit::COLIS,
'tax_rate' => TaxRate::TVA_5_5,
'description' => 'Test description',
'status' => true,
]);
@ -258,13 +274,30 @@ describe('ProductsController', function () {
$product->picture_title = 'test title';
$product->picture_large_alt = 'test alt';
$product->picture_large_title = 'test title';
$product->weight = 1500;
$product->orderable_start = $orderableStart;
$product->orderable_end = $orderableEnd;
$product->save();
$product->tags()->attach($tag->id);
// WHEN
$response = $this->get("/bo/products/{$product->slug}/edit");
// THEN
expect($response->status())->toBe(200);
$content = (string) $response->getContent();
expect($content)
->toMatch('/<select[^>]+id="product-brand"[^>]*>.*?<option\s+value="legaulois"\s+selected/s')
->toMatch('/<select[^>]+id="product-range"[^>]*>.*?<option\s+value="' . $range->id . '"\s+selected/s')
->toMatch('/<select[^>]+id="product-tax"[^>]*>.*?<option\s+value="' . TaxRate::TVA_5_5->value . '"\s+selected/s')
->toMatch('/<select[^>]+id="product-sell-unit"[^>]*>.*?<option\s+value="KGM"\s+selected/s')
->toMatch('/<select[^>]+id="product-order-unit"[^>]*>.*?<option\s+value="COLIS"\s+selected/s')
->toMatch('/<select[^>]+id="product-invoicing-unit"[^>]*>.*?<option\s+value="KGM"\s+selected/s')
->toContain('"value":[{"id":' . $tag->id)
->toMatch('/id="product-weight"[^>]+value="1\.5"/')
->toContain('id="orderable-start" name="orderable_start" type="datetime-local"')
->toContain('value="' . Date::toLocalInput($orderableStart) . '"')
->toContain('value="' . Date::toLocalInput($orderableEnd) . '"');
});
it('can update a product', function () {

View file

@ -75,6 +75,7 @@ describe('GetProductFormDataQuery', function (): void {
// THEN
expect($output)->toBeInstanceOf(ProductFormDataDto::class)
->and($output->product)->toBeNull()
->and($output->selectedProds)->toBeEmpty()
->and($output->existingProds->pluck('id')->all())->toContain((string) $product1->id, (string) $product2->id)
->and($output->existingProds->first())->toBeInstanceOf(ProductDto::class)
@ -88,8 +89,11 @@ describe('GetProductFormDataQuery', function (): void {
$mainProduct = $createProduct('Principal', $range);
$productAssociated = $createProduct('Associe', $range);
$productNotAssociated = $createProduct('NonAssocie', $range);
$tag = new Tag(['label' => 'Tag Edit ' . uniqid(), 'status' => true]);
$tag->save();
$mainProduct->products()->attach($productAssociated->id, ['sort_order' => 1]);
$mainProduct->tags()->attach($tag->id);
$query = new GetProductFormDataQuery(new ProductDtoAssemblerService(new EloquentProductRepository()));
$input = new GetProductFormDataQueryInput(productId: $mainProduct->getId());
@ -99,6 +103,13 @@ describe('GetProductFormDataQuery', function (): void {
// THEN
expect($output)->toBeInstanceOf(ProductFormDataDto::class)
->and($output->product)->toBeInstanceOf(ProductDto::class)
->and($output->product?->id)->toBe((string) $mainProduct->id)
->and($output->product?->range?->id)->toBe($range->id)
->and($output->product?->tax_rate)->toBe(TaxRate::TVA_20)
->and($output->product?->invoicing_unit)->toBe(InvoicingUnit::PCE)
->and($output->product?->order_unit)->toBe(OrderUnit::PCE)
->and(array_map(static fn ($t): int => $t->id, $output->product?->tags ?? []))->toBe([$tag->id])
->and($output->selectedProds->pluck('id')->all())->toBe([(string) $productAssociated->id])
->and($output->selectedProds->first())->toBeInstanceOf(ProductDto::class)
->and($output->existingProds->pluck('id')->all())->toContain((string) $productNotAssociated->id)

View file

@ -42,14 +42,20 @@ describe('ProductDto', function () {
'picture_large_title' => null,
'price' => null,
'discount' => null,
'tax_rate' => null,
'invoicing_unit' => null,
'stock' => 0,
'sell_unit' => null,
'order_unit' => null,
'range' => null,
'tags' => [],
'order' => 0,
'product_weight' => null,
'weight_variable' => false,
'status' => false,
'star' => false,
'orderable_start' => null,
'orderable_end' => null,
'short_description' => null,
'description' => null,
]);