<?php

class wpsg_statisticabstract
{
	
	protected $shop;
	
	protected $db;
	
	public $sqlWhere = '';
	
	protected $observeStatuscodes;

	public function __construct()
	{
		$this->shop = &$GLOBALS['wpsg_sc']; 
		
		$this->db = &$GLOBALS['wpsg_db']; 
		
		$this->observeStatuscodes = $this->shop->get_option('wpsg_mod_statistics_status');
		
		$this->setSqlWhere();
	}
	
	/**
	 * Kundenanzahl ermitteln
	 * @return int
	 */
	public function getCustomerCount()
	{
		$data = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_KU."`");	
		
		if ($data !== false)
			return $data;
		
		return 0;
	}
	
	/**
	 * Produktanzahl ermitteln
	 * @return int
	 */
	public function getProductCount()
	{
		$data = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_PRODUCTS."` WHERE `deleted` != '1'");
		
		if ($data !== false)
			return $data;
		
		return 0;
	}
	
	/**
	 * Bestellanzahl ermitteln
	 * @return int
	 */
	public function getOrderCount()
	{
		
		$data = $this->db->fetchOne("SELECT COUNT(*) FROM `".WPSG_TBL_ORDER."` WHERE `status` IN(".$this->observeStatuscodes.")");
		
		if ($data !== false)
			return $data;
		
		return 0;
	}
	
	/**
	 * Tagesumsatz ermitteln
	 * @param int $timestamp
	 * @return double
	 */
	public function getDailyTurnover($timestamp = 0)
	{
		if ($timestamp == 0) { $timestamp = time(); }
		
		$data = $this->db->fetchOne("
				SELECT 
					SUM(`price_gesamt`) 
			  	FROM 
			  		`".WPSG_TBL_ORDER."` 
			  	WHERE 
			  		".$this->getSqlWhere('validOrder')." AND 
			  		YEAR( `cdate` ) = '".date('Y', $timestamp)."' AND 
	  				MONTH ( `cdate` ) = '".date('m', $timestamp)."' AND 
	  				DAY (`cdate`) = '".date('d', $timestamp)."'
	  	");
		
		if ($data !== false)
			return $data;
		
		return 0;
	}
	
	/**
	 * Gesamtumsatz ermitteln
	 * @return double
	 */
	public function getTotalTurnover()
	{
		$data = $this->db->fetchOne("
				SELECT 
				 	SUM(`price_gesamt`) 
			  	FROM 
			  		`".WPSG_TBL_ORDER."` 
			 	WHERE 
			  		".$this->getSqlWhere('validOrder')
		);
		
		if ($data !== false)
			return $data;
		
		return 0;
		
	}
	
	/**
	 * Sortierung der Daten vollziehen
	 * @param	array	$arData
	 * @param 	string	$order
	 * @param 	string	$sort
	 * @return	array
	 */
	public function addSorting(&$arData, $order = '', $sort = '')
	{
		
		if (wpsg_isSizedString($sort))
			$arData['records'] = wpsg_array_csort_pk($arData['records'], $sort);
				
		if($order == 'orderup')
			$arData['records'] = array_reverse($arData['records'], true);		
			
		return $arData;
	}
	
	public function setSqlWhere()
	{
		$this->sqlWhere['validOrder'] = "`cdate` != '0000-00-00 00:00:00' AND 
													  		`type_payment` != '' AND 
													  		`type_payment` IS NOT NULL AND 
													  		`status` IN(".$this->observeStatuscodes.")";
	}
	
	public function getSqlWhere($key = '')
	{
		if (isset($this->sqlWhere[$key]))
		{
			return $this->sqlWhere[$key];
		}
		
		return '';
	}
	
	/**
	 * Jahre in denen der Shop Bestellungen umgesetzt hat
	 * @return array
	 */
	public function getShopOrderYears()
	{
		$years = array_keys($this->db->fetchAssoc("  SELECT 
															YEAR( `cdate` ) AS orderyear 
													 FROM 
													 		`".WPSG_TBL_ORDER."` 
													 WHERE
													 		`cdate` != '0000-00-00 00:00:00' AND 
													  		`type_payment` != '' AND 
													  		`type_payment` IS NOT NULL AND 
													  		`status` IN(".$this->observeStatuscodes.") 
													 GROUP BY 
													  		orderyear
													 ORDER BY 
													  		orderyear DESC", "orderyear"));
		
		return $years;
	}
	
	/**
	 * Rückgabe der Monatstage eines bestimmenten Monats in einem Jahr
	 * @param	int	$month
	 * @param 	int	$year
	 * @return 	int
	 */
	public function getDaysofMonth($month, $year)
	{
		$time = mktime(0, 0, 0, $month, 1, $year);
		return date('t', $time);
	}
}

?>