<?php
class ModelCartRecover extends Model {
	
	public function getProductsCartInfo($data = array()) {
				$sql = "SELECT distinct(pc.data_add) as data_add, COUNT(pc.futura_produto_id) as products, "
							."c.firstname, c.lastname, SUM(p.price*pc.quantity) as total, p.tax_class_id FROM " . DB_PREFIX . "product_cart pc "
							."LEFT JOIN " . DB_PREFIX . "customer c ON (pc.futura_cadastro_id = c.id_futura) "
							."LEFT JOIN " . DB_PREFIX . "product p ON (pc.futura_produto_id = p.id_futura)"
							."WHERE pc.status = 1";
				
				if(!empty($data['customer_id'])){
					$sql.=" AND pc.futura_cadastro_id = ".(int)$data['customer_id']."";
				}
				
				if(!empty($data['data_add'])){
					$sql.=" AND pc.data_add = '".$data['data_add']."'";
				}
				
				$sql.=" group by pc.data_add order by pc.data_add DESC ";
				
				if (isset($data['start']) || isset($data['limit'])) {
					if ($data['start'] < 0) {
						$data['start'] = 0;
					}		
		          		

					if ($data['limit'] < 1) {
						$data['limit'] = 20;
					}	
				
					$sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit'];
				}	
				
				$query = $this->db->query($sql);

				return $query->rows;								
	}
		
	public function getTotalProductsCart($data = array()) {
		
			$sql = "SELECT COUNT(distinct(pc.data_add)) as total FROM " . DB_PREFIX . "product_cart pc WHERE pc.status = 1";
			
			if(!empty($data['customer_id'])){
				$sql.=" AND pc.futura_cadastro_id = ".(int)$data['customer_id']."";
			}
						
			$query = $this->db->query($sql);
			
			return $query->row['total'];							
		
	}
	
	public function getProductsCart($data = array()){
		
		$sql = "SELECT p.product_id, pc.quantity FROM " . DB_PREFIX . "product_cart pc INNER JOIN " . DB_PREFIX . "product p ON (pc.futura_produto_id = p.id_futura) WHERE pc.status = 1";
		
		if(!empty($data['customer_id'])){
			$sql.=" AND pc.futura_cadastro_id = ".(int)$data['customer_id']."";
		}
		
		if(!empty($data['data_add'])){
			$sql.=" AND pc.data_add = '".$data['data_add']."'";
		}				

		$query = $this->db->query($sql);

		return $query->rows;
		
	}		
	

}
?>