'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']); }); } } }