From 0d5958aae639f1c173cdf1bc53c80af710baed2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?=
-
+
{{ __('Current price') }} :
@include('fo.modules.guarded-currency', [
'money' => $productWithPricing->pricing->hasDiscount
@@ -17,7 +17,7 @@
])
- @if ($productWithPricing->product->validDiscount?->is_old_price_displayed)
+ @if ($productWithPricing->pricing->hasDiscount && $productWithPricing->product->validDiscount?->is_old_price_displayed)
{{ __('Original price') }} :
@include('fo.modules.guarded-currency', [
diff --git a/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php b/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php
index b37909ea..be346894 100644
--- a/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php
+++ b/tests/Feature/App/Http/Controllers/Fo/Pages/HomeControllerTest.php
@@ -2,15 +2,18 @@
declare(strict_types=1);
+use App\Models\Discount;
use App\Models\Page;
use App\Models\Product;
use App\Models\Range;
use App\Models\Slide;
use Database\Seeders\ContentSeeder;
+use Domain\Enums\DiscountType;
use Domain\Enums\ProductBrand;
use Domain\Enums\SellUnit;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\InvoicingUnit;
+use Domain\Models\ValueObjects\DiscountValue;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\View;
use Money\Money;
@@ -121,4 +124,90 @@ describe('HomeController', function () {
$response->assertDontSee('id="hero-carousel"', false);
$response->assertDontSee('Expired Promo');
});
+
+ it('displays promo prices in red for discounted star products on homepage', function () {
+ /** @var \Tests\TestCase $this */
+ // GIVEN
+ $this->seed(ContentSeeder::class);
+ View::share('PageList', Page::getCached()
+ ->mapWithKeys(fn ($page) => [
+ $page->slug->value() => $page,
+ ]));
+
+ $range = new Range();
+ $range->name = 'Boucherie';
+ $range->slug = 'boucherie';
+ $range->picture = 'ranges/boucherie.jpg';
+ $range->picture_alt = 'Boucherie';
+ $range->picture_title = 'Boucherie';
+ $range->status = true;
+ $range->order = 1;
+ $range->save();
+
+ $discount = new Discount();
+ $discount->type = DiscountType::PERCENT;
+ $discount->value = DiscountValue::fromPercentage(10);
+ $discount->is_old_price_displayed = false;
+ $discount->date_start = Carbon::yesterday();
+ $discount->date_end = Carbon::tomorrow();
+ $discount->save();
+
+ $promoProduct = new Product();
+ $promoProduct->id = (string) Uuid::uuid7();
+ $promoProduct->name = 'Foie Gras Promo';
+ $promoProduct->brand = ProductBrand::loue;
+ $promoProduct->slug = 'foie-gras-promo';
+ $promoProduct->description = 'Foie Gras en promo';
+ $promoProduct->picture_alt = 'Foie Gras Promo';
+ $promoProduct->picture_title = 'Foie Gras Promo';
+ $promoProduct->picture_large_alt = 'Foie Gras Promo';
+ $promoProduct->picture_large_title = 'Foie Gras Promo';
+ $promoProduct->price = Money::EUR(2000);
+ $promoProduct->invoicing_unit = InvoicingUnit::PCE;
+ $promoProduct->sell_unit = SellUnit::PCE;
+ $promoProduct->tax_rate = TaxRate::TVA_5_5;
+ $promoProduct->stock = 10;
+ $promoProduct->range_id = $range->id;
+ $promoProduct->order = 1;
+ $promoProduct->star = true;
+ $promoProduct->status = true;
+ $promoProduct->discount_id = $discount->id;
+ $promoProduct->save();
+
+ $regularProduct = new Product();
+ $regularProduct->id = (string) Uuid::uuid7();
+ $regularProduct->name = 'Poulet Standard';
+ $regularProduct->brand = ProductBrand::loue;
+ $regularProduct->slug = 'poulet-standard';
+ $regularProduct->description = 'Poulet standard sans promo';
+ $regularProduct->picture_alt = 'Poulet Standard';
+ $regularProduct->picture_title = 'Poulet Standard';
+ $regularProduct->picture_large_alt = 'Poulet Standard';
+ $regularProduct->picture_large_title = 'Poulet Standard';
+ $regularProduct->price = Money::EUR(1500);
+ $regularProduct->invoicing_unit = InvoicingUnit::PCE;
+ $regularProduct->sell_unit = SellUnit::PCE;
+ $regularProduct->tax_rate = TaxRate::TVA_5_5;
+ $regularProduct->stock = 10;
+ $regularProduct->range_id = $range->id;
+ $regularProduct->order = 2;
+ $regularProduct->star = true;
+ $regularProduct->status = true;
+ $regularProduct->discount_id = null;
+ $regularProduct->save();
+
+ // WHEN
+ $response = $this->get('/');
+
+ // THEN
+ $response->assertOk();
+ $content = $response->getContent();
+ expect($content)->toContain('Foie Gras Promo')
+ ->and($content)->toContain('Poulet Standard');
+
+ // Promo product price span has text-danger class
+ expect($content)->toMatch('/Foie Gras Promo.*?/s')
+ // Regular product price span does NOT have text-danger class
+ ->and($content)->toMatch('/Poulet Standard.*?/s');
+ });
});
From 82697ed4162c5bdf66e1445dfacac4514675e840 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?=
- {{ $productWithPricing->product->weight_variable ? __('Product sold by the kilo') : __('Price per kilo') }} :
+ {{ $productWithPricing->product->weight_variable ? '*' . __('Product sold by the kilo') : __('Price per kilo') }} :
@include('fo.modules.guarded-currency', [
'money' => $productWithPricing->pricing->priceKgTtc(),
'product' => $productWithPricing->product,
diff --git a/tests/Unit/Domain/Services/PricingServiceTest.php b/tests/Unit/Domain/Services/PricingServiceTest.php
index 70963236..9536924b 100644
--- a/tests/Unit/Domain/Services/PricingServiceTest.php
+++ b/tests/Unit/Domain/Services/PricingServiceTest.php
@@ -17,6 +17,21 @@ use Money\Currency;
use Money\Money;
use Ramsey\Uuid\Uuid;
+/**
+ * @param array{
+ * id?: \Domain\Models\ValueObjects\Entities\ProductId,
+ * price?: \Money\Money,
+ * tax_rate?: \Domain\Enums\TaxRate,
+ * product_weight?: \Domain\Models\ValueObjects\ProductWeight|null,
+ * invoicing_unit?: \Domain\Enums\Units\InvoicingUnit,
+ * sell_unit?: \Domain\Enums\SellUnit,
+ * validDiscount?: \Domain\Models\ValueObjects\DiscountValue|null,
+ * status?: bool,
+ * orderable_start?: \Carbon\CarbonImmutable|null,
+ * orderable_end?: \Carbon\CarbonImmutable|null,
+ * hasInco?: bool,
+ * } $attributes
+ */
function createBaseProduct(array $attributes = []): Product
{
$currency = new Currency('EUR');
@@ -44,7 +59,7 @@ function createBaseProduct(array $attributes = []): Product
product_weight: $data['product_weight'],
invoicing_unit: $data['invoicing_unit'],
sell_unit: $data['sell_unit'],
- validDiscount: $data['validDiscount'] ? clone $data['validDiscount'] : null,
+ validDiscount: $data['validDiscount'] instanceof DiscountValue ? clone $data['validDiscount'] : null,
status: $data['status'],
orderable_start: $data['orderable_start'],
orderable_end: $data['orderable_end'],
@@ -231,4 +246,117 @@ describe(PricingService::class, function (): void {
// THEN
expect($result->discountRate->basisPoints())->toBe(0);
});
+
+ it('calculates the price per KG TTC for a product invoiced by KGM and sold by PCE', function (): void {
+ // GIVEN
+ $currency = new Currency('EUR');
+ $product = createBaseProduct([
+ 'price' => new Money(865, $currency), // 8.65 €/kg
+ 'tax_rate' => TaxRate::TVA_5_5,
+ 'invoicing_unit' => InvoicingUnit::KGM,
+ 'sell_unit' => SellUnit::PCE,
+ 'product_weight' => createWeight(360), // 360g
+ 'status' => true,
+ ]);
+
+ $service = new PricingService();
+
+ // WHEN
+ $result = $service->calculateForProduct($product);
+
+ // THEN
+ expect($result->basePriceHt()->getAmount())->toBe('312')
+ ->and($result->basePriceTtc()->getAmount())->toBe('330')
+ ->and($result->priceKgTtc()->getAmount())->toBe('913');
+ });
+
+ it('calculates the price per KG TTC for a product invoiced by KGM with percentage discount', function (): void {
+ // GIVEN
+ $currency = new Currency('EUR');
+ $discount = createDiscount(
+ amount: new Money(0, $currency),
+ isPercentage: true,
+ percentageRate: 10.0
+ );
+
+ $product = createBaseProduct([
+ 'price' => new Money(865, $currency), // 8.65 €/kg
+ 'tax_rate' => TaxRate::TVA_5_5,
+ 'invoicing_unit' => InvoicingUnit::KGM,
+ 'sell_unit' => SellUnit::PCE,
+ 'product_weight' => createWeight(360), // 360g
+ 'validDiscount' => $discount,
+ 'status' => true,
+ ]);
+
+ $service = new PricingService();
+
+ // WHEN
+ $result = $service->calculateForProduct($product);
+
+ // THEN
+ expect($result->discountedPriceHt()->getAmount())->toBe('280')
+ ->and($result->discountedPriceTtc()->getAmount())->toBe('296')
+ ->and($result->priceKgTtc()->getAmount())->toBe('821');
+ });
+
+ it('calculates the price per KG TTC for a product invoiced by KGM with fixed amount discount', function (): void {
+ // GIVEN
+ $currency = new Currency('EUR');
+ $discount = createDiscount(
+ amount: new Money(50, $currency),
+ isPercentage: false
+ );
+
+ $product = createBaseProduct([
+ 'price' => new Money(865, $currency), // 8.65 €/kg
+ 'tax_rate' => TaxRate::TVA_5_5,
+ 'invoicing_unit' => InvoicingUnit::KGM,
+ 'sell_unit' => SellUnit::PCE,
+ 'product_weight' => createWeight(360),
+ 'validDiscount' => $discount,
+ 'status' => true,
+ ]);
+
+ $service = new PricingService();
+
+ // WHEN
+ $result = $service->calculateForProduct($product);
+
+ // THEN
+ // 50 cts off a 360 g piece => 139 cts off per kg (rounded up): (865 - 139) HT x 1.055
+ expect($result->discountedPriceTtc()->getAmount())->toBe('277')
+ ->and($result->priceKgTtc()->getAmount())->toBe('766');
+ });
+
+ it('caps the discount at the piece price when a fixed discount exceeds the piece price', function (): void {
+ // GIVEN
+ $currency = new Currency('EUR');
+ $discount = createDiscount(
+ amount: new Money(500, $currency), // 5.00 € discount on a 3.12 € piece
+ isPercentage: false
+ );
+
+ $product = createBaseProduct([
+ 'price' => new Money(865, $currency), // 8.65 €/kg => 3.12 € HT for 360g
+ 'tax_rate' => TaxRate::TVA_5_5,
+ 'invoicing_unit' => InvoicingUnit::KGM,
+ 'sell_unit' => SellUnit::PCE,
+ 'product_weight' => createWeight(360),
+ 'validDiscount' => $discount,
+ 'status' => true,
+ ]);
+
+ $service = new PricingService();
+
+ // WHEN
+ $result = $service->calculateForProduct($product);
+
+ // THEN
+ expect($result->discountAmountHt()->getAmount())->toBe('312')
+ ->and($result->discountedPriceHt()->getAmount())->toBe('0')
+ ->and($result->discountedPriceTtc()->getAmount())->toBe('0')
+ ->and($result->priceKgTtc()->getAmount())->toBe('0')
+ ->and($result->discountRate->basisPoints())->toBe(10000);
+ });
});
From a94d8a4b49f2f257b9032cc31140680161ddb834 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?=
-
diff --git a/resources/views/bo/products/index-list.blade.php b/resources/views/bo/products/index-list.blade.php
index 810f393e..26c212fa 100644
--- a/resources/views/bo/products/index-list.blade.php
+++ b/resources/views/bo/products/index-list.blade.php
@@ -75,6 +75,7 @@
@if (!request()->routeIs('bo.merch.starred'))
@include('bo.modules.change-order', [
'route' => 'bo.products.change-order',
+ 'selectRoute' => 'bo.products.change-selected-order',
'model' => $product,
'first' => $loop->first,
'last' => $loop->last,
diff --git a/routes/web-bo.php b/routes/web-bo.php
index 4d1fc39b..a13170e2 100644
--- a/routes/web-bo.php
+++ b/routes/web-bo.php
@@ -78,6 +78,7 @@ Route::middleware(['auth:backend', 'has-role:Admin'])->group(function (): void {
Route::post('/products/mass-actions', [ProductsController::class, 'massActions'])->name('products.mass-actions');
Route::patch('/products/{product}/toggle-status', [ProductsController::class, 'toggleStatus'])->name('products.toggle-status');
Route::patch('/products/{product}/toggle-star', [ProductsController::class, 'toggleStar'])->name('products.toggle-star');
+ Route::patch('/products/{product}/change-selected-order', [ProductsController::class, 'changeSelectedOrder'])->name('products.change-selected-order');
Route::patch('/products/{product}/{action}', [ProductsController::class, 'changeOrder'])->name('products.change-order');
// * PRODUCTS INCO
diff --git a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php
index 1f389eaf..25793f4c 100644
--- a/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php
+++ b/tests/Feature/App/Http/Controllers/Bo/ProductsControllerTest.php
@@ -469,6 +469,73 @@ describe('ProductsController', function () {
expect($response->status())->toBe(302);
});
+ it('can change selected order to specific position', function (): void {
+ /** @var \Tests\TestCase $this */
+ // GIVEN
+ $range = new Range();
+ $range->name = 'Test Range ' . uniqid();
+ $range->status = true;
+ $range->order = random_int(1, 9999);
+ $range->picture_alt = 'alt';
+ $range->picture_title = 'title';
+ $range->save();
+
+ $product1 = new Product([
+ 'range_id' => $range->id,
+ 'name' => 'Test Product 1 ' . uniqid(),
+ 'brand' => ProductBrand::loue,
+ 'invoicing_unit' => InvoicingUnit::PCE,
+ 'sell_unit' => SellUnit::PCE,
+ 'order_unit' => OrderUnit::PCE,
+ 'tax_rate' => TaxRate::TVA_20,
+ 'description' => 'Test description',
+ 'status' => true,
+ ]);
+ $product1->price = Money::EUR(1000);
+ $product1->short_description = 'Short test description';
+ $product1->picture_alt = 'test alt';
+ $product1->picture_title = 'test title';
+ $product1->picture_large_alt = 'test alt';
+ $product1->picture_large_title = 'test title';
+ $product1->save();
+ $product1->setOrder(1);
+ $product1->save();
+
+ $product2 = new Product([
+ 'range_id' => $range->id,
+ 'name' => 'Test Product 2 ' . uniqid(),
+ 'brand' => ProductBrand::loue,
+ 'invoicing_unit' => InvoicingUnit::PCE,
+ 'sell_unit' => SellUnit::PCE,
+ 'order_unit' => OrderUnit::PCE,
+ 'tax_rate' => TaxRate::TVA_20,
+ 'description' => 'Test description',
+ 'status' => true,
+ ]);
+ $product2->price = Money::EUR(1000);
+ $product2->short_description = 'Short test description';
+ $product2->picture_alt = 'test alt';
+ $product2->picture_title = 'test title';
+ $product2->picture_large_alt = 'test alt';
+ $product2->picture_large_title = 'test title';
+ $product2->save();
+ $product2->setOrder(2);
+ $product2->save();
+
+ // WHEN
+ $response = $this->patch("/bo/products/{$product2->slug}/change-selected-order", [
+ 'order' => 1,
+ ]);
+
+ // THEN
+ $response->assertSessionHasNoErrors();
+ $response->assertRedirect();
+ $product1->refresh();
+ $product2->refresh();
+ expect($product2->getOrder())->toBe(1)
+ ->and($product1->getOrder())->toBe(2);
+ });
+
describe('mass actions', function () {
beforeEach(function (): void {
/** @var \Tests\TestCase $this */
diff --git a/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderPositionTest.php b/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderPositionTest.php
new file mode 100644
index 00000000..4776e195
--- /dev/null
+++ b/tests/Integration/WithApp/App/Actions/Common/ChangeModelOrderPositionTest.php
@@ -0,0 +1,134 @@
+create();
+ $tag1->setOrder(1);
+ $tag1->save();
+
+ $tag2 = Tag::factory()->create();
+ $tag2->setOrder(2);
+ $tag2->save();
+
+ $tag3 = Tag::factory()->create();
+ $tag3->setOrder(3);
+ $tag3->save();
+
+ $action = new ChangeModelOrderPosition();
+ $input = new ChangeModelOrderPositionInput($tag3, 1);
+
+ // WHEN
+ $result = $action($input);
+
+ // THEN
+ expect($result)->toBeTrue();
+
+ $fresh1 = Tag::query()->find($tag1->id);
+ $fresh2 = Tag::query()->find($tag2->id);
+ $fresh3 = Tag::query()->find($tag3->id);
+
+ expect($fresh3?->getOrder())->toBe(1)
+ ->and($fresh1?->getOrder())->toBe(2)
+ ->and($fresh2?->getOrder())->toBe(3);
+ });
+
+ it('shifts orders when moving a model down to target position', function (): void {
+ // GIVEN
+ $tag1 = Tag::factory()->create();
+ $tag1->setOrder(1);
+ $tag1->save();
+
+ $tag2 = Tag::factory()->create();
+ $tag2->setOrder(2);
+ $tag2->save();
+
+ $tag3 = Tag::factory()->create();
+ $tag3->setOrder(3);
+ $tag3->save();
+
+ $action = new ChangeModelOrderPosition();
+ $input = new ChangeModelOrderPositionInput($tag1, 3);
+
+ // WHEN
+ $result = $action($input);
+
+ // THEN
+ expect($result)->toBeTrue();
+
+ $fresh1 = Tag::query()->find($tag1->id);
+ $fresh2 = Tag::query()->find($tag2->id);
+ $fresh3 = Tag::query()->find($tag3->id);
+
+ expect($fresh2?->getOrder())->toBe(1)
+ ->and($fresh3?->getOrder())->toBe(2)
+ ->and($fresh1?->getOrder())->toBe(3);
+ });
+
+ it('returns true without modification when target order is identical to current order', function (): void {
+ // GIVEN
+ $tag = Tag::factory()->create();
+ $tag->setOrder(2);
+ $tag->save();
+
+ $action = new ChangeModelOrderPosition();
+ $input = new ChangeModelOrderPositionInput($tag, 2);
+
+ // WHEN
+ $result = $action($input);
+
+ // THEN
+ expect($result)->toBeTrue();
+ $fresh = Tag::query()->find($tag->id);
+ expect($fresh?->getOrder())->toBe(2);
+ });
+
+ it('clamps target order to maximum existing order when target exceeds max', function (): void {
+ // GIVEN
+ $tag1 = Tag::factory()->create();
+ $tag1->setOrder(1);
+ $tag1->save();
+
+ $tag2 = Tag::factory()->create();
+ $tag2->setOrder(2);
+ $tag2->save();
+
+ $action = new ChangeModelOrderPosition();
+ $input = new ChangeModelOrderPositionInput($tag1, 100);
+
+ // WHEN
+ $result = $action($input);
+
+ // THEN
+ expect($result)->toBeTrue();
+
+ $fresh1 = Tag::query()->find($tag1->id);
+ $fresh2 = Tag::query()->find($tag2->id);
+
+ expect($fresh2?->getOrder())->toBe(1)
+ ->and($fresh1?->getOrder())->toBe(2);
+ });
+
+ it('throws when an invalid input is passed', function (): void {
+ // GIVEN
+ $action = new ChangeModelOrderPosition();
+ $invalidInput = new class () implements ActionInput
+ {
+ };
+
+ // WHEN
+ expect(
+ /** @phpstan-ignore argument.type */
+ fn () => $action($invalidInput)
+ )
+ // THEN
+ ->toThrow(\InvalidArgumentException::class);
+ });
+});
diff --git a/tests/Unit/App/Actions/Common/ChangeModelOrderPositionInputTest.php b/tests/Unit/App/Actions/Common/ChangeModelOrderPositionInputTest.php
new file mode 100644
index 00000000..ed3aadf2
--- /dev/null
+++ b/tests/Unit/App/Actions/Common/ChangeModelOrderPositionInputTest.php
@@ -0,0 +1,50 @@
+model)->toBe($mock)
+ ->and($input->targetOrder)->toBe(5);
+ });
+
+ it('throws when target order is less than 1', function (): void {
+ // GIVEN
+ $mock = new class () extends Model implements ModelWithOrder
+ {
+ public function getOrder(): int
+ {
+ return 1;
+ }
+
+ public function setOrder(int $order): void
+ {
+ }
+ };
+
+ // WHEN & THEN
+ expect(fn () => new ChangeModelOrderPositionInput(model: $mock, targetOrder: 0))
+ ->toThrow(\InvalidArgumentException::class);
+ });
+});
From 5d366397059aba66ba39301576140cc8f152bb94 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?=