<?

/*
FlexCMS (http://www.flexcms.com)
Flexible, Powerful, Affordable.

Contents of this file are protected by the copyright
notice found at http://www.flexcms.com/flex/pages/copyright.html,
and its usage is subject to the terms of the licensing agreement 
available at http://www.flexcms.com/flex/pages/license.html
*/



	// ========================================================================================
	
	// ========================================================================================
	
	$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_ecom.gif" width="20" height="21" align="middle"> '.$MsgText[9][175];
	$BrowserTitle = $MsgText[9][175];
	$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > '.$MsgText[9][175];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[906] == 'y') {
		
			if ($Arguments3 == '') {
			
				
				$PageContents = '<form><b>'.$MsgText[9][259].':</b><br><br><input type="button" value="'.$MsgText[9][260].'" onClick="window.location=\''.$MainURL.'/admin/store-reports/most_popular.html\'" style="'.$FormStyle.'"></form><br><br>

<form><b>'.$MsgText[9][261].':</b><br><br><input type="button" value="'.$MsgText[9][262].'" onClick="window.location=\''.$MainURL.'/admin/store-reports/sales.html\'" style="'.$FormStyle.'"></form><br><br>

<form><b>'.$MsgText[9][263].':</b><br><br><input type="button" value="'.$MsgText[9][264].'" onClick="window.location=\''.$MainURL.'/admin/store-inventory.html\'" style="'.$FormStyle.'"></form><br>';
			
			
			}
			else if ($Arguments3 == 'most_popular') {
			
			
				$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_ecom.gif" width="20" height="21" align="middle"> '.$MsgText[9][260];
				$BrowserTitle = $MsgText[9][260];
				$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/store-reports.html">'.$MsgText[9][175].'</a> > '.$MsgText[9][260];
			
			

				$PageContents .= '<br><br><b>'.$MsgText[9][265].':</b><br>';
			
				$query = "select `".$Settings['DBPrefix']."ec-Stats`.ProductID, SUM(`".$Settings['DBPrefix']."ec-Stats`.Count) as TotalCount from `".$Settings['DBPrefix']."ec-Stats` left join `".$Settings['DBPrefix']."ec-Products` on `".$Settings['DBPrefix']."ec-Products`.RecordNumber=`".$Settings['DBPrefix']."ec-Stats`.ProductID where `".$Settings['DBPrefix']."ec-Products`.Active=1 group by `".$Settings['DBPrefix']."ec-Stats`.ProductID order by TotalCount desc limit 10";
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
			
					$query2 = "select * from `".$Settings['DBPrefix']."ec-Products` where RecordNumber='".$row['ProductID']."' limit 1";
					$result2 = mysql_query($query2) or die (mysql_error());
					$row2 = mysql_fetch_array($result2);
					
					$PageContents .= '<br> - <a href="'.$MainURL.'/store/details/'.$row2['ProductID'].'.html">'.$row2['Name'].'</a> ('.$row['TotalCount'].')';
						
				}
			
			
				$PageContents .= '<br><br><br><br><b>'.$MsgText[9][266].':</b><br>';
				
				$query = "select * from `".$Settings['DBPrefix']."ec-Orders` where PaymentReceived>0";
				$result = mysql_query($query) or die (mysql_error());
				while ($row = mysql_fetch_array($result)) {
				
					$ItemsArray = explode(',', $row['Items']);
					
					for ($x = 0; $x < count($ItemsArray); $x++) {
							if ($ItemsArray[$x] != '') {
					
							list ($ItemNumber, $Qty) = explode('x', $ItemsArray[$x]);
						
							$query2 = "select Product from `".$Settings['DBPrefix']."ec-Items` where RecordNumber='".$ItemNumber."' limit 1";
							$result2 = mysql_query($query2) or die (mysql_error());
							$row2 = mysql_fetch_array($result2);
							
							if ($ExistsInArray[$row2['Product']] == 'y') {
								$TotalCounts[$row2['Product']] += $Qty;
							}
							else {
								$ExistsInArray[$row2['Product']] = 'y';
								
								$ProductIDs[] = $row2['Product'];
								$TotalCounts[$row2['Product']] = $Qty;
							}
							
						}
					}
					
				}
				
			
				for ($x = 0; $x < count($ProductIDs); $x++) {
					for ($y = 0; $y < (count($ProductIDs)-1); $y++) {
					
						if ($TotalCounts[$ProductIDs[$y]] < $TotalCounts[$ProductIDs[$y+1]]) {
						
							$Swap = $ProductIDs[$y];
							
							$ProductIDs[$y] = $ProductIDs[$y+1];
							
							$ProductIDs[$y+1] = $Swap;
											
						}
					
					}			
				}
				
				
				if (count($ProductIDs) > 10) {
					$LoopLimit = 10;
				}
				else {
					$LoopLimit = count($ProductIDs);
				}
				
				
				for ($x = 0; $x < $LoopLimit; $x++) {
				
					$query2 = "select * from `".$Settings['DBPrefix']."ec-Products` where RecordNumber='".$ProductIDs[$x]."' limit 1";
					$result2 = mysql_query($query2) or die (mysql_error());
					$row2 = mysql_fetch_array($result2);
					
					$PageContents .= '<br> - <a href="'.$MainURL.'/store/details/'.$row2['ProductID'].'.html">'.$row2['Name'].'</a> ('.$TotalCounts[$ProductIDs[$x]].')';
				
				}
				
				
			} // end if most_popular
			else if ($Arguments3 == 'sales') {
			
			
				$PageTitle = '<img src="'.$ImagesURL.'/admin/admincp_ecom.gif" width="20" height="21" align="middle"> '.$MsgText[9][262];
				$BrowserTitle = $MsgText[9][262];
				$LocationBar = '<a href="'.$IndexURL.'">'.$MsgText[0][1].'</a> > <a href="' . $MainURL . '/options.html">'.$MsgText[0][4].'</a> > <a href="'.$MainURL.'/admin/store-reports.html">'.$MsgText[9][175].'</a> > '.$MsgText[9][262];
			
			

				$PageContents .= '<br><br><b>'.$MsgText[9][267].':</b><br><br><table border="0" cellpadding="5" cellspacing="0">
    <tr>
      <td width="150"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>Month</b></font></td>
      <td width="100" align="center"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$MsgText[9][268].'</b></font></td>
      <td width="100" align="center"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$MsgText[9][269].'</b></font></td>
      <td width="100" align="right"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$MsgText[9][270].'</b></font></td>
    </tr>';
			
				$query = "select * from `".$Settings['DBPrefix']."ec-Orders` where PaymentReceived>0 order by DateCode asc";
				$result = mysql_query($query) or die (mysql_error());
				if (mysql_num_rows($result) > 0) {
					while ($row = mysql_fetch_array($result)) {
				
						if ($FirstFound != true) {
						
							$LastMonth = date('n',$row['DateCode']);
							$LastYear = date('Y',$row['DateCode']);
						
							$FirstFound = true;
						}
					
						
						if (date('n',$row['DateCode']) != $LastMonth || date('Y',$row['DateCode']) != $LastYear) {
												
							$PageContents .= '<tr>
      <td><font face="'.$FontFace.'" size="'.$FontSize.'">'.date('F Y',mktime(0,0,0,$LastMonth,1,$LastYear)).'</font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">'.number_format($MonthOrders,0).'</font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">'.number_format($MonthQty,0).'</font></td>
      <td align="right"><font face="'.$FontFace.'" size="'.$FontSize.'">$'.number_format($MonthSales,2).'</font></td>
    </tr>';
						
							$MonthOrders = 0;
							$MonthQty = 0;
							$MonthSales = 0;
							
							$LastMonth = date('n',$row['DateCode']);
							$LastYear = date('Y',$row['DateCode']);
						
						}
						
						$ItemsList = explode(',',$row['Items']);
						$ItemFound = false;
					
						$OrderQty = 0;
				
						for ($i = 0; $i < (count($ItemsList)-1); $i++) {
							
							list ($ItemNumA, $Qty) = explode('x',$ItemsList[$i]);
							
							$OrderQty += $Qty;
							
						}	
						
						
						$MonthOrders++;
						$MonthQty += $OrderQty;
						$MonthSales += $row['ItemsTotal'];
						
						$TotalOrders++;
						$TotalQty += $OrderQty;
						$TotalSales += $row['ItemsTotal'];
					
											
					}
					
					$PageContents .= '<tr>
      <td><font face="'.$FontFace.'" size="'.$FontSize.'">'.date('F Y',mktime(0,0,0,$LastMonth,1,$LastYear)).'</font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">'.number_format($MonthOrders,0).'</font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">'.number_format($MonthQty,0).'</font></td>
      <td align="right"><font face="'.$FontFace.'" size="'.$FontSize.'">$'.number_format($MonthSales,2).'</font></td>
    </tr>';
	
					$PageContents .= '<tr>
      <td align="right"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.$MsgText[9][253].':&nbsp;</b></font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.number_format($TotalOrders,0).'</b></font></td>
      <td align="center"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>'.number_format($TotalQty,0).'</b></font></td>
      <td align="right"><font face="'.$FontFace.'" size="'.$FontSize.'"><b>$'.number_format($TotalSales,2).'</b></font></td>
    </tr>';
				
					
				} // end if rows
				
				$PageContents .= '</table>';			
							
				
			} // end if sales
		
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}

	}
	else {
	
		RedirectToLogin();
	
	}

	// ========================================================================================




?>
