<?php

// get items intelectra
$sql = "SELECT attribute_7, number FROM items";
$result = $this->db->query($sql);
$item_numbers = array();
while ($obj = $result->fetch_object()) {
	$item_numbers[$obj->number] = 1;
	$item_numbers[$obj->attribute_7] = 1;
}

$row = 0;
if (($handle = fopen(ROOT_DIR.'tmp/import/'.basename($_FILES['Filedata']['name']), "r")) !== FALSE) {
	while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
		$row++;
		if ($row > 1) {
			if (strlen($data[0]) == 5) {
				$data[0] = '0'.$data[0];
			} else if (strlen($data[0]) == 4) {
				$data[0] = '00'.$data[0];
			} else if (strlen($data[0]) == 3) {
				$data[0] = '000'.$data[0];
			} else if (strlen($data[0]) == 2) {
				$data[0] = '0000'.$data[0];
			}

			if (!isset($item_numbers[$data[0]])) {
				$short_uri = $data[2].'-'.$data[0];
				$short_uri = str_replace(array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß'), array('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss'), $short_uri);
				$short_uri = preg_replace("@[^0-9aA-zZ\-]@si", "-", $short_uri);
				$short_uri = preg_replace("@(-{2,})@si", "-", $short_uri);
				$new_uri = '';
				$old_uri = $short_uri;
				$i = 0;
				while ($new_uri == '') {
					$sql = "SELECT id FROM items WHERE short_uri = '$short_uri'";
					$result = $this->db->query($sql);
					if ($result->num_rows == 0) {
						$new_uri = $short_uri;
					} else {
						$i++;
						$short_uri = $old_uri.'_'.$i;
					}
				}

				$item_id = 0;
				// add item
				$sql = "
					INSERT INTO items
					SET
						number='".$this->db->real_escape_string(utf8_encode($data[0]))."',
						attribute_7='".$this->db->real_escape_string(utf8_encode($data[1]))."',
						attribute_10='".$this->db->real_escape_string('Stück')."',
						shippable=1,
						shippable_inventory=1,
						active=1,
						manufacturer_id=1,
						schema_id=3,
						price_type=1,
						base_price=0.0,
						bargain_price_type=0,
						seo_keywords='',
						tax_id=1,
						min_quantity=1,
						buy_quantity=1,
						structure_id=10682,
						shipping_cost_type=0,
						short_uri=\"".$this->db->real_escape_string($short_uri)."\",
						google_product_category='Heim & Garten',
						short_description=\"".$this->db->real_escape_string(utf8_encode($data[2]))."\",
						detail_description=\"".$this->db->real_escape_string(utf8_encode($data[2]))."\",
						name=\"".$this->db->real_escape_string(utf8_encode($data[2]))."\"
				";



				$this->db->query($sql);
				$item_id = $this->db->insert_id;
				if ($this->db->error) {
					$log_object->info('Error in /core/import_class', $this->db->error.', sql:'.$sql);
				}

				if ($item_id) {
					// add item to group (10682 and 9928)
					$sql3 = "INSERT INTO item_structure_assign SET structure_id=10682, item_id=".$item_id;
					$this->db->query($sql3);
					$sql3 = "INSERT INTO item_structure_assign SET structure_id=9928, item_id=".$item_id;
					$this->db->query($sql3);
					if ($this->db->error) {
						$log_object->info('Error in /core/import_class', $this->db->error.', sql:'.$sql3);
					}

					// add price endkunde
					$sql1 = "INSERT INTO item_prices SET price_1='".str_replace(',','.',$data[4])."', quantity_2=0, price_2='0', quantity_3=0, price_3='0', customergroup_id=1, item_id=".$item_id;
					$this->db->query($sql1);
					if ($this->db->error) {
						$log_object->info('Error in /core/import_class', $this->db->error.', sql:'.$sql1);
					}

					// add price haendler
					$sql2 = "INSERT INTO item_prices SET price_1='".str_replace(',','.',$data[3]);
					$sql2 .= "', quantity_2=0, price_2='0";
					$sql2 .= "', customergroup_id=101, item_id=".$item_id;
					$this->db->query($sql2);
					if ($this->db->error) {
						$log_object->info('Error in /core/import_class', $this->db->error.', sql:'.$sql2);
					}
				}
			} else {
				$log_object->info('Can not import new article with existing number!','Item with number '.$data[0].' already exists!');
			}
		}

	}
	fclose($handle);
}

?>