Advertisement
Guest User

Untitled

a guest
Sep 27th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 9.32 KB | None | 0 0
  1. <?php
  2. if (!defined('BASEPATH')) exit('No direct script access allowed');
  3.  
  4. /*
  5.  * InvoicePlane
  6.  *
  7.  * @author      InvoicePlane Developers & Contributors
  8.  * @copyright   Copyright (c) 2012 - 2018 InvoicePlane.com
  9.  * @license     https://invoiceplane.com/license.txt
  10.  * @link        https://invoiceplane.com
  11.  */
  12.  
  13. /**
  14.  * Class View
  15.  */
  16. class View extends Base_Controller
  17. {
  18.     /**
  19.      * @param $invoice_url_key
  20.      */
  21.     public function invoice($invoice_url_key = '')
  22.     {
  23.         if (!$invoice_url_key) {
  24.             show_404();
  25.         }
  26.  
  27.         $this->load->model('invoices/mdl_invoices');
  28.  
  29.         $invoice = $this->mdl_invoices->guest_visible()->where('invoice_url_key', $invoice_url_key)->get();
  30.  
  31.         if ($invoice->num_rows() != 1) {
  32.             show_404();
  33.         }
  34.  
  35.         $this->load->model('invoices/mdl_items');
  36.         $this->load->model('invoices/mdl_invoice_tax_rates');
  37.         $this->load->model('payment_methods/mdl_payment_methods');
  38.         $this->load->model('custom_fields/mdl_custom_fields');
  39.  
  40.         $invoice = $invoice->row();
  41.  
  42.         if ($this->session->userdata('user_type') <> 1 and $invoice->invoice_status_id == 2) {
  43.             $this->mdl_invoices->mark_viewed($invoice->invoice_id);
  44.         }
  45.  
  46.         $payment_method = $this->mdl_payment_methods->where('payment_method_id', $invoice->payment_method)->get()->row();
  47.         if ($invoice->payment_method == 0) {
  48.             $payment_method = null;
  49.         }
  50.  
  51.         // Get all custom fields
  52.         $custom_fields = array(
  53.             'invoice' => $this->mdl_custom_fields->get_values_for_fields('mdl_invoice_custom', $invoice->invoice_id),
  54.             'client' => $this->mdl_custom_fields->get_values_for_fields('mdl_client_custom', $invoice->client_id),
  55.             'user' => $this->mdl_custom_fields->get_values_for_fields('mdl_user_custom', $invoice->user_id),
  56.         );
  57.  
  58.         // Attachments
  59.         $attachments = $this->get_attachments($invoice_url_key);
  60.  
  61.         $is_overdue = ($invoice->invoice_balance > 0 && strtotime($invoice->invoice_date_due) < time() ? true : false);
  62.  
  63.         $data = array(
  64.             'invoice' => $invoice,
  65.             'items' => $this->mdl_items->where('invoice_id', $invoice->invoice_id)->get()->result(),
  66.             'invoice_tax_rates' => $this->mdl_invoice_tax_rates->where('invoice_id', $invoice->invoice_id)->get()->result(),
  67.             'invoice_url_key' => $invoice_url_key,
  68.             'flash_message' => $this->session->flashdata('flash_message'),
  69.             'payment_method' => $payment_method,
  70.             'is_overdue' => $is_overdue,
  71.             'attachments' => $attachments,
  72.             'custom_fields' => $custom_fields,
  73.         );
  74.  
  75.         $this->load->view('invoice_templates/public/' . get_setting('public_invoice_template') . '.php', $data);
  76.     }
  77.  
  78.     private function get_attachments($key)
  79.     {
  80.         $path = UPLOADS_FOLDER . '/customer_files';
  81.         $files = scandir($path);
  82.         $attachments = array();
  83.  
  84.         if ($files !== false) {
  85.             foreach ($files as $file) {
  86.                 if ('.' != $file && '..' != $file && strpos($file, $key) !== false) {
  87.                     $obj['name'] = substr($file, strpos($file, '_', 1) + 1);
  88.                     $obj['fullname'] = $file;
  89.                     $obj['size'] = filesize($path . '/' . $file);
  90.                     $obj['fullpath'] = base_url($path . '/' . $file);
  91.                     $attachments[] = $obj;
  92.                 }
  93.             }
  94.         }
  95.  
  96.         return $attachments;
  97.     }
  98.  
  99.     /**
  100.      * @param $invoice_url_key
  101.      * @param bool $stream
  102.      * @param null $invoice_template
  103.      */
  104.     public function generate_invoice_pdf($invoice_url_key, $stream = true, $invoice_template = null)
  105.     {
  106.         $this->load->model('invoices/mdl_invoices');
  107.  
  108.         $invoice = $this->mdl_invoices->guest_visible()->where('invoice_url_key', $invoice_url_key)->get();
  109.  
  110.         if ($invoice->num_rows() == 1) {
  111.             $invoice = $invoice->row();
  112.  
  113.             if (!$invoice_template) {
  114.                 $invoice_template = get_setting('pdf_invoice_template');
  115.             }
  116.  
  117.             $this->load->helper('pdf');
  118.  
  119.             generate_invoice_pdf($invoice->invoice_id, $stream, $invoice_template, 1);
  120.         }
  121.     }
  122.  
  123.     /**
  124.      * @param $invoice_url_key
  125.      * @param bool $stream
  126.      * @param null $invoice_template
  127.      */
  128.     public function generate_sumex_pdf($invoice_url_key, $stream = true, $invoice_template = null)
  129.     {
  130.         $this->load->model('invoices/mdl_invoices');
  131.  
  132.         $invoice = $this->mdl_invoices->guest_visible()->where('invoice_url_key', $invoice_url_key)->get();
  133.  
  134.         if ($invoice->num_rows() == 1) {
  135.             $invoice = $invoice->row();
  136.  
  137.             if ($invoice->sumex_id == NULL) {
  138.                 show_404();
  139.                 return;
  140.             }
  141.  
  142.             if (!$invoice_template) {
  143.                 $invoice_template = get_setting('pdf_invoice_template');
  144.             }
  145.  
  146.             $this->load->helper('pdf');
  147.  
  148.             generate_invoice_sumex($invoice->invoice_id);
  149.         }
  150.     }
  151.  
  152.     /**
  153.      * @param $quote_url_key
  154.      */
  155.     public function quote($quote_url_key = '')
  156.     {
  157.         if (!$quote_url_key) {
  158.             show_404();
  159.         }
  160.  
  161.         $this->load->model('quotes/mdl_quotes');
  162.  
  163.         $quote = $this->mdl_quotes->guest_visible()->where('quote_url_key', $quote_url_key)->get();
  164.  
  165.         if ($quote->num_rows() != 1) {
  166.             show_404();
  167.         }
  168.  
  169.         $this->load->model('quotes/mdl_quote_items');
  170.         $this->load->model('quotes/mdl_quote_tax_rates');
  171.         $this->load->model('custom_fields/mdl_custom_fields');
  172.  
  173.         $quote = $quote->row();
  174.  
  175.         if ($this->session->userdata('user_type') <> 1 and $quote->quote_status_id == 2) {
  176.             $this->mdl_quotes->mark_viewed($quote->quote_id);
  177.         }
  178.        
  179.         // Get all custom fields
  180.         $custom_fields = array(
  181.             'quote' => $this->mdl_custom_fields->get_values_for_fields('mdl_invoice_custom', $quote->quote_id),
  182.             'client' => $this->mdl_custom_fields->get_values_for_fields('mdl_client_custom', $quote->client_id),
  183.             'user' => $this->mdl_custom_fields->get_values_for_fields('mdl_user_custom', $quote->user_id),
  184.         );
  185.  
  186.         // Attachments
  187.         $attachments = $this->get_attachments($quote_url_key);
  188.         /*$path = '/uploads/customer_files';
  189.         $files = scandir(getcwd() . $path);
  190.         $attachments = array();
  191.  
  192.         if ($files !== false) {
  193.             foreach ($files as $file) {
  194.                 if ('.' != $file && '..' != $file && strpos($file, $quote_url_key) !== false) {
  195.                     $obj['name'] = substr($file, strpos($file, '_', 1) + 1);
  196.                     $obj['fullname'] = $file;
  197.                     $obj['size'] = filesize($path . '/' . $file);
  198.                     $obj['fullpath'] = base_url($path . '/' . $file);
  199.                     $attachments[] = $obj;
  200.                 }
  201.             }
  202.         }*/
  203.  
  204.         $is_expired = (strtotime($quote->quote_date_expires) < time() ? true : false);
  205.  
  206.         $data = array(
  207.             'quote' => $quote,
  208.             'items' => $this->mdl_quote_items->where('quote_id', $quote->quote_id)->get()->result(),
  209.             'quote_tax_rates' => $this->mdl_quote_tax_rates->where('quote_id', $quote->quote_id)->get()->result(),
  210.             'quote_url_key' => $quote_url_key,
  211.             'flash_message' => $this->session->flashdata('flash_message'),
  212.             'is_expired' => $is_expired,
  213.             'attachments' => $attachments,
  214.             'custom_fields' => $custom_fields,
  215.         );
  216.  
  217.         $this->load->view('quote_templates/public/' . get_setting('public_quote_template') . '.php', $data);
  218.     }
  219.  
  220.     /**
  221.      * @param $quote_url_key
  222.      * @param bool $stream
  223.      * @param null $quote_template
  224.      */
  225.     public function generate_quote_pdf($quote_url_key, $stream = true, $quote_template = null)
  226.     {
  227.         $this->load->model('quotes/mdl_quotes');
  228.  
  229.         $quote = $this->mdl_quotes->guest_visible()->where('quote_url_key', $quote_url_key)->get();
  230.  
  231.         if ($quote->num_rows() == 1) {
  232.             $quote = $quote->row();
  233.  
  234.             if (!$quote_template) {
  235.                 $quote_template = get_setting('pdf_quote_template');
  236.             }
  237.  
  238.             $this->load->helper('pdf');
  239.  
  240.             generate_quote_pdf($quote->quote_id, $stream, $quote_template);
  241.         }
  242.     }
  243.  
  244.     /**
  245.      * @param $quote_url_key
  246.      */
  247.     public function approve_quote($quote_url_key)
  248.     {
  249.         $this->load->model('quotes/mdl_quotes');
  250.         $this->load->helper('mailer');
  251.  
  252.         $this->mdl_quotes->approve_quote_by_key($quote_url_key);
  253.         email_quote_status($this->mdl_quotes->where('ip_quotes.quote_url_key', $quote_url_key)->get()->row()->quote_id, "approved");
  254.  
  255.         redirect('guest/view/quote/' . $quote_url_key);
  256.     }
  257.  
  258.     /**
  259.      * @param $quote_url_key
  260.      */
  261.     public function reject_quote($quote_url_key)
  262.     {
  263.         $this->load->model('quotes/mdl_quotes');
  264.         $this->load->helper('mailer');
  265.  
  266.         $this->mdl_quotes->reject_quote_by_key($quote_url_key);
  267.         email_quote_status($this->mdl_quotes->where('ip_quotes.quote_url_key', $quote_url_key)->get()->row()->quote_id, "rejected");
  268.  
  269.         redirect('guest/view/quote/' . $quote_url_key);
  270.     }
  271.  
  272. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement