<?php
// cms/api/economicact/entry

    header("Content-Type:application/json");
    include("../../../settings/dbconn.php");
    include("../../../settings/utils.php");
    
    // parametros obligatorios
    $parmsob = array("sessionid",'id'); 
    if (!parametrosValidos($_GET, $parmsob)){
        badEnd("400", array("msg"=>"Parametros obligatorios " . implode(", ", $parmsob)));
    }
    
    //Declaracion de variables
    $out = new stdClass();
    $sessionid = mysqli_real_escape_string($db, $_GET["sessionid"]);
    $id = mysqli_real_escape_string($db, $_GET['id']);
    $dscEsp = "";
    $dscEng = "";
    
    //Validamos la session
    isSessionValidCMS($db, $sessionid);
    
    if((isset($_GET['dsc']) && $_GET['dsc'] != '')){
        $dscEsp = mysqli_real_escape_string($db, $_GET['dsc']);
    }
    
    if(isset($_GET['dsc_eng']) && $_GET['dsc_eng'] != ""){
        $dscEng = mysqli_real_escape_string($db, $_GET['dsc_eng']);
    }
    
    if($dscEsp == "" && $dscEng == ""){
        badEnd("500",array("msg"=>"Debe enviar dsc o dsc_eng"));
    }
    
    // Si el id es 0, creamos el registro, si no actualizamos
    if($id == 0){
        
        //Validamos que el usuario tenga privilegios
        if(!tienePrivilegio($db, $sessionid, 66))
            badEnd("403", array("msg"=>"No tiene Privilegios"));
            
        // Validamos que no se inserte la misma actividad'
        $sql = "SELECT COUNT(id) AS qty ".
        "       FROM economicactivities WHERE ".($dscEsp != "" ? "dsc = '".$dscEsp."'" : "").
        "       ".($dscEng != "" ? ($dscEsp != "" ? " AND dsc_eng = '".$dscEng."'" : "dsc_eng = '".$dscEng."'"): "")." ";
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
            
        $row = $rs->fetch_assoc();
        //Si la cantidad es igual a 0, no existe
        if($row['qty'] > 0){
            badEnd('402',array('msg'=>'Registro duplicado'));
        }
            
        // Insertamos la ocupacion
        $sql = "INSERT INTO economicactivities(".($dscEsp != "" ? "dsc" : "")." ".($dscEng != "" ? ($dscEsp != "" ? ",dsc_eng" : "dsc_eng") : "").") ".
        "       VALUES(".($dscEsp != "" ? "'".$dscEsp."'" : "")." ".($dscEng != "" ? ($dscEsp != "" ? ",'".$dscEng."'" : "'".$dscEng."'") : "").")";
        if (!$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
            
        $out->id = (int)$db->insert_id;
        
        //auditoria
        setAudit($db, "ACT. ECONÓMICAS", $sessionid, "CMS", "Se agregó la actividad económica ".($dscEsp != "" ? $dscEsp : $dscEng));
        
    }else{
        
        //Validamos que el usuario tenga privilegios
        if(!tienePrivilegio($db, $sessionid, 67))
            badEnd("403", array("msg"=>"No tiene Privilegios"));
        
        // Validamos que no se inserte la misma actividad'
        $sql = "SELECT COUNT(id) AS qty ".
        "       FROM economicactivities WHERE ".($dscEsp != "" ? "dsc = '".$dscEsp."'" : "").
        "       ".($dscEng != "" ? ($dscEsp != "" ? " AND dsc_eng = '".$dscEng."'" : "dsc_eng = '".$dscEng."'"): "")." ";
        "       AND id != ".$id;
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
        
        $row = $rs->fetch_assoc();
        //Si la cantidad es igual a 0, no existe
        if($row['qty'] > 0){
            badEnd('402',array('msg'=>'Registro duplicado'));
        }
            
        // Consultamos que el registro exista
        $sql = "SELECT COUNT(id) AS qty ".
        "       FROM economicactivities ".
        "       WHERE id = ".$id;
        if (!$rs=$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
            
        $row = $rs->fetch_assoc();
        //Si la cantidad es igual a 0, no existe
        if($row['qty'] == 0){
            badEnd('204',array('msg'=>'No se encuentra el registro'));
        }
        
        // Actualizamos el registro
        $sql = "UPDATE economicactivities SET ".
        "       ".($dscEsp != "" ? "dsc = '".$dscEsp."'" : "")." ".
        "       ".($dscEng != "" ? ($dscEsp != "" ? ",dsc_eng = '".$dscEng."'" : "dsc_eng = '".$dscEng."'"): "")." ".
        "       WHERE id = ".$id;
        if (!$db->query($sql))
            badEnd("500", array("msg"=>determinateDBError($db)));
        
        $out->id = (int)$id;
        
        //auditoria
        setAudit($db, "ACT. ECONÓMICAS", $sessionid, "CMS", "Se actualizó la actividad económica ".($dscEsp != "" ? $dscEsp : $dscEng));
    }
    
    header("HTTP/1.1 200");
    echo (json_encode($out));
    die();
?>