Make recurring invoices more flexible with vars like {{{month}}}

Within recurring invoices I needed a function to show the billing period within an item. Something like this would do the trick:

billing period is {{{day+10}}} and {{{Month+2}}} and {{{Year+2}}}

Within modules/invoices/models/mdl_invoices.php in the public function copy_invoice I placed Q&D code snippet below

foreach ($invoice_items as $invoice_item) {
        // SNIPPET HERE
        $now = new DateTime();
        $replacements = explode('{{{', $invoice_item->item_description);
        foreach ($replacements as $replacement) {
            $replace = stristr($replacement, '}}}', true);
            if (empty($replace)) continue;
            $replacement='{{{'.$replace.'}}}';
            if ($pos = strpos($replace, '+')) {
                $add = substr($replace,$pos+1);
                $addtype = substr(strtolower($replace),0,$pos) . 's';
                $now = date_add($now, date_interval_create_from_date_string("$add $addtype"));
            }
            $format = substr($replace,0,1);
            setlocale(LC_TIME, 'DE');
            $replace = date_format($now, $format);
            $invoice_item->item_description = str_replace ($replacement,$replace,$invoice_item->item_description);
        }
        // END SNIPPET HERE
$db_array = array(

This does respect the first letter in uppercase for the long date version.

More thoughts:

  1. What I do not like is the setlocale(‘DE’) as constant fixed. It might be a better archievement to get it from the client record or alternately from the settings table, but I’ve not found a quick trick to pick it up (ip novice).

  2. Errors are not caught with in the loop. User must know what could be used (Ref http://php.net/manual/function.date.php). What will work for shure is (D/d)ay, (M/m)onth, (Y/y)ear may be more.

  3. Exented could also be the ‘+’ sign to also respect a ‘-’ and the date_sub function.

  4. This might be an idea for a more common function and the whole item selection on creating new invoices/quotes or other user text parts in the system.

hth Markus

[size=10]Last Update: 29.06.2015[/size]

See this pull request: https://github.com/InvoicePlane/InvoicePlane/pull/261