<?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 new_customer_registrations {
	private $base_object;

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

	function run($filter) {
		// get customer registrations
		$sql = "SELECT registration_date FROM customers
				WHERE registration_date >= '".$filter['date_start']."'
				AND registration_date <=  '".$filter['date_end']."'";
		$result = $this->db->query($sql);

		$data = array();
		// statistic of a year
		if ($filter['period'] == 'year') {
			// get stat data
			$sum = array(0,0,0,0,0,0,0,0,0,0,0,0);
			while ($obj = $result->fetch_object()) {
				$m = substr($obj->registration_date, 5, 2);
				$index = intval($m) - 1;
				$sum[$index]++;
			}

			// make table
			$data = array(
				array('Monat', 'neue Kundenregistrierung'),
				array('Januar', $sum[0]),
				array('Februar', $sum[1]),
				array('März', $sum[2]),
				array('April', $sum[3]),
				array('Mai', $sum[4]),
				array('Juni', $sum[5]),
				array('Juli', $sum[6]),
				array('August', $sum[7]),
				array('September', $sum[8]),
				array('Oktober', $sum[9]),
				array('November', $sum[10]),
				array('Dezember', $sum[11])
			);
		} 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++) {
				$sum[] = 0;
			}
			while ($obj = $result->fetch_object()) {
				$m = substr($obj->registration_date, 8, 2);
				$index = intval($m) - 1;
				$sum[$index]++;
			}

			// make table
			$data[] = array('Tag', 'neue Kundenregistrierung');
			for ($z=1;$z<=$max_days_of_month;$z++) {
				$i = $z - 1;
				$data[] = array($z, $sum[$i]);
			}
		} 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 = 0;
			while ($obj = $result->fetch_object()) {
				$m = substr($obj->registration_date, 8, 2);
				$index = intval($m) - 1;
				$sum++;
			}

			// make table
			$data = array(
				array('', 'neue Kundenregistrierung'),
				array($date, $sum)
			);
		}

		return $data;
	}

}