<?
//ob_start();
session_start();

function startsWith($haystack, $needle) {
    // search backwards starting from haystack length characters from the end
    return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

if(isset($_SESSION['ses_name']))
{
$user=$_SESSION['ses_name'];
}else{
header("location:index.php");
}


$veh_num='';
$sdate_12 ='';
$page='1';
include("include/config.php");
require_once "include/Database_functions.php";
$sqlObj=new sqlclass();
$msg = '';
$client_namek ='';

if(isset($_REQUEST['del']) && $_REQUEST['del']=='delete')
{
	$id = $_REQUEST['pur_del'];
	
	$respurchase = mysql_query("select * from cf_oilpurchase where pur_id='$id'");
    $resven = mysql_fetch_array($respurchase);
    $vendor_id = $resven['pur_vendor_id'];
	$grandtotal = $resven['pur_total'];
	$commodityval = $resven['pur_com_kg'];
	$productid = $resven['pur_prd_id'];
	
	
	$reselleres = mysql_query("select * from cf_ledger where ledgertype = 'vendorledger' and empid = '$vendor_id'");
    $resellrw = mysql_fetch_array($reselleres);
    $ledgerids = $resellrw['ledgerid'];
	
	mysql_query("update cf_acountspayable set `pendingamount`=`pendingamount`-$grandtotal where vendorid= '$vendor_id'");
	
	 $sq_stock=mysql_query("select * from cf_stock");
		  
		  if(mysql_num_rows($sq_stock) > 0)
		   {
		   		$rw = mysql_fetch_array($sq_stock);
				
				 if($productid == 1)
		   		 {
		   			$stockpalmoil = $rw['stock_palmoil'] - $commodityval;
		   			$sql4 = mysql_query("update cf_stock set stock_palmoil = '$stockpalmoil'");	
		   		 }
				 else if($productid == 2)
				 {
				 	$stockcoconutoil = $rw['stock_coconutoil'] - $commodityval;
		   			$sql4 = mysql_query("update cf_stock set stock_coconutoil = '$stockcoconutoil'");	
				 }
				
				
		   }
	
	
	$sql=mysql_query("delete from cf_purchase_account where purchaseid='$id' and ven_client_id = '$ledgerids' and purch_id = '$id'");
	$sql=mysql_query("delete from cf_purchase_account where purchaseid='$id' and ven_client_id = '1000' and purch_id = '$id'");
	$sql=mysql_query("delete from cf_purchase where purchaseid='$id'");
	$sql=mysql_query("delete from cf_oilpurchase where pur_id='$id'");
	header("location:purchase_list.php?msg=You Have Successfully Deleted");
}

function print1($selectype,$vendortype,$product_id,$fromdate,$todate)
			{
				$dates = date('Y-m-d');
				$names = $selectype.$dates;
				$outstr = NULL;
				//header("Content-type: application/vnd.ms-word");
				//header("Content-Disposition: attachment;Filename=".$names.".xls");
			
					//set the directory to get image			
					// Query database to get data
				
				//print_r($_REQUEST);
				$cond='';
				
				$outstr.='<div id="header" >
					<table  border="0" cellspacing="0" cellpadding="0" class="tableList" style="margin-left:20px;width:1200px; ">
  <tr class="heading">
    <td>No</td>  
	<td width="50">SrNo</td>  
    <td>Vendor</td>
	<td>Product</td>
    <td>Pur Type</td>
    <!--<td>FFA Req</td>
	<td>FFA Got</td>-->
	<td>VehicleNumber</td>
	<td>Driver</td>
    <td>Comdity Kg</td>
    <td>Tin Rate</td>
    <td>Comdity Rate</td>
    <td>Tanker Tonne</td>
    <td>Tanker Freight</td>
    <td>Tax%</td>
    <td>Tax Rs</td>
    <td>Grand Total</td>
    <td width="80">Date</td>	
  </tr>';
  
 

$master = '';
$notification = '';
$trip = '';
$accounting = '';
$accpay = '';
$report = '';

$i=1;
$vendortype ='';
$fromdate='';
$todate='';
$prdtype='';
if(isset($_REQUEST['vendortype'])){
$vendortype=$_REQUEST['vendortype'];
}
if(isset($_REQUEST['prdtype'])){
$prdtype=$_REQUEST['prdtype'];
}
if(isset($_REQUEST['fromdate'])){
$fromdate=$_REQUEST['fromdate'];
$fromdate1 = date('Y-m-d', strtotime($fromdate));
}

if(isset($_REQUEST['todate'])){
$todate=$_REQUEST['todate'];
$todate1 = date('Y-m-d', strtotime($todate));
}
$i=1;
$sq_arr="select * from cf_oilpurchase where 1 ";
if($vendortype!=''){
$sq_arr.=" and pur_vendor_id = '".$vendortype."' ";
}
if($prdtype!=''){
$sq_arr.=" and pur_prd_id = '".$prdtype."' ";
}
if($fromdate!="" && $todate!=""){
$sq_arr.=" and pur_addate between '$fromdate1' and '$todate1'";
}


/*if($sdate_12!=''){
$sq_arr.=" and date='".$sdate."' ";
}*/
$sq_arr.=" order by pur_id desc ";

//$sq_arr.=" order by pur_id desc ";


//calcutate total
$topGrandTot = 0;
$jarSaleLists =mysql_query($sq_arr);
if($jarSaleLists && mysql_num_rows($jarSaleLists)>0){
while($val =mysql_fetch_array($jarSaleLists))
{
	$topGrandTot += $val['pur_total'];
}
}

$outstr.='<div style="margin-left:22px;margin-top:22px;"><strong >'.('Grand Total : '.$topGrandTot).'</strong></div>';
//////


$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("pagingserch.php");
$sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=1;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;$grandTot = 0;
while($res=mysql_fetch_array($array))
{
$pagecnt++;

	$outstr.='<tr>
    <td>'.$pagecnt.'</td>
	<td>PR-'.$res['pur_id'].'</td>';
    
		if($res['pur_type'] == 1)
		{
			$purtype = 'Kg';
		}
		else if($res['pur_type'] == 2)
		{
			$purtype = 'tin';
		}
		else
		{
			$purtype = 'Jar';
		}
	
		$vendorid = $res['pur_vendor_id'];	
        $sq_ven=mysql_query("select * from cf_reseller where reseller_id = '$vendorid'");
    	$resven=mysql_fetch_array($sq_ven);
   	
     $outstr.='<td>'.$resven['res_name'].'</td>';
    
		$prdid = $res['pur_prd_id'];	
        $sq_prd=mysql_query("select * from cf_open_oil where open_stock_id = '$prdid'");
    	$resprd=mysql_fetch_array($sq_prd);
    
	 $outstr.='<td>'.$resprd['item_name'].'</td>
     <td>'.$purtype.'</td>';
     
	 
		$vehid = $res['vehicleid'];	
        $sq_veh=mysql_query("select * from cf_vehicle where id = '$vehid'");
    	$resveh=mysql_fetch_array($sq_veh);
    	
	 $outstr.='<td>'.$resveh['vehiclenum'].'</td>
	 <td>'.$res['driver_names'].'</td>
     <td>'.$res['pur_com_kg'].'</td>
     <td>'.$res['pur_tin_rate'].'</td>
     <td>'.$res['pur_com_total'].'</td>
     
     <td>'.$res['pur_tank_tonne'].'</td>
     <td>'.$res['pur_tank_total'].'</td>
    
     <td>'.$res['pur_tax_per'].'</td>
     <td>'.$res['pur_tax_rate'].'</td>
    <td>'.$res['pur_total'].'</td>
	 <td>'.date(('d-m-Y'),strtotime($res['pur_addate'])).'</td>';
    $grandTot+= $res['pur_total'];
  $outstr.='</tr>';
  
    }}else{
	 $outstr.='<tr>
    <td class="errorMessage"  style="border-right:0px;border-bottom:0px; "><font color="#FF0000">No Records Found</font></td></tr>';
	 }
 
   $outstr.='<tr>
     <td colspan="2"><strong>Total</strong></td><td colspan="12"></td>
     <td><strong>'.$grandTot.'</strong></td><td colspan="4"></td>
 </tr>
</table>
				

					</div>';
					
		if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

             $ext = end(explode(".",$entry));
			if($ext=="xls" && startsWith($entry, "Export") === true)	
				
				unlink($entry);
        }
    }

    closedir($handle);
}

				$fname = "Export".time().".xls";
				  $file = fopen($fname,"w+");
				  fwrite($file,$outstr);
				  fclose($file);
				  header('Location:'.$fname);			
					
					
				/*$fname = "Export".time().".xls";
				  $file = fopen($fname,"w+");
				  fwrite($file,$outstr);
				  fclose($file);
				  header('Content-Type: application/vnd.ms-excel');
				  header('Content-Disposition: attachment; filename="'.basename($fname).'"');
				  readfile($fname);
				  unlink($fname);*/ 	
					 
				//echo $outstr;
				exit;
			
				
			}
			
			
				
			if(isset($_POST['download']))
			{
				$selectype = "Purchase List";
				$vendortype = $_POST['vendortype'];
				$product_id = $_POST['product_id'];
				$fromdate = $_POST['from_date'];
				$todate = $_POST['to_date'];
				
				
				print1($selectype,$vendortype,$product_id,$fromdate,$todate);
			}
?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OIL FILL</title>
<link href="css/styles.css" rel="stylesheet"  />
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<link type="text/css" href="css_date/ui-lightness/jquery-ui-1.7.2.custom.css" rel="stylesheet" />
<script type='text/javascript' src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

<script type='text/javascript' src="js/jquery.autocomplete.js"></script>
<link rel="stylesheet" type="text/css" href="js/jquery.autocomplete.css" />
<script src="js/labview_popup.js" type="text/javascript"></script>

<script type="text/javascript" src="scripts_date/jquery-calendar-1.7.2.js"></script>
<script>

$(document).ready(function(){
$('#from_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});

$(document).ready(function(){
$('#to_date').datepicker({
showOn: 'both',
buttonImage: 'icons_date/calendar.gif',
buttonImageOnly: true,
changeMonth: true,
changeYear: true,
dateFormat: 'dd-mm-yy'
});
});


function getXMLHTTP()
	 { //fuction to return the xml http object
	var xmlhttp=false;	
		try
		{
		xmlhttp=new XMLHttpRequest();
		}
		catch(e)	
		{		
			try{			
				xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e)
			{
				try
				{
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e1)
				{
				xmlhttp=false;
				}
			}
		}
	return xmlhttp;
	}   


function Searchdate(page,val)
{ 
 	
	 var page=page;	
	 var vendortype='';
	 var fromdate='';
	 var todate='';
	 prdtype='';
	 if(document.purchase_frm.vendortype){
	 vendortype= document.purchase_frm.vendortype.value;
	 } 
	 
	 if(document.purchase_frm.from_date){
	 fromdate= document.purchase_frm.from_date.value;
	 }
	 
	 if(document.purchase_frm.to_date){
	 todate= document.purchase_frm.to_date.value;
	 }
	  if(document.purchase_frm.prdtype){
	 prdtype= document.purchase_frm.prdtype.value;
	 }
	 
	var strURL="ajax/purchase_list_data.php?&page="+page;
	
	if(vendortype!=''){
	strURL+="&vendortype="+vendortype;
	}
	
	if(fromdate!=''){
	strURL+="&fromdate="+fromdate;
	}
	
	if(todate!=''){
	strURL+="&todate="+todate;
	}
	if(prdtype!=''){
	strURL+="&prdtype="+prdtype;
	}
	//alert(strURL);
	
	var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
		 
			if ((req.readyState == 0)||(req.readyState == 1)||(req.readyState == 2)||(req.readyState == 3))
      		{
			//document.getElementById('order').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			//document.getElementById('Cakeorder').innerHTML='<div align="center"><img src="'+path+'images/ajax_loader.gif"/></div>';
			}
		 
		 
      if (req.readyState == 4)
      {
	 // only if "OK"
	 	if (req.status == 200)
	 	{ 
	   document.getElementById('list').innerHTML=req.responseText;
	//	document.getElementById('Cakeorder').innerHTML=req.responseText;
	 	}
	  else 
	 	{
   	   alert("There was a problem while using XMLHTTP:\n" + req.statusText);

	 }
     }
     }
   req.open("GET", strURL, true);
   req.send(null);
   }
 
   
}
function showClientcodedropdown(str){
var xmlhttp;    
if (str=="")
  {
  document.getElementById("txtcodeshow").innerHTML="";
  Searchdate(1,str);
  return;
  }
  
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("txtcodeshow").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","clientajax_code_dropdown.php?str="+str,true);
xmlhttp.send();
}
</script>
</head>

<body>
<!------------------popup area (view, edit)--------------->

<div class="viewEmployeePopup login-popup"  id="login-box" style="width:500px;height:200px;">
<a href="#" class="close"><img src="images/close.png" class="btn_close" title="Close Window" alt="Close" /></a>
   <div id="popupid"></div>
   
     
    </div>

<!---------------popup area ends------------------>
<div class="wrapper">
   <div>
<?php 
	include("header.php");
?>
</div>
 
 <div class="tabbedArea" style="position:relative; margin-top:">

  
  <div class="tabBody">
   <h1>Oil Purchase List</h1>
   
   <form name="purchase_frm" id="purchase_frm" method="post"  >
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px; ">
   <div style="padding:20px;">
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">Vendor</p>
     <select name="vendortype" class="input1">
         <option value="">-Select Vendor-</option>
		 <?php 
		 $sq_resell=mysql_query("select * from cf_reseller"); 
		 while($resel=mysql_fetch_array($sq_resell))
			{
		?>
			<option value="<?=$resel['reseller_id']?>" ><?=$resel['res_name']?></option>
        <?php
			} ?>
     </select>
    </div>
	
	 <div class="searchField" style="padding-left:20px;">
     <p class="input1">Product</p>
     <select class="input1" name="product_id" id="product_id" >
     			<option value="">Select Product</option>
     			<?php 
					$sq_product="select * from cf_open_oil";
					$array1=mysql_query($sq_product);
					while($resprd=mysql_fetch_array($array1))
					{
					?>     	
        				<option value="<?=$resprd['open_stock_id']?>" ><?=$resprd['item_name']?></option>
               		<?php
					}
	 				?>
     		</select>
    </div>
     
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">From Date</p>
     <input type="text" name="from_date" id="from_date" style="width:75px;" maxlength="100" class="input1" />
    </div>
    
    <div class="searchField" style="padding-left:20px;">
     <p class="input1">To Date</p>
     <input type="text" name="to_date" id="to_date" style="width:75px;" maxlength="100" class="input1" />
    </div>
       
    <div class="searchField">
     <input name="button" type="button" class="input1" onClick="Searchdate(1);" value="search" />
    </div>
	
	<div class="searchField" style="padding-left:60px;margin-right: 20px;">
    <input name="download" type="submit" class="input1"  value="download" />
    </div>
  
   </div>
   </div>
   </form>
   
   
   <div class="searchBox" style="width:93%; margin-right:0px;  padding:0px;">
  
   
   <div id="list">
   <table  border="0" cellspacing="0" cellpadding="0" class="tableList" style="margin-left:20px;width:1200px; ">
  <tr class="heading">
    <td>No</td>  
	<td width="50">SrNo</td>  
    <td>Vendor</td>
	<td>Product</td>
    <td>Pur Type</td>
    <!--<td>FFA Req</td>
	<td>FFA Got</td>-->
	<td>VehicleNumber</td>
	<td>Driver</td>
    <td>Comdity Kg</td>
    <td>Tin Rate</td>
    <td>Comdity Rate</td>
    <td>Tanker Tonne</td>
    <td>Tanker Freight</td>
    <td>Tax%</td>
    <td>Tax Rs</td>
    <td>Grand Total</td>
    <td width="80">Date</td>
	<td align="center">View</td>
    <td align="center">Edit</td>
    <td align="center">Del</td>
  </tr>
  
  <?php

$master = '';
$notification = '';
$trip = '';
$accounting = '';
$accpay = '';
$report = '';

$i=1;
$sq_arr="select * from cf_oilpurchase where 1 ";

$sq_arr.=" order by pur_id desc ";


//calcutate total
$topGrandTot = 0;
$jarSaleLists =mysql_query($sq_arr);
if($jarSaleLists && mysql_num_rows($jarSaleLists)>0){
while($val =mysql_fetch_array($jarSaleLists))
{
	$topGrandTot += $val['pur_total'];
}
}
echo '<div style="margin-left:22px;margin-top:22px;"><strong >'.('Grand Total : '.$topGrandTot).'</strong></div>';
//////


$query=$sq_arr;
//echo $query;
$function="Searchdate";
include("pagingserch.php");
$sq_arr.=" LIMIT $start, $limit";

if(isset($_REQUEST['page'])){
$page=$_REQUEST['page'];
}else{
$page=1;
}

$array=mysql_query($sq_arr);
if($array && mysql_num_rows($array)>0){
$pagecnt=$start;$grandTot = 0;
while($res=mysql_fetch_array($array))
{
$pagecnt++;
  ?> 
  <tr>
    <td><?php echo $pagecnt;?></td>
	<td><?php echo "PR-".$res['pur_id'];?></td>
    <?php
		if($res['pur_type'] == 1)
		{
			$purtype = 'Kg';
		}
		else if($res['pur_type'] == 2)
		{
			$purtype = 'tin';
		}
		else
		{
			$purtype = 'Jar';
		}
	 ?>
    
    <?php 
		$vendorid = $res['pur_vendor_id'];	
        $sq_ven=mysql_query("select * from cf_reseller where reseller_id = '$vendorid'");
    	$resven=mysql_fetch_array($sq_ven);
    ?>	
     <td><?php echo $resven['res_name'];?></td>
     <?php 
		$prdid = $res['pur_prd_id'];	
        $sq_prd=mysql_query("select * from cf_open_oil where open_stock_id = '$prdid'");
    	$resprd=mysql_fetch_array($sq_prd);
    ?>	
	 <td><?php echo $resprd['item_name'];?></td>
     <td><?php echo $purtype;?></td>
     <?php /*?><td><?php echo $res['pur_ffa_req'];?></td>
     <td><?php echo $res['pur_ffa_got'];?></td><?php */?>
	  <?php 
		$vehid = $res['vehicleid'];	
        $sq_veh=mysql_query("select * from cf_vehicle where id = '$vehid'");
    	$resveh=mysql_fetch_array($sq_veh);
    ?>	
	 <td><?php echo $resveh['vehiclenum'];?></td>
	 <td><?php echo $res['driver_names'];?></td>
     <td><?php echo $res['pur_com_kg'];?></td>
     <td><?php echo $res['pur_tin_rate'];?></td>
     <td><?php echo $res['pur_com_total'];?></td>
     
     <td><?php echo $res['pur_tank_tonne'];?></td>
     <td><?php echo $res['pur_tank_total'];?></td>
    
     <td><?php echo $res['pur_tax_per'];?></td>
     <td><?php echo $res['pur_tax_rate'];?></td>
    <td><?php echo $res['pur_total']; $grandTot+= $res['pur_total']; ?></td>
	 <td><?php echo date(('d-m-Y'),strtotime($res['pur_addate']));?></td>
    <td align="center" class="tr_td_b"><a href="#login-box" class="login-window"  data-id=<?php echo $res['pur_id'];?>><img src="images/eye-icon-new.png" title="View"></a></td>
   <td align="center" class="tr_td_b"><a href="add-purchase.php?pur_mainid=<?=$res['pur_id']?>"><img src="images/edit.png" title="edit"></a></td>
   
   <td align="center" class="tr_td_r" ><a href="purchase_list.php?pur_del=<?=$res['pur_id']?>&del=delete" onClick="return confirm('Do you really want to delete this field?');"><img src="images/delete.png" title="delete"></a></td>
  </tr>
  <?php 
    }}else{?>
	 <tr>
    <td class="errorMessage"  style="border-right:0px;border-bottom:0px; "><font color="#FF0000">No Records Found</font></td></tr>
	<? }
  ?>
   <tr>
     <td colspan="2"><strong>Total</strong></td><td colspan="12"></td>
     <td><strong><?php echo $grandTot;?></strong></td><td colspan="4"></td>
 </tr>
</table>

  <div align="right" style="padding-top:10px;"><?=$pagination?></div> 
  </div>
  </div>
 </div>
 <div class="footer"><p>Powered by <a href="http://www.aadhocc.co.uk/" target="_blank">Aadhocc</a></p></div>
 
</div>
<div class="quicLInksCover">
 <?php 
	include("leftlink.php");
?>

</div>
</body>
</html>
