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

/**
 * 
 * This model contains all db functions related to Cart Page
 * @author Teamtweaks
 *
 */
class Cart_model extends My_Model
{
	
	public function add_cart($dataArr=''){
			$this->db->insert(PRODUCT,$dataArr);
	}

	public function edit_cart($dataArr='',$condition=''){
			$this->db->where($condition);
			$this->db->update(PRODUCT,$dataArr);
	}
	
	
	public function view_cart($condition=''){
			return $this->db->get_where(PRODUCT,$condition);
			
	}
		
	/******************** Cart Page View Function  ********************/
	public function mani_cart_view($userid=''){
		$countryList = $this->cart_model->get_all_details(COUNTRY_LIST,array());
		//echo "<pre>"; print_r($countryList); die;
		
		$cart_page_url = $this->uri->segment(1);
		
		$MainShipCost = 0;
		$MainTaxCost = 0; $cartQty = 0; $usercartQty = 0;
		
		$paypalDetailsVal = unserialize(PaypalIDDetails);
		$authorizeDetailsVal = unserialize(API_LOGINIDDetails);
		
		$PaypalVal = unserialize($paypalDetailsVal['settings']);
		$AuthorizesVal = unserialize($authorizeDetailsVal['settings']);
		
		//echo '<pre>'; print_r($PaypalVal);
		//echo '<pre>'; print_r($AuthorizesVal);	
		
		$shipVal = $this->cart_model->get_all_details(SHIPPING_ADDRESS,array( 'user_id' => $userid));
		//echo $this->db->last_query();die;
		/******************** If Shipping address is not empty, update the shipping,tax values in cart,usercart,subscribe Function  ********************/		
		
		/*if($shipVal -> num_rows() >0 ){
		
			$shipValID = $this->cart_model->get_all_details(SHIPPING_ADDRESS,array( 'user_id' => $userid, 'primary' => 'Yes'));				
			$ShipCostVal = $this->cart_model->get_all_details(COUNTRY_LIST,array( 'country_code' => $shipValID->row()->country));
			
			$MainShipCost = $ShipCostVal->row()->shipping_cost;
			$MainTaxCost = $ShipCostVal->row()->shipping_tax;
			$dataArr2 = array('shipping_cost' => $MainShipCost, 'tax' => $MainTaxCost);
			$dataArr = array('shipping_id' => $shipValID->row()->id,'shipping_cost' => $MainShipCost, 'tax' => $MainTaxCost);
			$condition = array('user_id' => $userid);
														
			$this->cart_model->update_details(FANCYYBOX_TEMP,$dataArr,$condition);
			$this->cart_model->update_details(SHOPPING_CART,$dataArr2,$condition); 
			$this->cart_model->update_details(USER_SHOPPING_CART,$dataArr2,$condition); 
			
		}*/
		
		
		$GiftValue = ''; $CartValue = ''; $SubscribValue = '';	 $UserCartValue = '';

		$giftSet = $this->cart_model->get_all_details(GIFTCARDS_SETTINGS,array( 'id' => '1'));
		$giftRes = $this->cart_model->get_all_details(GIFTCARDS_TEMP,array( 'user_id' => $userid));

		$SubcribRes = $this->minicart_model->get_all_details(FANCYYBOX_TEMP,array( 'user_id' => $userid));	
		
		$this->db->select('a.*,b.product_name,b.max_quantity as mqty,b.seourl,b.image,b.id as prdid,b.price as orgprice');
		$this->db->from(SHOPPING_CART.' as a');
		$this->db->join(PRODUCT.' as b' , 'b.id = a.product_id');
		//$this->db->join(PRODUCT_ATTRIBUTE.' as c' , 'c.id = a.attribute_values','left');		
		$this->db->where('a.user_id = '.$userid);
		$cartVal = $this->db->get();
		
		
		
		
		$this->db->select('sell_id');
		$this->db->from(USER_SHOPPING_CART);
		$this->db->where('user_id = '.$userid);
		$this->db->group_by("sell_id");
		$UsercartSellVal = $this->db->get();
		
		//echo '<pre>'; print_r($UsercartSellVal); die;
		
		
		/*Start Lnnguage Translation*/
		 if($this->lang->line('cart_orderform') != '') { $cart_orderForm = stripslashes($this->lang->line('cart_orderform')); } else $cart_orderForm = 'Order from'; 
		 if($this->lang->line('giftcard_cards') != '') { $cart_giftCard = stripslashes($this->lang->line('giftcard_cards')); } else $cart_giftCard = 'Gift Cards'; 
		 if($this->lang->line('cart_recipient') != '') { $cart_recip = stripslashes($this->lang->line('cart_recipient')); } else $cart_recip = 'Recipient'; 
		 if($this->lang->line('user_from') != '') { $cart_from = stripslashes($this->lang->line('user_from')); } else $cart_from = 'From'; 
		 if($this->lang->line('cart_message') != '') { $cart_msg = stripslashes($this->lang->line('cart_message')); } else $cart_msg = 'Message'; 
		 if($this->lang->line('cart_editgc') != '') { $cart_editgiftCard = stripslashes($this->lang->line('cart_editgc')); } else $cart_editgiftCard = 'Edit gift card';
		 if($this->lang->line('user_remove') != '') { $cart_remove = stripslashes($this->lang->line('user_remove')); } else $cart_remove = 'Remove'; 
		 if($this->lang->line('cart_delinfo') != '') { $cart_delInfo = stripslashes($this->lang->line('cart_delinfo')); } else $cart_delInfo = 'Delivery Info'; 
		 if($this->lang->line('cart_sendto') != '') { $cart_sendTo = stripslashes($this->lang->line('cart_sendto')); } else $cart_sendTo = 'Send to'; 
		 if($this->lang->line('cart_ofgiftcard') != '') { $cart_ofGiftcard = stripslashes($this->lang->line('cart_ofgiftcard')); } else $cart_ofGiftcard = 'of Gift Card'; 
		 if($this->lang->line('cart_howtopay') != '') { $cart_howyoupay = stripslashes($this->lang->line('cart_howtopay')); } else $cart_howyoupay = 'How You will Pay'; 
		 if($this->lang->line('cart_ordertot') != '') { $cart_orderTotal = stripslashes($this->lang->line('cart_ordertot')); } else $cart_orderTotal = 'Order total'; 
		 if($this->lang->line('cart_proceed') != '') { $cart_prodCheckout = stripslashes($this->lang->line('cart_proceed')); } else $cart_prodCheckout = 'Proceed to checkout'; 
		 if($this->lang->line('cart_anothergc') != '') { $cart_anothgiftCard = stripslashes($this->lang->line('cart_anothergc')); } else $cart_anothgiftCard = 'Add another gift card'; 
		 if($this->lang->line('prod_product') != '') { $cart_prod = stripslashes($this->lang->line('prod_product')); } else $cart_prod = 'Product'; 
		 if($this->lang->line('giftcard_price') != '') { $cart_price = stripslashes($this->lang->line('giftcard_price')); } else $cart_price = 'Price'; 
		 if($this->lang->line('shop_quantity') != '') { $cart_quantity = stripslashes($this->lang->line('shop_quantity')); } else $cart_quantity = 'Quantity'; 
		 if($this->lang->line('cart_total') != '') { $cart_Total = stripslashes($this->lang->line('cart_total')); } else $cart_Total = 'Total'; 
		 if($this->lang->line('cart_shipto') != '') { $cart_shipTo = stripslashes($this->lang->line('cart_shipto')); } else $cart_shipTo = 'Ship to'; 
		 if($this->lang->line('cart_choose') != '') { $cart_chose = stripslashes($this->lang->line('cart_choose')); } else $cart_chose = 'Choose Your Shipping Address'; 
		 if($this->lang->line('cart_deladdr') != '') { $cart_delAddr = stripslashes($this->lang->line('cart_deladdr')); } else $cart_delAddr = 'Delete this address'; 
		 if($this->lang->line('cart_addaddr') != '') { $cart_addAddr = stripslashes($this->lang->line('cart_addaddr')); } else $cart_addAddr = 'Add new shipping address'; 
		 if($this->lang->line('cart_order') != '') { $cart_Order = stripslashes($this->lang->line('cart_order')); } else $cart_Order = 'Order'; 
		 if($this->lang->line('cart_itemtot') != '') { $cart_itemTot = stripslashes($this->lang->line('cart_itemtot')); } else $cart_itemTot = 'Item total'; 
		 if($this->lang->line('shopsec_shipping') != '') { $cart_shiping = stripslashes($this->lang->line('shopsec_shipping')); } else $cart_shiping = 'Shipping'; 
		 if($this->lang->line('cart_tax') != '') { $cart_Tax = stripslashes($this->lang->line('cart_tax')); } else $cart_Tax = 'Tax'; 
		 if($this->lang->line('cart_orderform') != '') { $cart_orderForm = stripslashes($this->lang->line('cart_orderform')); } else $cart_orderForm = 'Order from'; 
		 if($this->lang->line('cart_merchant') != '') { $cart_Merchant = stripslashes($this->lang->line('cart_merchant')); } else $cart_Merchant = 'Merchant'; 
		 if($this->lang->line('cart_retailprice') != '') { $cart_retailPrice = stripslashes($this->lang->line('cart_retailprice')); } else $cart_retailPrice = 'retail price'; 
		 if($this->lang->line('cart_readytoship') != '') { $cart_readyShip = stripslashes($this->lang->line('cart_readytoship')); } else $cart_readyShip = 'Ready to ship'; 
		 if($this->lang->line('cart_makeorder') != '') { $cart_makeOrder = stripslashes($this->lang->line('cart_makeorder')); } else $cart_makeOrder = 'Make to Order'; 
		 if($this->lang->line('cart_shipaddr') != '') { $cart_shipAddr = stripslashes($this->lang->line('cart_shipaddr')); } else $cart_shipaddr = 'Choose Your Shipping Address';
		 if($this->lang->line('cart_couponcode') != '') { $cart_coupCode = stripslashes($this->lang->line('cart_couponcode')); } else $cart_coupCode = 'Coupon Codes'; 
		 if($this->lang->line('cart_hvcoupcode') != '') { $cart_hvCoupcode = stripslashes($this->lang->line('cart_hvcoupcode')); } else $cart_hvCoupcode = 'Have a coupon code'; 
		 if($this->lang->line('cart_apply') != '') { $cart_Apply = stripslashes($this->lang->line('cart_apply')); } else $cart_Apply = 'Apply'; 
		 if($this->lang->line('cart_discount') != '') { $cart_Discount = stripslashes($this->lang->line('cart_discount')); } else $cart_Discount = 'Discount'; 
		 if($this->lang->line('cart_paycont') != '') { $cart_contPay = stripslashes($this->lang->line('cart_paycont')); } else $cart_contPay = 'Continue to Payment'; 
		 if($this->lang->line('cart_conowner') != '') { $cart_conOwner = stripslashes($this->lang->line('cart_conowner')); } else $cart_conOwner = 'Contact shop owner'; 
		 if($this->lang->line('cart_notshipto') != '') { $cart_notShipto = stripslashes($this->lang->line('cart_notshipto')); } else $cart_notShipto = 'This item does not ship to';
		 if($this->lang->line('cart_noteto') != '') { $cart_noteTo = stripslashes($this->lang->line('cart_noteto')); } else $cart_noteTo = 'Note to'; 
		 if($this->lang->line('cart_optional') != '') { $cart_Optional = stripslashes($this->lang->line('cart_optional')); } else $cart_Optional = 'Optional'; 
		 if($this->lang->line('cart_completeorder') != '') { $cart_compOrder = stripslashes($this->lang->line('cart_completeorder')); } else $cart_compOrder = 'You can enter any info needed to complete your order or write a note to the shop'; 
		 if($this->lang->line('cart_applycouponcode') != '') { $cart_appCoupcode = stripslashes($this->lang->line('cart_applycouponcode')); } else $cart_appCoupcode = 'Apply shop coupon code'; 
		 if($this->lang->line('cart_couponcod') != '') { $cart_Coupcode = stripslashes($this->lang->line('cart_couponcod')); } else $cart_Coupcode = 'copuncode'; 
		 if($this->lang->line('shop_itemdetails') != '') { $cart_itemDetails = stripslashes($this->lang->line('shop_itemdetails')); } else $cart_itemDetails = 'Item Details'; 
		 if($this->lang->line('cart_itemscart') != '') { $cart_itemsCart = stripslashes($this->lang->line('cart_itemscart')); } else $cart_itemsCart = 'Items in Your Cart'; 
		 if($this->lang->line('cart_keepshop') != '') { $cart_keepShop = stripslashes($this->lang->line('cart_keepshop')); } else $cart_keepShop = 'Keep Shopping'; 
		 if($this->lang->line('cart_cartempty') != '') { $cart_cartEmpty = stripslashes($this->lang->line('cart_cartempty')); } else $cart_cartEmpty = 'Your Shopping Cart is Empty'; 
		 if($this->lang->line('cart_shopcartempty') != '') { $cart_shopCartEmpty = stripslashes($this->lang->line('cart_shopcartempty')); } else $cart_shopCartEmpty = 'Shopping Cart Empty'; 
		 if($this->lang->line('cart_awesomesales') != '') { $cart_awesomeSales = stripslashes($this->lang->line('cart_awesomesales')); } else $cart_awesomeSales = 'Don`t miss out on awesome sales right here on'; 
		 if($this->lang->line('cart_fillcart') != '') { $cart_fillCart = stripslashes($this->lang->line('cart_fillcart')); } else $cart_fillCart = 'Let`s fill that cart, shall we?'; 
		 if($this->lang->line('cart_urshopcart') != '') { $cart_urShopCart = stripslashes($this->lang->line('cart_urshopcart')); } else $cart_urShopCart = 'Your Shopping Cart'; 
		/*End Lnnguage Translation*/
		
		//$resultCart = $cartVal->result_array();
		/****************************** Gift Card Displays **************************************/
		if($giftRes -> num_rows() > 0 ){ 
			$giftAmt = 0; $g=-1;
			$GiftValue.= '<div id="UserCartTable_'.$selId.'" style="display:block;">
			<form method="post" name="giftSubmit" id="giftSubmit" class="continue_payment" enctype="multipart/form-data" action="checkout/gift">
			 			<div class="cart_items">
                	<h2>
                    	<span class="shop-name">'.$cart_orderForm.'<span class="shop-name1"><a href="gift-cards">'.ucwords($this->config->item('email_title').' '.$cart_giftCard).'</a></span></span>
                        <span class="cart_icons"><a href="javascript:giftcardCartRemove('.$userid.','.$g.')" cid="66577"" class="close-btn"></a>
                        </span>
                    </h2>
					<div class="cart_details">
                    	<div class="product_info">
					';
			    
				foreach ($giftRes->result() as $giftRow){
					
				$curdate = date('Y-m-d');
				if($giftRow->image!=''){
					$newImgpath = GIFTPATH.$giftRow->image;			
				}else{
					$newImgpath = GIFTPATH.'default-gift-card.jpg';
				}
				$send_mail=($giftRow->recipient_mail!='')? $giftRow->recipient_mail : $giftRow->sender_mail;
			
			$GiftValue.='<div id="UsercartdivId_'.$g.'">
		
						<div class="cart_top">
                            	<div class="cart_top_left">
                                	<a href="gift-cards/'.$giftRow->id.'"><img src="'.$newImgpath.'" alt="'.$giftRow->image.'"></a>
									<div class="clear"></div>
                                </div>
								
                                <div style="padding:0 10px"  class="cart_top_right">
								
								<div class="gift-outer-wrap">
                                	<a href="gift-cards/'.$giftRow->id.'" class="cartitem_name gift-sender-title">'.$this->data['currencySymbol'].number_format(($giftRow->price_value * $this->data['currencyValue']),2).' '.$this->data['currencyType'].' '.$cart_ofGiftcard.'</a>
									<div class="cartitem_detail">
										<table width="100%" border="0" cellpadding="6" cellspacing="0" class="table_property_use">
											<tr><td width="50%">'.$cart_recip.'</td><td>'.$cart_from.'</td></tr>
											<tr><td width="50%">'.$giftRow->recipient_name.'</td><td>'.$giftRow->sender_name.'</td></tr>
											<tr><td width="50%">'.$cart_msg.'</td><td>'.$giftRow->description.'</td></tr>
										</table>   
										<ul class="cartitem_links">
										<li><a href="gift-cards/'.$giftRow->id.'">'.$cart_editgiftCard.'</a></li>
										<li><a href="javascript:delete_gift('.$giftRow->id.','.$g.')">'.$cart_remove.'</a></li>
                                    </ul>                                     
										<div class="clear"></div>
									</div>
									
									</div>
									
									<div class="gift-sender">
									<label class="ship_to" style="margin:10px 0 0">'.$cart_delInfo.'</label>
									<label style="margin:10px 0 0">'.$cart_sendTo.'</label><br>
									<label style="margin:10px 0 0">'.$send_mail.'</label>
									</div>
									
							
							 	</div>
							</div>
						</div>
						';
				$giftAmt = $giftAmt + $giftRow->price_value;
				$g++;
			}
			
			$GiftValue.= '</div>
							<div class="order_summary">
								<div class="clear"></div>
								<label class="ship_to" style="margin:10px 0 0">'.$cart_howyoupay.'</label><br>';
								
								if($paypalDetailsVal['status']=='Enable'){
				if($PaypalVal['merchant_email']!=''){
					$GiftValue.='<input type="radio"  name="gift_payment_value" value="Paypal" ><img src="images/facebook22.png" /><br>';	
				}
			}
			if($authorizeDetailsVal['status']=='Enable'){
				if($AuthorizesVal['Login_ID']!='' && $AuthorizesVal['Transaction_Key']!=''){
					$GiftValue.='<input type="radio"  name="gift_payment_value" value="Credit-Card" ><img src="images/social_icon.png" />';
				}
			}
								
							$GiftValue.='<br><span id="GiftCartErr"></span><br><br>
								<table class="summary_detail">
									<tbody>
										<tr>
											<td><strong>'.$cart_orderTotal.'</strong></td>
											<td class="txt_right"><strong>'.$this->data['currencySymbol'].number_format(($giftAmt * $this->data['currencyValue']),2).' '.$this->data['currencyType'].'</strong></td>
										</tr>
									</tbody>
								</table>   
								<input name="gift_cards_amount" id-"gift_cards_amount" value="'.number_format($giftAmt,2,'.','').'" type="hidden">                         
								<input type="button" class="checkout_btn" name="cartPayment" id="button-submit-merchant" value="'.$cart_prodCheckout.'" onclick="javascript:giftcardprocess();" />
								<div class="clear"></div><br>
								<a href="gift-cards">'.$cart_anothgiftCard.'</a>
							</div>
							</div>
							</form>
							</div>';
		}

		
		/****************************** Subscribe Card Displays **************************************/
		if($SubcribRes -> num_rows() > 0 ){ 
			
			$SubscribValue.= '<div id="SubscribeCartTable" style="display:block;">
			<form method="post" name="SubscribeSubmit" id="SubscribeSubmit" class="continue_payment" enctype="multipart/form-data" action="site/cart/Subscribecheckout">
				<div class="cart-payment-wrap cart-note"><span class="cart-payment-top"><b></b></span><div class="table-cart-wrap"><table class="table-cart">
			<thead><tr><th width="51%" colspan="2" class="product">'.$cart_prod.'</th><th width="18%">'.$cart_price.'</th><th width="15%">'.$cart_quantity.'</th><th width="21%">'.$cart_Total.'</th></tr></thead></table>
				';	
			$SubscribAmt = 0; $subcribSAmt = 0; $subcribTAmt = 0; $SubgrantAmt = 0; 
			
			foreach ($SubcribRes->result() as $SubcribRow){
				$SubscribValue.= '<div id="SubscribId_'.$s.'" style="display:block;"><table class="table-cart"><tbody><tr class="first">
					<td rowspan="2" class="thumnail"><img src="'.FANCYBOXPATH.$SubcribRow->image.'" alt="'.$SubcribRow->name.'"><a href="javascript:delete_subscribe('.$SubcribRow->id.','.$s.')" class="remove_gift_card" cid="66577">'.$cart_remove.'</a></td>
					<td class="title"><a href=""><b>'.$SubcribRow->name.'</b></a><br></td>
					<td class="price">'.$this->data['currencySymbol'].number_format($SubcribRow->price,2,'.','').'</td>
					<td class="price">1</td>
					<td class="total">'.$this->data['currencySymbol'].number_format($SubcribRow->price,2,'.','').'</td>
				</tr>
				</tbody></table></div>';
				$SubscribAmt = $SubscribAmt + $SubcribRow->price;
				$s++;
			}
			
				$subcribSAmt = $MainShipCost;
				$subcribTAmt = ($SubscribAmt * 0.01 * $MainTaxCost);
				$SubgrantAmt = $SubscribAmt + $subcribSAmt + $subcribTAmt ;
		

		$SubscribValue.= '</div>
			 <div class="cart-payment" id="merchant-cart-payment">
		    <input type="hidden">
		    <span class="bg-cart-payment"></span>
		    <dl class="cart-payment-ship">
		      <dt>'.$cart_shipTo.'</dt>
		      <dd>
			<select id="address-cart" class="select-round select-shipping-addr" onchange="SubscribeChangeAddress(this.value);">
				  <option value="" id="address-select">'.$cart_chose.'</option>
			';
			
			foreach ($shipVal->result() as $Shiprow){
			if($Shiprow->primary == 'Yes'){ $optionsValues = 'selected="selected"'; 
			$ChooseVal = $Shiprow->full_name.'<br>'.$Shiprow->address1.'<br>'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'<br>'.$Shiprow->country.'<br>'.$Shiprow->phone; $ship_id =$Shiprow->id;  }else{ $optionsValues ='';}
			$SubscribValue.='<option '.$optionsValues.' value="'.$Shiprow->id.'" l1="'.$Shiprow->full_name.'" l2="'.$Shiprow->address1.'" l3="'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'" l4="'.$Shiprow->country.'" l5="'.$Shiprow->phone.'">'.$Shiprow->full_name.'</option>';
			}  
			  

		$SubscribValue.='</select>
			<input type="hidden" name="SubShip_address_val" id="SubShip_address_val" value="'.$ship_id.'" />
			
			<p class="default_addr"><span id="SubChg_Add_Val">'.$ChooseVal.'</span></p>
			<span style="color:#FF0000;" id="Ship_Sub_err"></span>
			<a href="javascript:void(0);" class="delete_addr" onclick="shipping_cart_address_delete();">'.$cart_delAddr.'</a>
			
			<a href="settings/shipping" class="add_addr add_" onclick="shipping_address_cart();">'.$cart_addAddr.'</a>

		      </dd>
			</dl>

			   <dl class="cart-payment-order">
		      <dt>'.$cart_Order.'</dt>
		      <dd>
			<ul>
			  <li class="first">
			    <span class="order-payment-type">'.$cart_itemTot.'</span>
			    <span class="order-payment-usd"><b>'.$this->data['currencySymbol'].'<span id="SubCartAmt">'.number_format($SubscribAmt,2,'.','').'</span></b> '.$this->data['currencyType'].'</span>
			  </li>
			  <li>
			    <span class="order-payment-type">'.$cart_shiping.'</span>
			    <span class="order-payment-usd"><b>'.$this->data['currencySymbol'].'<span id="SubCartSAmt">'.number_format($subcribSAmt,2,'.','').'</span></b> '.$this->data['currencyType'].'</span>
			  </li>
			  <li>
			    <span class="order-payment-type">'.$cart_Tax.' (<span id="SubTamt">'.$MainTaxCost.'</span>%) of '.$this->data['currencySymbol'].$SubscribAmt.'</span>
			    <span class="order-payment-usd"><b>'.$this->data['currencySymbol'].'<span id="SubCartTAmt">'.number_format($subcribTAmt,2,'.','').'</span></b> '.$this->data['currencyType'].'</span>
			  </li></ul>';
			 
			 $SubscribValue.='
			  <ul>
			 <li class="total">
			    <span class="order-payment-type"><b>'.$cart_Total.'</b></span>
			    <span class="order-payment-usd"><b>'.$this->data['currencySymbol'].'<span id="SubCartGAmt">'.number_format($SubgrantAmt,2,'.','').'</span></b> '.$this->data['currencyType'].'</span>
			  </li>
			</ul>
		      </dd>
	              
		    </dl>
			
		    <input name="user_id" value="'.$userid.'" type="hidden">
			<input name="subcrib_amount" id="subcrib_amount" value="'.number_format($SubscribAmt,2,'.','').'" type="hidden">
			<input name="subcrib_ship_amount" id="subcrib_ship_amount" value="'.number_format($subcribSAmt,2,'.','').'" type="hidden">
			<input name="subcrib_tax_amount" id="subcrib_tax_amount" value="'.number_format($subcribTAmt,2,'.','').'" type="hidden">
			<input name="subcrib_total_amount" id="subcrib_total_amount" value="'.number_format($SubgrantAmt,2,'.','').'" type="hidden">
		    <input type="submit" class="btn" name="SubscribePayment" id="button-submit-merchant" value="'.$cart_contPay.'" />
		    
		  </div>
		</div>
	</form></div>';
		}
		
		
		/****************************** Cart Displays **************************************/
		if($cartVal -> num_rows() > 0 ){
			$CartValue.='<div id="CartTable" style="display:block;">
			<h2 class="title_use" style="width:98%;"> '.$cart_orderForm.' <b>'.$this->config->item('email_title').' '.$cart_Merchant.'</b></h2>
			<div class="cart_full"><div class="cart_payment">
			<form method="post" name="cartSubmit" id="cartSubmit" class="continue_payment" enctype="multipart/form-data" action="site/cart/cartcheckout">
       		
			
        <table width="700" border="0" cellpadding="0" cellspacing="0" class="table_property_use">
  <tr>
    <th class="product_head" width="51%">'.$cart_prod.'</th>
    <th class="product_head">'.$cart_price.'</th>
    <th class="product_head">'.$cart_quantity.'</th>
    <th class="product_head">'.$cart_Total.'</th>
  </tr></table>
       ';
		$cartAmt = 0; $cartShippingAmt = 0; $cartTaxAmt = 0; 
		$s=0;
		foreach ($cartVal->result() as $CartRow){
			//echo '<pre>';print_r($CartRow);
			$curdate = date('Y-m-d');
			$newImg = @explode(',',$CartRow->image);
			if($newImg[0]!=''){
				$newImgpath = PRODUCTPATHTHUMB.$newImg[0];			
			}else{
				$newImgpath = PRODUCTPATH.'dummyProductImage.jpg';
			}
			
		$CartValue.='<div id="cartdivId_'.$s.'">
		<table width="700" border="0" cellpadding="0" cellspacing="0" class="table_property_use">
		
		
		<tr>
    <td width="50%">
    <div class="inside_property">
    <div class="img_split">

		<img src="'.$newImgpath.'" alt="'.$CartRow->product_name.'" width="172">
        <div class=" clear"></div>
        <a href="javascript:void(0);" onclick="javascript:delete_cart('.$CartRow->id.','.$s.')" class="remove_item">'.$cart_remove.'</a>
    </div>
    <p><a href="shop/'.$CartRow->user_id.'/'.$CartRow->seourl.'"><b>'.$CartRow->product_name.'</b></a></p>';
			if($CartRow->attribute_values!=''){
			$CartValue.='<p class="new_rateuse">'.str_replace('|-|',', ',$CartRow->attribute_values).'</p>';
			}
    
    $CartValue.='<p class="new_rateuse">'.$this->data['currencySymbol'].$CartRow->orgprice.' '.$cart_retailPrice.'</p> <!--<span class="new_rateuse">13% off </span>-->
  
    <p class="new_rateuse">'.$cart_shiping.': ';
			if($CartRow->ship_immediate == 'true'){
				$CartValue.=$cart_readyShip;
			}else{
				$CartValue.=$cart_makeOrder;
			}	
			
			$CartValue.='</p>
    
    </div>
    </td>
    <td valign="middle"><p class="text_des">'.$this->data['currencySymbol'].$CartRow->price.'</p></td>
    <td valign="middle"><p class="text_des"><select name="quantity'.$s.'" id="quantity'.$s.'" class="accent_drop" style="width:60px;" data-mqty="'.$CartRow->mqty.'" onchange="javascript:update_cart('.$CartRow->id.','.$s.')">';
				for($p=1;$p<=$CartRow->mqty;$p++){
					if($CartRow->quantity == $p){ $SelVal='selected="selected"'; }else{ $SelVal='';}
					$CartValue.='<option '.$SelVal.' value="'.$p.'"  >'.$p.'</option>';
				}
			$CartValue.='</select></p></td>
    <td><p class="text_des">'.$this->data['currencySymbol'].'<span id="IndTotalVal'.$s.'">'.$CartRow->indtotal.'</span></p></td>
  </tr>
</table></div>';
				$cartAmt = $cartAmt + (($CartRow->product_shipping_cost + $CartRow->price + ($CartRow->price * 0.01 * $CartRow->product_tax_cost))  * $CartRow->quantity);
				$cartShippingAmt = $cartShippingAmt + ($CartRow->product_shipping_cost * $CartRow->quantity);
				$cartTaxAmt = $cartTaxAmt + ($CartRow->product_tax_cost * $CartRow->quantity);
				$cartQty = $cartQty + $CartRow->quantity;
				$s++;
			}
			$cartSAmt = $MainShipCost;
			$cartTAmt = ($cartAmt * 0.01 * $MainTaxCost);
			$grantAmt = $cartAmt + $cartSAmt + $cartTAmt ;
			
  $CartValue.='
 </div>
 	
 			<div class="right_cart">
  <table border="0" cellpadding="0" cellspacing="0" class="table_property_use">
  <tr>
    <th class="product_head">&nbsp;</th>
    
  </tr>
  <tr>
    <td width="40%">
    <div class="ship_content">
    <span>'.$cart_shipTo.'</span>
    <div class="clear"></div>
     <select id="address-cart" class="accent_drop" onchange="CartChangeAddress(this.value);">
				  <option value="">'.$cart_shipAddr.'</option>
			';
			
			foreach ($shipVal->result() as $Shiprow){
			if($Shiprow->primary == 'Yes'){ $optionsValues = 'selected="selected"'; $ChooseVal = $Shiprow->full_name.'<br>'.$Shiprow->address1.'<br>'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'<br>'.$Shiprow->country.'<br>'.$Shiprow->phone; $ship_id =$Shiprow->id;  }else{ $optionsValues ='';}
			$CartValue.='<option '.$optionsValues.' value="'.$Shiprow->id.'" l1="'.$Shiprow->full_name.'" l2="'.$Shiprow->address1.'" l3="'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'" l4="'.$Shiprow->country.'" l5="'.$Shiprow->phone.'">'.$Shiprow->full_name.'</option>';
			}  
			  

		$CartValue.='</select>
			<input type="hidden" name="Ship_address_val" id="Ship_address_val" value="'.$ship_id.'" />
			
			<p class="default_addr"><span id="Chg_Add_Val">'.$ChooseVal.'</span></p>
			<span style="color:#FF0000;" id="Ship_err"></span>';
			
			if($this->checkLogin(U)!=''){
			$CartValue.='<a href="javascript:void(0);"  onclick="shipping_cart_address_delete();">'.$cart_delAddr.'</a>
			<a href="site/user_settings/display_shippings/'.$cart_page_url.'" onclick="shipping_address_cart();">'.$cart_addAddr.'</a>';	
			}else{
			$CartValue.='<a href="javascript:void(0);" onclick="shipping_address_login();">'.$cart_addAddr.'</a>';	
			}
			$CartValue.='</div>
 
 <div class="clear"></div>
 <div class="border_use"></div>';

 
  
  
  
  if($disAmt>0){
			$CartValue.='<div class="coupon_code">
 <span>'.$cart_coupCode.'</span>
 <div class="clear"></div>
 
	<input id="is_coupon" name="is_coupon" class="coupon" placeholder="'.$cart_hvCoupcode.'?" readonly="readonly" type="text" value="'.$discountQuery->row()->couponCode.'">
	<input type="button"  id="CheckCodeButton" class="coupon_btn" onclick="javascript:checkRemove();" value="'.$cart_remove.'" style="cursor:pointer;" />

  </div>
			';
		    }else{
			$CartValue.='<div class="coupon_code">
 <span>'.$cart_coupCode.'</span>
 <div class="clear"></div>
 
	<input id="is_coupon" name="is_coupon" class="coupon" placeholder="'.$cart_hvCoupcode.'?" type="text">
	<input type="button"   id="CheckCodeButton" class="coupon_btn" onclick="javascript:checkCode();" value="'.$cart_Apply.'" style="cursor:pointer;" />

  </div>';
		    }

$CartValue.='  
 <div class="clear"></div>
<span id="CouponErr" style="color:#FF0000;"></span>
 <div class="border_use"></div>
 <div class="clear"></div>
 <div class="order_details_con">
 <p style="margin-left:15px;"><b>'.$cart_Order.'</b></p>
 <ul>
 	<li>'.$cart_itemTot.'</li>
	<li><strong>'.$this->data['currencySymbol'].'<span id="CartAmt">'.number_format($cartAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
 
	<li>'.$cart_shiping.'</li>
	<li><strong>'.$this->data['currencySymbol'].'<span id="CartSAmt">'.number_format($cartSAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
			  
	<li>'.$cart_Tax.' (<span id="carTamt">'.$MainTaxCost.'</span>%) of '.$this->data['currencySymbol'].'<span id="CartAmtDup">'.$cartAmt.'</span></li>
	<li><strong>'.$this->data['currencySymbol'].'<span id="CartTAmt">'.number_format($cartTAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
 </ul>';
	if($disAmt >0){
		$grantAmt = $grantAmt - $disAmt;
		$CartValue.='<div class="clear"></div><div id="disAmtValDiv"><ul>
		<li>'.$Discount.'</li>
		<li><strong>'.$this->data['currencySymbol'].'<span id="disAmtVal">'.number_format($disAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
		<ul></div>';
	}else{
		$CartValue.='<div class="clear"></div><div id="disAmtValDiv" style="display:none;"><ul>
		<li>'.$Discount.'</li>
		<li><strong>'.$this->data['currencySymbol'].'<span id="disAmtVal">'.number_format($disAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
		</ul></div>';
	}
	
	$CartValue.='<div class="clear"></div><ul>
		<li><b>'.$cart_Total.'</b></li>
		<li><strong>'.$this->data['currencySymbol'].'<span id="CartGAmt">'.number_format($grantAmt,2,'.','').'</span></strong> '.$this->data['currencyType'].'</li>
		</ul>
		      </div>
<div class="clear"></div>
			
		    <input name="user_id" value="'.$userid.'" type="hidden">
			<input name="cart_amount" id="cart_amount" value="'.number_format($cartAmt,2,'.','').'" type="hidden">
			<input name="cart_ship_amount" id="cart_ship_amount" value="'.number_format($cartSAmt,2,'.','').'" type="hidden">
			<input name="cart_tax_amount" id="cart_tax_amount" value="'.number_format($cartTAmt,2,'.','').'" type="hidden">
			<input name="cart_tax_Value" id="cart_tax_Value" value="'.number_format($MainTaxCost,2,'.','').'" type="hidden">
			<input name="cart_total_amount" id="cart_total_amount" value="'.number_format($grantAmt,2,'.','').'" type="hidden">
			<input name="discount_Amt" id="discount_Amt" value="'.number_format($disAmt,2,'.','').'" type="hidden">';
			if($disAmt>0){
			$CartValue.='<input name="CouponCode" id="CouponCode" value="'.$discountQuery->row()->couponCode.'" type="hidden">
			<input name="Coupon_id" id="Coupon_id" value="'.$discountQuery->row()->couponID.'" type="hidden">
			<input name="couponType" id="couponType" value="'.$discountQuery->row()->coupontype.'" type="hidden">';
			}else{
			$CartValue.='<input name="CouponCode" id="CouponCode" value="" type="hidden">
			<input name="Coupon_id" id="Coupon_id" value="0" type="hidden">
			<input name="couponType" id="couponType" value="" type="hidden">';
			}
		    $CartValue.='
		    <input type="submit" class="payment_btn" name="cartPayment" id="button-submit-merchant" value="'.$cart_contPay.'" />
		   
    </td>
    
  </tr>
</table>
 
 </div>
 			
			</div></form>
			</div>';
		}
		
		/****************************** User Cart Displays **************************************/
		if($UsercartSellVal -> num_rows() > 0 ){
		$g=0; $UsercartQty = 0;	 $s=0;
		foreach ($UsercartSellVal->result() as $UserSellRow){
		
			$this->db->select('a.*,b.product_name,b.seourl,b.image,b.id as prdid,b.sale_price as orgprice,b.quantity as mqty,c.paypal_email,b.seller_product_id,c.user_name,d.seller_businessname,d.seourl as shopurl,d.PayPal_mode,d.PayPal_email,d.authorize_mode,d.authorize_key,d.authorize_id');
			$this->db->from(USER_SHOPPING_CART.' as a');
			$this->db->join(PRODUCT.' as b' , 'b.id = a.product_id');
			$this->db->join(USERS.' as c' , 'c.id = a.sell_id');
			$this->db->join(SELLER.' as d' , 'd.seller_id = c.id');
			$this->db->where('a.user_id = '.$userid.' and c.id='.$UserSellRow->sell_id);		
			$UsercartVal = $this->db->get();
			//echo $this->db->last_query();
			//echo '<pre>'; print_r($UsercartVal->result_array()); 
			$selId = $UserSellRow->sell_id;
			
			
			$favShopArr = $this->product_model->getUserFavoriteShopDetails(stripslashes($selId));
			if(empty($favShopArr)){ 
				$ShopfavVal="changeShopToFavourite('".$selId."','Fresh')";
				$shopFaVclass="fav1-btn";
			} else {
				$ShopfavVal="changeShopToFavourite('".$selId."','Old')";
				$shopFaVclass="fav1-btn2";				
 			} 

			$this->db->select('discountAmount,couponID,couponCode,coupontype');
			$this->db->from(USER_SHOPPING_CART);
			$this->db->where('user_id = '.$userid.' and sell_id = '.$selId);
			$discountQuery = $this->db->get();
			
			$disAmt = $discountQuery->row()->discountAmount;
				
			$UserCartValue.='<div id="UserCartTable_'.$selId.'" style="display:block;">
			 <div class="cart_items">
                	<h2>
                    	<span class="shop-name">Order from<span class="shop-name1"><a href="shop-section/'.$UsercartVal->row()->shopurl.'">'.ucwords($UsercartVal->row()->seller_businessname).'</a></span></span>
                        <span class="cart_icons">';
					if($this->checkLogin('U')!=''){
                   		$UserCartValue.='<a href="javascript:void(0);" class="'.$shopFaVclass.'" onclick="'.$ShopfavVal.'"></a>';
					}
                   $UserCartValue.='<a href="javascript:void(0);" class="close-btn" onclick="sellerCartdelete('.$selId.');"></a>
                        </span>
                    </h2>

				<form method="post" name="cartSubmit" id="cartSubmit" class="continue_payment" enctype="multipart/form-data" action="site/cart/usercartcheckout">
				<div class="cart_details">
                    	<div class="product_info">';
						
		$UsercartAmt = 0; $UsercartShippingAmt = 0; $UsercartTaxAmt = 0;  $UserCartPaymentShow = $UserCartShow = 0;
		
		foreach ($UsercartVal->result() as $UserCartRow){
			
			//echo '<pre>';print_r($UserCartRow);
			$curdate = date('Y-m-d');
			$UsernewImg = @explode(',',$UserCartRow->image);
			if($UsernewImg[0]!=''){
				$newImgpath = PRODUCTPATHTHUMB.$UsernewImg[0];			
			}else{
				$newImgpath = PRODUCTPATH.'dummyProductImage.jpg';
			}
			
			
			
			$favPrdArr = $this->product_model->getUserFavoriteProductDetails($UserCartRow->prdid);
			if(empty($favPrdArr)){ 
				$PrdFavArr = "changeProductToFavourite('".$UserCartRow->prdid."','Fresh')"; 
				$PrdFavTxt="Move to favorites";
			} else {
				$PrdFavArr = "changeProductToFavourite('".$UserCartRow->prdid."','Old')";
				$PrdFavTxt="Remove from favorites";
			 } 
						
		$UserCartValue.='<div id="UsercartdivId_'.$g.'">
		
						<div class="cart_top">
                            	<div class="cart_top_left">
                                	<a href="products/'.$UserCartRow->seourl.'"><img src="'.$newImgpath.'" alt="'.$UserCartRow->product_name.'"></a>
                                </div>
                                <div class="cart_top_right">
                                	<a href="products/'.$UserCartRow->seourl.'" class="cartitem_name">'.$UserCartRow->product_name.'</a>';
									if($UserCartRow->attribute_values!=''){
										$UserCartValue.='<br>'.$UserCartRow->attribute_values.'';
									}
									
                                    $UserCartValue.='<div class="cartitem_detail">
                                    	<label>'.$cart_quantity.' : </label>
										<select name="userquantity'.$g.'" id="userquantity'.$g.'"  tyle="float:left; width:auto; margin:0 0 0 5px;" data-mqty="'.$UserCartRow->mqty.'" onchange="javascript:update_cart_user('.$UserCartRow->id.','.$g.','.$selId.')">';
                                        	for($sp=1;$sp<=$UserCartRow->mqty;$sp++){
												if($UserCartRow->quantity == $sp){ $SelVal='selected="selected"'; }else{ $SelVal='';}
												$UserCartValue.='<option '.$SelVal.' value="'.$sp.'"  >'.$sp.'</option>';
											}
                                       $UserCartValue.='</select>
                                        <span>'.$this->data['currencySymbol'].number_format(($UserCartRow->price * $this->data['currencyValue']),2,'.','').' '.$this->data['currencyType'].' + '.$this->data['currencySymbol'].number_format(($UserCartRow->product_shipping_cost * $this->data['currencyValue']),2,'.','').' '.$this->data['currencyType'].' '.$cart_shiping.'  </span>
										 <span id="UserIndTotalVal'.$g.'_'.$selId.'" style="display:block;">Total : '.$this->data['currencySymbol'].number_format(((($UserCartRow->price * $UserCartRow->quantity)+ $UserCartRow->shipping_cost) * $this->data['currencyValue']),2,'.','').' '.$this->data['currencyType'].'</span>
                                    </div>
                                    <ul class="cartitem_links">';
									if($this->checkLogin('U')!=''){
                                    $UserCartValue.='<li><a href="javascript:void(0);" onclick="'.$PrdFavArr .'">'.$PrdFavTxt.'</a></li>';
									if($selId!=1){
                                    $UserCartValue.='<li><a href="javascript:void(0);" onclick="contactshopowner('.$selId.','.$UserCartRow->prdid.');">'.$cart_conOwner.'</a><a class="reg-popup2 cboxElement"></a></li>';
									}
									}	
                                    $UserCartValue.='<li><a href="javascript:void(0);" onclick="javascript:delete_cart_user('.$UserCartRow->id.','.$g.','.$selId.')">'.$cart_remove.'</a></li>
                                    </ul>
										<div class="clear"></div>';
                           if($this->session->userdata('shopId-'.$selId)!=''){
							   if($UserCartRow->shipping == 'No'){
								$UserCartValue.='<div class="error_message" style="display:block;">'.$cart_notShipto.' '.$this->session->userdata('ShopCountry-'.$selId).'.</div>';
								$UserCartShow = 1;

							   }else{
								   $UserCartValue.='<div class="error_message" style="display:none;">'.$cart_notShipto.' '.$this->session->userdata('ShopCountry-'.$selId).'.</div>';
							   }
							}
                               $UserCartValue.='</div>
							
							 </div>
						</div>
						';
				$UsercartAmt = $UsercartAmt + ($UserCartRow->price * $UserCartRow->quantity);
				$UsercartShippingAmt = $UsercartShippingAmt + $UserCartRow->shipping_cost;
				$UsercartTaxAmt = $UsercartTaxAmt + ($UserCartRow->product_tax_cost * $UserCartRow->quantity);
				$UsercartQty = $UsercartQty + $UserCartRow->quantity;
				$g++;
			}
			$UsercartSAmt = $UsercartShippingAmt;
			$UsercartTAmt = ($UsercartAmt * 0.01 * $MainTaxCost);
			$UsergrantAmt = $UsercartAmt + $UsercartSAmt + $UsercartTAmt ;
		
		
			
		$UserCartValue.='
			<div class="cart_bottom">
               	<label>'.$cart_noteTo.' '.$UsercartVal->row()->seller_businessname.' <small style="margin:0 0 0 5px">'.$cart_Optional.'</small></label>
            	<textarea name="note" data-id="cart-note" placeholder="'.$cart_compOrder.'"></textarea>
         	</div>
		  	</div>
		  
		  <div class="order_summary">
                        	<label class="ship_to">'.$cart_shipTo.'</label>
		  <select id="address-cart" class="ship_to" onchange="UserCartChangeAddress(this.value,'.$selId.');">
				  <option value="" id="address-select">'.$cart_chose.'</option>
			';
			
			foreach ($shipVal->result() as $Shiprow){
			if($Shiprow->id == $this->session->userdata('shopId-'.$selId)){ $optionsValues = 'selected="selected"'; $ChooseVal = $Shiprow->full_name.'<br>'.$Shiprow->address1.'<br>'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'<br>'.$Shiprow->country.'<br>'.$Shiprow->phone; $ship_id =$Shiprow->id;  }else{ $optionsValues ='';}
			$UserCartValue.='<option '.$optionsValues.' value="'.$Shiprow->id.'" l1="'.$Shiprow->full_name.'" l2="'.$Shiprow->address1.'" l3="'.$Shiprow->city.' '.$Shiprow->state.' '.$Shiprow->postal_code.'" l4="'.$Shiprow->country.'" l5="'.$Shiprow->phone.'">'.$Shiprow->full_name.'</option>';
			}  
			  

		$UserCartValue.='</select>
			<input type="hidden" name="Ship_address_val" id="User_Ship_address_val_'.$selId.'" value="'.$ship_id.'" />
			<!--<label class="ship_days">Ready to ship in 1–5 business days.</label>-->
			<p class="default_addr"><span id="Chg_Add_Val_'.$selId.'">'.$ChooseVal.'</span></p>
			
			<span style="color:#FF0000;" id="User_Ship_err_'.$selId.'"></span>
			<!--<a href="javascript:void(0);" class="delete_addr" onclick="shipping_user_cart_address_delete('.$selId.');">Delete this address</a>
			<br>-->
			<a href="settings/cart-shipping-address" class="add_addr add_" onclick="shipping_address_cart();">'.$cart_addAddr.'</a>
			<div class="clear"></div>
			<label class="ship_to" style="margin:10px 0 0">'.$cart_howyoupay.'</label>';
			
			/*if($UsercartVal->row()->PayPal_email != '' ){ //&& $UsercartVal->row()->PayPal_mode == "Live"
				$UserCartValue.='<input type="radio"  name="payment_value" value="Paypal" ><img src="images/facebook22.png" /><br>';
				$UserCartPaymentShow = 1;
			}*/
			
			if($paypalDetailsVal['status']=='Enable'){
				if($PaypalVal['merchant_email']!=''){
					$UserCartValue.='<input type="radio"  name="payment_value" value="Paypal" ><img src="images/facebook22.png" /><br>';	
					$UserCartPaymentShow = 1;
				}
			}
			if($authorizeDetailsVal['status']=='Enable'){
				if($AuthorizesVal['Login_ID']!='' && $AuthorizesVal['Transaction_Key']!=''){
					$UserCartValue.='<input type="radio"  name="payment_value" value="Credit-Card" ><img src="images/social_icon.png" />';
					$UserCartPaymentShow = 1;
				}
			}
			 
			$UserCartValue.='<a href="javascript:void(0);" class="coupon" id="shopcoupon'.$selId.'" onclick="apply_coupon_code('.$selId.');">'.$cart_appCoupcode.'</a><div class="clear"></div>';
							
			if($disAmt>0){
			
				$UserCartValue.='<div class="copun_apply" id="Coupon_apply_'.$selId.'" style="display:block;">
							<span id="CouponErr_'.$selId.'" style="color:#FF0000;"></span>
			<label>'.$copuncode.'</label>
	<input id="is_coupon_'.$selId.'" name="is_coupon" class="coupon-search" placeholder="'.$cart_hvCoupcode.'?" readonly="readonly" type="text" value="'.$discountQuery->row()->couponCode.'">
	<input type="button"  id="CheckCodeButton" class="keep_btn" onclick="javascript:checkRemove('.$selId.');" value="'.$cart_remove.'" style="cursor:pointer;" />
	</div>
			';
		    }else{
			$UserCartValue.='<div class="copun_apply" id="Coupon_apply_'.$selId.'" style="display:none;">
							<span id="CouponErr_'.$selId.'" style="color:#FF0000;"></span>
							<label>'.$cart_coupCode.'</label>
			<input id="is_coupon_'.$selId.'" name="is_coupon" class="coupon" placeholder="'.$cart_hvCoupcode.'?" type="text">
			<input type="button" id="CheckCodeButton" class="keep_btn" onclick="javascript:checkCode('.$selId.');" value="'.$cart_Apply.'" style="cursor:pointer;" />
			</div>';
		    }
							
							
/*							<label>copuncode</label>
							<input class="coupon-search" type="text" placeholder="enter the code" name="search_items">
							<a class="keep_btn" href="javacript:void(0);">Apply</a>
*/							
							$UserCartValue.='
                            <table class="summary_detail">
                            	<tbody>
                                	<tr>
                                    	<td>'.$cart_itemDetails.'</td>
                                        <td class="txt_right"><b>'.$this->data['currencySymbol'].'<span id="UserCartAmt_'.$selId.'">'.number_format($UsercartAmt * $this->data['currencyValue'],2,'.','').'</span></b> '.$this->data['currencyType'].'</td>
                                    </tr>
                                    <tr>
                                    	<td>'.$cart_shiping.'</td>
                                        <td class="txt_right">'.$this->data['currencySymbol'].'<span id="UserCartSAmt_'.$selId.'">'.number_format($UsercartSAmt * $this->data['currencyValue'],2,'.','').'</span></b> '.$this->data['currencyType'].'</td>
                                    </tr>
                                    <tr>
                                    	<td>'.$cart_Tax.' (<span id="UsercarTamt_'.$selId.'">'.$MainTaxCost.'</span>%) of '.$this->data['currencySymbol'].'<span id="UserCartAmtDup_'.$selId.'">'.$UsercartAmt * $this->data['currencyValue'].'</span></td>
                                        <td class="txt_right">'.$this->data['currencySymbol'].'<span id="UserCartTAmt_'.$selId.'">'.number_format($UsercartTAmt * $this->data['currencyValue'],2,'.','').'</span></b> '.$this->data['currencyType'].'</td>
                                    </tr>';
									
									if($disAmt >0){
										
		$UsergrantAmt = $UsergrantAmt - $disAmt;
			$UserCartValue.='<tr>
                               	<td>'.$cart_Discount.'</td>
                                <td class="txt_right">'.$this->data['currencySymbol'].'<span id="disAmtVal'.$selId.'">'.number_format($disAmt * $this->data['currencyValue'],2,'.','').'</span></b> '.$this->data['currencyType'].'</td>
                                    </tr>';
									}
	
									
                             $UserCartValue.='<tr style="border-top:2px solid #EAEAE3">
                                    	<td><strong>'.$cart_orderTotal.'</strong></td>
                                        <td class="txt_right"><strong>'.$this->data['currencySymbol'].'<span id="UserCartGAmt_'.$selId.'">'.number_format($UsergrantAmt * $this->data['currencyValue'],2,'.','').'</span></b> '.$this->data['currencyType'].'</strong></td>
                                    </tr>
                                </tbody>
                            </table>
                            
                        
		  
		  
		    <input name="user_id" value="'.$userid.'" type="hidden">
			 <input name="sell_id" value="'.$selId.'" type="hidden">
			<input name="user_cart_amount" id="user_cart_amount_'.$selId.'" value="'.number_format($UsercartAmt,2,'.','').'" type="hidden">
			<input name="user_cart_ship_amount" id="user_cart_ship_amount_'.$selId.'" value="'.number_format($UsercartSAmt,2,'.','').'" type="hidden">
			<input name="user_cart_tax_amount" id="user_cart_tax_amount_'.$selId.'" value="'.number_format($UsercartTAmt,2,'.','').'" type="hidden">
			<input name="user_cart_tax_Value" id="user_cart_tax_Value_'.$selId.'" value="'.number_format($MainTaxCost,2,'.','').'" type="hidden">
			<input name="user_cart_total_amount" id="user_cart_total_amount_'.$selId.'" value="'.number_format($UsergrantAmt,2,'.','').'" type="hidden">
			<input name="user_discount_Amt" id="user_discount_Amt_'.$selId.'" value="'.number_format($disAmt,2,'.','').'" type="hidden">';
			
			if($disAmt>0){
			$UserCartValue.='<input name="CouponCode" id="CouponCode" value="'.$discountQuery->row()->couponCode.'" type="hidden">
			<input name="Coupon_id" id="Coupon_id" value="'.$discountQuery->row()->couponID.'" type="hidden">
			<input name="couponType" id="couponType" value="'.$discountQuery->row()->coupontype.'" type="hidden">';
			}else{
			$UserCartValue.='<input name="CouponCode" id="CouponCode" value="" type="hidden">
			<input name="Coupon_id" id="Coupon_id" value="0" type="hidden">
			<input name="couponType" id="couponType" value="" type="hidden">';
			}
		    
			
			if($UserCartShow == 0 && $UserCartPaymentShow == 1){
					$UserCartValue.='<input type="submit" class="checkout_btn" name="cartPayment" id="button-submit-merchant" value="'.$cart_prodCheckout.'"/>';
			}else{
				$UserCartValue.='<input type="submit" class="" style="padding:6px; width:214px" name="cartPayment" id="button-submit-merchant" value="'.$cart_prodCheckout.'" disabled="disabled"/>';
			}
			
		    $UserCartValue.='</div>
		  </div>
		</div>
	</form></div>';
		
				
				
		}
		}
		
		$countVal = $giftRes -> num_rows() + $cartQty + $UsercartQty + $SubcribRes -> num_rows();
		$cartListItems=$this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		
		//$countVal = $cartListItems->num_rows();
		//
		if ($this->agent->is_referral()){
	       $urlRefe = $this->agent->referrer();
   		}
		if($countVal > 0 ){
			$CartDisp = '
			
			<div class="cart_header">
               	<h1><span id="Shop_id_count">'.$countVal.'</span> '.$cart_itemsCart.' </h1>
            	<a href="'.$urlRefe.'" class="keep_btn">'.$cart_keepShop.'</a>
				
				
   
				
         	</div>
			
			'.$GiftValue.$SubscribValue.$CartValue.$UserCartValue.'
			
			 <div class="cart_items" id="EmptyCart" style="display:none;">
                	<h2>
                    	<span class="shop-name"><span class="shop-name1">'.$cart_cartEmpty.'</span></span>
                        <span class="cart_icons">
                            <!--<a href="#" class="close-btn"></a>-->
                        </span>
                    </h2>
					 <div class="cart_details">
					 <div  class="empty-alert" >
					<p style="text-align:center;"><img src="images/site/shopping_empty.jpg" alt="'.$cart_shopCartEmpty.'"></p>
					<p style="text-align:center;"><b></b></p>
					<p style="text-align:center;">'.$cart_awesomeSales.' '.ucwords($this->config->item('email_title')).'. '.$cart_fillCart.'</p>
				</div>
					 </div>
			</div>		
			
			';
		}else{
			$CartDisp = '
			<div class="cart_header">
               	<h1> '.$cart_urShopCart.' </h1>
            	<a href="'.$urlRefe.'" class="keep_btn">'.$cart_keepShop.'</a>
         	</div>
			<div class="cart_items" id="EmptyCart" style="display:block;">
                	<h2>
                    	<span class="shop-name"><span class="shop-name1">'.$cart_cartEmpty.'</span></span>
                        <span class="cart_icons">
                            <!--<a href="#" class="close-btn"></a>-->
                        </span>
                    </h2>
					 <div class="cart_details">
					 <div  class="empty-alert" >
					<p style="text-align:center;"><img src="images/site/shopping_empty.jpg" alt="'.$cart_shopCartEmpty.'"></p>
					<p style="text-align:center;"><b></b></p>
					<p style="text-align:center;">'.$cart_awesomeSales.' '.ucwords($this->config->item('email_title')).'. '.$cart_fillCart.'</p>
				</div>
					 </div>
			</div>';
				
		}
		
		return $CartDisp;

	}
	
	
	/******************** Main Gift Count Function  ********************/
	public function mani_gift_total($userid=''){
	
		$cartMiniMainCount = 0; $UsercartMiniMainCount = 0;
		$SubcribRes = $this->cart_model->get_all_details(FANCYYBOX_TEMP,array( 'user_id' => $userid));
		$cartVal = $this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		$UsercartValTotl = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid));
		$giftRes = $this->cart_model->get_all_details(GIFTCARDS_TEMP,array( 'user_id' => $userid));
		
		$giftAmt = 0;
		if($giftRes -> num_rows() > 0 ){ 
			
			foreach ($giftRes->result() as $giftRow){
				$giftAmt = $giftAmt + $giftRow->price_value;
			}

		}
		if($cartVal -> num_rows() > 0 ){ 
			foreach ($cartVal->result() as $CartRow){
				$cartMiniMainCount = $cartMiniMainCount + $CartRow->quantity;
			}
		}
		
		if($UsercartValTotl -> num_rows() > 0 ){ 
			foreach ($UsercartValTotl->result() as $UserValCartRow){
				$UsercartMiniMainCount = $UsercartMiniMainCount + $UserValCartRow->quantity;
			}
		}
		
		$countVal = $giftRes -> num_rows() + $SubcribRes -> num_rows() + $cartMiniMainCount + $UsercartMiniMainCount ;
		
		return number_format($giftAmt,2,'.','').'|'.$countVal;

	}
	
	/******************** Main Subscribe Count Function  ********************/
	public function mani_subcribe_total($userid=''){
		
		$giftRes = $this->cart_model->get_all_details(GIFTCARDS_TEMP,array( 'user_id' => $userid));
		$cartVal = $this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		$SubcribRes = $this->cart_model->get_all_details(FANCYYBOX_TEMP,array( 'user_id' => $userid));
		$UsercartValTotl = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid));
		$SubcribAmt = 0; $SubcribSAmt = 0; $SubcribTAmt = 0; $SubcribTotalAmt = 0; $cartMiniMainCount = 0; $UsercartMiniMainCount = 0;
		
		if($SubcribRes -> num_rows() > 0 ){ 
			
			foreach ($SubcribRes->result() as $SubscribRow){
				$SubcribAmt = $SubcribAmt + $SubscribRow->price;
			}
			$SubcribSAmt = $SubcribRes->row()->shipping_cost;
			$SubcribTAmt = $SubcribAmt * 0.01 * $SubcribRes->row()->tax;
			$SubcribTotalAmt = $SubcribAmt + $SubcribSAmt + $SubcribTAmt ;
			
		}
		
		if($cartVal -> num_rows() > 0 ){ 
			foreach ($cartVal->result() as $CartRow){
				$cartMiniMainCount = $cartMiniMainCount + $CartRow->quantity;
			}
		}
		
		if($UsercartValTotl -> num_rows() > 0 ){ 
			foreach ($UsercartValTotl->result() as $UserValCartRow){
				$UsercartMiniMainCount = $UsercartMiniMainCount + $UserValCartRow->quantity;
			}
		}
		
		$countVal = $SubcribRes -> num_rows() + $giftRes -> num_rows() + $cartMiniMainCount + $UsercartMiniMainCount ;
		
		return number_format($SubcribAmt,2,'.','').'|'.number_format($SubcribSAmt,2,'.','').'|'.number_format($SubcribTAmt,2,'.','').'|'.number_format($SubcribTotalAmt,2,'.','').'|'.$countVal;

	}
	
	/******************** Main cart Count Function  ********************/
	public function mani_cart_total($userid=''){
		
		$giftRes = $this->cart_model->get_all_details(GIFTCARDS_TEMP,array( 'user_id' => $userid));
		$cartVal = $this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		$SubcribRes = $this->cart_model->get_all_details(FANCYYBOX_TEMP,array( 'user_id' => $userid));
		$UsercartValTotl = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid));
		$cartAmt = 0; $cartShippingAmt = 0; $cartTaxAmt = 0; $cartMiniMainCount = 0; $UsercartMiniMainCount = 0;

		if($cartVal -> num_rows() > 0 ){ 
			foreach ($cartVal->result() as $CartRow){
				$cartAmt = $cartAmt + (($CartRow->product_shipping_cost +  ($CartRow->product_tax_cost * 0.01 * $CartRow->price ) + $CartRow->price)  * $CartRow->quantity);
				
				$cartMiniMainCount = $cartMiniMainCount + $CartRow->quantity;
				
			}
			$cartSAmt = $cartVal->row()->shipping_cost;
			$cartTAmt = $cartAmt * 0.01 * $cartVal->row()->tax;
			$grantAmt = $cartAmt + $cartSAmt + $cartTAmt ;
			
		}
		
		if($UsercartValTotl -> num_rows() > 0 ){ 
			foreach ($UsercartValTotl->result() as $UserValCartRow){
				$UsercartMiniMainCount = $UsercartMiniMainCount + $UserValCartRow->quantity;
			}
		}
		
		$countVal = $giftRes -> num_rows() + $SubcribRes -> num_rows() + $cartMiniMainCount + $UsercartMiniMainCount;
		
		$this->db->select('discountAmount');
		$this->db->from(SHOPPING_CART);
		$this->db->where('user_id = '.$userid);
		$query = $this->db->get();
		
		if($query->row()->discountAmount !=''){
			$grantAmt = $grantAmt - $query->row()->discountAmount;
		}
		
		return number_format($cartAmt,2,'.','').'|'.number_format($cartSAmt,2,'.','').'|'.number_format($cartTAmt,2,'.','').'|'.number_format($grantAmt,2,'.','').'|'.$countVal.'|'.number_format($query->row()->discountAmount,2,'.','');

	}
	
	/******************** Main User Cart Count Function  ********************/
	public function mani_user_cart_total($userid='',$sellerId=''){
		
		$giftRes = $this->cart_model->get_all_details(GIFTCARDS_TEMP,array( 'user_id' => $userid));
		$cartVal = $this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		$SubcribRes = $this->cart_model->get_all_details(FANCYYBOX_TEMP,array( 'user_id' => $userid));
		$UsercartValTotl = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid));
		
		$UsercartVal = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid,'sell_id'=>$sellerId));
		
		$cartMiniMainCount = 0;
		$UsercartAmt = 0; $UsercartShippingAmt = 0; $UsercartTaxAmt = 0; $UsercartMiniMainCount = 0;

		if($cartVal -> num_rows() > 0 ){ 
			foreach ($cartVal->result() as $CartRow){
				$cartMiniMainCount = $cartMiniMainCount + $CartRow->quantity;
			}
		}
		
		if($UsercartValTotl -> num_rows() > 0 ){ 
			foreach ($UsercartValTotl->result() as $UserValCartRow){
				$UsercartMiniMainCount = $UsercartMiniMainCount + $UserValCartRow->quantity;
			}
		}
		
		if($UsercartVal -> num_rows() > 0 ){ 
			foreach ($UsercartVal->result() as $UserCartRow){
				$UsercartAmt = $UsercartAmt + ( $UserCartRow->price * $UserCartRow->quantity);
			}
			$UsercartSAmt = $UsercartVal->row()->shipping_cost;
			$UsercartTAmt = $UsercartAmt * 0.01 * $UsercartVal->row()->tax;
			$UsergrantAmt = $UsercartAmt + $UsercartSAmt + $UsercartTAmt ;
			
		}
		
		
		
		$countVal = $giftRes -> num_rows() + $SubcribRes -> num_rows() + $cartMiniMainCount + $UsercartMiniMainCount;
		
		
		
		return number_format($UsercartAmt,2,'.','').'|'.number_format($UsercartSAmt,2,'.','').'|'.number_format($UsercartTAmt,2,'.','').'|'.number_format($UsergrantAmt,2,'.','').'|'.$countVal;

	}
	
	/******************** Main cart Coupon Function  ********************/
	public function mani_cart_coupon_sucess($userid=''){
		
		$cartVal = $this->cart_model->get_all_details(SHOPPING_CART,array( 'user_id' => $userid));
		$cartAmt = 0; $cartShippingAmt = 0; $cartTaxAmt = 0;

		if($cartVal -> num_rows() > 0 ){ 
			$k=0;
			foreach ($cartVal->result() as $CartRow){
				$cartAmt = $cartAmt + (($CartRow->product_shipping_cost +  ($CartRow->product_tax_cost * 0.01 * $CartRow->price ) + $CartRow->price)  * $CartRow->quantity);
				$newCartInd[] = $CartRow->indtotal;
				$k = $k + 1;
			}
			$cartSAmt = $cartVal->row()->shipping_cost;
			$cartTAmt = $cartAmt * 0.01 * $cartVal->row()->tax;
			$grantAmt = $cartAmt + $cartSAmt + $cartTAmt ;
			
		}
		
		$this->db->select('discountAmount');
		$this->db->from(SHOPPING_CART);
		$this->db->where('user_id = '.$userid);
		$query = $this->db->get();
		$newAmtsValues = @implode('|',$newCartInd);
		
		if($query->row()->discountAmount !=''){
			$grantAmt = $grantAmt - $query->row()->discountAmount;
		}
		
		return number_format($cartAmt,2,'.','').'|'.number_format($cartSAmt,2,'.','').'|'.number_format($cartTAmt,2,'.','').'|'.number_format($grantAmt,2,'.','').'|'.number_format($query->row()->discountAmount,2,'.','').'|'.$k.'|'.$newAmtsValues;

	}
	
	public function view_cart_details($condition = ''){
		$select_qry = "select p.*,u.full_name,u.user_name,u.thumbnail from ".PRODUCT." p LEFT JOIN ".USERS." u on u.id=p.user_id ".$condition;
		$cartList = $this->ExecuteQuery($select_qry);
		return $cartList;
			
	}
	
	public function view_atrribute_details(){
		$select_qry = "select * from ".ATTRIBUTE." where status='Active'";
		return $attList = $this->ExecuteQuery($select_qry);
	
	}
	
	/******************** Coupon code cart Function  ********************/
	public function Check_Code_Val($Code = '',$amount = '',$shipamount = '', $sellerId = '', $userid = ''){
	
		
		$code = $Code;
		$amount = $amount;
		$amountOrg = $amount;
		$ship_amount = $shipamount;
		$sellerId = $sellerId;

		$CoupRes = $this->cart_model->get_all_details(COUPONCARDS,array( 'code' => $code, 'card_status' => 'not used', 'sell_id' => $sellerId,'status'=>'Active'));
		$ShopArr = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid, 'sell_id' => $sellerId));
		$excludeArr = array('code','amount','shipamount');

		//echo '<pre>'; print_r($CoupRes->result_array()); die;
		if($CoupRes->num_rows() > 0){
			
			//$PayVal = $this->cart_model->get_all_details(PAYMENT,array( 'user_id' => $userid, 'coupon_id' => $CoupRes->row()->id));
			
			//if($PayVal->num_rows() == 0){
			
			
				if($ShopArr->row()->couponID == 0){

				if($CoupRes->row()->quantity > $CoupRes->row()->purchase_count){

					$today = getdate();
					$tomorrow = mktime(0,0,0,date("m"),date("d"),date("Y"));
					$currDate = date("Y-d-m", $tomorrow);
					$couponExpDate = $CoupRes->row()->dateto.' 23:59:59'; 

					$curVal = (strtotime($couponExpDate) < time());
					if($curVal != '') {
						echo '5';
						exit();
					} 

						
					

						if($CoupRes->row()->price_type == 2){
								
							$percentage = $CoupRes->row()->price_value;
							$discount = ($percentage * 0.01) * $amount; 
							$totalAmt = number_format($amount-$discount,2,'.',''); 
									
							$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Cart',
											'is_coupon_used' => 'Yes',
											'total' => $totalAmt);
									$condition =array('user_id' => $userid, 'sell_id' => $sellerId);

									$this->cart_model->update_details(USER_SHOPPING_CART,$dataArr,$condition); 
									
									/*foreach($ShopArr->result() as $shopRow){
							
										$amountOrg = $shopRow->indtotal;									
										$discount = ($percentage * 0.01) * $amountOrg; 
										$IndAmt = number_format($amountOrg - $discount,2,'.',''); 
	
										$dataArr = array('indtotal' => $IndAmt);
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									}*/
									
									echo 'Success|'.$CoupRes->row()->id;
									exit();
								
								}elseif($CoupRes->row()->price_type == 1){
								
									$discount = $CoupRes->row()->price_value;
									if($amount > $discount){									
										$amountOrg = number_format($amount-$discount,2,'.','');
										$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Cart',
											'is_coupon_used' => 'Yes',
											'total' => $amountOrg);
										$condition =array('user_id' => $userid);
										$this->cart_model->update_details(USER_SHOPPING_CART,$dataArr,$condition); 
										/*$newDisAmt = ($discount / $ShopArr->num_rows());
									foreach($ShopArr->result() as $shopRow){
										$amountOrg = $shopRow->indtotal;									
										$IndAmt = number_format($amountOrg - $newDisAmt,2,'.',''); 
										$dataArr = array('indtotal' => $IndAmt);
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									}
*/
										
										echo 'Success|'.$CoupRes->row()->id;
										exit();

									}else{
										echo '7';
										exit();
									}								
								}
								
					
					
				} else {
					echo '6';
					exit();
				}
				}else{
					echo '2';
					exit();
				}
	
			
			
			/*}else{
				echo '2';
				exit();
			}*/
		
		
		
		}else{
			echo '1';
			exit();
		}

	}
	
	/******************** Coupon Code remove funciton   ********************/
	public function Check_Code_Val_Remove($userid = '',$sellerId = ''){
	
		$excludeArr = array('code');
	
						$dataArr = array('discountAmount' => 0, 
											'couponID' => 0,
											'couponCode' => '',
											'coupontype' => '',
											'is_coupon_used' => 'No');
						$condition =array('user_id' => $userid,'sell_id'=>$sellerId);
						$this->cart_model->update_details(USER_SHOPPING_CART,$dataArr,$condition); 
						return;

		

	}
	
	
	/******************** Coupon code cart Function  ********************/
	public function Check_Code_Val_giftcard($Code = '',$amount = '',$shipamount = '', $sellerId = '', $userid = ''){
	
		$code = $Code;
		$amount = $amount;
		$amountOrg = $amount;
		$ship_amount = $shipamount;
		$sellerId = $sellerId;

		$CoupRes = $this->cart_model->get_all_details(COUPONCARDS,array( 'code' => $code, 'card_status' => 'not used', 'status' => 'Active'));
		$ShopArr = $this->cart_model->get_all_details(USER_SHOPPING_CART,array( 'user_id' => $userid, 'sell_id' => $sellerId));
		$excludeArr = array('code','amount','shipamount');

		
		if($CoupRes->num_rows() > 0){

			$PayVal = $this->cart_model->get_all_details(PAYMENT,array( 'user_id' => $userid, 'coupon_id' => $CoupRes->row()->id));
			
			if($PayVal->num_rows() == 0){
			
				if($ShopArr->row()->couponID == 0){

				if($CoupRes->row()->quantity > $CoupRes->row()->purchase_count){

					$today = getdate();
					$tomorrow = mktime(0,0,0,date("m"),date("d"),date("Y"));
					$currDate = date("Y-d-m", $tomorrow);
					$couponExpDate = $CoupRes->row()->dateto; 

					$curVal = (strtotime($couponExpDate.' 23:59:59') < time());
					if($curVal != '') {
						echo '5';
						exit();
					} 

						
					if($CoupRes->row()->coupon_type == "shipping") {
						$totalamt = number_format($amount - $ship_amount,2,'.','');
						$discount ='0';
						
						$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Free Shipping',
											'is_coupon_used' => 'Yes',
											'shipping_cost' => 0,
											'total' => $totalamt);
						$condition =array('user_id' => $userid);
						$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
						echo 'Success|'.$CoupRes->row()->id.'|Shipping';
						exit();

			
					} elseif($CoupRes->row()->coupon_type == "category") {
							$newAmt = $amount;
						$catAry = @explode(',',$CoupRes->row()->category_id);
						foreach($ShopArr->result() as $shopRow){
							$shopCatArr = '';

							$shopCatArr = @explode(',',$shopRow->cate_id);
							
							$combArr = array_merge($catAry, $shopCatArr);
							$combArr1 = array_unique($combArr);
							if(count($combArr) != count($combArr1)){
							
								if($CoupRes->row()->price_type == 2){
									$percentage = $CoupRes->row()->price_value;
									$amountOrg = $shopRow->indtotal;									
									$discount = ($percentage * 0.01) * $amountOrg; 
									$IndAmt = number_format($amountOrg-$discount,2,'.','');
									$TotalAmt = $newAmt = number_format($newAmt - $discount,2,'.','');
									$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Category',
											'is_coupon_used' => 'Yes',
											'indtotal' => $IndAmt);
									$condition =array('id' => $shopRow->id);
									$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									
									$dataArr1 = array('total' => $TotalAmt);
									$condition1 =array('user_id' => $userid);
									$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr1,$condition1); 

									
								}elseif($CoupRes->row()->price_type == 1){
								
									$discount = $CoupRes->row()->price_value;
									$amountOrg = $shopRow->indtotal;
									if($amountOrg > $discount){									
										$amountOrg = number_format($amountOrg-$discount,2,'.','');
										$TotalAmt = $newAmt = number_format($newAmt - $discount,2,'.','');
										$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Category',
											'is_coupon_used' => 'Yes',
											'indtotal' => $amountOrg);
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
										$dataArr1 = array('total' => $TotalAmt);
										$condition1 =array('user_id' => $userid);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr1,$condition1); 										
									}else{
										echo '7';
										exit();
									}								
								}
							}
						}
						echo 'Success|'.$CoupRes->row()->id.'|Category';
						exit();
			
					} elseif($CoupRes->row()->coupon_type== "product") {
						$PrdArr = @explode(',',$CoupRes->row()->product_id);
						$newAmt = $amount;
						foreach($ShopArr->result() as $shopRow){

							$shopPrd = $shopRow->product_id;
							
							if(in_array($shopPrd,$PrdArr)==1){
							
								if($CoupRes->row()->price_type == 2){
									
									$percentage = $CoupRes->row()->price_value;
									$amountOrg = $shopRow->indtotal;									
									$discount = ($percentage * 0.01) * $amountOrg; 
									$IndAmt = number_format($amountOrg - $discount,2,'.',''); 
									$TotalAmt = $newAmt = number_format($newAmt - $discount,2,'.','');
									$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Product',
											'is_coupon_used' => 'Yes',
											'indtotal' => $IndAmt);
									$condition =array('id' => $shopRow->id);
									
									$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									$dataArr1 = array('total' => $TotalAmt);
									$condition1 =array('user_id' => $userid);
									$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr1,$condition1); 	

								}elseif($CoupRes->row()->price_type == 1){
								
									$discount = $CoupRes->row()->price_value;
									$amountOrg = $shopRow->indtotal;
									if($amountOrg > $discount){									
										$newDisAmt = number_format($amountOrg - $discount,2,'.','');
										$TotalAmt = $newAmt = number_format($newAmt - $discount,2,'.','');
										$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Product',
											'is_coupon_used' => 'Yes',
											'indtotal' => $newDisAmt);
										
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
										$dataArr1 = array('total' => $TotalAmt);
										$condition1 =array('user_id' => $userid);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr1,$condition1); 	
		
									}else{
										echo '7';
										exit();
									}								
								}
							}
						}
						echo 'Success|'.$CoupRes->row()->id.'|Product';
						exit();

					}else{

						if($CoupRes->row()->price_type == 2){
								
									$percentage = $CoupRes->row()->price_value;
									$discount = ($percentage * 0.01) * $amount; 
									$totalAmt = number_format($amount-$discount,2,'.',''); 
									
									$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Cart',
											'is_coupon_used' => 'Yes',
											'total' => $totalAmt);
									$condition =array('user_id' => $userid);

									$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									
									/*foreach($ShopArr->result() as $shopRow){
							
										$amountOrg = $shopRow->indtotal;									
										$discount = ($percentage * 0.01) * $amountOrg; 
										$IndAmt = number_format($amountOrg - $discount,2,'.',''); 
	
										$dataArr = array('indtotal' => $IndAmt);
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									}*/
									
									echo 'Success|'.$CoupRes->row()->id;
									exit();
								
								}elseif($CoupRes->row()->price_type == 1){
								
									$discount = $CoupRes->row()->price_value;
									if($amount > $discount){									
										$amountOrg = number_format($amount-$discount,2,'.','');
										$dataArr = array('discountAmount' => $discount, 
											'couponID' => $CoupRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Cart',
											'is_coupon_used' => 'Yes',
											'total' => $amountOrg);
										$condition =array('user_id' => $userid);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
										/*$newDisAmt = ($discount / $ShopArr->num_rows());
									foreach($ShopArr->result() as $shopRow){
										$amountOrg = $shopRow->indtotal;									
										$IndAmt = number_format($amountOrg - $newDisAmt,2,'.',''); 
										$dataArr = array('indtotal' => $IndAmt);
										$condition =array('id' => $shopRow->id);
										$this->cart_model->commonInsertUpdate(SHOPPING_CART,'update',$excludeArr,$dataArr,$condition); 
									}
*/
										
										echo 'Success|'.$CoupRes->row()->id;
										exit();

									}else{
										echo '7';
										exit();
									}								
								}
								
					}
				} else {
					echo '6';
					exit();
				}
				}else{
					echo '2';
					exit();
				}
	
			
			}else{
				echo '2';
				exit();
			}
		
		
		}elseif($GiftRes->num_rows() > 0){ 
		
			$curGiftVal = (strtotime($GiftRes->row()->expiry_date) < time());
			if($curGiftVal != '') {
					echo '8';
					exit();
			} 
			
			if($GiftRes->row()->price_value > $GiftRes->row()->used_amount){
				
				$NewGiftAmt = $GiftRes->row()->price_value - $GiftRes->row()->used_amount;
				if($amount > $NewGiftAmt){
					$amountOrg = $amountOrg - $NewGiftAmt;
					
						$dataArr = array('discountAmount' => $NewGiftAmt, 
											'couponID' => $GiftRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Gift',
											'is_coupon_used' => 'Yes',
											'total' => $totalamt);
						$condition =array('user_id' => $userid);
						$this->cart_model->update_details(SHOPPING_CART,$dataArr,$condition); 
						
						
						/*$newDisAmt = ($NewGiftAmt / $ShopArr->num_rows());
						foreach($ShopArr->result() as $shopRow){
								$amountOrg = $shopRow->indtotal;									
								$IndAmt = number_format($amountOrg - $newDisAmt,2,'.',''); 
								$dataArr = array('indtotal' => $IndAmt);
								$condition =array('id' => $shopRow->id);
								$this->cart_model->update_details(SHOPPING_CART,$dataArr,$condition); 
						}*/
					
				}else{
					$dataArr = array('discountAmount' => $amountOrg, 
											'couponID' => $GiftRes->row()->id,
											'couponCode' => $code,
											'coupontype' => 'Gift',
											'is_coupon_used' => 'Yes',
											'total' => '0');
						$condition =array('user_id' => $userid);
						$this->cart_model->update_details(SHOPPING_CART,$dataArr,$condition); 
						
						/*$newDisAmt = ($NewGiftAmt / $ShopArr->num_rows());
						foreach($ShopArr->result() as $shopRow){
								$amountOrg = $shopRow->indtotal;									
								$IndAmt = number_format($amountOrg - $newDisAmt,2,'.',''); 
								$dataArr = array('indtotal' => '0');
								$condition =array('id' => $shopRow->id);
								$this->cart_model->update_details(SHOPPING_CART,$dataArr,$condition); 
						}*/
				}
				
				echo 'Success|'.$GiftRes->row()->id.'|Gift';
				exit();
			
			}else{
					echo '2';
					exit();
			}
		
		}else{
			echo '1';
			exit();
		}

	}
	
	
	/******************** Add shopping cart product transfer to  payment table Function  ********************/
	public function addPaymentCart($userid = ''){
	
	
		$this->db->select('a.*,b.city,b.state,b.country,b.postal_code');
		$this->db->from(SHOPPING_CART.' as a');
		$this->db->join(SHIPPING_ADDRESS.' as b' , 'a.user_id = b.user_id and a.user_id = "'.$userid.'" and b.id="'.$this->input->post('Ship_address_val').'"');
		$AddPayt = $this->db->get();
		
		
		if($this->session->userdata('randomNo') != '') {
			$delete = 'delete from '.PAYMENT.' where dealCodeNumber = "'.$this->session->userdata('randomNo').'" and user_id = "'.$userid.'" ';
			$this->ExecuteQuery($delete, 'delete');
			$dealCodeNumber = $this->session->userdata('randomNo');
		} else {
			$dealCodeNumber = time();
		}
		
		$insertIds = array();
		foreach ($AddPayt->result() as $result) {
					
					if($this->input->post('is_gift')==''){
						$ordergift = 0;
					}else{
						$ordergift = 1;
					}
					
				$sumTotal = number_format((($result->price + $result->product_shipping_cost + ($result->product_tax_cost * 0.01 * $result->price)) * $result->quantity ),2,'.','');
					
						$insert = ' insert into '.PAYMENT.' set 
								product_id = "'.$result->product_id.'",
								sell_id = "'.$result->sell_id.'",								
								price = "'.$result->price.'",
								quantity = "'.$result->quantity.'",
								indtotal = "'.$result->indtotal.'",
								shippingcountry = "'.$result->country.'",
								shippingid = "'.$this->input->post('Ship_address_val').'",
								shippingstate = "'.$result->state.'",
								shippingcity = "'.$result->city.'",
								shippingcost = "'.$this->input->post('cart_ship_amount').'",
								tax = "'.$this->input->post('cart_tax_amount').'",
								product_shipping_cost = "'.$result->product_shipping_cost.'",
								product_tax_cost = "'.$result->product_tax_cost.'",																												
								coupon_id  = "'.$result->couponID.'",
								discountAmount = "'.$this->input->post('discount_Amt').'",
								couponCode  = "'.$result->couponCode.'",
								coupontype = "'.$result->coupontype.'",
								sumtotal = "'.$sumTotal.'",
								user_id = "'.$result->user_id.'",
								created = now(),
								dealCodeNumber = "'.$dealCodeNumber.'",
								status = "Pending",
								payment_type = "",
								attribute_values = "'.$result->attribute_values.'",
								shipping_status = "Pending",
								total  = "'.$this->input->post('cart_total_amount').'", 
								note = "'.$this->input->post('note').'", 
								order_gift = "'.$ordergift.'", 
								inserttime = "'.time().'"';
									
						$insertIds[] = $this->cart_model->ExecuteQuery($insert, 'insert');
		}
					
						$paymtdata = array(
								'randomNo' => $dealCodeNumber,
								'randomIds' => $insertIds,
							);
						$this->session->set_userdata($paymtdata);
						
						return $insertIds;	
	}
	
	/******************** Add user shopping cart product transfer to  user payment table Function  ********************/
	public function addPaymentUserCart($userid = ''){
		
		$this->db->select('a.*,b.city,b.state,b.country,b.postal_code');
		$this->db->from(USER_SHOPPING_CART.' as a');
		$this->db->join(SHIPPING_ADDRESS.' as b' , 'a.user_id = b.user_id and a.user_id = "'.$userid.'" and b.id="'.$this->input->post('Ship_address_val').'"');
		$this->db->where("a.sell_id =".$this->input->post('sell_id'));
		$AddPayt = $this->db->get();
		
		
		if($this->session->userdata('UserrandomNo') != '') {
			$delete = 'delete from '.USER_PAYMENT.' where dealCodeNumber = "'.$this->session->userdata('UserrandomNo').'" and user_id = "'.$userid.'" ';
			$this->ExecuteQuery($delete, 'delete');
			$dealCodeNumberUser = $this->session->userdata('UserrandomNo');
		} else {
			$dealCodeNumberUser = time();
		}
		//echo $lastFeatureInsertId = $this->session->userdata('UserrandomNo');
		
		
		$insertIds = array();
		foreach ($AddPayt->result() as $result) {
		
					if($this->input->post('is_gift')==''){
						$ordergift = 0;
					}else{
						$ordergift = 1;
					}
				
				$indTotal = $this->input->post('user_cart_amount');	
				$sumTotal = $this->input->post('user_cart_total_amount');
					
						$insert = ' insert into '.USER_PAYMENT.' set 
								product_id = "'.$result->product_id.'",
								sell_id = "'.$result->sell_id.'",								
								price = "'.$result->price.'",
								quantity = "'.$result->quantity.'",
								indtotal = "'.$indTotal.'",
								shippingcountry = "'.$result->country.'",
								shippingid = "'.$this->input->post('Ship_address_val').'",
								shippingstate = "'.$result->state.'",
								shippingcity = "'.$result->city.'",
								shippingcost = "'.$result->shipping_cost.'",
								tax = "'.$this->input->post('user_cart_tax_amount').'",
								product_shipping_cost = "'.$result->product_shipping_cost.'",
								product_tax_cost = "'.$result->product_tax_cost.'",																												
								coupon_id  = "'.$result->couponID.'",
								discountAmount = "'.$this->input->post('user_discount_Amt').'",
								couponCode  = "'.$result->couponCode.'",
								coupontype = "'.$result->coupontype.'",
								sumtotal = "'.$sumTotal.'",
								user_id = "'.$result->user_id.'",
								created = now(),
								dealCodeNumber = "'.$dealCodeNumberUser.'",
								status = "Pending",
								payment_type = "'.$this->input->post('payment_value').'",
								attribute_values = "'.$result->attribute_values.'",
								digital_files="'.$result->digital_files.'",
								shipping_status = "Pending",
								total  = "'.$this->input->post('user_cart_total_amount').'", 
								note = "'.$this->input->post('note').'", 
								order_gift = "'.$ordergift.'", 
								inserttime = "'.time().'"';
									
						$insertIds[] = $this->cart_model->ExecuteQuery($insert, 'insert');
		}
		

						$paymtdata = array(
								'UserrandomNo' => $dealCodeNumberUser,
								'UserrandomIds' => $insertIds,
							);
						$this->session->set_userdata($paymtdata);
						
						return $insertIds;	
	}
	
	/******************** Add payment to subscribe table Function  ********************/
	public function addPaymentSubscribe($userid = ''){

		if($this->session->userdata('InvoiceNo') != '') {
			$InvoiceNo = $this->session->userdata('InvoiceNo');
		} else {
			$InvoiceNo = time();
		}
		
		$paymtdata = array(	'InvoiceNo' => $InvoiceNo);
		$this->session->set_userdata($paymtdata);
		
		$dataArr = array('invoice_no' => $InvoiceNo,
						'shipping_id' => $this->input->post('SubShip_address_val'),
						'shipping_cost' => $this->input->post('subcrib_ship_amount'),
						'tax' => $this->input->post('subcrib_tax_amount'),
						'total' => $this->input->post('subcrib_total_amount'),																		
							);
		$condition =array('user_id' => $userid);
		$this->cart_model->update_details(FANCYYBOX_TEMP,$dataArr,$condition); 
		
		
		return;
		
	}
	/******************** Neighbours Id  ********************/
	public function relatedPurchases($userid = ''){
		$this->db->select('other.user_id as neighbourId,product.id as productId');
		$this->db->from(USER_SHOPPING_CART.' as us');
		$this->db->join(USER_PAYMENT.' as other' , 'other.product_id = us.product_id','left');
		$this->db->join(PRODUCT.' as product' , 'other.product_id = product.id');	
		$this->db->where('us.user_id ='.$userid);
		$this->db->group_by('other.user_id');
		$resultVal= $this->db->get();
		#echo $this->db->last_query(); die;
		return $resultVal->result();
	}
	
}

?>