<?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);
        $identifiant = $inputdata['identifiant'];
        $user = $inputdata['user'];
        $ouverture=1;
        
        $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
            $pub=$db->prepare('INSERT INTO point_journalier (ouverture, identifiant, user) VALUES(?,?,?)');
            $pub->execute(array($ouverture, $identifiant, $user));
            if ($pub) {
                # code...
                echo(json_encode(['data' => 1]));
            }else {
                echo(json_encode(['data' => 0]));
            }
        

         }else {
            // la caisse est ouverte, il faut alors la fermer d'abord

            echo (json_encode(['data' => -1]));

         }
        
        
        