<?php
/*======================================================================*\
|| #################################################################### ||
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2013 Fillip Hannisdal AKA Revan/NeoRevan/Belazor 	  # ||
|| # All Rights Reserved. 											  # ||
|| # This file may not be redistributed in whole or significant part. # ||
|| # ---------------------------------------------------------------- # ||
|| # You are not allowed to use this on your server unless the files  # ||
|| # you downloaded were done so with permission.					  # ||
|| # ---------------------------------------------------------------- # ||
|| #################################################################### ||
\*======================================================================*/

if (!is_object($vbulletin->db))
{
	exit;
}

if (!$vbulletin->options['dbtech_vbshop_enable_feedback'])
{
	exit;
}

// ########################################################################
// ######################### START MAIN SCRIPT ############################
// ########################################################################

if (defined('IN_CONTROL_PANEL'))
{
	define('ECHO_CRON_LOG', true);
}

$feedback_q = $vbulletin->db->query_read_slave("
	SELECT 
		purchase.shopid,
		purchase.purchaseid,
		user.* 
	FROM " . TABLE_PREFIX . "dbtech_vbshop_purchase AS purchase
	INNER JOIN " . TABLE_PREFIX . "user AS user USING(userid)
	WHERE purchase.feedbackstatus = 0
		AND purchase.dateline <= (UNIX_TIMESTAMP() - 86400)
	GROUP BY purchase.shopid, purchase.userid
");
while ($feedback = $vbulletin->db->fetch_array($feedback_q))
{
	if (!$shop = VBSHOP::$cache['shop'][$feedback['shopid']])
	{
		log_cron_action('Reminder Error: Invalid shop ID: ' . $feedback['shopid'], $nextitem);

		// Flag as having sent reminder
		VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
		continue;
	}

	if (!$shop['canhavefeedback'])
	{
		log_cron_action('Reminder Error: Shop ID ' . $feedback['shopid'] . ' cannot receive feedback.', $nextitem);

		// Flag as having sent reminder
		VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
		continue;
	}

	if ($shop['ownerid'])
	{
		if ($shop['ownerid'] == $feedback['userid'])
		{
			log_cron_action('Reminder Error: Shop owner is also buyer for purchase: ' . $feedback['purchaseid'], $nextitem);

			// Flag as having sent reminder
			VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
			continue;			
		}

		$owner = fetch_userinfo($shop['ownerid']);

		if (!is_array($owner['permissions']))
		{
			// Ensure we have the perms
			cache_permissions($owner);
		}

		if (!($owner['permissions']['dbtech_vbshoppermissions'] & $vbulletin->bf_ugp_dbtech_vbshoppermissions['canreceivefeedback']))
		{
			log_cron_action('Reminder Error: ' . $owner['username'] . ' Cannot Receive Feedback', $nextitem);

			// Couldn't find the shop
			VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
			continue;
		}
	}

	$user = fetch_userinfo($feedback['userid']);
	if (!is_array($user['permissions']))
	{
		// Ensure we have the perms
		cache_permissions($user);
	}

	if (!($user['permissions']['dbtech_vbshoppermissions'] & $vbulletin->bf_ugp_dbtech_vbshoppermissions['canleavefeedback']))
	{
		log_cron_action('Reminder Error: ' . $user['username'] . ' Cannot Leave Feedback', $nextitem);

		// Couldn't find the shop
		VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
		continue;
	}

	// Send PM
	$res = VBSHOP::sendPM(
		$feedback['userid'],
		$vbphrase['dbtech_vbshop_feedbackalert_gemailsubject'],
		construct_phrase($vbphrase['dbtech_vbshop_feedbackalert_gemailbody'],
			$feedback['username'],
			$shop['title'],
			$vbulletin->options['bburl'],
			$feedback['shopid']
		),
		$feedback['username'],
		$feedback['userid']
	);

	if (is_array($res))
	{
		log_cron_action('Reminder Error(s): ' . var_export($res, true), $nextitem);
		continue;
	}

	if (!$res)
	{
		log_cron_action('Reminder Error: User "' . $feedback['userid'] . '" does not exist.', $nextitem);
		continue;
	}

	// Flag as having sent reminder
	VBSHOP::$db->update('dbtech_vbshop_purchase', array('feedbackstatus' => 1), 'WHERE purchaseid = ' . $feedback['purchaseid']);
}
$vbulletin->db->free_result($feedback_q);

log_cron_action('Reminders Sent', $nextitem);