2021-03-27 20:55:16 +01:00
|
|
|
<?php
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2021-03-27 20:55:16 +01:00
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
use App\Casts\MoneyCast;
|
|
|
|
|
use App\Casts\OrderLineProductCast;
|
|
|
|
|
use Domain\Enums\TaxRate;
|
|
|
|
|
use Illuminate\Database\Eloquent\Casts\Attribute;
|
2021-03-27 20:55:16 +01:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2026-06-14 21:32:07 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Money\Money;
|
2021-03-27 20:55:16 +01:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/**
|
|
|
|
|
* @property int $id
|
|
|
|
|
* @property string $order_id
|
|
|
|
|
* @property \Domain\Models\ValueObjects\OrderLineProduct $product Domain\Models\ValueObjects\OrderLineProduct
|
|
|
|
|
* @property int $quantity
|
|
|
|
|
* @property Money $unit_price
|
|
|
|
|
* @property Money $unit_tax
|
|
|
|
|
* @property Money $unit_discount
|
|
|
|
|
* @property TaxRate $tax_rate
|
|
|
|
|
* @property Money $total_ttc
|
|
|
|
|
* @property Money $total_ht
|
|
|
|
|
* @property Money $total_tax
|
|
|
|
|
* @property Money $total_discount
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
|
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
|
|
|
* @property-read \App\Models\Order $order
|
|
|
|
|
* @property-read Money $total_gross_ht
|
|
|
|
|
* @property-read Money $unit_price_discounted_ht
|
|
|
|
|
*
|
|
|
|
|
* @mixin \Eloquent
|
|
|
|
|
*/
|
2021-03-27 20:55:16 +01:00
|
|
|
class OrderLine extends Model
|
|
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\App\Models\Order,$this> */
|
|
|
|
|
public function order(): BelongsTo
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Order::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<Money,never> */
|
|
|
|
|
protected function unitPriceDiscountedHt(): Attribute
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn (): Money => $this->unit_price->subtract($this->unit_discount)
|
|
|
|
|
);
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
2021-04-08 15:49:20 +02:00
|
|
|
|
2026-06-14 21:32:07 +02:00
|
|
|
/** @return \Illuminate\Database\Eloquent\Casts\Attribute<Money,never> */
|
|
|
|
|
protected function totalGrossHt(): Attribute
|
2021-04-08 15:49:20 +02:00
|
|
|
{
|
2026-06-14 21:32:07 +02:00
|
|
|
return Attribute::make(
|
|
|
|
|
get: fn (): Money => $this->total_ht->add($this->total_discount)
|
2022-09-08 13:02:16 +02:00
|
|
|
);
|
2026-06-14 21:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[\Override]
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'quantity' => 'integer',
|
|
|
|
|
'unit_price' => MoneyCast::class,
|
|
|
|
|
'unit_tax' => MoneyCast::class,
|
|
|
|
|
'unit_discount' => MoneyCast::class,
|
|
|
|
|
'tax_rate' => TaxRate::class,
|
|
|
|
|
'total_ttc' => MoneyCast::class,
|
|
|
|
|
'total_ht' => MoneyCast::class,
|
|
|
|
|
'total_tax' => MoneyCast::class,
|
|
|
|
|
'total_discount' => MoneyCast::class,
|
|
|
|
|
'product' => OrderLineProductCast::class,
|
|
|
|
|
];
|
2021-04-08 15:49:20 +02:00
|
|
|
}
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|