<?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");
	$changeko=$amount - $balance;
	
mysqli_query($con,"UPDATE customer SET balance=balance-'$amount' where cust_id='$cid'") or die(mysqli_error($con)); 

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,partial1,mydate)
VALUES('$cid','$myid','$amount','0','0','$balance','$changeko','$date1','partial','1','$balance','partial','$amount','$date')")or die(mysqli_error($con));
	$sales_id=mysqli_insert_id($con);
	$_SESSION['sid']=$sales_id;	
	
	mysqli_query($con,"UPDATE sales SET partial3=partial3+'$amount' where sales_id='$sid'") or die(mysqli_error($con)); 
	$query1=mysqli_query($con,"SELECT * FROM payment ORDER BY payment_id DESC")or die(mysqli_error($con));
			$row1=mysqli_fetch_array($query1);
			$ornoko=$row1['or_no'];
	
mysqli_query($con,"INSERT INTO payment(cust_id,user_id,payment,payment_date,branch_id,payment_for,due,status,sales_id,or_no) 
	VALUES('$cid','$myid','$amount','$date','1','$date','$amount','cash','$sales_id','$ornoko')")or die(mysqli_error($con));
	
		$name="WALKIN";
		$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=$sales_id'</script>";
    		
				
    						
   
	
?>