<?php
/**
 * CustMesInvoice
 *
 * @author    IXYCOM <contact@ixycom.com>
 * @copyright 2012-2015 IXYCOM
 * @license   You are just allowed to modify this copy for your own use. You must not redistribute it. License is permitted for one Prestashop instance only but you can install it on your test instances.
 */

if (!defined('_PS_VERSION_'))
	exit;

class CustMesInvoice extends Module
{
	public function __construct()
	{
		$this->name = 'custmesinvoice';
		$this->tab = 'shipping_logistics';
		$this->version = '1.2.0';
		$this->bootstrap = true;
		$this->display = 'view';
		$this->author = 'Ixycom';
		$this->need_instance = 0;
		$this->module_key = '9c23b88d623b1c74c24ad20e50915d59';

		parent::__construct();

		$this->displayName = $this->l('Customer Messages on Invoice');
		$this->description = $this->l('Add Customer Messages on Invoice');
	}

	public function install()
	{
		if (!parent::install() || !$this->registerHook('PDFInvoice') || !$this->registerHook('backOfficeHeader'))
			return false;

		return true;
	}

	public function uninstall()
	{
		if (!parent::uninstall())
			return false;

		return true;
	}

	public function hookbackOfficeHeader()
	{
		if (Tools::getValue('module_name') == 'custmesinvoice')
		{
			$this->context->controller->addJquery();
			$this->context->controller->addJS($this->_path.'views/js/admin.js');
		}
	}

	public function hookDisplayPDFInvoice($params)
	{
		$my_invoice_object = $params['object'];

		$sql = 'SELECT `message`, cm.date_add FROM  '._DB_PREFIX_.'customer_message cm LEFT JOIN '
			._DB_PREFIX_.'customer_thread ct ON (ct.id_customer_thread = cm.id_customer_thread) WHERE ct.id_order = '.$my_invoice_object->id_order;

		$messages = Db::getInstance()->ExecuteS($sql);

		$html = '';

		if (is_array($messages) && count($messages) > 0)
		{
			if (count($messages) == 1)
				$html = '<p style="text-align:center">'.$this->l('Message client').'</p><br />';
			else
				$html = '<p style="text-align:center">'.$this->l('Messages clients').'</p><br />';

			$html .= '<table width="100%" cellspacing="10">';

			foreach ($messages as $row)
				$html .= '<tr><td width="30%" align="right">'.Tools::iconv('utf-8', 'iso-8859-1',
						date('d/m/Y - H:i', strtotime($row['date_add']))).' : </td><td align="left" width="70%">'
					.nl2br($row['message']).'</td></tr>';

			$html .= '</table>';
		}

		return $html;
	}

	public function getContent()
	{
		$output = $this->postProcess();
		$output .= $this->getRating();

		return $output;
	}

	public function postProcess()
	{
		if (Tools::getIsset('stop_rating'))
		{
			Configuration::updateValue('CUSTMESINVOICE_STOPRATING', 1);
			die;
		}
	}

	public function getRating()
	{
		$ouput = '';
		$stop_rating = (int)Configuration::get('CUSTMESINVOICE_STOPRATING');
		if ($stop_rating != 1)
		{
			$ouput .= '
			<div id="stop_rating" class="row text-center">
				<div style="margin-top: 20px; margin-bottom: 20px; padding: 0 .7em; text-align: center;">
					<p class="invite">'
				.$this->l('You are satisfied with our module and want to encourage us to add new features ?')
				.'<br/><a href="http://addons.prestashop.com/ratings.php" target="_blank"><strong>'
				.$this->l('Please rate it on Prestashop Addons, and give us 5 stars !')
				.'</strong></a>
					</p>
					<p class="stop" style="display: block;"><a style="cursor: pointer">'
				.'['
				.$this->l('No thanks, I don\'t want to help you. Close this dialog.')
				.']
					 </a></p>
				</div>
			</div>';
		}

		return $ouput;
	}
}

?>