<?php
session_start();
$where 					= " WHERE 1=1 ";
$next_page_condition 	= "";
$paging ="";

if($_REQUEST['start']){
	$paging .="&start=".$_REQUEST['start'];
}
if(isset($_REQUEST["from"]) && $_REQUEST["from"] !=""){
	$_SESSION["from"] = $_REQUEST["from"];
}
if(isset($_REQUEST['id']) && $_REQUEST['id'] !=""){
	$where .= " AND id = ".$_REQUEST['id']." ";
}
$po_id = utility::app_crypt($_REQUEST['po_id'],'d');
if($_REQUEST['id'] != $po_id){
	die("Invaild URL");
}
$restricted_groups = [14, 16];
$can_view = !in_array($_SESSION['admin_group_id'], $restricted_groups);
###########################################  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		= "purchase_order";
$paging_table_name	= $table_name;
$paging_query		= $where;
$join_query_for_paging = "SELECT * FROM $table_name  $where ";
include("paging/paging_query.inc.php");
############################################ END PAGING WITH PER PAGE  #######################################
$query = "SELECT * FROM $table_name $where ";
$rs = mysqli_query($conn,$query);
$enq_row = mysqli_fetch_assoc($rs);
$enq_row=utility::validate_crypt_data($enq_row);
?>
<style>
.form-group .control-label{
	padding-top:0px;
}
.content{
	min-height: 0px;
}
.content-header h1{
	float:left;
	padding-bottom: 10px;
}
</style>
<section class="content-header">
	<div class="container-fluid">
		<div class="row">
		<div class="col-lg-12 col-md-12 col-sm-12">
		  <div class="col-sm-12">
			  <h1 style="float:left;clear:right;">
				Purchase Order Details - <span style='color:#ed5c5c;'><?= $enq_row['order_ref'];?></span>
			  </h1>
		  </div>
			<?
				$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
				$query_string = str_ireplace("page=view_purchase_order_details", "page=view", $query_string);
			?>
			  <button type="button" id="back" name="back" value="back"  style="float:right;margin-bottom: 15px;" class="btn btn-success" onclick="window.location.href = 'index?<?=$query_string.$next_page_condition.$paging;?>'">Back</button>
			  <?php if($can_view){ ?>
			  <? if((isset($_REQUEST["from"]) && $_REQUEST["from"] == "view_purchase_order") || (isset($_REQUEST["from"]) && $_REQUEST['from'] == "view_purchase_order_details")){ 
			  ?>
				<a href="index?page=add&from=view_purchase_order_details&action=edit&po_id=<?=urlencode(utility::app_crypt($_REQUEST['id'],'e'));?>&id=<?=$_REQUEST['id'].$next_page_condition;?>" style="float:right;margin-right:5px;" class="btn btn-primary">Edit</a>
			  <? } ?>
			  <?php } ?>
		</div>
		</div>
	</div>
</section>

<section class="content">
	<div class="container-fluid">
		<div class="row">
			<div class="col-lg-12 no-padding">
				<div class="row">
					<!----------------------- Message ------------------------------->
					<? if(isset($_SESSION['info_message'])){ ?>
					  <div class="col-sm-12 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 ------------------------------->
					<!-- general form elements -->
					<? 
						$col_width = 12;
					?>
					<div class="col-lg-6 col-md-6 col-sm-12" style="/* min-height:700px; */">
					<div class=" scrollbar-default box box-warning card" style="height: 300px;overflow: hidden;overflow-y: scroll;">
					<form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add_placement&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
					<div class="box-body">
							<div class="form-group">
							  <label for="supplier_id" class="col-sm-4 control-label">Supplier</label>
							  <div class="col-sm-8">
								<? echo $gsi->getSupplierById($enq_row['supplier_id']); ?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Supplier Address" class="col-sm-4 control-label">Supplier Address</label>
							  <div class="col-sm-8">
							 <?php 
								$supplier_sql = "SELECT * FROM supplier_management WHERE id = '".$enq_row['supplier_id']."' ";
								$supplier_rs = mysqli_query($conn,$supplier_sql);
								$supplier_row = mysqli_fetch_assoc($supplier_rs);
							
								$supplier_address = "";
								if(isset($supplier_row["address1"]) && $supplier_row["address1"] !=""){
									$supplier_address .= $supplier_row["address1"].", ";
								}
								if(isset($supplier_row["address2"]) && $supplier_row["address2"] !=""){
									$supplier_address .= $supplier_row["address2"].", ";
								}
								if(isset($supplier_row["address3"]) && $supplier_row["address3"] !=""){
									$supplier_address .= $supplier_row["address3"].", ";
								}
								if(isset($supplier_row["town_city"]) && $supplier_row["town_city"] !=""){
									$supplier_address .= $supplier_row["town_city"].", ";
								}
								if(isset($supplier_row["postcode"]) && $supplier_row["postcode"] !=""){
									$supplier_address .= $supplier_row['postcode'].", ";
								}
								if(isset($supplier_row["country"]) && $supplier_row["country"] !=""){
									$supplier_address .= $gsi->getCountryById($supplier_row['country']);
								}
								echo $supplier_address;
							?>
							  </div>
							</div>
							
							</form>
							</div>
							</div><!-- /.box -->
							</div><!-- /.box -->
							<div class="col-lg-6 col-md-6 col-sm-12" style="/* min-height:700px; */">
						<div class=" scrollbar-default box box-success card" style="height: 300px;overflow: hidden;overflow-y: scroll;">
						<form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add_placement&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
						<div class="box-body">
						<div class="form-group">
							  <label for="site_id" class="col-sm-4 control-label">Site</label>
							  <div class="col-sm-8">
								<? echo $gsi->getSiteById($enq_row["site_id"]); ?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="Site Address" class="col-sm-4 control-label">Site Address</label>
							  <div class="col-sm-8">
								<?php 
								$site_sql = "SELECT * FROM supplier_management WHERE id = '".$enq_row['supplier_id']."' ";
								$site_rs = mysqli_query($conn,$site_sql);
								$site_row = mysqli_fetch_assoc($site_rs);
							
								$site_address_detail = "";
								if(isset($site_row["address1"]) && $site_row["address1"] !=""){
									$site_address_detail .= $site_row["address1"].", ";
								}
								if(isset($site_row["address2"]) && $site_row["address2"] !=""){
									$site_address_detail .= $site_row["address2"].", ";
								}
								if(isset($site_row["address3"]) && $site_row["address3"] !=""){
									$site_address_detail .= $site_row["address3"].", ";
								}
								if(isset($site_row["town_city"]) && $site_row["town_city"] !=""){
									$site_address_detail .= $site_row["town_city"].", ";
								}
								if(isset($site_row["postcode"]) && $site_row["postcode"] !=""){
									$site_address_detail .= $site_row['postcode'].", ";
								}
								if(isset($site_row["country"]) && $site_row["country"] !=""){
									$site_address_detail .= $gsi->getCountryById($site_row['country']);
								}
								echo $site_address_detail;
								?>
							  </div>
							</div>
						<div class="form-group">
							  <label for="call_reference_id" class="col-sm-4 control-label">Call Reference</label>
							  <div class="col-sm-8">
								<?php echo $gsi->getJobOrderById($enq_row["call_reference_id"]); ?>
							  </div>
							</div>
							<div class="form-group">
							  <label for="purchase_notes" class="col-sm-4 control-label">Notes</label>
							  <div class="col-sm-8">
								<?php echo $enq_row["purchase_notes"]; ?>
							  </div>
							</div>
						  </div><!-- /.box-body -->
						</form>
					  </div><!-- /.box -->
					</div><!-- /.box -->
				</div>
			</div>
		</div>
	</div>
</section>
<section class="content" >
	<div class="container-fluid">
		<div class="row">
			<div class="col-12"> 
				<div id="" class="card">
					<div class="card-header">
						<h3 class="card-title">Purchase Order Items</h3>

					</div>
					<?
					 $query = "SELECT * FROM purchase_order_items_mapping WHERE po_id = ".$_REQUEST['id']." ORDER BY id ASC ";
					$sql=mysqli_query($conn,$query);
					$result=mysqli_num_rows($sql);
					?>
					<div class="card-body table-responsive" style="height: auto;">
						<? if($result>0){ ?>
						<div class="box box-primary" id="overflow_div">
						<!-- Start  records header -->
						<table id="example1" class="table table-bordered table-striped">
							<thead>
								<tr>
									<th>Quantity</th>
									<th>Stock Ref</th>
									<th>Supplier</th>
									<th>Description</th>
									<th>Unit Price</th>
									<th>Total</th>
									
								</tr>
							</thead>
							<tbody>
							
							<!-- End records header -->
							
							<?
								
								$final_total=0;
								$grand_total=0;
								
								while($row = mysqli_fetch_array($sql)){	
								
								$total = $row["quantity"] * $row["unit_price"];
								$final_total = $final_total + $total; 
								$vat_amount = $row["total"] * 20 /100;
								$amount_with_vat = $vat_amount+$total;
								$grand_total = $amount_with_vat + $grand_total;
							?>
							<!-- Start data -->
							<tr>
								<td><?php echo $row['quantity'];?></td>
								<td><?php echo $row['stock_ref'];?></td>
								<td><?php echo $row['supplier_ref']; ?></td>
								<td><?php echo $row['description'];?></td>
								<td><?php echo $row['unit_price'];?></td>
								<td><?php echo $row['total'];?></td>
								
							</tr>
							<!-- End data -->
							<? }?>
							
							</tbody>
						</table>
						</div>
					<div class="form-group col-sm-12 ">	
						<label class="" style="margin-top:6px;float: right;">Total Excluding Vat: &pound;<?=number_format($final_total,2);?></label>
					</div><br/>
					<div class="form-group col-sm-12 ">	
						<label class="" style="margin-top:6px;float: right;" >Total Including Vat 20%: &pound;<?=number_format($grand_total,2);?></label>
					</div>
						<? }else{ ?>
						<div class="box box-primary p-15">No purchase order found...</div>
						<? } ?>
					</div>					
				</div>
			</div>
		</div>
	</div>
</section>

<style>
	#overflow_div::-webkit-scrollbar
    {
      width: 5px;  /* for vertical scrollbars */
      height: 5px; /* for horizontal scrollbars */
    }
 
    #overflow_div::-webkit-scrollbar-track
    {
      background:#E7E8E9;
    }
 
    #overflow_div::-webkit-scrollbar-thumb
    {
      background: #3c8dbc;
    }
	#overflow_div{
		height:200px;
		overflow-y: auto;
	}
	@media print{
		body{
			visibility: hidden;
		  }
		  #overflow_div{
			height:auto;
		  }
		  .printable{
			visibility: visible;
		  }
		  .printable{
			position: absolute;
			left: 0;
			top: 0;
		  }
	}
.contactpopup{
	height:620px !important;
}
</style>
<script type="text/javascript">
/* function divPrint(section){
	// Some logic determines which div should be printed...
	// This example uses div3.
	$("#"+section).addClass("printable");
	window.print();
} */
$(function () {
	$(".add_new_contacts").click(function(){		
		var forename = $(this).attr("data-forename");	
		$("#myModalLabel").html("Contacts for <span style='color:Tomato'>"+forename+"</span>");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var from = "&from=view_site_details";
		var site_id = $(this).attr("alt");
		$('.modal-dialog').css({"max-width":"80%"});		
		$(".modal_iframe").addClass("contactpopup").attr("src","<?=$config->frontEndURL?>/admin/site_management/index?page=add_site_contact&site_id="+site_id+from+hide_top_left);
	});
	
	$(".edit_contacts").click(function(){
		var forename = $(this).attr("data-forename");	
		$("#myModalLabel").html("Contacts for <span style='color:Tomato'>"+forename+"</span>");
		var hide_top_left = "&hide_top=1&hide_left=1";
		var from = "&from=view_site_details";
		//var site_id = $(this).attr("data-site_id");			
		var contact_encrypt_id = $(this).attr("data-contact_encrypt_id");			
		var contact_id = $(this).attr("alt");
		$('.modal-dialog').css({"max-width":"80%"});		
		$(".modal_iframe").addClass("contactpopup").attr("src","<?=$config->frontEndURL?>/admin/site_management/index?page=add_site_contact&action=edit&site_contact_id="+contact_encrypt_id+"&id="+contact_id+from+hide_top_left);
	});
	$(".contact_delete").click(function(){	
		if(confirm('Are you sure! You want to delete') == true){
			var site_id = $(this).attr("alt");
			if(site_id != ""){
				$('#delete_contact_load_'+site_id).show();
				var form_data   = {'site_id':site_id};       
				$.ajax({
					async: true,
					type: "POST",
					url: site_url+"/modules/site_management/index.php?page=ajax_actions&action=delete_contact",
					data: form_data,
					success: function(result){    
						var resultObj = jQuery.parseJSON(result); 
						if(resultObj.status == "success"){
							$('#delete_contact_done_'+site_id).show();
							$('#delete_contact_load_'+site_id).hide();
							
							setTimeout(function() {
								$('#delete_contact_done_'+site_id).hide();
								$('#contact_record_no_'+site_id).remove();
							},1000);
						}
					}
				});
			}
		}else{
			return false;
		}
	});		  
	$(".close_button").click(function(){
		$(".modal_iframe").attr("src","");
		location.reload();
	});
});
</script>