<?php
class ModelExtensionShippingCanadapost extends Model {
  public function createTable() {
    $this->db->query("CREATE TABLE IF NOT EXISTS `" . DB_PREFIX . "candapost_shipment` (
        `order_id` INT(11) NOT NULL,
        `product_id` INT(11) NOT NULL,
        `shipping_label` TEXT NOT NULL,
        `tracking` VARCHAR(30) NOT NULL
        )");
    }

    public function deleteTable() {
        $this->db->query("DROP TABLE IF EXISTS " . DB_PREFIX . "candapost_shipment");
    }

    public function getOrderedProduct($order_id = 0, $product_id = 0) {
		$query = $this->db->query("SELECT o.*, p.weight_class_id, p.weight, p.length, p.width, p.height, o.name FROM " . DB_PREFIX . "order_product o LEFT JOIN " . DB_PREFIX . "product p ON(p.product_id = o.product_id) WHERE o.order_id = '" . (int)$order_id . "' AND o.product_id = '" . (int)$product_id . "' ");

		return $query->row;
    }

    public function addShipment($data = array()) {
        $this->db->query("INSERT INTO " . DB_PREFIX . "candapost_shipment SET order_id = '" . (int)$data['order_id'] . "', product_id = '" . (int)$data['product_id'] . "', shipping_label = '" . $this->db->escape($data['shipping_label']) . "', tracking = '" . $data['tracking_no'] . "'");
    }

    public function getShipment($order_id = 0, $product_id) {
        $result =   $this->db->query("SELECT * FROM " . DB_PREFIX . "candapost_shipment WHERE product_id = '" . (int)$product_id . "' AND order_id = '" . (int)$order_id . "'")->row;
        return $result ;
    }
 }
