<?php

/*
 * ROMAN DIGITAL - Autor: Américo Patetta
 */

set_include_path("../../");
include_once("server/class/Delivery.php");

$jsonParams = file_get_contents('php://input');
$paramObj = json_decode($jsonParams);

//if(isset($paramObj->idDelivery) && isset($paramObj->idLocal) && isset($paramObj->tarjeta) && isset($paramObj->token) && $paramObj->token == TOKEN_BRODEVO){
if (isset($paramObj->idDelivery) && isset($paramObj->idLocal) && isset($paramObj->token) && $paramObj->token == TOKEN_BRODEVO) {
    $idDelivery = $paramObj->idDelivery;
    $idLocal = $paramObj->idLocal;
    $numeroTarjeta = (isset($paramObj->tarjeta))?$paramObj->tarjeta:1;
    $strict = (isset($paramObj->strict)&&$paramObj->strict=="1")?true:false;

    $delivery = new Delivery($idDelivery);
    $success = ($delivery->enroll($idLocal,$strict,$numeroTarjeta));
    switch ($success) {
        case null:
            $ack['result'] = -2;        // Ya estaba enrolado
            break;
        case 0:
            $ack['result'] = -1;        // No se pudo enrolar 
            break;
        case 1:
            $ack['result'] = 0;         // Se enroló
            break;
    }
} else {
    $ack['result'] = -3;        // Faltan parámetros
}

header('Content-Type: application/json; charset=utf-8');
echo json_encode($ack);
