<?php
/***copyright**/

header("Content-Type: text/html; charset=utf-8");
header("Cache-Control: store, no-cache, max-age=3600, must-revalidate");

$languages = $rlLang -> getLanguagesList();
$rlLang -> modifyLanguagesList( $languages );

$reefless -> loadClass( 'Valid' );
$reefless -> loadClass( 'Categories' );
$reefless -> loadClass( 'Listings' );
$reefless -> loadClass( 'Common' );

/* register functions in smarty*/ 
$rlSmarty -> register_function('rlHook', array( 'rlHook', 'load' ));

$item = $_GET['item'];

switch ($item)
{
	case 'item-sold' :
        $item_id = (int)$_GET['id'];

		/* get order info	*/
		$sql = "SELECT `T1`.*, `T2`.`Gateway`, `T3`.`Username` AS `bUsername`, `T3`.`Own_address` AS `bOwn_address`, ";
		$sql .= "`T4`.`Username` AS `dUsername`, `T4`.`Own_address` AS `dOwn_address`, ";
		$sql .= "GROUP_CONCAT(SUBSTRING_INDEX(`T5`.`Item`, ', $', 1) ORDER BY `T5`.`Date` DESC SEPARATOR '<br />') AS `title`, ";

		$sql .= "IF(`T3`.`Last_name` <> '' AND `T3`.`First_name` <> '', CONCAT(`T3`.`First_name`, ' ', `T3`.`Last_name`), `T3`.`Username`) AS `bFull_name`, ";
		$sql .= "IF(`T4`.`Last_name` <> '' AND `T4`.`First_name` <> '', CONCAT(`T4`.`First_name`, ' ', `T4`.`Last_name`), `T4`.`Username`) AS `dFull_name` ";

		$sql .= "FROM `" . RL_DBPREFIX . "shc_orders` AS `T1` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "transactions` AS `T2` ON `T1`.`Txn_ID` = `T2`.`Txn_ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T3` ON `T1`.`Buyer_ID` = `T3`.`ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T4` ON `T1`.`Dealer_ID` = `T4`.`ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "shc_order_details` AS `T5` ON `T1`.`Order_key` = CONCAT(`T5`.`Order_key`, '-D', `T1`.`Dealer_ID`) ";
		$sql .= "WHERE `T1`.`ID` = '{$item_id}' AND `T1`.`Dealer_ID` = '{$account_info['ID']}' AND `T1`.`pStatus` = 'paid' ";
		$sql .= "LIMIT 1";

		$order_info = $rlDb -> getRow( $sql );

		if ( !empty( $order_info['Order_key'] ) )
		{
			$total = 0;
			$order_key = explode( "-", $order_info['Order_key'] );

			$order_info['pStatus'] = $lang['shc_' . $order_info['pStatus']];
			$order_info['Shipping_status'] = $lang['shc_' . $order_info['Shipping_status']];

			if($order_info['Type'] == 'shop')
			{
				$sql = "SELECT `T1`.*, `T3`.`Path`, `T3`.`Type` AS `Listing_type`, `T3`.`Key` AS `Cat_key`, `T2`.`Main_photo`, `T2`.`shc_quantity` ";
				$sql .= "FROM `" . RL_DBPREFIX . "shc_order_details` AS `T1` ";
				$sql .= "LEFT JOIN `" . RL_DBPREFIX . "listings` AS `T2` ON `T1`.`Item_ID` = `T2`.`ID` ";
				$sql .= "LEFT JOIN `" . RL_DBPREFIX . "categories` AS `T3` ON `T2`.`Category_ID` = `T3`.`ID` ";
				$sql .= "WHERE `T1`.`Order_key` = '{$order_key[0]}' AND `T1`.`Dealer_ID` = '{$order_info['Dealer_ID']}' ";
				$sql .= "ORDER BY `T1`.`Date` DESC";

				$order_details = $rlDb -> getAll( $sql );
				
				if ( !empty ( $order_details ) )
				{
					foreach ( $order_details as $iKey => $iVal )
					{
						$order_details[$iKey]['total'] = round( ( $iVal['Quantity'] * $iVal['Price'] ), 2 );
						$total += $order_details[$iKey]['total'];
					}

					$total = round( $total, 2 );
					$order_info['items'] = $order_details; 
				}
			}
			else
			{
				$total = $order_info['Total'];
			}

			$rlSmarty -> assign_by_ref( 'order_info', $order_info );
			$rlSmarty -> assign( 'total', $total );
		}

		break;

	case 'item-purchased' :
        $item_id = (int)$_GET['id'];

		/* get order info	*/
		$sql = "SELECT `T1`.*, `T2`.`Gateway`, `T3`.`Username` AS `bUsername`, `T3`.`Own_address` AS `bOwn_address`, ";
		$sql .= "`T4`.`Username` AS `dUsername`, `T4`.`Own_address` AS `dOwn_address`, ";
		$sql .= "GROUP_CONCAT(SUBSTRING_INDEX(`T5`.`Item`, ', $', 1) ORDER BY `T5`.`Date` DESC SEPARATOR '<br />') AS `title`, ";

		$sql .= "IF(`T3`.`Last_name` <> '' AND `T3`.`First_name` <> '', CONCAT(`T3`.`First_name`, ' ', `T3`.`Last_name`), `T3`.`Username`) AS `bFull_name`, ";
		$sql .= "IF(`T4`.`Last_name` <> '' AND `T4`.`First_name` <> '', CONCAT(`T4`.`First_name`, ' ', `T4`.`Last_name`), `T4`.`Username`) AS `dFull_name` ";

		$sql .= "FROM `" . RL_DBPREFIX . "shc_orders` AS `T1` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "transactions` AS `T2` ON `T1`.`Txn_ID` = `T2`.`Txn_ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T3` ON `T1`.`Buyer_ID` = `T3`.`ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T4` ON `T1`.`Dealer_ID` = `T4`.`ID` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "shc_order_details` AS `T5` ON `T1`.`Order_key` = CONCAT(`T5`.`Order_key`, '-D', `T1`.`Dealer_ID`) ";
		$sql .= "WHERE `T1`.`ID` = '{$item_id}' AND `T1`.`Buyer_ID` = '{$account_info['ID']}' AND `T1`.`Type` = 'shop' ";
		$sql .= "LIMIT 1";

		$order_info = $rlDb -> getRow( $sql );	

		if ( !empty( $order_info['Order_key'] ) )
		{
			$total = 0;
			$order_key = explode( "-", $order_info['Order_key'] );

			$order_info['pStatus'] = $lang['shc_' . $order_info['pStatus']];
			$order_info['Shipping_status'] = $lang['shc_' . $order_info['Shipping_status']];

			$sql = "SELECT `T1`.*, `T3`.`Path`, `T3`.`Type` AS `Listing_type`, `T3`.`Key` AS `Cat_key`, `T2`.`Main_photo`, `T2`.`shc_quantity` ";
			$sql .= "FROM `" . RL_DBPREFIX . "shc_order_details` AS `T1` ";
			$sql .= "LEFT JOIN `" . RL_DBPREFIX . "listings` AS `T2` ON `T1`.`Item_ID` = `T2`.`ID` ";
			$sql .= "LEFT JOIN `" . RL_DBPREFIX . "categories` AS `T3` ON `T2`.`Category_ID` = `T3`.`ID` ";
			$sql .= "WHERE `T1`.`Order_key` = '{$order_key[0]}' ";

			if ( $GLOBALS['config']['shc_method'] == 'multi' )
			{
				$sql .= "AND `T1`.`Dealer_ID` = '{$order_info['Dealer_ID']}' ";
			}

			$sql .= "ORDER BY `T1`.`Date` DESC";

			$order_details = $rlDb -> getAll( $sql );

			if ( !empty( $order_details ) )
			{
				foreach( $order_details as $iKey => $iVal )
				{
					$order_details[$iKey]['total'] = round( ( $iVal['Quantity'] * $iVal['Price'] ), 2 );
					$total += $order_details[$iKey]['total'];
				}

				$total = round( $total, 2 );
				$order_info['items'] = $order_details; 
			}

			$rlSmarty -> assign_by_ref( 'order_info', $order_info );
			$rlSmarty -> assign( 'total', $total );
		} 
		break;
}

$rlSmarty -> display( RL_PLUGINS . 'shoppingCart' . RL_DS . 'print.tpl' );

exit;
