<?php

class Coupon_model extends CI_Model {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }


    function verify($coupon) {
        $this->db->select("id_coupons, coupon_number, coupon_status, coupon_category_id,
        coupon_category, coupon_category_desc, coupon_type, discount_perc, coupon_category_prefix,
        case when coupon_validity >= now() then 'Valid'
        when coupon_validity < now() then 'Expired'
        end as validity", false);   
        $this->db->join("coupon_category", "coupon_category.id_coupon_category = coupons.coupon_category_id");
        $this->db->where('coupon_number', ('upper("'. $coupon .'")'), false);
        $this->db->where('coupon_category.business_id', $this->session->userdata('businessid'));
        $this->db->where('coupon_status', 'open');
        $query = $this->db->get('coupons');

        return $query->row();
    }
    
    function coupon_services($coupon_category_id){
        
        $this->db->select('*');
        $this->db->join('business_services', 'business_services.id_business_services = coupon_services.service_id');
        $this->db->where('coupon_category_id', $coupon_category_id);
        $query = $this->db->get('coupon_services');
        
        return $query->result_array();
        
    }
    
    function get_coupon_categories(){
       
        
        
        $sql = "select id_coupon_category, coupon_category, coupon_category_desc, created_by, created_on, business_id, coupon_type, discount_perc,
                    coupon_category_prefix,
                    sum(a.open) as 'open', sum(used) as  used , sum(cancelled) as  cancelled 
                    from
                    (
                    SELECT id_coupon_category, coupon_category.coupon_category, coupon_category.coupon_category_desc, 
                    coupon_category.created_by, coupon_category.created_on, coupon_category.business_id, coupon_category.coupon_type, 
                    coupon_category.discount_perc,
                    coupon_category_prefix,
                    case when coupon_status='open' then 1 else 0
                    end as 'open',
                    case when coupon_status='used' then 1 else 0
                    end as 'used',
                    case when coupon_status='cancelled' then 1 else 0
                    end as 'cancelled'
                    FROM coupon_category 
                    left join coupons on id_coupon_category = coupon_category_id
                    where coupon_category.business_id = ". $this->session->userdata('businessid') ." 
                    ) as a
                    group by a.id_coupon_category
                    ";
        
        $query=$this->db->query($sql);
        return $query->result_array();
        
    }
    
    function get_coupon_category($categoryid){
        
        $this->db->select('*');
        $this->db->where('business_id', $this->session->userdata('businessid'));
        $this->db->where('id_coupon_category', $categoryid);
        $query = $this->db->get('coupon_category');
        
        return $query->row();
        
    }
    
    function insert_category($data){
        
        $this->db->insert('coupon_category', $data);
        return $this->db->insert_id;
        
    }
    
    function remove_coupon_services($categoryid){
        
        $this->db->where('coupon_category_id', $categoryid);
        $this->db->delete('coupon_services');
        
    }
    
    function insert_coupon_services($data){
        
        
        $this->db->insert('coupon_services', $data);
        return $this->db->insert_id();
        
       
    }
    
    function get_coupon_count($categoryid=null){
        
        $sql = "select a.coupon_category_id,
            sum(a.open) as 'open', sum(used) as  used , sum(cancelled) as  cancelled 
            from
            (
            SELECT coupon_category_id,
            case when coupon_status='open' then 1 else 0
            end as 'open',
            case when coupon_status='used' then 1 else 0
            end as 'used',
            case when coupon_status='cancelled' then 1 else 0
            end as 'cancelled'
            FROM coupons ";
        if($categoryid !==null){
            $sql .= " where coupon_category_id= ".$categoryid ." ";
        }
        $sql .= " ) as a group by a.coupon_category_id";
        
        $query = $this->db->query($sql);
        
        return $query->result_array();
        
    }
    
    function get_coupon_listing($categoryid){
        
        $sql = "select ifnull(min(coupon_number),0) as 'min', 
            ifnull(max(coupon_number),0) as 'max',
            ifnull(LPAD(convert(substring(max(coupon_number) , position('-' in max(coupon_number) )+1, length(max(coupon_number) )), UNSIGNED)+1, 5, 0), '00001')
            as 'starting',
            ifnull(count(coupon_number),0) as 'total'
            from coupons
            where coupon_category_id= ". $categoryid. " 
            and coupon_status != 'cancelled'";
        
        $query = $this->db->query($sql);
        
        return $query->row();
        
    }
    
    function generate_sequence($data){
        
        $this->db->insert('coupons', $data);
        return $this->db->insert_id();
        
    }
    
}
