<?

/*
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][263];
	$BrowserTitle = $MsgText[9][263];
	$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][263];
	
	if ($LoggedIn == 'y') {
		
		ReadPermissions();
		Global $Permissions;
		
		if ($Permissions[906] == 'y') {
		
		
			$query = "select * from `".$Settings['DBPrefix']."ec-Settings`";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				$ecSettings[$row['Name']] = $row['Value'];
			}
		
		
			
			$query = "select * from `".$Settings['DBPrefix']."ec-Categories` ORDER BY Name ASC, RecordNumber ASC";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) { 
				$CategoryIDsArray[$row['Parent']][] = $row['RecordNumber'];
				$CategoryNamesArray[$row['Parent']][] = $row['Name'];
				$CategoryExists[$row['RecordNumber']] = 'y';
			}
			
			$query = "select * from `".$Settings['DBPrefix']."ec-Products` where Active=1 ORDER BY Name ASC, RecordNumber ASC";
			$result = mysql_query($query) or die (mysql_error());
			while ($row = mysql_fetch_array($result)) {
				if ($CategoryExists[$row['Category']] == 'y' || $row['Category'] == 0) {
					$ProductIDsArray[$row['Category']][] = $row['RecordNumber'];
					$ProductNamesArray[$row['Category']][] = $row['Name'];
					
					if ($row['Category2'] > 0) {
						$ProductIDsArray[$row['Category2']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category2']][] = $row['Name'];
					}
					if ($row['Category3'] > 0) {
						$ProductIDsArray[$row['Category3']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category3']][] = $row['Name'];
					}
					if ($row['Category4'] > 0) {
						$ProductIDsArray[$row['Category4']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category4']][] = $row['Name'];
					}
					if ($row['Category5'] > 0) {
						$ProductIDsArray[$row['Category5']][] = $row['RecordNumber'];
						$ProductNamesArray[$row['Category5']][] = $row['Name'];
					}
				}
				else {
					$OrphanProducts++;
					$OrphanProductIDsArray[] = $row['RecordNumber'];
					$OrphanProductNamesArray[] = $row['Name'];
				}
				
				
				$query2 = "select * from `".$Settings['DBPrefix']."ec-Items` where Product=".$row['RecordNumber']." limit 2";
				$result2 = mysql_query($query2) or die (mysql_error());
				if (mysql_num_rows($result2) > 1) { 
					$ProductsWithItemsArray[] = $row['RecordNumber'];
					$ProductsWithItemsNamesArray[] = $row['Name'].' ('.$row['ProductID'].')';
				}
				
			}
			
			
			
			function PrintItemsInventory($ProductRecordNumber, $SpacerSection) {
				Global $Settings, $FontFace, $FontSize;
			
				$PIIContents = '';
			
				$query27 = "select * from `".$Settings['DBPrefix']."ec-Items` where Product=".$ProductRecordNumber." order by Option1, Option2, Option3, ItemID";
				$result27 = mysql_query($query27) or die (mysql_error());
				if (mysql_num_rows($result27) > 0) {
					$PIIContents .= '<table border="0" cellpadding="0" cellspacing="0">';
					while ($row27 = mysql_fetch_array($result27)) {
						if ($row27['Stock'] == -1) {
							$row27['Stock'] = '--';
						}
						
						if ($BGColor == '#ffffff') {
							$BGColor = '#eeeeee';
						}
						else {
							$BGColor = '#ffffff';
						}
					
						$PIIContents .= '<tr><td><font face="'.$FontFace.'" size="'.$FontSize.'">'.$SpacerSection.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font></td><td bgcolor="'.$BGColor.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;'.$row27['ItemID'].'&nbsp;</font></td><td bgcolor="'.$BGColor.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;'.$row27['Option1'].'&nbsp;</font></td><td bgcolor="'.$BGColor.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;'.$row27['Option2'].'&nbsp;</font></td><td bgcolor="'.$BGColor.'"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;'.$row27['Option3'].'&nbsp;</font><td bgcolor="'.$BGColor.'" align="center"><font face="'.$FontFace.'" size="'.$FontSize.'">&nbsp;<b>'.$row27['Stock'].'</b>&nbsp;</font></td></td></tr>';
					}
					$PIIContents .= '</table>';
				}
				
				
				return $PIIContents;
			
			}
			
			
						
			
			$query = "select Category from `".$Settings['DBPrefix']."ec-Products` ORDER BY RecordNumber DESC LIMIT 1";
			$result = mysql_query($query) or die (mysql_error());
			$row = mysql_fetch_array($result);
			$LastProductParent = $row['Category'];
			
			
			
			if ($Arguments3 != 'printable') {
				$PageContents .= '<br><b><a href="'.$MainURL.'/admin/store-inventory/printable.html" target="_blank">'.$MsgText[1][98].'</a></b>';
			}
			else {
				$PageContents .= '<div align="center"><b><font size="5">'.$Settings['SiteName'].'<br>'.$MsgText[9][263].'</font><br><br>'.date('F j, Y @ g:ia',GetTimestamp()).'</b></div>';
			}
			
			
			
			for ($j = 0; $j < count($ProductIDsArray[0]); $j++) {
				$CategoriesPrint .= '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$ProductNamesArray[0][$j].'';
				$PageContents .= PrintItemsInventory($ProductIDsArray[0][$j], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
			}
			
			if ($OrphanProducts > 0) {
				$CategoriesPrint .= '<br>Orphan Products';
				for ($j = 0; $j < count($OrphanProductIDsArray); $j++) {
					$CategoriesPrint .= '<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$OrphanProductNamesArray[$j].'';
					$PageContents .= PrintItemsInventory($OrphanProductIDsArray[$j], '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;');
				}
			}


			$CatParent5 = 0;
			$LastParentAbove5 = '0';
			$CatLevel5['0'] = 0;
									
			function SpiderCategories5($CatParent5) {
							
				Global $CategoryIDsArray, $CategoryNamesArray, $CategoryDropdown, $CategoryDropdown2, $LastParent5, $LastParentAbove5, $CatSpacersArray5, $CatLevel5, $CatsCount5, $CurrentPageCategory, $MainURL, $CategoriesPrint, $ProductIDsArray, $ProductNamesArray, $LastProductParent;
							
				for ($i = 0; $i < count($CategoryIDsArray[$CatParent5]); $i++) {
								
					$LastParentAboveB5 = $LastParentAbove5;
					$LastParentB5 = $LastParent5;
								
					$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5];
					if ($CatParent5 != $LastParent5 && $CatParent5 == $LastParentAbove5) {
							
						$CatLevel5[$CategoryIDsArray[$CatParent5][$i]] = $CatLevel5[$CatParent5] + 1;
						$CatLevel5[$CatParent5]++;
															
						$CatsCount5[$CatLevel5[$CatParent5]] = 0;
							
					}
								
					$LastParent5 = $CatParent5;
					$LastParentAbove5 = $CategoryIDsArray[$CatParent5][$i];
								
					$CatsCount5[$CatLevel5[$CatParent5]]++;
								
										
					$CatSpacer5 = '';
					for ($x = 0; $x < $CatLevel5[$CatParent5]; $x++) {
									
						if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5]) && $x == $CatLevel5[$CatParent5]-1) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'y';
						}
						else if ($CatsCount5[$CatLevel5[$CatParent5]] == count($CategoryIDsArray[$CatParent5])) {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
						}
						else {
							if ($x < $CatLevel5[$CatParent5]) {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
								$ResetAfter5 = 'y2';
							}
							else {
								$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							}
						}
						
							
						$CatSpacer5 .= $CatSpacersArray5[$x+1];
						
						if ($ResetAfter5 == 'y') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
						else if ($ResetAfter5 == 'y2') {
							$CatSpacersArray5[$CatLevel5[$CatParent5]] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
							$ResetAfter5 = 'n';
						}
													
					}
					
					
					
					$CategoryID = $CategoryIDsArray[$CatParent5][$i];
				
					
						
					$CategoriesPrint .= '<br><br><br>'.$CatSpacer5.' <b>'.$CategoryNamesArray[$CatParent5][$i].'</b>';
						
						
						
					for ($j = 0; $j < count($ProductIDsArray[$CategoryID]); $j++) {
						$CategoriesPrint .= '<br><br>'.$CatSpacer5.'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.$ProductNamesArray[$CategoryID][$j].'';
						$CategoriesPrint .= PrintItemsInventory($ProductIDsArray[$CategoryID][$j], $CatSpacer5);
					}	
					
								
												
					
					if (count($CategoryIDsArray[$CategoryIDsArray[$CatParent5][$i]]) > 0) {
						SpiderCategories5($CategoryIDsArray[$CatParent5][$i]);
					}
							
				}
							
							
			}
			
			SpiderCategories5('0');
			
			
			
			
			


			$PageContents .= $CategoriesPrint;
		
		
		}
		else {
		
			$PageContents = $MsgText[0][97];
		
		}
		
		if ($Arguments3 == 'printable') {
			print $PageContents;
			exit();
		}

	}
	else {
	
		RedirectToLogin();
	
	}

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




?>
