<?php
class ModelCatalogSpecialAction extends Model {

	public function getSpecialActions($data = array()){

		$this->load->helper('ftr_geral');
		$this->load->type('tipocomum');

		$data = ftr_geral::ConvertCaracter($data);

		$sql =" SELECT * from " . DB_PREFIX . "order_promotion op "
				 ." INNER join " . DB_PREFIX . "order_discount od ON (op.order_promotion_id = od.order_promotion_id) "
				 ." WHERE op.status = ".Type_TypeIntregationServer::Tp_ServerHabilitado." and (od.date_start = '0000-00-00' OR od.date_start <= CURDATE()) and (od.date_end = '0000-00-00' OR od.date_end >= CURDATE())";

		if (!empty($data['filter_special'])) {
			$sql .= " AND op.description LIKE '" . $this->db->escape($data['filter_special']) . "%'";
		}

		if (!empty($data['special_orders_id'])) {
			$sql .= " AND op.order_promotion_id IN (". $this->db->escape($data['special_orders_id']) .")";
		}

		$sql.= " GROUP BY op.order_promotion_id" ;

		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, true, Type_TimeCache::Tp_Medio);

		return $query->rows;

	}

	public function getSpecialAction($specialActionId){

		$this->load->helper('ftr_geral');
		$this->load->type('tipocomum');

		$sql = "SELECT * from " . DB_PREFIX . "order_promotion op WHERE op.order_promotion_id = ".$specialActionId;

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

		return $query->row;

	}


}
