<?php
/**
 * jeGroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2010 - 2011 J-Extension
 * @license GNU/GPL, see LICENSE.php for full license.
**/

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');

class jegroupbuyControllercategory extends JController
{

	function __construct( $config = array() )
	{
		parent::__construct( $config );

	}

	function display()
	{
		JRequest::setVar( 'view', 'category');
		parent::display();
	}

	function productdetails()
	{
		$pid = JRequest::getInt("pid","");
		JRequest::setVar( 'view', 'productdetails');
		JRequest::setVar( 'pid', $pid);
		parent::display();
	}

	function paymentDetails()
	{
		$pid = JRequest::getInt("pid","");
		JRequest::setVar("view","paymentdetails");
		JRequest::setVar("layout","default");
		JRequest::setVar("pid",$pid);
		parent::display();
	}

	function processPayment()
	{
		JRequest::setVar( 'view', 'paymentdetails' );
		JRequest::setVar( 'layout', 'process' );
		parent::display();
	}

	function changeToExpired()
	{
		$db = &JFactory::getDBO();
		$pID = JRequest::getInt("pID","");
		$sql = "UPDATE #__jegroupbuy_product SET expired = 1 WHERE id=".$pID;
		$db->setQuery($sql);
		$db->query();
		echo $pID;
		exit;
	}

	function paymentReturnUrl()
	{
		$db = &JFactory::getDBO();
		$user	=& JFactory::getUser();
		$id = JRequest::getInt("id","");
		$ppc = JRequest::getInt("ppc","");
		$sql ="SELECT count(*) FROM #__jegroupbuy_payment WHERE p_id=".$id." AND user_id = ".$user->id;
		$db->setQuery($sql);
		$ppcount = $db->loadResult();
		if($ppcount > $ppc)
		{
			//success
			$redirect=JURI::root().'index.php?option=com_jegroupbuy&view=couponpage&result=succ';
			$this->setRedirect($redirect);
		}else
		{
			$redirect=JURI::root().'index.php?option=com_jegroupbuy&task=paymentDetails&pid='.$id.'&result=fail';
			$this->setRedirect($redirect);
		}
	}

	function getCouponTemplate($pid)
	{
		$db = JFactory::getDBO();
		$user	=& JFactory::getUser();

		$sql = "SELECT pro.*,s.address,s.contact_number,py.transaction_id,py.method,s.store_logo,s.email,s.shop_url,s.owner,py.id,py.pdate,py.status,u.name,u.email as user_email FROM #__jegroupbuy_product AS pro"
			   ." LEFT JOIN #__jegroupbuy_stores AS s ON s.id = pro.store_id"
			   ." LEFT JOIN #__jegroupbuy_payment AS py ON py.p_id = pro.id"
			   ." LEFT JOIN #__users AS u ON u.id = py.user_id"
			   ." WHERE py.id=".$pid;
		$db->setQuery( $sql );
		$rows = $db->loadObject();

		$query = 'SELECT template FROM #__jegroupbuy_template where id=1';
		$db->setQuery( $query );
		$template = $db->loadResult();

		//store fields
		$op_val['shop_address_1']=$rows->address;
		$op_val['shop_phone']=$rows->contact_number;
		$op_val['shop_email']=$rows->email;
		$op_val['shop_url']=$rows->shop_url;
		if($rows->store_logo!="")
		{
			$op_val['logo']='<img src="'.JURI::root().'images/jegroupbuy/storelogo/'.$rows->store_logo.'"/>';
		}
		else
		{
			$op_val['logo']="";
		}


		//product fields
		$op_val['coupon_code']=$rows->coupon_code;
		$op_val['coupon_date']=$rows->deal_to_date;

		//payment field
		$op_val['order_number']=$rows->id;
		$op_val['order_date']=JHTML::_('date',$rows->pdate, JText::_('DATE_FORMAT_LC4').' H:i');
		if($rows->status =="1")
		$op_val['order_status']=JText::_('CC_PAYMENT_SUCCESS_MSG_TEMPLATE');
		else
		$op_val['order_status']=JText::_('CC_PAYMENT_UNSUCCESS_MSG_TEMPLATE');
		$op_val['transaction_id']=$rows->transaction_id;

		//product fields
		$price=substr($rows->product_price,1);
		$op_val['product_name']=$rows->title;
		$op_val['product_price']=$rows->product_price;
		$op_val['discount_price']="$".$rows->product_discount;;
		$op_val['you_save']=$rows->you_save;
		$op_val['deal_price']=$rows->deal_price;

		$op_val['user_name']		= $rows->name;
		$op_val['user_mailid']		= $rows->user_email;
		$op_val['user_payment']		= $rows->method;

		foreach($op_val as $op_vals=>$value)
		{
			if(trim($value)=="")
			{
				$find = "{".$op_vals."}";
				$template=str_replace($find,"",$template);
			}
			else
			{
				$find = "{".$op_vals."}";
				$template=str_replace($find,$value,$template);
			}
		}
		return $template;
	}
}	?>