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

	function get($id) {
		return;
	} // end get

	public function load_discounts($fetch_article_ids = false, $discount_group = false, $customer_id = false) {
		$discounts = array();

		if (is_numeric($fetch_article_ids)) {
			$article_ids = array($fetch_article_ids);
		} else if (is_array($fetch_article_ids)) {
			$article_ids = $fetch_article_ids;
		} else {
			$article_ids = array();
		}

		if (count($article_ids) > 0 && is_numeric($discount_group) && $discount_group > 0) {

			// Group discounts
			$rs = mysql_query("
			SELECT
				MAX(d.rg_".$discount_group.") AS discount,
				p.id
			FROM
				easyshop_rabattgruppen d
			RIGHT JOIN
				easyshop_zuweisungen a
			ON
				a.wg_id = d.wg_id
			LEFT JOIN
				easyshop_artikel p
			ON
				a.a_id = p.id
			WHERE
				a.a_id IN (".implode(',', $article_ids).")
			AND
				d.hersteller_id = p.hersteller_id
			GROUP BY
				p.id,
				d.hersteller_id
			HAVING
				discount > 0
			");

			while ($D = mysql_fetch_object($rs)) {
				$discounts[$D->id] = $D->discount;
			}

			if (is_numeric($customer_id)) {
				// Customer discounts
				$rs = mysql_query("
					SELECT
						MAX(d.rabatt) AS discount,
						p.id
					FROM
						easyshop_rabattkunden d
					RIGHT JOIN
						easyshop_zuweisungen a
					ON
						a.wg_id = d.wg_id
					LEFT JOIN
						easyshop_artikel p
					ON
						a.a_id = p.id
					WHERE
						a.a_id IN (".implode(',', $article_ids).")
					AND
						d.hersteller_id = p.hersteller_id
					AND
						d.kunden_id = ".$customer_id."
					GROUP BY
						p.id,
						d.hersteller_id
					HAVING
						discount > 0
				");

				while ($D = mysql_fetch_object($rs)) {
					$discounts[$D->id] = max(array($D->discount, $discounts[$D->id]));
				}
			}

			// Product discounts
			$rs = mysql_query("
				SELECT
					rg_".$discount_group." AS discount,
					artikel_id
				FROM
					easyshop_rabattartikel
				WHERE
					artikel_id IN (".implode(',', $article_ids).")
				AND
					rg_".$discount_group." > 0
			");

			while ($D = mysql_fetch_object($rs)) {
				$discounts[$D->artikel_id] = max(array($D->discount, $discounts[$D->artikel_id]));
			}
		}

		return $discounts;
	}// end load_discounts

	//speziell fuer article export
	public function load_discounts_export($fetch_article_ids = false, $discount_group = false, $customer_id = false) {
		$discounts = array();

		if (is_numeric($fetch_article_ids)) {
			$article_ids = array($fetch_article_ids);
		} else if (is_array($fetch_article_ids)) {
			$article_ids = $fetch_article_ids;
		} else {
			$article_ids = array();
		}

		if (is_numeric($discount_group) && $discount_group > 0) {

			// Group discounts
			$rs = mysql_query("
				SELECT
					MAX(d.rg_".$discount_group.") AS discount,
					p.id
				FROM
					easyshop_rabattgruppen d
				RIGHT JOIN
					easyshop_zuweisungen a
				ON
					a.wg_id = d.wg_id
				LEFT JOIN
					easyshop_artikel p
				ON
					a.a_id = p.id
				AND
					d.hersteller_id = p.hersteller_id
				GROUP BY
					p.id,
					d.hersteller_id
				HAVING
					discount > 0
			");

			while ($D = mysql_fetch_object($rs)) {
				$discounts[$D->id] = $D->discount;
			}

			if (is_numeric($customer_id) && $article_ids) {
				// Customer discounts
				$rs = mysql_query("
					SELECT
						MAX(d.rabatt) AS discount,
						p.id
					FROM
						easyshop_rabattkunden d
					RIGHT JOIN
						easyshop_zuweisungen a
					ON
						a.wg_id = d.wg_id
					LEFT JOIN
						easyshop_artikel p
					ON
						a.a_id = p.id
					WHERE
						a.a_id IN (".implode(',', $article_ids).")
					AND
						d.hersteller_id = p.hersteller_id
					AND
						d.kunden_id = ".$customer_id."
					GROUP BY
						p.id,
						d.hersteller_id
					HAVING
						discount > 0
				");

				while ($D = mysql_fetch_object($rs)) {
					$discounts[$D->id] = max(array($D->discount, $discounts[$D->id]));
				}
			}

			if ($article_ids) {
				// Product discounts
				$rs = mysql_query("
					SELECT
						rg_".$discount_group." AS discount,
						artikel_id
					FROM
						easyshop_rabattartikel
					WHERE
						artikel_id IN (".implode(',', $article_ids).")
					AND
						rg_".$discount_group." > 0
				");

				while ($D = mysql_fetch_object($rs)) {
					$discounts[$D->artikel_id] = max(array($D->discount, $discounts[$D->artikel_id]));
				}
			}
		}

		return $discounts;
	} // end load_discounts_export
}