85 lines
2.6 KiB
PHP
85 lines
2.6 KiB
PHP
<?php
|
|
|
|
namespace Database\Factories;
|
|
|
|
use App\Models\Contact;
|
|
use App\Models\Offer;
|
|
use App\Models\OfferVersion;
|
|
use App\User;
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
|
|
|
/**
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Offer>
|
|
*/
|
|
class OfferFactory extends Factory
|
|
{
|
|
protected $model = Offer::class;
|
|
|
|
public function definition(): array
|
|
{
|
|
$y = (int) now()->format('Y');
|
|
|
|
return [
|
|
'offer_number' => $y . '-' . str_pad((string) fake()->unique()->numberBetween(1, 99_999), 5, '0', STR_PAD_LEFT),
|
|
'contact_id' => Contact::factory(),
|
|
'inquiry_id' => null,
|
|
'booking_id' => null,
|
|
'status' => Offer::STATUS_DRAFT,
|
|
'current_version_id' => null,
|
|
'created_by' => User::factory(),
|
|
];
|
|
}
|
|
|
|
public function configure(): static
|
|
{
|
|
return $this->afterCreating(function (Offer $offer) {
|
|
if ($offer->current_version_id) {
|
|
return;
|
|
}
|
|
$v = OfferVersion::query()->create([
|
|
'offer_id' => $offer->id,
|
|
'version_no' => 1,
|
|
'status' => OfferVersion::STATUS_DRAFT,
|
|
'valid_until' => null,
|
|
'total_price' => 0,
|
|
'headline' => 'Test-Angebot',
|
|
'created_by' => $offer->created_by,
|
|
]);
|
|
$offer->update(['current_version_id' => $v->id]);
|
|
});
|
|
}
|
|
|
|
public function sent(): static
|
|
{
|
|
return $this
|
|
->state(['status' => Offer::STATUS_SENT])
|
|
->afterCreating(function (Offer $offer) {
|
|
$v = $offer->refresh()->currentVersion;
|
|
if (! $v) {
|
|
return;
|
|
}
|
|
$v->update([
|
|
'status' => OfferVersion::STATUS_SENT,
|
|
'sent_at' => now(),
|
|
]);
|
|
});
|
|
}
|
|
|
|
public function accepted(): static
|
|
{
|
|
return $this
|
|
->state(['status' => Offer::STATUS_ACCEPTED])
|
|
->afterCreating(function (Offer $offer) {
|
|
$v = $offer->refresh()->currentVersion;
|
|
if (! $v) {
|
|
return;
|
|
}
|
|
$v->update([
|
|
'status' => OfferVersion::STATUS_ACCEPTED,
|
|
'sent_at' => $v->sent_at ?? now(),
|
|
'accepted_at' => now(),
|
|
'accepted_via' => OfferVersion::ACCEPTED_VIA_ADMIN,
|
|
]);
|
|
});
|
|
}
|
|
}
|