<?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 WITH THIS DISTRIBUTION PACKAGE.			*
 ****************************************************************************/ //
//$Id: OrderModel.php, v 1.0 2011/06/14 20:52:11
// 
class OfflineOrderModel extends Model {
    public function listProductsForOrder($params, $languageIDs) {
        $defaultLanguageID = $languageIDs['Default'];
        $currentLanguageID = $languageIDs['Current'];
        $currDateTime      = isset($currDateTime) ? $currDateTime : '';
        $currDateTime      = ($currDateTime == "") ? "'" . date('Y-m-d') . "'" : "'" . $currDateTime . "'";
        $sqlJoin           =  "";
        $sqlBusIDJoin      =  "product";
        if ($params['businessID'] > 0) {
            $sqlJoin       =  " INNER JOIN ".DB_PREFIX."products_options_business pdtOptBus ON 
                                (pdtOptBus.productOptionID = productOpt.productOptionID AND
                                 pdtOptBus.businessID = '".$params['businessID']."')";
            $sqlBusIDJoin  =  "pdtOptBus";
        }
        $sql               = " SELECT 
						productOptDet.productOptionID,
						SUBSTRING(productOptDet.productOptionName,1,80) as productOptionName,
						productOpt.productID,
						SUBSTRING(productDet.productTitle,1,80) as productTitle,
						productOpt.productOptionProductPrice,
						product.productDefaultCurrency,
						product.InventoryTrackLevel,
						productOpt.productOptionQuantity
					FROM 
						" . DB_PREFIX . "products product
					INNER JOIN 
						" . DB_PREFIX . "product_options productOpt 
						ON 
						(
							productOpt.productID=product.productID 
							AND 
							productOpt.productOptionStatus='Active' 
						)
                                        $sqlJoin
					INNER JOIN 
						" . DB_PREFIX . "product_option_details AS productOptDet 
						ON 
						(
							productOptDet.productOptionID=productOpt.productOptionID 
							AND 
							productOptDet.languageID=" . $currentLanguageID . "
						)
					LEFT JOIN 
						" . DB_PREFIX . "product_details productDet
						ON
						(productDet.productID=product.productID AND productDet.languageID=
						'" . $currentLanguageID . "'
						)
					LEFT JOIN 
						" . DB_PREFIX . "business bus
						ON
						(bus.businessID=$sqlBusIDJoin.businessID AND bus.businessStatus=
						'Active'
						)
					LEFT JOIN 
						" . DB_PREFIX . "business_details busDet
						ON
						(busDet.businessID=bus.businessID AND busDet.languageID=
						'" . $currentLanguageID . "'
						)
					LEFT JOIN 
						" . DB_PREFIX . "timezones AS timz 
						ON (product.productTimeZone = timz.timeZoneID)	
					WHERE product.productTypeID !=3 
					AND (product.InventoryTrackLevel='No' OR product.InventoryTrackLevel='Options' OR (product.InventoryTrackLevel='Product'))"; //AND productOptionQuantity >0
        if ($params['productTitle'] != '') {
            $productTitle = $this->escape($params['productTitle']);
            $sql .= " and (productDet.productTitle LIKE '%" . $productTitle . "%') ";
        }
        if ($params['businessName'] != '') {
            $businessName = $this->escape($params['businessName']);
            $sql .= " and (busDet.businessName LIKE '%" . $businessName . "%') ";
        }
        if ($params['businessID'] > 0) {
            $sql .= " and pdtOptBus.businessID='" . $params['businessID'] . "' ";
        }
        $sql .= " 
					AND 
					(
						product.productStartTime <= " . $currDateTime . " 
						AND
						(
							product.productEndTime = '0000-00-00'
							OR
							product.productEndTime >=	" . $currDateTime . "
						)
					) 
					AND product.productStatus='Active'  
					ORDER BY product.productID desc";
        if($params['start'] && $params['end']){
            $sql .= "	LIMIT " . $params['start'] . "," . $params['end'] . "";
        }
        $result = $this->ObjDatabase->readValuesObject($sql);
        return $result;
    }
    public function checkOptionAvilable($productID, $productOptionID) {
        $sql    = " SELECT 
						proCusOptLin.customOptionLinkID
					FROM 
						" . DB_PREFIX . "product_custom_option_link proCusOptLin
					LEFT JOIN
						" . DB_PREFIX . "product_custom_option proCusOpt
					ON 
						(proCusOpt.customOptionID=proCusOptLin.customOptionID)
					WHERE 
						proCusOptLin.productID=" . $productID . " AND proCusOpt.status='A'
					LIMIT 0,1";
        //echo $sql;				
        $result = $this->ObjDatabase->readValues($sql);
        return count($result);
    }
    public function listProductOptions($params, $languageIDs) {
        $defaultLanguageID = $languageIDs['Default'];
        $currentLanguageID = $languageIDs['Current'];
        extract($taxParams);
    }
}