migrate to gtea from bistbucket
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Restaurant\Database\Seeders;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Modules\Authentication\Models\User;
|
||||
use Modules\Restaurant\Models\Customer;
|
||||
use Modules\Restaurant\Models\FoodItem;
|
||||
use Modules\Restaurant\Models\Order;
|
||||
use Modules\Restaurant\Models\OrderItem;
|
||||
use Modules\Restaurant\Models\PaymentMethod;
|
||||
use Modules\Restaurant\Models\Table;
|
||||
|
||||
class OrderSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$foods = FoodItem::all();
|
||||
$tables = Table::all();
|
||||
$customers = Customer::all();
|
||||
$paymentMethods = PaymentMethod::all();
|
||||
$waiter = User::first(); // assume 1st user is waiter
|
||||
|
||||
// Prevent running if no data available
|
||||
if ($foods->isEmpty() || $tables->isEmpty() || $customers->isEmpty() || $paymentMethods->isEmpty()) {
|
||||
$this->command->warn('⚠️ Missing related data (foods, tables, customers, or payment methods). Seeder skipped.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (range(1, 100) as $i) {
|
||||
// Pick a random order date within the last 30 days
|
||||
$orderDate = Carbon::now()->subDays(rand(0, 29))->setTime(rand(10, 22), [0, 30][rand(0, 1)]);
|
||||
|
||||
// Create main order
|
||||
$order = Order::create([
|
||||
'restaurant_id' => 1,
|
||||
'order_type' => collect(['dine_in', 'takeaway', 'delivery'])->random(),
|
||||
'order_number' => 'ORD-'.$orderDate->format('Ymd').'-'.rand(1000, 99999),
|
||||
'status' => collect(['pending', 'completed', 'cancelled'])->random(),
|
||||
'order_date' => $orderDate,
|
||||
'table_id' => $tables->random()->id,
|
||||
'payment_method_id' => $paymentMethods->random()->id,
|
||||
'payment_status' => collect([0, 1])->random(),
|
||||
'waiter_id' => $waiter?->id,
|
||||
'customer_id' => $customers->random()->id,
|
||||
'grand_total' => 0,
|
||||
]);
|
||||
|
||||
// Add random items
|
||||
$total = 0;
|
||||
foreach ($foods->random(rand(1, 3)) as $food) {
|
||||
$qty = rand(1, 2);
|
||||
$price = $food->price ?? 100;
|
||||
$lineTotal = $qty * $price;
|
||||
|
||||
OrderItem::create([
|
||||
'restaurant_id' => 1,
|
||||
'order_id' => $order->id,
|
||||
'food_item_id' => $food->id,
|
||||
'quantity' => $qty,
|
||||
'price' => $price,
|
||||
'total' => $lineTotal,
|
||||
]);
|
||||
|
||||
$total += $lineTotal;
|
||||
}
|
||||
|
||||
$order->update(['grand_total' => $total]);
|
||||
}
|
||||
|
||||
$this->command->info('✅ 100 random orders seeded successfully (within the last 30 days).');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user