119 lines
4.7 KiB
PHP
119 lines
4.7 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace Modules\Restaurant\Database\Seeders;
|
||
|
|
|
||
|
|
use Faker\Factory as Faker;
|
||
|
|
use Illuminate\Database\Seeder;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
use Modules\Authentication\Models\User;
|
||
|
|
use Modules\Restaurant\Models\Ingredient;
|
||
|
|
use Modules\Restaurant\Models\Purchase;
|
||
|
|
use Modules\Restaurant\Models\PurchaseItem;
|
||
|
|
use Modules\Restaurant\Models\Stock;
|
||
|
|
use Modules\Restaurant\Models\Supplier;
|
||
|
|
|
||
|
|
class PurchaseSeeder extends Seeder
|
||
|
|
{
|
||
|
|
public function run(): void
|
||
|
|
{
|
||
|
|
$faker = Faker::create();
|
||
|
|
|
||
|
|
$restaurantId = 1; // set dynamically later if needed
|
||
|
|
$supplierIds = Supplier::pluck('id')->toArray();
|
||
|
|
$ingredientIds = Ingredient::pluck('id')->toArray();
|
||
|
|
$userIds = User::pluck('id')->toArray();
|
||
|
|
|
||
|
|
if (empty($supplierIds) || empty($ingredientIds)) {
|
||
|
|
$this->command->warn('⚠️ Skipping PurchaseSeeder: No suppliers or ingredients found.');
|
||
|
|
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$purchaseCount = rand(10, 20);
|
||
|
|
|
||
|
|
for ($i = 1; $i <= $purchaseCount; $i++) {
|
||
|
|
$supplierId = $faker->randomElement($supplierIds);
|
||
|
|
$createdBy = ! empty($userIds) ? $faker->randomElement($userIds) : null;
|
||
|
|
|
||
|
|
$purchase = Purchase::create([
|
||
|
|
'restaurant_id' => $restaurantId,
|
||
|
|
'supplier_id' => $supplierId,
|
||
|
|
'invoice_no' => strtoupper(Str::random(8)),
|
||
|
|
'purchase_date' => $faker->dateTimeBetween('-3 months', 'now'),
|
||
|
|
'sub_total' => 0,
|
||
|
|
'discount_amount' => 0,
|
||
|
|
'tax_amount' => 0,
|
||
|
|
'total_amount' => 0,
|
||
|
|
'payment_status' => $faker->randomElement(['paid', 'unpaid', 'partial']),
|
||
|
|
'payment_method' => $faker->randomElement(['cash', 'bank', 'credit']),
|
||
|
|
'purchase_type' => $faker->randomElement(['ingredient', 'equipment', 'others']),
|
||
|
|
'created_by' => $createdBy,
|
||
|
|
'notes' => $faker->optional()->sentence(10),
|
||
|
|
'status' => 1,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$itemCount = rand(2, 6);
|
||
|
|
$subTotal = $taxTotal = $discountTotal = 0;
|
||
|
|
|
||
|
|
for ($j = 1; $j <= $itemCount; $j++) {
|
||
|
|
$ingredientId = $faker->randomElement($ingredientIds);
|
||
|
|
$quantity = $faker->randomFloat(2, 1, 20);
|
||
|
|
$unitPrice = $faker->randomFloat(2, 10, 500);
|
||
|
|
$tax = $faker->randomFloat(2, 0, 10);
|
||
|
|
$discount = $faker->randomFloat(2, 0, 5);
|
||
|
|
|
||
|
|
$totalCost = ($unitPrice * $quantity) + $tax - $discount;
|
||
|
|
|
||
|
|
$item = PurchaseItem::create([
|
||
|
|
'restaurant_id' => $restaurantId,
|
||
|
|
'purchase_id' => $purchase->id,
|
||
|
|
'ingredient_id' => $ingredientId,
|
||
|
|
'quantity' => $quantity,
|
||
|
|
'unit_price' => $unitPrice,
|
||
|
|
'total_cost' => $totalCost,
|
||
|
|
'tax_amount' => $tax,
|
||
|
|
'discount_amount' => $discount,
|
||
|
|
'batch_no' => strtoupper(Str::random(6)),
|
||
|
|
'expiry_date' => $faker->optional()->dateTimeBetween('now', '+1 year'),
|
||
|
|
'received_quantity' => $quantity,
|
||
|
|
'wastage_quantity' => $faker->randomFloat(2, 0, 1),
|
||
|
|
'returned_quantity' => $faker->randomFloat(2, 0, 1),
|
||
|
|
'status' => 1,
|
||
|
|
]);
|
||
|
|
|
||
|
|
// ✅ Stock Adjustment
|
||
|
|
Stock::create([
|
||
|
|
'restaurant_id' => $restaurantId,
|
||
|
|
'ingredient_id' => $ingredientId,
|
||
|
|
'type' => 'purchase',
|
||
|
|
'quantity' => $quantity,
|
||
|
|
'unit_cost' => $unitPrice,
|
||
|
|
'total_cost' => $totalCost,
|
||
|
|
'reference_type' => 'Purchase',
|
||
|
|
'purchase_id' => $purchase->id,
|
||
|
|
'batch_no' => $item->batch_no,
|
||
|
|
'expiry_date' => $item->expiry_date,
|
||
|
|
'added_by' => $createdBy,
|
||
|
|
'remarks' => 'Auto stock-in from purchase #'.$purchase->invoice_no,
|
||
|
|
'status' => 1,
|
||
|
|
'created_at' => now(),
|
||
|
|
'updated_at' => now(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
$subTotal += ($unitPrice * $quantity);
|
||
|
|
$taxTotal += $tax;
|
||
|
|
$discountTotal += $discount;
|
||
|
|
}
|
||
|
|
|
||
|
|
$purchase->update([
|
||
|
|
'sub_total' => $subTotal,
|
||
|
|
'tax_amount' => $taxTotal,
|
||
|
|
'discount_amount' => $discountTotal,
|
||
|
|
'total_amount' => ($subTotal + $taxTotal - $discountTotal),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->command->info("✅ Seeded {$purchaseCount} purchases with items and stock adjustments successfully.");
|
||
|
|
}
|
||
|
|
}
|