<?php

if(!defined('BASEPATH'))
    exit('No direct script access allowed');
    
class Cart extends CI_Model{

    public function __construct()
    {
        parent::__construct();
    }
    public function insertCart($cartData)
    {
        $this->db->insert('tbl_cart',$cartData);
    }
    public function checkCartProduct($plnid, $userid, $duration)
    {
        $this->db->select('*');
        $this->db->from('tbl_cart');
        $this->db->where('product_id',$plnid);
        $this->db->where('user_id',$userid);
        $this->db->where('duration',$duration);
        $cartChecked = $this->db->get()->result_array();
        return $cartChecked;
    }
    public function checkCart($plnid, $ipAddr, $duration)
    {
        $this->db->select('*');
        $this->db->from('tbl_cart');
        $this->db->where('product_id',$plnid);
        $this->db->where('ip_address',$ipAddr);
        $this->db->where('duration',$duration);
        $cartChecked = $this->db->get()->result_array();
        return $cartChecked;
    }
    public function checkCartById($userid)
    {
        $this->db->select('*');
        $this->db->from('tbl_cart');
        $this->db->where('user_id',$userid);
        $cartChecked=$this->db->get()->result_array();
        return $cartChecked;
    }
    public function showCart($ipAddr)
    {
        $this->db->select('*');
        $this->db->from('tbl_cart');
        $this->db->where('ip_address',$ipAddr);
        $cartItems=$this->db->get()->result_array();
        return $cartItems;
    }
    public function showCartById($userid)
    {
        $this->db->select('*');
        $this->db->from('tbl_cart');
        $this->db->where('user_id',$userid);
        $cartItems=$this->db->get()->result_array();
        return $cartItems;
    }
    public function getProductsForCart($implodedValue){
         
        if (!empty($implodedValue)) {
            $data = explode(',', $implodedValue);
        }
        $this->db->select("*");
        $this->db->from('tbl_service_plans');
        $this->db->where_in('id', $data);
        $this->db->join('tbl_cart', 'tbl_cart.product_id = tbl_service_plans.id');
        
              
        $productDetail = $this->db->get()->result_array();
        return $productDetail;
    }
    public function updateCartID($userid, $ipAddr)
    {
        $this->db->set('user_id',$userid);
        $this->db->where('ip_address',$ipAddr);
        $this->db->update('tbl_cart');
    }
    public function updatevalue($pid,$data)
    {
        $this->db->where('product_id',$pid);
        $this->db->update('tbl_cart',$data);
    }
    public function deleteCartItem($cid)
    {
        $this->db->where('cart_id',$cid);
        $this->db->delete('tbl_cart');
    }
    public function productTotalAmount($ipAddr)
    {
        $this->db->select_sum('subtotal');
        $this->db->where('ip_address',$ipAddr);
        $this->db->from('tbl_cart');
        $query=$this->db->get()->result();
        return $query;
    }
    public function productTotalAmountId($userid)
    {
        $this->db->select_sum('subtotal');
        $this->db->where('user_id',$userid);
        $this->db->from('tbl_cart');
        $query=$this->db->get()->result();
        return $query;
    }
    public function insertOrder($data)
    {
        $this->db->insert('tbl_order',$data);
        $orderID=$this->db->insert_id();
        return $orderID;

    }
    public function insertOrderDetails($order)
    {
        $this->db->insert('tbl_orderdetails',$order);
        $orderID=$this->db->insert_id();
        return $orderID;
    }
    public function deleteCartByUserId($userid)
    {
        $this->db->where('user_id',$userid);
        $this->db->delete('tbl_cart');
    }
    public function fetchTaxRate()
    {
        $this->db->select('rate');
        $this->db->where('status','Y');
        $query=$this->db->get('tbl_tax');
        return $query->result();
    }
}