<?php
session_start();
$total=$_GET['total'];
$staffcode=$_GET['staffcode'];
$amount_paidt=$_POST['amount_paid'];
if ($amount_paidt>$total) {
    $amount_paid=$total;
  $change=$amount_paidt-$total;
}else{
     $amount_paid=$amount_paidt;
    $change=0;
}
include '../includes/connection.php';
$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
$customername=$_POST['name'];
$customercontact=$_POST['contact'];

//date format function
function udate($format = 'u', $utimestamp = null) {
        if (is_null($utimestamp))
            $utimestamp = microtime(true);

        $timestamp = floor($utimestamp);
        $milliseconds = round(($utimestamp - $timestamp) * 1000000);

        return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
    }

$date=udate('Y-m-d H:i:s.u');
// Create connection


$sqll = "SELECT sum(quantity)as orders,saletype,(SELECT max(id)as lastid from sales where branch_id='$branch' && business_id='$business') as lastid from temp where branch_id='$branch' && user='$id_user'";
$result = mysqli_query($conn, $sqll);
if ($customername!='') {
    $ck=mysqli_query($con,"SELECT * from customers where name='$customername' && branch_id='$branch'");
                                            if (mysqli_num_rows($ck)>0) {
                                              
                                            }else{


                                            
                                              
                                              $sql2 = mysqli_query($con,"INSERT INTO customers(name, tel,business_id,branch_id,added_by)
                                        VALUES ('$customername', '$customercontact','$business','$branch','$id_user')");
                                          }
                                            
                                        
}
if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $saletype=$row['saletype'];
        $orders=$row['orders'];
        $max_id=$row['lastid']+1;
        $receiptdate= date('Y');
        $receiptmonth= date('m');

        $receipt_number=$branch."".$orders."".$receiptdate."".$receiptmonth."".$max_id;
    }
} else {
    echo "0 results";
}
//mysqli_close($conn);
?>



<!--insert data-->
<?php
if (isset($_POST['sale_date'])) {
  // sale date
$sale_date=$_POST['sale_date'];
  $sql = "INSERT INTO sales (saletype,quatity, total_price, paid,changebal,staff, receipt_no,branch_id,business_id, customer_name, contact,date_time)
VALUES ('$saletype','$orders', '$total', '$amount_paid','$change','$staffcode','$receipt_number','$branch','$business', '$customername','$customercontact','$sale_date')";
$balancee=$total-$amount_paid;


if (mysqli_query($conn, $sql)) {
  $sqlmaxid=mysqli_fetch_array(mysqli_query($conn,"SELECT max(id)as lastid from sales where branch_id='$branch' && business_id='$business'"));
$maximum_id=$sqlmaxid['lastid'];

$sql2="INSERT INTO payment_logs(order_id, amount_paid, balance, user_id, date_time,branch_id,business_id,category)
VALUES('$maximum_id','$amount_paid','$balancee','$id_user', '$sale_date','$branch','$business','sale')";
  if (mysqli_query($conn, $sql2)) {
     
  $activity="Sold ".$orders." items at Ushs.".$total;
  include '../includes/store_logs.php';
  ?>



    <meta http-equiv="refresh" content="0;URL='process_orderdetails.php?staffcode=<?php echo $staffcode; ?>&&id=<?php echo $maximum_id; ?>&&sale_date=<?php echo $sale_date; ?>'" />
    <?php
    } else {
    echo "Error: " . $sql2 . "<br>" . mysqli_error($conn);
}
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

}else{
  // no sale date

$sql = "INSERT INTO sales (saletype,quatity, total_price, paid,changebal,staff, receipt_no,branch_id,business_id, customer_name, contact)
VALUES ('$saletype','$orders', '$total', '$amount_paid','$change','$staffcode','$receipt_number','$branch','$business', '$customername','$customercontact')";
$balancee=$total-$amount_paid;


if (mysqli_query($conn, $sql)) {
  $sqlmaxid=mysqli_fetch_array(mysqli_query($conn,"SELECT max(id)as lastid from sales where branch_id='$branch' && business_id='$business'"));
$maximum_id=$sqlmaxid['lastid'];

$sql2="INSERT INTO payment_logs(order_id, amount_paid, balance, user_id, date_time,branch_id,business_id,category)
VALUES('$maximum_id','$amount_paid','$balancee','$id_user', '$date','$branch','$business','sale')";
  if (mysqli_query($conn, $sql2)) {
     
  $activity="Sold ".$orders." items at Ushs.".$total;
  include '../includes/store_logs.php';
  ?>



    <meta http-equiv="refresh" content="0;URL='process_orderdetails.php?staffcode=<?php echo $staffcode; ?>&&id=<?php echo $maximum_id; ?>'" />
    <?php
    } else {
    echo "Error: " . $sql2 . "<br>" . mysqli_error($conn);
}
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}



}



mysqli_close($conn);
?>
