<?php

class ModelCatalogCoupon extends Model {
    public function getDepartmentCoupom($coupon_id, $product_id){
        $result = $this->db->query("SELECT DISTINCT ndep.category_id dept_id FROM oc_coupon_category AS cc 
        INNER JOIN oc_product p ON (p.product_id = '". $product_id ."')
        INNER JOIN oc_category nsub ON (p.category_id  = nsub.category_id)
        INNER JOIN oc_category ncat ON (nsub.parent_id = ncat.category_id)
        LEFT  JOIN oc_category ndep ON (ncat.parent_id = ndep.category_id)
        WHERE cc.coupon_id = '". $coupon_id ."'")->row;

        if(count($result) > 0){
            return $result['dept_id'];
        }

        return null;
    }

    public function getProductCoupom($coupon_id, $product_id){
        $result = $this->db->query("SELECT cp.product_id FROM oc_coupon_product cp WHERE cp.coupon_id = '". $coupon_id ."' AND cp.product_id = '". $product_id ."'")->row;
        
        if(count($result) > 0){
            return $result['product_id'];
        }

        return null;
    }

    public function getGridCoupom($coupon_id, $product_id){
        $result = $this->db->query("SELECT cpg.product_grid_id FROM oc_coupon_product_grid cpg WHERE cpg.coupon_id = '". $coupon_id ."' AND cpg.product_grid_id = '". $product_id ."'")->row;

        if(count($result) > 0){
            return $result['product_grid_id'];
        }

        return null;
    }
}