<?php session_name('CAKEPHP'); session_start();
error_reporting(FALSE);
$PageSecurity = 11;
include('includes/DefinePOClass.php');
include('includes/DefineSerialItems.php');
include('includes/SQL_CommonFunctions.inc');
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>Vendor Wise Item Register</h1>
<form method="post" action="" id="vendor_form">
<table width="100%" cellpadding="3" cellspacing="0" border="0">
   	<tr class="row1">
		<td width="13%" align="right">Branch</td>
		<td width="35%"><select name="site" id="site">
	                <option value="0">Select</option>
	                <?php 		
					$ss1="Select loccode,locationname from locations where CompanyId='".$_SESSION['ACGCompanyId']."' and loccode in ('".$_SESSION['AssignedBranch']."')";
					$site_sql=DB_query($ss1,$db);
					while($site_row=DB_fetch_array($site_sql))
					{
						$p='';
						if($site_row['loccode'] == $_POST['site'])	
						$p="selected";
			?>
	                <option value="<?php echo $site_row['loccode']; ?>" <?php echo $p;?>><?php echo $site_row['locationname']; ?></option>
	                <?php } ?>
	              </select></td>
	  	<td width="13%" align="right">Vendor</td>
	  	<td><select name="vendor" id="vendor">
	        <option value="0">Select</option>
	        <?php 
	        $cn='';
	        	$cn="and site in('".$_SESSION['AssignedBranch']."')" ;

			  $vendor="Select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and assigned_items is not null and assigned_items!='' ".$cn;
			
			$vendor_sql=DB_query($vendor,$db);
			
			while($vendor_res=DB_fetch_array($vendor_sql))
			{
				$p1='';
				if($vendor_res['id'] == $_POST['vendor'])	
				$p1="selected";
			?>
	        <option value="<?php echo $vendor_res['id']; ?>" <?php echo $p1;?>><?php echo $vendor_res['vendor_name']; ?></option>
	        <?php } ?>
	      	</select></td>
	</tr>
    <tr class="row1">
    	<td colspan="4" style="text-align:center;"><input type="submit" value="search" name="search" class="search" /></td>
    </tr>
</table>
</form>

<?php 
if($_REQUEST['search']=='search')
{
	$cond='';
	if($_REQUEST['site']!='0')
		$cond.="and site='".$_REQUEST['site']."'" ;
	if($_REQUEST['vendor']!='0')
		$cond.="and id='".$_REQUEST['vendor']."'" ;
	
	$vendor12="Select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and assigned_items is not null  ".$cond; 
}
else
{
	$vendor12="Select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."' and assigned_items is not null and assigned_items!='' and   site in('".$_SESSION['AssignedBranch']."')"; 
}
$_SESSION['Vend_item_sql']=$vendor12;
//echo $vendor12;
$vendor1=DB_query($vendor12,$db);
?>
<p align="right"><a target="_blank" href="vendor_item_mapped_print.php" class="search">Print</a></p>
<table width="100%" border="0" cellpadding="3" cellspacing="0" class="thborder">
	<tr>
		<th width="12%">Item Code</th>
		<th>Item Name</th>
		<th width="10%">Unit</th>
	</tr>
<?php 
if(DB_num_rows($vendor1)>0){ 
while($vendor_res1=DB_fetch_assoc($vendor1)){  
	echo '<tr><td colspan="3" style=" background-color: #e4e4e4; color: #00463f;padding:2px;font-weight: bold;">'.$vendor_res1['vendor_name'].'</td></tr>';
	$item=explode(',',$vendor_res1['assigned_items']);
	//print_r($item);
	foreach($item as $val){
		$p1='';
	$item_details=DB_fetch_assoc(DB_query("select * from item_master where id='".$val."'",$db));
	$unit=DB_fetch_assoc(DB_query("select * from unitsofmeasure where unitid='".$item_details['mst_unitid']."'",$db));
	$p1.='<tr><td>'.$item_details['StockID'].'</td>';
	$p1.='<td>'.$item_details['item_name'].'</td>';
	$p1.='<td>'.$unit['unitname'].'</td>';
	$p1.='</tr>';
	echo $p1;
	}
 }
}
else
{
	echo '<tr><td colspan="3">No Record Found</td></tr>';
}
?>
</table>
</div>
</div>
<script>
$(document).ready(function(){
	$('#site').change(function(){
		$.ajax({url:'ajax_vendor_by_site.php',data:'site='+this.value,type:"post",success:function(response){
			$('#vendor').html(response);
		}});
	});
})
</script>