Last active
August 1, 2025 09:12
-
-
Save herbie4/22fac1d8db82825e50d47011a54ba1e0 to your computer and use it in GitHub Desktop.
Invoice Plane adapted function with multiple date parameters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Invoice Plane adapted function with date parameters | |
| // ===================================== | |
| /* | |
| Changed functionality to have an option to use from - until dates in the description of a product. | |
| Example product description: from 12-07-{{{year}}} until 12-07-{{{year+1}}} | |
| Only get_items_and_replace_vars function has been changed from the original set up | |
| Please check the original documentation for the correct set up: https://jefferytay.wordpress.com/2016/11/29/invoiceplane-v1-4-10-enabling-date-parameters/ | |
| Working and tested in version 1.6.1 | |
| July 07, 2025 - updated on 01 aug. 2025 (fix line 27) | |
| */ | |
| // ====================================== | |
| ///Modify – ADDED | |
| public function get_items_and_replace_vars($invoice_id, $invoice_date_created = 'now') | |
| { | |
| $items = array(); | |
| $query = $this->where('invoice_id', $invoice_id)->get(); | |
| foreach($query->result() as $item) { | |
| $item->item_name = $this->parse_item($item->item_name, $invoice_date_created); | |
| // changed to use 2 shortcodes in description | |
| // item description split into parts | |
| if(str_contains( $item->item_description,' until ')) { | |
| $item_desc_array = explode('until', $item->item_description); | |
| $parts = ''; // FIX: prevent duplicates, empty this! | |
| // parse description parts for multiple instances | |
| foreach($item_desc_array as $part) { | |
| $parts .= $this->parse_item($part, $invoice_date_created); | |
| $parts .= 'until'; // add filter back | |
| } | |
| // remove last part 'tot' from string | |
| $parts = substr($parts, 0,-5); | |
| $item->item_description = $parts; | |
| } else { | |
| $item->item_description = $this->parse_item($item->item_description, $invoice_date_created); | |
| } | |
| $items[] = $item; | |
| } | |
| return $items; | |
| } | |
| //Modify – ADDED | |
| private function parse_item($string, $invoice_date_created) | |
| { | |
| if (preg_match_all('/{{{(?<format>[yYmMdD])(?:(?<=[Yy])ear|(?<=[Mm])onth|(?<=[Dd])ay)(?:(?<operation>[-+])(?<amount>[1-9]+))?}}}/m', $string, $template_vars, PREG_SET_ORDER)) { | |
| try { | |
| $formattedDate = new DateTime($invoice_date_created); | |
| } | |
| catch(Exception $e) { // If creating a date based on the invoice_date_created isn't possible, use current date | |
| $formattedDate = new DateTime(); | |
| } | |
| /* Calculate the date first, before starting replacing the variables */ | |
| foreach($template_vars as $var) { | |
| if(!isset($var['operation'], $var['amount'])) continue; | |
| if($var['operation'] == '-') { | |
| $formattedDate->sub( new DateInterval('P' . $var['amount'] . strtoupper($var['format'])) ); | |
| } | |
| else if($var['operation'] == '+') { | |
| $formattedDate->add( new DateInterval('P' . $var['amount'] . strtoupper($var['format'])) ); | |
| } | |
| } | |
| /* Let's replace all variables */ | |
| foreach($template_vars as $var) { | |
| $string = str_replace($var[0], $formattedDate->format($var['format']), $string); | |
| } | |
| } | |
| return $string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment