Example usage for java.math BigDecimal divide

List of usage examples for java.math BigDecimal divide

Introduction

In this page you can find the example usage for java.math BigDecimal divide.

Prototype

public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) 

Source Link

Document

Returns a BigDecimal whose value is (this / divisor) , and whose scale is as specified.

Usage

From source file:com.selfsoft.business.service.impl.TbFixEntrustServiceImpl.java

public void printTbFixEntrustTemplateBlankXTL(OutputStream os, String tpl, Long tbFixEntrustId) {

    try {/*from   ww w  .j  a  v a 2s .  co  m*/

        TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);

        TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());

        TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());

        List<TbFixEntrustContent> tbFixEntrustContentList = tbFixEntrustContentService
                .findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrustId);

        // ??
        TmCompany tmCompany = tmCompanyService.acquireUniqueTmCompany();

        /*
         * List<TbMaintainPartContent> maintainList =
         * tbMaintainPartContentService
         * .getViewEntrustMaintianContent(tbFixEntrustId);
         * 
         * 
         * List<TmStockOutDetVo> solePartList =
         * tmStockOutService.getSellByEntrustCode
         * (tbFixEntrust.getEntrustCode());
         */

        /**
         * ??
         */
        List<TbMaintianVo> maintianvos = tbMaintainPartContentService
                .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);

        /**
         * ?
         */
        List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
                .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);

        /**
         * ??
         */

        List<TbMaintianVo> partAll = new ArrayList<TbMaintianVo>();

        if (null != maintianvos && maintianvos.size() > 0) {

            for (TbMaintianVo tbMaintianVo : maintianvos) {

                partAll.add(tbMaintianVo);

            }
        }

        if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

            for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

                TbMaintianVo tbMaintianVo = new TbMaintianVo();

                tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

                tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

                tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

                tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

                tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

                tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

                tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

                tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

                tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

                tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

                tbMaintianVo.setZl(tmStockOutDetVo.getZl());

                tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

                partAll.add(tbMaintianVo);
            }
        }

        int fixSize = (tbFixEntrustContentList == null ? 0 : tbFixEntrustContentList.size());

        int maintainSize = (maintianvos == null ? 0 : maintianvos.size());

        int solePartSize = (tmStockOutDetVos == null ? 0 : tmStockOutDetVos.size());

        int partAllSize = (partAll == null ? 0 : partAll.size());

        int pz = (fixSize / 8 > partAllSize / 12 ? (fixSize / 8 + 1) : (partAllSize / 12 + 1));

        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFCellStyle style10_5 = workbook.createCellStyle();

        style10_5.setWrapText(true);

        style10_5.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style10_5.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font10_5 = workbook.createFont();

        font10_5.setFontName("");

        font10_5.setFontHeightInPoints((short) 11);

        style10_5.setFont(font10_5);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.getRow(3);

        cell = row.getCell(0);

        cell.setCellValue("  ??:" + "?" + " (?:70691M)  ?:"
                + tmCompany.getCompanyAddress() + "  ?: "
                + (tmCompany.getCompanyPhone() == null ? "" : tmCompany.getCompanyPhone()));

        row = sheet.getRow(0);

        cell = row.getCell(40);

        // cell.setCellStyle(style);

        cell.setCellValue("*?:" + tbCustomer.getCustomerCode() + "\n" + "??:");

        row = sheet.getRow(6);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getCustomerName());

        row = sheet.getRow(6);

        cell = row.getCell(24);

        // cell.setCellStyle(style);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(6);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getContractPerson() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(8);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(8);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(12);

        cell = row.getCell(0);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        cell.setCellValue(/* tbFixEntrust.getEntrustCode() */newCode);

        row = sheet.getRow(12);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), "yyyy-MM-dd HH:mm"));

        row = sheet.getRow(12);

        cell = row.getCell(18);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(12);

        cell = row.getCell(26);

        // cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(12);

        cell = row.getCell(34);

        cell.setCellValue(tbCarInfo.getChassisCode());

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(0);

        cell.setCellValue(tbFixEntrust.getBjzzh() == null ? "" : tbFixEntrust.getBjzzh());

        row = sheet.getRow(16);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(18);

        cell.setCellValue(tbFixEntrust.getSbrq() == null ? "" : tbFixEntrust.getSbrq());

        row = sheet.getRow(16);

        cell = row.getCell(26);

        cell.setCellValue(tbFixEntrust.getEnterStationKilo() == null ? ""
                : new BigDecimal(tbFixEntrust.getEnterStationKilo().toString())
                        .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(16);

        cell = row.getCell(34);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(), "yyyy-MM-dd HH:mm"));

        Double fixCount = tbFixEntrustContentService.countTbFixEnTrustContentByTbFixEntrustId(tbFixEntrustId);

        row = sheet.getRow(75);

        cell = row.getCell(30);

        cell.setCellValue(new BigDecimal(fixCount).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        BigDecimal partPriceAll = new BigDecimal("0.00");

        if (partAllSize > 0) {

            for (int i = 0; i < partAllSize; i++) {

                TbMaintianVo tbMaintianVo = partAll.get(i);

                BigDecimal total = new BigDecimal(tbMaintianVo.getPrice())
                        .multiply(new BigDecimal(tbMaintianVo.getPartQuantity()));

                partPriceAll = partPriceAll.add(total);

            }

        }

        row = sheet.getRow(77);

        cell = row.getCell(30);

        cell.setCellValue(partPriceAll.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        BigDecimal total = new BigDecimal(fixCount).add(partPriceAll).divide(new BigDecimal("1.00"), 2,
                BigDecimal.ROUND_HALF_UP);

        row = sheet.getRow(81);

        cell = row.getCell(30);

        cell.setCellValue(total.toString());

        row = sheet.getRow(79);

        cell = row.getCell(30);

        cell.setCellValue("0.00");

        row = sheet.getRow(89);

        cell = row.getCell(6);

        cell.setCellValue("1");

        row = sheet.getRow(89);

        cell = row.getCell(1);

        cell.setCellValue(pz + "");

        int page = 1;

        if (fixSize / 8 >= (maintainSize + solePartSize) / 12) {

            page = fixSize / 8;

        }

        else {

            page = (maintainSize + solePartSize) / 12;

        }

        for (int i = 0; i < page; i++) {

            int p = 0;

            int k = 0;

            HSSFSheet sheetClone = workbook.cloneSheet(0);

            row = sheetClone.getRow(89);

            cell = row.getCell(1);

            cell.setCellValue((i + 1) + "");

            row = sheetClone.getRow(89);

            cell = row.getCell(6);

            cell.setCellValue(pz + "");

            if (fixSize > 8) {

                int printFixSize = (fixSize > (i + 2) * 8 ? (i + 2) * 8 : fixSize);

                for (int j = 8 * (i + 1); j < printFixSize; j++) {

                    TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                    List<TbFixShare> tbFixShareList = tbFixShareService
                            .findTbFixShareListByTbFixEntrustContentId(content.getId());

                    String fixPersons = "";

                    String workTypes = "";

                    if (null != tbFixShareList && tbFixShareList.size() > 0) {

                        for (TbFixShare tbFixShare : tbFixShareList) {

                            if (null != tbFixShare.getTmUser()) {

                                TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                                fixPersons += (tmUser.getUserRealName() == null
                                        || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                                : tmUser.getUserRealName())
                                        + " ";

                                workTypes += (tmUser.getTmWorkType() == null ? ""
                                        : tmUser.getTmWorkType().getWorkName()) + " ";
                            }

                        }
                    }

                    row = sheetClone.getRow(23 + p * 2);

                    cell = row.getCell(2);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(content.getStationName());

                    cell = row.getCell(20);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(content.getFixHour())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(content.getFixHourAll())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                    cell = row.getCell(40);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(workTypes);

                    cell = row.getCell(45);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(fixPersons);

                    p++;

                }

            }

            if (partAllSize > 12) {

                int prinPartSize = (partAllSize > (i + 2) * 12 ? (i + 2) * 12 : partAllSize);

                for (int j = 12 * (i + 1); j < prinPartSize; j++) {

                    TbMaintianVo t = partAll.get(j);

                    row = sheetClone.getRow(41 + k * 2);

                    cell = row.getCell(2);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(t.getPartCode());

                    cell = row.getCell(11);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(t.getPartName());

                    cell = row.getCell(20);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(t.getPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                    // 
                    cell = row.getCell(40);

                    // cell.setCellStyle(style10_5);

                    cell.setCellValue("");

                    k++;

                }
            }

        }

        if (fixSize > 0) {

            int printFixSize = (fixSize > 8 ? 8 : fixSize);

            for (int j = 0; j < printFixSize; j++) {

                TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                List<TbFixShare> tbFixShareList = tbFixShareService
                        .findTbFixShareListByTbFixEntrustContentId(content.getId());

                String fixPersons = "";

                String workTypes = "";

                if (null != tbFixShareList && tbFixShareList.size() > 0) {

                    for (TbFixShare tbFixShare : tbFixShareList) {

                        if (null != tbFixShare.getTmUser()) {

                            TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                            fixPersons += (tmUser.getUserRealName() == null
                                    || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                            : tmUser.getUserRealName())
                                    + " ";

                            workTypes += (tmUser.getTmWorkType() == null ? ""
                                    : tmUser.getTmWorkType().getWorkName()) + " ";
                        }

                    }
                }

                row = sheet.getRow(23 + j * 2);

                cell = row.getCell(2);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(content.getStationName());

                cell = row.getCell(20);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(content.getFixHour())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(content.getFixHourAll())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                cell = row.getCell(40);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(workTypes);

                cell = row.getCell(45);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(fixPersons);

            }

        }

        if (partAllSize > 0) {

            int printPartSize = (partAllSize > 12 ? 12 : partAllSize);

            for (int j = 0; j < printPartSize; j++) {

                TbMaintianVo t = partAll.get(j);

                row = sheet.getRow(41 + j * 2);

                cell = row.getCell(2);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(t.getPartCode());

                cell = row.getCell(11);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(t.getPartName());

                cell = row.getCell(20);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(t.getPartQuantity())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(t.getPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                // cell.setCellStyle(style10_5);

                cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                // 
                cell = row.getCell(40);

                // cell.setCellStyle(style10_5);

                cell.setCellValue("");

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:com.selfsoft.business.service.impl.TbFixEntrustServiceImpl.java

public void printTbFixEntrustTemplateXTL(OutputStream os, String tpl, Long tbFixEntrustId) {

    try {//ww  w  .  j a v  a  2  s. com

        TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);

        TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());

        TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());

        List<TbFixEntrustContent> tbFixEntrustContentList = this.composeContent(
                tbFixEntrustContentService.findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrustId));

        // ??
        TmCompany tmCompany = tmCompanyService.acquireUniqueTmCompany();

        /*
         * List<TbMaintainPartContent> maintainList =
         * tbMaintainPartContentService
         * .getViewEntrustMaintianContent(tbFixEntrustId);
         * 
         * 
         * List<TmStockOutDetVo> solePartList =
         * tmStockOutService.getSellByEntrustCode
         * (tbFixEntrust.getEntrustCode());
         */

        /**
         * ??
         */
        List<TbMaintianVo> maintianvos = tbMaintainPartContentService
                .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);

        /**
         * ?
         */
        List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
                .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);

        /**
         * ??
         */

        List<TbMaintianVo> partAll = new ArrayList<TbMaintianVo>();

        if (null != maintianvos && maintianvos.size() > 0) {

            for (TbMaintianVo tbMaintianVo : maintianvos) {

                partAll.add(tbMaintianVo);

            }
        }

        if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

            for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

                TbMaintianVo tbMaintianVo = new TbMaintianVo();

                tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

                tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

                tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

                tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

                tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

                tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

                tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

                tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

                tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

                tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

                tbMaintianVo.setZl(tmStockOutDetVo.getZl());

                tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

                partAll.add(tbMaintianVo);
            }
        }

        partAll = this.composePart(partAll);

        int fixSize = (tbFixEntrustContentList == null ? 0 : tbFixEntrustContentList.size());

        int maintainSize = (maintianvos == null ? 0 : maintianvos.size());

        int solePartSize = (tmStockOutDetVos == null ? 0 : tmStockOutDetVos.size());

        int partAllSize = (partAll == null ? 0 : partAll.size());

        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.getRow(3);

        cell = row.getCell(0);

        cell.setCellValue("  ??:" + "?" + " (?:70691M)  ?:"
                + tmCompany.getCompanyAddress() + "  ?: "
                + (tmCompany.getCompanyPhone() == null ? "" : tmCompany.getCompanyPhone()));

        row = sheet.getRow(0);

        cell = row.getCell(31);

        cell.setCellStyle(style);

        cell.setCellValue("*?:" + tbCustomer.getCustomerCode() + "\n" + "??:");

        row = sheet.getRow(6);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getCustomerName());

        row = sheet.getRow(6);

        cell = row.getCell(24);

        cell.setCellStyle(style);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(6);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getContractPerson() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(8);

        cell = row.getCell(41);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(8);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(12);

        cell = row.getCell(0);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        cell.setCellValue(/* tbFixEntrust.getEntrustCode() */newCode);

        row = sheet.getRow(12);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), "yyyy-MM-dd HH:mm"));

        row = sheet.getRow(12);

        cell = row.getCell(18);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(12);

        cell = row.getCell(26);

        cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(12);

        cell = row.getCell(34);

        cell.setCellValue(tbCarInfo.getChassisCode());

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(0);

        cell.setCellValue("");

        row = sheet.getRow(16);

        cell = row.getCell(9);

        cell.setCellValue(CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        // ?
        row = sheet.getRow(16);

        cell = row.getCell(18);

        cell.setCellValue("");

        row = sheet.getRow(16);

        cell = row.getCell(26);

        cell.setCellValue(tbFixEntrust.getEnterStationKilo() == null ? ""
                : new BigDecimal(tbFixEntrust.getEnterStationKilo().toString())
                        .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(16);

        cell = row.getCell(34);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(), "yyyy-MM-dd HH:mm"));

        Double fixCount = tbFixEntrustContentService.countTbFixEnTrustContentByTbFixEntrustId(tbFixEntrustId);

        row = sheet.getRow(75);

        cell = row.getCell(30);

        cell.setCellValue(new BigDecimal(fixCount).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        BigDecimal partPriceAll = new BigDecimal("0.00");

        if (partAllSize > 0) {

            for (int i = 0; i < partAllSize; i++) {

                TbMaintianVo tbMaintianVo = partAll.get(i);

                BigDecimal total = new BigDecimal(tbMaintianVo.getPrice())
                        .multiply(new BigDecimal(tbMaintianVo.getPartQuantity()));

                partPriceAll = partPriceAll.add(total);

            }

        }

        row = sheet.getRow(77);

        cell = row.getCell(30);

        cell.setCellValue(partPriceAll.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        BigDecimal total = new BigDecimal(fixCount).add(partPriceAll).divide(new BigDecimal("1.00"), 2,
                BigDecimal.ROUND_HALF_UP);

        row = sheet.getRow(81);

        cell = row.getCell(30);

        cell.setCellValue(total.toString());

        row = sheet.getRow(79);

        cell = row.getCell(30);

        cell.setCellValue("0.00");

        ByteArrayOutputStream byteArrayOutImgLion = new ByteArrayOutputStream();

        ByteArrayOutputStream byteArrayOutDBZF = new ByteArrayOutputStream();

        BufferedImage bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/xtl.png"));

        BufferedImage bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/xtl_logo.png"));

        ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

        ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 40, 0, (short) 47, 2);

        HSSFClientAnchor anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 100, (short) 0, 0, (short) 3, 3);

        patriarch.createPicture(anchorLion,
                workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        patriarch.createPicture(anchorDBZF,
                workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        int page = 1;

        if (fixSize / 8 >= (maintainSize + solePartSize) / 12) {

            page = fixSize / 8;

        }

        else {

            page = (maintainSize + solePartSize) / 12;

        }

        for (int i = 0; i < page; i++) {

            int p = 0;

            int k = 0;

            HSSFSheet sheetClone = workbook.cloneSheet(0);

            byteArrayOutImgLion = new ByteArrayOutputStream();

            byteArrayOutDBZF = new ByteArrayOutputStream();

            bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/xtl.png"));

            bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/xtl_logo.png"));

            ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

            ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

            patriarch = sheetClone.createDrawingPatriarch();

            anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 40, 0, (short) 47, 2);

            anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 3, 3);

            patriarch.createPicture(anchorLion,
                    workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            patriarch.createPicture(anchorDBZF,
                    workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            if (fixSize > 8) {

                int printFixSize = (fixSize > (i + 2) * 8 ? (i + 2) * 8 : fixSize);

                for (int j = 8 * (i + 1); j < printFixSize; j++) {

                    TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                    List<TbFixShare> tbFixShareList = tbFixShareService
                            .findTbFixShareListByTbFixEntrustContentId(content.getId());

                    String fixPersons = "";

                    String workTypes = "";

                    if (null != tbFixShareList && tbFixShareList.size() > 0) {

                        for (TbFixShare tbFixShare : tbFixShareList) {

                            if (null != tbFixShare.getTmUser()) {

                                TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                                fixPersons += (tmUser.getUserRealName() == null
                                        || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                                : tmUser.getUserRealName())
                                        + " ";

                                workTypes += (tmUser.getTmWorkType() == null ? ""
                                        : tmUser.getTmWorkType().getWorkName()) + " ";
                            }

                        }
                    }

                    row = sheetClone.getRow(23 + p * 2);

                    cell = row.getCell(2);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getStationName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getFixHour())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(content.getFixHourAll())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue(workTypes);

                    cell = row.getCell(45);

                    cell.setCellStyle(style);

                    cell.setCellValue(fixPersons);

                    p++;

                }

            }

            if (partAllSize > 12) {

                int prinPartSize = (partAllSize > (i + 2) * 12 ? (i + 2) * 12 : partAllSize);

                for (int j = 12 * (i + 1); j < prinPartSize; j++) {

                    TbMaintianVo t = partAll.get(j);

                    row = sheetClone.getRow(41 + k * 2);

                    cell = row.getCell(2);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartCode());

                    cell = row.getCell(11);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(24);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(28);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    cell = row.getCell(34);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                    // 
                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue("");

                    k++;

                }
            }

        }

        if (fixSize > 0) {

            int printFixSize = (fixSize > 8 ? 8 : fixSize);

            for (int j = 0; j < printFixSize; j++) {

                TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                List<TbFixShare> tbFixShareList = tbFixShareService
                        .findTbFixShareListByTbFixEntrustContentId(content.getId());

                String fixPersons = "";

                String workTypes = "";

                if (null != tbFixShareList && tbFixShareList.size() > 0) {

                    for (TbFixShare tbFixShare : tbFixShareList) {

                        if (null != tbFixShare.getTmUser()) {

                            TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                            fixPersons += (tmUser.getUserRealName() == null
                                    || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                            : tmUser.getUserRealName())
                                    + " ";

                            workTypes += (tmUser.getTmWorkType() == null ? ""
                                    : tmUser.getTmWorkType().getWorkName()) + " ";
                        }

                    }
                }

                row = sheet.getRow(23 + j * 2);

                cell = row.getCell(2);

                cell.setCellStyle(style);

                cell.setCellValue(content.getStationName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getFixHour())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getWorkingHourPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(content.getFixHourAll())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                cell.setCellStyle(style);

                cell.setCellValue(content.getProjectType() == null ? "" : content.getProjectType());

                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue(workTypes);

                cell = row.getCell(45);

                cell.setCellStyle(style);

                cell.setCellValue(fixPersons);

            }

        }

        if (partAllSize > 0) {

            int printPartSize = (partAllSize > 12 ? 12 : partAllSize);

            for (int j = 0; j < printPartSize; j++) {

                TbMaintianVo t = partAll.get(j);

                row = sheet.getRow(41 + j * 2);

                cell = row.getCell(2);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartCode());

                cell = row.getCell(11);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPartQuantity())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(24);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(28);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPartQuantity()).multiply(new BigDecimal(t.getPrice()))
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                cell = row.getCell(34);

                cell.setCellStyle(style);

                cell.setCellValue(t.getProjectType() == null ? "" : t.getProjectType());

                // 
                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue("");

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.selfsoft.business.service.impl.TbFixEntrustServiceImpl.java

public void printTbFixEntrustTemplate(OutputStream os, String tpl, Long tbFixEntrustId) {

    /*/*from  w w  w  . j  a  v  a2 s.  c  om*/
     * Workbook wb = null;
     * 
     * WritableWorkbook wwb = null;
     * 
     * WritableSheet ws = null;
     * 
     * try {
     * 
     * wb = Workbook.getWorkbook(this.getClass().getResourceAsStream(tpl));
     * 
     * wwb = Workbook.createWorkbook(os, wb);
     * 
     * ws = wwb.getSheet(0);
     * 
     * WritableCellFormat wcf = new WritableCellFormat();
     * 
     * wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
     * 
     * wcf.setAlignment(Alignment.CENTRE);
     * 
     * wcf.setWrap(true);
     * 
     * TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);
     * 
     * TbCustomer tbCustomer =
     * tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());
     * 
     * TbCarInfo tbCarInfo =
     * tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());
     * 
     * Label label = null;
     * 
     * label = new Label(0, 10, tbFixEntrust.getEntrustCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 10, tbCarInfo.getPurchaseDate()== null ? "" :
     * CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(),
     * "yyyy-MM-dd"), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(14, 10, tbFixEntrust.getTmUser().getUserRealName(),
     * wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 10, tbCarInfo.getLicenseCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(27, 10,
     * tbCarInfo.getTmCarModelType().getModelName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(32, 10, tbFixEntrust.getEnterStationKilo() == null
     * ? "" : tbFixEntrust.getEnterStationKilo().toString(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(41, 10, tbCarInfo.getColor() == null ? "" :
     * tbCarInfo.getColor().toString(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(45, 10, tbFixEntrust.getFixDate() == null ? "" :
     * CommonMethod.parseDateToString(tbFixEntrust.getFixDate(),
     * "yyyy-MM-dd HH:mm:ss"), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(0, 15, tbCustomer.getCustomerName() == null ? "" :
     * tbCustomer.getCustomerName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 15, tbCustomer.getTelephone() == null ? "" :
     * tbCustomer.getTelephone(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(14, 15, tbCustomer.getPhone() == null ? "" :
     * tbCustomer.getPhone(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 15, tbCarInfo.getChassisCode() == null ? "" :
     * tbCarInfo.getChassisCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(32, 15, tbCarInfo.getEngineCode() == null ? "" :
     * tbCarInfo.getEngineCode(),wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(45, 15, tbFixEntrust.getEstimateDate() == null ? ""
     * : CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(),
     * "yyyy-MM-dd HH:mm:ss"),wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 18, tbCustomer.getCustomerName() == null ? "" :
     * tbCustomer.getCustomerName(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(0, 22, tbCustomer.getAddress() == null ? "" :
     * tbCustomer.getAddress(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(8, 25, tbCustomer.getZipCode() == null ? "" :
     * tbCustomer.getZipCode(), wcf);
     * 
     * ws.addCell(label);
     * 
     * label = new Label(20, 20, (tbFixEntrust.getWrongDescribe() ==
     * null?"":tbFixEntrust.getWrongDescribe()) +";" +
     * (tbFixEntrust.getBeforeFixState
     * ()==null?"":tbFixEntrust.getBeforeFixState()) + ";" +
     * (tbFixEntrust.getCheckResult
     * ()==null?"":tbFixEntrust.getCheckResult()) + ";" +
     * (tbFixEntrust.getRemark()==null?"":tbFixEntrust.getRemark()), wcf);
     * 
     * ws.addCell(label);
     * 
     * wwb.write();
     * 
     * } catch (BiffException e) {
     * 
     * e.printStackTrace(); } catch (IOException e) {
     * 
     * e.printStackTrace(); } catch (RowsExceededException e) { // TODO
     * Auto-generated catch block e.printStackTrace(); } catch
     * (WriteException e) { // TODO Auto-generated catch block
     * e.printStackTrace(); } finally {
     * 
     * if (null != wb) {
     * 
     * wb.close();
     * 
     * }
     * 
     * if (null != wwb) {
     * 
     * try { wwb.close(); } catch (WriteException e) {
     * 
     * e.printStackTrace(); } catch (IOException e) {
     * 
     * e.printStackTrace(); } }
     * 
     * }
     */

    try {

        TbFixEntrust tbFixEntrust = this.findById(tbFixEntrustId);

        TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());

        TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());

        List<TbFixEntrustContent> tbFixEntrustContentList = tbFixEntrustContentService
                .findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrustId);

        /*
         * List<TbMaintainPartContent> maintainList =
         * tbMaintainPartContentService
         * .getViewEntrustMaintianContent(tbFixEntrustId);
         * 
         * 
         * List<TmStockOutDetVo> solePartList =
         * tmStockOutService.getSellByEntrustCode
         * (tbFixEntrust.getEntrustCode());
         */

        /**
         * ??
         */
        List<TbMaintianVo> maintianvos = tbMaintainPartContentService
                .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);

        /**
         * ?
         */
        List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
                .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);

        /**
         * ??
         */

        List<TbMaintianVo> partAll = new ArrayList<TbMaintianVo>();

        if (null != maintianvos && maintianvos.size() > 0) {

            for (TbMaintianVo tbMaintianVo : maintianvos) {

                partAll.add(tbMaintianVo);

            }
        }

        if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

            for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

                TbMaintianVo tbMaintianVo = new TbMaintianVo();

                tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

                tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

                tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

                tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

                tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

                tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

                tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

                tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

                tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

                tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

                tbMaintianVo.setZl(tmStockOutDetVo.getZl());

                tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

                partAll.add(tbMaintianVo);
            }
        }

        int fixSize = (tbFixEntrustContentList == null ? 0 : tbFixEntrustContentList.size());

        int maintainSize = (maintianvos == null ? 0 : maintianvos.size());

        int solePartSize = (tmStockOutDetVos == null ? 0 : tmStockOutDetVos.size());

        int partAllSize = (partAll == null ? 0 : partAll.size());

        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        sheet.setMargin(HSSFSheet.LeftMargin, (double) 0.5);

        sheet.setMargin(HSSFSheet.RightMargin, (double) 0.5);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFRow row = null;

        HSSFCell cell = null;

        ByteArrayOutputStream byteArrayOutImgLion = new ByteArrayOutputStream();

        ByteArrayOutputStream byteArrayOutDBZF = new ByteArrayOutputStream();

        BufferedImage bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/lion.png"));

        BufferedImage bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/dfbz.png"));

        ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

        ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 48, 0, (short) 53, 7);

        HSSFClientAnchor anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 8, 7);

        patriarch.createPicture(anchorLion,
                workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        patriarch.createPicture(anchorDBZF,
                workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        row = sheet.getRow(10);

        cell = row.getCell(0);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        // cell.setCellValue(tbFixEntrust.getEntrustCode());

        cell.setCellValue(newCode);

        row = sheet.getRow(10);

        cell = row.getCell(8);

        cell.setCellValue(tbCarInfo.getPurchaseDate() == null ? ""
                : CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        row = sheet.getRow(10);

        cell = row.getCell(14);

        cell.setCellValue(
                tbFixEntrust.getTmUser().getUserRealName() == null ? tbFixEntrust.getTmUser().getUserName()
                        : tbFixEntrust.getTmUser().getUserRealName());

        row = sheet.getRow(10);

        cell = row.getCell(19);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(10);

        cell = row.getCell(27);

        cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(10);

        cell = row.getCell(32);

        cell.setCellValue(
                tbFixEntrust.getEnterStationKilo() == null ? ""
                        : new BigDecimal(tbFixEntrust.getEnterStationKilo().toString())
                                .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString()
                                + "   Km");

        row = sheet.getRow(10);

        cell = row.getCell(41);

        cell.setCellValue(tbCarInfo.getColor() == null ? "" : tbCarInfo.getColor().toString());

        row = sheet.getRow(10);

        cell = row.getCell(45);

        cell.setCellValue(tbFixEntrust.getFixDate() == null ? ""
                : CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), Constants.TIMEFORMATOFMINUTE));

        row = sheet.getRow(15);

        cell = row.getCell(0);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(15);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(15);

        cell = row.getCell(14);

        cell.setCellValue(tbCustomer.getPhone() == null ? "" : tbCustomer.getPhone());

        row = sheet.getRow(15);

        cell = row.getCell(20);

        cell.setCellValue(tbCarInfo.getChassisCode() == null ? "" : tbCarInfo.getChassisCode());

        row = sheet.getRow(15);

        cell = row.getCell(32);

        // cell.setCellStyle(style);

        cell.setCellValue(tbCarInfo.getEngineCode() == null ? "" : tbCarInfo.getEngineCode());

        row = sheet.getRow(15);

        cell = row.getCell(45);

        // cell.setCellStyle(style);

        cell.setCellValue(tbFixEntrust.getEstimateDate() == null ? ""
                : CommonMethod.parseDateToString(tbFixEntrust.getEstimateDate(), Constants.TIMEFORMATOFMINUTE));

        row = sheet.getRow(18);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getCustomerName() == null ? "" : tbCustomer.getCustomerName());

        row = sheet.getRow(20);

        cell = row.getCell(8);

        cell.setCellStyle(style);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(25);

        cell = row.getCell(8);

        cell.setCellValue(tbCustomer.getZipCode() == null ? "" : tbCustomer.getZipCode());

        row = sheet.getRow(20);

        cell = row.getCell(23);

        cell.setCellStyle(style);

        cell.setCellValue((tbFixEntrust.getWrongDescribe() == null || "".equals(tbFixEntrust.getWrongDescribe())
                ? ""
                : tbFixEntrust.getWrongDescribe() + ";")
                + (tbFixEntrust.getBeforeFixState() == null || "".equals(tbFixEntrust.getBeforeFixState()) ? ""
                        : tbFixEntrust.getBeforeFixState() + ";"));

        Double fixCount = tbFixEntrustContentService.countTbFixEnTrustContentByTbFixEntrustId(tbFixEntrustId);

        row = sheet.getRow(70);

        cell = row.getCell(32);

        cell.setCellStyle(style);

        cell.setCellValue(new BigDecimal(fixCount).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        row = sheet.getRow(70);

        cell = row.getCell(48);

        cell.setCellStyle(style);

        cell.setCellValue("0.00");

        BigDecimal partPriceAll = new BigDecimal("0.00");

        if (partAllSize > 0) {

            for (int i = 0; i < partAllSize; i++) {

                TbMaintianVo tbMaintianVo = partAll.get(i);

                BigDecimal total = new BigDecimal(tbMaintianVo.getPrice())
                        .multiply(new BigDecimal(tbMaintianVo.getPartQuantity()));

                partPriceAll = partPriceAll.add(total);

            }

        }

        row = sheet.getRow(73);

        cell = row.getCell(33);

        cell.setCellStyle(style);

        cell.setCellValue(partPriceAll.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        BigDecimal total = new BigDecimal(fixCount).add(partPriceAll).divide(new BigDecimal("1.00"), 2,
                BigDecimal.ROUND_HALF_UP);

        row = sheet.getRow(76);

        cell = row.getCell(34);

        cell.setCellStyle(style);

        cell.setCellValue(total.toString());

        int page = 1;

        if (fixSize / 5 >= (maintainSize + solePartSize) / 13) {

            page = fixSize / 5;

        }

        else {

            page = (maintainSize + solePartSize) / 13;

        }

        for (int i = 0; i < page; i++) {

            int p = 0;

            int k = 0;

            HSSFSheet sheetClone = workbook.cloneSheet(0);

            byteArrayOutImgLion = new ByteArrayOutputStream();

            byteArrayOutDBZF = new ByteArrayOutputStream();

            bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream("/lion.png"));

            bufferImgDFBZ = ImageIO.read(this.getClass().getResourceAsStream("/dfbz.png"));

            ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

            ImageIO.write(bufferImgDFBZ, "png", byteArrayOutDBZF);

            patriarch = sheetClone.createDrawingPatriarch();

            anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 48, 0, (short) 53, 7);

            anchorDBZF = new HSSFClientAnchor(0, 0, 1023, 200, (short) 0, 0, (short) 8, 7);

            patriarch.createPicture(anchorLion,
                    workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            patriarch.createPicture(anchorDBZF,
                    workbook.addPicture(byteArrayOutDBZF.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

            if (fixSize > 5) {

                int printFixSize = (fixSize > (i + 2) * 5 ? (i + 2) * 5 : fixSize);

                for (int j = 5 * (i + 1); j < printFixSize; j++) {

                    TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                    List<TbFixShare> tbFixShareList = tbFixShareService
                            .findTbFixShareListByTbFixEntrustContentId(content.getId());

                    String fixPersons = "";

                    if (null != tbFixShareList && tbFixShareList.size() > 0) {

                        for (TbFixShare tbFixShare : tbFixShareList) {

                            if (null != tbFixShare.getTmUser()) {

                                TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                                fixPersons += (tmUser.getUserRealName() == null
                                        || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                                : tmUser.getUserRealName())
                                        + " ";
                            }

                        }
                    }

                    row = sheetClone.getRow(31 + p * 6);

                    cell = row.getCell(0);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getStationCode() + "    " + content.getStationName());

                    cell = row.getCell(20);

                    cell.setCellStyle(style);

                    cell.setCellValue(content.getXmlx() == null ? "" : content.getXmlx());

                    cell = row.getCell(26);

                    cell.setCellStyle(style);

                    cell.setCellValue(fixPersons);

                    p++;

                }

            }

            if (partAllSize > 13) {

                int prinPartSize = (partAllSize > (i + 2) * 13 ? (i + 2) * 13 : partAllSize);

                for (int j = 13 * (i + 1); j < prinPartSize; j++) {

                    TbMaintianVo t = partAll.get(j);

                    row = sheetClone.getRow(31 + k * 3);

                    cell = row.getCell(32);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartName());

                    cell = row.getCell(40);

                    cell.setCellStyle(style);

                    cell.setCellValue(t.getPartQuantity());

                    cell = row.getCell(44);

                    cell.setCellStyle(style);

                    cell.setCellValue(new BigDecimal(t.getPrice())
                            .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                    k++;

                }
            }

        }

        if (fixSize > 0) {

            int printFixSize = (fixSize > 5 ? 5 : fixSize);

            for (int j = 0; j < printFixSize; j++) {

                TbFixEntrustContent content = tbFixEntrustContentList.get(j);

                List<TbFixShare> tbFixShareList = tbFixShareService
                        .findTbFixShareListByTbFixEntrustContentId(content.getId());

                String fixPersons = "";

                if (null != tbFixShareList && tbFixShareList.size() > 0) {

                    for (TbFixShare tbFixShare : tbFixShareList) {

                        if (null != tbFixShare.getTmUser()) {

                            TmUser tmUser = tmUserService.findById(tbFixShare.getTmUser().getId());

                            fixPersons += (tmUser.getUserRealName() == null
                                    || "".equals(tmUser.getUserRealName()) ? tmUser.getUserName()
                                            : tmUser.getUserRealName())
                                    + " ";
                        }

                    }
                }

                row = sheet.getRow(31 + j * 6);

                cell = row.getCell(0);

                cell.setCellStyle(style);

                cell.setCellValue(content.getStationCode() + "    " + content.getStationName());

                cell = row.getCell(20);

                cell.setCellStyle(style);

                cell.setCellValue(content.getXmlx() == null ? "" : content.getXmlx());

                cell = row.getCell(26);

                cell.setCellStyle(style);

                cell.setCellValue(fixPersons);

            }

        }

        if (partAllSize > 0) {

            int printPartSize = (partAllSize > 13 ? 13 : partAllSize);

            for (int j = 0; j < printPartSize; j++) {

                TbMaintianVo t = partAll.get(j);

                row = sheet.getRow(31 + j * 3);

                cell = row.getCell(32);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartName());

                cell = row.getCell(40);

                cell.setCellStyle(style);

                cell.setCellValue(t.getPartQuantity());

                cell = row.getCell(44);

                cell.setCellStyle(style);

                cell.setCellValue(new BigDecimal(t.getPrice())
                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:sg.ncl.MainController.java

private TeamQuota extractTeamQuotaInfo(String responseBody) {
    JSONObject object = new JSONObject(responseBody);
    TeamQuota teamQuota = new TeamQuota();
    Double charges = Double.parseDouble(accountingProperties.getCharges());

    // amountUsed from SIO will never be null => not checking for null value
    String usage = object.getString(KEY_USAGE); // getting usage in String
    BigDecimal amountUsed = new BigDecimal(usage); //  using BigDecimal to handle currency
    amountUsed = amountUsed.multiply(new BigDecimal(charges)); // usage X charges

    //quota passed from SIO can be null , so we have to check for null value
    if (object.has(QUOTA)) {
        Object budgetObject = object.optString(QUOTA, null);
        if (budgetObject == null) {
            teamQuota.setBudget(""); // there is placeholder here
            teamQuota.setResourcesLeft("Unlimited"); // not placeholder so can pass string over
        } else {//from w w w .  ja  v a2 s.  c o  m
            Double budgetInDouble = object.getDouble(QUOTA); // retrieve budget from SIO in Double
            BigDecimal budgetInBD = BigDecimal.valueOf(budgetInDouble); // handling currency using BigDecimal

            // calculate resoucesLeft
            BigDecimal resourceLeftInBD = budgetInBD.subtract(amountUsed);
            resourceLeftInBD = resourceLeftInBD.divide(new BigDecimal(charges), 0, BigDecimal.ROUND_DOWN);
            budgetInBD = budgetInBD.setScale(2, BigDecimal.ROUND_HALF_UP);

            // set budget
            teamQuota.setBudget(budgetInBD.toString());

            //set resroucesLeft
            if (resourceLeftInBD.compareTo(BigDecimal.valueOf(0)) < 0)
                teamQuota.setResourcesLeft("0");
            else
                teamQuota.setResourcesLeft(resourceLeftInBD.toString());
        }
    }

    //set teamId and amountUsed
    teamQuota.setTeamId(object.getString(TEAM_ID));
    amountUsed = amountUsed.setScale(2, BigDecimal.ROUND_HALF_UP);
    teamQuota.setAmountUsed(amountUsed.toString());
    return teamQuota;
}

From source file:org.ofbiz.accounting.invoice.InvoiceServices.java

public static Map<String, Object> createInvoiceFromReturn(DispatchContext dctx, Map<String, Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    GenericValue userLogin = (GenericValue) context.get("userLogin");
    Locale locale = (Locale) context.get("locale");

    String returnId = (String) context.get("returnId");
    List<GenericValue> billItems = UtilGenerics.checkList(context.get("billItems"));
    String errorMsg = UtilProperties.getMessage(resource, "AccountingErrorCreatingInvoiceForReturn",
            UtilMisc.toMap("returnId", returnId), locale);
    // List invoicesCreated = new ArrayList();
    try {//ww  w  . j  av a 2  s  .c  o  m
        String invoiceTypeId;
        String description;
        // get the return header
        GenericValue returnHeader = EntityQuery.use(delegator).from("ReturnHeader").where("returnId", returnId)
                .queryOne();
        if (returnHeader == null || returnHeader.get("returnHeaderTypeId") == null) {
            return ServiceUtil.returnError("Return type cannot be null");
        }

        if (returnHeader.getString("returnHeaderTypeId").startsWith("CUSTOMER_")) {
            invoiceTypeId = "CUST_RTN_INVOICE";
            description = "Return Invoice for Customer Return #" + returnId;
        } else {
            invoiceTypeId = "PURC_RTN_INVOICE";
            description = "Return Invoice for Vendor Return #" + returnId;
        }
        // set the invoice data
        Map<String, Object> input = UtilMisc.<String, Object>toMap("invoiceTypeId", invoiceTypeId, "statusId",
                "INVOICE_IN_PROCESS");
        input.put("partyId", returnHeader.get("toPartyId"));
        input.put("partyIdFrom", returnHeader.get("fromPartyId"));
        input.put("currencyUomId", returnHeader.get("currencyUomId"));
        input.put("invoiceDate", UtilDateTime.nowTimestamp());
        input.put("description", description);
        input.put("billingAccountId", returnHeader.get("billingAccountId"));
        input.put("userLogin", userLogin);

        // call the service to create the invoice
        Map<String, Object> serviceResults = dispatcher.runSync("createInvoice", input);
        if (ServiceUtil.isError(serviceResults)) {
            return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
        }
        String invoiceId = (String) serviceResults.get("invoiceId");

        // keep track of the invoice total vs the promised return total (how much the customer promised to return)
        BigDecimal invoiceTotal = ZERO;
        BigDecimal promisedTotal = ZERO;

        // loop through shipment receipts to create invoice items and return item billings for each item and adjustment
        int invoiceItemSeqNum = 1;
        String invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                INVOICE_ITEM_SEQUENCE_ID_DIGITS);

        for (GenericValue item : billItems) {
            boolean shipmentReceiptFound = false;
            boolean itemIssuanceFound = false;
            if ("ShipmentReceipt".equals(item.getEntityName())) {
                shipmentReceiptFound = true;
            } else if ("ItemIssuance".equals(item.getEntityName())) {
                itemIssuanceFound = true;
            } else {
                Debug.logError("Unexpected entity " + item + " of type " + item.getEntityName(), module);
            }
            // we need the related return item and product
            GenericValue returnItem = null;
            if (shipmentReceiptFound) {
                returnItem = item.getRelatedOne("ReturnItem", true);
            } else if (itemIssuanceFound) {
                GenericValue shipmentItem = item.getRelatedOne("ShipmentItem", true);
                GenericValue returnItemShipment = EntityUtil
                        .getFirst(shipmentItem.getRelated("ReturnItemShipment", null, null, false));
                returnItem = returnItemShipment.getRelatedOne("ReturnItem", true);
            }
            if (returnItem == null)
                continue; // Just to prevent NPE
            GenericValue product = returnItem.getRelatedOne("Product", true);

            // extract the return price as a big decimal for convenience
            BigDecimal returnPrice = returnItem.getBigDecimal("returnPrice");

            // determine invoice item type from the return item type
            String invoiceItemTypeId = getInvoiceItemType(delegator, returnItem.getString("returnItemTypeId"),
                    null, invoiceTypeId, null);
            if (invoiceItemTypeId == null) {
                return ServiceUtil.returnError(errorMsg + UtilProperties.getMessage(resource,
                        "AccountingNoKnownInvoiceItemTypeReturnItemType",
                        UtilMisc.toMap("returnItemTypeId", returnItem.getString("returnItemTypeId")), locale));
            }
            BigDecimal quantity = BigDecimal.ZERO;
            if (shipmentReceiptFound) {
                quantity = item.getBigDecimal("quantityAccepted");
            } else if (itemIssuanceFound) {
                quantity = item.getBigDecimal("quantity");
            }

            // create the invoice item for this shipment receipt
            input = UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemTypeId", invoiceItemTypeId, "quantity",
                    quantity);
            input.put("invoiceItemSeqId", "" + invoiceItemSeqId); // turn the int into a string with ("" + int) hack
            input.put("amount", returnItem.get("returnPrice"));
            input.put("productId", returnItem.get("productId"));
            input.put("taxableFlag", product.get("taxable"));
            input.put("description", returnItem.get("description"));
            // TODO: what about the productFeatureId?
            input.put("userLogin", userLogin);
            serviceResults = dispatcher.runSync("createInvoiceItem", input);
            if (ServiceUtil.isError(serviceResults)) {
                return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
            }

            // copy the return item information into ReturnItemBilling
            input = UtilMisc.toMap("returnId", returnId, "returnItemSeqId", returnItem.get("returnItemSeqId"),
                    "invoiceId", invoiceId);
            input.put("invoiceItemSeqId", "" + invoiceItemSeqId); // turn the int into a string with ("" + int) hack
            input.put("quantity", quantity);
            input.put("amount", returnItem.get("returnPrice"));
            input.put("userLogin", userLogin);
            if (shipmentReceiptFound) {
                input.put("shipmentReceiptId", item.get("receiptId"));
            }
            serviceResults = dispatcher.runSync("createReturnItemBilling", input);
            if (ServiceUtil.isError(serviceResults)) {
                return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
            }
            if (Debug.verboseOn()) {
                Debug.logVerbose("Creating Invoice Item with amount " + returnPrice + " and quantity "
                        + quantity + " for shipment [" + item.getString("shipmentId") + ":"
                        + item.getString("shipmentItemSeqId") + "]", module);
            }

            String parentInvoiceItemSeqId = invoiceItemSeqId;
            // increment the seqId counter after creating the invoice item and return item billing
            invoiceItemSeqNum += 1;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);

            // keep a running total (note: a returnItem may have many receipts. hence, the promised total quantity is the receipt quantityAccepted + quantityRejected)
            BigDecimal cancelQuantity = ZERO;
            if (shipmentReceiptFound) {
                cancelQuantity = item.getBigDecimal("quantityRejected");
            } else if (itemIssuanceFound) {
                cancelQuantity = item.getBigDecimal("cancelQuantity");
            }
            if (cancelQuantity == null)
                cancelQuantity = ZERO;
            BigDecimal actualAmount = returnPrice.multiply(quantity).setScale(DECIMALS, ROUNDING);
            BigDecimal promisedAmount = returnPrice.multiply(quantity.add(cancelQuantity)).setScale(DECIMALS,
                    ROUNDING);
            invoiceTotal = invoiceTotal.add(actualAmount).setScale(DECIMALS, ROUNDING);
            promisedTotal = promisedTotal.add(promisedAmount).setScale(DECIMALS, ROUNDING);

            // for each adjustment related to this ReturnItem, create a separate invoice item
            List<GenericValue> adjustments = returnItem.getRelated("ReturnAdjustment", null, null, true);
            for (GenericValue adjustment : adjustments) {

                if (adjustment.get("amount") == null) {
                    Debug.logWarning("Return adjustment [" + adjustment.get("returnAdjustmentId")
                            + "] has null amount and will be skipped", module);
                    continue;
                }

                // determine invoice item type from the return item type
                invoiceItemTypeId = getInvoiceItemType(delegator,
                        adjustment.getString("returnAdjustmentTypeId"), null, invoiceTypeId, null);
                if (invoiceItemTypeId == null) {
                    return ServiceUtil
                            .returnError(
                                    errorMsg + UtilProperties
                                            .getMessage(resource,
                                                    "AccountingNoKnownInvoiceItemTypeReturnAdjustmentType",
                                                    UtilMisc.toMap("returnAdjustmentTypeId",
                                                            adjustment.getString("returnAdjustmentTypeId")),
                                                    locale));
                }

                // prorate the adjustment amount by the returned amount; do not round ratio
                BigDecimal ratio = quantity.divide(returnItem.getBigDecimal("returnQuantity"), 100, ROUNDING);
                BigDecimal amount = adjustment.getBigDecimal("amount");
                amount = amount.multiply(ratio).setScale(DECIMALS, ROUNDING);
                if (Debug.verboseOn()) {
                    Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount")
                            + " prorated to " + amount + " for return adjustment ["
                            + adjustment.getString("returnAdjustmentId") + "]", module);
                }

                // prepare invoice item data for this adjustment
                input = UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemTypeId", invoiceItemTypeId,
                        "quantity", BigDecimal.ONE);
                input.put("amount", amount);
                input.put("invoiceItemSeqId", "" + invoiceItemSeqId); // turn the int into a string with ("" + int) hack
                input.put("productId", returnItem.get("productId"));
                input.put("description", adjustment.get("description"));
                input.put("overrideGlAccountId", adjustment.get("overrideGlAccountId"));
                input.put("parentInvoiceId", invoiceId);
                input.put("parentInvoiceItemSeqId", parentInvoiceItemSeqId);
                input.put("taxAuthPartyId", adjustment.get("taxAuthPartyId"));
                input.put("taxAuthGeoId", adjustment.get("taxAuthGeoId"));
                input.put("userLogin", userLogin);

                // only set taxable flag when the adjustment is not a tax
                // TODO: Note that we use the value of Product.taxable here. This is not an ideal solution. Instead, use returnAdjustment.includeInTax
                if (adjustment.get("returnAdjustmentTypeId").equals("RET_SALES_TAX_ADJ")) {
                    input.put("taxableFlag", "N");
                }

                // create the invoice item
                serviceResults = dispatcher.runSync("createInvoiceItem", input);
                if (ServiceUtil.isError(serviceResults)) {
                    return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
                }

                // increment the seqId counter
                invoiceItemSeqNum += 1;
                invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                        INVOICE_ITEM_SEQUENCE_ID_DIGITS);

                // keep a running total (promised adjustment in this case is the same as the invoice adjustment)
                invoiceTotal = invoiceTotal.add(amount).setScale(DECIMALS, ROUNDING);
                promisedTotal = promisedTotal.add(amount).setScale(DECIMALS, ROUNDING);
            }
        }

        // ratio of the invoice total to the promised total so far or zero if the amounts were zero
        BigDecimal actualToPromisedRatio = ZERO;
        if (invoiceTotal.signum() != 0) {
            actualToPromisedRatio = invoiceTotal.divide(promisedTotal, 100, ROUNDING); // do not round ratio
        }

        // loop through return-wide adjustments and create invoice items for each
        List<GenericValue> adjustments = returnHeader.getRelated("ReturnAdjustment",
                UtilMisc.toMap("returnItemSeqId", "_NA_"), null, true);
        for (GenericValue adjustment : adjustments) {

            // determine invoice item type from the return item type
            String invoiceItemTypeId = getInvoiceItemType(delegator,
                    adjustment.getString("returnAdjustmentTypeId"), null, invoiceTypeId, null);
            if (invoiceItemTypeId == null) {
                return ServiceUtil
                        .returnError(
                                errorMsg + UtilProperties
                                        .getMessage(resource,
                                                "AccountingNoKnownInvoiceItemTypeReturnAdjustmentType",
                                                UtilMisc.toMap("returnAdjustmentTypeId",
                                                        adjustment.getString("returnAdjustmentTypeId")),
                                                locale));
            }

            // prorate the adjustment amount by the actual to promised ratio
            BigDecimal amount = adjustment.getBigDecimal("amount").multiply(actualToPromisedRatio)
                    .setScale(DECIMALS, ROUNDING);
            if (Debug.verboseOn()) {
                Debug.logVerbose("Creating Invoice Item with amount " + adjustment.getBigDecimal("amount")
                        + " prorated to " + amount + " for return adjustment ["
                        + adjustment.getString("returnAdjustmentId") + "]", module);
            }

            // prepare the invoice item for the return-wide adjustment
            input = UtilMisc.toMap("invoiceId", invoiceId, "invoiceItemTypeId", invoiceItemTypeId, "quantity",
                    BigDecimal.ONE);
            input.put("amount", amount);
            input.put("invoiceItemSeqId", "" + invoiceItemSeqId); // turn the int into a string with ("" + int) hack
            input.put("description", adjustment.get("description"));
            input.put("overrideGlAccountId", adjustment.get("overrideGlAccountId"));
            input.put("taxAuthPartyId", adjustment.get("taxAuthPartyId"));
            input.put("taxAuthGeoId", adjustment.get("taxAuthGeoId"));
            input.put("userLogin", userLogin);

            // XXX TODO Note: we need to implement ReturnAdjustment.includeInTax for this to work properly
            input.put("taxableFlag", adjustment.get("includeInTax"));

            // create the invoice item
            serviceResults = dispatcher.runSync("createInvoiceItem", input);
            if (ServiceUtil.isError(serviceResults)) {
                return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
            }

            // increment the seqId counter
            invoiceItemSeqNum += 1;
            invoiceItemSeqId = UtilFormatOut.formatPaddedNumber(invoiceItemSeqNum,
                    INVOICE_ITEM_SEQUENCE_ID_DIGITS);
        }

        // Set the invoice to READY
        serviceResults = dispatcher.runSync("setInvoiceStatus", UtilMisc.<String, Object>toMap("invoiceId",
                invoiceId, "statusId", "INVOICE_READY", "userLogin", userLogin));
        if (ServiceUtil.isError(serviceResults)) {
            return ServiceUtil.returnError(errorMsg, null, null, serviceResults);
        }

        // return the invoiceId
        Map<String, Object> results = ServiceUtil.returnSuccess();
        results.put("invoiceId", invoiceId);
        return results;
    } catch (GenericServiceException e) {
        Debug.logError(e, errorMsg + e.getMessage(), module);
        return ServiceUtil.returnError(errorMsg + e.getMessage());
    } catch (GenericEntityException e) {
        Debug.logError(e, errorMsg + e.getMessage(), module);
        return ServiceUtil.returnError(errorMsg + e.getMessage());
    }
}

From source file:org.kuali.ole.select.document.service.impl.OleInvoiceServiceImpl.java

/**
 * @see org.kuali.ole.select.document.service.OleInvoiceService#calculateProrateItemSurcharge(org.kuali.ole.select.document.OleInvoiceDocument)
 *//*  www  . j  a  v  a 2s.  c o  m*/
@Override
public void calculateProrateItemSurcharge(OleInvoiceDocument invoiceDocument) {
    LOG.debug("Inside Calculation for ProrateItemSurcharge");
    //  KualiDecimal addChargeItem = invoiceDocument.getGrandPreTaxTotalExcludingDiscount().subtract(invoiceDocument.getLineItemPreTaxTotal());
    BigDecimal addChargeItem = BigDecimal.ZERO;
    List<OleInvoiceItem> item = invoiceDocument.getItems();
    for (OleInvoiceItem items : item) {
        if (items.getItemType().isAdditionalChargeIndicator() && items.getExtendedPrice() != null) {
            addChargeItem = addChargeItem.add(items.getExtendedPrice().bigDecimalValue());
        }
    }
    List<PurApItem> items = new ArrayList<>();
    /*List<OlePurchaseOrderDocument> olePurchaseOrderDocuments = invoiceDocument.getPurchaseOrderDocuments();
    for (OlePurchaseOrderDocument olePurchaseOrderDocument : olePurchaseOrderDocuments) {
    for (OlePurchaseOrderItem purItem : (List<OlePurchaseOrderItem>) olePurchaseOrderDocument.getItems()) {
        purItem.setItemListPrice(new KualiDecimal(purItem.getInvoiceItemListPrice()));
        if (purItem.isItemForInvoice() && purItem.getItemListPrice().compareTo(KualiDecimal.ZERO) >= 0) {
            items.add(purItem);
        } else {
            purItem.setItemSurcharge(BigDecimal.ZERO);
        }
        if (purItem.getItemListPrice().compareTo(KualiDecimal.ZERO) < 0) {
            purItem.setItemUnitPrice(SpringContext.getBean(OlePurapService.class).calculateDiscount(purItem));
        }
    }
            
    }
    if (items.size() == 0) {*/
    for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
        /* if(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && invoiceItem.getRelatedViews() != null) {
            invoiceItem.setRelatedViews(null);
        }*/
        items.add(invoiceItem);
    }
    /* }
     else {
    for (OleInvoiceItem invoiceItem : (List<OleInvoiceItem>) invoiceDocument.getItems()) {
        if (!(invoiceItem.getItemType().isQuantityBasedGeneralLedgerIndicator())) {
            items.add(invoiceItem);
        }
    }
     }*/
    List<BigDecimal> newUnitPriceList = new ArrayList<>();
    BigDecimal totalExtPrice = new BigDecimal(0);
    BigDecimal newUnitPrice = new BigDecimal(0);
    BigDecimal extPrice = new BigDecimal(0);
    BigDecimal unitPricePercent = new BigDecimal(0);
    BigDecimal hundred = new BigDecimal(100);
    BigDecimal one = new BigDecimal(1);
    BigDecimal totalSurCharge = new BigDecimal(0);
    BigDecimal totalItemQuantity = new BigDecimal(0);
    BigDecimal itemSurchargeCons = new BigDecimal(0);
    for (PurApItem purItem : items) {
        if (purItem instanceof OlePurchaseOrderItem) {
            OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) purItem;
            //  purItem.setItemListPrice(new KualiDecimal(purItem.getInvoiceItemListPrice()));
            if ((poItem.getItemType().isQuantityBasedGeneralLedgerIndicator())
                    && !ObjectUtils.isNull(poItem.getNoOfCopiesInvoiced())
                    && poItem.getItemListPrice().compareTo(KualiDecimal.ZERO) >= 0) {
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    totalItemQuantity = totalItemQuantity.add(poItem.getNoOfCopiesInvoiced().bigDecimalValue());
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)
                        || invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)
                        || invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    if (poItem.getItemDiscount() == null) {
                        poItem.setItemDiscount(KualiDecimal.ZERO);
                    }
                    if (poItem.getItemDiscountType() != null && poItem.getItemDiscountType()
                            .equalsIgnoreCase(OleSelectConstant.DISCOUNT_TYPE_PERCENTAGE)) {
                        newUnitPrice = (hundred.subtract(poItem.getItemDiscount().bigDecimalValue()))
                                .divide(hundred).multiply(poItem.getItemListPrice().bigDecimalValue());
                    } else {
                        newUnitPrice = poItem.getItemListPrice().bigDecimalValue()
                                .subtract(poItem.getItemDiscount().bigDecimalValue());
                    }
                    newUnitPriceList.add(newUnitPrice);
                    extPrice = newUnitPrice.multiply(poItem.getNoOfCopiesInvoiced().bigDecimalValue());
                    totalExtPrice = totalExtPrice.add(extPrice);
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
                    if (poItem.getItemSurcharge() == null) {
                        poItem.setItemSurcharge(BigDecimal.ZERO);
                    }
                    totalSurCharge = totalSurCharge.add(poItem.getNoOfCopiesInvoiced().bigDecimalValue()
                            .multiply(poItem.getItemSurcharge()));
                }
            }
        }

        else if (purItem instanceof OleInvoiceItem) {
            OleInvoiceItem invItem = (OleInvoiceItem) purItem;
            if ((invItem.getItemType().isQuantityBasedGeneralLedgerIndicator())
                    && !ObjectUtils.isNull(invItem.getItemQuantity())
                    && invItem.getItemListPrice().compareTo(KualiDecimal.ZERO) >= 0) {
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    totalItemQuantity = totalItemQuantity.add(invItem.getItemQuantity().bigDecimalValue());
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)
                        || invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)
                        || invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    if (invItem.getItemDiscount() == null) {
                        invItem.setItemDiscount(KualiDecimal.ZERO);
                    }
                    if (invItem.getItemDiscountType() != null && invItem.getItemDiscountType()
                            .equalsIgnoreCase(OleSelectConstant.DISCOUNT_TYPE_PERCENTAGE)) {
                        newUnitPrice = (hundred.subtract(invItem.getItemDiscount().bigDecimalValue()))
                                .divide(hundred).multiply(invItem.getItemListPrice().bigDecimalValue());
                    } else {
                        newUnitPrice = invItem.getItemListPrice().bigDecimalValue()
                                .subtract(invItem.getItemDiscount().bigDecimalValue());
                    }
                    newUnitPriceList.add(newUnitPrice);
                    extPrice = newUnitPrice.multiply(invItem.getItemQuantity().bigDecimalValue());
                    totalExtPrice = totalExtPrice.add(extPrice);
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
                    if (invItem.getItemSurcharge() == null) {
                        invItem.setItemSurcharge(BigDecimal.ZERO);
                    }
                    totalSurCharge = totalSurCharge.add(
                            invItem.getItemQuantity().bigDecimalValue().multiply(invItem.getItemSurcharge()));
                }
            }
        }
    }

    if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
        if (totalItemQuantity.compareTo(BigDecimal.ZERO) != 0) {
            itemSurchargeCons = one.divide(totalItemQuantity, 8, RoundingMode.HALF_UP);
        }
    }
    for (int i = 0, j = 0; items.size() > i; i++) {
        PurApItem purItem = items.get(i);
        if (purItem instanceof OlePurchaseOrderItem) {
            OlePurchaseOrderItem poItem = (OlePurchaseOrderItem) purItem;

            if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && newUnitPriceList.size() > j
                    && !ObjectUtils.isNull(poItem.getNoOfCopiesInvoiced())) {
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)) {
                    if (totalExtPrice.compareTo(BigDecimal.ZERO) != 0) {
                        unitPricePercent = newUnitPriceList.get(j).divide(totalExtPrice, 8,
                                RoundingMode.HALF_UP);
                    }
                    poItem.setItemSurcharge(
                            unitPricePercent.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
                    poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    poItem.setItemSurcharge(
                            itemSurchargeCons.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
                    poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)
                        && poItem.getItemSurcharge() != null) {
                    poItem.setItemUnitPrice(newUnitPriceList.get(j).add(poItem.getItemSurcharge()));
                }
                j++;
            }
        } else if (purItem instanceof OleInvoiceItem) {
            OleInvoiceItem invItem = (OleInvoiceItem) purItem;
            if (invItem.getItemType().isQuantityBasedGeneralLedgerIndicator() && newUnitPriceList.size() > j
                    && !ObjectUtils.isNull(invItem.getItemQuantity())) {
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_DOLLAR)) {
                    if (totalExtPrice.compareTo(BigDecimal.ZERO) != 0) {
                        unitPricePercent = newUnitPriceList.get(j).divide(totalExtPrice, 8,
                                RoundingMode.HALF_UP);
                    }
                    invItem.setItemSurcharge(
                            unitPricePercent.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
                    invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.PRORATE_BY_QTY)) {
                    invItem.setItemSurcharge(
                            itemSurchargeCons.multiply(addChargeItem).setScale(4, RoundingMode.HALF_UP));
                    invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
                }
                if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)
                        && invItem.getItemSurcharge() != null) {
                    invItem.setItemUnitPrice(newUnitPriceList.get(j).add(invItem.getItemSurcharge()));
                }
                j++;
            }

        }
    }
    if (invoiceDocument.getProrateBy().equals(OLEConstants.MANUAL_PRORATE)) {
        if (totalSurCharge.compareTo(addChargeItem) != 0) {
            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_TAB_ERROR_PROPERTY,
                    OLEKeyConstants.ERROR_PAYMENT_REQUEST_TOTAL_MISMATCH);
        }
    }
    LOG.debug("Leaving Calculation for ProrateItemSurcharge");
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

public BigDecimal wievileTOPSArtikelWurdenBereitsZugebucht(Integer losIId, TheClientDto theClientDto) {
    LossollmaterialDto[] dtos = lossollmaterialFindByLosIId(losIId);
    try {//from  w  w  w .j av  a 2s.  com
        LosDto losDto = losFindByPrimaryKey(losIId);

        for (int i = 0; i < dtos.length; i++) {
            Integer artklaIId = null;

            ArtikelDto artikelDto = getArtikelFac().artikelFindByPrimaryKeySmall(dtos[i].getArtikelIId(),
                    theClientDto);
            artklaIId = artikelDto.getArtklaIId();

            if (artklaIId != null) {
                boolean bTops = Helper.short2boolean(
                        getArtikelFac().artklaFindByPrimaryKey(artklaIId, theClientDto).getBTops());

                if (bTops == true) {

                    BigDecimal sollsatzgroesse = dtos[i].getNMenge().divide(losDto.getNLosgroesse(), 4,
                            BigDecimal.ROUND_HALF_EVEN);
                    BigDecimal ausgegeben = getAusgegebeneMenge(dtos[i].getIId(), null, theClientDto);
                    return ausgegeben.divide(sollsatzgroesse, 4, BigDecimal.ROUND_HALF_EVEN);
                }
            }
        }

    } catch (RemoteException ex) {
        throwEJBExceptionLPRespectOld(ex);
    }
    return new BigDecimal(0);
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

public void pruefePositionenMitSollsatzgroesseUnterschreitung(Integer losIId, BigDecimal bdZuErledigendeMenge,
        TheClientDto theClientDto) throws EJBExceptionLP {

    // Basis fuer die Sollsatzgroesse sind die bisher erledigte Menge und
    // die zu erledigende
    BigDecimal bdKuenftigErledigt = getErledigteMenge(losIId, theClientDto).add(bdZuErledigendeMenge);
    LosDto losDto = losFindByPrimaryKey(losIId);
    // es sollte von jeder Position ein prozentueller Anteil ausgegeben sein
    // der prozentsatz wird durch die kuenftig erl. Menge im Verhaeltnis zur
    // Losgroesse bestimmt
    BigDecimal bdFaktor = bdKuenftigErledigt.divide(losDto.getNLosgroesse(), 10, BigDecimal.ROUND_HALF_EVEN);
    // alle Positionen holen
    LossollmaterialDto[] sollmat = lossollmaterialFindByLosIId(losIId);
    LinkedList<LossollmaterialDto> listUnterschritten = new LinkedList<LossollmaterialDto>();
    // fuer jede einzelne soll- und istmenge vergleichen
    for (int i = 0; i < sollmat.length; i++) {
        // bisher ausgegebene Menge
        BigDecimal bdAusgegeben = getAusgegebeneMenge(sollmat[i].getIId(), null, theClientDto);
        // Die soll-ausgabe menge berechnen
        BigDecimal bdSollAusgabeMenge = Helper.rundeKaufmaennisch(sollmat[i].getNMenge().multiply(bdFaktor), 3);
        if (bdSollAusgabeMenge.compareTo(bdAusgegeben) > 0) {
            // Wenn die soll-ausgabe-menge noch nicht erreicht ist, dann zur
            // Liste hinzufuegen
            listUnterschritten.add(sollmat[i]);
        }/*from   w  ww.j a  va  2s . c o m*/
    }
    // und jetzt noch ein Array aus der Liste bauen
    LossollmaterialDto[] sollmatUnterschritten = new LossollmaterialDto[listUnterschritten.size()];
    int i = 0;
    for (Iterator<?> iter = listUnterschritten.iterator(); iter.hasNext(); i++) {
        sollmatUnterschritten[i] = (LossollmaterialDto) iter.next();
    }

    if (sollmatUnterschritten.length > 0) {

        StringBuffer sText = new StringBuffer(getTextRespectUISpr("fert.sollsatzgroesseunterschritten",
                theClientDto.getMandant(), theClientDto.getLocUi()));

        String stkl = getTextRespectUISpr("lp.stueckliste", theClientDto.getMandant(), theClientDto.getLocUi());

        for (int j = 0; j < sollmatUnterschritten.length; j++) {
            sText.append("\n");

            if (losDto.getStuecklisteIId() != null) {
                sText.append(stkl);
                StuecklisteDto stklDto = getStuecklisteFac()
                        .stuecklisteFindByPrimaryKey(losDto.getStuecklisteIId(), theClientDto);
                sText.append(" " + stklDto.getArtikelDto().getCNr() + ": ");
            }

            ArtikelDto artikelDto = getArtikelFac()
                    .artikelFindByPrimaryKey(sollmatUnterschritten[j].getArtikelIId(), theClientDto);
            sText.append(artikelDto.formatArtikelbezeichnung());

        }
        ArrayList ai = new ArrayList();
        ai.add(sText);
        throw new EJBExceptionLP(EJBExceptionLP.FEHLER_FERTIGUNG_SOLLSATZGROESSE_UNTERSCHRITTEN, ai,
                new Exception("losIId == null"));

    }
}

From source file:com.lp.server.fertigung.ejbfac.FertigungFacBean.java

private void aktualisiereAZAllerLosablieferungenWennSollIstGleichIst(Integer losIId,
        TheClientDto theClientDto) {//from w  w  w  .j  a v  a2s  .co  m
    LosDto losDto = losFindByPrimaryKey(losIId);
    try {

        BigDecimal faktorFuerIstGleichSoll = new BigDecimal(0);
        BigDecimal erlMenge = getFertigungFac().getErledigteMenge(losIId, theClientDto);

        if (erlMenge.doubleValue() > 0) {
            if (losDto.getStatusCNr().equals(FertigungFac.STATUS_ERLEDIGT)
                    || losDto.getNLosgroesse().doubleValue() <= erlMenge.doubleValue()) {
                faktorFuerIstGleichSoll = new BigDecimal(1);
            } else {

                if (losDto.getNLosgroesse().doubleValue() > 0) {
                    faktorFuerIstGleichSoll = erlMenge.divide(losDto.getNLosgroesse(), 4,
                            BigDecimal.ROUND_HALF_EVEN);
                }
            }
            String sMandantWaehrung = theClientDto.getSMandantenwaehrung();
            // Sollzeiten
            LossollarbeitsplanDto[] soll = lossollarbeitsplanFindByLosIId(losDto.getIId());

            BigDecimal bdGesamtkosten = new BigDecimal(0.0000);

            // Gesamtkosten berechnen
            for (int i = 0; i < soll.length; i++) {

                // Kosten holen
                BigDecimal kosten = new BigDecimal(0.0000);
                if (soll[i].getMaschineIId() != null) {
                    kosten = getZeiterfassungFac()
                            .getMaschinenKostenZumZeitpunkt(soll[i].getMaschineIId(),
                                    Helper.cutTimestamp(new Timestamp(System.currentTimeMillis())))
                            .multiply(soll[i].getNGesamtzeit()).multiply(faktorFuerIstGleichSoll);
                }

                ArtikellieferantDto artlief = getArtikelFac().getArtikelEinkaufspreis(
                        soll[i].getArtikelIIdTaetigkeit(), new BigDecimal(1), sMandantWaehrung, theClientDto);
                if (artlief != null && artlief.getLief1Preis() != null) {
                    BigDecimal bdSollpreis = artlief.getLief1Preis();
                    kosten = kosten.add(
                            soll[i].getNGesamtzeit().multiply(bdSollpreis).multiply(faktorFuerIstGleichSoll));
                }
                bdGesamtkosten = bdGesamtkosten.add(kosten);

            }

            LosablieferungDto[] losablieferungDtos = losablieferungFindByLosIId(losIId, false, theClientDto);

            BigDecimal kostenProStueck = bdGesamtkosten.divide(erlMenge, 4, BigDecimal.ROUND_HALF_EVEN);

            for (int i = 0; i < losablieferungDtos.length; i++) {
                LosablieferungDto losablieferungDto = losablieferungDtos[i];

                // try {

                Losablieferung losablieferung = em.find(Losablieferung.class, losablieferungDto.getIId());
                if (losablieferung == null) {
                    throw new EJBExceptionLP(EJBExceptionLP.FEHLER_BEI_FINDBYPRIMARYKEY, "");
                }
                losablieferung.setNArbeitszeitwert(kostenProStueck);
                losablieferung.setNArbeitszeitwertdetailliert(kostenProStueck);
                // }
                // catch (FinderException ex) {
                // throw new
                // EJBExceptionLP(EJBExceptionLP.FEHLER_BEI_FINDBYPRIMARYKEY,
                // ex);
                // }

            }
        }

    } catch (RemoteException ex1) {
        throwEJBExceptionLPRespectOld(ex1);
    }

}

From source file:com.selfsoft.business.service.impl.TbBusinessBalanceServiceImpl.java

public void printTbBusinessBalanceTemplate(OutputStream os, String tpl, Long id, String companyName) {
    // ??/*from   w w  w.ja v  a2s.  c  o  m*/
    TbBusinessBalance tbBusinessBalance = this.findById(id);
    // ?
    TbFixEntrust tbFixEntrust = tbFixEntrustService.findById(tbBusinessBalance.getTbFixEntrust().getId());
    // ?
    TbCustomer tbCustomer = tbCustomerService.findById(tbFixEntrust.getTbCustomer().getId());
    // ?
    TbCarInfo tbCarInfo = tbCarInfoService.findById(tbFixEntrust.getTbCarInfo().getId());
    // ??
    TmCompany tmCompany = tmCompanyService.acquireUniqueTmCompany();

    // ?
    List<TbFixEntrustContent> tbFixEntrustContentList = tbFixEntrustContentService
            .findTbFixEnTrustContentListByTbFixEntrustId(tbFixEntrust.getId());

    List<TbFixEntrustContent> tbFixEntrustContentListPage = new ArrayList<TbFixEntrustContent>();

    List<TbFixEntrustContent> tbFixEntrustContentListTemp = new ArrayList<TbFixEntrustContent>();

    List<TbFixEntrustContent> tbFixEntrustContentListAdd = new ArrayList<TbFixEntrustContent>();

    if (null != tbFixEntrustContentList && tbFixEntrustContentList.size() > 0) {

        for (int i = 0; i < tbFixEntrustContentList.size(); i++) {

            boolean flag = false;

            if (tbFixEntrustContentListTemp.size() == 0) {

                tbFixEntrustContentListTemp.add(tbFixEntrustContentList.get(i));

            } else {
                if (tbFixEntrustContentListTemp.size() > 1) {

                    for (TbFixEntrustContent _tbFixEntrustContent : tbFixEntrustContentListTemp) {

                        if (_tbFixEntrustContent.getStationName()
                                .equals(tbFixEntrustContentList.get(i).getStationName())
                                && _tbFixEntrustContent.getTbWorkingInfo().getId()
                                        .equals(tbFixEntrustContentList.get(i).getTbWorkingInfo().getId())
                                && _tbFixEntrustContent.getFreesymbol()
                                        .equals(tbFixEntrustContentList.get(i).getFreesymbol())

                        ) {

                            flag = true;

                            break;

                        }

                    }

                }

            }

            if (flag) {

                continue;

            } else {

                tbFixEntrustContentListTemp.add(tbFixEntrustContentList.get(i));

            }

            TbFixEntrustContent temp = tbFixEntrustContentList.get(i);

            BigDecimal d = new BigDecimal(temp.getFixHourAll());

            BigDecimal d2 = new BigDecimal(temp.getFixHour());

            for (int j = i + 1; j < tbFixEntrustContentList.size(); j++) {

                if (temp.getStationName().equals(tbFixEntrustContentList.get(j).getStationName())
                        && temp.getTbWorkingInfo().getId()
                                .equals(tbFixEntrustContentList.get(j).getTbWorkingInfo().getId())
                        && temp.getFreesymbol().equals(tbFixEntrustContentList.get(j).getFreesymbol())) {

                    d = d.add(new BigDecimal(tbFixEntrustContentList.get(j).getFixHourAll()));

                    d2 = d2.add(new BigDecimal(tbFixEntrustContentList.get(j).getFixHour()));

                }

            }

            temp.setFixHourAll(d.doubleValue());

            temp.setFixHour(d2.doubleValue());

            if (!temp.getFreesymbol().equals(1d) || !temp.getFixHourAll().equals(0d)) {

                tbFixEntrustContentListAdd.add(temp);

            }

        }

    }

    // ??
    List<TbMaintianVo> maintianvos = tbMaintainPartContentService
            .getTbMaintianDetailVosByEntrustId(tbFixEntrust.getId(), Constants.BALANCE_ALL);
    // ?
    List<TmStockOutDetVo> tmStockOutDetVos = tmStockOutService
            .getSellDetailByEntrustCode(tbFixEntrust.getEntrustCode(), Constants.BALANCE_ALL);
    // 
    List<TbBusinessBalanceItem> tbBusinessBalanceItemList = tbBusinessBalanceItemService
            .findGroupTbBusinessBalanceItemListByTbBusinessBalanceId(tbBusinessBalance.getId());

    // ???
    if (null == maintianvos) {
        maintianvos = new ArrayList<TbMaintianVo>();
    }

    if (null != tmStockOutDetVos && tmStockOutDetVos.size() > 0) {

        for (TmStockOutDetVo tmStockOutDetVo : tmStockOutDetVos) {

            TbMaintianVo tbMaintianVo = new TbMaintianVo();

            tbMaintianVo.setPartId(tmStockOutDetVo.getPartinfoId());

            tbMaintianVo.setHouseName(tmStockOutDetVo.getHouseName());

            tbMaintianVo.setPartCode(tmStockOutDetVo.getPartCode());

            tbMaintianVo.setPartName(tmStockOutDetVo.getPartName());

            tbMaintianVo.setUnitName(tmStockOutDetVo.getUnitName());

            tbMaintianVo.setPrice(tmStockOutDetVo.getPrice());

            tbMaintianVo.setPartQuantity(tmStockOutDetVo.getQuantity());

            tbMaintianVo.setTotal(tmStockOutDetVo.getTotal());

            tbMaintianVo.setIsFree(tmStockOutDetVo.getIsFree());

            tbMaintianVo.setProjectType(tmStockOutDetVo.getProjectType());

            tbMaintianVo.setZl(tmStockOutDetVo.getZl());

            tbMaintianVo.setXmlx(tmStockOutDetVo.getXmlx());

            maintianvos.add(tbMaintianVo);
        }

    }

    /**
     * add by ccr 2010-12-18
     */
    List<TbMaintianVo> maintianvosTemp = new ArrayList<TbMaintianVo>();

    List<TbMaintianVo> maintianvosAdd = new ArrayList<TbMaintianVo>();

    if (maintianvos.size() > 0) {

        for (int i = 0; i < maintianvos.size(); i++) {

            boolean flag = false;

            if (maintianvosTemp.size() == 0) {

                maintianvosTemp.add(maintianvos.get(i));

            } else {

                if (maintianvosTemp.size() > 1) {

                    int l = 0;

                    for (TbMaintianVo _tbMaintianVo : maintianvosTemp) {

                        if (_tbMaintianVo.getPartId().equals(maintianvos.get(i).getPartId())
                                && _tbMaintianVo.getIsFree().equals(maintianvos.get(i).getIsFree())
                                && _tbMaintianVo.getPrice().equals(maintianvos.get(i).getPrice())) {

                            // maintianvosTemp.set(l, maintianvos.get(i));

                            flag = true;

                            break;

                        }

                        l++;

                    }

                }

            }

            if (flag) {

                continue;

            } else {

                maintianvosTemp.add(maintianvos.get(i));

            }

            TbMaintianVo temp = maintianvos.get(i);

            BigDecimal d1 = new BigDecimal(temp.getPartQuantity());

            BigDecimal d2 = new BigDecimal(temp.getTotal());

            for (int j = i + 1; j < maintianvos.size(); j++) {

                if (temp.getPartId().equals(maintianvos.get(j).getPartId())
                        && temp.getIsFree().equals(maintianvos.get(j).getIsFree())
                        && temp.getPrice().equals(maintianvos.get(j).getPrice())) {

                    temp.setPrice(maintianvos.get(j).getPrice());

                    d1 = d1.add(new BigDecimal(maintianvos.get(j).getPartQuantity()));

                    d2 = d2.add(new BigDecimal(maintianvos.get(j).getTotal()));

                }

            }

            temp.setPartQuantity(d1.doubleValue());

            temp.setTotal(d2.doubleValue());

            if (!temp.getIsFree().equals(1L) || !temp.getPartQuantity().equals(0d)) {

                /*
                 * temp.setPrice(new BigDecimal(temp.getTotal()).divide(new
                 * BigDecimal(temp.getPartQuantity()),2,
                 * BigDecimal.ROUND_HALF_UP).setScale(2,
                 * BigDecimal.ROUND_HALF_UP).doubleValue());
                 */

                maintianvosAdd.add(temp);

            }

        }

    }

    int fixSize = (tbFixEntrustContentListAdd == null ? 0 : tbFixEntrustContentListAdd.size());

    int partSize = (maintianvosAdd == null ? 0 : maintianvosAdd.size());

    try {
        HSSFWorkbook workbook = new HSSFWorkbook(this.getClass().getResourceAsStream(tpl));

        HSSFSheet sheet = workbook.getSheetAt(0);

        HSSFCellStyle style = workbook.createCellStyle();

        style.setWrapText(true);

        style.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        HSSFFont font = workbook.createFont();

        font.setFontName("");

        font.setFontHeightInPoints((short) 9);

        style.setFont(font);

        HSSFCellStyle styleRight = workbook.createCellStyle();

        styleRight.setWrapText(true);

        styleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);

        styleRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleRight.setFont(font);

        HSSFCellStyle styleBorderThinAll = workbook.createCellStyle();

        styleBorderThinAll.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAll.setBorderRight(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAll.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAll.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAll.setWrapText(true);

        styleBorderThinAll.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        styleBorderThinAll.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThinAll.setFont(font);

        HSSFCellStyle styleBorderThickLeft = workbook.createCellStyle();

        styleBorderThickLeft.setBorderLeft(HSSFCellStyle.BORDER_THICK);

        styleBorderThickLeft.setBorderRight(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeft.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeft.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeft.setWrapText(true);

        styleBorderThickLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        styleBorderThickLeft.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThickLeft.setFont(font);

        HSSFCellStyle styleBorderThickRight = workbook.createCellStyle();

        styleBorderThickRight.setBorderRight(HSSFCellStyle.BORDER_THICK);

        styleBorderThickRight.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRight.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRight.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRight.setWrapText(true);

        styleBorderThickRight.setAlignment(HSSFCellStyle.ALIGN_LEFT);

        styleBorderThickRight.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThickRight.setFont(font);

        HSSFCellStyle styleBorderThinAllCenter = workbook.createCellStyle();

        styleBorderThinAllCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAllCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAllCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAllCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThinAllCenter.setWrapText(true);

        styleBorderThinAllCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        styleBorderThinAllCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThinAllCenter.setFont(font);

        HSSFCellStyle styleBorderThickLeftCenter = workbook.createCellStyle();

        styleBorderThickLeftCenter.setBorderLeft(HSSFCellStyle.BORDER_THICK);

        styleBorderThickLeftCenter.setBorderRight(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeftCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeftCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThickLeftCenter.setWrapText(true);

        styleBorderThickLeftCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        styleBorderThickLeftCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThickLeftCenter.setFont(font);

        HSSFCellStyle styleBorderThickRightCenter = workbook.createCellStyle();

        styleBorderThickRightCenter.setBorderRight(HSSFCellStyle.BORDER_THICK);

        styleBorderThickRightCenter.setBorderLeft(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRightCenter.setBorderTop(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRightCenter.setBorderBottom(HSSFCellStyle.BORDER_THIN);

        styleBorderThickRightCenter.setWrapText(true);

        styleBorderThickRightCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        styleBorderThickRightCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleBorderThickRightCenter.setFont(font);

        HSSFCellStyle styleCenter = workbook.createCellStyle();

        styleCenter.setWrapText(true);

        styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        styleCenter.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

        styleCenter.setFont(font);

        HSSFRow row = null;

        HSSFCell cell = null;

        row = sheet.getRow(2);

        cell = row.getCell(4);

        // cell.setCellStyle(style);

        if ("dfbz".equals(companyName)) {
            cell.setCellValue(Constants.getCompanyMap().get("dfbzCode"));
        } else if ("xtl".equals(companyName)) {

            cell.setCellValue(Constants.getCompanyMap().get("xtlCode"));
        }

        row = sheet.getRow(2);

        cell = row.getCell(17);

        if ("xtl".equals(companyName)) {
            cell.setCellValue(/* tmCompany.getCompanyName() */"?");
        } else {
            cell.setCellValue(tmCompany.getCompanyName());
        }

        row = sheet.getRow(2);

        cell = row.getCell(33);

        cell.setCellValue(tmCompany.getCompanyPhone() == null ? "" : tmCompany.getCompanyPhone());

        row = sheet.getRow(4);

        cell = row.getCell(4);

        if ("xtl".equals(companyName)) {
            cell.setCellValue("");
        } else {

            cell.setCellValue(tmCompany.getCompanyZipCode() == null ? "" : tmCompany.getCompanyZipCode());
        }

        row = sheet.getRow(4);

        cell = row.getCell(17);

        cell.setCellValue(tmCompany.getCompanyAddress() == null ? "" : tmCompany.getCompanyAddress());

        row = sheet.getRow(4);

        cell = row.getCell(33);

        cell.setCellValue(tmCompany.getCompanyFax() == null ? "" : tmCompany.getCompanyFax());

        row = sheet.getRow(10);

        cell = row.getCell(2);

        // cell.setCellStyle(style);

        String entrustCode = tbFixEntrust.getEntrustCode();

        String[] es = entrustCode.split("-");

        String newCode = "RO" + es[0].substring(2, 6) + es[1];

        cell.setCellValue(newCode);

        row = sheet.getRow(10);

        cell = row.getCell(24);

        // cell.setCellStyle(style);

        cell.setCellValue((tbFixEntrust.getTmUser().getUserRealName() == null
                || "".equals(tbFixEntrust.getTmUser().getUserRealName()))
                        ? tbFixEntrust.getTmUser().getUserName()
                        : tbFixEntrust.getTmUser().getUserRealName());

        row = sheet.getRow(10);

        cell = row.getCell(40);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(
                CommonMethod.parseDateToString(tbBusinessBalance.getBananceDate(), "yyyy-MM-dd HH:mm"));

        row = sheet.getRow(8);

        cell = row.getCell(40);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(CommonMethod.parseDateToString(tbFixEntrust.getFixDate(), "yyyy-MM-dd HH:mm"));

        row = sheet.getRow(12);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getCustomerName());

        row = sheet.getRow(12);

        cell = row.getCell(24);

        cell.setCellValue(tbCarInfo.getLicenseCode());

        row = sheet.getRow(12);

        cell = row.getCell(41);

        cell.setCellValue(tbCarInfo.getTmCarModelType().getModelName());

        row = sheet.getRow(14);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(18);

        cell = row.getCell(12);

        cell.setCellValue(tbCustomer.getTelephone() == null ? "" : tbCustomer.getTelephone());

        row = sheet.getRow(14);

        cell = row.getCell(24);

        cell.setCellValue(tbCarInfo.getChassisCode());

        row = sheet.getRow(16);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getAddress() == null ? "" : tbCustomer.getAddress());

        row = sheet.getRow(16);

        cell = row.getCell(24);

        cell.setCellValue(CommonMethod.parseDateToString(tbCarInfo.getPurchaseDate(), "yyyy-MM-dd"));

        row = sheet.getRow(16);

        cell = row.getCell(41);

        if ("xtl".equals(companyName)) {

            cell.setCellValue(
                    tbFixEntrust.getEnterStationKilo() == null ? ""
                            : new BigDecimal(tbFixEntrust.getEnterStationKilo())
                                    .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString()
                                    + " Km");
        }

        else {

            cell.setCellValue(
                    tbFixEntrust.getEnterStationKilo() == null ? ""
                            : new BigDecimal(tbFixEntrust.getEnterStationKilo())
                                    .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP).toString()
                                    + "   Km");

        }

        row = sheet.getRow(18);

        cell = row.getCell(4);

        cell.setCellValue(tbCustomer.getContractPerson() == null ? "" : tbCustomer.getContractPerson());

        row = sheet.getRow(18);

        cell = row.getCell(24);

        cell.setCellValue(tbCarInfo.getEngineCode() == null ? "" : tbCarInfo.getEngineCode());

        row = sheet.getRow(18);

        cell = row.getCell(41);

        cell.setCellValue(tbCarInfo.getColor() == null ? "" : tbCarInfo.getColor());

        /*
         * if (null != tbBusinessBalanceItemList &&
         * tbBusinessBalanceItemList.size() > 0) {
         * 
         * for (TbBusinessBalanceItem tbBusinessBalanceItem :
         * tbBusinessBalanceItemList) {
         * 
         * if ("XLCLF".equals(tbBusinessBalanceItem .getBalanceItemCode()))
         * {
         * 
         * row = sheet.getRow(77);
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellValue(new BigDecimal(tbBusinessBalanceItem
         * .getBalanceItemTotal()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * continue; }
         * 
         * if ("XLGSF".equals(tbBusinessBalanceItem .getBalanceItemCode()))
         * {
         * 
         * row = sheet.getRow(75);
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellValue(new BigDecimal(tbBusinessBalanceItem
         * .getBalanceItemTotal()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * continue; }
         * 
         * if ("ZJE" .equals(tbBusinessBalanceItem.getBalanceItemCode())) {
         * 
         * row = sheet.getRow(81);
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellValue(new BigDecimal(tbBusinessBalanceItem
         * .getBalanceItemTotal()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * continue; }
         * 
         * }
         * 
         * row = sheet.getRow(79);
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellValue("0.00");
         * 
         * }
         */
        BigDecimal d_f_w = new BigDecimal("0.00");

        BigDecimal d_f_p = new BigDecimal("0.00");

        BigDecimal d_f_i = new BigDecimal("0.00");

        BigDecimal d_f_c = new BigDecimal("0.00");

        if (fixSize > 0) {

            for (int i = 0; i < fixSize; i++) {

                TbFixEntrustContent t = tbFixEntrustContentListAdd.get(i);

                if ("W".equals(t.getZl())) {

                    d_f_w = d_f_w.add(new BigDecimal(t.getFixHourAll()));

                }

                else if ("P".equals(t.getZl())) {

                    d_f_p = d_f_p.add(new BigDecimal(t.getFixHourAll()));

                }

                else if ("I".equals(t.getZl())) {

                    d_f_i = d_f_i.add(new BigDecimal(t.getFixHourAll()));

                }

                else if ("C".equals(t.getZl())) {

                    d_f_c = d_f_c.add(new BigDecimal(t.getFixHourAll()));

                }
            }

        }

        BigDecimal d_p_w = new BigDecimal("0.00");

        BigDecimal d_p_p = new BigDecimal("0.00");

        BigDecimal d_p_i = new BigDecimal("0.00");

        BigDecimal d_p_c = new BigDecimal("0.00");

        if (partSize > 0) {

            for (int i = 0; i < partSize; i++) {

                TbMaintianVo t = maintianvosAdd.get(i);

                if ("W".equals(t.getZl())) {

                    d_p_w = d_p_w.add(new BigDecimal(t.getTotal()));

                }

                else if ("P".equals(t.getZl())) {

                    d_p_p = d_p_p.add(new BigDecimal(t.getTotal()));

                }

                else if ("I".equals(t.getZl())) {

                    d_p_i = d_p_i.add(new BigDecimal(t.getTotal()));

                }

                else if ("C".equals(t.getZl())) {

                    d_p_c = d_p_c.add(new BigDecimal(t.getTotal()));

                }

            }

        }

        row = sheet.getRow(75);

        cell = row.getCell(6);

        cell.setCellValue(d_f_w.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(77);

        cell = row.getCell(6);

        cell.setCellValue(d_p_w.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(79);

        cell = row.getCell(6);

        cell.setCellValue("0.00");

        row = sheet.getRow(81);

        cell = row.getCell(6);

        cell.setCellValue(
                d_f_w.add(d_p_w).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(75);

        cell = row.getCell(12);

        cell.setCellValue(d_f_p.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(77);

        cell = row.getCell(12);

        cell.setCellValue(d_p_p.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(79);

        cell = row.getCell(12);

        cell.setCellValue("0.00");

        row = sheet.getRow(81);

        cell = row.getCell(12);

        cell.setCellValue(
                d_f_p.add(d_p_p).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(75);

        cell = row.getCell(18);

        cell.setCellValue(d_f_i.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(77);

        cell = row.getCell(18);

        cell.setCellValue(d_p_i.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(79);

        cell = row.getCell(18);

        cell.setCellValue("0.00");

        row = sheet.getRow(81);

        cell = row.getCell(18);

        cell.setCellValue(
                d_f_i.add(d_p_i).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(75);

        cell = row.getCell(24);

        cell.setCellValue(d_f_c.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(77);

        cell = row.getCell(24);

        cell.setCellValue(d_p_c.divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(79);

        cell = row.getCell(24);

        cell.setCellValue("0.00");

        row = sheet.getRow(81);

        cell = row.getCell(24);

        cell.setCellValue(
                d_f_c.add(d_p_c).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        Double wt = tbBusinessBalance.getWorkingHourTotalAll() == null ? 0d
                : tbBusinessBalance.getWorkingHourTotalAll();

        Double wf = tbBusinessBalance.getWorkingHourFavourRate() == null ? 0d
                : tbBusinessBalance.getWorkingHourFavourRate();

        Double pt = tbBusinessBalance.getFixPartTotalAll() == null ? 0d
                : tbBusinessBalance.getFixPartTotalAll();

        Double pf = tbBusinessBalance.getFixPartFavourRate() == null ? 0d
                : tbBusinessBalance.getFixPartFavourRate();

        Double st = tbBusinessBalance.getSolePartTotalAll() == null ? 0d
                : tbBusinessBalance.getSolePartTotalAll();

        Double sf = tbBusinessBalance.getSolePartFavourRate() == null ? 0d
                : tbBusinessBalance.getSolePartFavourRate();

        BigDecimal fixF = new BigDecimal(wt).divide(new BigDecimal(1 - wf), 2, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(wf));

        BigDecimal partF = new BigDecimal(pt).divide(new BigDecimal(1 - pf), 2, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(pf));

        BigDecimal soleF = new BigDecimal(st).divide(new BigDecimal(1 - sf), 2, BigDecimal.ROUND_HALF_UP)
                .multiply(new BigDecimal(sf));

        row = sheet.getRow(83);

        cell = row.getCell(4);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(fixF.add(partF).add(soleF).divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                .toString());

        row = sheet.getRow(83);

        cell = row.getCell(14);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(new BigDecimal(tbBusinessBalance.getPayedAmount())
                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

        row = sheet.getRow(68);

        cell = row.getCell(40);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(CommonMethod
                .parseDateToString(tbBusinessBalance.getTbFixEntrust().getRemindMaintainDate(), "yyyy-MM-dd"));

        row = sheet.getRow(70);

        cell = row.getCell(40);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(tbBusinessBalance.getTbFixEntrust().getRemindMaintainKilo() == null ? ""
                : new BigDecimal(tbBusinessBalance.getTbFixEntrust().getRemindMaintainKilo())
                        .divide(new BigDecimal("1.00"), 0, BigDecimal.ROUND_HALF_UP) + "");

        row = sheet.getRow(77);

        cell = row.getCell(30);

        // cell.setCellStyle(styleCenter);

        cell.setCellValue(tbBusinessBalance.getTbFixEntrust().getRemark());

        ByteArrayOutputStream byteArrayOutImgLion = new ByteArrayOutputStream();

        String pic = "";

        if ("dfbz".equals(companyName)) {
            pic = "/lion_jsd.png";
        } else if ("xtl".equals(companyName)) {

            pic = "/xtl_jsd.png";
        }

        BufferedImage bufferImgLion = ImageIO.read(this.getClass().getResourceAsStream(pic));

        ImageIO.write(bufferImgLion, "png", byteArrayOutImgLion);

        HSSFPatriarch patriarch = sheet.createDrawingPatriarch();

        HSSFClientAnchor anchorLion = new HSSFClientAnchor(0, 0, 1023, 200, (short) 41, 0, (short) 46, 7);

        patriarch.createPicture(anchorLion,
                workbook.addPicture(byteArrayOutImgLion.toByteArray(), HSSFWorkbook.PICTURE_TYPE_PNG));

        /*
         * int maxSize = 19;
         * 
         * int maxFixSize = (fixSize > 19 ? 19 : fixSize);
         * 
         * int maxPartSize = maxSize - maxFixSize > partSize ? partSize :
         * maxSize - maxFixSize;
         * 
         * int partStartRow = (fixSize == 0 ? 21 : 21 + maxFixSize * 2 + 4);
         * 
         * int partStartRowClone = ((fixSize - maxFixSize) == 0 ? 21 : 21 +
         * (fixSize - maxFixSize) * 2 + 4);
         * 
         * int minBlank = 9;
         * 
         * if ((fixSize + partSize) > 17) {
         * 
         * HSSFSheet sheetClone = workbook.cloneSheet(0);
         * 
         * HSSFRow rowClone = sheetClone.getRow(87);
         * 
         * HSSFCell cellClone = rowClone.getCell(34);
         * 
         * cellClone.setCellValue("2  2  ");
         * 
         * HSSFPatriarch patriarchClone = sheetClone
         * .createDrawingPatriarch();
         * 
         * HSSFClientAnchor anchorLionClone = new HSSFClientAnchor(0, 0,
         * 1023, 200, (short) 41, 0, (short) 47, 8);
         * 
         * patriarchClone.createPicture(anchorLionClone, workbook
         * .addPicture(byteArrayOutImgLion.toByteArray(),
         * HSSFWorkbook.PICTURE_TYPE_PNG));
         * 
         * if ((fixSize - maxFixSize) > 0) {
         * 
         * 
         * sheetClone.addMergedRegion(new Region(21, (short) 0, 21, (short)
         * 49));
         * 
         * sheetClone.addMergedRegion(new Region(21, (short) 0, 21, (short)
         * 0));
         * 
         * 
         * sheetClone.addMergedRegion(new CellRangeAddress(21, 21, 0, 49));
         * 
         * rowClone = sheetClone.getRow(21);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(style);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * rowClone = sheetClone.getRow(21);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * rowClone = sheetClone.getRow(24);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * rowClone = sheetClone.getRow(23);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cellClone.setCellValue("??");
         * 
         * cellClone = rowClone.getCell(2);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(10);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(21);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(24);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(30);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(34);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(44);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * for (int i = 0; i < (fixSize - maxFixSize); i++) {
         * 
         * TbFixEntrustContent tbFixEntrustContent =
         * tbFixEntrustContentListAdd .get(i + maxFixSize);
         * 
         * rowClone = sheetClone.getRow(25 + i * 2);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(i + 1);
         * 
         * cellClone = rowClone.getCell(2);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbFixEntrustContent .getStationCode());
         * 
         * cellClone = rowClone.getCell(10);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbFixEntrustContent .getStationName());
         * 
         * cellClone = rowClone.getCell(21);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbFixEntrustContent.getZl());
         * 
         * cellClone = rowClone.getCell(24);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbFixEntrustContent .getProjectType());
         * 
         * cellClone = rowClone.getCell(30);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(
         * tbFixEntrustContent.getFixHour()).divide( new BigDecimal("1.00"),
         * 2, BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(34);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(
         * tbFixEntrustContent.getWorkingHourPrice()) .divide(new
         * BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(tbBusinessBalance
         * .getWorkingHourFavourRate()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(44);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(
         * tbFixEntrustContent.getFixHourAll()).divide( new
         * BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString()); }
         * 
         * }
         * 
         * if ((partSize - maxPartSize) > 0) {
         * 
         * 
         * sheetClone.addMergedRegion(new Region(partStartRowClone, (short)
         * 0, partStartRowClone, (short) 49));
         * 
         * sheetClone.addMergedRegion(new Region(partStartRowClone, (short)
         * 0, partStartRowClone, (short) 49));
         * 
         * 
         * sheetClone.addMergedRegion(new CellRangeAddress(
         * partStartRowClone, partStartRowClone, 0, 49));
         * 
         * rowClone = sheetClone.getRow(partStartRowClone);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(style);
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * cellClone.setCellValue("");
         * 
         * rowClone = sheetClone.getRow(partStartRowClone + 1);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * rowClone = sheetClone.getRow(partStartRowClone + 2 + 1);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * rowClone = sheetClone.getRow(partStartRowClone + 2);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cellClone.setCellValue("??");
         * 
         * cellClone = rowClone.getCell(2);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(10);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("??");
         * 
         * cellClone = rowClone.getCell(21);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(24);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(30);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(34);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(44);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * for (int i = 0; i < (partSize - maxPartSize); i++) {
         * 
         * TbMaintianVo tbMaintianVo = maintianvosAdd.get(i + maxPartSize);
         * 
         * rowClone = sheetClone.getRow(partStartRowClone + 4 + i 2);
         * 
         * if(null == rowClone) break;
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(i + 1);
         * 
         * cellClone = rowClone.getCell(2);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbMaintianVo.getPartCode());
         * 
         * cellClone = rowClone.getCell(10);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbMaintianVo.getPartName());
         * 
         * cellClone = rowClone.getCell(21);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbMaintianVo.getZl());
         * 
         * cellClone = rowClone.getCell(24);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(tbMaintianVo.getProjectType());
         * 
         * cellClone = rowClone.getCell(30);
         * 
         * cellClone.setCellStyle(styleRight);
         * 
         * cellClone.setCellValue(new BigDecimal(tbMaintianVo
         * .getPartQuantity()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(34);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(tbMaintianVo
         * .getPrice()).divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * // .setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue(new BigDecimal(tbBusinessBalance
         * .getFixPartFavourRate()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cellClone = rowClone.getCell(44);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone .setCellValue(new BigDecimal(tbMaintianVo
         * .getPartQuantity()) .multiply( new BigDecimal(tbMaintianVo
         * .getPrice())) .divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP) .toString()); }
         * 
         * }
         * 
         * int attachStartRowClone = 21;
         * 
         * if ((fixSize - maxFixSize + partSize - maxPartSize) <= 17) {
         * 
         * if ((fixSize - maxFixSize) != 0 && (partSize - maxPartSize) == 0
         * && (fixSize - maxFixSize) < 18) {
         * 
         * attachStartRowClone = 21 + 2 + 2 + (fixSize - maxFixSize) * 2;
         * 
         * }
         * 
         * if ((partSize - maxPartSize) != 0 && (fixSize - maxFixSize) == 0
         * && (partSize - maxPartSize) < 18) {
         * 
         * attachStartRowClone = 21 + 2 + 2 + 2 + (partSize - maxPartSize) *
         * 2; }
         * 
         * if ((fixSize - maxFixSize) != 0 && (partSize - maxPartSize) != 0
         * && (fixSize - maxFixSize + partSize - maxPartSize < 18)) {
         * 
         * attachStartRowClone = 21 + 2 + 2 + 2 + 2 + (fixSize - maxFixSize)
         * * 2 + (partSize - maxPartSize) * 2;
         * 
         * }
         * 
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone,
         * (short) 0, attachStartRowClone, (short) 49));
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone,
         * (short) 0, attachStartRowClone, (short) 49));
         * 
         * 
         * sheetClone.addMergedRegion(new CellRangeAddress(
         * attachStartRowClone, attachStartRowClone, 0, 49));
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(style);
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * cellClone.setCellValue("");
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone + 1);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeft);
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone + 2 + 1);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone + 2);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cellClone.setCellValue("??");
         * 
         * if ("dfbz".equals(companyName)) {
         * 
         * cellClone = rowClone.getCell(10);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("??");
         * 
         * cellClone = rowClone.getCell(21);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(24);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(30);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(34);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("?");
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * } else if ("xtl".equals(companyName)) {
         * 
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone + 2,
         * (short) 2, attachStartRowClone + 2, (short) 39));
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone + 2,
         * (short) 2, attachStartRowClone + 2, (short) 39));
         * 
         * 
         * sheetClone.addMergedRegion(new CellRangeAddress(
         * attachStartRowClone + 2, attachStartRowClone + 2, 2, 39));
         * 
         * cellClone = rowClone.getCell(2);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * }
         * 
         * cellClone = rowClone.getCell(40);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * cellClone = rowClone.getCell(44);
         * 
         * cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellValue("");
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone + 4);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * // cellClone.setCellStyle(styleCenter);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cellClone.setCellValue("1");
         * 
         * rowClone = sheetClone.getRow(attachStartRowClone + 4 + 1);
         * 
         * cellClone = rowClone.getCell(0);
         * 
         * cellClone.setCellStyle(styleBorderThickLeftCenter);
         * 
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone + 6,
         * (short) 0, 66, (short) 49));
         * 
         * sheetClone.addMergedRegion(new Region(attachStartRowClone + 6,
         * (short) 0, 66, (short) 49));
         * 
         * 
         * sheetClone.addMergedRegion(new CellRangeAddress(
         * attachStartRowClone + 6, 66, 0, 49));
         * 
         * }
         * 
         * } else {
         * 
         * row = sheet.getRow(87);
         * 
         * cell = row.getCell(34);
         * 
         * cell.setCellValue(" 1   1  ");
         * 
         * }
         * 
         * if (fixSize > 0) {
         * 
         * 
         * sheet.addMergedRegion(new Region(21, (short) 0, 21, (short) 49));
         * 
         * sheet.addMergedRegion(new Region(21, (short) 0, 21, (short) 49));
         * 
         * 
         * sheet.addMergedRegion(new CellRangeAddress(21, 21, 0, 49));
         * 
         * row = sheet.getRow(21);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(style);
         * 
         * cell.setCellValue("");
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(22);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(24);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(23);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("??");
         * 
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cell = row.getCell(2);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(10);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(21);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(30);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(34);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(40);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(44);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * for (int i = 0; i < maxFixSize; i++) {
         * 
         * TbFixEntrustContent tbFixEntrustContent =
         * tbFixEntrustContentListAdd .get(i);
         * 
         * row = sheet.getRow(25 + i * 2);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(i + 1);
         * 
         * cell = row.getCell(2);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbFixEntrustContent.getStationCode());
         * 
         * cell = row.getCell(10);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbFixEntrustContent.getStationName());
         * 
         * cell = row.getCell(21);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbFixEntrustContent.getZl());
         * 
         * cell = row.getCell(24);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbFixEntrustContent.getProjectType());
         * 
         * cell = row.getCell(30);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal(tbFixEntrustContent
         * .getFixHour()).divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(34);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal(tbFixEntrustContent
         * .getWorkingHourPrice()) .divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(40);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal((1 - tbBusinessBalance
         * .getWorkingHourFavourRate()) * 100) .divide(new
         * BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(44);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal(tbFixEntrustContent
         * .getFixHourAll()).divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString()); }
         * 
         * }
         * 
         * if ((fixSize <= maxSize - 1) && partSize > 0) {
         * 
         * 
         * sheet.addMergedRegion(new Region(partStartRow, (short) 0,
         * partStartRow, (short) 49));
         * 
         * sheet.addMergedRegion(new Region(partStartRow, (short) 0,
         * partStartRow, (short) 49));
         * 
         * 
         * sheet.addMergedRegion(new CellRangeAddress(partStartRow,
         * partStartRow, 0, 49));
         * 
         * row = sheet.getRow(partStartRow);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(style);
         * 
         * cell.setCellValue("");
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(partStartRow + 1);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(partStartRow + 2 + 1);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * row = sheet.getRow(partStartRow + 2);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cell.setCellValue("??");
         * 
         * cell = row.getCell(2);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(10);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("??");
         * 
         * cell = row.getCell(21);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(30);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(34);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(40);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(44);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * if (partSize > 0) {
         * 
         * for (int i = 0; i < maxPartSize; i++) {
         * 
         * TbMaintianVo tbMaintianVo = maintianvosAdd.get(i);
         * 
         * row = sheet.getRow(partStartRow + 4 + i * 2);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(i + 1);
         * 
         * cell = row.getCell(2);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbMaintianVo.getPartCode());
         * 
         * cell = row.getCell(10);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbMaintianVo.getPartName());
         * 
         * cell = row.getCell(21);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbMaintianVo.getZl());
         * 
         * cell = row.getCell(24);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(tbMaintianVo.getProjectType());
         * 
         * cell = row.getCell(30);
         * 
         * cell.setCellStyle(styleRight);
         * 
         * cell.setCellValue(new BigDecimal(tbMaintianVo
         * .getPartQuantity()).divide( new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(34);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal(tbMaintianVo
         * .getPrice()).divide(new BigDecimal("1.00"), 2,
         * BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(40);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal((1 - tbBusinessBalance
         * .getFixPartFavourRate()) * 100).divide( new BigDecimal("1.00"),
         * 2, BigDecimal.ROUND_HALF_UP).toString());
         * 
         * cell = row.getCell(44);
         * 
         * // cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue(new BigDecimal(tbMaintianVo .getPartQuantity())
         * .multiply( new BigDecimal(tbMaintianVo.getPrice())) .divide(new
         * BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString()); }
         * 
         * }
         * 
         * }
         * 
         * int attachStartRow = 21;
         * 
         * if ((fixSize + partSize) <= 17) {
         * 
         * if (fixSize != 0 && partSize == 0 && fixSize < 18) {
         * 
         * attachStartRow = 19 + 2 + 2 + 2 + fixSize * 2;
         * 
         * }
         * 
         * if (partSize != 0 && fixSize == 0 && partSize < 18) {
         * 
         * attachStartRow = 19 + 2 + 2 + 2 + partSize * 2; }
         * 
         * if (fixSize != 0 && partSize != 0 && (fixSize + partSize < 18)) {
         * 
         * attachStartRow = 19 + 2 + 2 + 2 + 2 + 2 + fixSize * 2 + partSize
         * * 2;
         * 
         * }
         * 
         * 
         * sheet.addMergedRegion(new Region(attachStartRow, (short) 0,
         * attachStartRow, (short) 49));
         * 
         * sheet.addMergedRegion(new Region(attachStartRow, (short) 0,
         * attachStartRow, (short) 49));
         * 
         * 
         * sheet.addMergedRegion(new CellRangeAddress(attachStartRow,
         * attachStartRow, 0, 49));
         * 
         * row = sheet.getRow(attachStartRow);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(style);
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * cell.setCellValue("");
         * 
         * row = sheet.getRow(attachStartRow + 1);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeft);
         * 
         * row = sheet.getRow(attachStartRow + 2 + 1);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * row = sheet.getRow(attachStartRow + 2);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(styleCenter);
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cell.setCellValue("??");
         * 
         * if ("dfbz".equals(companyName)) {
         * 
         * cell = row.getCell(10);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("??");
         * 
         * cell = row.getCell(21);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(24);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(30);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(34);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("?");
         * 
         * cell = row.getCell(40);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * } else if ("xtl".equals(companyName)) {
         * 
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 2, (short) 2,
         * attachStartRow + 2, (short) 39));
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 2, (short) 2,
         * attachStartRow + 2, (short) 39));
         * 
         * 
         * sheet.addMergedRegion(new CellRangeAddress( attachStartRow + 2,
         * attachStartRow + 2, 2, 39));
         * 
         * cell = row.getCell(2);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * }
         * 
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 2, (short) 2,
         * attachStartRow + 2, (short) 39));
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 2, (short) 2,
         * attachStartRow + 2, (short) 39));
         * 
         * cell = row.getCell(2);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * 
         * cell = row.getCell(40);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * cell = row.getCell(44);
         * 
         * cell.setCellStyle(styleCenter);
         * 
         * cell.setCellValue("");
         * 
         * row = sheet.getRow(attachStartRow + 4);
         * 
         * cell = row.getCell(0);
         * 
         * // cell.setCellStyle(styleCenter);
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * cell.setCellValue("1");
         * 
         * row = sheet.getRow(attachStartRow + 4 + 1);
         * 
         * cell = row.getCell(0);
         * 
         * cell.setCellStyle(styleBorderThickLeftCenter);
         * 
         * if ("xtl".equals(companyName)) {
         * 
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 6, (short) 0,
         * 66, (short) 49));
         * 
         * sheet.addMergedRegion(new Region(attachStartRow + 6, (short) 0,
         * 66, (short) 49));
         * 
         * if (attachStartRow + 6 < 66) {
         * 
         * sheet.addMergedRegion(new CellRangeAddress( attachStartRow + 6,
         * 66, 0, 49)); }
         * 
         * } }
         */

        int pageSize = 0;

        if ((fixSize > 0 && partSize == 0) || (partSize > 0 && fixSize == 0)) {

            if ((fixSize + partSize) <= 18) {

                pageSize = 1;

            }

            else {

                pageSize = (fixSize + partSize - 19) / 21 + 2;

            }

            for (int p = pageSize - 1; p >= 0; p--) {

                int maxSize = (p == pageSize - 1) ? (fixSize + partSize - p * 21) : 21;

                int startRow = 21;

                if (p == 0) {

                    sheet = workbook.getSheetAt(0);

                    for (int k = 1; k <= pageSize / 2; k++) {

                        workbook.setSheetOrder(workbook.getSheetName(k), pageSize - k);

                    }

                    for (int k = 1; k < pageSize; k++) {

                        workbook.setSheetName(k, "" + (k + 1) + "");

                    }

                    workbook.setSheetName(0, "" + 1 + "");

                } else {
                    sheet = workbook.cloneSheet(0);

                    //this.clearSheet(sheet, 21, 0, 66, 49);
                }

                if (fixSize > 0) {

                    sheet.addMergedRegion(new CellRangeAddress(21, 21, 0, 49));

                    row = sheet.getRow(21);

                    cell = row.getCell(0);

                    cell.setCellValue("");

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(22);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(24);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(23);

                    cell = row.getCell(0);

                    cell.setCellValue("??");

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell = row.getCell(2);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(10);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(21);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(24);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(30);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(34);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    for (int i = 0; i < maxSize; i++) {

                        TbFixEntrustContent tbFixEntrustContent = tbFixEntrustContentListAdd.get(p * 21 + i);

                        row = sheet.getRow(25 + i * 2);

                        cell = row.getCell(0);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(p * 21 + i + 1);

                        cell = row.getCell(2);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getStationCode());

                        cell = row.getCell(10);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getStationName());

                        cell = row.getCell(21);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getZl());

                        cell = row.getCell(24);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getProjectType());

                        cell = row.getCell(30);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getFixHour())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(34);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getWorkingHourPrice())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(40);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(
                                new BigDecimal((1 - tbBusinessBalance.getWorkingHourFavourRate()) * 100)
                                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());

                        cell = row.getCell(44);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getFixHourAll())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
                    }

                }

                else if (partSize > 0) {

                    sheet.addMergedRegion(new CellRangeAddress(startRow, startRow, 0, 49));

                    row = sheet.getRow(startRow);

                    cell = row.getCell(0);

                    // cell.setCellStyle(style);

                    cell.setCellValue("");

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(startRow + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(startRow + 2 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    row = sheet.getRow(startRow + 2);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("??");

                    cell = row.getCell(2);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(10);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("??");

                    cell = row.getCell(21);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(24);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(30);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(34);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    for (int i = 0; i < maxSize; i++) {

                        TbMaintianVo tbMaintianVo = maintianvosAdd.get(p * 21 + i);

                        row = sheet.getRow(startRow + 4 + i * 2);

                        cell = row.getCell(0);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(p * 21 + i + 1);

                        cell = row.getCell(2);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getPartCode());

                        cell = row.getCell(10);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getPartName());

                        cell = row.getCell(21);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getZl());

                        cell = row.getCell(24);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getProjectType());

                        cell = row.getCell(30);

                        cell.setCellStyle(styleRight);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPartQuantity())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(34);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPrice())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(40);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal((1 - tbBusinessBalance.getFixPartFavourRate()) * 100)
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(44);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPartQuantity())
                                .multiply(new BigDecimal(tbMaintianVo.getPrice()))
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
                    }

                }

                row = sheet.getRow(87);

                cell = row.getCell(34);

                cell.setCellValue(" " + (p + 1) + "    " + pageSize + "  ");

                if (p == pageSize - 1) {

                    int attachStartRow = 21 + 2 + 2 + (fixSize + partSize - (pageSize - 1) * 21) * 2;

                    sheet.addMergedRegion(new CellRangeAddress(attachStartRow, attachStartRow, 0, 49));

                    row = sheet.getRow(attachStartRow);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    cell.setCellValue("");

                    row = sheet.getRow(attachStartRow + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(attachStartRow + 2 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    row = sheet.getRow(attachStartRow + 2);

                    cell = row.getCell(0);

                    // cell.setCellStyle(styleCenter);
                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("??");

                    if ("dfbz".equals(companyName)) {

                        cell = row.getCell(10);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("??");

                        cell = row.getCell(21);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(24);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("");

                        cell = row.getCell(30);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(34);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(40);

                        cell.setCellStyle(styleCenter);

                    } else if ("xtl".equals(companyName)) {

                        cell = row.getCell(2);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("");

                    }

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    row = sheet.getRow(attachStartRow + 4);

                    cell = row.getCell(0);

                    // cell.setCellStyle(styleCenter);
                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("1");

                    row = sheet.getRow(attachStartRow + 4 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    if ("xtl".equals(companyName)) {

                        if (attachStartRow + 6 < 66) {

                            sheet.addMergedRegion(new CellRangeAddress(attachStartRow + 6, 66, 0, 49));
                        }

                    }

                }

            }

        }

        if (fixSize > 0 && partSize > 0) {

            if ((fixSize + partSize) <= 16) {

                pageSize = 1;

            }

            else {

                pageSize = (fixSize + partSize - 17) / 19 + 2;

            }

            int fixPageSize = fixSize / pageSize;

            int partPageSize = partSize / pageSize;

            int fixLastPageSize = fixSize - (pageSize - 1) * fixPageSize;

            int partLastPageSize = partSize - (pageSize - 1) * partPageSize;

            if ((fixPageSize + partPageSize) < 19 && fixPageSize != 0) {

                //if(fixLastPageSize >= partSize){
                if (fixPageSize >= partPageSize) {

                    fixPageSize = (19 - partPageSize) > fixSize ? fixSize : (19 - partPageSize);

                }

                else {

                    partPageSize = (19 - fixPageSize) > partSize ? partSize : (19 - fixPageSize);

                }

            }

            fixLastPageSize = fixSize - (pageSize - 1) * fixPageSize;

            partLastPageSize = (partSize - (pageSize - 1) * partPageSize) < 0 ? 0
                    : (partSize - (pageSize - 1) * partPageSize);

            for (int p = pageSize - 1; p >= 0; p--) {

                int partStartRow = 21 + 2 + 2 + ((p == pageSize - 1) ? fixLastPageSize : fixPageSize) * 2;

                if (p == 0) {

                    sheet = workbook.getSheetAt(0);

                    if (pageSize > 1) {

                        for (int k = 1; k <= pageSize / 2; k++) {

                            workbook.setSheetOrder(workbook.getSheetName(k), pageSize - k);

                        }

                        for (int k = 1; k < pageSize; k++) {

                            workbook.setSheetName(k, "" + (k + 1) + "");

                        }
                    }

                    workbook.setSheetName(0, "" + 1 + "");

                } else {

                    sheet = workbook.cloneSheet(0);

                }

                if (fixSize > 0) {

                    sheet.addMergedRegion(new CellRangeAddress(21, 21, 0, 49));

                    row = sheet.getRow(21);

                    cell = row.getCell(0);

                    cell.setCellValue("");

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(22);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(24);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(23);

                    cell = row.getCell(0);

                    cell.setCellValue("??");

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell = row.getCell(2);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(10);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(21);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(24);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(30);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(34);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    for (int i = 0; i < ((p == pageSize - 1) ? fixLastPageSize : fixPageSize); i++) {

                        TbFixEntrustContent tbFixEntrustContent = tbFixEntrustContentListAdd
                                .get(p * fixPageSize + i);

                        row = sheet.getRow(25 + i * 2);

                        cell = row.getCell(0);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(p * fixPageSize + i + 1);

                        cell = row.getCell(2);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getStationCode());

                        cell = row.getCell(10);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getStationName());

                        cell = row.getCell(21);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getZl());

                        cell = row.getCell(24);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbFixEntrustContent.getProjectType());

                        cell = row.getCell(30);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getFixHour())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(34);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getWorkingHourPrice())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(40);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(
                                new BigDecimal((1 - tbBusinessBalance.getWorkingHourFavourRate()) * 100)
                                        .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP)
                                        .toString());

                        cell = row.getCell(44);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbFixEntrustContent.getFixHourAll())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
                    }

                }

                if (partSize > 0) {

                    sheet.addMergedRegion(new CellRangeAddress(partStartRow, partStartRow, 0, 49));

                    row = sheet.getRow(partStartRow);

                    cell = row.getCell(0);

                    // cell.setCellStyle(style);

                    cell.setCellValue("");

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(partStartRow + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(partStartRow + 2 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    row = sheet.getRow(partStartRow + 2);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("??");

                    cell = row.getCell(2);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(10);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("??");

                    cell = row.getCell(21);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(24);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(30);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(34);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("?");

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    for (int i = 0; i < ((p == pageSize - 1) ? partLastPageSize : partPageSize); i++) {

                        if (p * partPageSize + i > partSize - 1) {

                            break;
                        }

                        TbMaintianVo tbMaintianVo = maintianvosAdd.get(p * partPageSize + i);

                        row = sheet.getRow(partStartRow + 4 + i * 2);

                        cell = row.getCell(0);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(p * partPageSize + i + 1);

                        cell = row.getCell(2);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getPartCode());

                        cell = row.getCell(10);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getPartName());

                        cell = row.getCell(21);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getZl());

                        cell = row.getCell(24);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(tbMaintianVo.getProjectType());

                        cell = row.getCell(30);

                        cell.setCellStyle(styleRight);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPartQuantity())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(34);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPrice())
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(40);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal((1 - tbBusinessBalance.getFixPartFavourRate()) * 100)
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());

                        cell = row.getCell(44);

                        // cell.setCellStyle(styleCenter);

                        cell.setCellValue(new BigDecimal(tbMaintianVo.getPartQuantity())
                                .multiply(new BigDecimal(tbMaintianVo.getPrice()))
                                .divide(new BigDecimal("1.00"), 2, BigDecimal.ROUND_HALF_UP).toString());
                    }

                }

                row = sheet.getRow(87);

                cell = row.getCell(34);

                cell.setCellValue(" " + (p + 1) + "    " + pageSize + "  ");

                if (p == pageSize - 1) {

                    int attachStartRow = 21 + 2 + 2 + 2 + 2 + (fixLastPageSize + partLastPageSize) * 2;

                    sheet.addMergedRegion(new CellRangeAddress(attachStartRow, attachStartRow, 0, 49));

                    row = sheet.getRow(attachStartRow);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    cell.setCellValue("");

                    row = sheet.getRow(attachStartRow + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeft);

                    row = sheet.getRow(attachStartRow + 2 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    row = sheet.getRow(attachStartRow + 2);

                    cell = row.getCell(0);

                    // cell.setCellStyle(styleCenter);
                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("??");

                    if ("dfbz".equals(companyName)) {

                        cell = row.getCell(10);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("??");

                        cell = row.getCell(21);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(24);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("");

                        cell = row.getCell(30);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(34);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("?");

                        cell = row.getCell(40);

                        cell.setCellStyle(styleCenter);

                    } else if ("xtl".equals(companyName)) {

                        cell = row.getCell(2);

                        cell.setCellStyle(styleCenter);

                        cell.setCellValue("");

                    }

                    cell = row.getCell(40);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    cell = row.getCell(44);

                    cell.setCellStyle(styleCenter);

                    cell.setCellValue("");

                    row = sheet.getRow(attachStartRow + 4);

                    cell = row.getCell(0);

                    // cell.setCellStyle(styleCenter);
                    cell.setCellStyle(styleBorderThickLeftCenter);

                    cell.setCellValue("1");

                    row = sheet.getRow(attachStartRow + 4 + 1);

                    cell = row.getCell(0);

                    cell.setCellStyle(styleBorderThickLeftCenter);

                    if ("xtl".equals(companyName)) {

                        if (attachStartRow + 6 < 66) {

                            sheet.addMergedRegion(new CellRangeAddress(attachStartRow + 6, 66, 0, 49));
                        }

                    }

                }

            }

        }

        workbook.write(os);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // 8 41

}