<?php
if( $a == 'home' ){
	$_GET['iStatus'] = 1;
	$iStatus = 1;
	$aOrders = $oOrder->throwOrdersAdmin( 5 );
	$oTpl->assign( 'aOrders', $aOrders );
	$aInfo['sOrdersCountCurrentMonth'] = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersCountLastMonth']    = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of' ) . ' > `iTime` AND ' . strtotime( 'first day of previous month' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersCountCurrentYear']  = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of january this year' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersCountLastYear']     = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of january this year' ) . ' > `iTime` AND ' . strtotime( 'first day of january previous year' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersCountAll']          = $oDB->executeSQL( 'SELECT COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\'', true, DataBase::SINGLE_VALUE );
	
	$aInfo['sOrdersSummaryCurrentMonth'] = $oDB->executeSQL( 'SELECT SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersSummaryLastMonth']    = $oDB->executeSQL( 'SELECT SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of' ) . ' > `iTime` AND ' . strtotime( 'first day of previous month' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersSummaryCurrentYear']  = $oDB->executeSQL( 'SELECT SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of january this year' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersSummaryLastYear']     = $oDB->executeSQL( 'SELECT SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ' . strtotime( 'first day of january this year' ) . ' > `iTime` AND ' . strtotime( 'first day of january previous year' ) . ' <= `iTime`', true, DataBase::SINGLE_VALUE );
	$aInfo['sOrdersSummaryAll']          = $oDB->executeSQL( 'SELECT SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\'', true, DataBase::SINGLE_VALUE );
	
	$aInfo['sFinishingProducts'] = $oDB->executeSQL( 'SELECT COUNT( `iProduct` ) FROM ( SELECT DISTINCTROW `iProduct` FROM `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) . '` AS `pv` LEFT JOIN `' . $oDB->throwTableName( DB_PRODUCTS ) . '` AS `p` USING( `iProduct` ) WHERE `pv`.`TiStock` < IFNULL( `p`.`iStockSendAdminMin`, ' . intval( $GLOBALS['config']['stock_send_admin_min'] ) . ' ) ) AS `p`', true, DataBase::SINGLE_VALUE );
	$aInfo['sUsers'] = $oDB->executeSQL( 'SELECT COUNT( `iUser` ) FROM `' . $oDB->throwTableName( DB_USERS ) . '`', true, DataBase::SINGLE_VALUE );
	$aInfo['sGroups'] = $oDB->executeSQL( 'SELECT COUNT( `iGroup` ) FROM `' . $oDB->throwTableName( DB_USERS_GROUPS ) . '`', true, DataBase::SINGLE_VALUE );
	$aInfo['sPagesComments'] = $oDB->executeSQL( 'SELECT COUNT( `iComment` ) FROM `' . $oDB->throwTableName( DB_PAGES_COMMENTS ) . '` WHERE `iAccepted` = 0', true, DataBase::SINGLE_VALUE );
	$aInfo['sProductsComments'] = $oDB->executeSQL( 'SELECT COUNT( `iComment` ) FROM `' . $oDB->throwTableName( DB_PRODUCTS_COMMENTS ) . '` WHERE `iAccepted` = 0', true, DataBase::SINGLE_VALUE );
	$aInfo['sProducts'] = $oDB->executeSQL( 'SELECT COUNT( `iProduct` ) FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '`', true, DataBase::SINGLE_VALUE );
	if( defined( 'PRODUCTS_LIMIT' ) && PRODUCTS_LIMIT > 0 ){
		$aInfo['sProducts'] .= ' / ' . PRODUCTS_LIMIT;
	}
	$aInfo['sActiveProducts'] = $oDB->executeSQL( 'SELECT COUNT( `iProduct` ) FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` WHERE `iStatus` = 1', true, DataBase::SINGLE_VALUE );
	//Czyszczenie logów - kasowanie starszych niż 90 dni
	$oDB->executeSQL( 'DELETE FROM `' . $oDB->throwTableName( DB_ADMIN_ACTIONS ) . '` WHERE `iTime` < ' . intval( time() - /* ilość dni: */ 90 /* */ * 86400 ) );
	$aInfo['sDiscountedProducts'] = $oDB->executeSQL( 'SELECT COUNT( `iProduct` ) FROM `' . $oDB->throwTableName( DB_PRODUCTS ) . '` WHERE `fPriceOld` > `fPrice`', true, DataBase::SINGLE_VALUE );
	$aInfo['sBannersJSON'] = file_get_contents( 'http://sellsmart.pl/sellsmart_banners/banners.json' );
	$oTpl->assign( 'aInfo', $aInfo );
	$content.= $oTpl->parse( 'home.tpl' );
} elseif( $a = 'xml_orders_chart' ){
	$aOrdersCountStats = $oDB->executeSQL( 'SELECT DATE( FROM_UNIXTIME( `iTime` ) ), COUNT( `iOrder` ) FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ( UNIX_TIMESTAMP( CURDATE() ) - 30*24*60*60 ) < `iTime` GROUP BY DATE( FROM_UNIXTIME( `iTime` ) )', true, DataBase::ASSOCIATIVE );
	$aOrdersSummaryStats = $oDB->executeSQL( 'SELECT DATE( FROM_UNIXTIME( `iTime` ) ), SUM( `fPrice` * `iQuantity` ) FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` USING( `iOrder` ) WHERE `sLanguage` = \'' . LANGUAGE . '\' AND ( UNIX_TIMESTAMP( CURDATE() ) - 30*24*60*60 ) < `iTime` GROUP BY DATE( FROM_UNIXTIME( `iTime` ) )', true, DataBase::ASSOCIATIVE );
	$aChartInfo = Array( );
	for( $tTime = time( ) - 30 * 24 * 60 * 60;$tTime <= time( );$tTime+= 24 * 60 * 60 ){
		$sDate = date( 'Y-m-d', $tTime );
		$aChartInfo[$sDate] = Array( $aOrdersCountStats[$sDate], normalizePrice( $aOrdersSummaryStats[$sDate] ) );
	}
	header( 'Content-type: text/xml' );
	$oTpl->assign( 'aChartInfo', $aChartInfo );
	echo ( iconv( 'UTF-8', 'cp1250', $oTpl->parse( 'xml-home.tpl' ) ) );
	exit;
}
?>