<?php 
require_once"shikanisha.kts.php";
session_start();
if(mysqli_connect_error()){
  echo"<script>
  alert('cannot connect to database');
  window.location.href='../login.php';
  </script>";
  
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
  
if(isset($_POST['transact'])){
  
  
    $pay_method=($_POST['payment_method']);
    $paid_amount=($_POST['paid_amount']);
    $total=($_POST['total']);
    $telephone=($_POST['phone_number']);
    $seller=$_SESSION['user'];
    $date=date("Y-m-d");
  
  
  $query1="insert into transaction_receipts(payment_method,amount_received,total,phone_number,seller_name,seller_date)
  VALUES('$pay_method','$paid_amount','$total', '$telephone','$seller','$date')";
  if(mysqli_query($conn, $query1))
  {
    $receipt_id=mysqli_insert_id($conn);
    $query2="insert into sales(product_name,selling_price,quantity,product_id,subtotal,trans_id)
    values(?,?,?,?,?,?)";
    $stmt=mysqli_prepare($conn,$query2);
    if($stmt){
      mysqli_stmt_bind_param($stmt,"siiiii",$name,$price,$quantity,$id,$subtotal,$receipt_id);
     
      foreach($_SESSION['cart'] as $key => $value){
       
        $name =$value['name'];
        $price = $value['price'];
        $quantity= $value['qty'];
        $id=$value['id'];
        $subtotal=  ($value['price']*$value['qty']);
        mysqli_stmt_execute($stmt);
      }
      
       $query4 = "update products set shop_quantity = shop_quantity - ? where product_id = ?";
        $stmt4 = mysqli_prepare($conn, $query4);
        
        if ($stmt4) {
          
          foreach ($_SESSION['cart'] as $key => $value) {
            
            $quantity = $value['qty'];
            $id = $value['id'];
            mysqli_stmt_bind_param($stmt4, "ii", $quantity, $id);
            mysqli_stmt_execute($stmt4);
            
            // Check for errors
            if (mysqli_stmt_error($stmt4)) {
              echo "Error: " . mysqli_stmt_error($stmt4);
            }
            
          }
         
          echo "<script>alert('Items sent to shop successfully'); window.location.href='../print.php';</script>";
    
        } else {
          echo "Error: " . mysqli_error($conn);
        }
   
      
      echo"<script>
      
      window.location.href='../pos.php'; 
      </script>";
   }
  }
    else{
      echo"<script>
      alert('SQL Error`);
      window.location.href='../pos.php';
      </script>";
  
  
}
}
}


?>

<?php
if(isset($_POST['transact'])){

if(isset($_SESSION['transaction'])){
    
    $session_array_id = array_column($_SESSION['transaction'],'id');
    
    
    if(!in_array($_GET['id'], $session_array_id)){
        
     $session_array = array(    
    'phone'  => $_POST['phone_number'],
    'pay_method' => $_POST['payment_method'],
    'amount_paid'   => $_POST['paid_amount']   
        
        );
        
        $_SESSION['transaction'][] = $session_array;
        
    }
    
}else{
    
    
    $session_array = array(
    
    
    'phone'  => $_POST['phone_number'],
    'pay_method' => $_POST['payment_method'],
    'amount_paid'   => $_POST['paid_amount']   
    
    );
    
    $_SESSION['transaction'][]  = $session_array;
    
}
    
}


?>
