<?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
 */

class DelivererHelper {

	//=========================================
	//
	//	THIS CLASS IS STATIC ONLY
	//
	//=========================================

	public static $get_deliverers_by_country_error_var = '';

	private function __construct() {}

	private function __clone() {}

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

	// TODO: do this with a triple join
	public static function get_deliverers_by_country_error() {
		return DelivererHelper::$get_deliverers_by_country_error_var;
	}

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

		$sql = "SELECT shipping_area_id FROM countries WHERE id = ".$db->real_escape_string($country_id);

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

		if ($result->num_rows > 0) {
			$shipping_area_id = $result->fetch_object()->shipping_area_id;

			$sql = "SELECT deliverer_id
					FROM shipping_area_deliverers
					WHERE shipping_area_id = ".$db->real_escape_string($shipping_area_id).
					" AND active = 1";

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

			if ($result->num_rows > 0) {
				$deliverer_ids = '';
				while ($obj = $result->fetch_object()) {
					if ($deliverer_ids == '') {
						$deliverer_ids = $obj->deliverer_id;
					} else {
						$deliverer_ids .= ', '.$obj->deliverer_id;
					}
				}

				$sql = "SELECT *
						FROM deliverers
						WHERE id IN (".$db->real_escape_string($deliverer_ids).")
						and active = 1";

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

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

					return $return;
				}

				return false;
			}
			DelivererHelper::$get_deliverers_by_country_error_var = 'no shipping area defined for country';

			return false;
		}
		DelivererHelper::$get_deliverers_by_country_error_var = 'country not defined';

		return false;
	}

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

		$sql = "SELECT shipping_area_id
				FROM countries
				WHERE id = ".$db->real_escape_string($country_id);

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

		if ($result->num_rows > 0) {
			$shipping_area_id = $result->fetch_object()->shipping_area_id;

			$sql = "SELECT
						sad.price_add AS price,
						d.*
					FROM
						shipping_area_deliverers sad
					JOIN
						deliverers d
					ON
						sad.deliverer_id = d.id
					WHERE
						sad.shipping_area_id = ".$db->real_escape_string($shipping_area_id).
					" AND sad.active = 1";

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

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

				return $return;
			}

			return false;
		}

		return false;
	}

	public static function getDeliveryDataByIdAndCountry($method_id, $country_id, $default_vat_value, $customer_group_id = false) {
		$shippingObject = new stdClass();

		$shippingObject->methodId = $method_id;

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

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

		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;
			}
		}

		$shippingObject->areaId = $area_id;

		// Costs
		$result = $db->query("
			SELECT
				price_add
			FROM
				shipping_area_deliverers
			WHERE
				shipping_area_id = '".$db->real_escape_string($area_id)."'
			AND
				deliverer_id = '".$db->real_escape_string($method_id)."'
		");
		$obj = $result->fetch_object();
		$price = $obj ? $obj->price_add : 0;

		$shippingObject->priceGross = $price;
		$shippingObject->priceNet = round($price / (100 + $default_vat_value) * 100, 2);
		$shippingObject->priceVat = $shippingObject->priceGross - $shippingObject->priceNet;
		$shippingObject->vatRate = $default_vat_value;

		return $shippingObject;
	}

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

		$sql = "SELECT
					d.name,
					sad.*
				FROM
					deliverers d
				JOIN
					shipping_area_deliverers sad
				ON
					d.id = sad.deliverer_id
				WHERE
					d.active = 1";

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

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				if ($obj->active == '1') {
					$deliverers[$obj->shipping_area_id][$obj->deliverer_id] = array('name' => $obj->name, 'price' => $obj->price_add, 'cost_type' => $obj->deliverer_cost_type, 'bulk_goods_price_add' => $obj->bulk_goods_price_add, 'shipping_free_price' => $obj->shipping_free_price);
				}
			}

			return $deliverers;
		}

		return false;
	}

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

}

?>