<?php

require_once WPSG_PATH_MOD.'mod_statistics/wpsg_statisticabstract.php';

/**
 * Klasse zur Ermittlung Statistik für die Umsatz nach Zahlungsart
 * @author 	robert
 * @since	11.01.2013
 */
class wpsg_salestopaymentmethod extends wpsg_statisticabstract
{
	
	/**
	 * Rückgabe aller Statistikdaten
	 * @internal Bezug -> Jahr
	 * @return array
	 */
	public function getAll($point = 0, $params = array())
	{
		
		$arData['template'] = 'all';
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
	}	
	
	/**
	 * Rückgabe der Statistik auf ein Jahr bezogen
	 * @internal Bezug -> Monat
	 * @param int Bezugspunkt (0 aktuelles Jahr, -1 letztes Jahr)
	 * @return array
	 */
	public function getYear($point = 0, $params = array())
	{
		
		$arData['template'] = 'all';
		
		if ($point == '0')
		{
			$year = date('Y');
		}
		else if ($point == '-1')
		{
			$year = date('Y') - 1;
		}
		else
		{
			$year = $point;	
		}
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND 
													  		YEAR( `cdate` ) = '".$year."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
		
	}
	
	/**
	 * Rückgabe der Statistik auf ein Quartal bezogen
	 * @param string
	 * @internal Bezug -> Monat 
	 * @return array
	 */
	public function getQuarter($point = 0, $params = array())
	{
		
		$arData['template'] = 'all';
		
		$arQuarter = array(	'1' => array(1, 2, 3),
							'2'	=> array(4, 5, 6),
							'3'	=> array(7, 8, 9),
							'4' => array(10, 11, 12));
		
		if ($point == '0')
		{
			$year = date('Y');
			$month = date('n');
		}
		else if ($point == '-1')
		{
			$year = date('Y');
			$month = date('n');
			
			foreach ($arQuarter as $index => $quarter)
			{
				if (in_array($month, $quarter))
				{
					if (isset($actQuarter[$index - 1]))
					{
						$quarterFirstMonth = $arQuarter[$index - 1][0];	
					}
					else
					{
						$quarterFirstMonth = $arQuarter[count($arQuarter)][0];	
						$year--;
					}
				}
			}
			
			$month = $quarterFirstMonth;
		}
		
		foreach ($arQuarter as $index => $quarter)
		{
			if (in_array($month, $quarter))
			{
				
				$actQuarter = $index;
				
				$lastMonth = $arQuarter[$index][count($arQuarter[$index]) - 1];
				
				$daysLastMonth = wpsg_getDaysofMonth($lastMonth, $year);
				
				$dayLastMonthTS = mktime(0, 0, 0, $lastMonth, $daysLastMonth, $year);
				
				$firstMonth = array_shift($arQuarter[$index]);
				
				$dayFirstMonthTS = mktime(0, 0, 0, $firstMonth, 1, $year);
				
			}
		}
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND 
													  		`cdate` >= '".date('Y-m-d H:i:s', $dayFirstMonthTS)."' AND 
													  		`cdate` <= '".date('Y-m-d H:i:s', $dayLastMonthTS)."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
		
	}
	
	/**
	 * Rückgabe der Statistik auf einen Monat bezogen
	 * @internal Bezug -> Tag 
	 * @param string
	 * @return array
	 */
	public function getMonth($point = 0, $params = array())
	{
		
		if ($point == '0')
		{
			$year = date('Y');
			$month = date('n');
		}
		else if ($point == '-1')
		{
			$year = date('Y');
			$month = date('n') - 1;
			
			if ($month == 0)
			{
				$month = 12;
				$year--;
			}
		}
		
		$arData['template'] = 'all';
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND 
													  		YEAR( `cdate` ) = '".$year."' AND 
													  		MONTH ( `cdate` ) = '".$month."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
		
	}
	
	/**
	 * Rückgabe der Statistik auf eine Woche bezogen
	 * @internal Bezug -> Tag 
	 * @param string
	 * @return array
	 */
	public function getWeek($point = 0, $params = array())
	{
		
		$adjustment = 1;
		
		if ($point == '0')
		{
			$weekday = date("w", time());
		
			$firstDayofWeek = time() - (($weekday - $adjustment)*86400);
			$lastDayofWeek = time() + ((6 + $adjustment - $weekday) * 86400);
		}
		else if ($point == '-1') 
		{
			
			$time = time() - (6*86400);
			
			$weekday = date("w", $time);
		
			$firstDayofWeek = $time - (($weekday - $adjustment)*86400);
			$lastDayofWeek = $time + ((6 + $adjustment - $weekday) * 86400);
		}
		
		$arData['template'] = 'all';
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND
													  		`cdate`	>= '".date('Y-m-d 00:00:00', $firstDayofWeek)."' AND 
													  		`cdate` <= '".date('Y-m-d 23:59:59', $lastDayofWeek)."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
		
	}
	
	/**
	 * Rückgabe der Statistik auf einen Tag bezogen
	 * @internal Bezug -> Stunde 
	 * @param string
	 * @return array
	 */
	public function getDay($point = 0, $params = array())
	{
		
		if ($point == 0)
		{
			$day = time();
		}
		else if ($point == -1)
		{
			$day = time() - 86400;
		}
		
		$arData['template'] = 'all';
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND 
													  		YEAR( `cdate` ) = '".date('Y', $day)."' AND 
										  					MONTH ( `cdate` ) = '".date('m', $day)."' AND 
										  					DAY (`cdate`) = '".date('d', $day)."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;
 		
	}
	
	/**
	 * Rückgabe der Statistik bezogen auf einen benutzerdefinierten Zeitraum
	 * @internal Bezug -> Tag 
	 * @param string
	 * @return array
	 */
	public function getCustomRange()
	{

		$start = wpsg_fieldarray_todate($_REQUEST['filter']['day'][0]);
		$end = wpsg_fieldarray_todate($_REQUEST['filter']['day'][1]);
		
		$arData['template'] = 'all';
		
		$arData['records'] = $this->db->fetchAssoc(" SELECT 
														    SUM(`price_gesamt`) AS total, 
															`type_payment` AS payment 
													  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.") AND
													  		`cdate`	>= '".date('Y-m-d 00:00:00', $start)."' AND 
													  		`cdate` <= '".date('Y-m-d 23:59:59', $end)."'
													  GROUP BY 
													  		`type_payment`");
		
		$arData['records'] = $this->calculate($arData['records']);
		
		$this->addSorting($arData, $params['order'], ((!wpsg_isSizedString($params['sort']))?'name':$params['sort']));
		
		return $arData;

	}
	
	protected function calculate($arData =array())
	{
		
		foreach ($arData as $data)
		{
			
			if (isset($this->shop->arPayment[$data['payment']]))
			{
				$arTmpData[$data['payment']] = $data;
				$arTmpData[$data['payment']]['name'] = (($this->shop->arPayment[$data['payment']]['name'] != '')?$this->shop->arPayment[$data['payment']]['name']:'leer');

				$total += $data['total']; 
			}
			
		}
		
		if (wpsg_isSizedArray($arTmpData))
		{
			foreach ($arTmpData as $index => $data)
			{
				$arTmpData[$index]['percent'] = round(($data['total'] * 100)/$total, 2);	
				
				if ($arTmpData[$index]['percent'] < 1)
				{
					$arTmpData[$index]['percent'] = 1;
				}
				
			}	
		}
		
		return $arTmpData;
		
	}
	
}

?>