| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 | <?php
namespace Database\Factories\Accounting;
use App\Models\Accounting\DocumentLineItem;
use App\Models\Common\Offering;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
 * @extends Factory<DocumentLineItem>
 */
class DocumentLineItemFactory extends Factory
{
    /**
     * The name of the factory's corresponding model.
     */
    protected $model = DocumentLineItem::class;
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $quantity = $this->faker->numberBetween(1, 10);
        return [
            'company_id' => 1,
            'description' => $this->faker->sentence,
            'quantity' => $quantity,
            'created_by' => 1,
            'updated_by' => 1,
        ];
    }
    public function forInvoice(): static
    {
        return $this->state(function (array $attributes) {
            $offering = Offering::where('sellable', true)
                ->inRandomOrder()
                ->first();
            return [
                'offering_id' => $offering->id,
                'unit_price' => $offering->price,
            ];
        })->afterCreating(function (DocumentLineItem $lineItem) {
            $offering = $lineItem->offering;
            if ($offering) {
                $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
                $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
            }
            $lineItem->refresh();
            $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
            $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
            $lineItem->updateQuietly([
                'tax_total' => $taxTotal,
                'discount_total' => $discountTotal,
            ]);
        });
    }
    public function forEstimate(): static
    {
        return $this->state(function (array $attributes) {
            $offering = Offering::where('sellable', true)
                ->inRandomOrder()
                ->first();
            return [
                'offering_id' => $offering->id,
                'unit_price' => $offering->price,
            ];
        })->afterCreating(function (DocumentLineItem $lineItem) {
            $offering = $lineItem->offering;
            if ($offering) {
                $lineItem->salesTaxes()->syncWithoutDetaching($offering->salesTaxes->pluck('id')->toArray());
                $lineItem->salesDiscounts()->syncWithoutDetaching($offering->salesDiscounts->pluck('id')->toArray());
            }
            $lineItem->refresh();
            $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
            $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
            $lineItem->updateQuietly([
                'tax_total' => $taxTotal,
                'discount_total' => $discountTotal,
            ]);
        });
    }
    public function forBill(): static
    {
        return $this->state(function (array $attributes) {
            $offering = Offering::where('purchasable', true)
                ->inRandomOrder()
                ->first();
            return [
                'offering_id' => $offering->id,
                'unit_price' => $offering->price,
            ];
        })->afterCreating(function (DocumentLineItem $lineItem) {
            $offering = $lineItem->offering;
            if ($offering) {
                $lineItem->purchaseTaxes()->syncWithoutDetaching($offering->purchaseTaxes->pluck('id')->toArray());
                $lineItem->purchaseDiscounts()->syncWithoutDetaching($offering->purchaseDiscounts->pluck('id')->toArray());
            }
            $lineItem->refresh();
            $taxTotal = $lineItem->calculateTaxTotal()->getAmount();
            $discountTotal = $lineItem->calculateDiscountTotal()->getAmount();
            $lineItem->updateQuietly([
                'tax_total' => $taxTotal,
                'discount_total' => $discountTotal,
            ]);
        });
    }
}
 |