<?php
session_start();
$where 					= " WHERE 1=1  ";
$next_page_condition 	= "";
$paging ="";
if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
} 
$restricted_groups = [14, 16];
$can_view = !in_array($_SESSION['admin_group_id'], $restricted_groups);
##################################  STRAT FILTER CONITIONS    ###############################

$return_ref 	= isset($_REQUEST['return_ref']) ? $_REQUEST['return_ref'] : "";
if($return_ref) {
    $where	.= " AND (id ='$return_ref') ";
    $next_page_condition.="&return_ref=$return_ref";
}
$supplier_id 	= isset($_REQUEST['supplier_id']) ? $_REQUEST['supplier_id'] : "";
if($supplier_id) {
	$supplier_id=trim($supplier_id);
    $where	.= " AND supplier_id LIKE '%$supplier_id%' ";
    $next_page_condition.="&supplier_id=$supplier_id";
}
$org_id 	= isset($_REQUEST['org_id']) ? $_REQUEST['org_id'] : "";
if($org_id) {
	$org_id=trim($org_id);
    $where	.= " AND org_id LIKE '%$org_id%' ";
    $next_page_condition.="&org_id=$org_id";
}

/* $from_date = isset($_REQUEST['from_date']) ? utility::setFormatedDate($_REQUEST['from_date']) : "";
$to_date = isset($_REQUEST['to_date']) ? utility::setFormatedDate($_REQUEST['to_date']) : "";
if($from_date && $to_date){
	$where .= " AND site_date BETWEEN '$from_date 00:00:00' and '$to_date 23:59:59' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date']."&to_date=".$_REQUEST['to_date']." ";
}else if($from_date){
	$where.= " AND site_date >= '$from_date' ";
	$next_page_condition.="&from_date=".$_REQUEST['from_date'];
}else if($to_date){
	$where.= " AND site_date <= '$to_date' ";
	$next_page_condition.="&to_date=".$_REQUEST['to_date'];
} */




################################# END FILTER CONDITIONS ################################

###########################################  PAGING WITH PER PAGE  #####################################
$file_name		= "index"; // this is file name which is used during paging  , included at the bottom of the page
$table_name		= "return_notes";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "Returns Notes";
include("paging/paging_query.inc.php");
################################## END PAGING WITH PER PAGE  ################################
$query = "SELECT * FROM $table_name $where ORDER BY id ASC LIMIT $eu, $limit";

$sql=mysqli_query($conn,$query);
$result=mysqli_num_rows($sql);

$total_no_of_return = mysqli_num_rows(mysqli_query($conn,$join_query_for_paging));
#################################   DELETE    #########################################
// when we click on delete this area of php code will execute
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete')
{
	mysqli_query($conn,"DELETE FROM $table_name WHERE id = '".$_REQUEST['id']."' ");
	
	mysqli_query($conn,"DELETE FROM returns_notes_mapping WHERE return_id = '".$_REQUEST['id']."' "); 
	
	$_SESSION['info_message'] = "Return deleted sucessfully";
	header("location: $file_name?page=view".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE    

?>


<div class="content-header">
    <div class="container-fluid">
        <div class="row mb-2">
            <div class="col-sm-12">
                <h1 class="m-0"><?=$page_header;?></h1>
            </div>
        </div>
        <!-- /.row -->
    </div>
    <!-- /.container-fluid -->
</div>
<section class="content">
	<div class="container-fluid">
	<div class="row">
		<div class="col-lg-12">
		<div class="card">
			<!----------------------- Message ------------------------------->
			<? if(isset($_SESSION['info_message'])){ ?>
			  <div class="alert alert-info alert-dismissable">
				<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
				<? echo $_SESSION['info_message']; unset($_SESSION['info_message']);?>
			  </div>
			  <? } ?>
			<!----------------------- End Message ------------------------------->
			<div class="">
				<div class="card-body">
				<!-- search form -->
				<div class="hide_non_print_div">
				<?//php include_once('return_search_options.inc.php');?>
				</div>
				<!-- /.search form -->
				Total No. of Return <?=$total_no_of_return;?>
				
					<form action="index?page=add" method="post" name="form1" id="form1">
					<?php if($can_view){ ?>
					<div class="card-footer">
					   <button type="submit" name="add" class="btn btn-primary hide_non_print_div">Add Return</button>
					    <a href="" onClick="window.print();" class="btn btn-info hide" style="float:right !important;margin-left:4px;margin-bottom:4px;" id="print_button"><i class="fa fa-print"></i> Print</a>
					</div>
					<?php } ?>
					<? if($result>0){?>
					<div class="table-responsive">
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<!--<th>#</th>-->
							<th>Note Number </th>
							<th>Supplier</th>
							<!--<th>PDF</th>-->
							<?php if($can_view){ ?>
							<th class="hide_non_print_div">Edit</th>
							<th class="hide_non_print_div">Delete</th>
							<?php } ?>
						  </tr>
						</thead>
						<tbody>
							<?
								while($row =mysqli_fetch_array($sql)){ 
								$row=utility::validate_crypt_data($row);
								
								
							?>
						  <tr>
							<!--<td nowrap><a href="index?page=view_purchase_order_details&from=view_purchase_order&po_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>">
								<?php echo $row["id"];?>
								</a>
							</td>-->
							<td><?php echo $row["note_number"];?></td>
							<td><?php echo $gsi->getSuppliersById($row["supplier_id"]);?></td>
							
							
							
							
							<!--<td>
							<?php if($_SERVER['HTTP_HOST'] =="localhost"){?>							
								 <a href="<?=$config->frontEndURL;?>/admin/purchase_order/index?page=view_purchase_order_pdf&po_id=<?=$row['id'];?>&hide_top=1&hide_left=1" target="_blank"><i class="fa fa-fw fa-file-pdf"></i></a>
							<?}else{?>
								<a href="<?=$config->frontEndURL;?>/purchase_order_pdf.php?po_id=<?=$row['id'];?>&pdf_name=purchase_order" target="_blank"> <i class="fa fa-fw fa-file-pdf"></i></a>		
								<?}?>
							</td>-->
							<?php if($can_view){ ?>
							<td class="hide_non_print_div"><a href="index?page=add&action=edit&return_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit text-primary"></i> </a></td>
							<td class="hide_non_print_div"><a href="index?page=view&action=delete&id=<?=$row['id'].$next_page_condition.$paging; ?>"onclick=" javascript:return confirm('Are you sure! You want to delete');"><i class="fa fa-fw fa-trash text-danger"></i> </a></td>
							<?php } ?>
						  </tr>
							<? } ?>
							
						</tbody>
					 </table>
					 </div>
					 <? }else{ ?>
						No returns found...
					 <? } ?>
					 <div class="box-footer clearfix">
						<!-- Paging starts here  -->
						<? 
							$next_page_condition .="&page=view";
							include("paging/paging_row.inc.php") 
						?>
						<!-- Paging ends starts here  -->
					</div>
				</div>
				<?php if($can_view){ ?>
				<div class="card-footer">
                   <button type="submit" name="add" class="hide btn btn-primary hide_non_print_div">Add Return</button>
                </div>
				<?php } ?>
				</form>
			</div>
		</div>
		</div>
	</div>
	</div>
</section>
<style>
@media print
{
	#print_button{
		display:none;
	}
	.hide_non_print_div{
		display:none;
	}
	body{
		font-size:7px;
	}
}
</style>
<script type="text/javascript">
$(function () {
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});
</script>