<?php

$data_array = array();

$image_url = 'http://'.$_SERVER['HTTP_HOST'].'/web/'.SHOP_SYSTEM.'/images/article/';

$shippability_array = array(
	'0' => 'nicht lieferbar',
	'1' => 'lieferbar',
	'2' => 'lieferbar ab (bis)',
	'3' => 'lieferzeit unbekannt',
	'4' => 'Bestellartikel (Lieferzeit 2-3 Tage)',
	'5' => 'Bestellartikel (Lieferzeit 14 Tage)',
	'6' => 'Bestellartikel'
);

foreach ($items as $article_obj) {
	if (isset($article_obj->shop_bargain_price_1) && $article_obj->shop_bargain_price_1 > 0) {
		$price = $article_obj->shop_bargain_price_1;
	} else {
		$price = $article_obj->shop_price_1;
	}

	if (isset($article_obj->sample_price) && $article_obj->sample_price != 0.0000 ) {
		$price_per_unit = number_format($article_obj->sample_price, 2, ',', '').' € pro '.$article_obj->sample_unit;
	} else {
		$price_per_unit = '';
	}

	if ($article_obj->default_image_file_name == 'leer.gif' || $article_obj->default_image_file_name == '') {
		$item_image = '';
	} else {
		$item_image = $image_url.$article_obj->default_image_file_name;
	}

	if (isset($article_obj->uvp) && $article_obj->uvp != 0.00) {
		$uvp = number_format($article_obj->uvp, 2, ',', '');
	} else {
		$uvp = '';
	}

	$shippable = $shippability_array[$article_obj->shippable];

	if ($article_obj->shippable == 2) {
		if (isset($article_obj->available_from) && $article_obj->available_from != '0000-00-00') {
			$shippable .= ' '.$article_obj->available_from;

			if (isset($article_obj->available_till) && $article_obj->available_till != '0000-00-00') {
				$shippable .= ' '.$article_obj->available_till;
			}
		}
	}

	$data_array[] = array(
		// OLD 0 A Kundennummer
		$this->customer_number,
		// OLD 1 B Interne Nummer
		$article_obj->number,
		// OLD  2 C Artikel-Nummer / NEW Manufacturer name
		csv_string($article_obj->manufacturer_name),
		// OLD  3 D Artikel-Bezeichnung / NEW Manufacturer number
		$article_obj->manufacturer_item_number,
		// OLD  4 E Preis (inkl. Rabatte, exkl. Mehrwertsteuer) / NEW Artikel Name
		csv_string($article_obj->name),
		// OLD  5 F Artikelbeschreibung
		csv_string($article_obj->short_description),
		// OLD  6 G Mehrwertsteur-Satz-ID / NEW Mehrwertsteuer
		$article_obj->tax_rate,
		// OLD  7 H Groesse; Menge ... / NEW Preis (inkl. Rabatte, exkl. Mehrwertsteuer)
		number_format($price, 2, ',', ''),
		// OLD  8 I Gewicht; Menge ... / NEW UVP
		$uvp,
		// OLD  9 J EAN-Code
		$article_obj->ean_code,
		// OLD 10 K Reserviert
		$price_per_unit,
		// OLD 11 L Reserviert // New Lieferstatus
		$shippable,
		// OLD 12 M Bildname
		$item_image,
		// OLD 13 N Reserviert
		csv_string($article_obj->structure_name),
		// OLD 14 O Reserviert
		'',
		// OLD 15 P Reserviert
		'',
		// OLD 16 Q Reserviert
		'',
		// OLD 17 R Reserviert
		'',
		// OLD 18 S Reserviert
		'',
		// OLD 19 T Reserviert
		'',
		// OLD 20 U Reserviert
		''
	);
}

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