PHP File Manager
Editing File: Invoice.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Invoice extends CI_Model { private $pdf = NULL; private $pdf_path = 'assets/pdf/'; function __construct() { if(!$this->pdf) { require_once(APPPATH . 'third_party/dompdf/dompdf_config.inc.php'); $this->pdf = new DOMPDF(); $this->pdf->set_paper('A4'); } } public function generateOrderInvoice($order_id = 0, $download_invoice = false, $email_invoice = false, $detailed_invoice = false, $page_copies = 1) { if(($order_info = $this->orders->get($order_id))) { $contents = $this->template('invoice', array ( 'order_info' => $order_info, 'order_items' => $this->orders->getOrderItems($order_id), 'delivery_name' => (($formatted_name = format_name($order_info->delivery_first_name, $order_info->delivery_last_name))) ? $formatted_name : $order_info->delivery_business, 'delivery_address' => format_address($order_info->delivery_address_1, $order_info->delivery_address_2, $order_info->delivery_address_3, $order_info->delivery_town, $order_info->delivery_county, $order_info->delivery_postcode, $order_info->delivery_country), 'delivery_contact_details' => (($delivery_contact = array_filter(array($order_info->delivery_phone, $order_info->delivery_mobile, $order_info->delivery_email))) && isset($delivery_contact[0])) ? $delivery_contact[0] : NULL, 'invoice_name' => (($formatted_name = format_name($order_info->invoice_first_name, $order_info->invoice_last_name))) ? $formatted_name : $order_info->invoice_business, 'real_invoice_name' => (($order_info->invoice_business) ? $order_info->invoice_business : format_name($order_info->invoice_first_name, $order_info->invoice_last_name)), 'invoice_address' => format_address($order_info->invoice_address_1, $order_info->invoice_address_2, $order_info->invoice_address_3, $order_info->invoice_town, $order_info->invoice_county, $order_info->invoice_postcode, $order_info->invoice_country), 'invoice_contact_details' => (($invoice_contact = array_filter(array($order_info->invoice_phone, $order_info->invoice_mobile, $order_info->invoice_email))) && isset($invoice_contact[0])) ? $invoice_contact[0] : NULL, 'page_copies' => $page_copies, 'detailed_invoice' => $detailed_invoice )); $this->pdf->load_html($contents); $this->pdf->render(); $pdf_contents = $this->pdf->output(); $pdf_location = $this->pdf_path . 'invoice-' . $order_id . '.pdf'; if(!(is_really_writable($this->pdf_path) && file_put_contents($pdf_location, $pdf_contents))) { return false; } if($email_invoice && $order_info->invoice_email) { $this->common->sendEmail(array ( 'email' => $order_info->invoice_email, 'bcc' => $order_info->cc_email, 'subject' => $this->config->item('company_name') . ' order #' . $order_info->order_number . ' invoice', 'files' => array ( array($pdf_location, $order_info->order_number . '-invoice.pdf') ), 'variables' => array ( 'name' => ($order_info->invoice_business) ? $order_info->invoice_business : format_name($order_info->invoice_first_name, $order_info->invoice_last_name), 'order_info' => $order_info ) ), 'invoice'); $this->db->query($this->db->update_string('orders', array('invoice_last_sent' => time()), "`order_id` = $order_id")); if(!$download_invoice) { return true; } } if($download_invoice) { $this->downloadInvoice('Invoice #' . $order_info->order_number, $pdf_location); } $this->pdf->stream('invoice.pdf', array('Attachment' => 0)); } return false; } public function generatePaymentInvoice() { } public function downloadInvoice($file_name = '', $pdf_path = '') { if(file_exists($pdf_path)) { header("Content-Type: application/pdf", true); header("Content-Disposition: attachment; filename=\"$file_name.pdf\"", true); header("Content-Length: " . filesize($pdf_path), true); readfile($pdf_path); } } public function template($template = 'invoice', $data = array()) { return $this->load->view('templates/' . $template, $data, true); } }
Cancel