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

	//=========================================
	//
	//	THIS CLASS IS STATIC ONLY
	//
	//=========================================
	private function __construct() {}

	private function __clone() {}

	//=========================================
	//
	//	THE PUBLIC INTERFACE
	//
	//=========================================

	public static function get_best_selling_manufacturers($structure_id, $num = 3) {
		$manufacturers = ManufacturerHelper::get_best_selling_manufacturers_object($structure_id, $num, false);
		$manufacturer_object = new Manufacturer(Registry::get('base'));

		foreach ($manufacturers as $manufacturer) {
			$obj = $manufacturer_object->get_data($manufacturer->id);
			$obj->items_in_shop = $manufacturer->items_in_shop;
			$return_data[] = $obj;
		}

		return $return_data;
	}

	public static function get_best_selling_manufacturers_object($structure_id, $num, $with_logo = true) {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					i.manufacturer_id AS id,
					COUNT(oi.id) AS num
				FROM
					order_item oi
				RIGHT JOIN
					items i
				ON
					i.id = oi.item_id
				RIGHT JOIN
					manufacturers m
				ON
					i.manufacturer_id = m.id
				WHERE
					oi.item_id is not null
				AND
					m.active=1";

		if ($with_logo) {
			$sql .= " AND m.logo != ''";
		}

		if ($structure_id > 0) {
			$sql .= "
			AND (
			i.structure_id = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_2 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_3 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_4 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_5 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_6 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_7 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_8 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_9 = ".$db->real_escape_string($structure_id)."
			OR
			i.structure_id_10 = ".$db->real_escape_string($structure_id).")";
		}

		$sql .= " GROUP BY i.manufacturer_id ORDER BY num DESC LIMIT 0, ".$db->real_escape_string($num);
		$result = $db->query($sql);

		if ($result->num_rows > 0) {
			while ($obj = $result->fetch_object()) {
				$obj->items_in_shop = ItemHelper::get_number_of_items_by_manufacturer($obj->id, true);
				$return_data[] = $obj;
			}
			return $return_data;
		}

		return false;
	}

	public static function set_manufacturers_active_by_ids($ids, $active) {
		$db = Registry::get('base')->db;

		$sql = "UPDATE manufacturers
				SET active=".$db->real_escape_string($active).
				" WHERE id IN (".$db->real_escape_string($ids).")";

		return $db->query($sql);
	}

	public static function delete_manufacturers_by_ids($ids) {
		$db = Registry::get('base')->db;

		$sql = "DELETE FROM manufacturers 
				WHERE id IN (".$db->real_escape_string($ids).")";

		return $db->query($sql);
	}

	public static function get_manufacturer_array() {
		$db = Registry::get('base')->db;

		$sql = "SELECT
					id,
					name
				FROM
					manufacturers";

		$result = $db->query($sql);

		$return_data[0] = '';
		if ($result) {
			while ($obj = $result->fetch_object()) {
				$return_data[$obj->id] = $obj->name;
			}

			return $return_data;
		}

		return false;
	}
}