<?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/orderstatus.class.php';

class new_orders {
	private $base_object;

	function __construct($base_object) {
		$this->base_object = $base_object;
		$this->db = $base_object->db;
	} // end __construct

	function run($filter) {
		// get order states
		$order_state_object = new OrderStatus($this->base_object);
		$order_states = $order_state_object->get_all();

		// get customer registrations
		$sql = "SELECT id, order_date, order_status FROM orders
				WHERE order_date >= '".$filter['date_start']."'
				AND order_date <=  '".$filter['date_end']."'
				AND revised_order_id = 0";
		$result = $this->db->query($sql);

		$data = array();
		$first_row = array('Monat', 'neue Bestellungen');
		foreach ($order_states as $state) {
			$first_row[] = 'davon '.$state->name;
		}
		// statistic of a year
		if ($filter['period'] == 'year') {
			// first inital
			$sum = array();
			for ($i=0;$i<12;$i++) {
				$cols = array('all' => 0);
				foreach ($order_states as $order_state) {
					$cols['id_'.$order_state->id] = 0;
				}  
				$sum[] = $cols;
			}

			// get stat data
			while ($obj = $result->fetch_object()) {
				$m = substr($obj->order_date, 5, 2);
				$index = intval($m) - 1;
				$sum[$index]['all']++;
				$sum[$index]['id_'.$obj->order_status]++;
			}

			// make table
			$data = array($first_row);
			$months = array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember');
			$month = array();
			for ($i=0;$i<12;$i++) {
				$cols = array($months[$i], $sum[$i]['all']);
				foreach ($order_states as $order_state) {
					$cols[] = $sum[$i]['id_'.$order_state->id];
				} 
				$data[] = $cols;
			}

		} else if ($filter['period'] == 'month') { // statistic of a month
			$year = substr($filter['date_start'],0,4);
			$month = substr($filter['date_start'],5,2);
			$day = substr($filter['date_start'],8,2);

			// get max month days
			$monat = mktime(0, 0, 0, $month, 1, $year);
			$max_days_of_month = date("t", $monat);

			// get stat data
			$sum = array();
			for ($z=1;$z<=$max_days_of_month;$z++) {
				$cols = array('all' => 0);
				foreach ($order_states as $order_state) {
					$cols['id_'.$order_state->id] = 0;
				}  
				$sum[] = $cols;
			}
			while ($obj = $result->fetch_object()) {
				$m = substr($obj->order_date, 8, 2);
				$index = intval($m) - 1;
				$sum[$index]['all']++;
				$sum[$index]['id_'.$obj->order_status]++;
			}

			// make table
			$first_row[0] = 'Tag';
			$data = array($first_row);
			for ($z=1;$z<=$max_days_of_month;$z++) {
				$i = $z - 1;
				$cols = array($z, $sum[$i]['all']);
				foreach ($order_states as $order_state) {
					$cols[] = $sum[$i]['id_'.$order_state->id];
				} 
				$data[] = $cols;
			}
		} else if ($filter['period'] == 'day') { // statistic of a day
			$year = substr($filter['date_start'],0,4);
			$month = substr($filter['date_start'],5,2);
			$day = substr($filter['date_start'],8,2);
			$date = $day.'.'.$month.'.'.$year;


			// get stat data
			$sum = array();
			$cols = array('all' => 0);
			foreach ($order_states as $order_state) {
				$cols['id_'.$order_state->id] = 0;
			}
			$sum[] = $cols;

			while ($obj = $result->fetch_object()) {
				$m = substr($obj->order_date, 8, 2);
				$sum[0]['all']++;
				$sum[0]['id_'.$obj->order_status]++;
			}

			// make table
			$first_row[0] = 'Datum';
			$data = array($first_row);
			$cols = array($date, $sum[0]['all']);
			foreach ($order_states as $order_state) {
				$cols[] = $sum[0]['id_'.$order_state->id];
			}
			$data[] = $cols;

		}

		return $data;
	}

}