<?php
/*
 * @version			$Id: index.php 10381 2008-06-01 03:35:53Z $
 * @package			Carteasy
 * @copyright		Copyright (C) 2005 - 2011 Wlanium
 * @license			proprietary
 * @author			Thomas Bartelt
 * Carteasy is a web shop system
 */

include_once './core/countryhelper.class.php';

class PaymentHelper {

	//=========================================
	//
	//	THIS CLASS IS STATIC ONLY
	//
	//=========================================
	private function __construct() {}

	private function __clone() {}

	//=========================================
	//
	//	THE PUBLIC INTERFACE
	//
	//=========================================

	public static function getActivePaymentMethodsByCountry($country_id) {
	//public static function getActivePaymentMethodsByCountry($country_id, $default_vat_value, $order_items_value, $customer_group_id) {
		/*echo "country = ".$country_id;
		echo "<br /> default vat = ".$default_vat_value;
		echo "<br /> order items value = ".$order_items_value;
		echo "<br /> customer group = ".$customer_group_id;
		exit();*/

		$helperjoins = PaymentHelper::get_helper_joins();
		$nametoid = PaymentHelper::get_name_to_id_array();
		$shipping_area_id = CountryHelper::get_shipping_area_by_country_id($country_id);
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM shipping_area WHERE id=".$shipping_area_id;

		$result = $db->query($sql);

		if ($result->num_rows > 0 && $helperjoins) {
			$return = array();

			while ($obj = $result->fetch_object()) {
				foreach ($helperjoins as $name => $short) {
					$active = 'active_'.$short;
					$type = 'account_type_'.$short;
					$add = 'add_'.$short;

					if ($obj->$active) {
						$return[] = array('name' => $name, 'id' => $nametoid[$name]);
					}

				}
			}

			return $return;
		}

		return false;
	}

	public static function getPaymentDataByIdAndCountry($method_id, $country_id, $default_vat_value, $order_items_value, $customer_group_id = false) {
		/*echo "<pre>";
		print_r($method_id);
		exit();*/

		$paymentObject = new stdClass();

		$paymentObject->methodId = $method_id;

		$db = Registry::get('base')->db;

		// Helper join key
		$helperjoinkey = $db->query("
			SELECT
				helperjoinkey
			FROM
				payment_methods
			WHERE
				id = '".$db->real_escape_string($method_id)."'
		")->fetch_object()->helperjoinkey;

		// Area
		$area_id = $db->query("
			SELECT
				shipping_area_id
			FROM
				countries
			WHERE
				id = '".$db->real_escape_string($country_id)."'
		")->fetch_object()->shipping_area_id;

		if ($customer_group_id) {
			$sql = "SELECT * FROM country_customergroup_shippingarea
				WHERE country_id = ".$country_id." AND customer_group_id = $customer_group_id";
			$result2 = $db->query($sql);
			if ($result2->num_rows > 0) {
				$area_id = $result2->fetch_object()->shipping_area_id;
			}
		}

		$paymentObject->areaId = $area_id;

		// Costs
		$sql = "
			SELECT
				add_".$helperjoinkey." AS num,
				account_type_".$helperjoinkey." AS type
			FROM
				shipping_area
			WHERE
				id = '".$db->real_escape_string($area_id)."'";

		$PM = $db->query($sql)->fetch_object();

		if ($PM->type == '0') {
			$price = $PM->num;
			$paymentObject->priceGross = $price;
			$paymentObject->priceNet = round($price / (100 + $default_vat_value) * 100, 2);
			$paymentObject->priceVat = $paymentObject->priceGross - $paymentObject->priceNet;
			$paymentObject->vatRate = $default_vat_value;
		} else if ($PM->type == '1') {
			$price = $PM->num;
			$paymentObject->priceGross = $price;
			$paymentObject->priceNet = round($price / (100 + $default_vat_value) * 100, 2);
			$paymentObject->priceVat = $paymentObject->priceGross - $paymentObject->priceNet;
			$paymentObject->vatRate = $default_vat_value;

			$paymentObject->priceGross = -$paymentObject->priceGross;
			$paymentObject->priceNet = -$paymentObject->priceNet;
			$paymentObject->priceVat = -$paymentObject->priceVat;
		} else if ($PM->type == '2') {
			$price = $PM->num / 100 * $order_items_value;
			$paymentObject->priceGross = $price;
			$paymentObject->priceNet = round($price / (100 + $default_vat_value) * 100, 2);
			$paymentObject->priceVat = $paymentObject->priceGross - $paymentObject->priceNet;
			$paymentObject->vatRate = $default_vat_value;
		} else if ($PM->type == '3') {
			$price = $PM->num / 100 * $order_items_value;
			$paymentObject->priceGross = $price;
			$paymentObject->priceNet = round($price / (100 + $default_vat_value) * 100, 2);
			$paymentObject->priceVat = $paymentObject->priceGross - $paymentObject->priceNet;
			$paymentObject->vatRate = $default_vat_value;

			$paymentObject->priceGross = -$paymentObject->priceGross;
			$paymentObject->priceNet = -$paymentObject->priceNet;
			$paymentObject->priceVat = -$paymentObject->priceVat;
		}

		return $paymentObject;
	}

	public static function get_payment_methods_by_shipping_area_array() {
		$db = Registry::get('base')->db;

		$helperjoins = PaymentHelper::get_helper_joins();

		$sql = "SELECT * FROM shipping_area";

		$result = $db->query($sql);

		if ($result->num_rows > 0 && $helperjoins) {
			while ($obj = $result->fetch_object()) {
				foreach ($helperjoins as $name => $short) {
					$active = 'active_'.$short;
					$type = 'account_type_'.$short;
					$add = 'add_'.$short;

					$data[$obj->id][] = array('name' => $name, 'active' => $obj->$active, 'type' => $obj->$type, 'add' => $obj->$add);
				}
			}

			return $data;
		}

		return false;
	}

	//=========================================
	//
	//	THE PRIVATE INTERFACE
	//
	//=========================================

	private static function get_helper_joins() {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM payment_methods";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$data[$obj->name] = $obj->helperjoinkey;
			}

			return $data;
		}

		return false;
	}



	private static function get_name_to_id_array() {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM payment_methods";

		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$data[$obj->name] = $obj->id;
			}

			return $data;
		}

		return false;
	}
}