<?php

/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.X
 *	LICENSE: FL0M2SG681CQ - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Pets Classifieds
 *	DOMAIN: pupsnshop.com
 *	FILE: REQUESTS.INC.PHP
 *
 *	The software is a commercial product delivered under single, non-exclusive, 
 *	non-transferable license for one domain or IP address. Therefore distribution, 
 *	sale or transfer of the file in whole or in part without permission of Flynax 
 *	respective owners is considered to be illegal and breach of Flynax License End 
 *	User Agreement. 
 *
 *	You are not allowed to remove this information from the file without permission
 *	of Flynax respective owners.
 *
 *	Flynax Classifieds Software 2014 |  All copyrights reserved. 
 *
 *	http://www.flynax.com/
 *
 ******************************************************************************/

$reefless -> loadClass( 'Notice' );
$reefless -> loadClass( 'Actions' );
$reefless -> loadClass( 'BankWireTransfer', null, 'bankWireTransfer' );

/* if enabled Shopping Cart plugin */
if( $GLOBALS['config']['shc_method'] == 'multi')
{
	$reefless -> loadClass( 'ShoppingCart', false, 'shoppingCart' );
}

$item_id = (int)$_GET['item'];

if ( !$_POST['xjxargs'] )
{
	if ( $item_id )
	{
		/* get transaction info	*/
		$sql = "SELECT `T1`.*,  `T2`.`Username`, `T2`.`Own_address`, ";
		$sql .= "IF(`T2`.`Last_name` <> '' AND `T2`.`First_name` <> '', CONCAT(`T2`.`First_name`, ' ', `T2`.`Last_name`), `T2`.`Username`) AS `Full_name` ";
		$sql .= "FROM `".RL_DBPREFIX."bwt_transactions` AS `T1` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T2` ON `T1`.`Account_ID` = `T2`.`ID` ";
		$sql .= "WHERE `T1`.`ID` = '{$item_id}' ";
		$sql .= "LIMIT 1";

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

		if( $txn_info['Service'] == 'shoppingCart' )
		{
			$txn_info['Item'] = $GLOBALS['rlShoppingCart'] -> getOrderTitle( $txn_info['Item_ID'] );
		}
		elseif ( $txn_info['Service'] == 'auction' )
		{
			$sql = "SELECT * FROM `" . RL_DBPREFIX . "shc_orders` WHERE `ID` = '{$txn_info['Item_ID']}' LIMIT 1";
			$auction_info = $GLOBALS['rlAuction'] -> getRow( $sql );

			$item_tmp = $GLOBALS['rlAuction'] -> getOrderTitle( $auction_info );
			$txn_info['Item'] = $item_tmp['order_title'];

			unset( $item_tmp, $auction_info );
		}
		
		$rlHook -> load( 'phpBankWireTransferRequestDetails' );

		$txn_info['pStatus'] = $txn_info['Status'] == 'approval' ? 'unpaid' : 'paid';
	
		$rlSmarty -> assign_by_ref( 'txn_info', $txn_info );

		if($txn_info['Type'] == 'by_check')
		{
			$payment_details[] = array(
					'description' => $account_info['shc_bankWireTransfer_details']
				);

			$rlSmarty -> assign_by_ref( 'payment_details', $payment_details );
		}
		
		$bread_crumbs[] = array(
				'name' => $GLOBALS['lang']['bwt_request_details'] . ' (#'.$txn_info['Txn_ID'].')'
			);
	}
	else
	{
		$pInfo['current'] = (int)$_GET['pg'];
		$page = $pInfo['current'] ? $pInfo['current'] - 1: 0;

		$from = $page * $config['listings_per_page'];
		$limit = $config['listings_per_page'];
		
		$sql = "SELECT SQL_CALC_FOUND_ROWS `T1`.*, `T2`.`Username`, `T2`.`Own_address`, ";
		$sql .= "IF(`T2`.`Last_name` <> '' AND `T2`.`First_name` <> '', CONCAT(`T2`.`First_name`, ' ', `T2`.`Last_name`), `T2`.`Username`) AS `Full_name` ";
		$sql .= "FROM `" . RL_DBPREFIX . "bwt_transactions` AS `T1` ";
		$sql .= "LEFT JOIN `" . RL_DBPREFIX . "accounts` AS `T2` ON `T1`.`Account_ID` = `T2`.`ID` ";

	    $sql .= "WHERE `T1`.`Status` <> 'trash' AND `T1`.`Dealer_ID` = '{$account_info['ID']}' ";
		$sql .= "ORDER BY `T1`.`Date` DESC LIMIT {$from}, {$limit}";

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

		foreach( $requests as $key => $val )
		{
			if( $val['Service'] == 'shoppingCart' )
			{
				$requests[$key]['Item'] = $GLOBALS['rlShoppingCart'] -> getOrderTitle( $val['Item_ID'] );
			}
			elseif ( $val['Service'] == 'auction' )
			{
				$sql = "SELECT * FROM `" . RL_DBPREFIX . "shc_orders` WHERE `ID` = '{$val['Item_ID']}' LIMIT 1";
				$auction_info = $GLOBALS['rlAuction'] -> getRow( $sql );

				$item_tmp = $GLOBALS['rlAuction'] -> getOrderTitle( $auction_info );
				$requests[$key]['Item'] = $item_tmp['order_title'];

				unset( $item_tmp, $auction_info );
			}
			else
			{
				$rlHook -> load( 'phpBankWireTransferRequestsItem', $val );
			}
			      
			$requests[$key]['Type'] = $GLOBALS['lang'][$requests[$key]['Type']];
			$requests[$key]['pStatus'] = $requests[$key]['Status'] == 'approval' ? 'unpaid' : 'paid';
			$requests[$key]['Status'] = $GLOBALS['lang'][$requests[$key]['Status']];
		}

		$calc = $rlDb -> getRow( "SELECT FOUND_ROWS() AS `calc`" );
		
		$pInfo['calc'] = $calc['calc'];
		$rlSmarty -> assign_by_ref( 'pInfo', $pInfo );

		$rlHook -> load( 'phpBankWireTransferRequests', $requests );
		
		$rlSmarty -> assign_by_ref( 'requests', $requests );                                                                                                                       
	}
}

$rlXajax -> registerFunction( array( 'completeTransaction', $rlBankWireTransfer, 'ajaxCompleteTransaction' ) );