<?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 DeliveryItemsHTML {

	private $smarty;
	private $order;
	private $items;
	private $footer;

	public function __construct($order) {
		$this->smarty = Registry::get('layout_object');
		$this->order = $order;
		$this->MakeItems();
		$this->MakeFooter();
	}

	public function PrintOrder() {
		$this->smarty->assign('order_items', $this->items);
		$this->smarty->assign('showtaxes', $this->order->cg_show_taxes);
		$this->smarty->assign('footer', $this->footer);
		$this->smarty->assign('order_data', $this->order);
		return $this->smarty->fetch(ROOT_DIR.'/core/output/templates/order_items.tpl');
	}

	private function MakeFooter() {
		$footer = new stdClass();

		$footer->sub = 0.0;
		$footer->sub_with_taxes = 0.0;
		$footer->taxes = array();
		$footer->shipping_charges = $this->order->shipping_charges;
		$footer->payment_method_charges = $this->order->payment_method_charges;

		foreach ($this->order->items as $item) {
			$footer->sub += $item->total;
			$footer->sub_with_taxes += $item->total_with_taxes;

			if (isset($footer->taxes[$item->tax_rate])) {
				$footer->taxes[$item->tax_rate] += $item->total_taxes;
			} else {
				$footer->taxes[$item->tax_rate] = $item->total_taxes;
			}

			if (isset($footer->taxes_netto_sum[$item->tax_rate])) {
				$footer->taxes_netto_sum[$item->tax_rate] += $item->total;
			} else {
				$footer->taxes_netto_sum[$item->tax_rate] = $item->total;
			}
		}

		if ($this->order->is_eu_order != 1) {
			//$footer->order_total = $footer->sub_with_taxes + $footer->shipping_charges + $footer->payment_method_charges;
			// brutto
			if ($this->order->cg_show_taxes) {
				$footer->order_total = $footer->sub_with_taxes + $footer->shipping_charges + $footer->payment_method_charges;
			} else { // netto
				if ($footer->shipping_charges > 0) {
					$footer->shipping_charges = $this->order->shipping_charges / 119.0 * 100.0;
				}
				if ($footer->payment_method_charges > 0) {
					$footer->payment_method_charges = $this->order->payment_method_charges / 119.0 * 100.0;
				}
				$footer->order_total = $footer->sub + $footer->shipping_charges + $footer->payment_method_charges;
				$footer->order_total = $footer->order_total / 100 * 119;
				$footer->taxes['19.00'] = ($footer->taxes_netto_sum['19.00'] + $footer->shipping_charges + $footer->payment_method_charges) / 100 * 19;
			}
		} else {
			if ($footer->shipping_charges > 0) {
				$footer->shipping_charges = $this->order->shipping_charges / 119.0 * 100.0;
			}
			if ($footer->payment_method_charges > 0) {
				$footer->payment_method_charges = $this->order->payment_method_charges / 119.0 * 100.0;
			}
			$footer->order_total = $footer->sub + $footer->shipping_charges + $footer->payment_method_charges;
		}

		// gift certificates
		if ($this->order->gift_certificate_id) {
			$footer->gift_certificate_id = $this->order->gift_certificate_id;
			$footer->gift_certificate_code = $this->order->gift_certificate_code;
			$footer->gift_certificate_total = $this->order->gift_certificate_total;
		}

		$this->footer = $footer;
	}

	private function MakeItems() {
		$items = array();

		foreach ($this->order->items as $item) {

			$item->price_with_taxes = $item->price + $item->taxes;
			$item->total = $item->price * $item->amount;
			$item->total_with_taxes = $item->price_with_taxes * $item->amount;
			$item->total_taxes = $item->taxes * $item->amount;
			/*if (isset($item->attribute_array[0])) {
				$item->attribute_1_text = $item->attribute_array[0]->name.': '.$item->attribute_array[0]->value;
			}

			if (isset($item->attribute_array[1])) {
				$item->attribute_2_text = $item->attribute_array[1]->name.': '.$item->attribute_array[1]->value;
			}*/
			if (isset($item->attribute_array[0]) && isset($item->attribute_array[0]->variant)) {
				$item->attribute_1_text = $item->attribute_array[0]->name.': '.$item->attribute_array[0]->value;
			}

			if (isset($item->attribute_array[1]) && isset($item->attribute_array[1]->variant)) {
				$item->attribute_2_text = $item->attribute_array[1]->name.': '.$item->attribute_array[1]->value;
			}

			// intelectra start
			$db = Registry::get('base')->db;
			$sql = "SELECT
					attribute_7
				FROM
					items
				WHERE
					id=".$db->real_escape_string($item->item_id);
			$result = $db->query($sql);
			$item->attribute_7 = '';
			if ($result->num_rows > 0) {
				$item->attribute_7 = $result->fetch_object()->attribute_7;
			}

			//intelectra end

			$items[] = $item;
		}

		$this->items = $items;
	}
}