/*
 * 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 sikd.ikonsol.ctrl;

import java.io.IOException;
import java.io.Serializable;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.faces.view.ViewScoped;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import sikd.ikonsol.ejb.session.JenisBasEjbHome;
import sikd.ikonsol.ejb.session.PeriodeEjbHome;
import sikd.ikonsol.ejb.session.RefPemdaEjbHome;
import sikd.ikonsol.ejb.session.TrialBalanceEjbHome;
import sikd.ikonsol.model.JenisBas;
import sikd.ikonsol.model.Periode;
import sikd.ikonsol.model.ReferensiPemda;
import sikd.ikonsol.model.TrialBalance;

/**
 *
 * @author riyan
 */
@ViewScoped
@Named(value = "tbBeanCtrl")
public class TrialBalanceBeanCtrl implements Serializable {

    TrialBalance tb;

    private List<TrialBalance> list;

    private List<TrialBalance> listDetail, listDetail2, listDetail3, listDetail4;

    private List<JenisBas> listjbas;

    private List<Periode> listperiode;

    private int idbas = 0;

    private int tahun = 0;
    private String kdPemda = null;
    private String kdPeriode = null;

    private String idItem = null;

    int tahun_a = 0, idbas_a = 0;
    String kdpemda_a = null, kdperiode_a = null;

    private List<ReferensiPemda> listPemda;

    @EJB
    TrialBalanceEjbHome tbEjbHome;

    @EJB
    RefPemdaEjbHome pemdaEjbHome;

    @EJB
    JenisBasEjbHome jenisBasEjbHome;

    @EJB
    PeriodeEjbHome periodeBasEjbHome;

    @PostConstruct
    public void init() {
        try {
            list = tbEjbHome.findAllByTahunKode(tahun, kdPemda, idbas, kdPeriode);

            // listDetail = tbEjbHome.findAllByTahunKodeDetail(tahun, kdPemda);
            listPemda = pemdaEjbHome.findAllProvinsi("__.00");
            //listProv = pemdaEjbHome.findAllProvinsi("__.00");
        } catch (Exception e) {
            e.printStackTrace();
        }

        listjbas = jenisBasEjbHome.findAllAktif();

        listperiode = periodeBasEjbHome.findAllAktif();

//        try {
//            FacesContext context = FacesContext.getCurrentInstance();
//            idItem = (String) context.getExternalContext().getSessionMap().get("IDITEM");
//
//            tahun_a = (int) context.getExternalContext().getSessionMap().get("IDTAHUN");
//            kdpemda_a = (String) context.getExternalContext().getSessionMap().get("IDPEMDA");
//            idbas_a = (int) context.getExternalContext().getSessionMap().get("IDBAS");
//            kdperiode_a = (String) context.getExternalContext().getSessionMap().get("IDPERIODE");
//
//            HttpSession session = (HttpSession) context.getExternalContext()
//                    .getSession(true);
//
//            //System.out.println("Bagan " + idItem);
//            //listJenisBas = baganakunstandarEjbHome.findAllByJenisBas(Integer.valueOf(idItem));
//            System.out.println("CETAK " + idItem + " " + tahun_a + " " + kdpemda_a + " " + idbas_a + " " + kdperiode_a);
//            listDetail = tbEjbHome.findAllByTahunKodeDetail(idItem, tahun_a, kdpemda_a, idbas_a, kdperiode_a);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
    }

    public String getSumDebet() {

        double total = 0;
        list = tbEjbHome.findAllByTahunKode(tahun, kdPemda, idbas, kdPeriode);

        for (TrialBalance obj : list) {

            total += obj.getNilaidebit();

         //   System.out.println("ttl1 " + total);
        }

        return new DecimalFormat("###,###.###").format(total);

    }

    public String getSumKredit() {

        double total = 0;
        list = tbEjbHome.findAllByTahunKode(tahun, kdPemda, idbas, kdPeriode);

        for (TrialBalance obj : list) {

            total += obj.getNilaikredit();

        //    System.out.println("ttl " + total);
        }
        return new DecimalFormat("###,###.###").format(total);
    }

    public void reload() {
        list = tbEjbHome.findAllByTahunKode(tahun, kdPemda, idbas, kdPeriode);

//        System.out.println("Tahun " + tahun);
//        System.out.println("Tahun " + kdPemda);
//        System.out.println("Tahun " + idbas);
//        System.out.println("Tahun " + kdPeriode);

        //System.out.println("Size " + list.size());
        //listDetail = tbEjbHome.findAllByTahunKodeDetail(tahun, kdPemda, idbas, kdPeriode);
//listDetail = tbEjbHome.findAllByTahunKodeDetail(tahun, kdPemda, idbas, kdPeriode);
        this.getSumDebet();
        this.getSumKredit();
//        System.out.println("CXCX th : "+tahun+" pmd "+kdPemda);
        //listPemda = pemdaEjbHome.findAll();
    }

    public void listdetail2(String id) {

       // System.out.println("A " + id);
        //listDetail2 = tbEjbHome.findAllByTahunKodeDetail(id, tahun, kdPemda, idbas, kdPeriode);
        listDetail2 = tbEjbHome.findAllByTahunKode2(id, tahun, kdPemda, idbas, kdPeriode);
    }

    public void listdetail3(String id, String id2) {

     //   System.out.println("A " + id);
        //listDetail2 = tbEjbHome.findAllByTahunKodeDetail(id, tahun, kdPemda, idbas, kdPeriode);
        listDetail3 = tbEjbHome.findAllByTahunKode3(id, id2, tahun, kdPemda, idbas, kdPeriode);
    }

    public void listdetail4(String id, String id2, String id3) {

      //  System.out.println("A " + id);
        //listDetail2 = tbEjbHome.findAllByTahunKodeDetail(id, tahun, kdPemda, idbas, kdPeriode);
        listDetail4 = tbEjbHome.findAllByTahunKodeDetail(id, id2, id3, tahun, kdPemda, idbas, kdPeriode);
    }

    public String detail(String id) {
        this.saveToSession(id, tahun, kdPemda, idbas, kdPeriode);

        FacesContext context = FacesContext.getCurrentInstance();
        System.out.println("Bas " + id);
        HttpServletRequest oriquest
                = (HttpServletRequest) context.getExternalContext().getRequest();
        String nexurl = "/ikonsol/proseskonsolidasi/trialbalancedetail.sikd";
        try {
            context.getExternalContext()
                    .redirect(oriquest.getContextPath() + nexurl);// "/logbook/view.xhtml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "view?faces-redirect=true";

    }

    private void saveToSession(String idx, int tahun2, String kdpemda2, int idbas2, String kdperiode2) {
        FacesContext context = FacesContext.getCurrentInstance();
        context.getExternalContext().getSessionMap()
                .put("IDITEM", idx);
        context.getExternalContext().getSessionMap()
                .put("IDTAHUN", tahun2);
        context.getExternalContext().getSessionMap()
                .put("IDPEMDA", kdpemda2);
        context.getExternalContext().getSessionMap()
                .put("IDBAS", idbas2);
        context.getExternalContext().getSessionMap()
                .put("IDPERIODE", kdperiode2);
      //  System.out.println("Session id " + idx);

    }

    public ArrayList<Integer> listTahun() {

        ArrayList<Integer> numbers = new ArrayList<Integer>();
        for (int years = 2015; years <= Calendar.getInstance().get(Calendar.YEAR); years++) {
            numbers.add(years);
        }
        return (numbers);
    }

    public void postProcessXLS(Object document) {

        XSSFWorkbook wb = (XSSFWorkbook) document;
        XSSFSheet sheet = wb.getSheetAt(0);
        XSSFRow header = sheet.getRow(0);

        Iterator<Row> rowIterator = sheet.iterator();

        if (rowIterator.hasNext()) {
            rowIterator.next();
        }

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                if (cell.getColumnIndex() == 0) {
                    cell.setCellValue(cell.getStringCellValue());
                } else if (cell.getColumnIndex() == 1) {
                    cell.setCellValue(cell.getStringCellValue());
                } else if (cell.getColumnIndex() > 1) {
                    if (!cell.getStringCellValue().isEmpty()) {
                        cell.setCellValue(Double.valueOf(cell.getStringCellValue().replace(",", "")));
                    }
                }
            }
        }
    }

    public TrialBalance getTb() {
        return tb;
    }

    public void setTb(TrialBalance tb) {
        this.tb = tb;
    }

    public List<TrialBalance> getList() {
        return list;
    }

    public void setList(List<TrialBalance> list) {
        this.list = list;
    }

    public int getTahun() {
        return tahun;
    }

    public void setTahun(int tahun) {
        this.tahun = tahun;
    }

    public String getKdPemda() {
        return kdPemda;
    }

    public void setKdPemda(String kdPemda) {
        this.kdPemda = kdPemda;
    }

    public List<ReferensiPemda> getListPemda() {
        return listPemda;
    }

    public void setListPemda(List<ReferensiPemda> listPemda) {
        this.listPemda = listPemda;
    }

    public List<TrialBalance> getListDetail() {
        return listDetail;
    }

    public void setListDetail(List<TrialBalance> listDetail) {
        this.listDetail = listDetail;
    }

    public List<JenisBas> getListjbas() {
        return listjbas;
    }

    public void setListjbas(List<JenisBas> listjbas) {
        this.listjbas = listjbas;
    }

    public int getIdbas() {
        return idbas;
    }

    public void setIdbas(int idbas) {
        this.idbas = idbas;
    }

    public TrialBalanceEjbHome getTbEjbHome() {
        return tbEjbHome;
    }

    public void setTbEjbHome(TrialBalanceEjbHome tbEjbHome) {
        this.tbEjbHome = tbEjbHome;
    }

    public RefPemdaEjbHome getPemdaEjbHome() {
        return pemdaEjbHome;
    }

    public void setPemdaEjbHome(RefPemdaEjbHome pemdaEjbHome) {
        this.pemdaEjbHome = pemdaEjbHome;
    }

    public JenisBasEjbHome getJenisBasEjbHome() {
        return jenisBasEjbHome;
    }

    public void setJenisBasEjbHome(JenisBasEjbHome jenisBasEjbHome) {
        this.jenisBasEjbHome = jenisBasEjbHome;
    }

    public List<Periode> getListperiode() {
        return listperiode;
    }

    public void setListperiode(List<Periode> listperiode) {
        this.listperiode = listperiode;
    }

    public String getKdPeriode() {
        return kdPeriode;
    }

    public void setKdPeriode(String kdPeriode) {
        this.kdPeriode = kdPeriode;
    }

    public String getIdItem() {
        return idItem;
    }

    public void setIdItem(String idItem) {
        this.idItem = idItem;
    }

    public int getTahun_a() {
        return tahun_a;
    }

    public void setTahun_a(int tahun_a) {
        this.tahun_a = tahun_a;
    }

    public int getIdbas_a() {
        return idbas_a;
    }

    public void setIdbas_a(int idbas_a) {
        this.idbas_a = idbas_a;
    }

    public String getKdpemda_a() {
        return kdpemda_a;
    }

    public void setKdpemda_a(String kdpemda_a) {
        this.kdpemda_a = kdpemda_a;
    }

    public String getKdperiode_a() {
        return kdperiode_a;
    }

    public void setKdperiode_a(String kdperiode_a) {
        this.kdperiode_a = kdperiode_a;
    }

    public List<TrialBalance> getListDetail2() {
        return listDetail2;
    }

    public void setListDetail2(List<TrialBalance> listDetail2) {
        this.listDetail2 = listDetail2;
    }

    public List<TrialBalance> getListDetail3() {
        return listDetail3;
    }

    public void setListDetail3(List<TrialBalance> listDetail3) {
        this.listDetail3 = listDetail3;
    }

    public List<TrialBalance> getListDetail4() {
        return listDetail4;
    }

    public void setListDetail4(List<TrialBalance> listDetail4) {
        this.listDetail4 = listDetail4;
    }

}
