<?php 
if($_REQUEST['action']=='delete')
{
	$condition="parantId='".$_REQUEST['id']."'";
	$s->DeleteRecord('purchorderdetails',  $condition);
	$condition="id='".$_REQUEST['id']."'";
	$s->DeleteRecord('purchorders',  $condition);
	$update=DB_query("update comprative set status_active=0 , po_del_status=1
	 where id='".$_REQUEST['comid']."'",$db);	
}

$array=array('IO'=>'Import Order','DO'=>'Domestic Order','SO'=>'Service Order','AO'=>'Asset Order');
//echo "SELECT * FROM purchorders where ordertype='D' and po_date='".date('Y-m-d')."'" ;

if(isset($_POST['Go'])) 
{ 
	$fromdate=$_REQUEST['fdate']; 
	$todate=$_REQUEST['todate']; 
	$fromdate_brk=explode('/',$fromdate); 
	$from=$fromdate_brk[2]."-".$fromdate_brk[1]."-".$fromdate_brk[0]; 
	$todate_brk=explode('/',$todate); 
	$to=$todate_brk[2]."-".$todate_brk[1]."-".$todate_brk[0]; 
	$sql1="SELECT * FROM purchorders  where ordertype='D'"; 
	$filter=array();
	if($_REQUEST['Site']!="") 
	{ 
		//$sql1 .=" AND site='".$_REQUEST['Site']."'";	 
		//$filter[]=
		$filter[]="site='".$_REQUEST['Site']."'";		 	 
	} 
	if($_REQUEST['fdate']!="" && $_REQUEST['todate']!="") 
	{ 
			//$sql1 .=" AND created_date BETWEEN '".$from."' and '".$to."'";	 
			$filter[]="po_date between '".FormatDateFORSQL($_REQUEST['fdate'])."' and '".FormatDateFORSQL($_REQUEST['todate'])."'";
	} 
	if($filter!='')
	{
				$filter1=implode(" and ",$filter);
				$sql1="SELECT * FROM purchorders  where ordertype='D' and $filter1 ";
	}
	
    $SQLPurch=DB_query($sql1,$db); 
}
else
{
	$query = "SELECT * FROM purchorders where ordertype='D' and po_date='".date('Y-m-d')."' and site in('".$_SESSION['AssignedBranch']."')";
	 
	$SQLPurch=DB_query( $query ,$db); 
}
?>

<link rel="stylesheet" href="css/new_mxerp.css">
<div class="body_contain">
<div class="tab_div">
<h1>View Purchase Orders</h1>
<div class="centre" id="slidingDiv">
<form method="post" action="">	
	<table width="100%" cellpadding="3" cellspacing="0" border="0">
		<tr class="row1"> 
			<td width="13%" align="right">Branch</td> 
			<td width="20%"><select name="Site" id="Site"> 
					<option value="0">--Select--</option> 
					<?php 
					$SiteName=DB_query("Select * from locations where mst_companyid='".$_SESSION['ACGCompanyId']."' and loccode in('".$_SESSION['AssignedBranch']."')",$db); 
					while($SiteRow=DB_fetch_array($SiteName)) 
					{ ?> 
					<option value="<?php echo $SiteRow['loccode']?>"<?php echo($_REQUEST['Site']==$SiteRow['loccode']?'selected':'');?>><?php echo $SiteRow['locationname']?></option>	 
						 
				<?php } ?> 
				</select> 
			</td> 	 
			<td width="13%" align="right">From Date</td> 
			<td width="20%"><input type="text" name="fdate" class="datepicker" id="fromdate" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" onchange="javascript:checkdate(this.value)" /></td> 
			<td width="13%" align="right">To Date</td> 
			<td><input type="text" name="todate" class="datepicker" id="todate" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" onchange="javascript:checkdate(this.value)"  />
				<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrStart']);?>" id='from'>
			<input type="hidden" value="<?php echo ConvertSQLDate($_SESSION['financialYrEnd']);?>" id='to'>
			</td> 
		</tr> 
		<tr class="row1"> 
		  <td colspan="6" style="text-align:center;"><input name="Go" type="submit" value="Search" onclick="return frmvalidate()" class="search" /></td> 
		</tr> 
	</table>
</form>
</div>

<p align="left">
		<a href="index.php?route=POModule/create_direct_purch_order<?php echo $rurl; ?>"><input type="button" value="Direct Purchase Order" class="search"></a>
	</p>

<table border="0" cellpadding="3" cellspacing="0" width="100%" class="thborder">
<tr>
	<th width="10%" rowspan="2">Branch</th>
	<th width="9%" rowspan="2">PO No.</th>
	<th width="6%" rowspan="2">PO Date</th>
	<th rowspan="2">Vendor Name</th>	
	<th width="6%" rowspan="2">Vendor Type</th>
	<th width="6%" rowspan="2">MRN Status</th>
	<th width="6%" rowspan="2">PO Status</th>
	<th width="6%" rowspan="2">Valid Till</th>
	<th width="6%" rowspan="2">PO Type</th>
	<th colspan="5" style="text-align:center;">Action</th>
</tr>
<tr>
	<th style="text-align:center;" width="3%">View</th>
	<th style="text-align:center;" width="3%">Edit</th>
	<th style="text-align:center;" width="4%">Cancel</th>
	<th style="text-align:center;" width="3%">Delete</th>
	<th style="text-align:center;" width="3%">Print</th>
</tr>
<?php $indentArr=array();
if(db_num_rows($SQLPurch)>0)
{
while($PurchDetails=DB_fetch_array($SQLPurch)){ 
$ComparativeNo=$s->GetFieldValue('comprative',"id='".$PurchDetails['comperativeId']."'",'comparative_no');
$indentNo=$s->GetFieldValue('materialrequisitions',"requisitionno='".$PurchDetails['indentPRno']."'",'Reqno');
$indentArr=explode(',',$PurchDetails['indentPRno']);
$PO_status=$PurchDetails['final_app_status'];
if($PO_status=='1'){
    $status="Approved";
}else if($PO_status=='0'){
     $status="Pending";
}

  $cancelpo=$PurchDetails['cancel_status'];

 
?>
<tr>
	<td><?php echo $s->GetFieldValue('locations',"loccode='".$PurchDetails['site']."'",'locationname'); ?></td>
	<td><?php echo $PurchDetails['purchase_ordNo']; ?></td>
	<td><?php echo ConvertSQLDate($PurchDetails['po_date']); ?></td>
	<td><?php echo $s->GetFieldValue('suppliers',"id='".$PurchDetails['vendorId']."'",'vendor_name'); ?></td>	
	<td><?php  if($PurchDetails['purchase_ord_Type']=='DO') {echo 'Domestic';} else {echo 'Import';} ?></td>
    <td><?php  echo $PurchDetails['Po_recive_status'];?></td>
	<td><?php  echo $status;  ?></td>
	<td><?php echo ConvertSQLDate($PurchDetails['valid_till']); ?></td>
	<td><?php  if($PurchDetails['ordertype']=='D') echo 'Direct' ; ?></td>
	<td style="text-align:center;"><?php if($_SESSION['frmView']==1) { ?><a href="index.php?route=POModule/view_purchase_order_edit<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&action=edit&modify=viewonly"><img src="images/search.gif" height="15" width="15" title="View" /></a><?php } else { echo "No Permission"; } ?></td>	
	<td style="text-align:center;"><?php if($status=='Pending' && $indentNo=='' && $cancelpo!=1 && $_SESSION['frmEdit']==1) { ?>	
	<a href="index.php?route=POModule/view_purchase_order_edit&<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&action=edit"><img src="images/icon-edit.png" height="15" width="15" title="Edit" /></a><?php }
	else if($status=='Pending' && $indentNo!='' && $cancelpo!=1 && $_SESSION['frmEdit']==1) { ?>
	<a href="index.php?route=POModule/view_purchase_order_edit1&<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&action=edit"><img src="images/icon-edit.png" height="15" width="15" title="Edit" /></a>	
	 <?php } else {?><img src="images/icon-edit.png" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php } ?></td>			
	<td style="text-align:center;"><?php if($cancelpo==1) { echo "Cancelled";	 ?>
	<input type="button" name="reason"  value="Reason" class="search" onclick="javascript:popup('poreason.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $PurchDetails['id'];?>',200,220)">
	<? } else {?>
	<a onclick="popup('pocancel_direct.php?modid=<?php echo $_REQUEST['modid'];?>&frmid=<?php echo $_REQUEST['frmid'];?>&id=<?php echo $PurchDetails['id'];?>',600,200)" href="#" style="text-decoration:none;" ><img src="images/icon_cancel.png" height="15" width="15" title="Cancel PO" /></a><?php } ?></td>
	<td style="text-align:center;"><?php if($cancelpo!=1 && $status=='Pending' && $_SESSION['frmDelete']=='1' ) {  ?>
	<a href="index.php?route=POModule/view_purchase_order_direct&<?php echo $rurl; ?>&id=<?php echo $PurchDetails['id'] ?>&comid=<?php echo $PurchDetails['comperativeId']; ?>&action=delete" onclick="return confirm('Are you sure you want to delete?')"><img src="images/Icon_Delete.png" height="15" width="15" title="Delete" /></a>
	<? } else if($cancelpo==1) { echo "Cancelled"; } else { ?><img src="images/Icon_Delete.png" height="15" width="15" title="No Permission" style="opacity:0.5;" /><?php }?>
	</td>	
	<td style="text-align:center;"><a target="_blank" href="purchase_print.php?id=<?php echo $PurchDetails['id'] ?>"><img src="images/icon_print.jpg" height="20" width="20" title="Print" /></a></td>
</tr>
<?php } 
}
else
{?>
	<tr><td colspan="16" align="center">No Record Found</td></tr>
<?php }
?>
</table>

<div id="PickedItems" style="display:none;">
	<p align="left"><input type="button" value="Add Row" class="search" onclick="javascript:addrow();"> <input type="button" onclick="deleterow()" value="Delete" class="search"></p>
	<table cellspacing="0" cellpadding="3" border="0" width="100%" class="thborder">
		<tr>
			<th width="5%">Select</th>
			<th width="18%">Check Number</th>
			<th width="13%">Check Date</th>
			<th>Bank Name</th>
			<th width="18%">Amount</th>
		</tr>
		<tr>
			<td><input type="checkbox" id="select" name="select" ></td>
			<td><input type="text" id='checkno' name="checkno" placeholder="Check Number"><input type="hidden" name="poid" value="" id='poid'></td>
			<td><input type="text" name="date" id='date' class="datepicker" placeholder="Check Date"   ></td>
			<td><input type="text" name="bank_name" id='bank_name'  placeholder="Bank Name" ></td>
			<td><input type="text" name="amount" id='amount'  placeholder="Amount" ></td>
		</tr>
	</table>
</div>

</div>
</div>
<script type="text/javascript">
function deleterow(){
	$('table tr').has('input[name="select"]:checked').remove();
}


	function addrow(){
		var thisRow = $("#poid").closest( 'tr' )[0];
      	$( thisRow ).clone().insertAfter( thisRow ).find( 'input:text' );
      	   $('.datepicker').removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy',changeMonth: true, changeYear: true,yearRange: '-80:+20'});
      
	}
	function submitPo(){
		var bankarray = [];
		var i=1;
		jQuery('input[id^=select]').each(function(){
   			var checkno   =   $(this).closest('tr').find('input[id=checkno]').val();
   			var date      =   $(this).closest('tr').find('input[id=date]').val();
   			var bank_name =   $(this).closest('tr').find('input[id=bank_name]').val();
   			var amount 	  =   $(this).closest('tr').find('input[id=amount]').val();
   			var poid 	  =   $(this).closest('tr').find('input[id=poid]').val();

   			
   			//bankarray.push(checkno,date,bank_name,amount);
   			bankarray[i] = [checkno,date,bank_name,amount,poid];
   			//bankarray.push(date);
   			//bankarray.push(bank_name);
   			//bankarray.push(amount);
   			i++;
    	});
    	 
    	$.post("ajax_vendor_by_site.php",{"insertdata":bankarray},function(data){
			 
				alert("Details has been updated");
			 
		});

		//alert(bankarray);
		return;
		var checkno = document.getElementById('checkno').value;
		var checkdate = document.getElementById('date').value;
		var poid = document.getElementById('poid').value;
		var bank_name = document.getElementById('bank_name').value;

		if(checkno==''){
			alert("Please enter check no");
			return false;
		}
		if(checkdate==''){
			alert("Please enter check date");
			return false;
		}
		if(bank_name==''){
			alert("Please enter Bank Name");
			return false;
		}
		$.post("ajax_vendor_by_site.php",{'checkno':checkno,"checkdate":checkdate,"poid":poid,"bank_name":bank_name},function(data){
			if(data.trim()=='1'){
				alert("Check Book Details has been updated successfully");
				 $(this).dialog("close");
				 window.location.reload();
			}
		})	;
	}
	function loadCheckBook(po_id){
		 
	 if(po_id=='')
  	 {
    	alert("Purchase Order Id not found");
  	 }
  	 else
  	 {
  	 	document.getElementById('poid').value = po_id;
  	 	$.post("ajax_vendor_by_site.php",{'checkdetails':'yes',"po_id":po_id},function(data){
			 	if(data!=''){
			 		var data = data.split("__")	;
			 		document.getElementById("checkno").value =  data[1];
			 		document.getElementById("date").value =  data[2];
			 		document.getElementById("bank_name").value = data[0];
			 		document.getElementById("amount").value = data[3];
			 		document.getElementById("poid").value = po_id;

			 		

			 	}
			 	
		})	;


  	 	$("#PickedItems").dialog({
			  title:'Check Book List',
			  modal: true,
			  resizable: false,
			  width:800,
			  height:300,
			  closeText: 'fechar',
			  draggable: true,
			  show: 'fade',
			  hide: 'fade',
			  dialogClass: 'main-dialog-class',
			  buttons: {
			    Exit: function()
			    {
			      $(this).dialog("close");
			    }
			  }
			});
		}	 
		  
		$('.ui-dialog-buttonset').append('<button type="button" onclick="submitPo()"  class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only search" role="button" aria-disabled="false"><span class="ui-button-text">Add</span></button>');

	  }
function checkdate(date)
{
	var from =document.getElementById('from').value;
	var to =document.getElementById('to').value;
	
	var cdate=date.split("/");
	var sdate=from.split("/");
	var edate=to.split("/");

	cdate[1]=cdate[1]-1;
	sdate[1]=sdate[1]-1;
	edate[1]=edate[1]-1;
		 
	var dateC = new Date(cdate[2], cdate[1], cdate[0]); //Year, Month, Date 
	var dateS = new Date(sdate[2], sdate[1], sdate[0]); //Year, Month, Date
    var dateE = new Date(edate[2], edate[1], edate[0]); //Year, Month, Date

    if (dateS > dateE) {
        alert("Start Date is not greather then Date End.");
        document.getElementById('fromdate').value=from;
        document.getElementById('todate').value=to;
        
    }

    if (dateC > dateE) {
        alert("Date Not greather than "+to);
       document.getElementById('fromdate').value=from;
        document.getElementById('todate').value=to;
       
    }

    if (dateC < dateS) {
        alert(" Date Not less than "+from);
        document.getElementById('fromdate').value=from;
        document.getElementById('todate').value=to;
       
    }	
	
	
}
</script>