<?php if (!defined('BASEPATH')) exit('No direct script access allowed');



class Cart_Model extends CI_Model 

{
	public function addProductInCart($post=array())

	{

		$cart = $cartdata = $names = array();

		$qty='';

		$found = $inc =  false;

		$productdata = $post['productid'];

		$p = $this->getAllProducts(array('P.productid'=>$productdata));

		$product = $p[0];

		if($post['type']=='' || $post['type']=='undefined'){
			$pstock = $product['product_stock'];
		}else{
			$punit = $this->common->getsData('product_unit',array('productid'=>$product['productid'],'type'=>$post['type']));
			$pstock = $punit['product_stock'];
		}

		if(isset($post['qty']))

		{

			$qty = $post['qty'];

		}

		$msg = "";

		$cart = $this->cart->contents();

		//print_r($cart);die;

		if($cart)

		{

			foreach($cart as $value):

					if(($productdata == $value['id']) && ($post['size'] == $value['options']['size']) && ($post['color'] == $value['options']['color']))

					{

						$incr = $value['qty']+(($qty!='')?$qty:1);

						if($incr>$value['options']['product_stock'])

						{

							$msg = "Sorry! Product is OUT OF STOCK...";

							$inc = true;

						}else{

							$this->cart->update(array(

				               'rowid' => $value['rowid'],

				               'qty'   => $incr,

				            ));

							$found = true;

						}

					}

			endforeach;

		}

		//print_r($this->cart->contents());die;

		if($found == false && $inc == false)

		{

			if($qty>$pstock)

			{

				$msg = "Sorry! Product is OUT OF STOCK...";

				//echo $msg;

				$inc=true;

			}else{

				$cartdata = $this->createCart($product,$qty,$post);

				$this->cart->insert($cartdata);

			}

		}

		if($inc == false)

		{

			$productAmountCount = productAmountCount();

			$cartDropdown = cartDropdown($this->cart->contents());

			$cart = array('productAmountCount'=>$productAmountCount,'cartDropdown'=>$cartDropdown,'found'=>0,"count"=>count($this->cart->contents()));

		}else{

			$cart = array('msg'=>$msg,'found'=>1);

		}

		//$this->countCouponCategory();

		return $cart;

		/*print_r($productAmountCount);

		print_r($cartDropdown);*/

	}

	public function getAllProducts($where=array(),$limit='')

	{

		$prod = array();

		$products = $this->allProducts($where,$limit);

		return $products;

	}

	public function allProducts($where=array(),$limit=1)

	{

		$this->db->select("P.*,PM.product_image");

		$this->db->from('products as P');

		$this->db->join('product_media as PM','P.productid=PM.product_id','left');

		if(!empty($where))

		{

			$this->db->where($where);

		}

		$this->db->where('PM.isThumb',1);

		// $this->db->group_by('PM.product_id');

		if($limit==1)

		{

			$this->db->limit(10);

		}

		$res = $this->db->get()->result_array();

		return $res;

	}



	public function createCart($product=array(),$qty1='',$post=array())

	{
		$productcart = array();


		if($post['type']=='' || $post['type']=='undefined'){
			$price = ($product['product_sale_price'])?$product['product_sale_price']:$product['product_price'];
			$pstock = $product['product_stock'];
			$rprice = $product['product_price'];
			$sprice = $product['product_sale_price'];

			$image = $product['product_image'];
			$cartid = $product['productid'];
		}else{
			$punit = $this->common->getsData('product_unit',array('productid'=>$product['productid'],'type'=>$post['type']));
			$price = ($punit['product_sale_price'])?$punit['product_sale_price']:$punit['product_price'];
			$pstock = $punit['product_stock'];
			$rprice = $punit['product_price'];
			$sprice = $punit['product_sale_price'];

			if($punit['unit_image']!=''){
				$image = $punit['unit_image'];
			}else{
				$image = $product['product_image'];
			}

			$cartid = $product['productid'].$punit['id'];
		}
		// echo "<pre>";
		// print_r ($price);
		// echo "</pre>";
		$new_price = $price;

		// echo "<pre>";
		// print_r ($new_price);
		// echo "</pre>";



		$productcart = array(

               'id'      => $cartid,

               'qty'     => ($qty1!='')?$qty1:1,

               'price'   => $new_price,

               'name'    => author,//htmlentities($product['product_name'],ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8" ),
               //'coupon_amount'  => 0,
               'options' => array(

               					'productid' => $product['productid'],

               					'product_name' => htmlentities($product['product_name'],ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8" ),

               					'orgional_price' => $price,
               					'product_price' => $rprice,

               					'product_sale_price' => $sprice,

               					'product_stock' => $pstock,
               					
               					'product_weight' => $product['product_weight'],

               					'product_photos' => $image,

               					'size' => $post['size'],

               					'color' => $post['color'],
               					
               					'product_type' => $post['type'],
               					'shop_rewards' => $product['shop_rewards'],

               					'category' => ((isset($product['product_category']) && $product['product_category']!='')?$product['product_category']:0)

               				)

            );

			// echo "<pre>";
			// print_r($productcart); die;

		return $productcart;

	}

	public function productAmountCount()

	{

		//$c = $this->countCouponCategory();

		$cart = $this->cart->contents();

	    $count = $amt = 0;

	    $amount = $shipping = $discount = $st = $gst = array();

	    foreach ($cart as $key => $value) {
	    	if($value['options']['shop_rewards']==0){
				$price = 0;

	            $price = (($value['price']*$value["qty"])); 

	            $amount[] = $price;

	            //$GST[] = (($price*$value['options']['product_gst'])/100);

	            $shipping[] = ($value["price"]*$value["qty"]);

	            //array_push($discount, array("d_".$key=>number_format($value['coupon_amount'],2)));

	            array_push($st, array("st_".$key=>$price));

	            //array_push($gst, array("gst_".$key=>number_format((($price*$value['options']['product_gst'])/100),2)));

				$count++;
			}

	    }

	    $cart_amount = array_sum($amount);

        //$gst_amount = array_sum($GST);

        //$shipping_count = array_sum($shipping);

		/*if($c==0){

			$this->session->unset_userdata("coupon");

		}*/

	    return array("st"=>$cart_amount,"cst"=>$st);

	}

	public function updateCartProductQty($post=array())

	{

		$count = 0;

		$cart = $this->cart->contents();

		foreach ($cart as $key => $value) {

			foreach ($_POST['rowid'] as $key1 => $value1) {

				if($value['rowid']==$value1 && $value['options']['shop_rewards']==0)

				{
					$qty = $post['qty'][$key1];

					$price = $value['options']['orgional_price'];
					// echo $price;
					$new_price = $price;
					// echo "<br>";
					// echo $new_price;
					// echo "<br>";
					// echo $qty; die;


					if($value['qty']!=$qty)

					{

						$incr = $qty;

						if($incr>$value['options']['product_stock'])

						{

							//echo $incr." > ".$value['options']['product_stock'];

							$this->session->set_flashdata('emessage','Product out of stock');

							$msg = "Sorry! Product is OUT OF STOCK...";

							$inc = true;

							//exit(0);

						}else{

							$this->cart->update(array(

				               'rowid' => $value['rowid'],

				               'qty'   => $incr,
				               'price'   => $new_price,

				            ));

							$found = true;

						}

					}

				}

			}

		}

		//$this->countCouponCategory();

		return true;

	}

	public function deleteProductFromCart($post=array())

	{

		$cart1 = array();

		$product = $this->getAllProducts(array('P.productid'=>$post['productid']));

		$cart = $this->cart->contents();

		if($cart) {

			foreach($cart as $value):

				if($post['productid'] == $value['rowid']) {

					$this->cart->update(array(

		               'rowid' => $post['productid'],

		               'qty'   => 0,

		            ));

				}

			endforeach;

		}

		$cart1 = $this->cart->contents();

		if(empty($cart1)) {
			$this->session->unset_userdata("coupon");
		}
		$c = $this->productAmountCount();

		$cartDropdown = cartDropdown($this->cart->contents());

		$cart1 = array_merge(array('cartDropdown'=>$cartDropdown),$c);//'productAmountCount'=>$productAmountCount,

		return $cart1;

	}

	public function getAllProductsEp($where=array(),$limit='')

	{

		$prod = array();

		$products = $this->allProductsEp($where,$limit);

		return $products;

	}

	public function allProductsEp($where=array(),$limit=1)

	{

		$this->db->select("P.*");

		$this->db->from('eproduct as P');

		if(!empty($where))

		{

			$this->db->where($where);

		}

		if($limit==1)

		{

			$this->db->limit(10);

		}

		$res = $this->db->get()->result_array();

		return $res;

	}


	public function addProductInWishlist($post=array())

	{
		$cart = $cartdata = $names = array();

		$qty = $post['qty'];

		$found = $inc =  false;

		$productdata = $post['productid'];

		$p = $this->getAllProductsEp(array('P.id'=>$productdata));

		$product = $p[0];

		$msg = "";

		$cart = $this->wishlist->contents();

		//print_r($cart);die;
		if($cart)

		{

			foreach($cart as $value):
				if($productdata == $value['id'])
				{
					$incr = $value['qty']+(($qty!='')?$qty:1);
					$this->wishlist->update(array(

		               'rowid' => $value['rowid'],

		               'qty'   => $incr,

		            ));
					$found = true;
				}
			endforeach;
		}

		//print_r($this->cart->contents());die;

		if($found == false)
		{
			$cartdata = $this->createWishlist($product,$qty);
			$this->wishlist->insert($cartdata);
		}

		if($inc == false)
		{
			$cart = array('found'=>0,"count"=>count($this->wishlist->contents()));
		}else{
			$cart = array('msg'=>$msg,'found'=>1);
		}

		return $cart;

	}

	public function createWishlist($product=array(),$qty1='')
	{
		$productcart = array();

		$image = $product['image'];

		$price = 1;

		$productcart = array(

               'id'      => $product['id'],

               'qty'     => ($qty1!='')?$qty1:1,

               'price'   => $price,

               'name'    => author,//htmlentities($product['product_name'],ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8" ),
               //'coupon_amount'  => 0,
               'options' => array(

               					'productid' => $product['id'],

               					'product_name' => htmlentities($product['title'],ENT_QUOTES | ENT_SUBSTITUTE, "UTF-8" ),

               					'product_price' => 1,

               					'product_sale_price' => 1,

               					'product_stock' => 500,

               					'product_photos' => $image,

               					'category' => $product['category']

               				)

            );

			// echo "<pre>";
			// print_r($productcart); die;

		return $productcart;

	}

	public function deleteProductFromWishlist($post=array())

	{

		$cart1 = array();

		$product = $this->getAllProductsEp(array('P.id'=>$post['productid']));

		$cart = $this->wishlist->contents();

		if($cart) {

			foreach($cart as $value):

				if($post['productid'] == $value['rowid']) {

					$this->wishlist->update(array(

		               'rowid' => $post['productid'],

		               'qty'   => 0,

		            ));

				}

			endforeach;

		}

		$cart1 = $this->wishlist->contents();

		return $cart1;

	}


}