The SimpleInvoices forum has moved to Google Plus - this forum is now read-only
Payment date based on creation date
  • adfad666adfad666 June 2009

    I was also looking for how to add a payment date to my invoice, but I didn't find the answer here, so hopefully this will help someone else too.

    Firstly I saw that in template.tpl the date is called by the function $invoice.date

    I found the code for this function on line 909 of sql_queries.php (in beta 4)

    note: the date output format is different from the original, but I forget what it was, maybe m-d-Y or something?)


    $invoice['date'] = date('d.m.Y', strtotime( $invoice['date'] ) );


    I then added this code on the following line:


    $invoice['due_date'] = date('d.m.Y', strtotime( $invoice['date'] . '+28 days' ) );


    then in the template I made a new section in the Invoice Summary section and called the function in exactly the same way as the standard current date with $invoice.due_date

    This is just a simple modification of the strtotime() function where I got the information to do this simple hack from http://us2.php.net/manual/en/function.strtotime.php

    I my case I simply set it to add 28 days.

    in the future I want to make this more complex, as I actually want to add one month, so that it's on the same day of the following month (eg creation date 15/05 due date 15/06) however if you do that on eg 31/01 there is no corresponding 31/02 and so it doesn't work properly, so I want it to then fallback to the last day of the month. There's guides how to do that on the net, I've just got to find some time to do it :)

    in addition, this should also be fairly easy to add to the invoice properties page, but again, see above :)

    Adam

  • adfad666adfad666 June 2009

    As stated above, I wanted to create a due date of +1 month that didn't roll over into the second month if the following month had less days. I used the information from http://us2.php.net/manual/en/function.strtotime.php#88679 to do this.

    Here's the code I added to sql_queries.php:


    <?php
    function get_x_months_to_the_future( $base_time = null, $months = 1 )
    {
    if (is_null($base_time))
    $base_time = time();

    $x_months_to_the_future = strtotime( "+" . $months . " months", $base_time );

    $month_before = (int) date( "m", $base_time ) + 12 * (int) date( "Y", $base_time );
    $month_after = (int) date( "m", $x_months_to_the_future ) + 12 * (int) date( "Y", $x_months_to_the_future );

    if ($month_after > $months + $month_before)
    $x_months_to_the_future = strtotime( date("Ym01His", $x_months_to_the_future) . " -1 day" );

    return $x_months_to_the_future;
    }

    $invoice['calc_date'] = date('Y-m-d', strtotime( $invoice['date'] ) );
    $invoice['date'] = date('d.m.Y', strtotime( $invoice['date'] ) );
    $invoice['due_date'] = date('d.m.Y', get_x_months_to_the_future( strtotime( $invoice['date'] ) ) );
    ?>


    I didn't have to change my template as i simply added the new function to the existing due_date value I had previously set up, and so all my existing test invoices were updated as a result.

    Adam

  • justinjustin June 2009

    hey adam,

    thanks for posting your code - very helpful to everyone who needs due date on the invoices!

    i may add a due date type thing in the future - but till then will refer everyone to your examples

    cheers

    justin

    SmarterInvoices.com :: Simple Invoices hosting | Kelly.org.au :: Simple Invoices custom development | justin.kelly.org.au
  • FlarsenFlarsen April 2012

    Hi

    I've tried to put the code into sql_queries.php without result.
    Is it any other changes to be done?

    / Finn

Categories