/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
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.TaxiController;
import com.arrebol.taxiservicios.controller.stocktaking.KardexViewController;
import com.arrebol.taxiservicios.model.admin.Taxi;
import com.arrebol.taxiservicios.model.stocktaking.view.KardexView;
import java.io.Serializable;
import java.time.LocalDate;
import java.time.ZoneId;
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 pc one
 */
@Named
@ViewScoped
public class KardexViewBean extends TaxiGenericBean implements Serializable, Datatable {

    private static final long serialVersionUID = -4475615978271025834L;
    final Logger logger = LogManager.getLogger(TransferStocktakingListBean.class);
    private TaxiController taxiController;
    private List<Taxi> taxiList;
    private KardexViewController kardexViewController;
    private List<KardexView> kardexViewList;
    private KardexView selectedkardex;
    private String selectedTaxi;
    private Date startDate;
    private Date endDate;
    private Date today;
    private Double total;

    @PostConstruct
    public void init() {
        logger.info("init");
        try {
            LocalDate fechaLocalHoy = LocalDate.now();
            LocalDate fechaLocalMenosUmMes = fechaLocalHoy.minusMonths(1);
            loadBundlePropertyFile();
            setTaxiController(new TaxiController());
            setTaxiList(getTaxiController().fillTaxiDatatable2());
            setToday(new Date());
            setStartDate(Date.from(fechaLocalMenosUmMes.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            setEndDate(Date.from(fechaLocalHoy.atStartOfDay(ZoneId.systemDefault()).toInstant()));
            logger.info("loading taxi list");
        } catch (Exception e) {
            logger.error("init", e);
        }
    }

    public void updateKardexViewList() {
        setKardexViewController(new KardexViewController());
        setKardexViewList(getKardexViewController().findAllKardexView(getSelectedTaxi(), getStartDate(), getEndDate()));
        totalPago();
    }

    public void totalPago() {
        total = 0.0;
        for (KardexView kar : getKardexViewList()) {
            total += kar.getPago();
        }
        setTotal(total);
    }

    @Override
    public void editRow(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void onRowCancel(RowEditEvent event) {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @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."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    @Override
    public void deleteRow() {
        throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
    }

    public KardexViewController getKardexViewController() {
        return kardexViewController;
    }

    public void setKardexViewController(KardexViewController kardexViewController) {
        this.kardexViewController = kardexViewController;
    }

    public List<KardexView> getKardexViewList() {
        return kardexViewList;
    }

    public void setKardexViewList(List<KardexView> kardexViewList) {
        this.kardexViewList = kardexViewList;
    }

    public String getSelectedTaxi() {
        return selectedTaxi;
    }

    public void setSelectedTaxi(String selectedTaxi) {
        this.selectedTaxi = selectedTaxi;
    }

    public TaxiController getTaxiController() {
        return taxiController;
    }

    public void setTaxiController(TaxiController taxiController) {
        this.taxiController = taxiController;
    }

    public List<Taxi> getTaxiList() {
        return taxiList;
    }

    public void setTaxiList(List<Taxi> taxiList) {
        this.taxiList = taxiList;
    }

    public KardexView getSelectedkardex() {
        return selectedkardex;
    }

    public void setSelectedkardex(KardexView selectedkardex) {
        this.selectedkardex = selectedkardex;
    }

    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;
    }

    @Override
    public Date getToday() {
        return today;
    }

    @Override
    public void setToday(Date today) {
        this.today = today;
    }

    public Double getTotal() {
        return total;
    }

    public void setTotal(Double total) {
        this.total = total;
    }

}
