A
A
Anton Ivanov2016-08-14 10:05:30
Database design
Anton Ivanov, 2016-08-14 10:05:30

How to organize the correct logic of earnings/payments?

Hello!
There is a task to write a system in which the company will reflect the movement of money. Roughly speaking, we place an order for the company, we take payment, then, minus interest, we pay this money to the company.
There is a question how to implement it technically in the database
Option 1, two tables, earnings and payments. All is well, as long as these tables do not get very large. In this case, the calculation of the required amount for the payout may take an unacceptably long time (add everything in the earnings table and subtract everything from the payout table).
Option 2, earnings table marked "paid or not" (boolean). In this version, everything is simpler, everything is considered quickly. But there is one problem. Sometimes a company needs an extraordinary payment. And the company can specify a payout amount at which it will not be possible to mark a certain number of orders as "paid". Let's say a company has 3 orders, each for $600, and the company requests a payout of a thousand dollars. I don't want to limit the amount to get a finite number of orders. You can make a crutch in the form of a table of extraordinary payments, but it seems to me that there should be another solution. It is? :)
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lega, 2016-08-14
@lega

I would make a journal where I reflected all movements (all + -). That it was possible to restore/recalculate all derived tables.
In parallel, lay out the amounts by accounts (line by line, one company / person - one line), who has how much money after the movement.
The next payment - we put it in the journal, subtract from the account.
Total: works quickly + there is information for recovery if something goes wrong.

A
Artemy, 2016-08-15
@MetaAbstract

The task is solved through OLTP with OLAP. How you will store the initial data from the user for accruals and payments and process transactions is another matter. And to account for mutual settlements with employees, you make a table of transactions for mutual settlements, for example: (Date, Employee, Accrual / Payout, Amount) and a table for the aggregate amount of settlements, for example: (Month, Employee, Amount of accruals, Amount of payments, Final balance). Further, respectively, when recording data from the user (OLTP), create a transaction and update aggregates for it by months with a balance (OLAP).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question