1

I have two collection in database income_funds and expenditure_funds:

income_funds
    timestamp
    amount
    status (not confirmed, confirmed, refunded)

expenditure_funds
    timestamp
    amount

I need get current account balance and get reports about using. How better implement billing logic with this collections?

The simplest way (without additional data):

  1. to get account balance sum(confirmed income_funds) - sum(expenditure_funds)
  2. to get report get income and expenditure funds by period

But I don't like that I must all time calculate account balance.

Add account collection:

I will add account collection:

account
    balance
  1. to get account balance just get account.balance
  2. get report as previous method

But I don't like that I must change balance any time when any funds changed (added or changed status). To verified account balance I always must calculate all funds as in first method. If I get mistake I will don't know how and when it happened.

Add period collection:

I will add period collection:

period
    is_closed (yes, no)
    start_timestamp
    end_timestamp
    start_balance
    end_balance
  1. to get account balance return last period.end_balance
  2. get report as previous method but easy get timestamps by period

I must change end_balance any time when any funds changed in period. But I can verify balance by period and if will have mistake I will know that it's happened on this period.


Last method likes me most of all, but maybe somebody can give me advice in my conclusions or know other solution.

4

2 回答 2

2

我认为您的设计有些问题。我会建议一些不同的东西(这里非常简化,注意我专业地构建会计软件):

account (id, account_number, description, account_type)
journal (id, name, description)
journal_entry (id, journal_id, reference, description, transaction_date, memo)
journal_line (journal_entry_id, account_id, amount)
invoice (id, journal_entry_id, customer_vendor_id, duedate, terms)
invoice_line (id, invoice_id, goods_service_id, qty, sellprice, discount)
account_checkpoint (id, account_id, debits, credits, balance, end_date)

然后你要做的是在 journal_entry 上触发,这样 transaction_date 的任何条目早于 max(account_checkpoint.end_date) 都会被拒绝。通过这种方式,您可以从上一个关闭期间向前滚动并通过开放期间。这使报告变得更加容易,并最大限度地减少了您实际需要汇总的内容。

也可以为周期添加其他检查点表,因此如果企业愿意,您可以比 AP 更频繁地关闭 AR,反之亦然。通过这种方式,检查点既可以用作结束点,也可以用作您可以前滚以从中生成报告的点。

于 2013-03-07T15:31:28.240 回答
1

我想我已经在我的项目中使用了所有 3 个选项。根据您的项目范围和可能性,您可以决定走哪条路。

如果您的数据库支持触发器,那么您可以使用单独的 Balance 表并在您的 *_funds 表中使用更新/插入/删除触发器来更新 Balance 表中的数据。这样可以确保无论数据如何变化,您的余额始终是正确的。我会推荐使用这个选项。

如果您没有大量数据并且您不需要经常计算余额,那么您可以重新计算总和。恕我直言,它将传入和传出的资金存储在一张表中会更容易一些。添加指示交易方向的额外字段(交易)(1 表示收款,-1 表示费用)。然后你可以通过做得到平衡

SELECT SUM(amount*transaction) from funds

第三,当我将数据存储在月度文件中时,我使用了过去定期余额的选项。每当创建新文件时,第一条记录就是开始余额,根据上个月的数据当场计算。当上个月的数据发生变化时,我还提供了在极少数情况下重新计算余额的可能性。当用户非常清楚自己在做什么时,这可以正常工作 - 例如在会计知道他需要重新计算余额的会计系统中。

于 2013-03-07T07:55:02.100 回答