
<?php
//ini_set('display_errors', 1);
//ini_set('display_startup_errors', 1);
//error_reporting(E_ALL);
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_repas = $inputdata['prix_repas'];
        $accompagnement = $inputdata['accompagnement'];
        $qte = $inputdata['qte'];
        $num_cmd = $inputdata['num_cmd'];
        $user = $inputdata['user'];
        
        $pub=$db->prepare('SELECT * FROM point_journalier WHERE user=? AND ouverture=? AND fermeture=? ORDER BY id DESC LIMIT 1');
        $pub->execute(array($user, 1, 0));
        while($pubs=$pub->fetch()) {
            $id_point_journalier = $pubs['identifiant'];
        }
      
        


            $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'];
                $benefice=$req2s['benefice'];
                $total=$prix2*$qte;
                $des2=$req2s['nom'].' '.$req2s['designation'];
                $tbenefice=$benefice*$qte;
                $qte_stock=$req2s['qte_stock'];
            }
            
            $req12=$db->prepare('SELECT prix_repas.*, repas.nom FROM prix_repas, repas WHERE repas.id=prix_repas.repas AND prix_repas.id=?');
            $req12->execute(array($accompagnement));
            while ($req12s=$req12->fetch()) {
                
                $des12=$req12s['nom'].' '.$req12s['designation'];
            }
            
            $designation="$des2 ($des12)";
            
            
            if($qte_stock < $qte) {
                // La quantité en stock est inférieure à celle voulue
                echo (json_encode(['response' => -1]));
            } else {
                $insert2=$db->prepare('INSERT INTO table_invoice_details(num_cmd,prix_repas, designation,qte,price, total,order_date,order_time,id_point_journalier,tbenefice) VALUES(?,?,?,?,?,?,?,?,?,?)');
            $insert2->execute(array($num_cmd, $prix_repas, $designation, $qte, $prix2, $total, date('Y-m-d'), date('H:i:s'),$id_point_journalier,$tbenefice));

            /*$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']));
            }*/
            $red=$db->prepare('UPDATE prix_repas SET qte_stock=qte_stock-? WHERE id=?');
            $red->execute(array($qte, $prix_repas));
            
            $ress=$db->prepare('UPDATE prix_repas SET qte_stock=qte_stock-? WHERE id=?');
            $ress->execute(array($qte, $accompagnement));
            
            if ($insert2 && $red && $ress) {
                    //success
                    echo (json_encode(['total' => $total]));
                } else {
                    // erreur
                    echo (json_encode(['total' => 0]));
                }
            }


        