*/ class QuickSaleFactory extends Factory { protected $model = QuickSaleModel::class; /** * @return array */ public function definition(): array { return [ 'user_id' => User::factory(), 'customer_id' => null, 'payment_method' => PaymentMethod::Cash, 'sold_at' => now(), 'sold_on' => now(config('app.business_timezone'))->toDateString(), 'total_cents' => 0, 'credits_quick_sale_id' => null, ]; } /** * Adds a line of the product and sets the sale's total. */ public function withLine(ProductModel $product, int $quantity = 1, int $vatBasisPoints = 2550): static { return $this->state(fn (): array => ['total_cents' => $quantity * $product->price_cents]) ->afterCreating(function (QuickSaleModel $sale) use ($product, $quantity, $vatBasisPoints): void { QuickSaleLineModel::query()->create([ 'quick_sale_id' => $sale->id, 'product_id' => $product->id, 'product_name' => $product->name, 'quantity' => $quantity, 'unit_price_cents' => $product->price_cents, 'vat_basis_points' => $vatBasisPoints, 'sort_order' => 1, ]); }); } }