PHP code example of michael-orenda / general-ledger
1. Go to this page and download the library: Download michael-orenda/general-ledger library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
michael-orenda / general-ledger example snippets
return [
'default_currency' => env('GL_DEFAULT_CURRENCY', 'KES'),
'default_start_of_day' => '00:00:00',
'journal_prefix' => env('GL_JOURNAL_PREFIX', 'JE'),
'locking' => [
'enable_db_triggers' => true,
],
// mapping of account classes/types
'accounts' => [
'asset' => 1,
'liability' => 2,
'equity' => 3,
'revenue' => 4,
'expense' => 5,
],
];
$ledger = $accountLedgerService->getLedger(
organisationId: 1,
ledgerAccountId: 101,
from: '2025-01-01',
to: '2025-01-31'
);
$audit = $auditTrailService->forJournal($journalId);
$report = $glReportService->generate(
organisationId: 1,
fiscalPeriodId: 12
);
$entries = $ledgerQueryService
->forOrganisation(1)
->forPeriod(12)
->forAccount(101)
->get();
$journalId = $journalPostingService->post([
'organisation_id' => 1,
'fiscal_period_id' => 12,
'reference' => 'INV-1001',
'narration' => 'Invoice payment',
'lines' => [
['ledger_account_id' => 10, 'type' => 'debit', 'amount' => 1000],
['ledger_account_id' => 200, 'type' => 'credit', 'amount' => 1000],
],
]);
$reversalJournalId = $journalReversalService->reverse(
journalId: 55,
reason: 'Invoice cancelled'
);
$trialBalance = $trialBalanceService->generate(
organisationId: 1,
fiscalPeriodId: 12
);
$openingBalanceService->generate(
organisationId: 1,
fromPeriodId: 12,
toPeriodId: 13
);
$periodCloseService->close(
organisationId: 1,
fiscalPeriodId: 12
);
$service->post([
'organisation_id' => 1,
'fiscal_period_id' => 12,
'reference' => 'INV-1001',
'narration' => 'Payment for invoice 1001',
'lines' => [
[ 'ledger_account_id' => 10, 'type' => 'debit', 'amount' => 1000.00, 'description' => 'Cash' ],
[ 'ledger_account_id' => 200, 'type' => 'credit', 'amount' => 1000.00, 'description' => 'Revenue' ],
],
'posted_by' => 5,
]);
$periodCloseService->close($organisationId, $fiscalPeriodId);
Route::prefix('api')->group(function () {
Route::prefix('general-ledger')->middleware(['auth:api'])->group(function () {
Route::get('accounts', [AccountController::class, 'index']);
Route::get('accounts/{id}', [AccountController::class, 'show']);
Route::post('journals', [JournalController::class, 'store']);
Route::get('journals/{id}', [JournalController::class, 'show']);
Route::post('periods/{id}/close', [PeriodController::class, 'close']);
});
});
event(new JournalPosted($journal));
DB::transaction(function () use ($payload) {
$journal = Journal::create([...]);
foreach ($payload['lines'] as $line) {
LedgerEntry::create([...]);
}
});
$totalDebits = array_sum(array_map(fn($l) => $l['type'] === 'debit' ? $l['amount'] : 0, $lines));
$totalCredits = array_sum(array_map(fn($l) => $l['type'] === 'credit' ? $l['amount'] : 0, $lines));
if (bccomp((string)$totalDebits, (string)$totalCredits, 4) !== 0) {
throw new \DomainException('Journal is not balanced.');
}
bash
php artisan vendor:publish --provider="MichaelOrenda\GeneralLedger\GeneralLedgerServiceProvider" --tag="config"
php artisan vendor:publish --provider="MichaelOrenda\GeneralLedger\GeneralLedgerServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="MichaelOrenda\GeneralLedger\GeneralLedgerServiceProvider" --tag="seeds"
bash
php artisan migrate
bash
php artisan db:seed --class="\MichaelOrenda\GeneralLedger\Database\Seeders\ChartOfAccountsSeeder"
php artisan db:seed --class="\MichaelOrenda\GeneralLedger\Database\Seeders\OrganisationSeeder"