Home > Invoice Plane, Open Source > InvoicePlane v1.4.10 – Enabling Date Parameters

InvoicePlane v1.4.10 – Enabling Date Parameters

As an update to https://jefferytay.wordpress.com/2016/02/25/invoiceplaneenabling-date-parameters/

File: Application\helpers\pdf_helper.php
Action:
Replace
    $items = $CI->mdl_items->where(‘invoice_id’, $invoice_id)->get()->result();   
with
    $items = $CI->mdl_items->get_items_and_replace_vars($invoice_id, $invoice->invoice_date_due);

image

File: application\modules\guest\controllers\view.php
Action:
Replace
    ‘items’ => $this->mdl_items->where(‘invoice_id’, $invoice->invoice_id)->get()->result(),
with
    ‘items’ => $this->mdl_items->get_items_and_replace_vars($invoice->invoice_id, $invoice->invoice_date_due),

image

File: application\modules\invoices\models\mdl_items.php
Action:
Add the following lines after "class Mdl_Items extends Response_Model {"

    ///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);
             $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;
    }

image

Advertisements
Categories: Invoice Plane, Open Source
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: