<?php
final class DModelShippingShippingLabel extends Model {

  public function addShippingLabel($data=array()){
    $date = $this->db->dataAtual();
    $sql = "INSERT INTO ".DB_PREFIX."order_shipping_label SET 
            order_id = ".$this->db->escape($data['order_id']).", 
            shipping_code = '".$this->db->escape($data['shipping_code'])."', 
            shipping_label = '".$this->db->escape($data['shipping_label'])."', 
            complete = ".$this->db->escape($data['complete']).",
            date_att = '".$this->db->escape($date)."'";
    $this->db->query($sql);
  }

  public function getShippingLabels($order_id){

    $sql = "SELECT * FROM ".DB_PREFIX."order_shipping_label sp WHERE sp.order_id = ".$order_id;

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

    if($results->num_rows > 0){
      $result = $results->rows;
      return $result;
    }else{
      return false;
    }
  }

  public function getOrdersShippingLabelInfo($order_id = array()){
    if (empty($order_id)){
      return false;
    }

    $sql = "SELECT sl.order_id, o.order_status_id, max(sl.complete) as complete
            FROM ".DB_PREFIX."order_shipping_label sl
            LEFT JOIN oc_order o ON o.order_id = sl.order_id 
            WHERE sl.order_id in (".implode(',', $order_id).")
            GROUP BY sl.order_id";

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

    if($results->num_rows > 0){
      $result = $results->rows;
      return $result;
    }else{
      return false;
    }
  }

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

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

    $sql = "SELECT sl.* FROM ".DB_PREFIX."order_shipping_label sl
            LEFT JOIN ".DB_PREFIX."order o on sl.order_id = o.order_id 
            WHERE sl.date_att is not null";

    if(isset($data['order_id']) && ($data['order_id'])){
      $sql .= ' AND sl.order_id = '.$this->db->escape($data['order_id']);
    }

    if(isset($data['date_added_betwenn'])){
      $sql .= ' AND '.$this->db->escape($data['date_added_betwenn']);
    }

    if(isset($data['label_complete'])){
      $sql .= ' AND sl.complete = '.$this->db->escape($data['label_complete']);
    }

    if(isset($data['shipping_code'])){
      $sql .= " AND sl.shipping_code = '".$this->db->escape($data['shipping_code']) . "'";
    }

    $sql .= ' GROUP BY sl.shipping_code, sl.shipping_label ';

    $sort_data = array(
      'sl.date_att'
    );

    if (isset($data['sort']) && in_array($data['sort'], $sort_data)) {
       $sql .= ftr_geral::sqlOrderByAdd('', $data['sort']);
    }


		if (isset($data['order']) && ($data['order'] == 'DESC')) {
			$sql .= " DESC";
		} else {
			$sql .= " ASC";
    }

		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'];
		}

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

    if($results->num_rows > 0){
      $result = $results->rows;
      return $result;
    }else{
      return false;
    }
  }

  public function setShippingLabelComplete($order_id, $shipping_code, $shipping_label){
    $dateAtt = $this->db->dataAtual();

    //Atualiza Etiqueta pra completa
    $sql = "UPDATE ".DB_PREFIX."order_shipping_label SET 
            complete = ".$this->db->escape(Type_Status::Tp_Habilitado).",
            date_att = '".$this->db->escape($dateAtt)."'
            WHERE order_id = ".$this->db->escape($order_id) . "
            AND shipping_code = '".$this->db->escape($shipping_code)."'
            AND shipping_label = '".$this->db->escape($shipping_label)."'";
    $this->db->query($sql);
  }

}