From 192e72f5549060af1d4dadd47c32c50e7ecb4ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 2 Nov 2026 14:53:06 +0100 Subject: [PATCH] refactor(dto): enhance OrderLineDto with flattened product properties - Added new properties for product details in OrderLineDto. - Updated fromModel method to map product properties correctly. - Adjusted toArray method to include new properties. - Improved unit tests for OrderLineDto to validate new behavior. --- app/Dto/OrderLineDto.php | 60 ++++++++--- app/Models/OrderLine.php | 9 ++ resources/views/bo/orders/view.blade.php | 7 +- .../views/fo/customer/orders/show.blade.php | 16 +-- resources/views/fo/tunnel/confirmed.blade.php | 2 +- .../WithApp/App/Dto/OrderLineDtoTest.php | 97 +++++++++++++++++ tests/Unit/App/Dto/OrderLineDtoTest.php | 101 ++++++++++++++++++ 7 files changed, 263 insertions(+), 29 deletions(-) create mode 100644 tests/Integration/WithApp/App/Dto/OrderLineDtoTest.php create mode 100644 tests/Unit/App/Dto/OrderLineDtoTest.php diff --git a/app/Dto/OrderLineDto.php b/app/Dto/OrderLineDto.php index 8610cc52..583cb5ba 100644 --- a/app/Dto/OrderLineDto.php +++ b/app/Dto/OrderLineDto.php @@ -6,7 +6,6 @@ namespace App\Dto; use App\Models\OrderLine; use Domain\Enums\TaxRate; -use Domain\Models\ValueObjects\OrderLineProduct; use Illuminate\Contracts\Support\Arrayable; use Money\Money; @@ -15,39 +14,60 @@ use Money\Money; */ final readonly class OrderLineDto implements Arrayable { + public Money $unit_price_discounted_ht; + + public Money $unitPriceTtc; + + public Money $total_gross_ht; + public function __construct( public int $id, public string $order_id, - public OrderLineProduct $product, + public string $product_name, + public ?string $product_inco_id, + public ?string $product_brand_label, + public bool $is_variable_weight, + public ?Money $price_kg_ttc, public int $quantity, public Money $unit_price, public Money $unit_tax, public Money $unit_discount, public TaxRate $tax_rate, - public Money $total_ttc, + public Money $unit_price_ttc, public Money $total_ht, public Money $total_tax, public Money $total_discount, + public Money $total_ttc, public Money $unitPriceDiscountedHt, public Money $totalGrossHt, ) { + $this->unit_price_discounted_ht = $this->unitPriceDiscountedHt; + $this->unitPriceTtc = $this->unit_price_ttc; + $this->total_gross_ht = $this->totalGrossHt; } public static function fromModel(OrderLine $line): self { + $product = $line->product; + return new self( id: (int) $line->id, order_id: (string) $line->order_id, - product: $line->product, + product_name: $product->name, + product_inco_id: (string) $product->incoId->value, + product_brand_label: $product->brand->label(), + is_variable_weight: $product->weight->isVariable, + price_kg_ttc: $product->priceKgTtc, quantity: (int) $line->quantity, unit_price: $line->unit_price, unit_tax: $line->unit_tax, unit_discount: $line->unit_discount, tax_rate: $line->tax_rate, - total_ttc: $line->total_ttc, + unit_price_ttc: $line->unitPriceTtc, total_ht: $line->total_ht, total_tax: $line->total_tax, total_discount: $line->total_discount, + total_ttc: $line->total_ttc, unitPriceDiscountedHt: $line->unitPriceDiscountedHt, totalGrossHt: $line->totalGrossHt, ); @@ -60,17 +80,25 @@ final readonly class OrderLineDto implements Arrayable public function toArray(): array { return [ - 'id' => $this->id, - 'order_id' => $this->order_id, - 'quantity' => $this->quantity, - 'unit_price' => $this->unit_price->getAmount(), - 'unit_tax' => $this->unit_tax->getAmount(), - 'unit_discount' => $this->unit_discount->getAmount(), - 'tax_rate' => $this->tax_rate->value, - 'total_ttc' => $this->total_ttc->getAmount(), - 'total_ht' => $this->total_ht->getAmount(), - 'total_tax' => $this->total_tax->getAmount(), - 'total_discount' => $this->total_discount->getAmount(), + 'id' => $this->id, + 'order_id' => $this->order_id, + 'product_name' => $this->product_name, + 'product_inco_id' => $this->product_inco_id, + 'product_brand_label' => $this->product_brand_label, + 'is_variable_weight' => $this->is_variable_weight, + 'price_kg_ttc' => $this->price_kg_ttc?->getAmount(), + 'quantity' => $this->quantity, + 'unit_price' => $this->unit_price->getAmount(), + 'unit_tax' => $this->unit_tax->getAmount(), + 'unit_discount' => $this->unit_discount->getAmount(), + 'tax_rate' => $this->tax_rate->value, + 'unit_price_ttc' => $this->unit_price_ttc->getAmount(), + 'unit_price_discounted_ht' => $this->unit_price_discounted_ht->getAmount(), + 'total_ttc' => $this->total_ttc->getAmount(), + 'total_ht' => $this->total_ht->getAmount(), + 'total_tax' => $this->total_tax->getAmount(), + 'total_discount' => $this->total_discount->getAmount(), + 'total_gross_ht' => $this->total_gross_ht->getAmount(), ]; } } diff --git a/app/Models/OrderLine.php b/app/Models/OrderLine.php index 6dadfc94..74b2c2d6 100644 --- a/app/Models/OrderLine.php +++ b/app/Models/OrderLine.php @@ -30,6 +30,7 @@ use Money\Money; * @property-read \App\Models\Order $order * @property-read Money $total_gross_ht * @property-read Money $unit_price_discounted_ht + * @property-read Money $unit_price_ttc * * @mixin \Eloquent */ @@ -49,6 +50,14 @@ class OrderLine extends Model ); } + /** @return \Illuminate\Database\Eloquent\Casts\Attribute */ + protected function unitPriceTtc(): Attribute + { + return Attribute::make( + get: fn (): Money => $this->unit_price->subtract($this->unit_discount)->add($this->unit_tax) + ); + } + /** @return \Illuminate\Database\Eloquent\Casts\Attribute */ protected function totalGrossHt(): Attribute { diff --git a/resources/views/bo/orders/view.blade.php b/resources/views/bo/orders/view.blade.php index a62a3ecc..e451a410 100644 --- a/resources/views/bo/orders/view.blade.php +++ b/resources/views/bo/orders/view.blade.php @@ -158,11 +158,10 @@ @foreach ($order->lines as $line) - {{ $line->product->incoId->value }} + {{ $line->product_inco_id }} - {{ $line->product->name }} - {{ $line->product->brand->label() }} - @money($line->product->discountedPrice ?? $line->product->price) + {{ $line->product_name }} + {{ $line->product_brand_label }} {{ $line->quantity }} @money($line->unit_price) diff --git a/resources/views/fo/customer/orders/show.blade.php b/resources/views/fo/customer/orders/show.blade.php index 96a530d5..c17041f7 100644 --- a/resources/views/fo/customer/orders/show.blade.php +++ b/resources/views/fo/customer/orders/show.blade.php @@ -42,15 +42,15 @@ @endif - {{ $line->quantity }} x {{ $line->product->name }} + {{ $line->quantity }} x {{ $line->product_name }} - @if ($line->product->weight->isVariable) - @money($line->product->priceKgTtc) + @if ($line->is_variable_weight && $line->price_kg_ttc) + @money($line->price_kg_ttc) @endif - @money($line->product->discountedPrice ?? $line->product->price) + @money($line->unit_price_ttc) @endforeach @@ -82,19 +82,19 @@
- {{ $line->quantity }} x {{ $line->product->name }} + {{ $line->quantity }} x {{ $line->product_name }}
- @if ($line->product->weight->isVariable) + @if ($line->is_variable_weight && $line->price_kg_ttc)
{{ __('Price per kg (incl. tax)') }} - @money($line->product->priceKgTtc) + @money($line->price_kg_ttc)
@endif
{{ __('Unit price (incl. tax)') }} - @money($line->product->discountedPrice ?? $line->product->price) + @money($line->unit_price_ttc)
diff --git a/resources/views/fo/tunnel/confirmed.blade.php b/resources/views/fo/tunnel/confirmed.blade.php index eba36863..40ee06ea 100644 --- a/resources/views/fo/tunnel/confirmed.blade.php +++ b/resources/views/fo/tunnel/confirmed.blade.php @@ -31,7 +31,7 @@ @foreach ($order->lines as $line) - {{ $line->quantity }} x {{ $line->product->name }} + {{ $line->quantity }} x {{ $line->product_name }} @money($line->total_ttc) diff --git a/tests/Integration/WithApp/App/Dto/OrderLineDtoTest.php b/tests/Integration/WithApp/App/Dto/OrderLineDtoTest.php new file mode 100644 index 00000000..97234ccd --- /dev/null +++ b/tests/Integration/WithApp/App/Dto/OrderLineDtoTest.php @@ -0,0 +1,97 @@ +create(); + $order = Order::forceCreate([ + 'id' => Uuid::uuid7()->toString(), + 'shop_id' => $shop->id, + 'reference' => new OrderReference(Uuid::uuid7()), + 'status' => OrderStatus::VALIDATED->value, + 'total' => Money::EUR(4154), + 'employee' => new OrderEmployee(new Matricule('8888001'), 'Jean', 'Dupont', new ShopperEstablishmentCode('FAC01'), new ShopperSiteCode('SITE'), new ShopperServiceCode('101')), + 'delivery_address' => new DeliveryAddress('Shop Name', 'Address', '75000', 'Paris'), + 'pickup_day' => Date::now()->toDateString(), + ]); + + $orderLineProduct = new OrderLineProduct( + incoId: new IncoId(999), + originalId: new ProductId(Uuid::uuid7()), + name: 'Terrine de Foie Gras de Canard mi-cuit 500g', + weight: new ProductWeight(500, false), + brand: ProductBrand::loue, + orderUnit: OrderUnit::PCE, + price: Money::EUR(1969), + discountedPrice: Money::EUR(1969), + priceKgTtc: Money::EUR(3938), + taxRate: TaxRate::TVA_5_5, + picture: null, + ); + + $line = new OrderLine(); + $line->order()->associate($order); + $line->product = $orderLineProduct; + $line->quantity = 2; + $line->unit_price = Money::EUR(1969); + $line->unit_tax = Money::EUR(108); + $line->unit_discount = Money::EUR(0); + $line->tax_rate = TaxRate::TVA_5_5; + $line->total_ttc = Money::EUR(4154); + $line->total_ht = Money::EUR(3938); + $line->total_tax = Money::EUR(216); + $line->total_discount = Money::EUR(0); + $line->save(); + + // WHEN + $dto = OrderLineDto::fromModel($line); + + // THEN + expect($line->unitPriceTtc)->toEqual(Money::EUR(2077)) + ->and($dto->id)->toBe((int) $line->id) + ->and($dto->order_id)->toBe((string) $line->order_id) + ->and($dto->product_name)->toBe('Terrine de Foie Gras de Canard mi-cuit 500g') + ->and($dto->product_inco_id)->toBe('999') + ->and($dto->product_brand_label)->toBe(ProductBrand::loue->label()) + ->and($dto->is_variable_weight)->toBeFalse() + ->and($dto->price_kg_ttc)->toEqual(Money::EUR(3938)) + ->and($dto->quantity)->toBe(2) + ->and($dto->unit_price)->toEqual(Money::EUR(1969)) + ->and($dto->unit_tax)->toEqual(Money::EUR(108)) + ->and($dto->unit_discount)->toEqual(Money::EUR(0)) + ->and($dto->tax_rate)->toBe(TaxRate::TVA_5_5) + ->and($dto->unit_price_ttc)->toEqual(Money::EUR(2077)) + ->and($dto->unitPriceTtc)->toEqual(Money::EUR(2077)) + ->and($dto->total_ht)->toEqual(Money::EUR(3938)) + ->and($dto->total_tax)->toEqual(Money::EUR(216)) + ->and($dto->total_ttc)->toEqual(Money::EUR(4154)); + }); +}); diff --git a/tests/Unit/App/Dto/OrderLineDtoTest.php b/tests/Unit/App/Dto/OrderLineDtoTest.php new file mode 100644 index 00000000..a7a908fc --- /dev/null +++ b/tests/Unit/App/Dto/OrderLineDtoTest.php @@ -0,0 +1,101 @@ +id)->toBe($id) + ->and($dto->order_id)->toBe($orderId) + ->and($dto->product_name)->toBe($productName) + ->and($dto->product_inco_id)->toBe($productIncoId) + ->and($dto->product_brand_label)->toBe($productBrandLabel) + ->and($dto->is_variable_weight)->toBe($isVariableWeight) + ->and($dto->price_kg_ttc)->toBe($priceKgTtc) + ->and($dto->quantity)->toBe($quantity) + ->and($dto->unit_price)->toBe($unitPrice) + ->and($dto->unit_tax)->toBe($unitTax) + ->and($dto->unit_discount)->toBe($unitDiscount) + ->and($dto->tax_rate)->toBe($taxRate) + ->and($dto->unit_price_ttc)->toBe($unitPriceTtc) + ->and($dto->unitPriceTtc)->toBe($unitPriceTtc) + ->and($dto->unit_price_discounted_ht)->toBe($unitPriceDiscountedHt) + ->and($dto->unitPriceDiscountedHt)->toBe($unitPriceDiscountedHt) + ->and($dto->total_gross_ht)->toBe($totalGrossHt) + ->and($dto->totalGrossHt)->toBe($totalGrossHt) + ->and($dto->total_ht)->toBe($totalHt) + ->and($dto->total_tax)->toBe($totalTax) + ->and($dto->total_discount)->toBe($totalDiscount) + ->and($dto->total_ttc)->toBe($totalTtc); + + expect($dto->toArray())->toBe([ + 'id' => $id, + 'order_id' => $orderId, + 'product_name' => $productName, + 'product_inco_id' => $productIncoId, + 'product_brand_label' => $productBrandLabel, + 'is_variable_weight' => $isVariableWeight, + 'price_kg_ttc' => '3938', + 'quantity' => $quantity, + 'unit_price' => '1969', + 'unit_tax' => '108', + 'unit_discount' => '0', + 'tax_rate' => $taxRate->value, + 'unit_price_ttc' => '2077', + 'unit_price_discounted_ht' => '1969', + 'total_ttc' => '4154', + 'total_ht' => '3938', + 'total_tax' => '216', + 'total_discount' => '0', + 'total_gross_ht' => '3938', + ]); + }); +});