<?php
include('POModule/Class-ItemMaster.php');
include('includes/stockcalculation.php');
$function_filter[]=$_REQUEST['site'];
$function_filter[]='';
$function_filter[]=date('Y-m-d');
?>
<?php
if($_REQUEST['Save']){

	for($i=0;$i<count($_REQUEST['max_level']);$i++)
	{ 
            if($_REQUEST['max_level'][$i]!=''){
           
          if($_REQUEST['site']!='0' && $_REQUEST['storecode']!='0'){
                $sqlId=DB_query("SELECT id FROM item_level_creation WHERE site='".$_REQUEST['site']."' AND store='".$_REQUEST['storecode']."' AND item_id='".$_REQUEST['hid_itemId'][$i]."'",$db);
                $resultId=DB_fetch_array($sqlId);
                if($resultId){
                $data['site']=$_REQUEST['site'];
		$data['store'] = $_REQUEST['storecode'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = $_REQUEST['max_level'][$i];
                $data['min_level'] = $_REQUEST['min_level'][$i]; 
                $data['reorder_level'] = $_REQUEST['reorder_level'][$i];
				$data['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
                $data['max_qty']     = $_REQUEST['max_qty'][$i];
                $cond="id='".$resultId['id']."'";
	        $s->RecordEdit('item_level_creation',$data,$cond); 

		$itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);

                }else{
		$data['site']=$_REQUEST['site'];
		$data['store'] = $_REQUEST['storecode'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = $_REQUEST['max_level'][$i];
                $data['min_level'] = $_REQUEST['min_level'][$i]; 
                $data['reorder_level'] = $_REQUEST['reorder_level'][$i];
				$data['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
                $data['max_qty']     = $_REQUEST['max_qty'][$i];
                 $s->RecordInsert('item_level_creation',$data);
$itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);
                } 
              
               
            }else if($_REQUEST['site']!='0' && $_REQUEST['storecode']=='0'){
          
                $sqlStore=DB_query("select id from storetype where CompanyId='".$_SESSION['ACGCompanyId']."' and Store_Site='".$_REQUEST['site']."'",$db);
                $num=DB_num_rows($sqlStore);
	           while($resultStore=DB_fetch_array($sqlStore))
	        {                
           $sqlId=DB_query("SELECT id FROM item_level_creation WHERE site='".$_REQUEST['site']."' AND store='".$resultStore['id']."' AND item_id='".$_REQUEST['hid_itemId'][$i]."'",$db);
                $resultId=DB_fetch_array($sqlId);
                if($resultId){
                $data['site']=$_REQUEST['site'];
		$data['store'] = $resultStore['id'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = round(($_REQUEST['max_level'][$i]/$num),0);
                $data['min_level'] = round(($_REQUEST['min_level'][$i]/$num),0); 
                $data['reorder_level'] = round(($_REQUEST['reorder_level'][$i]/$num),0);
				$data['reorder_qty'] = round(($_REQUEST['reorder_qty'][$i]/$num),0);
                $data['max_qty']     = round(($_REQUEST['max_qty'][$i]/$num),0);
                $cond="id='".$resultId['id']."'";
	        $s->RecordEdit('item_level_creation',$data,$cond);
			
			$itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);
                }else{
                $data['site']=$_REQUEST['site'];
		$data['store'] = $resultStore['id'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = round(($_REQUEST['max_level'][$i]/$num),0);
                $data['min_level'] = round(($_REQUEST['min_level'][$i]/$num),0); 
                $data['reorder_level'] = round(($_REQUEST['reorder_level'][$i]/$num),0);
				$data['reorder_qty'] = round(($_REQUEST['reorder_qty'][$i]/$num),0);
                $data['max_qty']     = round(($_REQUEST['max_qty'][$i]/$num),0);
                $s->RecordInsert('item_level_creation',$data);

				$itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);
                }
                }
                   // }
                }else if($_REQUEST['site']=='0' && $_REQUEST['storecode']=='0'){
     
                $sqlStore1=DB_query("SELECT storetype.id,locations.loccode
                       FROM locations INNER JOIN  storetype ON locations.loccode=storetype.Store_Site
                      WHERE locations.CompanyId='".$_SESSION['ACGCompanyId']."'",$db);
                 $num=DB_num_rows($sqlStore1);
	           while($resultStore1=DB_fetch_array($sqlStore1))
	        {                
          $sqlId=DB_query("SELECT id FROM item_level_creation WHERE site='".$resultStore1['loccode']."' AND store='".$resultStore1['id']."' AND item_id='".$_REQUEST['hid_itemId'][$i]."'",$db);
                $resultId=DB_fetch_array($sqlId);
                if($resultId){
                $data['site']=$resultStore1['loccode'];
		$data['store'] = $resultStore1['id'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = round(($_REQUEST['max_level'][$i]/$num),0);
                $data['min_level'] = round(($_REQUEST['min_level'][$i]/$num),0); 
                $data['reorder_level'] = round(($_REQUEST['reorder_level'][$i]/$num),0);
				$data['reorder_qty'] = round(($_REQUEST['reorder_qty'][$i]/$num),0);
                $data['max_qty']     = round(($_REQUEST['max_qty'][$i]/$num),0);
                $cond="id='".$resultId['id']."'";
	        $s->RecordEdit('item_level_creation',$data,$cond); 
			$itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);
                }else{
                $data['site']=$resultStore1['loccode'];
		$data['store'] = $resultStore1['id'];
                $data['item_id'] = $_REQUEST['hid_itemId'][$i]; 
                $data['max_level'] = round(($_REQUEST['max_level'][$i]/$num),0);
                $data['min_level'] = round(($_REQUEST['min_level'][$i]/$num),0); 
                $data['reorder_level'] = round(($_REQUEST['reorder_level'][$i]/$num),0);
				$data['reorder_qty'] = round(($_REQUEST['reorder_qty'][$i]/$num),0);
                $data['max_qty']     = round(($_REQUEST['max_qty'][$i]/$num),0);
                 $s->RecordInsert('item_level_creation',$data);
				 $itemData = array();
$itemData['max_level']     = $_REQUEST['max_level'][$i];
$itemData['min_level']     = $_REQUEST['min_level'][$i];
$itemData['reorder_level'] = $_REQUEST['reorder_level'][$i];
$itemData['reorder_qty'] = $_REQUEST['reorder_qty'][$i];
$itemData['max_qty']     = $_REQUEST['max_qty'][$i];

$itemCond = "id='".$_REQUEST['hid_itemId'][$i]."'";
$s->RecordEdit('item_master', $itemData, $itemCond);
                }
                }
                  //  }
		
              
            }
         }
    }
                 $_SESSION['success_msg']="Save Successfully";
	$s->pageLocation("index.php?route=Inventory/maintenance/item_level_creation".$rurl);
               
}
	

if(isset($_REQUEST['search']))
{ 

	$result=DB_query("select * from item_master where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 

	while($result_row1=DB_fetch_assoc($result))
	{
		$result_row[]=$result_row1;
	}
}

 
 
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Item Level Creation</h1>
<div align="center">
<form method="post" action="">
<table width="100%" cellpadding="2" cellspacing="0" border="1">
	<tr>
		<th >Branch*</th>
		<td><select name="site" id="site" style="width:70%;" onchange="GetStore(this.value)">
			<option value="0">All</option>
			<?php $location=DB_query("SELECT * FROM `locations` WHERE companyid='".$_SESSION['ACGCompanyId']."'",$db);
						while($all_row=DB_fetch_assoc($location))
						{ ?>
							<option value="<?php echo $all_row['loccode']; ?>" <?php if($_REQUEST['site']==$all_row['loccode']){ ?> selected="selected" <?php } ?>      ><?php echo $all_row['locationname']; ?></option>
						<?php }?>
		</select></td>
                <th >Store*</th>
		<td >
               <select id="storecode" name="storecode"  style="width:70%;">
	<?php 
        $sql_store =DB_query("select id, StoreName from storetype where Store_Site='".$_REQUEST['site']."' and id='".$_REQUEST['storecode']."' and CompanyId='".$_SESSION['ACGCompanyId']."'",$db);?>
              <option value="0">All</option>
<?php while($Storedetail=DB_fetch_array($sql_store)){?>
<option value="<?php echo $Storedetail['id']; ?>" <?php echo $_REQUEST['storecode']==$Storedetail['id']?'selected':'';?>><?php echo $Storedetail['StoreName']; ?></option>
<?php }?>
		</select></td>
		<td><input type="submit" name="search" class="search" value="Search" /></td>
	</tr>
</table>

<table width="98%" border="1" cellpadding="2" cellspacing="1" style="border-collapse:collapse;" align="center">
<tr>
<th><strong>S.No</strong></th>
<th><strong>Item Code</strong></th>
<th><strong>Item Name</strong></th>
<th><strong>Unit</strong></th>
<th><strong>Max. Level</strong></th>
<th><strong>Min. Level</strong></th>
<th><strong>Reorder Level</strong></th>
<th><strong>Reorder Quantity</strong></th>
<th><strong>Max Quantity</strong></th>
</tr>
<?php
if(!(empty($result_row)))
{
$i=1;
	foreach($result_row as $all_data)
	{ 
if($_REQUEST['site']!='0' && $_REQUEST['storecode']!='0'){
 $sqlStoreSum=DB_query("SELECT sum(max_level) AS max_level,sum(min_level) AS min_level,sum(reorder_level) AS reorder_level,sum(reorder_qty) AS reorder_qty,sum(max_qty) AS max_qty FROM item_level_creation WHERE item_id='".$all_data['id']."' AND site='".$_REQUEST['site']."' AND store='".$_REQUEST['storecode']."'",$db);
}
 if($_REQUEST['site']!='0' && $_REQUEST['storecode']=='0'){
 $sqlStoreSum=DB_query("SELECT sum(max_level) AS max_level,sum(min_level) AS min_level,sum(reorder_level) AS reorder_level,sum(reorder_qty) AS reorder_qty,sum(max_qty) AS max_qty FROM item_level_creation WHERE item_id='".$all_data['id']."' AND site='".$_REQUEST['site']."'",$db);
}
 if($_REQUEST['site']=='0' && $_REQUEST['storecode']=='0'){
 $sqlStoreSum=DB_query("SELECT sum(max_level) AS max_level,sum(min_level) AS min_level,sum(reorder_level) AS reorder_level,sum(reorder_qty) AS reorder_qty,sum(max_qty) AS max_qty FROM item_level_creation WHERE item_id='".$all_data['id']."'",$db);
}
 $resultStoreSum=DB_fetch_array($sqlStoreSum);
?>
	<tr>
	<td><?php echo $i; ?><input type="hidden" name="hid_itemId[]" value="<?php echo $all_data['id']; ?>">
        </td>
	<td><?php echo $all_data['StockID']; ?></td>
	<td><?php echo $all_data['item_name']; ?></td>
	<td><?php $unit=DB_fetch_assoc(DB_query("select unitname from unitsofmeasure where unitid='".$all_data['mst_unitid']."'",$db)); echo $unit['unitname'] ?></td>
	<td><input type="text" style="width:100%;" name="max_level[]" value="<?php echo $resultStoreSum['max_level']?>"></td>
	<td><input type="text" style="width:100%;" name="min_level[]" value="<?php echo $resultStoreSum['min_level']?>"></td>
	<td><input type="text" style="width:100%;" name="reorder_level[]" value="<?php echo $resultStoreSum['reorder_level']?>"></td>
	<td><input type="text" style="width:100%;" name="reorder_qty[]" value="<?php echo $resultStoreSum['reorder_qty']?>"></td>

     <td><input type="text" style="width:100%;" name="max_qty[]" value="<?php echo $resultStoreSum['max_qty']?>"></td>
	</tr>
<?php $i++; } ?>
<tr class="row1">
<td colspan="14"><p align="center"><input type="submit" name="Save" value="Save"  class="search" onclick="return confirm('Are You Sure?')" ></p></td>
<!--onclick="return confirm('This Associate will be deleted. Are you sure?')"-->
</tr>
 <?php }
 else { ?>
<tr>
	<td colspan="9">No Record Found</td>
</tr>
<?php }?>
 </table>
    </form>
</div>
</div>
</div>
<script>
	function validate()
	{
	var site=document.getElementById('site').value;
		if(site=='0')
		{
		alert("Select the Site First");
		return false;
		}
	}
function GetStore(str)
{
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
	if (xmlhttp.readyState==4 && xmlhttp.status==200)
	{
		document.getElementById("storecode").innerHTML=xmlhttp.responseText;
	}
	}
	xmlhttp.open("GET","ajax_getSote_item.php?branchcode="+str,true);
	xmlhttp.send();
}
</script>