<?php
// get for intelectra geraetezuordnung

/*if ($obj->attribute_17 != '') {
	$sql = "SELECT * FROM import_geraetezuordnung WHERE anr='" . $obj->attribute_17 . "'";
	$result = $this->db->query($sql);
}
else {
	$sql = "SELECT * FROM import_geraetezuordnung WHERE anr='" . $obj->number . "'";
	$result = $this->db->query($sql);
}*/

if ($obj->krempl_id) {
	$sql = "SELECT * FROM krempl_artikelgeraet_22 ka
			left join krempl_geraete_22 kg on kg.id=ka.geraet
			WHERE ka.ersatzteil=" . $this->db->real_escape_string($obj->krempl_id) . " AND not isnull(nr)";
	$result = $this->db->query($sql);
}

$obj->geraetezuordnung = array();
if ($result) {
	while ($obj1 = $result->fetch_object()) {
		$obj->geraetezuordnung[] = $obj1;
	}
}

// get parent warengruppe
$sql = "select i.name, s.name, s2.name as wg from items i
	left join item_structure_assign isa on isa.item_id=i.id
	left join structure s on s.id=isa.structure_id
	left join structure s2 on s2.id=s.parent_id
	where i.id=" . $obj->id .' AND s.active=1 LIMIT 1';
$result = $this->db->query($sql);
$parent_warengruppe = '';
if ($result) {
	while ($obj1 = $result->fetch_object()) {
		$parent_warengruppe = $obj1->wg;
	}
}

$table_data = array();
foreach ($obj->geraetezuordnung as $geraet) {
	$nr1 = str_replace('#', '', $geraet->nr);
	$nr1 = str_replace('_', ' ', $nr1);
	$nr2 = str_replace('#', '', $geraet->produktionsstart);
	$nr2 = str_replace('_', ' ', $nr2);
	$nr3 = str_replace('#', '', $geraet->produktionsende);
	$nr3 = str_replace('_', ' ', $nr3); 
	$nr3 = empty($nr3) ? "" : " - " . $nr3;
	$nr2 = $nr2.$nr3;
	$bezeichnungoriginal = str_replace('#', '', $geraet->bezeichnungoriginal);
	$bezeichnungoriginal = str_replace('_', ' ', $bezeichnungoriginal);
	$einkaufsmoeglichkeitencached = $geraet->einkaufsmoeglichkeitencached . ' ' . $geraet->zusatz;
	$table_data[$einkaufsmoeglichkeitencached][$bezeichnungoriginal][] = array($nr1, $nr2);
}


$html = '<table border="0" cellpadding="2" cellspacing="0">';
foreach ($table_data as $key => $marken) {
	$y = 0;
	$html .= '<tr class="intelectra_item_table_header"><td colspan="3"><h4><b>'.$obj->name.' passend zu ' . $key . ' '.$parent_warengruppe.'</b></h4></tr>';
	$html .= '<tr class="intelectra_item_table_header"><td>Ger&auml;te-/Modellbezeichnung</td><td>E-, Service-, Produktions- oder Hauptnummer</td><td>Zusatz-, F- oder Seriennummer oder Fertigungsdatum</td></tr>';
	foreach ($marken as $geraet => $nummern) {
		$color = 'intelectra_table_row1';
		if ($y % 2 == 1) {
			$color = 'intelectra_table_row2';
		}
		$z = 0;
		foreach ($nummern as $nummer) {
			if ($z == 0) {
				$html .= '<tr class="' . $color . '"><td>' . $geraet . '</td><td>' . $nummer[0] . '</td><td>' . $nummer[1] . '</td></tr>';
			} else {
				$html .= '<tr class="' . $color . '"><td>&nbsp;</td><td>' . $nummer[0] . '</td><td>' . $nummer[1] . '</td></tr>';
			}
			$z++;
		}
		$y++;
	}
}
$html .= '</table>';
$obj->geraetezuordnung = $html;



// base price calc
if ($obj->attribute_11 && $obj->attribute_15) {
	$obj->sample_unit = $obj->attribute_15;
	if (strstr($obj->sample_unit, '100')) {
		$obj->sample_price = $obj->shop_price_1 / ($obj->attribute_11 * 10);
	} else {
		$obj->sample_price = $obj->shop_price_1 / $obj->attribute_11;
	}
}