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

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

$purchase_order_search 	= isset($_REQUEST['purchase_order_search']) ? $_REQUEST['purchase_order_search'] : "";
if($purchase_order_search) {
    $where	.= " AND purchase_order_id = '$purchase_order_search' ";
    $next_page_condition.="&purchase_order_search=$purchase_order_search";
}
$po_inv_status 	= isset($_REQUEST['po_inv_status']) ? $_REQUEST['po_inv_status'] : "";
if($po_inv_status) {
    $where	.= " AND is_inv_item_status = '$po_inv_status' ";
    $next_page_condition.="&po_inv_status=$po_inv_status";
}

$from_invoice_date = isset($_REQUEST['from_invoice_date']) ? utility::setFormatedDate($_REQUEST['from_invoice_date']) : "";
$to_invoice_date = isset($_REQUEST['to_invoice_date']) ? utility::setFormatedDate($_REQUEST['to_invoice_date']) : "";
if($from_invoice_date && $to_invoice_date){
	$where .= " AND invoice_date BETWEEN '$from_invoice_date 00:00:00' and '$to_invoice_date 23:59:59' ";
    $next_page_condition.="&from_invoice_date=".$_REQUEST['from_invoice_date']."&to_invoice_date=".$_REQUEST['to_invoice_date']."";
}else if($from_invoice_date){
	$where.= " AND invoice_date >= '$from_invoice_date' ";
	$next_page_condition.="&from_invoice_date=".$_REQUEST['from_invoice_date'];
}else if($to_invoice_date){
	$where.= " AND invoice_date <= '$to_invoice_date' ";
	$next_page_condition.="&to_invoice_date=".$_REQUEST['to_invoice_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		= "po_invoices";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
$page_header = "P/O Invoices";
include("paging/paging_query.inc.php");
################################## END PAGING WITH PER PAGE  ################################
$query = "SELECT * FROM $table_name $where ORDER BY id DESC LIMIT $eu, $limit";

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

$total_no_of_po_invoices = 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 po_invoices_items_mapping WHERE po_invoices_id = '".$_REQUEST['id']."' "); 
	
	$_SESSION['info_message'] = "PO Invoices deleted sucessfully";
	header("location: $file_name?page=view".$next_page_condition.$paging);
	exit();
}
#################################  END DELETE#################################################
#################################  Export CSV    ##################################
if(isset($_REQUEST['action']) && $_REQUEST['action']=='export_to_po_invoices_csv'){
  include("export_to_po_invoices_csv.inc.php");
} 
#################################  End Export  #####################################    

?>


<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 -->
				<?//php include_once('po_invoices_search_options.inc.php');?>
				<!-- /.search form -->
				Total No. of PO Invoices <?=$total_no_of_po_invoices;?>
				<div style="float:right;margin-bottom:4px;"><a href="index?page=view&action=export_to_po_invoices_csv<?=$next_page_condition;?>">Export to CSV&nbsp;&nbsp;<i class="fa fa-download" style="font-size:20px;"></i></a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					   </div>
					<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">Add PO Invoices</button>
					   <div style="float:right;margin-bottom:4px;">&nbsp;&nbsp;</div>
					</div>
					<?php } ?>
					<? if($result>0){?>
					<div class="table-responsive">
					 <table id="example1" class="table table-bordered table-striped">
						<thead>
						  <tr>
							<!--<th>#</th>-->
							<th>Purchase Order Ref.</th>
							<th>Invoice No.</th>
							<th>Invoice Date</th>
							<th>Total Inc VAT</th>
							<th>Status</th>
							<th>Discrepancy</th>
						     <th>Send to Xero</th>
							 <?php if($can_view){ ?>
							<th>Edit</th>
							<th>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 $gsi->getPurchaseById($row["purchase_order_id"]); ?></td>
							<td><?php echo $row["invoice_number"];?></td>
							<td><?php echo utility::getFormatedDateReturn($row["invoice_date"]); ?></td>
							<td><?php echo $row["total_inc_vat"];?></td>
							<td>
								<?php 
								
									if($row["is_inv_item_status"] == 1){
										echo "All Items Matching";
									}else if($row["is_inv_item_status"] == 2){
										echo "Items Not Matching";
									}else if($row["is_inv_item_status"] == 3){
										echo "Not All Items Received";
									}
								?>
							</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>-->
								<td>
								<?php if ($row["discrepancy"] == 1): ?>
									Yes 
									<a href="#" class="view-details text-primary" data-bs-toggle="modal" data-bs-target="#detailsModal" 
									   data-details="<?php echo ucfirst( htmlspecialchars($row['discrepancy_details'], ENT_QUOTES, 'UTF-8')); ?>">
									   (View Details)
									</a>
								<?php else: ?>
									No
								<?php endif; ?>
							</td>

							<td><a href="index?page=view&action=export_to_po_invoices_item<?=$next_page_condition;?>" target="_blank"><i class="fa fa-download" style="font-size:20px;"></i> Export</a></td>
							<?php if($can_view){ ?>
							<td><a href="index?page=add&action=edit&po_invoices_id=<?=urlencode(utility::app_crypt($row['id'],'e'));?>&id=<?=$row['id'].$next_page_condition.$paging;?>"> <i class="fa fa-fw fa-edit text-primary text-bold"></i> </a></td>
							<td><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 text-bold"></i> </a></td>
							<?php } ?>
						  </tr>
							<? } ?>
							
						</tbody>
					 </table>
					 </div>
					 <? }else{ ?>
						No PO Invoices 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">Add PO Invoices</button>
                </div>
				<?php } ?>
				</form>
			</div>
		</div>
		</div>
	</div>
	</div>
</section>
<div class="modal fade" id="detailsModal" tabindex="-1" aria-labelledby="detailsModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="detailsModalLabel">Discrepancy Details</h5>
                <button type="button" class="btn-close text-bold text-danger border-0" data-bs-dismiss="modal" aria-label="Close">X</button>
            </div>
            <div class="modal-body">
                <p id="modalDetails"></p>
            </div>
        </div>
    </div>
</div>

<script type="text/javascript">
$(function () {
	$(".btn-close").click(function(){
		 $("#detailsModal").modal('hide');
	});
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});$(document).on("click", ".view-details", function () {
    var details = $(this).data("details");
    $("#modalDetails").text(details);
    $("#detailsModal").modal('show'); // Show modal on hover
});



</script>