PHP code example of byrokrat / accounting

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

    

byrokrat / accounting example snippets


use Money\Formatter\DecimalMoneyFormatter;
use Money\Currencies\ISOCurrencies;

$moneyFormatter = new DecimalMoneyFormatter(new ISOCurrencies());

use byrokrat\accounting\Template\TransactionTemplate;
use byrokrat\accounting\Template\VerificationTemplate;

$template = new VerificationTemplate(
    description: '{description}',
    transactionDate: '{date}',
    transactions: [
        new TransactionTemplate(
            account: '1920',
            amount: '{bank_amount}'
        ),
        new TransactionTemplate(
            account: '{income_account}',
            amount: '{income_amount}'
        )
    ]
);

use byrokrat\accounting\Container;
use byrokrat\accounting\Dimension\Account;

$accounts = new Container(
    new Account(id: '1920', description: 'Bank'),
    new Account(id: '3000', description: 'Incomes'),
    new Account(id: '3010', description: 'Sales'),
);

use byrokrat\accounting\Template\TemplateRendererFactory;
use byrokrat\accounting\Template\Translator;

$renderer = (new TemplateRendererFactory)->createRenderer($accounts);

$verifications = new Container(
    $renderer->render(
        $template,
        new Translator([
            'description' => 'Some donation...',
            'date' => '2021-01-12',
            'bank_amount' => '999',
            'income_amount' => '-999',
            'income_account' => '3000'
        ])
    ),
    $renderer->render(
        $template,
        new Translator([
            'description' => 'Daily cash register',
            'date' => '2021-01-12',
            'bank_amount' => '333',
            'income_amount' => '-333',
            'income_account' => '3010'
        ])
    )
);

use byrokrat\accounting\Sie4\Writer\Sie4iWriter;

$sie = (new Sie4iWriter)->generateSie($verifications);

use byrokrat\accounting\Sie4\Parser\Sie4Parser;

$parser = new Sie4Parser();

$container = $parser->parse($sie);

echo $container->select()->verifications()->first()->getDescription();

$orderedAccounts = $verifications->select()->accounts()->orderById()->asArray();

echo $moneyFormatter->format(
    $verifications->select()->verifications()->asSummary()->getMagnitude()
);

$verifications->select()->accounts()->orderById()->each(function ($account) use ($moneyFormatter) {
    printf(
        "%s %s\nIncoming balance %s\n",
        $account->getId(),
        $account->getDescription(),
        $moneyFormatter->format($account->getSummary()->getIncomingBalance())
    );

    foreach ($account->getTransactions() as $trans) {
        printf(
            "%s\t%s\t%s\n",
            $trans->getVerificationId(),
            $account->getDescription(),
            $moneyFormatter->format($trans->getAmount()),
        );
    }

    printf(
        "Outgoing balance %s\n\n",
        $moneyFormatter->format($account->getSummary()->getOutgoingBalance())
    );
});

use byrokrat\accounting\Query;

Query::macro('filterOnDescription', function (string $desc) {
    return $this->filter(
        fn($item) => str_contains($item->getDescription(), $desc)
    );
});

echo $verifications->select()->filterOnDescription('donation')->first()->getDescription();