wzulfikar/eloquent-simple-ledger

该软件包最新版本(1.1.3)没有可用的许可证信息。

1.1.3 2016-05-07 02:36 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:53:06 UTC


README

安装

composer require wzulfikar\eloquent-simple-ledger

用法

// find account from table `accounts`	
$account = \Wzulfikar\EloquentSimpleLedger\Account::findOrFail($your_account_id);

// assuming that current balance is 0
// and now we want to debit 500
// this will create new debit record in table `account_ledgers`
$account->debit(500, 'initial deposit');

// the balance now is 500
// which is previous balance (0) + current transaction (500)
echo $account->balance;

// let's withdraw some money.
// this will create new credit record in table `account_ledgers`
$account->credit(50, 'withdrawal');

// the balance now is 450
// which is previous balance (500) + current transaction (-50)
echo $account->balance;

获取账户余额

$account = \Wzulfikar\EloquentSimpleLedger\Account::findOrFail($your_account_id);
$account->balance;

获取上一期余额

$account->prev_balance;

此方法用于在每个交易后计算余额。

记录新的借方交易

$account->debit($amount, $description);

记录新的贷方交易

$account->credit($amount, $description);

获取账簿记录

$account->ledger->all();

幕后

当发生交易时,eloquent将在account_ledgers表中创建新行。如果交易是借方,则debit列不会为空,但credit列将为空,反之亦然。然后,它将从上一行(其中account_id与新行的account_id相同)获取balance列的值,并将交易金额加到余额上以获得当前交易的余额。

交易完成后,eloquent将缓存account_ledgers表中的最后余额到accounts表中。

表定义

accounts

  • (int) id
    账户ID
  • (int) balance
    账户当前余额

account_ledgers

  • (int) id
  • (int) account_id
  • (int) debit
    借方金额,可为空。
  • (int) credit
    贷方金额,可为空。
  • (text) desc
    交易描述,可为空。
  • (int) balance
    上一期余额和当前交易的总和。此列确保我们有任何时间点的交易余额。

account_ledgers表包含accounts表中所有账户的交易。两个表都包含balance列,但用途不同。

account_ledgers中的balance记录每次交易后的新余额。

借方示例 :
如果上一行的余额是50,当前行的借方是10,则当前行的余额将是50 + 10 = 60。

贷方示例 :
如果上一行的余额是60,当前行的贷方是20,则当前行的余额将是60 + -(20) = 40。

由于每笔交易的余额都记录在account_ledgers中,生成报告(例如银行对账单)将更容易。

accounts中的balance列用于获取特定账户的余额,这实际上是特定账户的account_ledgers表中最后一行的balance值。这是为了避免仅为了获取账户余额而查询整个account_ledgers行。

account_ledgers表中的所有行都应仅作为只读数据。只能创建、更新或修改不应处理的行。

借方、贷方和余额默认都是整数。

与用户模型集成

例如,您想获取当前登录用户的余额,可能如下所示

auth()->user()->account->balance;

您需要将您的用户模型与账户模型关联起来。

首先,确保您的用户模型表中存在account_id列,并且它包含属于用户的账户ID。

其次,在您的用户模型中添加eloquent关系

// user model
public function account(){
	return $this->hasOne(Wzulfikar\EloquentSimpleLedger\Account::class);	
}

迁移

将包的迁移文件夹中的文件复制到您的Laravel迁移文件夹中,并运行php artisan migrate

如果您不想将文件复制到应用程序的迁移文件夹中,请在Artisan迁移命令中传递包迁移文件的路径。例如:php artisan migrate --path=vendor/wzulfikar/eloquent-simple-ledger/migrations

外观效果

要查看外观效果,

  • 将包的routes.php包含到您应用程序的routes.php
    require_once base_path('vendor/wzulfikar/eloquent-simple-ledger/routes.php');
  • accountaccount_ledgers创建示例数据,然后访问/ledger/{account_id}

示例视图:无数据

image

示例视图:有数据

image

示例视图包含一些功能

  • 导出到Excel、csv和pdf
  • 使用Ajax重新加载数据
  • 最新交易指示器
  • 使用moment.js的友好时间
  • 响应式表格、可排序列和可搜索——是的,它使用datatables :)
  • 债务指示器:如果余额小于0,则余额将显示为红色

附加面板

  • 交易历史

image