<?php
ob_start();
if(!isset($_SESSION)){ session_start(); } 
include 'load.php'; 
if(!isset($_SESSION['update_grn'])) { $_SESSION['update_grn'] = array(); } 
if(isset($_POST['product']) and isset($_POST['quntity'])){
    

    $cart_item = array();
    $productid = $_POST['product'];
    $qty = $_POST['quntity'];
    $discount = $_POST['discount'];
    $price = $_POST['price'];
   
    $productinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid='$productid'"));
    $stockinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM tbl_stock WHERE prid='$productid'"));

    
        if(count($_SESSION['update_grn']) != 0){  
            $c=0;
            foreach ($_SESSION['update_grn'] as $key => $cart_item) {	
                if($cart_item['product_id'] == $productid){
                    $c=0;
                    //if($stockinfo['qty'] > $cart_item['quantity'] + $qty){
                        $_SESSION['update_grn'][$key]['quantity'] = $cart_item['quantity'] + $qty;
                        echo '<script> window.history.go(-1); </script>';
                        
                        exit;
                  //  }else{
                    //    echo '<script> window.history.go(-1); </script>';
                       
                    //    exit;
                   // }
                    
                }else{
                    $c=1;
                    
                }
            }
            if($c==1){
                array_push($cart_item,
                        $cart_item['product_id'] = $productid,
                        $cart_item['discount'] = $discount,
                        $cart_item['quantity'] = $qty, 
                        $cart_item['price'] = $price 
                        
                    );
                    array_push($_SESSION['update_grn'], $cart_item);
                   echo '<script> window.history.go(-1); </script>';
            }



        }else{
            array_push($cart_item,
                $cart_item['product_id'] = $productid,
                $cart_item['discount'] = $discount,
                $cart_item['quantity'] = $qty,
                $cart_item['price'] = $price
                
            );
            array_push($_SESSION['update_grn'], $cart_item);
            echo '<script> window.history.go(-1); </script>';
        }
    

}


?>