/*
 * Arrebol Consultancy copyright.
 * 
 * This code belongs to Arrebol Consultancy
 * its use, redistribution or modification are prohibited 
 * without written authorization from Arrebol Consultancy.
 */
package com.arrebol.apc.ws.administration;

import com.arrebol.apc.controller.mobile.controller.admin.ClosingDayController;
import com.arrebol.apc.controller.mobile.controller.admin.StableSmallBoxController;
import com.arrebol.apc.controller.mobile.controller.bitacora.BitacoraController;
import com.arrebol.apc.controller.mobile.json.loan.GenericAPCResponserJaxb;
import java.io.Serializable;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 *
 * @author Carlos Janitzio Zavala Lopez <janitzio.zavala@arrebol.com.mx>
 */
@Path("closing-days")
public class ClosingDayWS implements Serializable {

    @PUT
    @Path("delete-closing-day")
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteClosingDay(@FormParam("id") String idClosingDay,
            @FormParam("updatedBy") String updatedBy,
            @FormParam("office") String office) {
        Response response;
        ClosingDayController controller = new ClosingDayController();
        StableSmallBoxController smallBoxController = new StableSmallBoxController();
        GenericAPCResponserJaxb apcResponse = new GenericAPCResponserJaxb();
        try {
            if (!smallBoxController.existStableSmallBoxByCreatedOn(office)) {
                if (controller.disableClosingDayById(idClosingDay, updatedBy)) {
                    BitacoraController bitacoraController = new BitacoraController();
                    
                    if (bitacoraController.insertBitacoraRecord("Eliminar corte del día", "APK", "Se borro el corte de día por administrador desde el celular", updatedBy, office, "Borrado desde la aplicación móvil")) {
                        apcResponse.setSuccessful(Boolean.TRUE);
                        apcResponse.setEnabledSubmit(Boolean.FALSE);
                        apcResponse.setMessage("Se borro el corte del día.");
                        response = Response.ok().entity(apcResponse).build();
                    } else {
                        apcResponse.setSuccessful(Boolean.FALSE);
                        apcResponse.setEnabledSubmit(Boolean.FALSE);
                        apcResponse.setMessage("Error al guardar la bitacora. Contacte a su supervisor.");
                        response = Response.ok().entity(apcResponse).build();
                    }
                } else {
                    apcResponse.setSuccessful(Boolean.FALSE);
                    apcResponse.setEnabledSubmit(Boolean.FALSE);
                    apcResponse.setMessage("No se borro el corte del día.");
                    response = Response.ok().entity(apcResponse).build();
                }
            } else {
                apcResponse.setSuccessful(Boolean.FALSE);
                apcResponse.setEnabledSubmit(Boolean.FALSE);
                apcResponse.setMessage("Ya existe un cuadre de caja chica del día, el corte del día no puede ser borrado.");
                response = Response.ok().entity(apcResponse).build();
            }
        } catch (Exception e) {
            apcResponse.setSuccessful(Boolean.FALSE);
            apcResponse.setEnabledSubmit(Boolean.FALSE);
            apcResponse.setMessage("Hubo un error al intentar borrar el corte del día. Notifique a su supervisor.");
            response = Response.ok().entity(apcResponse).build();
            logger.error("deleteClosingDay", e);

        }
        return response;
    }

    @GET
    @Path("find-closing-day")
    @Produces(MediaType.APPLICATION_JSON)
    public Response findClosingDayByEmployeeAndOffice(@QueryParam("employee") String idEmployee,
            @QueryParam("office") String idOffice) {
        Response response;
        ClosingDayController controller = new ClosingDayController();
        StableSmallBoxController smallBoxController = new StableSmallBoxController();
        GenericAPCResponserJaxb apcResponse = new GenericAPCResponserJaxb();
        try {
            if (!smallBoxController.existStableSmallBoxByCreatedOn(idOffice)) {
                String id = controller.findIdClosingDayByEmployee(idEmployee, idOffice);
                if (null != id && id.length() == 36) {
                    apcResponse.setSuccessful(Boolean.TRUE);
                    apcResponse.setEnabledSubmit(Boolean.TRUE);
                    apcResponse.setIdClosingDay(id);
                    apcResponse.setMessage("Corte del día encontrado.");
                    response = Response.ok(apcResponse).build();
                } else {
                    apcResponse.setSuccessful(Boolean.FALSE);
                    apcResponse.setEnabledSubmit(Boolean.FALSE);
                    apcResponse.setMessage("Corte del día del usuario seleccionado no existente.");
                    response = Response.ok(apcResponse).build();
                }
            } else {
                apcResponse.setSuccessful(Boolean.FALSE);
                apcResponse.setEnabledSubmit(Boolean.FALSE);
                apcResponse.setMessage("No se pude seleccionar el corte del día porque ya exite un corte de caja chica.");
                response = Response.ok(apcResponse).build();
            }
        } catch (Exception e) {
            apcResponse.setSuccessful(Boolean.FALSE);
            apcResponse.setEnabledSubmit(Boolean.FALSE);
            apcResponse.setMessage("Hubo un error obteniendo el corte del día. Contacte a su administrador.");
            response = Response.status(Response.Status.NOT_ACCEPTABLE).build();
            logger.error("updatePaidFeesStatusInLoanDetailIds", e);

        }
        return response;
    }

    final Logger logger = LogManager.getLogger(getClass());
}
