<?php 
class ModelModuleAbandonedcarts extends Model {

	public function install() {
		$this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "abandonedcarts` (
			`id` INT(11) NOT NULL AUTO_INCREMENT,
			`date_created` DATETIME NULL DEFAULT NULL, 
			`date_modified` DATETIME NULL DEFAULT NULL, 
			`cart` TEXT NULL DEFAULT NULL, 
			`customer_info` TEXT NULL DEFAULT NULL,
			`last_page` VARCHAR(255) NULL DEFAULT NULL,
			`restore_id` VARCHAR(255) NULL DEFAULT NULL,
			`ip` VARCHAR(100) NULL DEFAULT NULL,  
			 PRIMARY KEY (`id`))");
	}
	
	public function uninstall() {
		$this->db->query("DROP TABLE IF EXISTS `" . DB_PREFIX . "abandonedcarts`");
	}
	
	public function viewAbandonedCarts($page=1, $limit=8, $sort="id", $order="DESC") {	
		if ($page) {
				$start = ($page - 1) * $limit;
			}
			$query =  $this->db->query("SELECT * FROM `" . DB_PREFIX . "abandonedcarts`
			ORDER BY `id` DESC
			LIMIT ".$start.", ".$limit);
		
		return $query->rows; 
	}
	
	public function getTotalAbandonedCarts(){
		$query = $this->db->query("SELECT COUNT(*) as `count`  FROM `" . DB_PREFIX . "abandonedcarts`");
		
		return $query->row['count']; 
	}
	
	public function getCartInfo($id) {	
		$query =  $this->db->query("SELECT * FROM `" . DB_PREFIX . "abandonedcarts`
			WHERE `id`=".$id);
		
		return $query->row; 
	}
	
	public function isUniqueCode($randomCode) {
		$query = $this->db->query("SELECT * FROM `" . DB_PREFIX . "coupon` WHERE code='".$this->db->escape($randomCode)."'");
				if($query->num_rows == 0) {
					return true;
							} else {
					return false;
				}	
	}
	
	public function sendMail($data = array()) {
		$mailToUser = new Mail();
		$mailToUser->protocol 	= $this->config->get('config_mail_protocol');
		$mailToUser->parameter   = $this->config->get('config_mail_parameter');
		$mailToUser->hostname 	= $this->config->get('config_smtp_host');
		$mailToUser->username 	= $this->config->get('config_smtp_username');
		$mailToUser->password 	= $this->config->get('config_smtp_password');
		$mailToUser->port 		= $this->config->get('config_smtp_port');
		$mailToUser->timeout 	 = $this->config->get('config_smtp_timeout');
		$mailToUser->setTo($data['email']);
		$mailToUser->setFrom($this->config->get('config_email'));
		$mailToUser->setSender($this->config->get('config_email'));
		$mailToUser->setSubject(html_entity_decode($data['subject'], ENT_QUOTES, 'UTF-8'));
		$mailToUser->setHtml($data['message']);
		$mailToUser->send(); 
		if ($mailToUser) 
		return true;
			else
		return false;
	}
}
?>