<?php
/*
Template Name: Orders Handling
*/
?>

<?php get_header(); ?>
<?php 
$aOptions = SofaShoppr::initOptions( false );
$entrycurrency = $aOptions[ 'entrycurrency' ];
$taxpercentage = $aOptions[ 'taxpercentage' ];
if( $taxpercentage < 0 ) $taxpercentage = 0;
$shipinlandcost = $aOptions[ 'shipinlandcost' ];
if( $shipinlandcost < 0 ) $shipinlandcost = 0;

$entrycurrency = $aOptions[ 'entrycurrency' ];
global $current_user;
get_currentuserinfo();
?>
<?php if( have_posts() ) : while( have_posts() ) : the_post(); ?>
<?php if( $current_user->ID == 1 ) { ?>
<div id="content" class="fullwidth">
    
    <div class="article">

        <h1><?php the_title(); ?></h1>
        <?php edit_post_link( 'EDIT', '<div class="post-info"><span class="date">', '</span></div>' ); ?>
        <?php the_content(); ?>
        <?php
		global $wpdb;
		$global_counter = 1;
		$transactions = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . "purchases_history ORDER BY trans_date DESC" );
		if( $transactions ) {
			echo '<table id="overvew">';
			echo '<tr>';
			echo '<th>' . __( "ID", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Date", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Transaction ID", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Buyer Name", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Gross", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Order Status", "sofa_shoppr" ) . '</th>';
			echo '<th>' . __( "Delete", "sofa_shoppr" ) . '</th>';
			echo '</tr>';
			foreach( $transactions as $transaction ) {
				$purchase_id = $transaction->id; // ID
				$purchase_date = $transaction->trans_date; // date
				$purchase_code = $transaction->unique_code; // unique code returned by Paypal
				$purchase_basket = $transaction->trans_basket; // in da basket
				$purchase_buyer = $transaction->user_name; // user name, last name
				$purchase_buyer_status = $transaction->trans_status; // user verified?
				$purchase_buyer_email = $transaction->user_email; // user email
				$purchase_buyer_phone = $transaction->user_phone; // user phone
				$purchase_buyer_address = $transaction->user_address;
				$purchase_buyer_zipcode = $transaction->user_zip;
				$purchase_buyer_city = $transaction->user_city;
				$purchase_buyer_country = $transaction->user_country;
				$purchase_buyer_currency = $transaction->currency;
				$purchase_buyer_gross = $transaction->gross;
				$purchase_buyer_flag_shipped = $transaction->flag_shipped;
				
				if( $global_counter % 2 == 0 ) echo '<tr class="bggray">';
				else echo '<tr class="bgwhite">';
				
				echo '<td>' . $global_counter . '</td>';
				echo '<td>' . date( get_option( 'date_format' ), strtotime( $purchase_date ) ) . '</td>';
				echo '<td><a href="#" title="' . __( "Order details", "sofa_shoppr" ) . '" id="showbasket_' . $global_counter . '">' . $purchase_code . '</a></td>';
				echo '<td>' . $purchase_buyer . ' (' . $purchase_buyer_status . ')</td>';
				echo '<td>' . $entrycurrency . ' ' . $purchase_buyer_gross . '</td>';
				// status
				$ad__link = get_bloginfo( 'template_directory' ) . '/order_item_screen.php?itemid=' . $purchase_id;
				$del_link = get_bloginfo( 'template_directory' ) . '/order_delete_screen.php?itemid=' . $purchase_id;
				if( $purchase_buyer_flag_shipped == 0 ) echo '<td><a href="' . $ad__link . '" class="nyroModal pending">' . __( "Pending", "sofa_shoppr" ) . '</a></td>';
				if( $purchase_buyer_flag_shipped == 1 ) echo '<td><a href="' . $ad__link . '" class="nyroModal shipped">' . __( "Shipped", "sofa_shoppr" ) . '</a></td>';
				if( $purchase_buyer_flag_shipped == 2 ) echo '<td><a href="' . $ad__link . '" class="nyroModal returned">' . __( "Returned", "sofa_shoppr" ) . '</a></td>';
				echo '<td><a href="' . $del_link . '" class="nyroModal">' . __( "DELETE", "sofa_shoppr" ) . '</a></td>';
				
				echo '</tr>';
				
				// meta basket
				echo '<tr class="bgwhite meta" id="metabasket_' . $global_counter . '">';
				echo '<td colspan="3">';
				echo '<h2>' . __( "Basket details", "sofa_shoppr" ) . '</h2>';
				// format basket
				$basket_items_array = explode( ',', $purchase_basket );
				$count_basket_items = count( $basket_items_array ) - 1;
				$count_pairs = 0;
				$sum_subtotal = 0;
				while( $count_pairs < $count_basket_items ) {
					$basket_pairs_array = explode( ':', $basket_items_array[ $count_pairs ] );
					$basket_post_obj = get_post( $basket_pairs_array[ 0 ] );
					$custom_field = get_post_custom( (int)$basket_post_obj->ID );
					$item_price_net = ( $basket_pairs_array[ 5 ] != NULL ) ? $basket_pairs_array[ 5 ] : $custom_field[ 'price' ][ 0 ]; // backwards compliance // unit price
					$item_all = stripslashes( strip_tags( $basket_post_obj->post_title ) ); // item title
					if( $basket_pairs_array[ 3 ] != NULL ) $item_all .= ' | ' . $basket_pairs_array[ 3 ]; // size
					if( $basket_pairs_array[ 4 ] != NULL ) $item_all .= ' | ' . $basket_pairs_array[ 4 ]; // color
					echo $basket_pairs_array[ 2 ] . 'x ' . $item_all . ' | ' . $purchase_buyer_currency . ' ' . number_format( $item_price_net, 2, '.', ',' ) . '<br />';
					$sum_subtotal = $sum_subtotal + ( $item_price_net * $basket_pairs_array[ 2 ] );
					
					$count_pairs ++;
				}
				echo __( "Subtotal", "sofa_shoppr" ) . ': <strong>' . $purchase_buyer_currency . ' ' . number_format( $sum_subtotal, 2, '.', ',' ) . '</strong>';
				echo '</td>';
				echo '<td>&nbsp;</td>';
				echo '<td colspan="3">';
				echo '<h2>' . __( "Shipped to", "sofa_shoppr" ) . '</h2>';
				echo $purchase_buyer . '<br />';
				echo $purchase_buyer_address . '<br />';
				echo $purchase_buyer_city . ', ' . $purchase_buyer_zipcode . '<br />';
				echo $purchase_buyer_country . '<br />';
				echo __( "Phone", "sofa_shoppr" ) . ': ' . $purchase_buyer_phone . '<br />';
				echo __( "E-mail address", "sofa_shoppr" ) . ': <a href="mailto:' . $purchase_buyer_email . '">' . $purchase_buyer_email . '</a>';
				echo '</td>';
				echo '</tr>';
				
				$global_counter ++;
			}
			echo '</table>';

		?>
        
			<script language="javascript" type="text/javascript">
			jQuery( document ).ready( function() {
				jQuery( "#overvew td a" ).click( function( event ) {
					var objclicked = event.target.id;
					var prefix_array = objclicked.split( "_" );
					var trgt = "";
					
					if( prefix_array[ 0 ] == "showbasket" ) {
						trgt = "#metabasket_" + prefix_array[ 1 ];
						jQuery( trgt ).toggle();
					}
					
					return false;
				} );
			} );
            </script>
            
        <?php } else { 
        
        	echo '<p><i>' . __( "No orders found!", "sofa_shoppr" ) . '</i></p>';
        
         } ?>
    
    </div>

</div>

<?php } else { ?>
<div id="content" class="fullwidth">
    
    <div class="article">

        <h1><?php _e( "Ups!", "sofa_shoppr" ); ?></h1>
        <p><?php _e( "You don't have a permission to view this page!", "sofa_shoppr" ); ?></p>
    </div>

</div>
<?php } ?>
<?php endwhile; endif; ?>

<?php get_footer(); ?>
		   