/*
 * 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.invoice;

import com.arrebol.taxiservicios.beans.Datatable;
import com.arrebol.taxiservicios.beans.TaxiGenericBean;
import com.arrebol.taxiservicios.controller.admin.ProviderController;
import com.arrebol.taxiservicios.controller.catalog.ProductController;
import com.arrebol.taxiservicios.controller.stocktaking.KardexInStocktakingController;
import com.arrebol.taxiservicios.controller.util.DateWrapper;
import com.arrebol.taxiservicios.model.catalog.Product;
import com.arrebol.taxiservicios.model.core.Person;
import com.arrebol.taxiservicios.model.enums.PersonType;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexInStocktakingView;
import java.io.Serializable;
import java.util.Calendar;
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 KardexInBean 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(providerId.equalsIgnoreCase("AllProvider") && productId.equalsIgnoreCase("AllProducts"))
                {
                   setKardex(getController().fillDatatable(getGenericStartDate(), getGenericEndDate())); 
                }
                
                if(!providerId.equalsIgnoreCase("AllProvider") && productId.equalsIgnoreCase("AllProducts"))
                {
                    setKardex(getController().fillDatatableByProvider(getGenericStartDate(), getGenericEndDate(), providerId)); 
                }
                if(providerId.equalsIgnoreCase("AllProvider") && !productId.equalsIgnoreCase("AllProducts"))
                {
                    setKardex(getController().fillDatatableByProduct(getGenericStartDate(), getGenericEndDate(), productId)); 
                }
                if(!providerId.equalsIgnoreCase("AllProvider") && !productId.equalsIgnoreCase("AllProducts"))
                {
                    setKardex(getController().fillDatatableByProductByProvider(getGenericStartDate(), getGenericEndDate(), providerId, productId)); 
                }
                
                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;
       totalMenudeo = 0F;
       totalUtilidad = 0F;
       for(KardexInStocktakingView temp : kardex)
       {
           totalMayoreo += (temp.getCantidad() * temp.getPrecioMayoreo());
           totalMenudeo += (temp.getCantidad() * temp.getPrecioMenudeo());
           totalUtilidad += temp.getUtility();
       }
    }
    
    final Logger logger = LogManager.getLogger(KardexInBean.class);
    KardexInStocktakingController controller;
    ProductController productController;
    ProviderController providerController;
    private Date startDate;
    private Date endDate;
    private List<KardexInStocktakingView> kardex;
    private float totalMayoreo;
    private float totalMenudeo;
    private float totalUtilidad;
    private String providerId;
    private String productId;
    private List<Person> provider;
    private List<Product> product;
    
    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            loadBundlePropertyFile();
            setGenericUpDates(true);
            setController(new KardexInStocktakingController());
            setProductController(new ProductController());
            setProviderController(new ProviderController());
            
            product = getProductController().fillProductDatatable();
            provider = getProviderController().getAllPersonsByPersonTye(PersonType.PROVIDER);

            setKardex(getController().fillDatatable( getGenericStartDate(),
                    getGenericEndDate()));
            
            calcularTotales();

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

    public List<Person> getProvider() {
        return provider;
    }

    public void setProvider(List<Person> provider) {
        this.provider = provider;
    }

    public List<Product> getProduct() {
        return product;
    }

    public void setProduct(List<Product> product) {
        this.product = product;
    }

    public String getProviderId() {
        return providerId;
    }

    public void setProviderId(String providerId) {
        this.providerId = providerId;
    }

    public String getProductId() {
        return productId;
    }

    public void setProductId(String productId) {
        this.productId = productId;
    }

    public ProductController getProductController() {
        return productController;
    }

    public void setProductController(ProductController productController) {
        this.productController = productController;
    }

    public ProviderController getProviderController() {
        return providerController;
    }

    public void setProviderController(ProviderController providerController) {
        this.providerController = providerController;
    }

    public float getTotalMayoreo() {
        return totalMayoreo;
    }

    public void setTotalMayoreo(float totalMayoreo) {
        this.totalMayoreo = totalMayoreo;
    }

    public float getTotalMenudeo() {
        return totalMenudeo;
    }

    public void setTotalMenudeo(float totalMenudeo) {
        this.totalMenudeo = totalMenudeo;
    }

    public float getTotalUtilidad() {
        return totalUtilidad;
    }

    public void setTotalUtilidad(float totalUtilidad) {
        this.totalUtilidad = totalUtilidad;
    }
    
    

    public KardexInStocktakingController getController() {
        return controller;
    }

    public void setController(KardexInStocktakingController 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 List<KardexInStocktakingView> getKardex() {
        return kardex;
    }

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