<?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
 */

include_once './core/pageable.class.php';

class Billing extends Pageable {

	public function __construct($base_object) {
		parent::__construct($base_object);
		$this->config_object = $base_object->config;
		$this->base_object = $base_object;
	} // end constructor


	private function get_order_statuses() {
		$sql = "
			SELECT
				id
			FROM
				order_status
			WHERE
				in_billing = 1";

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

		if ($result->num_rows) {
			while ($obj = $result->fetch_object()) {
				$return[] = $obj->id;
			}

			return $return;
		} else {
			return false;
		}

		throw new Exception("Es wurde noch kein Bestellstatus den Rechnungen zugewiesen.");
	} // end get_order_statuses


	private function get_billing_statuses() {
		$sql = "
			SELECT
				id
			FROM
				billing_status
			WHERE
				in_billing = 1";

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

		if ($result->num_rows) {
			while ($obj = $result->fetch_object()) {
				$return[] = $obj->id;
			}

			return $return;
		} else {
			return false;
		}

		throw new Exception("Es wurde noch kein Rechnungsstatus den Rechnungen zugewiesen.");
	} // end get_billing_statuses


	private function get_where_clause() {
		$order_where = '';

		$order_statuses = $this->get_order_statuses();

		foreach ($order_statuses as $order_status) {
			if ($order_where == '') {
				$order_where .= "WHERE order_status = $order_status ";
			} else {
				$order_where .= "OR order_status = $order_status ";
			}
		}

		$billing_where = '';

		$billing_statuses = $this->get_billing_statuses();

		foreach ($billing_statuses as $billing_status) {
			if ($billing_where == '') {
				$billing_where .= "AND billing_status = $billing_status ";
			} else {
				$billing_where .= "OR billing_status = $billing_status ";
			}
		}

		return $order_where.$billing_where;
	} // end get_where_clause


	public function get_number_of_pages($items) {
		$sql = "
			SELECT
				COUNT(id)
			FROM
				orders ";

		$sql .= $this->get_where_clause();

		return parent::get_number_of_pages($items, $sql);
	} // end get_number_of_pages


	public function get_pagination_array($items, $page) {
		$sql = "
			SELECT
				COUNT(id)
			FROM
				orders ";

		$sql .= $this->get_where_clause();

		return parent::get_pagination_array($items, $page, $sql);
	} // end get_pagination_array


	public function get_all_paginated($items, $page, $order = false) {
		$page = ($page - 1) * $items;

		$sql = "
			SELECT
				*
			FROM
				orders ";

		$sql .= $this->get_where_clause();

		$sql .= ' '."
			ORDER BY
					order_date DESC
			LIMIT $items OFFSET $page";

		return parent::get_all_paginated($items, $page, $sql);
	} // end get_all_paginated
}