<?php
$this_file = str_replace( '\\', '/', __FILE__ );
$this_file = explode( 'wp-content', $this_file );
$this_file = $this_file[ 0 ];

// required for making tables easier
require_once( $this_file . 'wp-config.php' );

// or an alternative way
// require_once( '../../../wp-config.php' );

global $wpdb;

$aOptions = SofaShoppr::initOptions( false );
$entrycurrency  = $aOptions[ 'entrycurrency' ];
$taxpercentage = $aOptions[ 'taxpercentage' ];
if( $taxpercentage < 0 ) $taxpercentage = 0;
$shipinlandcost = stripslashes( strip_tags( $_POST[ 'ab_shipping_cost' ] ) );

$owner = get_userdata( 1 ); // to admin
$to_admin = $owner->user_email;

$report_message = '';
$output_userdata = '';
$entire_name = '';
$payer_email = '';
$contact_phone = '';
$address_street = '';
$address_zip = '';
$address_city = '';
$entire_country = '';
$payment_amount = '';

// user
$output_userdata .= __( "USER DETAILS", "sofa_shoppr" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';
foreach( $_POST as $key => $value ) {
	if( $key == 'ab_firstname' ) {
		$output_userdata .= __( "First Name", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$entire_name .= $value . ' ';
	}
	if( $key == 'ab_lastname' ) {
		$output_userdata .= __( "Last Name", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$entire_name .= $value;
	}
	if( $key == 'ab_cellphone' ) {
		$output_userdata .= __( "Cell Phone Number", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$contact_phone .= $value . ' | ';
	}
	if( $key == 'ab_phone' ) {
		$output_userdata .= __( "Phone Number", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$contact_phone .= $value;
	}
	if( $key == 'ab_email' ) {
		$output_userdata .= __( "E-mail Address", "sofa_shoppr" ) . ': ' . $value . '<br /><br />';
		$payer_email = $value;
	}
}

// shipping
$output_userdata .= __( "SHIPPING DETAILS", "sofa_shoppr" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';
foreach( $_POST as $key => $value ) {
	if( $key == 'ab_address' ) {
		$output_userdata .= __( "Shipping Address", "sofa_shoppr" ) . ': ' . $value . ' ';
		$address_street .= $value . ' ';
	}
	if( $key == 'ab_address2' ) {
		$output_userdata .= $value . '<br />';
		$address_street .= $value;
	}
	if( $key == 'ab_zipcode' ) {
		$output_userdata .= __( "ZIP Code", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$address_zip .= $value;
	}
	if( $key == 'ab_city' ) {
		$output_userdata .= __( "City", "sofa_shoppr" ) . ': ' . $value . '<br />';
		$address_city .= $value;
	}
	if( $key == 'ab_country' ) {
		$output_userdata .= __( "Country", "sofa_shoppr" ) . ': ' . $value . '<br /><br />';
		$entire_country .= $value;
	}
}

// basket
$output_userdata .= __( "ORDERED ITEMS", "sofa_shoppr" ) . '<br />';
$output_userdata .= '----------------------------------------<br />';

if( isset( $_COOKIE[ 'sofashoppr' ] ) ) {
	$items_sum = 0;
	$ppal_custom = '';
	foreach( $_COOKIE[ 'sofashoppr' ] as $name => $value ) {
		$item_info_extra_array = explode( ':', $value );
		$item_size = '';
		$item_color = '';
		$ppal_custom .= $name . ':' . $value . ',';
		if( $item_info_extra_array[ 2 ] != NULL ) $item_size  = $item_info_extra_array[ 2 ];
		if( $item_info_extra_array[ 3 ] != NULL ) $item_color = $item_info_extra_array[ 3 ];
		$p_o = get_post( $name );
		$custom_fields = get_post_custom( (int)$name );
		$item_price = ( $item_info_extra_array[ 4 ] != NULL ) ? $item_info_extra_array[ 4 ] : $custom_fields[ 'price' ][ 0 ];
		
		if( $item_size != '' && $item_color == '' ) {
			$output_userdata .= stripslashes( strip_tags( $p_o->post_title ) ) . ' | Size: ' . $item_size . ' | Quantity: ' . $item_info_extra_array[ 1 ] . ' | Price: ' . $entrycurrency . ' ' . number_format( $item_price * intval( $item_info_extra_array[ 1 ] ), 2, '.', ',' ) . '<br />';
		} else if( $item_size == '' && $item_color != '' ) {
			$output_userdata .= stripslashes( strip_tags( $p_o->post_title ) ) . ' | Color: ' . $item_color . ' | Quantity: ' . $item_info_extra_array[ 1 ] . ' | Price: ' . $entrycurrency . ' ' . number_format( $item_price * intval( $item_info_extra_array[ 1 ] ), 2, '.', ',' ) . '<br />';
		} else if( $item_size != '' && $item_color != '' ) {
			$output_userdata .= stripslashes( strip_tags( $p_o->post_title ) ) . ' | Size: ' . $item_size . ' | Color: ' . $item_color . ' | Quantity: ' . $item_info_extra_array[ 1 ] . ' | Price: ' . $entrycurrency . ' ' . number_format( $item_price * intval( $item_info_extra_array[ 1 ] ), 2, '.', ',' ) . '<br />';
		} else {
			$output_userdata .= stripslashes( strip_tags( $p_o->post_title ) ) . ' | Quantity: ' . $item_info_extra_array[ 1 ] . ' | Price: ' . $entrycurrency . ' ' . number_format( $item_price * intval( $item_info_extra_array[ 1 ] ), 2, '.', ',' ) . '<br />';	
		}
		
		$items_sum = $items_sum + ( $item_price * $item_info_extra_array[ 1 ] );
		
		$output_userdata .= '<br /><br />';
	}
	
	$output_userdata .= '----------------------------------------<br />';
	
	if( $taxpercentage > 0 ) {
		$bare_tax = ( $items_sum * $taxpercentage ) / 100;
		$items_sum = $items_sum + $bare_tax;
	}
	if( $shipinlandcost > 0 ) {
		$items_sum = $items_sum + $shipinlandcost;
	}
	
	if( $taxpercentage > 0 ) {
		$output_userdata .=  __( "Tax", "sofa_shoppr" ) . '(' . $taxpercentage . '%): ' . $entrycurrency . ' ' . number_format( $bare_tax, 2, '.', ',' ) . '<br />';
	}
	if( $shipinlandcost > 0 ) {
		$output_userdata .= __( "Shipping", "sofa_shoppr" ) . ': ' . $entrycurrency . ' ' . number_format( $shipinlandcost, 2, '.', ',' ) . '<br />';
	}
	
	$output_userdata .= '----------------------------------------<br />';
	
	$output_userdata .= __( "Total", "sofa_shoppr" ) . ': ' . $entrycurrency . ' ' . number_format( $items_sum, 2, '.', ',' ) . '<br />';
	
}

// save data to a DB
$payment_date = current_time( mysql );
$txn_id = time();
$payer_status = 'verified';
$payment_amount = number_format( $items_sum, 2, '.', ',' );
$Q = $wpdb->insert( ( $wpdb->prefix . 'purchases_history' ), array( 'trans_date' => $payment_date, 'unique_code' => $txn_id, 'trans_status' => $payer_status, 'trans_basket' => $ppal_custom, 'user_name' => $entire_name, 'user_email' => $payer_email, 'user_phone' => $contact_phone, 'user_address' => $address_street, 'user_zip' => $address_zip, 'user_city' => $address_city, 'user_country' => $entire_country, 'currency' => $entrycurrency, 'gross' => $payment_amount, 'flag_shipped' => 0 ), array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%d', '%s', '%s', '%s', '%s', '%d' ) );

// notify both site admin and user
if( $Q ) {
	// append to message
	$report_message .= '&raquo; ' . __( "Your Order has been received!", "sofa_opnpress" ) . '<br />';
	
	// send notification
	$subject = __( "Purchase made", "sofa_shoppr" ) . ', ' . get_bloginfo( 'name' );
	$headers = "Content-Type: text/html; charset=utf-8; format=flowed\r\n"
	. "Content-Transfer-Encoding: 8bit\r\n"
	. "MIME-Version: 1.0\r\n"
	. "From: $to_admin\r\n"
	. "Reply-To: $to_admin\r\n"
	. "Return-Path: $to_admin\r\n";
	
	$message = __( "Just to let you know that your Order has been received. Here are the details:", "sofa_shoppr" );
	$message .= '<br /><br />';
	$message .= $output_userdata;
	$message .= '<br /><br />';
	$message .= __( "Thank you very much!", "sofa_shoppr" ) . '<br />' . get_bloginfo( 'url' );

	// send mail to buyer
	if( @mail( $payer_email, $subject, $message, $headers ) ) {
		$report_message .= '&raquo; ' . __( "You may check you mailbox, confirmation message with purchase details has been sent too!", "sofa_opnpress" ) . '<br />';
	} else {
		$report_message .= '&raquo; ' . __( "Confirmation message with purchase details can not be sent, mail server error!", "sofa_opnpress" ) . '<br />';
	}
	
	// send mail to admin
	// $s_adm_arr = explode( '@', $to_admin );
	// $fake_email = 'system@' . $s_adm_arr[ 1 ];
	$subject_admin = __( "New Order received", "sofa_shoppr" ) . ', ' . get_bloginfo( 'name' );
	$headers_admin = "Content-Type: text/html; charset=utf-8; format=flowed\r\n"
	. "Content-Transfer-Encoding: 8bit\r\n"
	. "MIME-Version: 1.0\r\n"
	. "From: $to_admin\r\n"
	. "Reply-To: $to_admin\r\n"
	. "Return-Path: $to_admin\r\n";
	
	$message_admin = __( "Just to let you know that new Order has been placed. Here are the details:", "sofa_shoppr" );
	$message_admin .= '<br /><br />';
	$message_admin .= $output_userdata;

	@mail( $to_admin, $subject_admin, $message_admin, $headers_admin );
	
	// clear cookies?
	foreach( $_COOKIE[ 'sofashoppr' ] as $name => $value ) {
		$itemid = $name;
		setcookie( "sofashoppr[$itemid]", "", time()-36000, '/' );
	}
	
} else {
	// append to message
	$report_message .= '&raquo; ' . __( "Your Order can not be processed, service responded with error!", "sofa_opnpress" ) . '<br />';
}

echo $report_message;
?>