<?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/customerhelper.class.php';
include_once './core/customer_group.class.php';
include_once './core/customergroups.class.php';

class GiftCertificateHelper { 

	private static $certificate_error_number = 0;

	private static $certificate_error_array = array(
		1 => array('en' => 'Certificate is not active', 'de' => 'Dieser Gutschein ist nicht g&uuml;ltig.'),
		2 => array('en' => 'Certificate not active yet, too early', 'de' => 'Dieser Gutschein ist nicht Aktiv.'),
		3 => array('en' => 'Certificate expired, too late', 'de' => 'Dieser Gutschein ist abgelaufen.'),
		4 => array('en' => 'Order amount to small for certificate', 'de' => 'Die Kaufsumme ist zu gering f&uuml;r diesen Gutschein.'),
		5 => array('en' => 'Group is not eligible', 'de' => 'Dieser Gutschein ist nicht f&uuml;r Ihre Gruppe g&uuml;ltig.'),
		6 => array('en' => 'Certificate was already used', 'de' => 'Dieser Gutschein wurde schon eingel&ouml;st.'),
		7 => array('en' => 'Certificate does not exist', 'de' => 'Dieser Gutschein existiert nicht.')
	);

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

		$sql = "SELECT * FROM gift_certificates WHERE id=".$db->real_escape_string($id);

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

		if ($result->num_rows > 0) {
			return $result->fetch_object()->account_type;
		}

		return false;
	}

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

		$sql = "SELECT id FROM gift_certificates WHERE code='".$db->real_escape_string($code)."'";

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

		if ($result->num_rows > 0) {
			return $result->fetch_object()->id;
		}

		return false;
	}

	public static function get_certificate_value($code, $total, $group_id) {
		$base = Registry::get('base');
		$customer_groups = new CustomerGroups($base);
		$show_taxes = $customer_groups->get_data($group_id)->show_tax;
		$gift = GiftCertificateHelper::get_certificate_by_code($code);
		$gift_cert = new stdClass();

		if ($gift->account_type == '1') {
			$gift_cert->with_vat = number_format($gift->account, 2);
		} else if ($gift->account_type == '2') {
			$gift_cert->with_vat = (float)($total * $gift->account) / 100;
		}

		$gift_cert->without_vat = round($gift_cert->with_vat / (100 + $base->config->shopConfiguration['default_vat_value']) * 100, 2);
		$gift_cert->vat = $gift_cert->with_vat - $gift_cert->without_vat;

		if ($show_taxes) {
			$gift_cert->show = $gift_cert->with_vat;
		} else {
			$gift_cert->show = $gift_cert->without_vat;
		}

		return $gift_cert;
	}

	public static function code_is_valide($customer_id, $code, $total) {
		$certificate = GiftCertificateHelper::get_certificate_by_code($code);

		if ($certificate) {
			GiftCertificateHelper::$certificate_error_number = 0;
			GiftCertificateHelper::certificate_is_active($customer_id, $certificate, $total);

			if (GiftCertificateHelper::$certificate_error_number) {
				return false;
			}

			return true;
		} else {
			GiftCertificateHelper::$certificate_error_number = 7;
		}

		return false;
	}

	public static function get_error_text($lang = false) {
		if (!$lang) {
			$lang = 'de';
		}

		return GiftCertificateHelper::$certificate_error_array[GiftCertificateHelper::$certificate_error_number][$lang];
	}

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

		$sql = "SELECT * FROM gift_certificates WHERE code='".$db->real_escape_string($code)."'";

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

		if ($result->num_rows > 0) {
			return $result->fetch_object();
		}

		return false;
	}

	public static function certificate_is_active($customer_id, $certificate, $total) {
		if ($certificate == '0') {
			GiftCertificateHelper::$certificate_error_number = 1;

			return false;
		}

		$today = getdate();
		$today = strtotime($today['year'].'-'.$today['mon'].'-'.$today['mday']);

		$startDate = strtotime($certificate->available_from);

		$stopDate = strtotime($certificate->available_until);

		if (!($startDate <= $today && $today <= $stopDate)) {
			if ($today < $startDate) {
				GiftCertificateHelper::$certificate_error_number = 2;
			}
			if ($stopDate < $today) {
				GiftCertificateHelper::$certificate_error_number = 3;
			}

			return false;
		}

		if (is_numeric($certificate->min_order_sum)) {
			$min_order = (double)$certificate->min_order_sum;

			if ($min_order > $total) {
				GiftCertificateHelper::$certificate_error_number = 4;

				return false;
			}
		}

		// checks customer group
		if (!(GiftCertificateHelper::customer_group_is_eligable($customer_id, $certificate))) {
			GiftCertificateHelper::$certificate_error_number = 5;

			return false;
		}

		if ($certificate->customer_valid_type == '1') {
			$certificatWasUsed = GiftCertificateHelper::customer_used_certificate($customer_id, $certificate);
			if ($certificatWasUsed) {
				GiftCertificateHelper::$certificate_error_number = 6;

				return false;
			}
		}

		return true;
	}

	public static function customer_group_is_eligable($customer, $certificate) {
		if ($certificate->customer_group_id == 0) {
			return true;
		} else {
			$customer_group_id = CustomerHelper::get_customer_group_id_by_customer_id($customer);

			if ($customer_group_id == $certificate->customer_group_id) {
				return true;
			}

		}

		return false;
	}

	public static function customer_used_certificate($customer, $certificate) {
		$db = Registry::get('base')->db;

		$sql = "SELECT * FROM orders 
				WHERE
					customer_id=".$db->real_escape_string($customer)."
				AND
					gift_certificate_id=".$db->real_escape_string($certificate->id);

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

		if ($result->num_rows > 0) {
			return true;
		}

		return false;
	}
}

?>