HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/database/factories/InvoiceFactory.php
<?php

namespace Database\Factories;

use App\Model\Invoice;
use App\Model\Customer;
use App\Model\Order;
use App\Model\OrderItem;
use Illuminate\Database\Eloquent\Factories\Factory;

class InvoiceFactory extends Factory
{
    protected $model = Invoice::class;

    public function definition(): array
    {
        return [
            'uid'                           => fake()->uuid(),
            'order_id'                      => null,  // set in configure() below
            'customer_id'                   => Customer::factory(),
            'status'                        => 'new',
            'billing_first_name'            => fake()->firstName(),
            'billing_last_name'             => fake()->lastName(),
            'billing_address'               => fake()->address(),
            'billing_email'                 => fake()->safeEmail(),
            'billing_phone'                 => fake()->phoneNumber(),
            'billing_country_id'            => null,
            'number'                        => 'INV-' . fake()->randomNumber(5),
            'no_payment_required_when_free' => 0,
        ];
    }

    public function configure(): static
    {
        // Create a default Order + OrderItem (total = 29.99 USD) so Invoice::total() works.
        return $this->afterMaking(function (Invoice $invoice) {
            if (!$invoice->order_id) {
                $order = Order::factory()->create([
                    'customer_id' => $invoice->customer_id,
                ]);
                OrderItem::factory()->create([
                    'order_id' => $order->id,
                    'amount'   => 29.99,
                ]);
                $invoice->order_id = $order->id;
            }
        });
    }

    public function paid(): self
    {
        return $this->state(fn() => ['status' => 'paid']);
    }
}