diff --git a/app/Cqs/Queries/Products/GetProductFormDataQuery.php b/app/Cqs/Queries/Products/GetProductFormDataQuery.php index 89e65f96..5a18461b 100644 --- a/app/Cqs/Queries/Products/GetProductFormDataQuery.php +++ b/app/Cqs/Queries/Products/GetProductFormDataQuery.php @@ -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, ); } } diff --git a/app/Dto/ProductDto.php b/app/Dto/ProductDto.php index 2ae59719..55a74f3a 100644 --- a/app/Dto/ProductDto.php +++ b/app/Dto/ProductDto.php @@ -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 $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 $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|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, + ]; + } } diff --git a/app/Dto/ProductFormDataDto.php b/app/Dto/ProductFormDataDto.php index b3121d85..e67bc91b 100644 --- a/app/Dto/ProductFormDataDto.php +++ b/app/Dto/ProductFormDataDto.php @@ -34,6 +34,7 @@ final readonly class ProductFormDataDto public Collection $tags, public Collection $existingProds, public Collection $selectedProds, + public ?ProductDto $product = null, ) { } } diff --git a/app/Http/Controllers/Bo/ProductsController.php b/app/Http/Controllers/Bo/ProductsController.php index 5fa0af4e..c021a3d3 100644 --- a/app/Http/Controllers/Bo/ProductsController.php +++ b/app/Http/Controllers/Bo/ProductsController.php @@ -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, diff --git a/resources/views/bo/products/form.blade.php b/resources/views/bo/products/form.blade.php index beb04ed0..f44050b2 100644 --- a/resources/views/bo/products/form.blade.php +++ b/resources/views/bo/products/form.blade.php @@ -23,11 +23,11 @@ @@ -41,11 +41,11 @@
@@ -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 @@
@@ -287,11 +287,11 @@ @@ -309,11 +309,11 @@ @@ -332,11 +332,11 @@ + value="{{ old('weight', isset($product?->product_weight) ? round($product->product_weight->toGrams() / 1000, 3) : '') }}" step="0.001"> @include('bo.modules.input-error', ['inputName' => 'weight'])
diff --git a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php index 6871c527..07322ee6 100644 --- a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php +++ b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php @@ -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('/]+id="product-brand"[^>]*>.*?toMatch('/]+id="product-range"[^>]*>.*?id . '"\s+selected/s') + ->toMatch('/]+id="product-tax"[^>]*>.*?value . '"\s+selected/s') + ->toMatch('/]+id="product-sell-unit"[^>]*>.*?toMatch('/]+id="product-order-unit"[^>]*>.*?toMatch('/]+id="product-invoicing-unit"[^>]*>.*?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 () { diff --git a/tests/Integration/WithApp/App/Queries/Products/GetProductFormDataQueryTest.php b/tests/Integration/WithApp/App/Queries/Products/GetProductFormDataQueryTest.php index 236130b8..e486c644 100644 --- a/tests/Integration/WithApp/App/Queries/Products/GetProductFormDataQueryTest.php +++ b/tests/Integration/WithApp/App/Queries/Products/GetProductFormDataQueryTest.php @@ -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) diff --git a/tests/Unit/App/Dto/ProductDtoTest.php b/tests/Unit/App/Dto/ProductDtoTest.php index 624906af..b97b3356 100644 --- a/tests/Unit/App/Dto/ProductDtoTest.php +++ b/tests/Unit/App/Dto/ProductDtoTest.php @@ -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, ]);