Files
kulakpos_web/public/restaurant/Modules/Accounting/database/seeders/ExpenseSeeder.php

119 lines
4.3 KiB
PHP
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace Modules\Accounting\Database\Seeders;
use Carbon\Carbon;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Modules\Accounting\Models\Account;
use Modules\Accounting\Models\Expense;
use Modules\Accounting\Models\Fund;
use Modules\Accounting\Models\Transaction;
use Modules\Accounting\Models\TransactionDetail;
class ExpenseSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$restaurantId = 1; // example restaurant
// Sample Expense Data
$expenses = [
[
'fund_name' => 'Cash', // fund where money arrived
'account_code' => 'AC-101', // source account (income/sales)
'amount' => 500.00,
'transaction_date' => Carbon::now()->subDays(2),
'voucher_no' => 'DEP-001',
'received_from' => 'Customer A',
'note' => 'Cash sale expense',
'created_by' => 1,
],
[
'fund_name' => 'Bank',
'account_code' => 'AC-101',
'amount' => 1000.00,
'transaction_date' => Carbon::now()->subDay(),
'voucher_no' => 'DEP-002',
'received_from' => 'Customer B',
'note' => 'Bank expense from sales',
'created_by' => 1,
],
];
foreach ($expenses as $dep) {
$fund = Fund::where('restaurant_id', $restaurantId)
->where('name', $dep['fund_name'])
->first();
$account = Account::where('restaurant_id', $restaurantId)
->where('code', $dep['account_code'])
->first();
if (! $fund || ! $account) {
continue;
}
DB::transaction(function () use ($restaurantId, $fund, $account, $dep) {
// 1⃣ Create Expense
$expense = Expense::create([
'restaurant_id' => $restaurantId,
'fund_id' => $fund->id,
'account_id' => $account->id,
'expense_category_id' => 1,
'amount' => $dep['amount'],
'transaction_date' => $dep['transaction_date'],
'voucher_no' => $dep['voucher_no'],
'received_from' => $dep['received_from'],
'note' => $dep['note'],
'created_by' => $dep['created_by'],
'status' => 1,
]);
// 2⃣ Create Transaction
$transaction = Transaction::create([
'restaurant_id' => $restaurantId,
'fund_id' => $fund->id,
'transaction_type' => 'expense',
'transaction_date' => $dep['transaction_date'],
'reference_no' => $dep['voucher_no'],
'notes' => $dep['note'],
'total_debit' => 0,
'total_credit' => $dep['amount'],
'created_by' => $dep['created_by'],
'status' => 1,
]);
// 3⃣ Transaction Details
// Credit to Fund (increase)
TransactionDetail::create([
'restaurant_id' => $restaurantId,
'transaction_id' => $transaction->id,
'account_id' => $fund->id, // fund receives money (asset)
'debit' => $dep['amount'],
'credit' => 0,
'note' => $dep['note'].' (Fund)',
'status' => 1,
]);
// Debit from Source Account
TransactionDetail::create([
'restaurant_id' => $restaurantId,
'transaction_id' => $transaction->id,
'account_id' => $account->id, // source account (income)
'debit' => 0,
'credit' => $dep['amount'],
'note' => $dep['note'].' (Source Account)',
'status' => 1,
]);
// 4⃣ Update Fund Balance
$fund->increment('current_balance', $dep['amount']);
});
}
}
}