72 lines
2.2 KiB
PHP
72 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Offer;
|
|
use App\Models\OfferItem;
|
|
use App\Models\OfferVersion;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\OfferItem>
|
|
*/
|
|
class OfferItemFactory extends Factory
|
|
{
|
|
protected $model = OfferItem::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
$qty = fake()->numberBetween(1, 4);
|
|
$ppu = (float) fake()->randomFloat(2, 200, 4_000);
|
|
return [
|
|
'position' => 0,
|
|
'type' => OfferItem::TYPE_TRAVEL,
|
|
'title' => fake()->sentence(3),
|
|
'description' => null,
|
|
'quantity' => $qty,
|
|
'price_per_unit' => $ppu,
|
|
'total_price' => round($qty * $ppu, 2),
|
|
'travel_program_id' => null,
|
|
'fewo_lodging_id' => null,
|
|
'metadata' => null,
|
|
];
|
|
}
|
|
|
|
public function asDiscount(float $absAmount = 150): static
|
|
{
|
|
return $this->state([
|
|
'type' => OfferItem::TYPE_DISCOUNT,
|
|
'title' => 'Rabatt',
|
|
'quantity' => 1,
|
|
'price_per_unit' => -1 * abs($absAmount),
|
|
'total_price' => -1 * abs($absAmount),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Hängt die Position an die aktuelle Version eines per Factory erstellten Angebots.
|
|
*/
|
|
public function forCurrentVersionOf(Offer $offer): static
|
|
{
|
|
$v = $offer->currentVersion;
|
|
if (! $v) {
|
|
throw new \InvalidArgumentException('Offer hat keine currentVersion — zuerst Offer::factory() erzeugen.');
|
|
}
|
|
return $this->state(fn (array $a) => [
|
|
'offer_version_id' => $v->id,
|
|
'position' => (int) $v->items()->count(),
|
|
]);
|
|
}
|
|
|
|
public function forVersion(OfferVersion $version, ?int $position = null): static
|
|
{
|
|
return $this->state(function (array $a) use ($version, $position) {
|
|
$pos = $position ?? (int) $version->items()->count();
|
|
|
|
return [
|
|
'offer_version_id' => $version->id,
|
|
'position' => $pos,
|
|
];
|
|
});
|
|
}
|
|
}
|