<?php
session_start();
include ('../inc/dbcon.php');
include ('../inc/functions.php');
if(isset($_SESSION["axeslogin"]) && $_SESSION["axeslogin"] == true){
$aid=$_SESSION['uid'];
$brid=$_SESSION['abrid'];    
}else{
header('Location:../index.php');
exit;     
}
$dtnow = date("Y-m-d h:i:s", time());
?>



<?php 
if(isset($_POST['addsitem'])){ 
$item=intval($_POST['item']);   
?>
<div class="col-md-12 popup_details_div addservice">
<div class="row">
<div id="server-results"><!-- For server results --></div>    
</div>
<div class="row">
<div class="col-md-12">
<div class="col-md-1"></div>
<div class="col-md-10">
<?php 
$sql="SELECT * FROM tbl_purchase WHERE id='".$item."' ORDER BY id ASC LIMIT 1";    
$admin=mysqli_query($con,$sql) or die(mysqli_error($con));    
$adm=mysqli_fetch_array($admin);    
?>
<div class="form-group">
    <label>Invoice No</label>  
    <input type="text" name="invno" maxlength="45" value="<?php echo $adm['invno'];?>" id="invno" class="form-control"  readonly/>    
    <input type="hidden" maxlength="11" value="<?php echo $adm['id'];?>" class="form-control" name="invid" autocomplete="off" readonly> 
</div>    

<input type="hidden" name="purduepay" value='1' readonly />  
<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
            <label>Customer</label>    
            <input type="text" name="chkno" maxlength="45" value="<?php echo get_fild_data('tbl_supplier',$adm['supid'],'name');?>" id="supid" class="form-control"  readonly/>    
        </div>
    </div>    
    <div class="col-md-6">
        <div class="form-group">
            <label>Invoice Date</label>
            <input type="text" name="apdate" maxlength="45" value="<?php echo date("d M Y", strtotime($adm['apdate']));?>" id="apdate" class="form-control"  readonly/>    
        </div>
    </div>     
</div> 
<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
            <label>Invoice Amount</label>    
            <input type="text" name="total" maxlength="45" value="<?php echo $adm['total'];?>" id="total" class="form-control"  readonly/>    
        </div>
    </div>    
    <div class="col-md-6">
        <div class="form-group">
            <label>Paid Amount</label>
            <input type="text" name="paid_amount" maxlength="45" value="<?php echo $adm['rawcash'];?>" id="paid_amount" class="form-control"  readonly/>    
        </div>
    </div>     
</div>  

<div class="row">
    <div class="col-md-6">    
        <div class="form-group">
        <label>Due Amount</label>
        <input type="text" name="due_amount" maxlength="45" value="<?php echo $adm['total']-$adm['rawcash'];?>" id="due_amount" class="form-control"  readonly/>    
        </div>
    </div> 
    <div class="col-md-6">    
        <div class="form-group">
        <label>Now Pay</label>
        <input type="text" name="pay_amount" maxlength="45" value="<?php echo $adm['total']-$adm['rawcash'];?>" id="pay_amount" class="form-control" />    
        </div>
    </div>    
   
</div>    
</div>    
<div class="col-md-1"></div>    
</div>    
</div>    

</div>
<div class="clearfix" ></div>
<div class="col-md-12 nopadding widgets_area"></div>    
<div class="row"style="margin-top: 15px" >
<div class="col-md-8"></div>
<div class="col-md-4 text-right" >
<?php if($adm['total']-$adm['rawcash']>0){ ?>
<input type="button" id="additem" class="btn btn-flat bg-purple btn-sm " value="Paid"/>
<?php }?>
</div> 
</div>
<script type="text/javascript">
function chek_error(){
var pay_amount = $('#pay_amount').val();
$('.LV_validation_message').remove();
    
    
if(pay_amount == '' || pay_amount <= 0){
$('#pay_amount').addClass('LV_invalid_field');   
$('#pay_amount').after("<span class='LV_validation_message LV_invalid'>Can't be empty!</span>").addClass('has-error'); } else {
$('#pay_amount').removeClass('LV_invalid_field');     
}

if(pay_amount == '' || pay_amount <= 0){
return false;    
}else{
return true;     
}    
}  
   
    
$(document).on('blur', '#pay_amount', function() {
chek_error();    
});    
</script> 

<?php }
if(isset($_POST['purduepay'])){
   
$purid = remove_junk(escape($_POST['invid']));
$total = remove_junk(escape($_POST['total']));
$pay_amount = remove_junk(escape($_POST['pay_amount']));
$invno = remove_junk(escape($_POST['invno']));
$paid_amount = remove_junk(escape($_POST['paid_amount']));

$amount=$paid_amount+$pay_amount;

if($amount<=$total){
    $sqls="UPDATE tbl_purchase SET rawcash=$amount WHERE id='$purid'";
    $results = mysqli_query($con,$sqls) or die(mysqli_error($con));  
}
       


  
$efid=mysqli_affected_rows($con);    
if($efid>0){    
$act =remove_junk(escape('Invoice No: '.$invno.'- Paid Amount'.$amount));    
write_activity($aid,'PUR','Invoice has been Updated',$act);    
echo json_encode(array(
'status' => 'success',
'message'=> 'Data Update Successfully!!'   
));
exit;
}else{
echo json_encode(array(
'status' => 'error',
'message'=> 'Sorry! Data Fail to Update!!'
));    
}   
}


?> 

