<?xml version="1.0" encoding="utf-8"?>
<modification>
	<name>PDF Invoice with Autosend</name>
	<version>1.3.1</version>
	<code>ocxpiwa131</code>
	<author>oc-extensions.com</author>
	<link>http://www.oc-extensions.com/PDF-Invoice-AutoSend-Opencart-2.x</link>
	
	<file path="admin/controller/sale/order.php">
		<operation error="log">
            <search><![CDATA[$data['button_invoice_print'] = $this->language->get('button_invoice_print');]]></search>
			<add position="after"><![CDATA[$data['button_invoice_print_pdf'] = $this->language->get('button_invoice_print') . ' PDF' ;]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$data['invoice'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'], 'SSL');]]></search>
			<add position="after"><![CDATA[$data['invoice_pdf'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'] . '&pdf=1', 'SSL');]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$data['invoice'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'], 'SSL');]]></search>
			<add position="after"><![CDATA[$data['invoice_pdf'] = $this->url->link('sale/order/invoice', 'token=' . $this->session->data['token'] . '&order_id=' . (int)$this->request->get['order_id'] . '&pdf=1', 'SSL');]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$this->response->setOutput($this->load->view('sale/order_info.tpl', $data));]]></search>
			<add position="before"><![CDATA[
			//PDF INVOICE AutoSend
			
			if ($this->config->get('pdf_invoice_autosend')) {
				$data['pdf_invoice_autosend'] = true;
			} else {
				$data['pdf_invoice_autosend'] = false;
			}
			]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$this->response->setOutput($this->load->view('sale/order_invoice.tpl', $data));]]></search>
			<add position="before"><![CDATA[
			//PDF INVOICE 
			
			$data['margin_top'] = $this->config->get('pdf_invoice_margin_top');
			$data['margin_right'] = $this->config->get('pdf_invoice_margin_right');
			$data['margin_bottom'] = $this->config->get('pdf_invoice_margin_bottom');
			$data['margin_left'] = $this->config->get('pdf_invoice_margin_left');
			$data['is_pdf'] = false;
			]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$this->response->setOutput($this->load->view('sale/order_invoice.tpl', $data));]]></search>
			<add position="replace"><![CDATA[
			if (isset($this->request->get['pdf']) && count($data['orders']) >= 1 ) {
				$data['is_pdf'] = true;
			
				require_once(DIR_SYSTEM . 'library/dompdf/dompdf_config.inc.php');
				$dompdf = new DOMPDF();
				$dompdf->set_paper($this->config->get('pdf_invoice_page_size'), $this->config->get('pdf_invoice_page_orientation'));
				$dompdf->load_html($this->load->view('sale/order_invoice.tpl', $data));
				$dompdf->render();
				
				// PDF file name
				if (count($data['orders']) > 1) {  // multiple orders printed in same file
					$pdf_file_name = date('Ymd_His') .'.pdf';
					
				} else {
					
					$pdf_name_find = array(
						'{invoice_prefix}',
						'{invoice_no}',
						'{order_id}'
					);
					
					$pdf_name_replace = array(
						'invoice_prefix' => $order_info['invoice_prefix'],
						'invoice_no' => $order_info['invoice_no'],
						'order_id' => $order_id,
					);
					
					$pdf_file_name = trim(str_replace($pdf_name_find, $pdf_name_replace, $this->config->get('pdf_invoice_file_name')));
					
					if (!$order_info['invoice_no']) {
						$pdf_file_name = $order_id . '.pdf';
					}
				}
				// PDF file name stop
				
				// check if autosend is enabled (this means if called from ajax)
				if (isset($this->request->get['pdf_autosend'])) {
					$pdf_file_location = $this->config->get('pdf_invoice_save_location') . $pdf_file_name;
					
					$pdf_saved = file_put_contents($pdf_file_location, $dompdf->output());
					
					if ($pdf_saved !== false) {  // PDF was saved on disk
						$this->sendEmailPDFInvoice($order_id, $pdf_file_location);
					}
				
				} else {  // print PDF button is clicked
					
					if ($this->config->get('pdf_invoice_invoice_button') == "browser" ) {
						$dompdf->stream($pdf_file_name, array('Attachment' => 0));
					} else {
						$dompdf->stream($pdf_file_name);
					}
				}				
			}
			
			if (!isset($this->request->get['pdf']) || (isset($this->request->get['pdf']) && $this->config->get('pdf_invoice_invoice_button') != "browser" )) {
				$this->response->setOutput($this->load->view('sale/order_invoice.tpl', $data));
			}
			
			]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[public function invoice() {]]></search>
			<add position="before"><![CDATA[
			//SEND PDF INVOICE to customer (atached in email)
			function sendEmailPDFInvoice($order_id, $pdf_file) {
				$this->load->model('sale/order');
				
				$order_info = $this->model_sale_order->getOrder($order_id);
				
				if ($order_info) {
					$find = array(
						'{firstname}',
						'{lastname}',
						'{order_id}',
						'{invoice_prefix}',
						'{invoice_no}',
						'{date_added}',
						'{store_name}'
					);
					
					$replace = array(
						'firstname'      => $order_info['firstname'],
						'lastname'       => $order_info['lastname'],
						'order_id'       => $order_info['order_id'],
						'invoice_prefix' => $order_info['invoice_prefix'],
						'invoice_no'     => $order_info['invoice_no'],
						'date_added'     => date($this->language->get('date_format_short'), strtotime($order_info['date_added'])),
						'store_name'     => $order_info['store_name']
					);
					
					$pdf_invoice_email_template = $this->config->get('pdf_invoice_email_template');
					$pdf_invoice_email = $pdf_invoice_email_template[$this->config->get('config_language_id')];
					
					$subject = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', str_replace($find, $replace, html_entity_decode($pdf_invoice_email['subject'], ENT_QUOTES, 'UTF-8'))));
					$message = str_replace(array("\r\n", "\r", "\n"), '<br />', preg_replace(array("/\s\s+/", "/\r\r+/", "/\n\n+/"), '<br />', str_replace($find, $replace, html_entity_decode($pdf_invoice_email['message'], ENT_QUOTES, 'UTF-8'))));
					
					
					$mail = new Mail($this->config->get('config_mail')); 			
					$mail->setTo($order_info['email']);
					$mail->setFrom($this->config->get('config_email'));
					$mail->setSender($order_info['store_name']);
					$mail->setSubject($subject);
					$mail->setHtml($message);
					$mail->addAttachment($pdf_file);
					$mail->send();
					
					if ($this->config->get('pdf_invoice_admin_copy')) {
						$mail->setTo($this->config->get('config_email'));
						$mail->send();
					}
				}
			}
			]]></add>
		</operation>
	</file>

	<file path="admin/view/template/sale/order_list.tpl">
		<operation error="log">
            <search><![CDATA[<button type="submit" id="button-invoice" form="form-order" formaction="<?php echo $invoice; ?>" data-toggle="tooltip" title="<?php echo $button_invoice_print; ?>" class="btn btn-info"><i class="fa fa-print"></i></button>]]></search>
			<add position="after"><![CDATA[<button type="submit" id="button-invoice-pdf" form="form-order" formaction="<?php echo $invoice_pdf; ?>" data-toggle="tooltip" title="<?php echo $button_invoice_print_pdf; ?>" class="btn btn-info"><i class="fa fa-file-pdf-o"></i></button>]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$('#button-shipping, #button-invoice').prop('disabled', true);]]></search>
			<add position="replace"><![CDATA[$('#button-shipping, #button-invoice, #button-invoice-pdf').prop('disabled', true);]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[$('#button-invoice').prop('disabled', false);]]></search>
			<add position="replace"><![CDATA[$('#button-invoice, #button-invoice-pdf').prop('disabled', false);]]></add>
		</operation>		
	</file>	
	
	<file path="admin/view/template/sale/order_info.tpl">
		<operation error="log">
            <search><![CDATA[<div class="pull-right"><a href="<?php echo $invoice; ?>" target="_blank" data-toggle="tooltip" title="<?php echo $button_invoice_print; ?>" class="btn btn-info"><i class="fa fa-print"></i></a> <a href="<?php echo $shipping; ?>" target="_blank" data-toggle="tooltip" title="<?php echo $button_shipping_print; ?>" class="btn btn-info"><i class="fa fa-truck"></i></a> <a href="<?php echo $edit; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a> <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>]]></search>
			<add position="replace"><![CDATA[<div class="pull-right"><a href="<?php echo $invoice; ?>" target="_blank" data-toggle="tooltip" title="<?php echo $button_invoice_print; ?>" class="btn btn-info"><i class="fa fa-print"></i></a> <a href="<?php echo $invoice_pdf; ?>" target="_blank" data-toggle="tooltip" title="<?php echo $button_invoice_print_pdf; ?>" class="btn btn-info"><i class="fa fa-file-pdf-o"></i></a> <a href="<?php echo $shipping; ?>" target="_blank" data-toggle="tooltip" title="<?php echo $button_shipping_print; ?>" class="btn btn-info"><i class="fa fa-truck"></i></a> <a href="<?php echo $edit; ?>" data-toggle="tooltip" title="<?php echo $button_edit; ?>" class="btn btn-primary"><i class="fa fa-pencil"></i></a> <a href="<?php echo $cancel; ?>" data-toggle="tooltip" title="<?php echo $button_cancel; ?>" class="btn btn-default"><i class="fa fa-reply"></i></a></div>]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[if (json['invoice_no']) {]]></search>
			<add position="after"><![CDATA[
			<?php if ($pdf_invoice_autosend) { ?>
			PDFInvoiceAutoSend();
			<?php } ?>
			]]></add>
		</operation>

		<operation error="log">
            <search><![CDATA[<script type="text/javascript"><!--]]></search>
			<add position="after"><![CDATA[
			function PDFInvoiceAutoSend() {
				$.ajax({
					url: 'index.php?route=sale/order/invoice&token=<?php echo $token; ?>&order_id=<?php echo $order_id; ?>&pdf=1&pdf_autosend=1',
					success: function(data) {
					
					}
				});
			}
			]]></add>
		</operation>
	</file>

	<file path="admin/view/template/sale/order_invoice.tpl">
		<operation error="log">
            <search><![CDATA[<base href="<?php echo $base; ?>" />]]></search>
			<add position="after"><![CDATA[<?php if (!$is_pdf) { ?>]]></add>
		</operation>
		
		<operation error="log">
            <search><![CDATA[</head>]]></search>
			<add position="before"><![CDATA[<?php } else { ?>
			<link rel="stylesheet" type="text/css" href="view/stylesheet/pdf_invoice.css" />
			<style type="text/css">
			<?php if ($margin_top && $margin_bottom && $margin_right && $margin_left) { ?>
			@page { 
				margin-top: <?php echo $margin_top; ?>cm; 
				margin-right: <?php echo $margin_right; ?>cm; 
				margin-bottom: <?php echo $margin_bottom; ?>cm; 
				margin-left: <?php echo $margin_left; ?>cm; 
			}
			<?php } ?>

			.printable:last-of-type {
				page-break-after: avoid !important;
			}
			</style>
			<?php } ?>
			]]></add>
		</operation>

		<operation error="log">
            <search><![CDATA[<div style="page-break-after: always;">]]></search>
			<add position="replace"><![CDATA[<div class="printable" style="page-break-after: always;">]]></add>
		</operation>		
	</file>		
	
</modification>