<?php 
$controller = 'Pos';
$action = '';
if(isset($_POST['action'])){
    $action = $_POST['action'];
    if (isset($_POST['id'])) {$id = $_POST['id'];}
}elseif(isset($_GET['action'])){
    $action = $_GET['action'];
    if (isset($_GET['id'])) {$id = $_GET['id'];}
}

include $_SERVER["DOCUMENT_ROOT"].'/app/include/bootstrap.php';
$venta = new VentaEntity();
$login = new Login();

if($action == '' && !$venta->cajaAbierta($login->getToken())){
    header("Location: Register.php?r");
}

if($action != 'ajax'){
    $form = new VentaForm();
    $_listFormasPago = $venta->getListaFormaPago();
    $_listCategorias = $form->getListaCategorias('Producto');
    $_listProductos = $form->getListaProductos();
}

switch($action){               
    case 'ajax':
        $ajaxPos = new PosAjax();
        $json = $ajaxPos->getResponse($_POST['request'],$_POST);
        
        echo json_encode($json);
        break;       
    
    default: 
        $venta->crearTablaDetallesForUser();       
        $venta->crearTablaCobrosForUser();
        
        $cobroRepoTemp = new CobroTempRepository();
        $cobroRepoTemp->truncate();
        
        #sr = specialRequisition; no elimino detalles cuando esta presente esta variable
        #Mando sr desde view SpecialRequisition.php
        if(!isset($_GET['sr'])){
            $_sr = false;
            $venta->clearDetalles();
        }else{
            $_sr = true;
        }        
        unset($_SESSION['pagoDevolucion']);

        $vista = 'Pos.php';
        include $root.'/View/TemplatePos.php';
}