<?php
/**
 * JE GroupBuy package
 * @author J-Extension <contact@jextn.com>
 * @link http://www.jextn.com
 * @copyright (C) 2011 - 2012 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');

// Import Joomla predefined functions
	jimport( 'joomla.application.component.view' );

class jegroupbuyViewcouponpage  extends JViewLegacy
{

	function display( $tpl = null )
	{
		$user	= JFactory::getUser();
		$app 	= JFactory::getApplication();
		$db 	= JFactory::getDBO();
		if($user->get('guest'))
		{
			$url = base64_encode(JRoute::_('index.php?option=com_jegroupbuy&view=couponpage'));
			$link = "index.php?option=com_users&view=login&return=".$url;
			$app->redirect( $link);
		}

		if ($this->getLayout() == 'coupon')
		{
			$payid=JRequest::getInt('payment_id','');
			$print=JRequest::getInt('print','');
			$template=$this->getCouponTemplate($payid);
			$this->assignRef('payid',$payid);
			$this->assignRef('print',$print);
			$this->assignRef('template',$template);

		}else
		{
			$result = JRequest::getVar("result","");
			$sql =" SELECT py.*,pr.title,pr.deal_price FROM #__jegroupbuy_payment AS py"
				 ." LEFT JOIN #__jegroupbuy_product AS pr ON pr.id=py.p_id"
				 ." WHERE user_id=".$user->id." AND status = 1 ORDER BY pdate DESC LIMIT 5";
			$db->setQuery($sql);
			$rows = $db->loadObjectList();
			$this->assignRef('rows',$rows);
			$this->assignRef('result',$result);
		}
		parent::display( $tpl );
	}
	function getCouponTemplate($pid)
	{
		$db 	= JFactory::getDBO();
		$user	= JFactory::getUser();

		$sql = "SELECT pro.*,u.email as user_email,u.username,s.address,s.contact_number,py.transaction_id,py.method,s.store_logo,s.email,s.shop_url,py.id,py.pdate,py.status 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();

		$query = "SELECT email FROM #__users As u LEFT JOIN #__jegroupbuy_payment AS p ON p.user_id = u.id WHERE p.id=".$pid;
		$db->setQuery( $query );
		$emailid = $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().$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->username;
		$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;
	}
}
