<?php

include 'load.php';

$ReturnId= $_GET['g'];

$getReturnDetailsOngoingIdQuery=$conn->query("SELECT * FROM tbl_return WHERE return_id='$ReturnId' ");
if ($GRETDOrs=$getReturnDetailsOngoingIdQuery->fetch_array()) {
    //$ReturnId=$GRETDOrs[0];
    $OrderId=$GRETDOrs[1];
    $ReturnDateTime=$GRETDOrs[3];

    $getOrderDetailsQuery=$conn->query("SELECT * FROM mobileorders mor INNER JOIN invoice_other_details iod ON mor.order_id=iod.order_id WHERE mor.order_id='$OrderId' ");
	if ($GODrs=$getOrderDetailsQuery->fetch_array()) {
	    $ORID=$GODrs[1];
	    $Amount=$GODrs[3];
	    $BranchId=$GODrs[19];
	}

}

?>

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
	<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard

">
	<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard

">
	<meta name="author" content="Payroll | Pos & Invetory System | Dashboard

">
	<meta name="robots" content="noindex, nofollow">

	<title>Home | Payroll | Pos & Invetory System | Dashboard

</title>


	<?php
	require_once 'headercss.php';
	?>
</head>
<body>
	<div id="global-loader">
		<div class="whirly-loader"> </div>
	</div>

	<div class="main-wrapper">
		<?php
		require_once 'header.php';
		?>

		<?php
		require_once 'navbar.php';
		?>
		<div class="page-wrapper">
			<div class="content">
				<div class="page-header">
					<div class="page-title">
						<h4>Create Sales Return</h4>
						<h6>Add/Update Sales Return</h6>
					</div>
				</div>
				<div class="card">
					<div class="card-body">

						<div class="row">
							<div class="col-md-8">
								<h3>Return Invoice</h3> 
							</div>

							<div class="col-md-4">
								<p style="float: right;">Order Number - <?php echo $ORID; ?></p>
							</div>
						</div>

						<div class="col-md-12">
							<div class="table-responsive">
								<table class="table">
									<thead>
										<tr>
											<th>Product</th>
											<th>Quantity</th>
											<th>Price</th>
											<th>Discount(%)</th>
											<th>Subtotal</th>
											<th>Return Qty</th>
											<th>Return Value</th>
											<th>Action</th>
										</tr>
									</thead>
									<tbody>
										<?php
											$total_bill_amount = 0;
											$total_bill_return = 0;
											$getOrderItemsQuery=$conn->query("SELECT * FROM mobiletemporarycart WHERE orderid='$OrderId' ");
											while($GOIrs=$getOrderItemsQuery->fetch_array()) {
										    	$TempId=$GOIrs[0];
										    	$ProId=$GOIrs[2];
										    	$ProPrice=$GOIrs[3];
										    	$ProDis=$GOIrs[4];
										    	$ProQty=$GOIrs[5];
										    	$ReturnQty=$GOIrs[7];

										    	$discounted_amount = $ProPrice/100 * $ProDis;
												$sub_total_one_item = $ProPrice - $discounted_amount;
												$sub_total = $sub_total_one_item*$ProQty;
												$total_bill_amount = $total_bill_amount+$sub_total;

												$return_sub_total = $sub_total_one_item * $ReturnQty;
												$total_bill_return = $total_bill_return+$return_sub_total;
												$available_to_return = $ProQty - $ReturnQty;

												$getProductDetailsQuery=$conn->query("SELECT * FROM mobileproducts WHERE prid='$ProId' ");
												if ($GPDrs=$getProductDetailsQuery->fetch_array()) {
												    $ProductName=$GPDrs[3];
												    $Cover=$GPDrs[14];
												}

										?>
										<tr>
											<td class="productimgname">
												<a class="product-img">
													<img src="images/products/cover/<?php echo $Cover; ?>">
												</a>
												<a href="javascript:void(0);"><?php echo $ProductName; ?></a>
											</td>
											<td><?php echo $ProQty; ?></td>
											<td><?php echo number_format($ProPrice,2); ?></td>
											<td><?php echo $ProDis; ?></td>
											<td><?php echo number_format($sub_total,2); ?></td>
											<td style="background: #fde1e1"><?php echo $ReturnQty; ?></td>
											<td style="background: #fde1e1"><?php echo number_format($return_sub_total,2); ?></td>
											<td>
											<?php
												if($available_to_return>0){ ?>
												<button class="btn btn-danger btn-sm" onclick="makeReturn(<?php echo $TempId; ?>, <?php echo $ProQty; ?>, <?php echo $ReturnQty; ?>,<?php echo $OrderId; ?>,<?php echo $ProId; ?>,<?php echo $BranchId; ?>,<?php echo $ReturnId; ?>);">Return</button>
											<?php }else{
												echo "Returned";
											} ?>
											</td>
										</tr>
										<?php } ?>
									</tbody>
									<tfoot>
										<tr>
											<td colspan="4">Total</td>
											<td><?php echo number_format((float) $total_bill_amount,2); ?></td>
											<td></td>
											<td><?php echo number_format((float) $total_bill_return,2); ?></td>
										</tr>
									</tfoot>'
								</table>
							</div>

							<form id="Finalize-Return-Order">
								<input type="hidden" name="return_id" value="<?php echo $ReturnId; ?>">
								<button type="submit" class="btn btn-primary">Finalize</button>
							</form>

						</div>
						
						
					</div>
				</div>
			</div>
		</div>
	</div>


	<script src="assets/js/jquery-3.6.0.min.js"></script>

	<script src="assets/js/feather.min.js"></script>

	<script src="assets/js/jquery.slimscroll.min.js"></script>

	<script src="assets/js/jquery.dataTables.min.js"></script>
	<script src="assets/js/dataTables.bootstrap4.min.js"></script>

	<script src="assets/js/bootstrap.bundle.min.js"></script>

	<script src="assets/plugins/select2/js/select2.min.js"></script>

	<script src="assets/js/moment.min.js"></script>
	<script src="assets/js/bootstrap-datetimepicker.min.js"></script>

	<script src="assets/plugins/sweetalert/sweetalert2.all.min.js"></script>
	<script src="assets/plugins/sweetalert/sweetalerts.min.js"></script>
	<script src="assets/alertify/alertify.js"></script>
	<script src="assets/alertify/alertify.min.js"></script>

	<script src="assets/js/script.js"></script>

	<script src="process.js"></script>


	<script>
        
        $(document).on('submit', '#Finalize-Return-Order', function(e){
        e.preventDefault(); //stop default form submission
        var formData = new FormData($(this)[0]);

        $.ajax({

                beforeSend : function() {

                    Swal.fire({
                      title:'Info !',
                      icon:'info',
                      text:'Details is being sending...Please wait.',
                      showConfirmButton:false,
                      showCancelButton:false,
                      allowOutsideClick: false,
                    });

                },

                url:"post/finalize_return.php",
                type: 'POST',
                data: formData,
                //async: false,
                cache: false,
                contentType: false,
                processData: false,

                success: function (data) {

                    var json=JSON.parse(data);
                    if(json.result){

                        setTimeout(function () {
                        
                        window.location.href = "returnreciept.php?rno=<?php echo $ReturnId; ?>";
                        //window.location.href = "salesreturnlist.php";
                       //location.reload();
                    },1000);

                    }else{
                        Swal.fire({
                        title:'Warning !',
                        icon:'warning',
                        text:'Something went wrong, please try again.'
                        });
                    }

                    

                }

            });

        return false;
        });
    </script>

</body>
</html>