<?php
/**
* @package      Easyway Shop
* @copyright    Copyright (C) 2005 - 2011 TA-EDV
* @license      proprietary
* @author       John Daly <jd@ta-edv.de>
* Easyway Shop is a web shop system
*/

include_once './core/paypal/transaction.class.php';
include_once './core/order.class.php';
include_once './core/orderhelper.class.php';
include_once './core/paypalrefund.class.php';
include_once './modules/admin_order_actions.php';

class admin_paypal_transaction { 
	
	private $base_object;
	private $layout_object;
	private $order_object;
	private $transaction_object;
	private $paypal_refund_object;
	private $order_actions_object;
	
	function __construct($base_object, $layout_object) {
		$this->base_object = $base_object;
		$this->layout_object = $layout_object;
		$this->order_object = new Order($base_object);
		$this->transaction_object = new Transaction();
		$this->paypal_refund_object = new PaypalRefund($base_object);
		$this->order_actions_object = new admin_order_actions($base_object, $layout_object);
	}
	
	function run() {
		if (isset($_GET['action'])) {
			$action = $_GET['action'];
		} else {
			$action = false;
		}
		
		if (isset($_GET['order_id'])) {
			$order_id = $_GET['order_id'];
		} else {
			$order_id = false;
		}
		
		if ($action == 'submit') {
			$this->save($id);
		} else {
			return $this->edit($order_id);
		}
	}
	
	private function edit($order_id) {
		if ($order_id) {
			
			$this->layout_object->assign('this_order', $this->order_actions_object->get_order_talbe($order_id));
			
			// get the order in question
			$this->order_object->set_id($order_id);
			$order_data = $this->order_object->get_data();
			
			// get the sum of the items that can't be delivered
			$order_data->item_refund = OrderHelper::get_money_back($order_id);
			
			// get the previous order
			$this->order_object->set_id($order_data->revised_order_id);
			$order_data->prev_order = $this->order_object->get_data();
			
			$this->layout_object->assign('old_order', $this->order_actions_object->get_order_talbe($order_data->revised_order_id));
			
			// assign the order data
			$this->layout_object->assign('order_data', $order_data);
			
			// get the data from paypal
			$transaction_data = $this->transaction_object->get_transaction_details($order_id);
			
			if ($transaction_data) {
				$this->layout_object->assign('transaction_data', $transaction_data);
			} else {
				$this->layout_object->assign('no_transaction_data', true);
			}
		} else {
			$this->layout_object->assign('no_order_id', true);
		}
		
		return $this->layout_object->fetch('admin_paypal_transaction.tpl');
	}
	
	private function save() {
		$data = $this->clean_data($_POST);
		
		$is_valid = $this->validate($data);
		
		$refund_id = ($this->paypal_refund_object->create($data));
		
		$transaction_data = $this->transaction_object->refund_transaction($return);
		
		echo "<pre>";
		print_r($transaction_data);
		exit();
		
/*		
		if ($is_valid) {
			$refund_id = ($this->paypal_refund_object->create($data));
			
			if ($refund_id) {
				$transaction_data = $this->transaction_object->refund_transaction($return);
				
				if ($transaction_data) {
					$this->paypal_refund_object->update($transaction_data, $id);
					header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul');
				} else {
					echo "no transaction data";
					//header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_paypal_transaction&order_id=' . $data['order_id']);
				}
			} else {
				echo "no refund id";
				//header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_paypal_transaction&order_id=' . $data['order_id']);
			}
		} else {
			echo "data is not valid";
			//header('location: http://' . $_SERVER["SERVER_NAME"] . '/index.php?admin_modul=admin_paypal_transaction&order_id=' . $data['order_id']);
		}
*/
		exit();
	}
	
	private function clean_data($data) {
		if (isset($data['order_total'])) {
			$data['order_total'] = $this->parse_number($data['order_total']);
		}
		
		if (isset($data['item_refund'])) {
			$data['item_refund'] = $this->parse_number($data['item_refund']);
		}
		
		if (isset($data['shipping_charges'])) {
			$data['shipping_charges'] = $this->parse_number($data['shipping_charges']);
		}
		
		if (isset($data['refund_amount'])) {
			$data['refund_amount'] = $this->parse_number($data['refund_amount']);
		}
		
		return $data;
	}
	
	private function parse_number($num) {
		$num = str_replace(',', '.', $num);
		$num = str_replace(' ', '', $num);
		
		return $num;
	}
	
	private function validate($data) {
		if ($data['refund_type'] == '1') {
			return true;
		}
		
		return is_numeric($data['refund_amount']);
	}
}