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.
This commit is contained in:
Jérémy Munsch 2026-11-02 14:53:06 +01:00
parent fab265cd68
commit 192e72f554
No known key found for this signature in database
GPG key ID: 2AAECDA6B7330F91
7 changed files with 263 additions and 29 deletions

View file

@ -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(),
];
}
}

View file

@ -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<Money,never> */
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<Money,never> */
protected function totalGrossHt(): Attribute
{

View file

@ -158,11 +158,10 @@
<tbody>
@foreach ($order->lines as $line)
<tr>
<td>{{ $line->product->incoId->value }}</td>
<td>{{ $line->product_inco_id }}</td>
<td>
<span class="d-block">{{ $line->product->name }}</span>
<span class="d-block fst-italic">{{ $line->product->brand->label() }}</span>
<span class="d-block d-none">@money($line->product->discountedPrice ?? $line->product->price)</span>
<span class="d-block">{{ $line->product_name }}</span>
<span class="d-block fst-italic">{{ $line->product_brand_label }}</span>
</td>
<td>{{ $line->quantity }}</td>
<td>@money($line->unit_price)</td>

View file

@ -42,15 +42,15 @@
</td>
@endif
<td class="text-start align-middle">
{{ $line->quantity }} x {{ $line->product->name }}
{{ $line->quantity }} x {{ $line->product_name }}
</td>
<td class="text-center align-middle">
@if ($line->product->weight->isVariable)
@money($line->product->priceKgTtc)
@if ($line->is_variable_weight && $line->price_kg_ttc)
@money($line->price_kg_ttc)
@endif
</td>
<td class="text-center align-middle">
@money($line->product->discountedPrice ?? $line->product->price)
@money($line->unit_price_ttc)
</td>
</tr>
@endforeach
@ -82,19 +82,19 @@
<div class="card rounded-4 mb-3 shadow-sm">
<div class="card-body">
<div class="fw-bold border-bottom mb-2 pb-2">
<span>{{ $line->quantity }} x {{ $line->product->name }}</span>
<span>{{ $line->quantity }} x {{ $line->product_name }}</span>
</div>
@if ($line->product->weight->isVariable)
@if ($line->is_variable_weight && $line->price_kg_ttc)
<div class="d-flex justify-content-between align-items-center mb-2">
<span class="fw-medium text-muted">{{ __('Price per kg (incl. tax)') }}</span>
<span>@money($line->product->priceKgTtc)</span>
<span>@money($line->price_kg_ttc)</span>
</div>
@endif
<div class="d-flex justify-content-between align-items-center">
<span class="fw-medium text-muted">{{ __('Unit price (incl. tax)') }}</span>
<span class="fw-bold">@money($line->product->discountedPrice ?? $line->product->price)</span>
<span class="fw-bold">@money($line->unit_price_ttc)</span>
</div>
</div>
</div>

View file

@ -31,7 +31,7 @@
@foreach ($order->lines as $line)
<tr class="border-bottom border-dark-subtle">
<th class="fw-normal py-3 ps-0" scope="row">
<span class="fw-semibold">{{ $line->quantity }}</span> x {{ $line->product->name }}
<span class="fw-semibold">{{ $line->quantity }}</span> x {{ $line->product_name }}
</th>
<td class="fw-medium py-3 pe-0 text-end">@money($line->total_ttc)</td>
</tr>

View file

@ -0,0 +1,97 @@
<?php
declare(strict_types=1);
use App\Dto\OrderLineDto;
use App\Models\Order;
use App\Models\OrderLine;
use App\Models\Shop;
use Domain\Enums\OrderStatus;
use Domain\Enums\ProductBrand;
use Domain\Enums\TaxRate;
use Domain\Enums\Units\OrderUnit;
use Domain\Models\ValueObjects\DeliveryAddress;
use Domain\Models\ValueObjects\Entities\ProductId;
use Domain\Models\ValueObjects\IncoId;
use Domain\Models\ValueObjects\Matricule;
use Domain\Models\ValueObjects\OrderEmployee;
use Domain\Models\ValueObjects\OrderLineProduct;
use Domain\Models\ValueObjects\OrderReference;
use Domain\Models\ValueObjects\ProductWeight;
use Domain\Models\ValueObjects\ShopperEstablishmentCode;
use Domain\Models\ValueObjects\ShopperServiceCode;
use Domain\Models\ValueObjects\ShopperSiteCode;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Date;
use Money\Money;
use Ramsey\Uuid\Uuid;
uses(RefreshDatabase::class);
describe('OrderLineDto Integration', function (): void {
test('it maps from OrderLine Eloquent model with correct unit price TTC and flattened product properties', function (): void {
// GIVEN
$shop = Shop::factory()->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));
});
});

View file

@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
use App\Dto\OrderLineDto;
use Domain\Enums\TaxRate;
use Money\Money;
describe('OrderLineDto', function (): void {
it('instantiates correctly with flattened properties and exposes unitPriceTtc', function (): void {
// GIVEN
$id = 42;
$orderId = '018f6c40-1234-7123-8123-123456789012';
$productName = 'Terrine de Foie Gras';
$productIncoId = '12345';
$productBrandLabel = 'LDC';
$isVariableWeight = false;
$priceKgTtc = Money::EUR(3938);
$quantity = 2;
$unitPrice = Money::EUR(1969);
$unitTax = Money::EUR(108);
$unitDiscount = Money::EUR(0);
$taxRate = TaxRate::TVA_5_5;
$unitPriceTtc = Money::EUR(2077);
$totalHt = Money::EUR(3938);
$totalTax = Money::EUR(216);
$totalDiscount = Money::EUR(0);
$totalTtc = Money::EUR(4154);
$unitPriceDiscountedHt = Money::EUR(1969);
$totalGrossHt = Money::EUR(3938);
// WHEN
$dto = new OrderLineDto(
id: $id,
order_id: $orderId,
product_name: $productName,
product_inco_id: $productIncoId,
product_brand_label: $productBrandLabel,
is_variable_weight: $isVariableWeight,
price_kg_ttc: $priceKgTtc,
quantity: $quantity,
unit_price: $unitPrice,
unit_tax: $unitTax,
unit_discount: $unitDiscount,
tax_rate: $taxRate,
unit_price_ttc: $unitPriceTtc,
total_ht: $totalHt,
total_tax: $totalTax,
total_discount: $totalDiscount,
total_ttc: $totalTtc,
unitPriceDiscountedHt: $unitPriceDiscountedHt,
totalGrossHt: $totalGrossHt,
);
// THEN
expect($dto->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',
]);
});
});