<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program. 
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED W
 ITH THIS DISTRIBUTION PACKAGcitDet.            *
 ****************************************************************************/
//
//$Id: WishlistModel.php, v 1.0 2013/06/29 20:52:11
//
class WishlistModel extends Model {
    //ADD TO WISHLIST
    public function addToWishlist() {
        $sql        = "SELECT 
				  listID
				FROM 
				  " . DB_PREFIX . "user_wishlist
				WHERE userID=" . $_POST['userID'] . " LIMIT 1";
        $userlistID = $this->ObjDatabase->readValuesSlave($sql);
        if (empty($userlistID)) {
            $sql = "INSERT INTO " . DB_PREFIX . "user_wishlist(
									  userID,  
									  addedDate
									) 
									VALUES 
									  (
										" . $_POST['userID'] . ", 
										NOW()
									  )";
            $this->ObjDatabase->setQuery($sql);
        }
        $listID = (empty($userlistID) ? $this->ObjDatabase->getInsertId() : $userlistID[0]['listID']);
        $sql1   = "INSERT INTO " . DB_PREFIX . "user_wishlist_items(
								  listID,  
								  productID,
								  addedDate
								) 
								VALUES 
								  (
									" . $listID . ",
									" . $_POST['productID'] . ", 
									NOW()
								  )";
        $this->ObjDatabase->setQuery($sql1);
    }
    //END OF FUNCTION
    //CHECK PRODUCT AVAILABLE IN THE WISHLIST FOR USER
    public function checkInWishlist($productID, $regUserID) {
        $sql    = "SELECT 
				  COUNT(*) AS count, UsrWisIte.itemID
				FROM 
				  " . DB_PREFIX . "user_wishlist UsrWis
				  INNER JOIN " . DB_PREFIX . "user_wishlist_items AS UsrWisIte ON (
					UsrWis.listID = UsrWisIte.listID 
				  )
				WHERE 
				   userID= " . $regUserID . "
				   AND productID=" . $productID . "";
        $result = $this->ObjDatabase->readValueSlave($sql);
        return $result;
    }
    //END OF FUNCTION
    public function listUsersWishListItems($params = array()) {
        extract($params);
        $currentLanguageID = $languageIDs['Current'];
        $sql               = "
				SELECT 
					product.productID
					
				FROM 
						" . DB_PREFIX . "user_wishlist UsrWis
					INNER JOIN 
						" . DB_PREFIX . "user_wishlist_items AS UsrWisIte 
							ON (
								UsrWis.listID = UsrWisIte.listID 
							)
					LEFT JOIN 	
						" . DB_PREFIX . "products  AS product 
							ON (
								product.productID = UsrWisIte.productID 
							)
					LEFT JOIN 
						" . DB_PREFIX . "product_details AS productDet 
							ON (
								product.productID = productDet.productID 
								AND 
								productDet.languageID = " . $currentLanguageID . "
							)
					LEFT JOIN 
						" . DB_PREFIX . "business AS bus 
							ON (
								product.businessID = bus.businessID 
							) 
					LEFT JOIN 
						" . DB_PREFIX . "product_options	AS productOpt 
							ON (
								product.productID = productOpt.productID
								AND
								productOpt.productOptionStatus = 'Active' 
								AND 
								productOpt.productOptionDefault = 'Yes'
							) 
				WHERE
					product.productStatus != 'Deleted' 
					AND 
					UsrWis.userID=" . (int) $userID;
        return $this->ObjDatabase->getFieldsSlave($sql);
    }
    //LIST ITEMS ADDED IN WISHLIST FOR A USER
    public function listUserWishlist($params = array()) {
        extract($params);
        $currentLanguageID = $languageIDs['Current'];
        $now               = date('Y-m-d');
        $sql               = "
				SELECT 
					UsrWisIte.itemID,
					product.productID,
					product.productDefaultCurrency,
					LEAST(
						COALESCE(
							varOffPri.offerPrice,
							productOpt.productOptionProductPrice
						),
						COALESCE(
							varGroPri.specialPrice,
							productOpt.productOptionProductPrice
						) 
					)
					AS offerPrice,
					ROUND(
						(
							(
								(
								productOpt.productOptionRegularPrice-
									LEAST(
										COALESCE(
											varOffPri.offerPrice,
											productOpt.productOptionProductPrice
										),
										COALESCE(
											varGroPri.specialPrice,
											productOpt.productOptionProductPrice
										) 
									)
								)*100
							)/productOpt.productOptionRegularPrice
						),2
					) AS productOptionOfferDiscount,
					product.productUrl,
					product.productDefaultCurrency,
					product.InventoryTrackLevel,
					product.multiOptionsAvailable,
					product.productImage,
					productDet.productTitle,
					productOpt.productOptionID,
					productOpt.productOptionProductPrice,
					productOpt.productOptionRegularPrice,
					bus.businessStatus,
					ROUND((((productOpt.productOptionRegularPrice-productOpt.productOptionProductPrice)*100)/productOpt.productOptionRegularPrice),2) AS productOptionDiscount,
					IF(product.productEndTime != '0000-00-00' AND product.productEndTime < '" . $now . "','1','0') AS productEnd,
					COUNT(ProductReviewID) AS productReviewsCount,
					SUM(productRating) ratingTotal,
					productStatus
				FROM 
				  " . DB_PREFIX . "user_wishlist UsrWis
				  INNER JOIN " . DB_PREFIX . "user_wishlist_items AS UsrWisIte 
						ON (
							UsrWis.listID = UsrWisIte.listID 
						)
				LEFT JOIN 	
					" . DB_PREFIX . "products  AS product 
						ON (
							product.productID = UsrWisIte.productID 
						)
				LEFT JOIN 
					" . DB_PREFIX . "product_details AS productDet 
						ON (
							product.productID = productDet.productID 
							AND 
							productDet.languageID = " . $currentLanguageID . "
						)
				LEFT JOIN 
						" . DB_PREFIX . "business AS bus 
					ON (
						product.businessID = bus.businessID 
					) 
				";
        $sql .= "
			LEFT JOIN 
				" . DB_PREFIX . "product_options	AS productOpt 
					ON (
						product.productID = productOpt.productID
						AND
						productOpt.productOptionStatus = 'Active' 
						AND 
						productOpt.productOptionDefault = 'Yes'
					) 
			LEFT JOIN
				" . DB_PREFIX . "variant_offer_price AS varOffPri
					ON	(
						varOffPri.productOptionID = productOpt.productOptionID
						AND
						varOffPri.userGroupID='" . $userGroupID . "'
					)
			LEFT JOIN
				" . DB_PREFIX . "variant_group_price AS varGroPri
					ON	(
						varGroPri.productOptionID = productOpt.productOptionID
						AND
						varGroPri.userGroupID='" . $userGroupID . "'
					)	
			LEFT JOIN 
				" . DB_PREFIX . "product_reviews	AS productRev
					ON  (
						product.productID = productRev.productID
						AND
						productRev.reviewStatus = 'Active'
					) 
			 ";
        $sql .= " 
			WHERE
					product.productTypeID = '" . $productType['productTypeID'] . "' 
					AND product.productStatus != 'Deleted' 
					AND product.productStartTime <= '" . $now . "'  
					AND UsrWis.userID=" . $regUserID;
        $sql .= " 
			GROUP BY product.productID 
			ORDER BY 
				UsrWisIte.addedDate DESC ";
        $resSelectProducts = $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC); //echo $sql;
        return $resSelectProducts;
    }
    //END OF FUNCTION
    //TO REMOVE ITEMS FROM WISHLIST
    public function removeFromWishlist($regUserID) {
        $sql = "DELETE usrWisIte.* FROM 
					" . DB_PREFIX . "user_wishlist_items usrWisIte
					INNER JOIN " . DB_PREFIX . "user_wishlist AS usrWis 
						ON (
							usrWis.listID = usrWisIte.listID 
						)
					 WHERE 
						itemID = '" . $_POST['itemid'] . "'
						AND userID='" . $regUserID."'";
       // echo $sql;exit;
        return $this->ObjDatabase->setQuery($sql);
    }
    //END OF FUNCTION
}
