| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 | <?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 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,
            ]);
        });
    }
}
 |