<?php

session_start();
header("Content-type: text/html; charset=".$_SESSION['encodingmode']);
if($_SESSION['dbName']==""){
header("location:../index.php");
}

include("config.inc.php");
include("function.inc.php");

$connection = mysql_connect($config['dbServer'],$config['dbUser'],$config['dbPass']) or die("Could not connect to DB");
mysql_select_db($_SESSION['dbName'],$connection) or die("Could not find DB");



if(	$_SESSION['encodingmode'] == 'utf8' || $_SESSION['encodingmode'] == 'utf-8'){
	mysql_query("SET NAMES 'utf8'"); 
	mysql_query('SET CHARACTER SET utf8'); 
	mysql_set_charset('utf8', $connection);
}

//Read From Setup Table							
$querysetup= " select grid_color1, grid_color2,maxperpage,base1,base2,DecimalBase,DecimalBase1,DecimalBase2,maxperpage from setup ";
$resultsetup=mysql_query($querysetup,$connection) or die(mysql_error());
while($rowsetup=mysql_fetch_array($resultsetup)){
	extract($rowsetup);
}

$querylog="SELECT language from ".$_SESSION['dbLabel'].".login where user_account='".$_SESSION['useraccount']."' 	and Upper(Login_Schema) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
	

$querylog="SELECT ifnull(FI_bulkitemminmaxAdjust,0)  as loginaccess from ".$_SESSION['dbLabel'].".ListUserDir where Usercode='".$_SESSION['useraccount']."' 	and Upper(concat(CompanyName,'_',Diryear)) = Upper('".$_SESSION['dbName']."')	";
	$resultlog=mysql_query($querylog,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlog)){
		extract($rows);
	}
	
$items = json_decode($_POST['items'],true);			
	
$querylang="select language, buttonclear, save, print, find, clear, results, page from ".$_SESSION['dbLabel'].".buttons_label 
where language='$language'";


$resultlang=mysql_query($querylang,$connection) or die(mysql_error());
	while($rows=mysql_fetch_array($resultlang)){
		//extract($rows);
		
		$buttonclear=$rows[1];
		$save=$rows[2];
		$print=$rows[3];
		$find=$rows[4];
		$clear=$rows[5];
		$results=$rows[6];
		$page=$rows[7];
		
	}

	
if($AdjustDays == '') $AdjustDays = 7;
if($AdjustCoef== '') $AdjustCoef = 10;

if ($loginaccess=='' or $loginaccess==0)
	header("location:menu.php");
	//echo "$itemloaded";
if($itemloaded=='') $itemloaded=0;	

$sellingprice_label = array('Price','Price Local', 'Price Export', 'Price 3', 'Price 4', 'Price 5');	
print <<<HERE

<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-utf8" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 


<link rel="stylesheet" type="text/css" media="all" href="css-v2/style.css">


<title>Items min. Max. Qty Adjust</title>

<style type="text/css">
/* Hide HTML5 Up and Down arrows. */
input[type="number"]::-webkit-outer-spin-button, input[type="number"]::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
input[type="number"] {
-moz-appearance: textfield;
}

body,td,th {
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size: 11px;
}
body {
	margin-top: 2px;
	margin-left: 1px;
}
a:link {
	text-decoration: none;
	color: #0000FF;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: none;
	color: #0000FF;
}
a:active {
	text-decoration: none;
	color: #0000FF;
}

.style12 {
	font-size: 9px;
	color: #999999;
	font-weight: bold;
}
.style15 {color: #5151FF; font-size: 10px;}
.style16 {
	font-size: 16px;
	color: #FF0000;
	font-weight: bold;
	font-family: "Times New Roman", Times, serif;
}
.style17 {font-size: 9px}
.style25 {color: #999999; font-weight: bold; font-size: 10px; }
.style26 {color: #FF0000}
.style27 {color: #999999 ;font-size: 10px;}
.style28 {color: #FFFFFF ;font-size: 10px;}

</style>

<link type="text/css" rel="stylesheet" href="dhtmlgoodies_calendar.css?random=20051112" media="screen"></LINK>
<SCRIPT type="text/javascript" src="dhtmlgoodies_calendar.js?random=20060118"></script>

</head>
<body >
<form name='sellingprice' method='post' enctype='multipart/form-data'>
<input type=hidden name='message' value='$message'>
<input type=hidden name='itemloaded' value='$itemloaded'>
<input type='hidden' name='items' id='items' value='$items'>
<input type='hidden' name='loadedfromexcel' id='loadedfromexcel' value='$loadedfromexcel'>
HERE;
echo "<input type='hidden' name='items2' id='items2' value='".json_encode($items)."'>";



echo"<table cellpadding='0' cellspacing='0' border='0'  align='center' class='layout-width'>";
include_once("header.php");
echo" <tr><td  valign='top'>";

include_once("sidemenu.php");
echo"  </td></tr>";


echo" <tr style='height:26px;'>
		<td  valign='top' id='content-top'>
			<table border='0' cellpadding='0' cellspacing='0' style='table-layout:fixed; height:26px;' class='layout-width'>
			<tr style='height:26px;'>
				<td style='width:38px; height:26px;'>&nbsp;</td>
				<td valign='middle' style='width:300px; height:26px;'><span class='title'>Items min. Max. Qty Adjust</span></td>
				<td class='message' valign='middle' style='height:26px;'>".htmlspecialchars($message, ENT_QUOTES)."</td>
			</tr></table>
		</td>
	  </tr>";


echo"<tr>
		<td  height='34'  valign='top' id='content-top2'>
			<table border='0' cellpadding='0' cellspacing='0' class='layout-width'>
			<tr>
				<td width='38'  height='34'>&nbsp;</td>
				<td valign='middle' height='34' colspan = '2'>";
echo"		<button type='button' id='btnClear' name='btnClear' onclick=\"ClearPage(); return false;\" title='Clear Page'><span><span>$buttonclear</span></span></button>";
if($itemloaded==1)
echo"		<button type='button' id='btnSave' name='btnSave' onclick=\"savePage(); return false;\" title='Save Page'><span><span>$save</span></span></button>";
echo"		<button type='button' id='btnLoad' name='btnLoad' onclick=\"loadItems(); return false;\" title='Load Items'><span><span>Load Items</span></span></button>";
echo"		<button type='button' id='btnPrint' name='btnPrint' onclick=\"printItems('0'); return false;\" title='Print'><span><span>Print</span></span></button>";
echo"		<button type='button' id='btnExcel' name='btnExcel' onclick=\"printItems('1'); return false;\" title='Print'><span><span>Excel</span></span></button>";

echo"   </td></tr>";
echo"	</table>";
echo"</td></tr>";

echo"<tr><td id='content-body'>";
echo"<table border='0'  align='center' valign='top' width=100% class='tablecontent'>
	<col width=65%>";

echo"   <tr bgcolor='#F7F7F7'><td valign='top'>";	 

	  
echo"  
        <table border='0' style='table-layout:fixed' align='left' valign='top'>
		<col width=120px><col width=180px><col width=120px><col width=180px><col width=120px><col width=180px>
		 ";

echo"<tr bgcolor='#F7F7F7' height='25'>
           <td valign='center' align='left' ><span class='style1'>Group Code</span></td>
           <td valign='center' align='left' ><input type='text' name='group_code' id='group_code' value='$group_code' style='width:120px;'>
				<img src='img/btnopen.jpg' width15px height=15px onmouseover=\"style.cursor='hand'\" onclick=\"openWindow('group');\">
		   </td>
           <td valign='center' align='left' ><span class='style1'>Subgroup Code</span></td>
           <td valign='center' align='left' ><input type='text' name='subgroup_code' id='subgroup_code' value='$subgroup_code' style='width:120px;'>
				<img src='img/btnopen.jpg' width15px height=15px onmouseover=\"style.cursor='hand'\" onclick=\"openWindow('subgroup');\">
		   </td>
		   <td valign='center' align='left' ><span class='style1'>Category Code</span></td>
           <td valign='center' align='left' ><input type='text' name='category_code' id='category_code' value='$category_code' style='width:120px;'>
				<img src='img/btnopen.jpg' width15px height=15px onmouseover=\"style.cursor='hand'\" onclick=\"openWindow('category');\">
		   </td>
	 </tr>";

echo "<tr bgcolor='#F7F7F7' height='25'> 
		<td><span class='style1'>Days</span></td>	
		<td><input type='number' name='AdjustDays' id='AdjustDays' value='$AdjustDays' style='width:120px;' onkeyup=\"setNewVal(this.id);\"  max=100 min=-100></td>
		<td><span class='style1'>Max = min * </span></td>	
		<td><input type='number' name='AdjustCoef' id='AdjustCoef' value='$AdjustCoef' style='width:120px;' onkeyup=\"setNewVal(this.id);\" max=100 min=-100></td>
		<td><span class='style1'>Default Min </span></td>	
		<td>";
		echo "<select id='defmin' name='defmin' > ";
			echo"<option value='1'"; if($defmin == 1) echo "Selected"; echo ">Sold Quantity</option>";
			echo"<option value='2'"; if($defmin == 2) echo "Selected"; echo ">Purchased Quantity</option>";
			echo"<option value='3'"; if($defmin == 3) echo "Selected"; echo ">Difference</option>";
		echo "</select> ";
		echo"</td>";
echo"</tr>";
	
echo"</table></td></tr><tr>";

echo"<td  valign='top'>";

 
echo"<table style='width:1050px;' border='0' cellspacing=0>
	 
	 <tr><td  valign='top' colspan=3 id='' style='width:1050px;'>
	 <table style='width:1050px;' border='0' cellspacing=0>
	 <col width=120>
	 <col width=200> 
	 <col width=90>  
	 <col width=90>
	 <col width=90>
	 <col width=90>
	 <col width=90>
	 <col width=90>
	 <col width=90>
	<tr height='30px' style='background-color:darkgray;'>
	<td align='left'><span class='style1'><b>Item Code</b></td>
	<td align='left'><span class='style1'><b>Item Description</b></td> 
	<td align='center'><span class='style1'><b>Old Min. Qty</b></td>
	<td align='center'><span class='style1'><b>Old Max. Qty</b></td>
	<td align='center'><span class='style1'><b>Total Purchases</b></td>
	<td align='center'><span class='style1'><b>Total Sales</b></td>
	<td align='center'><span class='style1'><b>Diff </b></td>
	<td align='center'><span class='style1'><b>New Min. Qty</b></td>
	<td align='center'><span class='style1'><b>new Max. Qty</b></td>
	</tr></table></td></tr>";
 	$queryload = "SELECT
				itms.item_code, itms.Item_Description , IFNULL(Q.totQtyPurchased,0), IFNULL(Q2.totQtydelivered,0), IFNULL(Q.totQtyPurchased,0) - IFNULL(Q2.totQtydelivered,0)qtydiff , ITEM_MINQTY,ITEM_MAXQTY
				FROM
					items itms
					LEFT JOIN (
					SELECT item_code, IFNULL(sum( PurchaseDt_Quantity ),0 ) as totQtyPurchased  FROM purchasesdt WHERE Purchase_Date >= DATE_ADD( NOW( ), INTERVAL - $AdjustDays DAY ) AND Journal_Type = 'PI'  group by purchasesdt.item_code
					) Q ON  itms.item_code = Q.item_code 
					
					LEFT JOIN (
					SELECT item_code, IFNULL(sum( deliveryDt_Quantity ),0 ) as totQtydelivered  FROM deliverydt WHERE delivery_Date >= DATE_ADD( NOW( ), INTERVAL - $AdjustDays DAY ) AND Journal_Type = 'DI'  group by deliverydt.item_code
					) Q2 ON  itms.item_code = Q2.item_code 
	 
			where   if('$group_code'='',1=1, itms.group_code = '$group_code')
					and if('$subgroup_code'='',1=1, itms.subgroup_code = '$subgroup_code') 
					and if('$category_code'='',1=1, itms.category_code = '$category_code') and $itemloaded = 1"; 
	// TO Comment Here if Tony dont want it
	$queryload .= " and (	itms.item_code in (select distinct item_code from purchasesdt where Purchase_Date >= DATE_ADD(now(),INTERVAL - $AdjustDays day))";
	$queryload .= "			 or (itms.item_code in (select distinct item_code from deliverydt where delivery_Date >= DATE_ADD(now(),INTERVAL - $AdjustDays day)))";
	$queryload .= "	)";
	// TO Comment Here if Tony dont want it
	$queryload .= "order by itms.item_code desc ";
	$i=0;
 	echo "<tr><td  valign='top' colspan=3 id='' style='width:1050px;'><div style='overflow-x:hidden;overflow-y:scroll;width:100%;height:700px;'>
		<table style='width:1050px;' border='0' cellspacing=0>
		<col width=120>
		<col width=200> 
		<col width=90>  
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>
		<col width=90>";
	
	// echo $queryload;
	$resload = mysql_query($queryload,$connection);
	while($rowload = mysql_fetch_array($resload)){	
			echo "<tr>"; 
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' name='item_code[$i]' id='item_code[$i]' value='".$rowload[0]."' readOnly /></td>";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[1]."' readOnly /></td> ";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[5]."' readOnly /></td>";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[6]."' readOnly /></td>";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[2]."' readOnly /></td>";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[3]."' readOnly /></td>";
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' value='".$rowload[4]."' readOnly /></td>";
				if($defmin == 1)	$minqty = $rowload[3];
				if($defmin == 2)	$minqty = $rowload[2];
				if($defmin == 3)	$minqty = $rowload[4];

				if($minqty == 0 && $rowload[5] > 0) 	$minqty = $rowload[5];
				if($minqty == 0 && $rowload[5] ==  0) 	$minqty = 0;
				 
				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' name='item_Nminqty[$i]' id='item_Nminqty[$i]' value='".$minqty."' /></td>";
				// $rowload[2]  * $AdjustCoef
				$maxqty =  $minqty * $AdjustCoef;

				echo "<td align='left'><span class='style1'><input style='width:100%;'  type='text' name='item_Nmaxqty[$i]' id='item_Nmaxqty[$i]' value='".$maxqty."' /></td>";
			echo "</tr>"; 
			$i++;	
		
	}
echo"</table></div></td></tr>";		
echo"<tr height='50px'><td id='' colspan='2'><table width=100% border='0'><tr><td align='center' width=50%>";
		
	
		echo"</td><td  align='center'>";
		
		
		echo"</td></tr><tr>
					<td id='' style='width:1050px;height:20px;' colspan='6'></td>
				</tr></table></td></tr>";
			


echo"</tr></table></td></tr>";	
echo"</tr></table></td></tr>";	
	 
			 
		  

print <<< HERE
       </td>
      </tr>  <tr height='30'><td align='center' id='content-bottom' ></td></tr>
	  </table>
HERE;


print <<<HERE
</form>

<script>
 
function loadItems(){
	document.sellingprice.itemloaded.value=1;
	document.sellingprice.submit();
}

function ClearPage(){
	document.sellingprice.group_code.value 			= '';
	document.sellingprice.subgroup_code.value 		= '';
	document.sellingprice.category_code.value 		= '';
	document.sellingprice.AdjustDays.value 	= '';
	document.sellingprice.AdjustCoef.value 			= '';
	document.sellingprice.itemloaded.value 			= '';
	document.sellingprice.message.value 			= ''; 
	document.sellingprice.action = 'Form_ItemsAdjust.php';
	document.sellingprice.submit();
}

function printItems(exp){
	let str = 'rep_itemsMinMaxQty.php';
 	let group_code = document.sellingprice.group_code.value;
	let subgroup_code = document.sellingprice.subgroup_code.value;
	let category_code = document.sellingprice.category_code.value;
	let AdjustDays = document.sellingprice.AdjustDays.value;
	let AdjustCoef = document.sellingprice.AdjustCoef.value;
	 
		
		str = str+'?AdjustDays='+AdjustDays+'&AdjustCoef='+AdjustCoef+'&group_code='+group_code+'&subgroup_code='+subgroup_code+'&category_code='+category_code+'&exp=0';   
		window.open(str);
	 
	
} 

function printHistory(exp){
	let str = 'rep_itemsMinMaxQty.php';
 	let group_code = document.sellingprice.group_code.value;
	let subgroup_code = document.sellingprice.subgroup_code.value;
	let category_code = document.sellingprice.category_code.value;
	let AdjustDays = document.sellingprice.AdjustDays.value;
	let AdjustCoef = document.sellingprice.AdjustCoef.value;
	 
		
		str = str+'?AdjustDays='+AdjustDays+'&AdjustCoef='+AdjustCoef+'&group_code='+group_code+'&subgroup_code='+subgroup_code+'&category_code='+category_code+'&exp=1';   
		window.open(str);
	 
	
}

function savePage(){
	if(confirm('are you sure you want to update Min And Max Qty ?')==true){
		let alertVal = 1;
	
	if ( document.sellingprice.AdjustDays.value != 0 && document.sellingprice.itemloaded.value == 1 && document.sellingprice.AdjustCoef.value != 0  )
		alertVal =0;
	if ( alertVal == 1) {
		alert('No Item Was Loaded!');
		return 0;		
	} else {
		document.sellingprice.action = 'Form_itemsAdjust_Actions.php?mode=changeQtys';
		document.sellingprice.submit();		
	}
	}
	
}
function openWindow(winName){
	let strName = '';
	if(winName=='group')
		strName= 'List_Grouping.php?str=16';
	else if(winName=='subgroup')
		strName= 'List_SubGroup.php?str=11';
	else if(winName=='category')
		strName= 'List_Categories.php?str=9';
	else if(winName=='history')
		strName= 'Form_BrowseSellingPrice.php';
	
	window.open(strName,winName, 'width=1000px,height=600px');
}



</script>
</body>

HERE;

?>