<?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);
        
        $num_cmd = $inputdata['num_cmd'];
        $serveur = $inputdata['serveur'];
        $tables = $inputdata['tables'];
        $remise = $inputdata['remise'];
        $user = $inputdata['user'];
        

            $rin=$db->prepare('SELECT SUM(total) AS tot FROM table_invoice_details WHERE num_cmd=?');
            $rin->execute(array($num_cmd));
            while ($rins=$rin->fetch()) {
                $bil=$rins['tot'];
            }
            $nap=$bil-$remise;
            
            $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'];
            }

            $finalise=$db->prepare('INSERT INTO tbl_invoice(num_cmd,serveur,tables,id_point_journalier,order_date,order_time,total,remise,nap) VALUES(?,?,?,?,?,?,?,?,?)');
            $finalise->execute(array($num_cmd, $serveur, $tables, $id_point_journalier, date('Y-m-d'), date('H:i:s'), $bil, $remise, $nap ));

            if ($finalise) {
                
                echo (json_encode(['response' => 1]));
            } else {
                echo (json_encode(['response' => 0]));
            }



      
        
