I'm currently going to write something to automatically create invoices with cronjobs by using PHP and timestamps.
I have a, for me, well-considered idea of how to solve it, but I want to ask the community if someone may see errors in reasoning and possible problems with my solution.
I'm trying to describe my idea as detailed as possible so everyone can follow my way of thinking:
In General there are 4 types of invoices:
- Paid yearly
- Paid semiyearly
- Paid quarterly
- Paid monthly
Purchased products are saved in a SQL database with the billing cycle:
- ID of User
- Product ID
- Billing Cycle
- Last Due Date
Now there is a cronjob that runs once a day to check if it should create a new invoice for each purchased product. In the row Last Due Date
I save the timestamp of the first date to pay when it's created.
A code I already wrote calculates the time that has gone by since the Last Due Date
timestamp and outputs something like this:
- Timestamp is in past or in future
- Month gone by
- Days gone by
Now my rules for creating a new invoice are:
Paid yearly
if ( Timestamp is in past = true AND Month gone by = 11 AND Days gone by >= 20 )
then ( create a new invoice and set "Last Due Date" to time() )
Paid semiyearly
if ( Timestamp is in past = true AND Month gone by = 5 AND Days gone by >= 20 )
then ( create a new invoice and set "Last Due Date" to time() )
Paid quarterly
if ( Timestamp is in past = true AND Month gone by = 3 AND Days gone by >= 20 )
then ( create a new invoice and set "Last Due Date" to time() )
Paid monthly
if ( Timestamp is in past = true AND Month gone by = 0 AND Days gone by >= 20 )
then ( create a new invoice and set "Last Due Date" to time() )
As you can see a new invoice would be created ~10 days before date of payment and the timestamp in Last Due Date
is set to the current time, so when the cronjob checks back the next day no invoice will be created for this purchased product.
My question is if this is an appropriate way of solving this and if you can see any errors in reasoning or problems that may occur?