<?php 
session_start();
if(empty($_SESSION['id'])):
header('Location:../index.php');
endif;
include('../dist/includes/dbcon.php');

date_default_timezone_set('Asia/Manila');
	$myid = $_SESSION['id'];
	$cid = $_POST['cid'];	
	$sid = $_POST['sid'];
	$amount = $_POST['amount'];
	$balance = $_POST['balance'];
	date_default_timezone_set("Asia/Manila"); 
	$date = date("Y-m-d");
	$id = $_SESSION['id'];
	$date1 = date("Y-m-d H:i:s");
	
	

	$query3=mysqli_query($con,"select * from payment where sales_id='$sid'")or die(mysqli_error($con));
    	$row3=mysqli_fetch_array($query3);
				
    			$pid=$row3['payment_id'];
				$sales_id=$row3['sales_id'];
    			$payment=$row3['payment'];
    			$due=$row3['due'];
    			$total=$row3['due']+$row3['interest'];
    			$bal=$row3['remaining'];
    			$sid=$row3['sales_id'];
    			$payment_date=date("Y-m-d",strtotime($row3['payment_for']));
				
				$query22=mysqli_query($con,"select * from sales where sales_id='$sales_id'")or die(mysqli_error($con));
				$row22=mysqli_fetch_array($query22);
				$discountko=$row22['discount'];
				$discko=$row22['discko'];
				$amount_due=$row22['amount_due'];
				$branch_id=$row22['branch_id'];
				$total=$row22['total'];
    			    			
								
					$query111=mysqli_query($con,"select * from customer where cust_id='$cid'")or die(mysqli_error($con));
				$row111=mysqli_fetch_array($query111);			
								
								
    			if ($amount >= $total)
    			{
					$change=$amount-$total;
				mysqli_query($con,"UPDATE payment SET payment=payment+'$total',user_id='$id',payment_date='$date1',status='paid',remaining='0',rebate='0' where payment_id='$pid'") or die(mysqli_error($con));
				$amount1=$amount-$total;
				//mysqli_query($con,"INSERT INTO sales(cust_id,user_id,cash_tendered,discount,discko,amount_due,cash_change,date_added,modeofpayment,branch_id,total,status) \
				//VALUES('$cid','$myid','$amount','$discountko','$discko','$amount_due','$change','$date1','cash','$branch_id','$total','cash')")or die(mysqli_error($con));
				mysqli_query($con,"UPDATE sales SET date_added='$date1',modeofpayment='cash',cash_tendered='$amount',cash_change='$change',status='cash' where sales_id='$sales_id'") or die(mysqli_error($con));
    				
				$name=$row111['cust_last'].", ".$row111['cust_first'];
		$remarks="added a payment of $amount for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		
	
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='receipt_credit.php?cid=$cid&sid=$sid'</script>";
    			}
    			elseif($amount < $total)
    			{
					$toro=$total - $amount;
					//mysqli_query($con,"UPDATE customer SET balance=balance-'$amount' where cust_id='$cid'") or die(mysqli_error($con)); 
					$remaining=$total-$amount;
    				mysqli_query($con,"UPDATE payment SET payment=payment+'$amount',user_id='$id',payment_date='$date1',status='partially paid',remaining='$remaining' where payment_id='$pid'") or die(mysqli_error($con));
    				$amount1=$amount-$total;
					//mysqli_query($con,"INSERT INTO sales(cust_id,user_id,cash_tendered,discount,discko,amount_due,cash_change,date_added,modeofpayment,branch_id,total,status) \
				//VALUES('$cid','$myid','$amount','$discountko','$discko','$amount_due','$change','$date1','cash','$branch_id','$total','cash')")or die(mysqli_error($con));
				
				mysqli_query($con,"UPDATE sales SET date_added='$date1',modeofpayment='partial',cash_tendered='$amount',partial1='$amount',partial2=partial2+'$amount', status='partial' where sales_id='$sales_id'") or die(mysqli_error($con));
				mysqli_query($con,"UPDATE customer SET balance=balance+'$toro' where cust_id='$cid'") or die(mysqli_error($con)); 
				
				$name=$row111['cust_last'].", ".$row111['cust_first'];
		$remarks="added a payment of $amount for $name";  
	
		mysqli_query($con,"INSERT INTO history_log(user_id,action,date) VALUES('$id','$remarks','$date')")or die(mysqli_error($con));
		
	
		echo "<script type='text/javascript'>alert('Successfully added payment!');</script>";
		echo "<script>document.location='receipt_credit.php?cid=$cid&sid=$sid'</script>";
    			}
    						
   
	
?>