8
The Problem
I am creating a financial system where I need to register a recurring movement, for example, a light bill that repeats every month. In the system, this type of movement is treated as a fixed expense/revenue.
My table has the following structure:
[tabela de movimentações]
- id // Código do registro (int, auto increment)
- title // Um título para a movimentação (var_char)
- description // Descrição da movimentação (text)
- type // Se é receita ou despesa (tinyint)
- repeat // Tipo de repetição (tinyint)
- is_recurring // Se é uma movimentação recorrente ou não (tinyint)
- start_date // Data de início (timestamp)
- value // valor da movimentação (decimal) só preenchido se for recorrente
The field repeat
records an integer representing the repeat interval, they being:
0 - Nunca
1 - Todos os dias
2 - Toda semana
3 - A cada duas semanas
4 - Todo mês
5 - A cada dois meses
6 - A cada três meses
7 - A cada seis meses
8 - Todo ano
If the movement is non-recurring and has predefined plots, either one or several, are already saved in another table that has the following structure:
[tabela de parcelas]
- id // Código do registro (int, auto increment)
- value // valor da movimentação (decimal)
- date // Data da parcela (date)
- status // Status da parcela (tinyint) pago, pendente...
- trasanction_id // Referencia á primeira tabela para conseguir recuperar todas as
// parcelas envolvidas em uma movimentação
So I can recover all the split moves, but my problem is the recurring ones, which only enter the plot table after they are made.
How to build a forecast of future expenses considering these recurrent movements.
I cannot add multiple entries to the installment table until a certain date, because if a report is generated for a period longer than the last record, the user would make wrong decisions without knowing that that fixed income/expense was not considered.
The solution can be a mix of PHP and Mysql as long as it doesn’t get too heavy.
Example
I have a revenue of R $ 10,00 real that I receive every week from 04/08/2014.
The day 04/08/2014 has been paid and has been registered in the table of installments as paid. The future not yet.
I need to list all recipes in the period from 01/08/2014 to 05/09/2014.
The above revenue should appear on the dates 04/08, 11/08, 18/08, 25/08 and 01/09 in this listing
Solutions found that did not meet
I have found some solutions, which I quote below:
Possible solution 1
In this solution a table is used to store when the data must be repeated, using a cron-like mechanism. It works, but the performance is horrible for reports cases of several days.
The query used in the solution is based on the account on a specific day, so, to assemble a monthly report, I will hold 30 consultations at the bank, and for an annual report, 365 queries.
Possible solution 2
This solution uses a calendar table. A branch break solution, which can bring many future maintenance problems, such as the lack of dates registered in the calendar table, volume of records stored without need, etc.
I believe it would be better to generate multiple records and link these records to a recurring account, then at the time of generating you ask the user how much ends and what recurrence, then you generate all installments until the end date. Otherwise everything will get complicated, think of the reports you will have to generate
– Fábio Lemos Elizandro
This type of movement has no end date, and it is precisely because of the reports that I need it. The registry of movement as revenue/fixed expenditure is used to make a forecast. If I always put a final date, the person can forget and make a serious management mistake by thinking that in the forecast that drive was being considered.
– marcusagm
I see several problems in the desired exit. One of them, for example: if you change the value, you cancel the past release, lose all history and make new event? Intermediate tables in these applications are not merely facilitators, but ways to maintain consistency. One of the simplest solutions is a table with a "rolling" event, which updated each release leaving traces in the past (at the right time, the event drops a clone in the history, and is scheduled for the future date). The rolling event.
– Bacco
I am already saving what actually occurred in another table, this is not the problem, I keep the history. The problem is to recover the future data. For example, I have a quarterly account, which started on 07/10/2014, if I request a report for 08 month this account does not appear, but in a report for 10 month will appear. If I request a report from 1/07/2014 to 30/11/2014 this account has to appear twice
– marcusagm
In this case I cannot clone the data after something happens, because I need to assemble future predictions, that is, I need to perform calculations that consider the recurrent record.
– marcusagm
@marcusagm, I think I completely misunderstood your doubt... I removed my answer to not pollute.
– Papa Charlie
All right @papa-charlie, this question is very difficult to come to a conclusion
– marcusagm