<?php
$today = getdate();
$today = $today['mday'].'.'.$today['mon'].'.'.$today['year'];
$data_array[] = array('Fachhandelspreisliste (Preise ohne Gewähr)', 'vom '.$today, '| Intelectra GmbH', 'Sachsenring 48', '94315 Straubing', 'Tel. +49 (0)9421-62015', 'Fax +49 (0)9421-72677');
$data_array[] = array('Best.Nr.', 'Bezeichnung', 'Beschreibung', 'Preis 1 St. (ohne MwSt.)', 'Preis ab 3 St. (ohne MwSt.)', 'Preis ab 100 St. (ohne MwSt.)', 'UVP ohne MwSt.');

$sql = "SELECT item_id, price_1 FROM item_prices WHERE customergroup_id=1";
$res = $this->db->query($sql);
$item_uvp = array();
while ($o = $res->fetch_object()) {
	$item_uvp[$o->item_id] = $o->price_1;
}

foreach ($items as $article_obj) {
	$price = '';
	$price2 = 0;
	$price3 = 0;

	if (isset($article_obj->bargain_price_1) && $article_obj->bargain_price_1 > 0 && $article_obj->bargain_price_type == 1) {
		$price = $article_obj->bargain_price_1;
	} else if (isset($article_obj->bargain_price_1) && $article_obj->bargain_price_1 > 0 && $article_obj->bargain_price_type == 2) {
		$today = getdate();
		$today = $today['year'].'-'.$today['mon'].'-'.$today['mday'];
		$sql = "SELECT item_types.item_type_id
				FROM item_types 
				LEFT JOIN item_type_definitions ON item_type_definitions.item_type_definition_id=item_types.item_type_definition_id
				WHERE item_id =".$this->db->real_escape_string($article_obj->id).
				" AND item_type_available_from <= '".$this->db->real_escape_string($today)."'
				AND item_type_available_until >= '".$this->db->real_escape_string($today)."'
				AND item_type_definitions.item_type_definition_type=1
				AND item_type_definitions.item_type_definition_is_active=1";
		$result = $this->db->query($sql);
		$found = $result->fetch_array();
		if ($found) {
			$price = $article_obj->bargain_price_1;
		} else {
		}
		$price = $article_obj->price_1;
	} else {
		$price = $article_obj->price_1;
	}

	if (isset($article_obj->bargain_price_2) && $article_obj->bargain_price_2 > 0 && $article_obj->bargain_price_type == 1) {
		$price2 = $article_obj->bargain_price_2;
	} else {
		$price2 = $article_obj->price_2;
	}

	if (isset($article_obj->bargain_price_3) && $article_obj->bargain_price_3 > 0 && $article_obj->bargain_price_type == 1) {
		$price3 = $article_obj->bargain_price_3;
	} else {
		$price3 = $article_obj->price_3;
	}

	$price = number_format($price,2,',','').' EUR';
	$price_2 = '';
	$price_3 = '';
	$uvp = '';
	if ($price2 && $article_obj->quantity_2 == 3) {
		$price_2 = number_format($price2,2,',','').' EUR';
	} else if ($price2 && $article_obj->quantity_2 == 100) {
		$price_2 = number_format($price3,2,',','').' EUR';
	}

	if ($price3 && $article_obj->quantity_3 == 100) {
		$price_3 = number_format($price3,2,',','').' EUR';
	} else if ($price2 && $article_obj->quantity_3 == 3) {
		$price_3 = number_format($price2,2,',','').' EUR';
	}

	if (isset($item_uvp[$article_obj->id])) {
		$uvp = number_format($item_uvp[$article_obj->id],2,',','').' EUR';
	}

	if ($article_obj->shippable) {
		$data_array[] = array(
			// Item number
			csv_string($article_obj->number),
			// Item name
			csv_string($article_obj->name),
			// Short description
			csv_string($article_obj->short_description),
			// preis ein stueck
			csv_string($price),
			// staffelpreis ab 3 stueck
			csv_string($price_2),
			// staffelpreis ab 100 stueck
			csv_string($price_3),
			// uvp
			$uvp
		);
	}
}

function csv_string ($string) {
	$string = trim($string);
	$string = strip_tags($string);
	$string = str_replace(array('&auml;', '&uuml;', '&ouml;', '&Auml;', '&Uuml;', '&Ouml;', '&szlig;', '&euro;', '&amp;'), array('ä', 'ü', 'ö', 'Ä', 'Ü', 'Ö', 'ß', '€', '&'), $string);
	$string = str_replace(array(';', "\r", "\n"), array(',', ' ', ' '), $string);

	return $string;
}