<?php
session_start();
    header('Access-Control-Allow-Origin:*');
    header('Content-Type; application/json; charset=utf-8');
    header('Access-control-Allow-Method: POST');
    header('Access-control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-request-With');
    include('db.php');
        $inputdata = json_decode(file_get_contents("php://input"), true);

        
        $prix_boisson = $inputdata['prix_boisson'];
        $prix_repas = $inputdata['prix_repas'];
        $qte = $inputdata['qte'];
        $num_cmd = $inputdata['num_cmd'];
      
        
        if (!empty($prix_boisson)) {
            $req1=$db->prepare('SELECT prix_boisson.*, boisson.nom FROM prix_boisson, boisson WHERE boisson.id=prix_boisson.boisson AND prix_boisson.id=?');
            $req1->execute(array($prix_boisson));
            while ($req1s=$req1->fetch()) {
                $prix=$req1s['prix_vente'];
                $qte_stock=$req1s['qte_stock'];
                $benefice=$req1s['benefice'];
                $total=$prix*$qte;
                $tbenefice=$benefice*$qte;
                $des=$req1s['nom'].' '.$req1s['designation'];
                
            }
            if ($qte_stock < $qte) {
                // La quantité en stock est inférieure à celle voulue
                echo (json_encode(['response' => -1]));
            } else {
                $insert=$db->prepare('INSERT INTO table_invoice_details(num_cmd,prix_boisson, designation,qte,price, total,tbenefice,order_date,order_time) VALUES(?,?,?,?,?,?,?,?,?)');
                $insert->execute(array($num_cmd, $prix_boisson, $des, $qte, $prix, $total, $tbenefice, date('Y-m-d'), date('H:i:s')));

                $red=$db->prepare('UPDATE prix_boisson SET qte_stock=qte_stock-? WHERE id=?');
                $red->execute(array($qte, $prix_boisson));

                if ($insert && $red) {
                    //success
                    echo (json_encode(['total' => $total]));
                } else {
                    // erreur
                    echo (json_encode(['total' => 0]));
                }
                
            }


        } elseif (!empty($prix_repas)) {
            $req2=$db->prepare('SELECT prix_repas.*, repas.nom FROM prix_repas, repas WHERE repas.id=prix_repas.repas AND prix_repas.id=?');
            $req2->execute(array($prix_repas));
            while ($req2s=$req2->fetch()) {
                $prix2=$req2s['prix'];
                $total=$prix2*$qte;
                $des2=$req2s['nom'].' '.$req2s['designation'];
            }
            $insert2=$db->prepare('INSERT INTO table_invoice_details(num_cmd,prix_repas, designation,qte,price, total,order_date,order_time) VALUES(?,?,?,?,?,?,?,?)');
            $insert2->execute(array($num_cmd, $prix_repas, $des2, $qte, $prix2, $total, date('Y-m-d'), date('H:i:s')));

            $sep=$db->prepare('SELECT * FROM fiche_repas WHERE prix_repas=?');
            $sep->execute(array($prix_repas));
            while ($seps=$sep->fetch()) {
                $quantite=$seps['qte']*$qte;
                $red2=$db->prepare('UPDATE matiere_premiere SET qte_stock=qte_stock-? WHERE id=?');
                $red2->execute(array($quantite, $seps['matiere_premiere']));
            }
            if ($insert2 && $red2) {
                    //success
                    echo (json_encode(['total' => $total]));
                } else {
                    // erreur
                    echo (json_encode(['total' => 0]));
                }


        }