<?php
/*function adddoublequotes( $str ){
	return htmlspecialchars( $str, ENT_QUOTES );
}*/
require_once( 'stats.php' );
function throwCustomersStats( $sDateFrom, $sDateTo, $iStatus1, $iStatus2 ){
	global $oProduct;
	$oDB = DataBase::getInstance( );
	$iTimeStart = dateToTime( $sDateFrom, 'ymd' );
	$iTimeStop = dateToTime( $sDateTo . ' 23:59:59', 'ymd' );
	$sql = 'SELECT CONCAT( `orders`.`sFirstName`, \' \', `orders`.`sLastName` ) AS `sName`, SUM( `orders_products`.`iQuantity` ) AS `iQuantity`, SUM( `orders_products`.`iQuantity` * `orders_products`.`fPrice` ) AS `fSummary`
						FROM `' . $oDB->throwTableName( DB_ORDERS ) . '` AS `orders`
							LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` AS `orders_products` ON ( `orders_products`.`iOrder` = `orders`.`iOrder` )
						WHERE `orders`.`iTime` >= ' . $iTimeStart . ' AND `orders`.`iTime` <= ' . $iTimeStop . '
						GROUP BY `orders`.`sFirstName`, `orders`.`sLastName`, `orders`.`sEmail`';
	$aReturn['aCustomers'] = $oDB->executeSQL( $sql, true );
	$aReturn['fSummarySum'] = 0;
	$aReturn['iQuantitySum'] = 0;
	foreach( $aReturn['aCustomers'] as &$aCustomer ){
		$aCustomer['fSummary'] = normalizePrice( $aCustomer['fSummary'] );
		$aCustomer['sSummary'] = displayPrice( $aCustomer['fSummary'] );
		$aReturn['fSummarySum']+= $aCustomer['fSummary'];
		$aReturn['iQuantitySum']+= $aCustomer['iQuantity'];
	}
	$aReturn['sSummarySum'] = displayPrice( $aReturn['fSummarySum'] );
	return $aReturn;
}
function throwOrderedProductsStats( $sDateFrom, $sDateTo, $iStatus1, $iStatus2 ){
	global $oProduct;
	$oDB = & DataBase::getInstance( );
	$iTimeStart = dateToTime( $sDateFrom, 'ymd' );
	$iTimeStop = dateToTime( $sDateTo . ' 23:59:59', 'ymd' );
	$sql = 'SELECT CONCAT( `orders`.`sLanguage`, \'-\', `pv`.`iProduct` ) AS `iProduct`, `op`.`sName`, `op`.`iProductVariant`, SUM(`op`.`iQuantity`) AS `iQuantity`, SUM(`op`.`iQuantity` * `op`.`fPrice` ) AS `fSummary`, `orders`.`sLanguage` AS `sLang`
						FROM `' . $oDB->throwTableName( DB_ORDERS_PRODUCTS ) . '` AS `op`
							LEFT JOIN `' . $oDB->throwTableName( DB_PRODUCTS_VARIANTS ) . '` AS `pv` USING( `iProductVariant` )
							LEFT JOIN `' . $oDB->throwTableName( DB_ORDERS ) . '` AS `orders` USING( `iOrder` )
						WHERE `orders`.`iTime` >= ' . $iTimeStart . ' AND `orders`.`iTime` <= ' . $iTimeStop . '
						GROUP BY `pv`.`iProduct`, `op`.`iProductVariant`, `op`.`sName`, `orders`.`sLanguage`';
	$aReturn['aProducts'] = $oDB->executeSQL( $sql, true, DataBase::ASSOCIATIVE, 'iProductVariant' );
	foreach( $aReturn['aProducts'] as &$aProduct ){
		$aProduct['sName'] = ( isset( $oProduct->aProducts[$aProduct['iProduct']] ) ? $oProduct->aProducts[$aProduct['iProduct']]['sName'] : $aProduct['sName'] ) . ' (' . $aProduct['sLang'] . ')';
		$aProduct['fSummary'] = normalizePrice( $aProduct['fSummary'] );
		$aProduct['sSummary'] = displayPrice( $aProduct['fSummary'] );
		$aReturn['fSummarySum']+= $aProduct['fSummary'];
		$aReturn['iQuantitySum']+= $aProduct['iQuantity'];
	}
	$aReturn['sSummarySum'] = displayPrice( $aReturn['fSummarySum'] );
	return $aReturn;
}
?>