wzulfikar/eloquent-simple-ledger 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wzulfikar/eloquent-simple-ledger

Composer 安装命令:

composer require wzulfikar/eloquent-simple-ledger

包简介

README 文档

README

Installation

composer require wzulfikar\eloquent-simple-ledger

Usage

// 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;

Get account balance

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

Get Previous Balance

$account->prev_balance;

This method used to calculate the balance after each transaction.

Record New Debit

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

Record New Credit

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

Get Ledger Records

$account->ledger->all();

Behind The Scene

When a transaction happens, eloquent will create new row in account_ledgers table. If the transaction is debit, the debit column won't be null but the credit column will be, and vice versa. Then, it will get value of balance column from previous row (where the account_id is same as the account_id of new row) and add amount of transaction to get the balance of current transaction.

After the transaction finished, eloquent will cache the last balance of account_ledgers table in accounts table.

Table Definitions

Table accounts :

  • (int) id
    id of account
  • (int) balance
    current balance of account

Table account_ledgers :

  • (int) id
  • (int) account_id
  • (int) debit
    amount of debit, nullable.
  • (int) credit
    amount of credit, nullable.
  • (text) desc
    description of transaction, nullable.
  • (int) balance
    sum of previous balance and current transaction. this column ensures we have balance of a transaction of any time.

Table account_ledgers contains transactions from all accounts in table accounts. Both tables have balance column, but with different usage.

balance in account_ledgers records new balance after every transaction.

Example Debit :
if balance of last row is 50 and current row's debit is 10, then the balance of current row will be 50 + 10 = 60.

Example Credit :
if balance of last row is 60 and current row's credit is 20, then the balance of current row will be 60 + -(20) = 40.

Since balance of every transaction is recorded in account_ledgers, generating report (eg. bank-statement) will be easier.

The balance column in table accounts used to get balance of particular account, which is actually value of balance of last row in table account_ledgers of that particular account. This is to avoid querying whole rows in account_ledgers just to get balance of an account.

All rows in table account_ledgers are meant to be read-only data. A row can only be created, update or modification should not be processed.

debit, credit & balance are all integers by default.

Integrating with User Model

Say, you want to get balance of currently logged in user, which might be something like this:

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

you need to connect your user model with account model.

First, make sure the column account_id is available in table of your user model and it contains id of account that belongs to the user.

Secondly, add eloquent relationship in your user model:

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

Migration

Copy files in package's migrations folder into your laravel's migrations folder and run php artisan migrate.

If you don't want to copy thus files into your app's migration folder, pass the path to package's migration files in artisan migrate command. Like this: php artisan migrate --path=vendor/wzulfikar/eloquent-simple-ledger/migrations

How it Looks

To see how it looks,

  • include package's routes.php into your app's routes.php :
    require_once base_path('vendor/wzulfikar/eloquent-simple-ledger/routes.php');
  • create dummy data for account and account_ledgers and then visit /ledger/{account_id}.

Sample View : No Data

image

Sample View : With Data

image

The sample view included some features:

  • export to excel, csv & pdf
  • reloading data using ajax
  • indicator for latest transaction
  • human friendly time using moment.js
  • responvie table, sortable columns & searchable -- yes, it uses datatables :)
  • debts indicator : balance will be red if it less than 0

Additional Panels

  • Transaction History

image

wzulfikar/eloquent-simple-ledger 适用场景与选型建议

wzulfikar/eloquent-simple-ledger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.68k 次下载、GitHub Stars 达 24, 最近一次更新时间为 2016 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wzulfikar/eloquent-simple-ledger 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wzulfikar/eloquent-simple-ledger 我们能提供哪些服务?
定制开发 / 二次开发

基于 wzulfikar/eloquent-simple-ledger 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 2.68k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 24
  • 点击次数: 11
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 24
  • Watchers: 6
  • Forks: 15
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2016-04-10