<?php
include "header.php";
include "dbconnection.php";

// STart IF, form submission
if ( isset($_POST["updatesale"]) ) {


$saleidd=$_POST["saleidd"];
$next_payment=$_POST["next_payment"];
$next_amount=$_POST["next_amount"];
$dueamount=$_POST["dueamount"];
$ins_number=$_POST["ins_number"];

$finalamount=$dueamount-$next_amount;

$sql3 ="INSERT INTO ospos_sales_payments (payment_type,payment_amount,sale_id) VALUES ('$next_payment','$next_amount','$saleidd') ";
$result3 = mysqli_query($con, $sql3);

$sql34 ="UPDATE ospos_sales_payments SET ins_number='$ins_number' WHERE sale_id='$saleidd' AND payment_type='Due' ";
$result34 = mysqli_query($con, $sql34);


$sql33 ="UPDATE ospos_sales_payments SET payment_amount='$finalamount' WHERE sale_id='$saleidd' AND payment_type='Due'";
$result33 = mysqli_query($con, $sql33);
echo "DONE!";
// header( 'refresh:2;url=http://localhost:4848/custom-pos/credit.php?sale_id='.$. );
header('Location: http://localhost:4848/custom-pos/credit.php?sale_id='.$saleidd);
}
else 
{

if ( isset($_GET["sale_id"]) ) {
    $saleid=$_GET["sale_id"];
}


$query1="SELECT * FROM ospos_sales WHERE sale_id='$saleid' ";
$res=mysqli_query($con,$query1);
  while ($row = $res->fetch_assoc()) {
    $sale_time=$row['sale_time'];
    $invoice_number=$row['invoice_number'];
}


$query2="SELECT * FROM ospos_sales_payments WHERE sale_id='$saleid' ";
$res2=mysqli_query($con,$query2);


$query9="SELECT payment_amount FROM ospos_sales_payments WHERE sale_id='$saleid' AND payment_type='Due' ";
$res9=mysqli_query($con,$query9);
  while ($row9 = $res9->fetch_assoc()) {
    $res99=$row9['payment_amount'];
}

$query19="SELECT ins_number FROM ospos_sales_payments WHERE sale_id='$saleid' AND payment_type='Due' ";
$res19=mysqli_query($con,$query19);
  while ($row19 = $res19->fetch_assoc()) {
    $ins_number1=$row19['ins_number'];
}
?>


<!-- Update Form -->
<div class="col-md-12">
							<form action="#" method="POST">
								<div class="panel panel-flat">
									<div class="panel-heading">
										<div class="row">
											<div class="col-md-10 col-md-offset-1">
												<h5 class="panel-title">Update Credit Sale</h5>
												<div class="heading-elements">
													<ul class="icons-list">
								                		<li><a data-action="collapse"></a></li>
								                		<li><a data-action="reload"></a></li>
								                		<li><a data-action="close"></a></li>
								                	</ul>
							                	</div>
											</div>
										</div>
<p>Sale#:<?php echo "<a href='http://localhost:4848/POS22/public/sales/receipt/".$saleid."' target='_blank'>".$saleid."</a>"; ?></p>
<p>Invoice#:<?php echo "<a href='http://localhost:4848/POS22/public/sales/invoice/".$invoice_number."' target='_blank'>".$invoice_number."</a>"; ?></p>
<p>Sale Date & time: <?php echo $sale_time; ?></p>
										
									</div>

									<div class="panel-body">
										<div class="row">
											<div class="col-md-10 col-md-offset-1">
<?php while ($row2 = $res2->fetch_assoc()) { ?>
<div class="form-group col-md-12">
<label for="" class="col-md-2">Payment Type</label>
<div class="col-md-2">
<input type="text" name="" value="<?php echo $row2['payment_type']; ?>" id="" class="form-control input-sm" readonly="true">
</div>
	<div class="col-md-2">
	<div class="input-group ">
	<span class="input-group-addon"><b>S.R</b></span>
	<input type="text" name="" value="<?php echo $row2['payment_amount']; ?>" id="" class="form-control input-sm" readonly="true">
					</div>
					</div>

					<div class="col-md-4">
	<div class="input-group">
	<span class="input-group-addon"><b>Date</b></span>
	<input type="text" name="" value="<?php echo $row2['payment_time']; ?>" id="" class="form-control input-sm" readonly="true">
					</div>
					</div>

</div>


<?php
 } 
 $ins_number1=$ins_number1+1;
 ?>
 </div>
<input type="hidden" name="ins_number" value="<?php echo $ins_number1; ?>" class="form-control" >
<input type="hidden" name="dueamount" value="<?php echo $res99; ?>" class="form-control" >
<input type="hidden" name="saleidd" value="<?php echo $saleid; ?>" class="form-control" >

<div class="form-group col-md-12">
<label for="" class="col-md-2">Next Payment</label>
<div class="col-md-2">
<input type="text" name="next_payment" value="<?php echo 'Installment-'.$ins_number1; ?>" class="form-control">
</select>
</div>

					<div class="col-md-2">
					<div class="input-group">
					<span class="input-group-addon"><b>S.R</b></span>
					<input type="number" name="next_amount" required="required" id="next_amount" class="form-control input-sm">
					</div>
					</div>

												<div class="text-right">
					<button type="submit" name="updatesale" id="updatesale" class="btn btn-primary">Update Sale<i class="icon-arrow-right14 position-right"></i></button>
												</div>
											</div>
										</div>
									</div>
							</form>
						</div>					

						<!-- End of Update Form -->

						<?php } ?> 
						<!-- End of If, form to be submitted -->