<?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);
        $user = $inputdata['user'];
        $id = $inputdata['id'];
        
        $nbr="";
        
        $verif=$db->prepare('SELECT COUNT(*) AS nbr FROM point_journalier WHERE user=? AND fermeture=?');
        $verif->execute(array($user,0));
        while($verifs=$verif->fetch()){
            $nbr=$verifs['nbr'];
        }
        
        if ($nbr == 0) {
            
            // la caisse n'est pas ouverte, il faut l'ouvrir d'abord
            
            echo(json_encode(['data' => -1]));
           
        

         }else {
            // la caisse est ouverte

            $pub=$db->prepare('UPDATE point_journalier SET fermeture=?, dat_ferme=? WHERE ouverture=? AND id=? AND user=?');
            $fermeture=1;
            $ouverture=1;
            $pub->execute(array($fermeture, date('Y-m-d H:i:s'), $ouverture, $id, $user));
            if ($pub) {
                # code...
                echo(json_encode(['data' => 1]));
            }else {
                echo(json_encode(['data' => 0]));
            }

         }
         

        
       