Files
xin-procurement/database/factories/StoreOrderItemModelFactory.php
T
2026-08-11 23:26:34 +08:00

50 lines
1.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Database\Factories;
use App\Models\StoreOrderItemModel;
use Illuminate\Database\Eloquent\Factories\Factory;
/**
* 门店订单明细工厂(order_id / store_id / product_id 需调用方指定;
* amount 未显式指定时按 price × quantity 自动计算;无需 Faker
*
* @extends Factory<StoreOrderItemModel>
*/
class StoreOrderItemModelFactory extends Factory
{
protected $model = StoreOrderItemModel::class;
public function definition(): array
{
return [
'order_id' => 0,
'store_id' => 0,
'product_id' => 0,
'category_id' => 0,
'supplier_id' => 0,
'product_name' => '测试商品',
'product_spec' => '500g/袋',
'unit' => '斤',
'price' => number_format(random_int(100, 5000) / 100, 2, '.', ''),
'image_ids' => '',
'content' => '',
'shelf_life' => 0,
'quantity' => number_format(random_int(100, 10000) / 100, 2, '.', ''),
'weight' => 0,
'amount' => 0,
'cost_price' => 0,
'remark' => '',
];
}
public function configure(): static
{
return $this->afterMaking(function (StoreOrderItemModel $item): void {
if ((float) $item->amount === 0.0 && (float) $item->price > 0 && (float) $item->quantity > 0) {
$item->amount = bcmul((string) $item->price, (string) $item->quantity, 2);
}
});
}
}