com.accounting.reportMBean.ReportCommonBean.java Source code

Java tutorial

Introduction

Here is the source code for com.accounting.reportMBean.ReportCommonBean.java

Source

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

import com.accounting.commonutils.HelperUtil;
import static com.accounting.commonutils.HelperUtil.showFacesMsg;
import com.accounting.dto.FinishedGoodsDTO;
import javax.transaction.HeuristicRollbackException;
import javax.transaction.NotSupportedException;
import javax.transaction.RollbackException;
import javax.transaction.UserTransaction;
import com.accounting.dto.SalesDTO;
import com.accounting.entities.inventory.IItemMaster;
import com.accounting.entities.inventory.SalesOrder;
import com.accounting.entities.office.Office;
import com.accounting.entities.user.User;
import com.accounting.session.commonreport.CommonSessionReport;
import com.accounting.sessions.inventory.ItemMasterSession;
import com.accounting.sessions.inventory.SalesChallanBean;
import com.accounting.util.JsfUtil;
import com.accounting.utils.NepaliDateConverter;
import com.accounting.utils.PageNameEnum;
import com.accounting.utils.Util;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.transaction.HeuristicMixedException;
import javax.transaction.SystemException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

/**
 *
 * @author faisal
 */
@ManagedBean
@SessionScoped
public class ReportCommonBean {

    @EJB
    public CommonSessionReport commonSessionReport;
    @EJB
    public SalesChallanBean salesChallanBean;
    @EJB
    public ItemMasterSession itemMasterSession;
    @Resource
    UserTransaction tx;
    private List<Object[]> resultsObject;
    private String pageName;
    private double sumOfPurchaseQuantityy;
    private double sumOfPurchaseAmount;
    private double sumOfPurchaseReturnQuantity;
    private double sumOfPurchaseReturnAmount;
    private double sumOfNetPurchaseQuantity;
    private double sumOfNetPurchaseAmount;
    private double sumOfSalesQuantityy;
    private double sumOfSaleAmount;
    private double sumOfReturnQuantity;
    private double sumOfReturnAmount;
    private double sumOfNetSalesQuantity;
    private double sumOfOppening;
    private double sumOfclosing;
    private Double purchaseOpening;
    private Double issueOpening;
    private Double returnOpening;
    private Double productionOpening;
    private Double salesOpening;
    private Double salesreturnOpening;
    private Double totalOpening;
    private Double totalClosing;

    private SalesOrder selectedSalesOrderDetail;
    public List<FinishedGoodsDTO> itemSummaryfinished;
    public List<FinishedGoodsDTO> itemSummaryRow;

    private Date startDate;
    private Date endDate;

    private NepaliDateConverter nepaliDateConverter = new NepaliDateConverter();
    private long customerId;
    private SalesDTO salesDTO = new SalesDTO();
    private boolean editRender;
    private int stage;
    private List<SalesDTO> salesDTOList = new ArrayList<SalesDTO>();
    private List<SalesOrder> orderDetailsList;
    private double totalAmount;
    private List<IItemMaster> allIteamList;

    private Date beforeDate;

    private double sumOfnetSalesAmount;
    private String fromDate, toDate;
    private String filterString;
    private boolean status;
    private NepaliDateConverter ndc = new NepaliDateConverter();

    public ReportCommonBean() {
    }

    @PostConstruct
    public void init() {
    }

    public void postProcessXLSGeneral(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        HSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        //        headerFont.setFontHeightInPoints((short) 20);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        //         Font headerFont1 = wb.createFont();
        //        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        //        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        //        headerCellStyle1.setFont(headerFont);
        //        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        HSSFSheet sheet = wb.getSheetAt(0);
        sheet.shiftRows(0, sheet.getLastRowNum(), 6);
        sheet.autoSizeColumn(0);
        sheet.autoSizeColumn(1);
        sheet.autoSizeColumn(2);
        sheet.autoSizeColumn(3);
        sheet.autoSizeColumn(4);
        sheet.autoSizeColumn(5);
        sheet.autoSizeColumn(6);

        HSSFRow firstRow = sheet.createRow(0);
        firstRow.createCell(0).setCellValue(HelperUtil.getLoggedInOffice().getName());
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow secondRow = sheet.createRow(1);
        secondRow.createCell(0).setCellValue(HelperUtil.getLoggedInOffice().getAddress());
        secondRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow thirdRow = sheet.createRow(2);
        thirdRow.createCell(0).setCellValue(HelperUtil.getPageName().getPageName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow forthRow = sheet.createRow(4);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        forthRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        forthRow.getCell(0).setCellStyle(headerCellStyle2);

        HSSFRow fifthRow = sheet.createRow(3);
        fifthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fifthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 12));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 12));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 12));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 12));
        sheet.addMergedRegion(new CellRangeAddress(4, 4, 0, 12));
        sheet.addMergedRegion(new CellRangeAddress(5, 5, 0, 12));
    }

    public String redirectToSalesOrderReport() {
        HelperUtil.setPageName(PageNameEnum.SALESORDERREPORT);
        stage = 1;
        resultsObject = new ArrayList<Object[]>();
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);
        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);

        resultsObject = commonSessionReport.listOfSalesOrderByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());
        pageName = "SALES ORDER REPORT";
        return "reportSalesOrder.xhtml?faces-redirect=true";

    }

    public void editSalesOrderItems(SalesOrder so) {
        selectedSalesOrderDetail = so;
        customerId = so.getSoldToAccmaster().getId();
        salesDTO = new SalesDTO();
        salesDTOList = new ArrayList<SalesDTO>();
        orderDetailsList = salesChallanBean.returnSODetailBySHeader(so.getId());
        if (orderDetailsList != null && !orderDetailsList.isEmpty()) {
            for (SalesOrder p : orderDetailsList) {
                SalesDTO sd = new SalesDTO();
                sd.setSalesOrderDetail(p);
                sd.setItemName(p.getPurchaseDetail().getItemId().getItemName());
                //                sd.sets(p.getPurchaseDetail().getItemId().getItemCode());
                //                sd.setQuantity(p.getItemId().getId());
                //                sd.set(p.getUnitPrice());
                sd.setQuantity(p.getOrderQty());
                sd.setSalesAmount(p.getPurchaseDetail().getUnitPrice());

                salesDTOList.add(sd);
            }
        }
        editRender = false;
        stage = 2;
        calculateTotal();
    }

    public void cancelOrderListClicked() {
        stage = 1;
    }

    public void calculateTotal() {
        totalAmount = 0;
        for (SalesDTO m : salesDTOList) {
            totalAmount += (m.getPurchaseDetail().getUnitPrice() * m.getQuantity());
        }
    }

    public void filterPurchaseAnalysis() {
        if (filterString.equals("All")) {
            purchaseAnalysisReportSearchClicked();
        } else if (filterString.equalsIgnoreCase("Finished")) {
            purchaseAnalysisReportSearchClickedd(filterString);
        } else if (filterString.equalsIgnoreCase("Raw")) {
            purchaseAnalysisReportSearchClickedd(filterString);
        }
    }

    public List<IItemMaster> getPurchaseItemMasterList() {
        List<IItemMaster> iproductMstrlst = itemMasterSession
                .retrieveIItemMasterByOffice(HelperUtil.getLoggedInOfficeID());
        for (IItemMaster prd : iproductMstrlst) {
            prd.setItemName(prd.getItemName() + " : " + prd.getItemCode());
        }
        return iproductMstrlst;
    }

    public void ajaxPurchaseDataTable() {
        if (salesDTO.getPurchaseItemId() > 0) {

            IItemMaster itemMaster = itemMasterSession.retrieveItemMstrByOfficeAndId(salesDTO.getPurchaseItemId(),
                    HelperUtil.getLoggedInOfficeID());
            //            salesDTO.setPurchaseAvailableQty(itemMaster.getAvailableQty());
            salesDTO.setItemName(itemMaster.getItemName());
            if (itemMaster.getMeasurementUnitId() != null) {
                salesDTO.setPurchaseUnit(itemMaster.getMeasurementUnitId().getName());
            } else {
                salesDTO.setPurchaseUnit("-");
            }
            salesDTO.setItemCode(itemMaster.getItemCode());

        } else {

            //            salesDTO.se("-");
            //            salesDTO.setPurchaseAvailableQty(0L);
            salesDTO.setPurchaseUnit("-");
            //            salesDTO.set("");
            salesDTO.setSalesAmount(0.0);
            salesDTO.setQuantity(0.0);
            salesDTO.setTotalSalesAmount(0.0);

        }

    }

    public void ajaxPurchaseTotalling() {
        salesDTO.setSalesAmount(salesDTO.getPurchaseDetail().getUnitPrice() * salesDTO.getQuantity());
        salesDTO.setQuantity(salesDTO.getQuantity());
        //        salesDTO.setsa(salesDTO.getPurchaseUnitPrice());
    }

    public void cancelEditOrderReport() {
        editRender = false;
        salesDTO = new SalesDTO();
    }

    public void updateOrderDetailEditRender() throws NotSupportedException, SystemException, RollbackException,
            HeuristicMixedException, HeuristicRollbackException {

        if (salesDtoValidation()) {
            return;
        }

        tx.begin();

        //        selectedSalesOrderDetail.setBillAmount(selectedPurchaseOrderHeader.getBillAmount() - (selectedPurchaseOrderDetail.getQuantity() * selectedPurchaseOrderDetail.getUnitPrice()));
        selectedSalesOrderDetail.setOrderQty(salesDTO.getQuantity());
        //        selectedSalesOrderDetail.setPurchaseDetail(purchaseDTO.getPurchaseUnitPrice());
        salesChallanBean.merge(selectedSalesOrderDetail);

        //        selectedSalesOrderDetail.setBillAmount(selectedPurchaseOrderHeader.getBillAmount() + (selectedPurchaseOrderDetail.getQuantity() * selectedPurchaseOrderDetail.getUnitPrice()));
        //        iPurchaseDetailSession.merge(selectedPurchaseOrderHeader);
        tx.commit();
        showFacesMsg(FacesMessage.SEVERITY_INFO, "Transaction successfully updated.", "");

        if (salesDTOList != null && !salesDTOList.isEmpty()) {
            for (SalesDTO m : salesDTOList) {
                if (m.getPurchaseItemId() == salesDTO.getPurchaseItemId()) {
                    //                    m.setPurchaseD(purchaseDTO.getPurchaseUnitPrice());
                    m.setQuantity(salesDTO.getQuantity());
                    break;
                }
            }
        }
        calculateTotal();
        salesDTO = new SalesDTO();
        editRender = false;
    }

    public void persistItemToOrderListReport() throws NotSupportedException, SystemException, RollbackException,
            HeuristicMixedException, HeuristicRollbackException {

        if (salesDtoValidation()) {
            return;
        }

        if (orderDetailsList != null && !orderDetailsList.isEmpty()) {
            for (SalesOrder m : orderDetailsList) {
                if (m.getPurchaseDetail().getItemId().getId() == salesDTO.getPurchaseItemId()) {
                    JsfUtil.addErrorMessage("Item already added. Please update the item.");
                    return;
                }
            }
        }

        tx.begin();

        singleOrderTransaction(salesDTO, selectedSalesOrderDetail);

        //        selectedSalesOrderDetail.setBillAmount(selectedPurchaseOrderHeader.getBillAmount() + (purchaseDTO.getPurchaseQty() * purchaseDTO.getPurchaseUnitPrice()));
        //        iPurchaseDetailSession.merge(selectedPurchaseOrderHeader);
        showFacesMsg(FacesMessage.SEVERITY_INFO, "Transaction Order successfully added.", "");
        tx.commit();
        totalAmount += (salesDTO.getSalesAmount() * salesDTO.getQuantity());
        salesDTOList.add(salesDTO);

        salesDTO = new SalesDTO();
    }

    private void singleOrderTransaction(SalesDTO purDet, SalesOrder poh) {
        IItemMaster item = itemMasterSession.findItemMaster(purDet.getPurchaseItemId());
        SalesOrder pur = new SalesOrder();
        //        pur.setItemId(item);
        pur.setDeleted(false);
        //        pur.setUnitPrice(purDet.getPurchaseUnitPrice());
        pur.setOrderQty(purDet.getQuantity());
        //        pur.setPurchaseOrderHeaderId(poh);
        salesChallanBean.persist(pur);
        //        purDet.setSalesDetail(pur);
    }

    public boolean salesDtoValidation() {
        if (salesDTO.getPurchaseItemId() == 0) {
            JsfUtil.addErrorMessage("Item is required.");
            return true;
        } else if (!JsfUtil.validationNumber(salesDTO.getQuantity(), "Qty")) {
            return true;
        } else if (!JsfUtil.validationNumber(salesDTO.getSalesAmount(), "Price")) {
            return true;
        }
        if (HelperUtil.getPageName() == PageNameEnum.PURCHASEORDERREPORT) {
            if (editRender) {
                if (salesDTO.getQuantity() < selectedSalesOrderDetail.getOrderQty()) {
                    JsfUtil.addErrorMessage("Sorry, the item has " + selectedSalesOrderDetail.getOrderQty() + " "
                            + salesDTO.getSalesAmount() + " already purchased.");
                    return true;
                }
            }
        }
        return false;
    }

    public String redirectToSalesChallanReport() {
        System.out.println("searching challan list by office id");
        resultsObject = new ArrayList<Object[]>();
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);
        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);
        resultsObject = commonSessionReport.listOfSalesChallanByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());
        pageName = "SALES CHALLAN REPORT";
        return "reportSalesChallan.xhtml?faces-redirect=true";
    }

    public void displayAllSalesChallanReport() {
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesChallanByOfficeId(getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
    }

    public String redirectToRowMaterialSummary() {
        pageName = "Raw Material In Out Register- Item  Wise Summary";

        resetGrandTotal();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        allIteamList = new ArrayList<IItemMaster>();
        allIteamList = itemMasterSession.retrieveItemsNameByOfficeid(HelperUtil.getLoggedInOfficeID());
        System.out.println("all iteam size" + allIteamList.size());

        itemSummaryRow = new ArrayList<FinishedGoodsDTO>();
        for (int i = 0; i < allIteamList.size(); i++) {
            FinishedGoodsDTO f = new FinishedGoodsDTO();
            f.setItemName(allIteamList.get(i).getItemName());
            f.setIteamCode(allIteamList.get(i).getItemCode());
            f.setUnit(allIteamList.get(i).getMeasurementUnitId().getShortName());
            //                 f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                  f.setOppeningPurchase(itemMasterSession.retrieveIssueOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                   f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            purchaseOpening = itemMasterSession.retrievePurchaseOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            issueOpening = itemMasterSession.retrieveIssueOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            returnOpening = itemMasterSession.retrieveReturnOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            totalOpening = purchaseOpening - issueOpening - returnOpening;
            f.setOppeningPurchase(totalOpening);

            if (totalOpening != null) {
                sumOfOppening += totalOpening;
            }
            //            List< Object[]> totalList = itemMasterSession.returnAllSummaryRawByItemId(startDate, endDate, HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double purchase = commonSessionReport.retrievePurchase(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double purchaseReturn = commonSessionReport.retrievePurchaseReturn(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double issue = commonSessionReport.retrieveIssue(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            //             for (Object[] s : totalList) {
            f.setTotalPurchase(purchase);
            if (purchase != null) {
                sumOfNetPurchaseQuantity += purchase;
            }
            f.setTotalreturn(purchaseReturn);
            if (purchaseReturn != null) {
                sumOfReturnQuantity += purchaseReturn;
            }
            f.setTotalIssue(issue);
            if (issue != null) {
                sumOfNetSalesQuantity += issue;
            }
            //            }
            try {
                totalClosing = totalOpening + f.getTotalPurchase() - f.getTotalreturn() - f.getTotalIssue();
                f.setOpeningIssue(totalClosing);
                if (totalClosing != null) {
                    sumOfclosing += totalClosing;
                }
            } catch (Exception e) {

            }
            itemSummaryRow.add(f);
        }
        return "reportSummaryRaw.xhtml?faces-redirect=true";
    }

    public String redirectToFinishedGoodsReport() {

        pageName = "Finished Goods In Out Register - Product Wise Summary";
        itemSummaryfinished = new ArrayList<FinishedGoodsDTO>();
        allIteamList = new ArrayList<IItemMaster>();
        resetGrandTotal();
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        allIteamList = new ArrayList<IItemMaster>();
        allIteamList = itemMasterSession.retrieveItemsNameFinishedGoods(HelperUtil.getLoggedInOfficeID());
        System.out.println("all iteam size" + allIteamList.size());
        resetGrandTotal();
        for (int i = 0; i < allIteamList.size(); i++) {
            FinishedGoodsDTO f = new FinishedGoodsDTO();
            f.setItemName(allIteamList.get(i).getItemName());
            f.setIteamCode(allIteamList.get(i).getItemCode());
            f.setUnit(allIteamList.get(i).getMeasurementUnitId().getShortName());
            //                 f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                  f.setOppeningPurchase(itemMasterSession.retrieveIssueOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                   f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            productionOpening = itemMasterSession.retrievePurchaseOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            salesOpening = itemMasterSession.retrieveSalesOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            salesreturnOpening = itemMasterSession.retrieveSalesRetunOpeningStockItem(cal.getTime(),
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            System.out.println("pur oppeing" + productionOpening + "issueOpening" + salesOpening + "returnOpening"
                    + salesreturnOpening);
            totalOpening = productionOpening - salesOpening + salesreturnOpening;
            f.setOppeningPurchase(totalOpening);

            if (totalOpening != null) {
                sumOfOppening += totalOpening;
            }

            Double production = commonSessionReport.retrievePurchase(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double sales = commonSessionReport.retrieveSales(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double saleReturn = commonSessionReport.retrieveSalesReturn(cal.getTime(), cal.getTime(),
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            //            List< Object[]> totalList = itemMasterSession.returnAllFinishedGoodsSummaryByItemId(startDate, endDate, HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            //            for (Object[] s : totalList) {
            f.setTotalPurchase(production);
            if (production != null) {
                sumOfNetPurchaseQuantity += production;
            }
            f.setTotalSales(sales);
            if (sales != null) {
                sumOfReturnQuantity += sales;
            }
            f.setTotalSalesReturn(saleReturn);
            if (saleReturn != null) {
                sumOfNetSalesQuantity += saleReturn;
            }
            //            }

            totalClosing = totalOpening + f.getTotalPurchase() - f.getTotalSales() + f.getTotalSalesReturn();
            f.setOpeningIssue(totalClosing);
            if (totalClosing != null) {
                sumOfclosing += totalClosing;
            }

            itemSummaryfinished.add(f);
            //                   f.setTotalPurchase(itemMasterSession.retrievePurchaseStockBetweenDate(allIteamList.get(i).getId()), startDate, endDate, HelperUtil.getLoggedInOfficeID());
        }
        return "reportSummaryFinishedGoods.xhtml?faces-redirect=true";
    }

    public void rowMaterialReportSummary() {
        pageName = "Raw Material In Out Register- Item  Wise Summary";
        resetGrandTotal();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateReading = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        startDate = nepaliDateConverter.convertToEnglishDate(startDateString);
        endDate = nepaliDateConverter.convertToEnglishDate(endDateReading);
        allIteamList = itemMasterSession.retrieveItemsNameByOfficeid(HelperUtil.getLoggedInOfficeID());
        if (allIteamList == null || allIteamList.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry no record found!");

        }

        itemSummaryRow = new ArrayList<FinishedGoodsDTO>();
        for (int i = 0; i < allIteamList.size(); i++) {
            FinishedGoodsDTO f = new FinishedGoodsDTO();
            f.setItemName(allIteamList.get(i).getItemName());
            f.setIteamCode(allIteamList.get(i).getItemCode());
            f.setUnit(allIteamList.get(i).getMeasurementUnitId().getShortName());
            //                 f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                  f.setOppeningPurchase(itemMasterSession.retrieveIssueOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                   f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            purchaseOpening = itemMasterSession.retrievePurchaseOpeningStockItem(startDate,
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            issueOpening = itemMasterSession.retrieveIssueOpeningStockItem(startDate, getLoggedInOffice().getId(),
                    allIteamList.get(i).getId());
            returnOpening = itemMasterSession.retrieveReturnOpeningStockItem(startDate, getLoggedInOffice().getId(),
                    allIteamList.get(i).getId());
            totalOpening = purchaseOpening - issueOpening - returnOpening;
            f.setOppeningPurchase(totalOpening);

            if (totalOpening != null) {
                sumOfOppening += totalOpening;
            }
            //            List< Object[]> totalList = itemMasterSession.returnAllSummaryRawByItemId(startDate, endDate, HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double purchase = commonSessionReport.retrievePurchase(startDate, endDate,
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double purchaseReturn = commonSessionReport.retrievePurchaseReturn(startDate, endDate,
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double issue = commonSessionReport.retrieveIssue(startDate, endDate, HelperUtil.getLoggedInOfficeID(),
                    allIteamList.get(i).getId());
            //             for (Object[] s : totalList) {
            f.setTotalPurchase(purchase);
            if (purchase != null) {
                sumOfNetPurchaseQuantity += purchase;
            }
            f.setTotalreturn(purchaseReturn);
            if (purchaseReturn != null) {
                sumOfReturnQuantity += purchaseReturn;
            }
            f.setTotalIssue(issue);
            if (issue != null) {
                sumOfNetSalesQuantity += issue;
            }
            //            }
            try {
                totalClosing = totalOpening + f.getTotalPurchase() - f.getTotalreturn() - f.getTotalIssue();
                f.setOpeningIssue(totalClosing);
                if (totalClosing != null) {
                    sumOfclosing += totalClosing;
                }
            } catch (Exception e) {

            }
            itemSummaryRow.add(f);

            //                   f.setTotalPurchase(itemMasterSession.retrievePurchaseStockBetweenDate(allIteamList.get(i).getId()), startDate, endDate, HelperUtil.getLoggedInOfficeID());
        }
    }

    public void finishedGoodsReportSummary() {
        itemSummaryfinished = new ArrayList<FinishedGoodsDTO>();
        pageName = "Finished Goods In Out Register - Product Wise Summary";
        resetGrandTotal();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateReading = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        startDate = nepaliDateConverter.convertToEnglishDate(startDateString);
        endDate = nepaliDateConverter.convertToEnglishDate(endDateReading);
        System.out.println("startDate  " + startDate + "  endDate   " + endDate);
        allIteamList = new ArrayList<IItemMaster>();
        allIteamList = itemMasterSession.retrieveItemsNameFinishedGoods(HelperUtil.getLoggedInOfficeID());
        System.out.println("all iteam size" + allIteamList.size());
        if (allIteamList == null || allIteamList.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry no record found!");

        }
        resetGrandTotal();
        for (int i = 0; i < allIteamList.size(); i++) {
            FinishedGoodsDTO f = new FinishedGoodsDTO();
            f.setItemName(allIteamList.get(i).getItemName());
            f.setIteamCode(allIteamList.get(i).getItemCode());
            f.setUnit(allIteamList.get(i).getMeasurementUnitId().getShortName());
            //                 f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                  f.setOppeningPurchase(itemMasterSession.retrieveIssueOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            //                   f.setOppeningPurchase(itemMasterSession.retrievePurchaseOpeningStockItem(endDate,HelperUtil.getLoggedInOfficeID(),allIteamList.get(i).getId()));
            productionOpening = itemMasterSession.retrievePurchaseOpeningStockItem(startDate,
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            salesOpening = itemMasterSession.retrieveSalesOpeningStockItem(startDate, getLoggedInOffice().getId(),
                    allIteamList.get(i).getId());
            salesreturnOpening = itemMasterSession.retrieveSalesRetunOpeningStockItem(startDate,
                    getLoggedInOffice().getId(), allIteamList.get(i).getId());
            totalOpening = productionOpening - salesOpening + salesreturnOpening;
            f.setOppeningPurchase(totalOpening);

            if (totalOpening != null) {
                sumOfOppening += totalOpening;
            }

            Double production = commonSessionReport.retrievePurchase(startDate, endDate,
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            Double sales = commonSessionReport.retrieveSales(startDate, endDate, HelperUtil.getLoggedInOfficeID(),
                    allIteamList.get(i).getId());
            Double saleReturn = commonSessionReport.retrieveSalesReturn(startDate, endDate,
                    HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            //            List< Object[]> totalList = itemMasterSession.returnAllFinishedGoodsSummaryByItemId(startDate, endDate, HelperUtil.getLoggedInOfficeID(), allIteamList.get(i).getId());
            //            for (Object[] s : totalList) {
            f.setTotalPurchase(production);
            if (production != null) {
                sumOfNetPurchaseQuantity += production;
            }
            f.setTotalSales(sales);
            if (sales != null) {
                sumOfReturnQuantity += sales;
            }
            f.setTotalSalesReturn(saleReturn);
            if (saleReturn != null) {
                sumOfNetSalesQuantity += saleReturn;
            }
            //            }

            totalClosing = totalOpening + f.getTotalPurchase() - f.getTotalSales() + f.getTotalSalesReturn();
            f.setOpeningIssue(totalClosing);
            if (totalClosing != null) {
                sumOfclosing += totalClosing;
            }

            itemSummaryfinished.add(f);
            //                   f.setTotalPurchase(itemMasterSession.retrievePurchaseStockBetweenDate(allIteamList.get(i).getId()), startDate, endDate, HelperUtil.getLoggedInOfficeID());
        }

    }

    public void salesChallanReportSearchClicked() {
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfSalesChallanByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
    }

    public void displayAllSalesOrderReport() {
        System.out.println("searching Order list by office id");
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesOrderByOfficeId(getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
    }

    public void salesOrderReportSearchClicked() {
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfSalesOrderByDate(startDate, endDate, getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

    }

    public void displayAllOfSalesAnalysis() {
        resetGrandTotal();
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesAnalysisByOfficeId(getLoggedInOffice().getId());

        if (!resultsObject.isEmpty()) {

            resetGrandTotal();

            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfSalesQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfSaleAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetSalesQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfnetSalesAmount += (Double) obj[7];
                }
            }
        } else {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

    }

    public void resetGrandTotal() {
        this.sumOfSalesQuantityy = 0.0;
        this.sumOfSaleAmount = 0.0;
        this.sumOfReturnQuantity = 0.0;
        this.sumOfReturnAmount = 0.0;
        this.sumOfNetSalesQuantity = 0.0;
        this.sumOfnetSalesAmount = 0.0;
        this.sumOfOppening = 0.0;
        this.sumOfclosing = 0.0;
        this.purchaseOpening = 0.0;
        this.issueOpening = 0.0;
        this.sumOfNetPurchaseQuantity = 0.0;
        this.sumOfNetSalesQuantity = 0.0;
        this.totalOpening = 0.0;
        this.totalClosing = 0.0;
    }

    public void salesAnalysisCustomerWiseSearchClicked() {
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfSalesCustomerWiseAnalysisByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfSalesQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfSaleAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetSalesQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfnetSalesAmount += (Double) obj[6];
                }
            }
        }

    }

    public String redirectToSalesAnalysisCustomerWiseReport() {
        resetGrandTotal();
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesCustomerWiseAnalysisByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfSalesQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfSaleAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetSalesQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfnetSalesAmount += (Double) obj[6];
                }
            }
        }
        pageName = "SAlES ANALYSIS CUSTOMER WISE REPORT";
        return "reportSalesAnalysisCustomerWise.xhtml?faces-redirect=true";
    }

    public void displayAllSalesAnalysisCustomerWiseReport() {
        resetGrandTotal();
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesAnalysisCustomerWiseByOfficeId(getLoggedInOffice().getId());
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfSalesQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfSaleAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetSalesQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfnetSalesAmount += (Double) obj[6];
                }
            }
        } else {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

    }

    public String redirectToSalesAnalysisReport() {
        resetGrandTotal();
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);

        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfSalesAnalysisByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfSalesQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfSaleAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetSalesQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfnetSalesAmount += (Double) obj[7];
                }
            }
        }
        pageName = "SALES ANALYSIS REPORT";
        return "reportSalesAnalysis.xhtml?faces-redirect=true";
    }

    public void salesAnalysisReportSearchClicked() {
        resetGrandTotal();
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfSalesAnalysisByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfSalesQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfSaleAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetSalesQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfnetSalesAmount += (Double) obj[7];
                }
            }
        }
    }

    public void resultsObjectReportSearchClicked() {
        resetGrandTotal();
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate5");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfSalesCustomerWiseAnalysisByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
        if (!resultsObject.isEmpty()) {

            resetGrandTotal();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfSalesQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfSaleAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetSalesQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfnetSalesAmount += (Double) obj[6];
                }
            }
        }
    }

    public void simplePostProcessXLSSalesSummary(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate7");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate5");
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        HSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        HSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        HSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue("Sales Report Summary Wise");
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        HSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
    }

    public void simplePostProcessXLSForPurchase(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate5");
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        HSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        HSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        HSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue(HelperUtil.getPageName().getPageName());
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        HSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
    }

    public void simplePostProcessXLSForPurchaseforSearch(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate7");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate5");
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        HSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        HSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        HSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue(HelperUtil.getPageName().getPageName());
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        HSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
    }

    public void simplePostProcessXLS(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        HSSFWorkbook wb = (HSSFWorkbook) document;
        HSSFCellStyle headerCellStyle = wb.createCellStyle();
        HSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        HSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        HSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(0).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        HSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue(pageName);
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);

        HSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        HSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
        HSSFRow lastRow;
        for (Row row : sheet) {
            if (row.getRowNum() > 4) {
                for (int i = 3; i < 8; i++) {

                    String cost = row.getCell(i).getStringCellValue();
                    if (cost != null && !cost.isEmpty()) {
                        row.getCell(i).setCellType(HSSFCell.CELL_TYPE_BLANK);
                        row.getCell(i).setCellType(HSSFCell.CELL_TYPE_NUMERIC);
                        row.getCell(i).setCellValue(Double.parseDouble(cost.replace(",", "")));
                    }
                }
            }
        }
    }

    public void simplePostProcessXLSXOfStock(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        XSSFWorkbook wb = (XSSFWorkbook) document;
        XSSFCellStyle headerCellStyle = wb.createCellStyle();
        XSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        XSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        XSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(2).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        XSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue("STOCK VALUE WITH VALUES");
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        XSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);
        //
        XSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        XSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
    }

    public void simplePostProcessXLSX(Object document) {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        XSSFWorkbook wb = (XSSFWorkbook) document;
        XSSFCellStyle headerCellStyle = wb.createCellStyle();
        XSSFCellStyle headerCellStyle1 = wb.createCellStyle();
        XSSFCellStyle headerCellStyle2 = wb.createCellStyle();

        Font headerFont = wb.createFont();
        headerFont.setBoldweight(Font.BOLDWEIGHT_BOLD);
        headerFont.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle.setFont(headerFont);
        headerCellStyle.setAlignment(CellStyle.ALIGN_CENTER);

        Font headerFont1 = wb.createFont();
        headerFont1.setBoldweight(Font.U_SINGLE_ACCOUNTING);
        headerFont1.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle1.setFont(headerFont);
        headerCellStyle1.setAlignment(CellStyle.ALIGN_RIGHT);

        Font headerFont3 = wb.createFont();
        headerFont3.setBoldweight(Font.U_SINGLE);
        headerFont3.setFontName(HSSFFont.FONT_ARIAL);
        headerCellStyle2.setFont(headerFont1);
        headerCellStyle2.setAlignment(CellStyle.ALIGN_RIGHT);

        XSSFSheet sheet = wb.getSheetAt(0);
        int noOfColumns = sheet.getRow(2).getLastCellNum();
        for (int i = 0; i < noOfColumns; i++) {
            sheet.autoSizeColumn(i);
        }
        sheet.shiftRows(0, sheet.getLastRowNum(), 4);

        XSSFRow firstRow = sheet.createRow(1);
        firstRow.createCell(0).setCellValue(pageName);
        firstRow.getCell(0).setCellStyle(headerCellStyle);

        XSSFRow secondRow = sheet.createRow(0);
        secondRow.createCell(0).setCellValue(getLoggedInOffice().getName());
        secondRow.getCell(0).setCellStyle(headerCellStyle);
        //
        XSSFRow thirdRow = sheet.createRow(3);
        String date = ndc.convertToNepaliDate(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss a");
        String time = sdf.format(new Date());
        thirdRow.createCell(0)
                .setCellValue("Generated on:" + date + " " + time + " by:" + getLoggedInUser().getName());
        thirdRow.getCell(0).setCellStyle(headerCellStyle2);

        XSSFRow fourthRow = sheet.createRow(2);
        fourthRow.createCell(0).setCellValue("FROM: " + startDateString + " TO: " + endDateString);
        fourthRow.getCell(0).setCellStyle(headerCellStyle);

        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(2, 2, 0, 7));
        sheet.addMergedRegion(new CellRangeAddress(3, 3, 0, 7));
    }

    public String redirectToPurchaseAnalysisReporSupplierWise() {
        resetGrandTotalOfPurchaseAnalysis();
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);
        resultsObject = commonSessionReport.listOfPurchaseAnalysisSupplierWiseByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());

        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfPurchaseAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[6];
                }
            }
        }
        pageName = "PURCHASE ANALYSIS SUPPLIER WISE";
        return "reportPurchaseAnalysisSupplierWise.xhtml?faces-redirect=true";
    }

    public void purchaseAnalysisSupplierWiseReportSearchClicked() {
        resetGrandTotalOfPurchaseAnalysis();
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfPurchaseAnalysisSupplierWiseByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfPurchaseAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[6];
                }
            }
        }
    }

    public void displayAllPurchaseAnalysisSupplierWise() {
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfPurchaseAnalysisBySupplierWise(getLoggedInOffice().getId());
        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[1] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[1];
                }
                if (obj[2] != null) {
                    sumOfPurchaseAmount += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[6];
                }
            }
        } else {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
    }

    public void displayAllPurchaseAnalysis() {
        resultsObject = new ArrayList<Object[]>();
        resultsObject = commonSessionReport.listOfPurchaseAnalysisByOfficeId(getLoggedInOffice().getId());
        System.out.println("size of sales Analysis list" + resultsObject.size());
        if (!resultsObject.isEmpty()) {

            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                //                Object[] o = (Object[]) obj;
                if (obj[2] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[7];
                }
            }
        } else {

            JsfUtil.addErrorMessage("Sorry, no record found.");
        }
        filterString = "All";
    }

    public void resetGrandTotalOfPurchaseAnalysis() {
        this.sumOfPurchaseQuantityy = 0.0;
        this.sumOfPurchaseAmount = 0.0;
        this.sumOfPurchaseReturnQuantity = 0.0;
        this.sumOfPurchaseReturnAmount = 0.0;
        this.sumOfNetPurchaseQuantity = 0.0;
        this.sumOfNetPurchaseAmount = 0.0;
    }

    public String redirectToPurchaseAnalysisReport() {
        resetGrandTotalOfPurchaseAnalysis();
        filterString = "All";
        fromDate = JsfUtil.dateBeforThirtyDaysNDC(new Date(), -30);
        toDate = ndc.convertToNepaliDate(new Date());

        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        cal.set(Calendar.MILLISECOND, 59);

        Date fdate = JsfUtil.dateBeforThirtyDays(new Date(), -30);
        resultsObject = commonSessionReport.listOfPurchaseAnalysisByDate(fdate, cal.getTime(),
                getLoggedInOffice().getId());

        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[7];
                }
            }
        }
        pageName = "PURCHASE ANALYSIS PRODUCT WISE";
        return "reportPurchaseAnalysisProductWise.xhtml?faces-redirect=true";
    }

    public void purchaseAnalysisReportSearchClicked() {
        resetGrandTotalOfPurchaseAnalysis();
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfPurchaseAnalysisByDate(startDate, endDate,
                getLoggedInOffice().getId());
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[7];
                }
            }
        }
        filterString = "All";
    }

    public void purchaseAnalysisReportSearchClickedd(String pStatus) {
        resetGrandTotalOfPurchaseAnalysis();
        System.out.println("status of paremre" + pStatus);
        if (pStatus.equalsIgnoreCase("Finished")) {
            status = true;
        }

        if (pStatus.equalsIgnoreCase("Raw")) {
            status = false;
        }
        resultsObject = new ArrayList<Object[]>();
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        String endDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if ("".equals(startDateString) || startDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter From Date", "");
            return;
        }
        if ("".equals(endDateString) || endDateString == null) {
            showFacesMsg(FacesMessage.SEVERITY_ERROR, "Please Enter To Date", "");
            return;
        }

        Date startDate = ndc.convertToEnglishDate(startDateString);
        Date endDate = ndc.convertToEnglishDate(endDateString);
        resultsObject = commonSessionReport.listOfPurchaseAnalysisByDateFilter(startDate, endDate,
                getLoggedInOffice().getId(), status);
        if (resultsObject == null || resultsObject.isEmpty()) {
            JsfUtil.addErrorMessage("Sorry, no record found.");
        }

        if (!resultsObject.isEmpty()) {
            resetGrandTotalOfPurchaseAnalysis();
            for (Object[] obj : resultsObject) {
                if (obj[2] != null) {
                    sumOfPurchaseQuantityy += (Double) obj[2];
                }
                if (obj[3] != null) {
                    sumOfPurchaseAmount += (Double) obj[3];
                }
                if (obj[4] != null) {
                    sumOfPurchaseReturnQuantity += (Double) obj[4];
                }
                if (obj[5] != null) {
                    sumOfPurchaseReturnAmount += (Double) obj[5];
                }
                if (obj[6] != null) {
                    sumOfNetPurchaseQuantity += (Double) obj[6];
                }
                if (obj[7] != null) {
                    sumOfNetPurchaseAmount += (Double) obj[7];
                }
            }
        }
    }

    public User getLoggedInUser() {
        User u = (User) Util.getSession().getAttribute("user");
        return u;
    }

    public Office getLoggedInOffice() {
        Office o = (Office) Util.getSession().getAttribute("office");
        return o;
    }

    public String getPreviousDate() {
        String previousDate = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        if (previousDate == null || previousDate.isEmpty()) {
            previousDate = ndc.convertToNepaliDate(new Date());
        }
        return previousDate;
    }

    public String getCurrentDate() {
        String currentDate = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate1");
        if (currentDate == null || currentDate.isEmpty()) {
            currentDate = ndc.convertToNepaliDate(new Date());
        }
        return currentDate;
    }

    public List<Object[]> getResultsObject() {
        return resultsObject;
    }

    public void setResultsObject(List<Object[]> resultsObject) {
        this.resultsObject = resultsObject;
    }

    public double getSumOfPurchaseQuantityy() {
        return sumOfPurchaseQuantityy;
    }

    public void setSumOfPurchaseQuantityy(double sumOfPurchaseQuantityy) {
        this.sumOfPurchaseQuantityy = sumOfPurchaseQuantityy;
    }

    public double getSumOfPurchaseAmount() {
        return sumOfPurchaseAmount;
    }

    public void setSumOfPurchaseAmount(double sumOfPurchaseAmount) {
        this.sumOfPurchaseAmount = sumOfPurchaseAmount;
    }

    public List<IItemMaster> getAllIteamList() {
        return allIteamList;
    }

    public void setAllIteamList(List<IItemMaster> allIteamList) {
        this.allIteamList = allIteamList;
    }

    public double getSumOfPurchaseReturnQuantity() {
        return sumOfPurchaseReturnQuantity;
    }

    public void setSumOfPurchaseReturnQuantity(double sumOfPurchaseReturnQuantity) {
        this.sumOfPurchaseReturnQuantity = sumOfPurchaseReturnQuantity;
    }

    public double getSumOfPurchaseReturnAmount() {
        return sumOfPurchaseReturnAmount;
    }

    public void setSumOfPurchaseReturnAmount(double sumOfPurchaseReturnAmount) {
        this.sumOfPurchaseReturnAmount = sumOfPurchaseReturnAmount;
    }

    public double getSumOfnetSalesAmount() {
        return sumOfnetSalesAmount;
    }

    public void setSumOfnetSalesAmount(double sumOfnetSalesAmount) {
        this.sumOfnetSalesAmount = sumOfnetSalesAmount;
    }

    public double getSumOfNetPurchaseQuantity() {
        return sumOfNetPurchaseQuantity;
    }

    public void setSumOfNetPurchaseQuantity(double sumOfNetPurchaseQuantity) {
        this.sumOfNetPurchaseQuantity = sumOfNetPurchaseQuantity;
    }

    public double getSumOfNetPurchaseAmount() {
        return sumOfNetPurchaseAmount;
    }

    public void setSumOfNetPurchaseAmount(double sumOfNetPurchaseAmount) {
        this.sumOfNetPurchaseAmount = sumOfNetPurchaseAmount;
    }

    public double getSumOfSalesQuantityy() {
        return sumOfSalesQuantityy;
    }

    public void setSumOfSalesQuantityy(double sumOfSalesQuantityy) {
        this.sumOfSalesQuantityy = sumOfSalesQuantityy;
    }

    public double getSumOfSaleAmount() {
        return sumOfSaleAmount;
    }

    public void setSumOfSaleAmount(double sumOfSaleAmount) {
        this.sumOfSaleAmount = sumOfSaleAmount;
    }

    public double getSumOfReturnQuantity() {
        return sumOfReturnQuantity;
    }

    public void setSumOfReturnQuantity(double sumOfReturnQuantity) {
        this.sumOfReturnQuantity = sumOfReturnQuantity;
    }

    public double getSumOfReturnAmount() {
        return sumOfReturnAmount;
    }

    public void setSumOfReturnAmount(double sumOfReturnAmount) {
        this.sumOfReturnAmount = sumOfReturnAmount;
    }

    public double getSumOfNetSalesQuantity() {
        return sumOfNetSalesQuantity;
    }

    public void setSumOfNetSalesQuantity(double sumOfNetSalesQuantity) {
        this.sumOfNetSalesQuantity = sumOfNetSalesQuantity;
    }

    public NepaliDateConverter getNdc() {
        return ndc;
    }

    public void setNdc(NepaliDateConverter ndc) {
        this.ndc = ndc;
    }

    public String getFromDate() {
        String startDateString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()
                .get("nepaliDate4");
        if (startDateString != null && !startDateString.isEmpty()) {
            fromDate = startDateString;
        }
        return fromDate;
    }

    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }

    public String getToDate() {
        return toDate;
    }

    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

    public String getPageName() {
        return pageName;
    }

    public void setPageName(String pageName) {
        this.pageName = pageName;
    }

    public int getStage() {
        return stage;
    }

    public void setStage(int stage) {
        this.stage = stage;
    }

    public CommonSessionReport getCommonSessionReport() {
        return commonSessionReport;
    }

    public void setCommonSessionReport(CommonSessionReport commonSessionReport) {
        this.commonSessionReport = commonSessionReport;
    }

    public SalesOrder getSelectedSalesOrderDetail() {
        return selectedSalesOrderDetail;
    }

    public void setSelectedSalesOrderDetail(SalesOrder selectedSalesOrderDetail) {
        this.selectedSalesOrderDetail = selectedSalesOrderDetail;
    }

    public long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }

    public SalesDTO getSalesDTO() {
        return salesDTO;
    }

    public void setSalesDTO(SalesDTO salesDTO) {
        this.salesDTO = salesDTO;
    }

    public boolean isEditRender() {
        return editRender;
    }

    public void setEditRender(boolean editRender) {
        this.editRender = editRender;
    }

    public List<SalesDTO> getSalesDTOList() {
        return salesDTOList;
    }

    public void setSalesDTOList(List<SalesDTO> salesDTOList) {
        this.salesDTOList = salesDTOList;
    }

    public List<FinishedGoodsDTO> getItemSummaryfinished() {
        return itemSummaryfinished;
    }

    public void setItemSummaryfinished(List<FinishedGoodsDTO> itemSummaryfinished) {
        this.itemSummaryfinished = itemSummaryfinished;
    }

    public List<FinishedGoodsDTO> getItemSummaryRow() {
        return itemSummaryRow;
    }

    public void setItemSummaryRow(List<FinishedGoodsDTO> itemSummaryRow) {
        this.itemSummaryRow = itemSummaryRow;
    }

    public List<SalesOrder> getOrderDetailsList() {
        return orderDetailsList;
    }

    public void setOrderDetailsList(List<SalesOrder> orderDetailsList) {
        this.orderDetailsList = orderDetailsList;
    }

    public double getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(double totalAmount) {
        this.totalAmount = totalAmount;
    }

    public Date getBeforeDate() {
        return beforeDate;
    }

    public void setBeforeDate(Date beforeDate) {
        this.beforeDate = beforeDate;
    }

    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 Double getPurchaseOpening() {
        return purchaseOpening;
    }

    public void setPurchaseOpening(Double purchaseOpening) {
        this.purchaseOpening = purchaseOpening;
    }

    public Double getIssueOpening() {
        return issueOpening;
    }

    public void setIssueOpening(Double issueOpening) {
        this.issueOpening = issueOpening;
    }

    public Double getReturnOpening() {
        return returnOpening;
    }

    public void setReturnOpening(Double returnOpening) {
        this.returnOpening = returnOpening;
    }

    public Double getTotalOpening() {
        return totalOpening;
    }

    public void setTotalOpening(Double totalOpening) {
        this.totalOpening = totalOpening;
    }

    public Double getTotalClosing() {
        return totalClosing;
    }

    public void setTotalClosing(Double totalClosing) {
        this.totalClosing = totalClosing;
    }

    //    public Double getPurchaseOpening() {
    //        return purchaseOpening;
    //    }
    //
    //    public void setPurchaseOpening(Double purchaseOpening) {
    //        this.purchaseOpening = purchaseOpening;
    //    }
    //
    //    public Double getIssueOpening() {
    //        return issueOpening;
    //    }
    //
    //    public void setIssueOpening(Double issueOpening) {
    //        this.issueOpening = issueOpening;
    //    }
    //    public Double getReturnOpening() {
    //        return returnOpening;
    //    }
    //
    //    public void setReturnOpening(Double returnOpening) {
    //        this.returnOpening = returnOpening;
    //    }
    public double getSumOfOppening() {
        return sumOfOppening;
    }

    public void setSumOfOppening(double sumOfOppening) {
        this.sumOfOppening = sumOfOppening;
    }

    public double getSumOfclosing() {
        return sumOfclosing;
    }

    public void setSumOfclosing(double sumOfclosing) {
        this.sumOfclosing = sumOfclosing;
    }

    public Double getProductionOpening() {
        return productionOpening;
    }

    public void setProductionOpening(Double productionOpening) {
        this.productionOpening = productionOpening;
    }

    public Double getSalesOpening() {
        return salesOpening;
    }

    public void setSalesOpening(Double salesOpening) {
        this.salesOpening = salesOpening;
    }

    public Double getSalesreturnOpening() {
        return salesreturnOpening;
    }

    public void setSalesreturnOpening(Double salesreturnOpening) {
        this.salesreturnOpening = salesreturnOpening;
    }

    public String getFilterString() {
        return filterString;
    }

    public void setFilterString(String filterString) {
        this.filterString = filterString;
    }

    public boolean isStatus() {
        return status;
    }

    public void setStatus(boolean status) {
        this.status = status;
    }

}