81 lines
2.7 KiB
PHP
81 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use App\Models\Order;
|
|
use App\Models\OrderItem;
|
|
use App\Models\Product;
|
|
use Illuminate\Database\Seeder;
|
|
|
|
class OrderSeeder extends Seeder
|
|
{
|
|
public function run(): void
|
|
{
|
|
// Create some products first
|
|
$products = [];
|
|
$categories = ['Electronics', 'Clothing', 'Home & Garden'];
|
|
|
|
for ($i = 1; $i <= 10; $i++) {
|
|
$products[] = Product::updateOrCreate(
|
|
['sku' => 'DEMO-SKU-' . str_pad((string) $i, 3, '0', STR_PAD_LEFT)],
|
|
[
|
|
'name' => 'Demo Product ' . $i,
|
|
'description' => 'A sample product for order fulfillment demo',
|
|
'price' => round(mt_rand(1000, 50000) / 100, 2),
|
|
'stock_quantity' => mt_rand(50, 500),
|
|
'category' => $categories[array_rand($categories)],
|
|
'status' => 'active',
|
|
]
|
|
);
|
|
}
|
|
|
|
// Create 5 sample orders
|
|
$customers = [
|
|
['name' => 'Alice Johnson', 'email' => 'alice@example.com'],
|
|
['name' => 'Bob Smith', 'email' => 'bob@example.com'],
|
|
['name' => 'Carol Williams', 'email' => 'carol@example.com'],
|
|
['name' => 'David Brown', 'email' => 'david@example.com'],
|
|
['name' => 'Eve Davis', 'email' => 'eve@example.com'],
|
|
];
|
|
|
|
foreach ($customers as $index => $customer) {
|
|
$order = Order::updateOrCreate(
|
|
['order_number' => 'ORD-' . str_pad((string) ($index + 1), 5, '0', STR_PAD_LEFT)],
|
|
[
|
|
'customer_name' => $customer['name'],
|
|
'customer_email' => $customer['email'],
|
|
'status' => 'pending',
|
|
'total_amount' => 0,
|
|
]
|
|
);
|
|
|
|
$totalAmount = 0;
|
|
$itemCount = mt_rand(1, 4);
|
|
|
|
$selectedProducts = array_rand($products, min($itemCount, count($products)));
|
|
if (!is_array($selectedProducts)) {
|
|
$selectedProducts = [$selectedProducts];
|
|
}
|
|
|
|
foreach ($selectedProducts as $productIndex) {
|
|
$product = $products[$productIndex];
|
|
$quantity = mt_rand(1, 5);
|
|
$unitPrice = $product->price;
|
|
|
|
OrderItem::create([
|
|
'order_id' => $order->id,
|
|
'product_id' => $product->id,
|
|
'quantity' => $quantity,
|
|
'unit_price' => $unitPrice,
|
|
]);
|
|
|
|
$totalAmount += $quantity * $unitPrice;
|
|
}
|
|
|
|
$order->update(['total_amount' => $totalAmount]);
|
|
}
|
|
|
|
$this->command->info('Created 10 demo products and 5 sample orders');
|
|
}
|
|
}
|