<?php
include "../database/database.php";
include "../database/data.php";


 
    $barcode = '';
    $product_name = 'Cola';
 

    $tra_id_data = new DateTime(); 
    $rn = rand(0, 1000);
    $tra_id = $tra_id_data->format('YmdHis').''.$rn; 

    if ($barcode != '' && $product_name != ''){ 

        echo "<script>
                alert('barcode မထည့်ရသေးပါ');
                window.location.href = 'product_trans.php';
            </script>";

    }else{ 

        $type = 'ဖာ';
        $trans_amount = '1';
        $shop_name = 'ime_app';
        $voucher = '10001';
        $date = '2024-11-08';
        $b_id = '572ea862344010c7b99ac40d93219c811731083084';
    
        $sql = "SELECT * FROM ".$cargo." WHERE browser_id='$b_id'";
        $result = $connection->query($sql);
        if ($result->num_rows > 0) {
            $row = $result->fetch_assoc(); // Fetch the row here
            $cargo_data = $row["cargo_name"];
            echo $cargo_data;


            if ($barcode != '') {
                $sql = "SELECT * FROM ".$cargo_data." WHERE (barcode='$barcode' OR barcode_1='$barcode') AND delete_d='0'";
            } else {
                $sql = "SELECT * FROM ".$cargo_data." WHERE product_name='$product_name' AND delete_d='0'";
            }
            
            // Execute the new query to get the result for the product
            $new_result = $connection->query($sql);
            if ($new_result && $new_result->num_rows > 0) {
                while ($row = $new_result->fetch_assoc()) {
                    if ($barcode == '') {
                        $barcode = $row['barcode']; // If barcode is empty, fill it from DB
                    }
                    $product_name = $row['product_name'];
                    echo $product_name;
                    

                    $package_exist = $row['package_exist'];
                   
                    $unit_exist = $row['unit_exist'];
                    $pieces_exist = $row['pieces_exist'];
                
                    if($type == "ဖာ"){ 

                        if($package_exist >= $trans_amount){ 

                            $sql = "INSERT INTO ".$cargo_data."_trans_data (barcode, product_name,shop_name, type, qty, voucher,tra_id, date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','$date','0')";
                            if (mysqli_query($connection, $sql)) {

                            }

                            $sql = "INSERT INTO ".$cargo_data."_trans (barcode, product_name,shop_name, type, qty, voucher,tra_id,accept, date,receive_date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','none', '$date','$date','0')";
                            if (mysqli_query($connection, $sql)) {
                                // Insert query executed successfully
                                header('Location: product_trans.php');
                            } else {
                                // Error in insert query
                                echo "Error: " . $sql . " " . mysqli_error($connection);
                            }
                        }else { 
                            echo "<script>
                                alert('လက်ကျန်ဖာ ပမာဏထက်များနေပါသည်');
                                window.location.href = 'product_trans.php';
                            </script>";
                        }

                    } else if($type == "ကဒ်"){ 
                        if($unit_exist >= $trans_amount){ 

                            $sql = "INSERT INTO ".$cargo_data."_trans_data (barcode, product_name,shop_name, type, qty, voucher,tra_id, date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','$date','0')";
                            if (mysqli_query($connection, $sql)) {

                            }

                            $sql = "INSERT INTO ".$cargo_data."_trans (barcode, product_name,shop_name, type, qty, voucher,tra_id,accept, date,receive_date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','none', '$date','$date','0')";
                            if (mysqli_query($connection, $sql)) {
                                // Insert query executed successfully
                                header('Location: product_trans.php');
                            } else {
                                // Error in insert query
                                echo "Error: " . $sql . " " . mysqli_error($connection);
                            }
                        }else { 
                            echo "<script>
                                alert('လက်ကျန်ကဒ် ပမာဏထက်များနေပါသည်');
                                window.location.href = 'product_trans.php';
                            </script>";
                        }
                    } else if($type == "ဗူး/ခု"){ 
                        if($pieces_exist >= $trans_amount){ 

                            $sql = "INSERT INTO ".$cargo_data."_trans_data (barcode, product_name,shop_name, type, qty, voucher,tra_id, date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','$date','0')";
                            if (mysqli_query($connection, $sql)) {
                         

                            }

                            $sql = "INSERT INTO ".$cargo_data."_trans (barcode, product_name,shop_name, type, qty, voucher,tra_id,accept, date,delete_d)
                            VALUES ('$barcode', '$product_name','$shop_name', '$type', '$trans_amount', '$voucher','$tra_id','none', '$date','0')";
                            if (mysqli_query($connection, $sql)) {
                                // Insert query executed successfully
                                header('Location: product_trans.php');
                            } else {
                                // Error in insert query
                                echo "Error: " . $sql . " " . mysqli_error($connection);
                            }
                        }else { 
                            
                        }
                    }

                }
            } 

        }
        
            
        }

?>