<?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.
**/


// No direct access to this file
defined('_JEXEC') or die('Restricted access');

// import Joomla controllerform library
jimport('joomla.application.component.controlleradmin');

/**
 * JE GroupBuy Controller
 */
class jegroupbuyControllerorders extends JControllerAdmin
{

	protected $text_prefix = 'COM_JEGROUPBUY_ORDERS';

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

	}

	/**
	 * Proxy for getModel.
	 * @since	1.6
	 */
	public function getModel($name = 'order', $prefix = 'jegroupbuyModel')
	{
		$model = parent::getModel($name, $prefix, array('ignore_request' => true));
		return $model;
	}

	public function status()
	{
		$model	= $this->getModel();

		if($model->changeStatus(true)) {
			$msg	= JText::_('COM_JEGROUPBUY_ORDERS_N_ITEMS_PUBLISHED');
		} else {
			$msg	= JText::_('COM_JEGROUPBUY_ORDERS_N_ITEMS_PUBLISHED_FAILURE');
		}

		$this->setRedirect('index.php?option=com_jegroupbuy&view=orders', $msg);
	}

	public function unstatus()
	{
		$model	= $this->getModel();

		if($model->changeStatus(false)) {
			$msg	= JText::_('COM_JEGROUPBUY_ORDERS_N_ITEMS_UNPUBLISHED');
		} else {
			$msg	= JText::_('COM_JEGROUPBUY_ORDERS_N_ITEMS_UNPUBLISHED_FAILURE');
		}

		$this->setRedirect('index.php?option=com_jegroupbuy&view=orders', $msg);
	}
	public function resendmail()
	{
		$app		= JFactory::getApplication();
		$config		= JComponentHelper::getParams('com_jegroupbuy');

 		$id = JRequest::getVar('id');

		$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.username,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=".$id;
		$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;
		$op_val['owner']= $rows->owner;
		if($rows->store_logo!="")
		{
			$op_val['logo']='<img width="25%" 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);
			}
		}

		$from		= $app->getCfg('mailfrom');
		$name		= $app->getCfg('fromname');
		$site		= $app->getCfg('sitename');
		$emailSubject=  JText::_("PRODUCT_COUPON_FROM").$site;

		$query = "SELECT email FROM #__users As u LEFT JOIN #__jegroupbuy_payment AS p ON p.user_id = u.id WHERE p.id=".$id;
		$db->setQuery( $query );
		$emailid = $db->loadResult();
		$to = $emailid;

		JUtility::sendMail($from, $name,$to, $emailSubject, $template,1);
		$app->redirect(JRoute::_('index.php?option=com_jegroupbuy&view=orders'),JText::_("MAIL_SUCCESSFULLY_SEND"));
	}
}
