PHP code example of meita / journal-guard

1. Go to this page and download the library: Download meita/journal-guard 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/ */

    

meita / journal-guard example snippets


app('config')->set('journal-guard.resolvers.account', function ($line, $entry) {
    // return model/array/bool to indicate active account
    return $line->accountId() === 100 ? (object) ['is_active' => true] : null;
});

use Meita\JournalGuard\Facades\JournalGuard;
use Meita\JournalGuard\Exceptions\JournalValidationException;

$payload = [
    'company_id' => 1,
    'branch_id' => 2,
    'currency' => 'SAR',
    'entry_date' => '2024-01-31',
    'reference' => 'JV-1001',
    'description' => 'Accruals',
    'lines' => [
        ['account_id' => 10, 'debit' => 1000, 'credit' => 0, 'description' => 'Expense'],
        ['account_id' => 200, 'debit' => 0, 'credit' => 1000, 'description' => 'Accrual'],
    ],
];

try {
    $result = JournalGuard::validate($payload); // returns ValidationResult on success
} catch (JournalValidationException $e) {
    // $e->errors() holds code/message/path tuples
}

config(['journal-guard.mode' => 'warning']);
$result = JournalGuard::validate($payload);

if (!$result->passed()) {
    // $result->warnings() contains non-blocking issues
}

use Meita\JournalGuard\Traits\GuardsJournalEntries;

class JournalEntryService
{
    use GuardsJournalEntries;

    public function create(array $payload)
    {
        $this->guardJournalEntry($payload);
        // ...persist entry
    }
}

use Meita\JournalGuard\Contracts\RuleInterface;
use Meita\JournalGuard\DTO\JournalEntryDTO;
use Meita\JournalGuard\Exceptions\JournalValidationException;

class NoWeekendEntriesRule implements RuleInterface
{
    public function validate(JournalEntryDTO $entry): void
    {
        if ($entry->date()->isWeekend()) {
            throw new JournalValidationException([
                ['code' => 'weekend_block', 'message' => 'Weekend entries are not allowed.', 'path' => 'entry_date'],
            ]);
        }
    }
}

public function store(Request $request)
{
    try {
        JournalGuard::validate($request->all());
    } catch (JournalValidationException $e) {
        return back()->withErrors($e->errors());
    }

    // continue to save...
}