<?php
$PageSecurity = 5;
$title = _('Vendor Master');
include('includes/SQL_CommonFunctions.inc');
$_SESSION['modid']=$_REQUEST['modid'];
$_SESSION['frmid']=$_REQUEST['frmid'];
$frmpermission= $px->GetFileAccess($_SESSION['modid'],$_SESSION['frmid'],$_SESSION['ACGRoleId']);
$_SESSION['frmAdd']=$frmpermission[0];
$_SESSION['frmEdit']=$frmpermission[1];
$_SESSION['frmDelete']=$frmpermission[2];
$_SESSION['frmView']=$frmpermission[3];
if($_REQUEST['action']=='delete' && $_REQUEST['ID']!='')
{
	$delete1=DB_query("Delete from suppliers where id='".$_REQUEST['ID']."'",$db);
	$delete2=DB_query("Delete from contact_information_master where master_code='".$_REQUEST['ID']."' and master_prefix='S'",$db);
	echo "<script>location.href='index.php?route=Supplier_list&modid=".$_REQUEST['modid']."&frmid=".$_REQUEST['frmid']."'</script>";
}
?>
<h2 align="center"><strong>Vendor</strong></h2>
<form action="index.php?route=Supplier_list&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" method="post">
<table>	
	<tr><td><strong>Site</strong>
	<?php
		 $result=DB_query("SELECT locationname, loccode FROM locations  where CompanyId='".$_SESSION['ACGCompanyId']."'
    and loccode in('".$_SESSION['AssignedBranch']."')", $db);
		  ?>
	   <td><select name="site" style="width:100%">
	   <option value="">select</option>
	   <?php
		while($myrow = DB_fetch_array($result))
		 {
	 	$p='';
		if($_POST['site']==$myrow['loccode'])
		$p="selected";
		 ?>
		<option VALUE="<?php echo $myrow['loccode'];?>"<?php echo $p;?>><?php echo $myrow['locationname'];?></option>
		 <?php }?> 
		</option></select></td></tr>
	<tr><td><strong>Vendor Code</strong></td><td><input type="text"  name="vendor_code" style="width:100%"/></td></tr>
	<tr><td><strong>Vendor Name</strong></td><td><input type="text"  name="vendor_name" style="width:100%"/></td></tr>
	<tr><td><strong>Approved Item</strong></td><td><Select name="item" id="item">
		<option value="">--Select--</option>
		<?php $itemdata=DB_query("select * from item_master",$db);
		while($itemdata1=DB_fetch_array($itemdata))
		{
			?>
		<option value="<?php echo $itemdata1['id']; ?>"><?php echo $itemdata1['item_name']; ?></option>
		<?php } ?>
		
	</Select></td></tr>
	<!--<tr><td><strong>Category</strong></td><td><select name="category">
	<option value="0">Select</option>
	<option value="SiteVendor">SiteVendor</option>
	<option value="HeadOfficeVendor">HeadOfficeVendor</option>
	<option value="Expenses">Expenses</option>
	</select></td></tr>-->
	<tr><td><strong>Country</strong></td><td>
		<Select name="country" id="country">
		<option value="">--Select--</option>
		<?php $countrydata=DB_query("select * from country_master",$db);
		while($countrydata1=DB_fetch_array($countrydata))
		{
			?>
		<option value="<?php echo $countrydata1['id']; ?>"><?php echo $countrydata1['country']; ?></option>
		<?php } ?>
		
	</Select>
		
		
	</td></tr>
	
   </table>
   <p align="center"><input type="submit" name="submit" value="Search" /></p>
   </form>
   <?php if($_SESSION['frmAdd']=='1') {?>
	<div align="center"><a href="index.php?route=Suppliers&NewIssue=yes&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">ADD VENDOR</a></div>
<?php }?>
<br />
<br />
<p align="right"><a href="vender_data.php" target="_blank">Print</a></p>
<table width="100%" cellpadding="2" cellspacing="2">
<tr>
    <th><strong> Vendor Code </strong></th>
    <th><strong>Vendor Name</strong></th>
    <th><strong>Category</strong></th>
    <th><strong>Vendor Type</strong></th>
    <th><strong>Site</strong></th>
	<th><strong>View</strong></th>
	<th><strong>Edit</strong></th>
	<th><strong>Delete</strong></th>
  </tr>
 <?php
 //---------------------------Search Starts here-------------------------------
	if(isset($_REQUEST['submit']))
	{
		$sql_search='';
		$vendor_code1=$_REQUEST['vendor_code'];
		$vendor_name1=$_REQUEST['vendor_name'];
		$category1=$_REQUEST['category'];
		$site1=$_REQUEST['site'];
		$item=$_REQUEST['item'];
		$country=$_REQUEST['country'];
		if(empty($vendor_code1) && empty($vendor_name1) && empty($category1) && empty($site1) )
		{
		$sql_search = "select vendor_code, vendor_name, category, account_group, vendor_type, site, short_name, state, city from suppliers where 1 ";
		}
		else 
		{
		$sql_search = "select vendor_code, vendor_name, category, account_group, vendor_type, site, short_name, state, city from suppliers where 0 ";
		}
		if(!empty($vendor_code1))
		{
		$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' OR vendor_code LIKE '".$vendor_code1."%'";
		} 
		if(!empty($vendor_name1))
		{
		$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' OR vendor_name LIKE '".$vendor_name1."%'";
		}
		if(!empty($category1))
		{
			$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' OR category LIKE '".$category1."%'";
		}
		if(!empty($site1))
		{
			$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' OR site LIKE '".$site1."%'";
		}
		if(!empty($item))
		{
		$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' AND  assigned_items in ('".$item."')";	
		}
		
		if(!empty($country))
		{
		$sql_search .=" and CompanyId='".$_SESSION['ACGCompanyId']."' AND  country ='".$country."'";	
			
		}
		//echo $sql_search;
			$search_result = DB_query($sql_search, $db);
	}
$i=0;
if(isset($_REQUEST['submit']))
{
	//echo "inside IF IF ";
	if (DB_num_rows($search_result)>0)
	{
	 	while($my_search_row = DB_fetch_array($search_result))
	 	{?>
	 	<tr bgcolor="#FFFFFF">
				<td><?php echo $my_search_row['vendor_code'];?></td>
	  			<td><?php echo $my_search_row['vendor_name'];?></td>
				<td><?php echo $s->getField('category_master','id',$my_search_row['category'],'name');?></td>
				<td><?php echo $my_search_row['vendor_type']; ?></td>
				<td><?php echo $s->getField('locations','loccode',$my_search_row['site'],'locationname'); ?></td>
			  <td><?php if($_SESSION['frmView']=='1'){?><div align="center"><a href="index.php?route=view_vendor&action=view&vendorcode=<?php echo $my_search_row['vendor_code'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">View</a></div><?php }else{echo "No Permission";} ?></td>
			  <td><?php if($_SESSION['frmEdit']=='1'){?><div align="center"><a href="index.php?route=Suppliers&ID=<?php echo $myrow['id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">Edit</a></div><?php }else{echo "No Permission";} ?></td>
			  <td><?php if($_SESSION['frmDelete']=='1'){?><div align="center"><a href="index.php?route=Supplier_list&action=delete&ID=<?php echo $myrow['id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" onClick="return confirm('Are you sure you wish to delete this supplier?')">Delete</a></div><?php }else{echo "No Permission";} ?></td>
		</tr>	 		
	 	<?php
	 	 }	
	}
}
//----------------end of Search---------------- 
else 
{
	//echo "Select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."'";
	$sql=DB_query("Select * from suppliers where CompanyId='".$_SESSION['ACGCompanyId']."'",$db); 	
	if(DB_num_rows($sql)>0)
	{
		while($myrow=DB_fetch_array($sql))
	  	{ ?>
			<tr bgcolor="#FFFFFF">
				<td><?php echo $myrow['vendor_code'];?></td>
	  			<td><?php echo $myrow['vendor_name'];?></td>
				<td><?php echo $s->getField('category_master','id',$myrow['category'],'name');?></td>
				<td><?php echo $myrow['vendor_type']; ?></td>
				<td><?php echo $s->getField('locations','loccode',$myrow['site'],'locationname'); ?></td>
				<td><?php if($_SESSION['frmView']=='1'){?><div align="center"><a href="index.php?route=view_vendor&action=view&vendorcode=<?php echo $myrow['vendor_code'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">View</a></div><?php }else{echo "No Permission";} ?></td>
			  <td><?php if($_SESSION['frmEdit']=='1'){?><div align="center"><a href="index.php?route=Suppliers&ID=<?php echo $myrow['id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>">Edit</a></div><?php }else{echo "No Permission";} ?></td>
			  <td><?php if($_SESSION['frmDelete']=='1'){?><div align="center"><a href="index.php?route=Supplier_list&action=delete&ID=<?php echo $myrow['id'];?>&modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>" onClick="return confirm('Are you sure you wish to delete this supplier?')">Delete</a></div><?php }else{echo "No Permission";} ?></td>
		</tr>
				
	<?php    
		$i++;
	  }
  	}
 }//else
//end of page full new headings if

//end of while loop
?>
			
</table>

<?php
include('includes/footer.inc');
?>