<?php

require_once "config.php";
$id=$_POST['id'];
$msisdn=$_POST['msisdn'];
$customer_name=$_POST['customer_name'];
$credit_date=$_POST['credit_date'];
$amount=$_POST['edit_amount'];
$invoice_no=$_POST['edit_invoice_no'];
$sql="select * from invoice_details where  customer_name='".$customer_name."' and invoice_no='".$invoice_no."' and id!='".$id."'";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
    die("Customer exists");
}
$balance=0;
$sql="select sum(balance) as balance from invoice_details_history where   customer_id='".$id."'";
$query=mysqli_query($con,$sql);
if($result=mysqli_fetch_array($query)){
   $balance=$result['balance'];
}
$balance=$amount-$balance;


$sql="update invoice_details set amount='".$amount."',balance='".$balance."', invoice_no='".$invoice_no."', credit_date='".$credit_date."',customer_name='".$customer_name."',msisdn='".$msisdn."' where id='".$id."'";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}
echo mysqli_affected_rows($con);
$sql="update invoice_details_history set customer_name='".$customer_name."', invoice_no='".$invoice_no."' where customer_id='".$id."'";
$query=mysqli_query($con,$sql);
if($query==false){
    die(mysqli_error($con));
}

?>