/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.arrebol.taxiservicios.beans.stocktaking.service.order;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.stocktaking.KardexOutStocktakingController;
import com.arrebol.taxiservicios.model.catalog.Brand;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexOutStocktakingView;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.primefaces.event.ReorderEvent;
import org.primefaces.event.RowEditEvent;

/**
 *
 * @author Oscar
 */
@Named
@ViewScoped
public class KardexOutBean extends TaxiGenericBean implements Serializable, Datatable {

    public void fillDataTableBetweenDates() {
        try {
            if (getGenericStartDate().after(getGenericEndDate())) {
                showMessage(FacesMessage.SEVERITY_ERROR, getBundlePropertyFile().getString("generic.start.date"), getBundlePropertyFile().getString("generic.end.date.error"));
            } else {
                if(getKardex() != null)
                    getKardex().clear();
                
                if(selectedBrandId.equalsIgnoreCase("N/A") || selectedBrandId.equalsIgnoreCase("AllBrands"))
                    setKardex(getController().fillDatatable(getGenericStartDate(), getGenericEndDate()));
                else
                    setKardex(getController().fillDatatable(getGenericStartDate(), getGenericEndDate(), selectedBrandId));
                
                calcularTotales();
            }
        } catch (Exception e) {
            logger.error("", e);
        }
    }
    
    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void onRowReorder(ReorderEvent event) {
        showMessage(FacesMessage.SEVERITY_INFO, "Registro Movido", "De columna: " + (event.getFromIndex() + 1) + " a columna: " + (event.getToIndex() + 1));
    }

    @Override
    public void addRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
    
    public void calcularTotales(){
        
       totalMayoreo = 0F;
       totalMayoreo2 = 0F;
       utilidad = 0F;
       totalCobrados = 0F;
       totalFinanciados = 0F;
       totalSinAccion = 0F;
       for(KardexOutStocktakingView temp : kardex)
       {
           
           if((temp.getCobrado().equals("Cobrado") && temp.getFinanciado().equals("No financiado")) || (temp.getCobrado().equals("Cobrado") && temp.getFinanciado().equals("Financiado"))){
               totalCobrados += temp.getTotal();
           }
           if((temp.getCobrado().equals("No cobrado") && temp.getFinanciado().equals("Financiado"))){
               totalFinanciados += temp.getTotal();
           }
           
           if((temp.getCobrado().equals("No cobrado") && temp.getFinanciado().equals("No financiado"))){
               totalSinAccion += temp.getTotal();
           }
           
           
           
           totalMayoreo += temp.getTotal();
           totalMayoreo2 += temp.getMayoreo();
       }
       utilidad = totalMayoreo - totalMayoreo2;
    }
    
    final Logger logger = LogManager.getLogger(KardexOutBean.class);
    KardexOutStocktakingController controller;
    private Date startDate;
    private Date endDate;
    private List<KardexOutStocktakingView> kardex;
    private float totalMayoreo;
    private float totalMayoreo2;
    private float utilidad;
    private float totalCobrados;
    private float totalFinanciados;
    private float totalSinAccion;
    private String selectedBrandId;
    
    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setGenericUpDates(true);
            setController(new KardexOutStocktakingController());
            setKardex(getController().fillDatatable( getGenericStartDate(),
                    getGenericEndDate()));
            
            calcularTotales();

            logger.info("KardexOutStocktakingView loaded");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public float getTotalMayoreo2() {
        return totalMayoreo2;
    }

    public void setTotalMayoreo2(float totalMayoreo2) {
        this.totalMayoreo2 = totalMayoreo2;
    }

    public float getUtilidad() {
        return utilidad;
    }

    public void setUtilidad(float utilidad) {
        this.utilidad = utilidad;
    }

    public List<Brand> getBrandes() {
        return getController().fillProductBrandComboBox();
    }
    
    public String getSelectedBrandId() {
        return selectedBrandId;
    }

    public void setSelectedBrandId(String selectedBrandId) {
        this.selectedBrandId = selectedBrandId;
    }

    public float getTotalMayoreo() {
        return totalMayoreo;
    }

    public void setTotalMayoreo(float totalMayoreo) {
        this.totalMayoreo = totalMayoreo;
    }
    
    public KardexOutStocktakingController getController() {
        return controller;
    }

    public void setController(KardexOutStocktakingController controller) {
        this.controller = controller;
    }

    public Date getStartDate() {
        return startDate;
    }

    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }

    public Date getEndDate() {
        return endDate;
    }

    public void setEndDate(Date endDate) {
        this.endDate = endDate;
    }

    public float getTotalCobrados() {
        return totalCobrados;
    }

    public void setTotalCobrados(float totalCobrados) {
        this.totalCobrados = totalCobrados;
    }

    public float getTotalFinanciados() {
        return totalFinanciados;
    }

    public void setTotalFinanciados(float totalFinanciados) {
        this.totalFinanciados = totalFinanciados;
    }

    public float getTotalSinAccion() {
        return totalSinAccion;
    }

    public void setTotalSinAccion(float totalSinAccion) {
        this.totalSinAccion = totalSinAccion;
    }

    
    public List<KardexOutStocktakingView> getKardex() {
        return kardex;
    }

    public void setKardex(List<KardexOutStocktakingView> kardex) {
        this.kardex = kardex;
    }
    
}
