rip-byrokrat / accounting
根据瑞典标准分析和生成会计数据
dev-master
2023-05-24 13:25 UTC
Requires
- php: ^8.0
- moneyphp/money: ^3
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-26 18:08:06 UTC
README
会计
根据瑞典标准分析和生成会计数据。
安装
composer require byrokrat/accounting
为什么?
虽然可以在Accounting的基础上构建一个通用的会计应用,但这从未是主要关注点。创建Accounting的动机是为了解决以下两种场景的需求
- 使用模板(可能导入通用会计)生成会计数据。
- 分析会计数据(可能从通用会计导出)。
为了实现会计数据的导入和导出,Accounting支持解析和生成SIE4文件格式。
使用方法
处理货币金额
Accounting 使用 Moneyphp 来处理货币金额。关于Money api的更多信息可以在他们的网站上找到。在这些例子中,我们需要格式化金额,我们使用简单的 DecimalMoneyFormatter
来完成。
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' ]) ) );
编写SIE4文件
use byrokrat\accounting\Sie4\Writer\Sie4iWriter; $sie = (new Sie4iWriter)->generateSie($verifications);
解析SIE4文件
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() );
将交易分类到账簿(huvudbok)中
Accounting如何用于将交易分类到账簿(或瑞典语中的
$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()) ); });
编写宏
宏允许在运行时扩展查询API,而无需对查询类本身进行子类化。这对于添加特定于项目的排序和过滤方法非常合适。例如,如果我们想根据描述进行过滤,我们可以为这个定义一个宏
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();
黑客
安装了 composer 作为 composer
,并且安装了 phive 作为 phive
make
或者使用类似的方法
make COMPOSER_CMD=./composer.phar PHIVE_CMD=./phive.phar