Example usage for java.math BigDecimal equals

List of usage examples for java.math BigDecimal equals

Introduction

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

Prototype

@Override
public boolean equals(Object x) 

Source Link

Document

Compares this BigDecimal with the specified Object for equality.

Usage

From source file:org.kuali.kpme.tklm.leave.accrual.service.AccrualServiceImpl.java

@Override
public RateRangeAggregate buildRateRangeAggregate(String principalId, DateTime startDate, DateTime endDate) {
    RateRangeAggregate rrAggregate = new RateRangeAggregate();
    List<RateRange> rateRangeList = new ArrayList<RateRange>();
    // get all active jobs that are effective before the endDate
    List<Job> activeJobs = HrServiceLocator.getJobService().getAllActiveLeaveJobs(principalId,
            endDate.toLocalDate());/*from   www .  ja  va  2 s  .  c  om*/
    List<Job> inactiveJobs = HrServiceLocator.getJobService().getAllInActiveLeaveJobsInRange(principalId,
            endDate.toLocalDate());

    List<PrincipalHRAttributes> phaList = HrServiceLocator.getPrincipalHRAttributeService()
            .getAllActivePrincipalHrAttributesForPrincipalId(principalId, endDate.toLocalDate());
    List<PrincipalHRAttributes> inactivePhaList = HrServiceLocator.getPrincipalHRAttributeService()
            .getAllInActivePrincipalHrAttributesForPrincipalId(principalId, endDate.toLocalDate());

    if (activeJobs.isEmpty() || phaList.isEmpty()) {
        return rrAggregate;
    }

    Set<String> phaLpSet = new HashSet<String>();
    Set<String> calNameSet = new HashSet<String>();
    if (CollectionUtils.isNotEmpty(phaList)) {
        for (PrincipalHRAttributes pha : phaList) {
            phaLpSet.add(pha.getLeavePlan());
            calNameSet.add(pha.getPayCalendar());
        }
    }

    List<LeavePlan> activeLpList = new ArrayList<LeavePlan>();
    List<LeavePlan> inactiveLpList = new ArrayList<LeavePlan>();
    for (String lpString : phaLpSet) {
        List<LeavePlan> aList = HrServiceLocator.getLeavePlanService().getAllActiveLeavePlan(lpString,
                endDate.toLocalDate());
        activeLpList.addAll(aList);

        aList = HrServiceLocator.getLeavePlanService().getAllInActiveLeavePlan(lpString, endDate.toLocalDate());
        inactiveLpList.addAll(aList);
    }

    // get all pay calendar entries for this employee. used to determine interval dates
    Map<String, List<CalendarEntry>> calEntryMap = new HashMap<String, List<CalendarEntry>>();
    for (String calName : calNameSet) {
        Calendar aCal = HrServiceLocator.getCalendarService().getCalendarByGroup(calName);
        if (aCal != null) {
            List<CalendarEntry> aList = HrServiceLocator.getCalendarEntryService()
                    .getAllCalendarEntriesForCalendarId(aCal.getHrCalendarId());
            Collections.sort(aList);
            calEntryMap.put(calName, aList);
        }
    }
    rrAggregate.setCalEntryMap(calEntryMap);

    Set<String> lpStringSet = new HashSet<String>();
    if (CollectionUtils.isNotEmpty(activeLpList)) {
        for (LeavePlan lp : activeLpList) {
            lpStringSet.add(lp.getLeavePlan());
        }
    }
    List<SystemScheduledTimeOffContract> sstoList = new ArrayList<SystemScheduledTimeOffContract>();
    for (String lpString : lpStringSet) {
        List<? extends SystemScheduledTimeOffContract> aList = LmServiceLocator.getSysSchTimeOffService()
                .getSystemScheduledTimeOffsForLeavePlan(startDate.toLocalDate(), endDate.toLocalDate(),
                        lpString);
        if (CollectionUtils.isNotEmpty(aList)) {
            sstoList.addAll(aList);
        }
    }

    List<AccrualCategory> activeAccrCatList = new ArrayList<AccrualCategory>();
    List<AccrualCategory> inactiveAccrCatList = new ArrayList<AccrualCategory>();
    for (String lpString : lpStringSet) {
        List<AccrualCategory> aList = HrServiceLocator.getAccrualCategoryService()
                .getActiveLeaveAccrualCategoriesForLeavePlan(lpString, endDate.toLocalDate());
        if (CollectionUtils.isNotEmpty(aList)) {
            activeAccrCatList.addAll(aList);
        }

        aList = HrServiceLocator.getAccrualCategoryService()
                .getInActiveLeaveAccrualCategoriesForLeavePlan(lpString, endDate.toLocalDate());
        if (CollectionUtils.isNotEmpty(aList)) {
            inactiveAccrCatList.addAll(aList);
        }
    }

    List<AccrualCategoryRule> activeRuleList = new ArrayList<AccrualCategoryRule>();
    List<AccrualCategoryRule> inactiveRuleList = new ArrayList<AccrualCategoryRule>();
    for (AccrualCategory ac : activeAccrCatList) {
        List<AccrualCategoryRule> aRuleList = HrServiceLocator.getAccrualCategoryRuleService()
                .getActiveRulesForAccrualCategoryId(ac.getLmAccrualCategoryId());
        activeRuleList.addAll(aRuleList);

        aRuleList = HrServiceLocator.getAccrualCategoryRuleService()
                .getInActiveRulesForAccrualCategoryId(ac.getLmAccrualCategoryId());
        inactiveRuleList.addAll(aRuleList);
    }

    List<LeaveCalendarDocumentHeader> lcDocList = LmServiceLocator.getLeaveCalendarDocumentHeaderService()
            .getAllDocumentHeadersInRangeForPricipalId(principalId, startDate, endDate);

    BigDecimal previousFte = null;
    List<Job> jobs;

    DateTime currentDate = startDate;
    while (!currentDate.isAfter(endDate)) {
        RateRange rateRange = new RateRange();

        jobs = this.getJobsForDate(activeJobs, inactiveJobs, currentDate.toLocalDate());
        if (jobs.isEmpty()) { // no jobs found for this day
            currentDate = currentDate.plusDays(1);
            continue;
        }
        rateRange.setJobs(jobs);

        // detect if there's a status change
        BigDecimal fteSum = HrServiceLocator.getJobService().getFteSumForJobs(jobs);
        rateRange.setAccrualRatePercentageModifier(fteSum);
        BigDecimal standardHours = HrServiceLocator.getJobService().getStandardHoursSumForJobs(jobs);
        rateRange.setStandardHours(standardHours);

        if (previousFte != null && !previousFte.equals(fteSum)) {
            rateRange.setStatusChanged(true);
            rrAggregate.setRateRangeChanged(true);
        }
        previousFte = fteSum;

        // figure out the PrincipalHRAttributes for this day
        PrincipalHRAttributes phra = this.getPrincipalHrAttributesForDate(phaList, currentDate.toLocalDate());
        rateRange.setPrincipalHRAttributes(phra);

        if (rateRange.getPrincipalHRAttributes() != null) {
            // figure out if there's an end principalHrAttributes for the initial principalHRAttributes
            PrincipalHRAttributes endPhra = this.getInactivePrincipalHrAttributesForDate(inactivePhaList,
                    rateRange.getPrincipalHRAttributes().getEffectiveLocalDate(), currentDate.toLocalDate());
            rateRange.setEndPrincipalHRAttributes(endPhra);
        }

        // get leave plan for this day
        if (rateRange.getPrincipalHRAttributes() != null) {
            rateRange.setLeavePlan(this.getLeavePlanForDate(activeLpList, inactiveLpList,
                    rateRange.getPrincipalHRAttributes().getLeavePlan(), currentDate.toLocalDate()));
        }

        if (rateRange.getLeavePlan() != null) {
            // get accrual category list for this day
            List<AccrualCategory> acsForDay = this.getAccrualCategoriesForDate(activeAccrCatList,
                    inactiveAccrCatList, rateRange.getLeavePlan().getLeavePlan(), currentDate.toLocalDate());
            rateRange.setAcList(acsForDay);

            // get System scheduled time off for this day
            for (SystemScheduledTimeOffContract ssto : sstoList) {
                if (ssto.getAccruedLocalDate().equals(currentDate.toLocalDate())
                        && ssto.getLeavePlan().equals(rateRange.getLeavePlan().getLeavePlan())) {

                    // figure out the primary leave assignment to use for ssto usage leave blocks
                    if (CollectionUtils.isNotEmpty(jobs)
                            && StringUtils.isBlank(rateRange.getPrimaryLeaveAssignmentId())) {
                        for (Job aJob : jobs) {
                            if (aJob.isEligibleForLeave() && aJob.isPrimaryJob()) {
                                List<Assignment> assignmentList = HrServiceLocator.getAssignmentService()
                                        .getActiveAssignmentsForJob(principalId, aJob.getJobNumber(),
                                                currentDate.toLocalDate());
                                for (Assignment anAssignment : assignmentList) {
                                    if (anAssignment != null && anAssignment.isPrimaryAssign()) {
                                        rateRange.setPrimaryLeaveAssignmentId(anAssignment.getTkAssignmentId());
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    // if there exists a ssto accrualed leave block with this ssto id, it means the ssto hours has been banked or transferred by the employee
                    // this logic depends on the deactivateOldAccruals() runs before buildRateRangeAggregate()
                    // because deactivateOldAccruals() removes accrued ssto leave blocks unless they are banked/transferred
                    List<LeaveBlock> sstoLbList = LmServiceLocator.getLeaveBlockService().getSSTOLeaveBlocks(
                            principalId, ssto.getLmSystemScheduledTimeOffId(), ssto.getAccruedLocalDate());
                    if (CollectionUtils.isEmpty(sstoLbList)) {
                        rateRange.setSysScheTimeOff(ssto);
                    }
                }
            }
        }
        // set accrual category rules for the day
        if (CollectionUtils.isNotEmpty(rateRange.getAcList())) {
            List<AccrualCategoryRule> rulesForDay = new ArrayList<AccrualCategoryRule>();
            for (AccrualCategory ac : rateRange.getAcList()) {
                rulesForDay.addAll(this.getAccrualCategoryRulesForDate(activeRuleList,
                        ac.getLmAccrualCategoryId(), currentDate.toLocalDate(),
                        rateRange.getPrincipalHRAttributes().getServiceLocalDate()));
            }
            rateRange.setAcRuleList(rulesForDay);

        }

        Interval range = new Interval(currentDate, currentDate.plusDays(1));
        rateRange.setRange(range);
        // assign leave document id to range if there is an existing leave doc for currentDate.
        // The doc Id will be assigned to leave blocks created at this rate range
        rateRange.setLeaveCalendarDocumentId(this.getLeaveDocumentForDate(lcDocList, currentDate));
        rateRangeList.add(rateRange);

        currentDate = currentDate.plusDays(1);
    }
    rrAggregate.setRateRanges(rateRangeList);
    rrAggregate.setCurrentRate(null);
    return rrAggregate;
}

From source file:fi.hoski.remote.ui.Admin.java

private Map<String, Object> checkRating(Map<String, Object> map) throws IOException, JSONException {
    String fleet = (String) map.get(RaceEntry.FLEET);
    String nat = (String) map.get(RaceEntry.NAT);
    nat = nat.toUpperCase();//from w  w  w  .  ja v  a  2  s .  com
    Number sailNo = (Number) map.get(RaceEntry.SAILNO);
    String clazz = (String) map.get(RaceEntry.CLASS);
    String entryRatingStr = (String) map.get(RaceEntry.RATING);
    if (entryRatingStr != null) {
        entryRatingStr = entryRatingStr.replace(',', '.');
    } else {
        entryRatingStr = "0.0";
    }
    int sn = 0;
    if (sailNo != null) {
        sn = sailNo.intValue();
    }
    JSONObject json = getRating(fleet, nat, sn, clazz);
    String ratingSystem = json.optString(BoatInfo.RATINGSYSTEM);
    if ("UNKNOWN".equals(ratingSystem)) {
        return map;
    }
    Map<String, Object> m2 = new HashMap<>();
    m2.putAll(map);
    String listedRatingStr = json.optString(RaceEntry.RATING);
    if (listedRatingStr != null && !listedRatingStr.isEmpty()) {
        try {
            BigDecimal listedRating = new BigDecimal(listedRatingStr.replace(',', '.'));
            m2.put(RaceEntry.RATING, listedRating);
            BigDecimal entryRating = new BigDecimal(entryRatingStr.replace(',', '.'));
            if (!entryRating.equals(listedRating)) {
                String privateNotes = TextUtil.getText("RATING DIFFERS");
                privateNotes = String.format(privateNotes, entryRating, listedRating);
                m2.put(RaceEntry.PRIVATENOTES, privateNotes);
            } else {
                String privateNotes = TextUtil.getText("RATING OK");
                m2.put(RaceEntry.PRIVATENOTES, privateNotes);
            }
        } catch (NumberFormatException ex) {
            String privateNotes = TextUtil.getText("RATING DIFFERS");
            privateNotes = String.format(privateNotes, entryRatingStr, listedRatingStr);
            m2.put(RaceEntry.PRIVATENOTES, privateNotes);
        }
    } else {
        String privateNotes = TextUtil.getText("NO RATING");
        m2.put(RaceEntry.PRIVATENOTES, privateNotes);
    }
    return m2;
}

From source file:pe.gob.mef.gescon.web.ui.WikiMB.java

public void onListTipoConocimientoChange(AjaxBehaviorEvent event) {
    try {//from w  w w  . j a  v  a2 s.c  om
        if (event != null) {
            final BigDecimal id = (BigDecimal) ((SelectOneMenu) event.getSource()).getValue();
            this.setIdTipoConocimiento(id);
            if (id != null) {
                HashMap filters = new HashMap();
                filters.put("ntipoconocimientoid", id);
                ConocimientoService service = (ConocimientoService) ServiceFinder
                        .findBean("ConocimientoService");
                if (this.getSelectedWiki() != null) {
                    filters.put("nconocimientoid", this.getSelectedWiki().getNconocimientoid().toString());
                    this.setListaTargetVinculos(new ArrayList());
                    List<Consulta> lista = service.getConcimientosVinculados(filters);
                    Collections.sort(lista, Consulta.Comparators.ID);
                    if (id.equals(Constante.BASELEGAL)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBL(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBL().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosPR(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosPR().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosWK(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosWK().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosCT(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosCT().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBP(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBP().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosOM(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosOM().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosOM());
                    }
                } else {
                    if (id.equals(Constante.BASELEGAL)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosOM());
                    }
                }
                if (CollectionUtils.isNotEmpty(this.getListaTargetVinculos())) {
                    List<String> ids = new ArrayList<String>();
                    for (Consulta c : this.getListaTargetVinculos()) {
                        ids.add(c.getIdconocimiento().toString());
                    }
                    String filter = StringUtils.join(ids, ',');
                    if (id.equals(Constante.WIKI)) {
                        filter = filter.concat(",")
                                .concat(this.getSelectedWiki().getNconocimientoid().toString());
                    }
                    filters.put("nconocimientovinc", filter);
                }
                if (id.equals(Constante.BASELEGAL)) {
                    this.setListaSourceVinculosBL(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBL());
                } else if (id.equals(Constante.PREGUNTAS)) {
                    this.setListaSourceVinculosPR(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosPR());
                } else if (id.equals(Constante.WIKI)) {
                    this.setListaSourceVinculosWK(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosWK());
                } else if (id.equals(Constante.CONTENIDO)) {
                    this.setListaSourceVinculosCT(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosCT());
                } else if (id.equals(Constante.BUENAPRACTICA)) {
                    this.setListaSourceVinculosBP(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBP());
                } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                    this.setListaSourceVinculosOM(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosOM());
                }
                this.setPickList(new DualListModel<Consulta>(this.getListaSourceVinculos(),
                        this.getListaTargetVinculos()));
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
}

From source file:com.gst.portfolio.loanaccount.service.LoanWritePlatformServiceJpaRepositoryImpl.java

private void checkIfLoanIsPaidInAdvance(final Long loanId, final BigDecimal transactionAmount) {
    BigDecimal overpaid = this.loanReadPlatformService.retrieveTotalPaidInAdvance(loanId).getPaidInAdvance();

    if (overpaid == null || overpaid.equals(new BigDecimal(0))
            || transactionAmount.floatValue() > overpaid.floatValue()) {
        if (overpaid == null)
            overpaid = BigDecimal.ZERO;
        throw new InvalidPaidInAdvanceAmountException(overpaid.toPlainString());
    }/* w  ww .j a  v a 2  s .c  o m*/
}

From source file:pe.gob.mef.gescon.web.ui.BuenaPracticaMB.java

public void onListTipoConocimientoChange(AjaxBehaviorEvent event) {
    try {//from w  w w. j a va2s.  co  m
        if (event != null) {
            final BigDecimal id = (BigDecimal) ((SelectOneMenu) event.getSource()).getValue();
            this.setIdTipoConocimiento(id);
            if (id != null) {
                HashMap filters = new HashMap();
                filters.put("ntipoconocimientoid", id);
                ConocimientoService service = (ConocimientoService) ServiceFinder
                        .findBean("ConocimientoService");
                if (this.getSelectedBuenaPractica() != null) {
                    filters.put("nconocimientoid",
                            this.getSelectedBuenaPractica().getNconocimientoid().toString());
                    this.setListaTargetVinculos(new ArrayList());
                    List<Consulta> lista = service.getConcimientosVinculados(filters);
                    Collections.sort(lista, Consulta.Comparators.ID);
                    if (id.equals(Constante.BASELEGAL)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBL(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBL().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosPR(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosPR().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosWK(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosWK().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosCT(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosCT().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBP(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBP().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosOM(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosOM().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosOM());
                    }
                } else {
                    if (id.equals(Constante.BASELEGAL)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosOM());
                    }
                }
                if (CollectionUtils.isNotEmpty(this.getListaTargetVinculos())) {
                    List<String> ids = new ArrayList<String>();
                    for (Consulta c : this.getListaTargetVinculos()) {
                        ids.add(c.getIdconocimiento().toString());
                    }
                    String filter = StringUtils.join(ids, ',');
                    if (id.equals(Constante.WIKI)) {
                        filter = filter.concat(",")
                                .concat(this.getSelectedBuenaPractica().getNconocimientoid().toString());
                    }
                    filters.put("nconocimientovinc", filter);
                }
                if (id.equals(Constante.BASELEGAL)) {
                    this.setListaSourceVinculosBL(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBL());
                } else if (id.equals(Constante.PREGUNTAS)) {
                    this.setListaSourceVinculosPR(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosPR());
                } else if (id.equals(Constante.WIKI)) {
                    this.setListaSourceVinculosWK(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosWK());
                } else if (id.equals(Constante.CONTENIDO)) {
                    this.setListaSourceVinculosCT(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosCT());
                } else if (id.equals(Constante.BUENAPRACTICA)) {
                    this.setListaSourceVinculosBP(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBP());
                } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                    this.setListaSourceVinculosOM(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosOM());
                }
                this.setPickList(new DualListModel<Consulta>(this.getListaSourceVinculos(),
                        this.getListaTargetVinculos()));
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
}

From source file:pe.gob.mef.gescon.web.ui.OportunidadMB.java

public void onListTipoConocimientoChange(AjaxBehaviorEvent event) {
    try {//from  www.  j a  v a2  s. c  om
        if (event != null) {
            final BigDecimal id = (BigDecimal) ((SelectOneMenu) event.getSource()).getValue();
            this.setIdTipoConocimiento(id);
            if (id != null) {
                HashMap filters = new HashMap();
                filters.put("ntipoconocimientoid", id);
                ConocimientoService service = (ConocimientoService) ServiceFinder
                        .findBean("ConocimientoService");
                if (this.getSelectedOportunidad() != null) {
                    filters.put("nconocimientoid",
                            this.getSelectedOportunidad().getNconocimientoid().toString());
                    this.setListaTargetVinculos(new ArrayList());
                    List<Consulta> lista = service.getConcimientosVinculados(filters);
                    Collections.sort(lista, Consulta.Comparators.ID);
                    if (id.equals(Constante.BASELEGAL)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBL(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBL().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosPR(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosPR().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosWK(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosWK().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosCT(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosCT().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBP(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBP().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosOM(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosOM().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosOM());
                    }
                } else {
                    if (id.equals(Constante.BASELEGAL)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosOM());
                    }
                }
                if (CollectionUtils.isNotEmpty(this.getListaTargetVinculos())) {
                    List<String> ids = new ArrayList<String>();
                    for (Consulta c : this.getListaTargetVinculos()) {
                        ids.add(c.getIdconocimiento().toString());
                    }
                    String filter = StringUtils.join(ids, ',');
                    if (id.equals(Constante.WIKI)) {
                        filter = filter.concat(",")
                                .concat(this.getSelectedOportunidad().getNconocimientoid().toString());
                    }
                    filters.put("nconocimientovinc", filter);
                }
                if (id.equals(Constante.BASELEGAL)) {
                    this.setListaSourceVinculosBL(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBL());
                } else if (id.equals(Constante.PREGUNTAS)) {
                    this.setListaSourceVinculosPR(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosPR());
                } else if (id.equals(Constante.WIKI)) {
                    this.setListaSourceVinculosWK(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosWK());
                } else if (id.equals(Constante.CONTENIDO)) {
                    this.setListaSourceVinculosCT(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosCT());
                } else if (id.equals(Constante.BUENAPRACTICA)) {
                    this.setListaSourceVinculosBP(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBP());
                } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                    this.setListaSourceVinculosOM(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosOM());
                }
                this.setPickList(new DualListModel<Consulta>(this.getListaSourceVinculos(),
                        this.getListaTargetVinculos()));
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
}

From source file:org.openbravo.erpCommon.ad_forms.DocInvoice.java

/**
 * Create Facts (the accounting logic) for ARI, ARC, ARF, API, APC.
 * //from   w  w  w.  ja  v  a 2s .c o m
 * <pre>
 *  ARI, ARF
 *      Receivables     DR
 *      Charge                  CR
 *      TaxDue                  CR
 *      Revenue                 CR
 *  ARC
 *      Receivables             CR
 *      Charge          DR
 *      TaxDue          DR
 *      Revenue         RR
 *  API
 *      Payables                CR
 *      Charge          DR
 *      TaxCredit       DR
 *      Expense         DR
 *  APC
 *      Payables        DR
 *      Charge                  CR
 *      TaxCredit               CR
 *      Expense                 CR
 * </pre>
 * 
 * @param as
 *          accounting schema
 * @return Fact
 */
public Fact createFact(AcctSchema as, ConnectionProvider conn, Connection con, VariablesSecureApp vars)
        throws ServletException {
    // Select specific definition
    String strClassname = AcctServerData.selectTemplateDoc(conn, as.m_C_AcctSchema_ID, DocumentType);
    if (strClassname.equals(""))
        strClassname = AcctServerData.selectTemplate(conn, as.m_C_AcctSchema_ID, AD_Table_ID);
    if (!strClassname.equals("")) {
        try {
            DocInvoiceTemplate newTemplate = (DocInvoiceTemplate) Class.forName(strClassname).newInstance();
            return newTemplate.createFact(this, as, conn, con, vars);
        } catch (Exception e) {
            log4j.error("Error while creating new instance for DocInvoiceTemplate - " + e);
        }
    }
    log4jDocInvoice.debug("Starting create fact");
    // create Fact Header
    Fact fact = new Fact(this, as, Fact.POST_Actual);
    String Fact_Acct_Group_ID = SequenceIdData.getUUID();
    // Cash based accounting
    if (!as.isAccrual())
        return null;

    /** @todo Assumes TaxIncluded = N */

    // ARI, ARF, ARI_RM
    if (DocumentType.equals(AcctServer.DOCTYPE_ARInvoice) || DocumentType.equals(AcctServer.DOCTYPE_ARProForma)
            || DocumentType.equals(AcctServer.DOCTYPE_RMSalesInvoice)) {
        log4jDocInvoice.debug("Point 1");
        // Receivables DR
        if (m_payments == null || m_payments.length == 0)
            for (int i = 0; m_debt_payments != null && i < m_debt_payments.length; i++) {
                if (m_debt_payments[i].isReceipt.equals("Y"))
                    fact.createLine(m_debt_payments[i],
                            getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn),
                            this.C_Currency_ID,
                            getConvertedAmt(m_debt_payments[i].Amount, m_debt_payments[i].C_Currency_ID_From,
                                    this.C_Currency_ID, DateAcct, "", AD_Client_ID, AD_Org_ID, conn),
                            "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                else
                    fact.createLine(m_debt_payments[i],
                            getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn),
                            this.C_Currency_ID, "",
                            getConvertedAmt(m_debt_payments[i].Amount, m_debt_payments[i].C_Currency_ID_From,
                                    this.C_Currency_ID, DateAcct, "", AD_Client_ID, AD_Org_ID, conn),
                            Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
            }
        else
            for (int i = 0; m_payments != null && i < m_payments.length; i++) {
                fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, true, false, conn),
                        this.C_Currency_ID, m_payments[i].Amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                        DocumentType, conn);
                if (m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)
                        && new BigDecimal(m_payments[i].PrepaidAmount).compareTo(ZERO) != 0) {
                    fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                            this.C_Currency_ID, m_payments[i].PrepaidAmount, "", Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                } else if (!m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)) {
                    try {
                        DocInvoiceData[] prepayments = DocInvoiceData.selectPrepayments(connectionProvider,
                                m_payments[i].Line_ID);
                        for (int j = 0; j < prepayments.length; j++) {
                            BigDecimal prePaymentAmt = convertAmount(new BigDecimal(prepayments[j].prepaidamt),
                                    true, DateAcct, TABLEID_Payment, prepayments[j].finPaymentId,
                                    m_payments[i].C_Currency_ID_From, as.m_C_Currency_ID, m_payments[i], as,
                                    fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn);
                            fact.createLine(m_payments[i],
                                    getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                    m_payments[i].C_Currency_ID_From, prePaymentAmt.toString(), "",
                                    Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                        }
                    } catch (ServletException e) {
                        log4jDocInvoice.warn(e);
                    }
                }
            }
        if ((m_payments == null || m_payments.length == 0)
                && (m_debt_payments == null || m_debt_payments.length == 0)) {
            BigDecimal grossamt = new BigDecimal(Amounts[AMTTYPE_Gross]);
            BigDecimal prepayment = new BigDecimal(prepaymentamt);
            BigDecimal difference = grossamt.abs().subtract(prepayment.abs());
            if (!prepaymentamt.equals("0")) {
                if (grossamt.abs().compareTo(prepayment.abs()) > 0) {
                    if (IsReturn.equals("Y")) {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                this.C_Currency_ID, "", prepaymentamt, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, false, conn),
                                this.C_Currency_ID, "", difference.toString(), Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    } else {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                this.C_Currency_ID, prepaymentamt, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, false, conn),
                                this.C_Currency_ID, difference.toString(), "", Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                } else {
                    if (IsReturn.equals("Y")) {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                this.C_Currency_ID, "", prepaymentamt, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    } else {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                this.C_Currency_ID, prepaymentamt, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    }
                }
            } else {
                fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, false, conn),
                        this.C_Currency_ID, Amounts[AMTTYPE_Gross], "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                        DocumentType, conn);
            }

        }
        // Charge CR
        log4jDocInvoice.debug("The first create line");
        fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn), C_Currency_ID, "",
                getAmount(AcctServer.AMTTYPE_Charge), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
        // TaxDue CR
        log4jDocInvoice.debug("m_taxes.length: " + m_taxes);
        BigDecimal grossamt = new BigDecimal(Amounts[AMTTYPE_Gross]);
        BigDecimal prepayment = new BigDecimal(prepaymentamt);

        for (int i = 0; m_taxes != null && i < m_taxes.length; i++) {
            // New docLine created to assign C_Tax_ID value to the entry
            DocLine docLine = new DocLine(DocumentType, Record_ID, "");
            docLine.m_C_Tax_ID = m_taxes[i].m_C_Tax_ID;

            BigDecimal percentageFinalAccount = CashVATUtil._100;
            final BigDecimal taxesAmountTotal = new BigDecimal(
                    StringUtils.isBlank(m_taxes[i].m_amount) ? "0" : m_taxes[i].m_amount);
            BigDecimal taxToTransAccount = BigDecimal.ZERO;
            int precission = 0;
            OBContext.setAdminMode(true);
            try {
                Currency currency = OBDal.getInstance().get(Currency.class, C_Currency_ID);
                precission = currency.getStandardPrecision().intValue();
            } finally {
                OBContext.restorePreviousMode();
            }
            if (IsReversal.equals("Y")) {
                if (isCashVAT && m_taxes[i].m_isCashVAT) {
                    if ((m_payments == null || m_payments.length == 0)
                            && (m_debt_payments == null || m_debt_payments.length == 0)
                            && (!prepaymentamt.equals("0"))) {
                        percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100)))
                                .divide(grossamt.abs(), precission, RoundingMode.HALF_UP));
                        taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                C_Currency_ID);
                    } else {
                        percentageFinalAccount = CashVATUtil
                                .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                        taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                C_Currency_ID);
                    }
                    fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue_Trans, as, conn),
                            C_Currency_ID, taxToTransAccount.toString(), "", Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                }
                final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
                fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as, conn), C_Currency_ID,
                        taxToFinalAccount.toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                        conn);
            } else {
                if (isCashVAT && m_taxes[i].m_isCashVAT) {
                    if ((m_payments == null || m_payments.length == 0)
                            && (m_debt_payments == null || m_debt_payments.length == 0)
                            && (!prepaymentamt.equals("0"))) {
                        percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100)))
                                .divide(grossamt.abs(), precission, RoundingMode.HALF_UP));
                        taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                C_Currency_ID);
                    } else {
                        percentageFinalAccount = CashVATUtil
                                .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                        taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                C_Currency_ID);
                    }
                    fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue_Trans, as, conn),
                            C_Currency_ID, "", taxToTransAccount.toString(), Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                }
                final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
                fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as, conn), C_Currency_ID,
                        "", taxToFinalAccount.toString(), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                        conn);
            }
        }
        // Revenue CR
        if (p_lines != null && p_lines.length > 0) {
            for (int i = 0; i < p_lines.length; i++) {
                Account account = ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Revenue, as,
                        conn);
                if (DocumentType.equals(AcctServer.DOCTYPE_RMSalesInvoice)) {
                    Account accountReturnMaterial = ((DocLine_Invoice) p_lines[i])
                            .getAccount(ProductInfo.ACCTTYPE_P_RevenueReturn, as, conn);
                    if (accountReturnMaterial != null) {
                        account = accountReturnMaterial;
                    }
                }
                String amount = p_lines[i].getAmount();
                String amountConverted = "";
                ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Invoice, Record_ID,
                        C_Currency_ID, as.m_C_Currency_ID);
                if (conversionRateCurrentDoc != null) {
                    amountConverted = applyRate(new BigDecimal(p_lines[i].getAmount()),
                            conversionRateCurrentDoc, true).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                } else {
                    amountConverted = getConvertedAmt(p_lines[i].getAmount(), C_Currency_ID, as.m_C_Currency_ID,
                            DateAcct, "", AD_Client_ID, AD_Org_ID, conn);
                }
                if (((DocLine_Invoice) p_lines[i]).isDeferred()) {
                    amount = createAccDefRevenueFact(
                            fact, (DocLine_Invoice) p_lines[i], account, ((DocLine_Invoice) p_lines[i])
                                    .getAccount(ProductInfo.ACCTTYPE_P_DefRevenue, as, conn),
                            amountConverted, as.m_C_Currency_ID, conn);
                    if (IsReversal.equals("Y")) {
                        fact.createLine(p_lines[i], account, as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    } else {
                        fact.createLine(p_lines[i], account, as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                } else {
                    if (IsReversal.equals("Y")) {
                        fact.createLine(p_lines[i], account, this.C_Currency_ID, amount, "", Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    } else {
                        fact.createLine(p_lines[i], account, this.C_Currency_ID, "", amount, Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                }
                // If revenue has been deferred
                if (((DocLine_Invoice) p_lines[i]).isDeferred() && !amount.equals(amountConverted)) {
                    amount = new BigDecimal(amountConverted).subtract(new BigDecimal(amount)).toString();
                    if (IsReversal.equals("Y")) {
                        fact.createLine(p_lines[i],
                                ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefRevenue, as,
                                        conn),
                                as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    } else {
                        fact.createLine(p_lines[i],
                                ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefRevenue, as,
                                        conn),
                                as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    }
                }
            }
        }
        // Set Locations
        FactLine[] fLines = fact.getLines();
        for (int i = 0; i < fLines.length; i++) {
            if (fLines[i] != null) {
                fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), true, conn); // from Loc
                fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, false, conn); // to Loc
            }
        }
    }
    // ARC
    else if (this.DocumentType.equals(AcctServer.DOCTYPE_ARCredit)) {
        log4jDocInvoice.debug("Point 2");
        // Receivables CR
        if (m_payments == null || m_payments.length == 0)
            for (int i = 0; m_debt_payments != null && i < m_debt_payments.length; i++) {
                BigDecimal amount = new BigDecimal(m_debt_payments[i].Amount);
                // BigDecimal ZERO = BigDecimal.ZERO;
                fact.createLine(m_debt_payments[i],
                        getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn),
                        this.C_Currency_ID, "",
                        getConvertedAmt(((amount.negate())).toPlainString(),
                                m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID, DateAcct, "",
                                AD_Client_ID, AD_Org_ID, conn),
                        Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
            }
        else
            for (int i = 0; m_payments != null && i < m_payments.length; i++) {
                BigDecimal amount = new BigDecimal(m_payments[i].Amount);
                BigDecimal prepaidAmount = new BigDecimal(m_payments[i].PrepaidAmount);
                fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, true, false, conn),
                        this.C_Currency_ID, "", amount.negate().toString(), Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
                // Pre-payment: Probably not needed as at this point we can not generate pre-payments
                // against ARC. Amount is negated
                if (m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)
                        && prepaidAmount.compareTo(ZERO) != 0) {
                    fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                            this.C_Currency_ID, "", prepaidAmount.negate().toString(), Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                } else if (!m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)) {
                    try {
                        DocInvoiceData[] prepayments = DocInvoiceData.selectPrepayments(connectionProvider,
                                m_payments[i].Line_ID);
                        for (int j = 0; j < prepayments.length; j++) {
                            BigDecimal prePaymentAmt = convertAmount(
                                    new BigDecimal(prepayments[j].prepaidamt).negate(), true, DateAcct,
                                    TABLEID_Payment, prepayments[j].finPaymentId,
                                    m_payments[i].C_Currency_ID_From, as.m_C_Currency_ID, m_payments[i], as,
                                    fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn);
                            fact.createLine(m_payments[i],
                                    getAccountBPartner(C_BPartner_ID, as, true, true, conn),
                                    m_payments[i].C_Currency_ID_From, "", prePaymentAmt.toString(),
                                    Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                        }
                    } catch (ServletException e) {
                        log4jDocInvoice.warn(e);
                    }
                }
            }
        if ((m_payments == null || m_payments.length == 0)
                && (m_debt_payments == null || m_debt_payments.length == 0)) {
            fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, true, false, conn), this.C_Currency_ID,
                    "", Amounts[AMTTYPE_Gross], Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
        }
        // Charge DR
        fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn), this.C_Currency_ID,
                getAmount(AcctServer.AMTTYPE_Charge), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                conn);
        // TaxDue DR
        for (int i = 0; m_taxes != null && i < m_taxes.length; i++) {
            // New docLine created to assign C_Tax_ID value to the entry
            DocLine docLine = new DocLine(DocumentType, Record_ID, "");
            docLine.m_C_Tax_ID = m_taxes[i].m_C_Tax_ID;

            BigDecimal percentageFinalAccount = CashVATUtil._100;
            final BigDecimal taxesAmountTotal = new BigDecimal(
                    StringUtils.isBlank(m_taxes[i].getAmount()) ? "0" : m_taxes[i].getAmount());
            BigDecimal taxToTransAccount = BigDecimal.ZERO;
            if (isCashVAT && m_taxes[i].m_isCashVAT) {
                percentageFinalAccount = CashVATUtil
                        .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                        CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal, C_Currency_ID);
                fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue_Trans, as, conn),
                        this.C_Currency_ID, taxToTransAccount.toString(), "", Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            }
            final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
            fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxDue, as, conn),
                    this.C_Currency_ID, taxToFinalAccount.toString(), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                    DocumentType, conn);
        }
        // Revenue CR
        for (int i = 0; p_lines != null && i < p_lines.length; i++) {
            String amount = p_lines[i].getAmount();
            String amountCoverted = "";
            ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Invoice, Record_ID,
                    C_Currency_ID, as.m_C_Currency_ID);
            if (conversionRateCurrentDoc != null) {
                amountCoverted = applyRate(new BigDecimal(p_lines[i].getAmount()), conversionRateCurrentDoc,
                        true).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            } else {
                amountCoverted = getConvertedAmt(p_lines[i].getAmount(), C_Currency_ID, as.m_C_Currency_ID,
                        DateAcct, "", AD_Client_ID, AD_Org_ID, conn);
            }
            Account account = ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Revenue, as,
                    conn);
            if (((DocLine_Invoice) p_lines[i]).isDeferred()) {
                amount = createAccDefRevenueFact(fact, (DocLine_Invoice) p_lines[i], account,
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefRevenue, as, conn),
                        amountCoverted, as.m_C_Currency_ID, conn);
                fact.createLine(p_lines[i], account, as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            } else {
                fact.createLine(p_lines[i], account, this.C_Currency_ID, amount, "", Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            }
            // If revenue has been deferred
            if (((DocLine_Invoice) p_lines[i]).isDeferred() && !amount.equals(amountCoverted)) {
                amount = new BigDecimal(amountCoverted).subtract(new BigDecimal(amount)).toString();
                fact.createLine(p_lines[i],
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefRevenue, as, conn),
                        as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                        conn);
            }
        }
        // Set Locations
        FactLine[] fLines = fact.getLines();
        for (int i = 0; fLines != null && i < fLines.length; i++) {
            if (fLines[i] != null) {
                fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), true, conn); // from Loc
                fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, false, conn); // to Loc
            }
        }
    }
    // API
    else if (this.DocumentType.equals(AcctServer.DOCTYPE_APInvoice)) {
        log4jDocInvoice.debug("Point 3");
        // Liability CR
        if (m_payments == null || m_payments.length == 0)
            for (int i = 0; m_debt_payments != null && i < m_debt_payments.length; i++) {
                if (m_debt_payments[i].isReceipt.equals("Y"))
                    fact.createLine(m_debt_payments[i],
                            getAccountBPartner(C_BPartner_ID, as, true, m_debt_payments[i].dpStatus, conn),
                            this.C_Currency_ID,
                            getConvertedAmt(m_debt_payments[i].Amount, m_debt_payments[i].C_Currency_ID_From,
                                    this.C_Currency_ID, DateAcct, "", AD_Client_ID, AD_Org_ID, conn),
                            "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                else
                    fact.createLine(m_debt_payments[i],
                            getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn),
                            this.C_Currency_ID, "",
                            getConvertedAmt(m_debt_payments[i].Amount, m_debt_payments[i].C_Currency_ID_From,
                                    this.C_Currency_ID, DateAcct, "", AD_Client_ID, AD_Org_ID, conn),
                            Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
            }
        else
            for (int i = 0; m_payments != null && i < m_payments.length; i++) {
                fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, false, false, conn),
                        this.C_Currency_ID, "", m_payments[i].Amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                        DocumentType, conn);
                if (m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)
                        && new BigDecimal(m_payments[i].PrepaidAmount).compareTo(ZERO) != 0) {
                    fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                            this.C_Currency_ID, "", m_payments[i].PrepaidAmount, Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                } else if (!m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)) {
                    try {
                        DocInvoiceData[] prepayments = DocInvoiceData.selectPrepayments(connectionProvider,
                                m_payments[i].Line_ID);
                        for (int j = 0; j < prepayments.length; j++) {
                            BigDecimal prePaymentAmt = convertAmount(new BigDecimal(prepayments[j].prepaidamt),
                                    false, DateAcct, TABLEID_Payment, prepayments[j].finPaymentId,
                                    m_payments[i].C_Currency_ID_From, as.m_C_Currency_ID, m_payments[i], as,
                                    fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn);
                            fact.createLine(m_payments[i],
                                    getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                    m_payments[i].C_Currency_ID_From, "", prePaymentAmt.toString(),
                                    Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                        }
                    } catch (ServletException e) {
                        log4jDocInvoice.warn(e);
                    }
                }
            }
        if ((m_payments == null || m_payments.length == 0)
                && (m_debt_payments == null || m_debt_payments.length == 0)) {
            BigDecimal grossamt = new BigDecimal(Amounts[AMTTYPE_Gross]);
            BigDecimal prepayment = new BigDecimal(prepaymentamt);
            BigDecimal difference = grossamt.abs().subtract(prepayment.abs());
            if (!prepaymentamt.equals("0")) {
                if (grossamt.abs().compareTo(prepayment.abs()) > 0) {
                    if (IsReturn.equals("Y")) {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                this.C_Currency_ID, prepaymentamt, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, false, conn),
                                this.C_Currency_ID, difference.toString(), "", Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    } else {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                this.C_Currency_ID, "", prepaymentamt, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, false, conn),
                                this.C_Currency_ID, "", difference.toString(), Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                } else {
                    if (IsReturn.equals("Y")) {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                this.C_Currency_ID, prepaymentamt, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    } else {
                        fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                this.C_Currency_ID, "", prepaymentamt, Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                                DocumentType, conn);
                    }
                }
            } else {
                fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, false, conn),
                        this.C_Currency_ID, "", Amounts[AMTTYPE_Gross], Fact_Acct_Group_ID, nextSeqNo(SeqNo),
                        DocumentType, conn);
            }

        }
        // Charge DR
        fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn), this.C_Currency_ID,
                getAmount(AcctServer.AMTTYPE_Charge), "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                conn);
        BigDecimal grossamt = new BigDecimal(Amounts[AMTTYPE_Gross]);
        BigDecimal prepayment = new BigDecimal(prepaymentamt);
        // TaxCredit DR
        for (int i = 0; m_taxes != null && i < m_taxes.length; i++) {
            // New docLine created to assign C_Tax_ID value to the entry
            DocLine docLine = new DocLine(DocumentType, Record_ID, "");
            docLine.m_C_Tax_ID = m_taxes[i].m_C_Tax_ID;
            OBContext.setAdminMode(true);
            int precission = 0;
            try {
                Currency currency = OBDal.getInstance().get(Currency.class, C_Currency_ID);
                precission = currency.getStandardPrecision().intValue();
            } finally {
                OBContext.restorePreviousMode();
            }
            if (!m_taxes[i].m_isTaxUndeductable) {
                BigDecimal percentageFinalAccount = CashVATUtil._100;
                final BigDecimal taxesAmountTotal = new BigDecimal(
                        StringUtils.isBlank(m_taxes[i].getAmount()) ? "0" : m_taxes[i].getAmount());
                BigDecimal taxToTransAccount = BigDecimal.ZERO;
                if (IsReversal.equals("Y")) {
                    if (isCashVAT && m_taxes[i].m_isCashVAT) {
                        if ((m_payments == null || m_payments.length == 0)
                                && (m_debt_payments == null || m_debt_payments.length == 0)
                                && (!prepaymentamt.equals("0"))) {
                            percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100)))
                                    .divide(grossamt.abs(), precission, RoundingMode.HALF_UP));
                            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                    CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                    C_Currency_ID);
                        } else {
                            percentageFinalAccount = CashVATUtil
                                    .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                    CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                    C_Currency_ID);
                        }
                        fact.createLine(docLine,
                                m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn),
                                this.C_Currency_ID, "", taxToTransAccount.toString(), Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                    final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
                    fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn),
                            this.C_Currency_ID, "", taxToFinalAccount.toString(), Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                } else {
                    if (isCashVAT && m_taxes[i].m_isCashVAT) {
                        if ((m_payments == null || m_payments.length == 0)
                                && (m_debt_payments == null || m_debt_payments.length == 0)
                                && (!prepaymentamt.equals("0"))) {
                            percentageFinalAccount = ((prepayment.multiply(new BigDecimal(100)))
                                    .divide(grossamt.abs(), precission, RoundingMode.HALF_UP));
                            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                    CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                    C_Currency_ID);
                        } else {
                            percentageFinalAccount = CashVATUtil
                                    .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                            taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                                    CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal,
                                    C_Currency_ID);
                        }
                        fact.createLine(docLine,
                                m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn),
                                this.C_Currency_ID, taxToTransAccount.toString(), "", Fact_Acct_Group_ID,
                                nextSeqNo(SeqNo), DocumentType, conn);
                    }
                    final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
                    fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn),
                            this.C_Currency_ID, taxToFinalAccount.toString(), "", Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                }

            } else {
                DocLineInvoiceData[] data = null;
                try {
                    data = DocLineInvoiceData.selectUndeductable(connectionProvider, Record_ID);
                } catch (ServletException e) {
                    log4jDocInvoice.warn(e);
                }

                BigDecimal cumulativeTaxLineAmount = new BigDecimal(0);
                BigDecimal taxAmount = new BigDecimal(0);
                for (int j = 0; data != null && j < data.length; j++) {
                    DocLine docLine1 = new DocLine(DocumentType, Record_ID, "");
                    docLine1.m_C_Tax_ID = data[j].cTaxId;
                    docLine1.m_C_BPartner_ID = data[j].cBpartnerId;
                    docLine1.m_M_Product_ID = data[j].mProductId;
                    docLine1.m_C_Costcenter_ID = data[j].cCostcenterId;
                    docLine1.m_C_Project_ID = data[j].cProjectId;
                    docLine1.m_User1_ID = data[j].user1id;
                    docLine1.m_User2_ID = data[j].user2id;
                    docLine1.m_C_Activity_ID = data[j].cActivityId;
                    docLine1.m_C_Campaign_ID = data[j].cCampaignId;
                    docLine1.m_A_Asset_ID = data[j].aAssetId;
                    String strtaxAmount = null;

                    try {

                        DocInvoiceData[] dataEx = DocInvoiceData.selectProductAcct(conn,
                                as.getC_AcctSchema_ID(), m_taxes[i].m_C_Tax_ID, Record_ID);
                        if (dataEx.length == 0) {
                            dataEx = DocInvoiceData.selectGLItemAcctForTaxLine(conn, as.getC_AcctSchema_ID(),
                                    m_taxes[i].m_C_Tax_ID, Record_ID);
                        }
                        strtaxAmount = m_taxes[i].getAmount();
                        taxAmount = new BigDecimal(strtaxAmount.equals("") ? "0.00" : strtaxAmount);
                        if (j == data.length - 1) {
                            data[j].taxamt = taxAmount.subtract(cumulativeTaxLineAmount).toPlainString();
                        }
                        try {

                            if (this.DocumentType.equals(AcctServer.DOCTYPE_APInvoice)) {
                                if (IsReversal.equals("Y")) {
                                    fact.createLine(docLine1, Account.getAccount(conn, dataEx[0].pExpenseAcct),
                                            this.C_Currency_ID, "", data[j].taxamt, Fact_Acct_Group_ID,
                                            nextSeqNo(SeqNo), DocumentType, conn);

                                } else {
                                    fact.createLine(docLine1, Account.getAccount(conn, dataEx[0].pExpenseAcct),
                                            this.C_Currency_ID, data[j].taxamt, "", Fact_Acct_Group_ID,
                                            nextSeqNo(SeqNo), DocumentType, conn);
                                }
                            } else if (this.DocumentType.equals(AcctServer.DOCTYPE_APCredit)) {
                                fact.createLine(docLine1, Account.getAccount(conn, dataEx[0].pExpenseAcct),
                                        this.C_Currency_ID, "", data[j].taxamt, Fact_Acct_Group_ID,
                                        nextSeqNo(SeqNo), DocumentType, conn);
                            }
                            cumulativeTaxLineAmount = cumulativeTaxLineAmount
                                    .add(new BigDecimal(data[j].taxamt));
                        } catch (ServletException e) {
                            log4jDocInvoice.error("Exception in createLineForTaxUndeductable method: " + e);
                        }
                    } catch (ServletException e) {
                        log4jDocInvoice.warn(e);
                    }
                }
            }
        }
        // Expense DR
        for (int i = 0; p_lines != null && i < p_lines.length; i++) {
            String amount = p_lines[i].getAmount();
            String amountConverted = "";
            ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Invoice, Record_ID,
                    C_Currency_ID, as.m_C_Currency_ID);
            if (conversionRateCurrentDoc != null) {
                amountConverted = applyRate(new BigDecimal(p_lines[i].getAmount()), conversionRateCurrentDoc,
                        true).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            } else {
                amountConverted = getConvertedAmt(p_lines[i].getAmount(), C_Currency_ID, as.m_C_Currency_ID,
                        DateAcct, "", AD_Client_ID, AD_Org_ID, conn);
            }
            if (((DocLine_Invoice) p_lines[i]).isDeferred()) {
                amount = createAccDefExpenseFact(fact, (DocLine_Invoice) p_lines[i],
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn),
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefExpense, as, conn),
                        amountConverted, as.m_C_Currency_ID, conn);
                if (IsReversal.equals("Y")) {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn),
                            as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                } else {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn),
                            as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                }
            } else {
                if (IsReversal.equals("Y")) {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn),
                            this.C_Currency_ID, "", amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                } else {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as, conn),
                            this.C_Currency_ID, amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                }
            }
            // If expense has been deferred
            if (((DocLine_Invoice) p_lines[i]).isDeferred() && !amount.equals(amountConverted)) {
                amount = new BigDecimal(amountConverted).subtract(new BigDecimal(amount)).toString();
                if (IsReversal.equals("Y")) {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefExpense, as,
                                    conn),
                            as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                } else {
                    fact.createLine(p_lines[i],
                            ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefExpense, as,
                                    conn),
                            as.m_C_Currency_ID, amount, "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                            conn);
                }
            }
        }
        // Set Locations
        FactLine[] fLines = fact.getLines();
        for (int i = 0; fLines != null && i < fLines.length; i++) {
            if (fLines[i] != null) {
                fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, true, conn); // from Loc
                fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), false, conn); // to Loc
            }
        }
        updateProductInfo(as.getC_AcctSchema_ID(), conn, con); // only API
    }
    // APC
    else if (this.DocumentType.equals(AcctServer.DOCTYPE_APCredit)) {
        log4jDocInvoice.debug("Point 4");
        // Liability DR
        if (m_payments == null || m_payments.length == 0)
            for (int i = 0; m_debt_payments != null && i < m_debt_payments.length; i++) {
                BigDecimal amount = new BigDecimal(m_debt_payments[i].Amount);
                // BigDecimal ZERO = BigDecimal.ZERO;
                fact.createLine(m_debt_payments[i],
                        getAccountBPartner(C_BPartner_ID, as, false, m_debt_payments[i].dpStatus, conn),
                        this.C_Currency_ID,
                        getConvertedAmt(((amount.negate())).toPlainString(),
                                m_debt_payments[i].C_Currency_ID_From, this.C_Currency_ID, DateAcct, "",
                                AD_Client_ID, AD_Org_ID, conn),
                        "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
            }
        else
            for (int i = 0; m_payments != null && i < m_payments.length; i++) {
                BigDecimal amount = new BigDecimal(m_payments[i].Amount);
                BigDecimal prepaidAmount = new BigDecimal(m_payments[i].PrepaidAmount);
                fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, false, false, conn),
                        this.C_Currency_ID, amount.negate().toString(), "", Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
                // Pre-payment: Probably not needed as at this point we can not generate pre-payments
                // against APC. Amount is negated
                if (m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)
                        && prepaidAmount.compareTo(ZERO) != 0) {
                    fact.createLine(m_payments[i], getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                            this.C_Currency_ID, prepaidAmount.negate().toString(), "", Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                } else if (!m_payments[i].C_Currency_ID_From.equals(as.m_C_Currency_ID)) {
                    try {
                        DocInvoiceData[] prepayments = DocInvoiceData.selectPrepayments(connectionProvider,
                                m_payments[i].Line_ID);
                        for (int j = 0; j < prepayments.length; j++) {
                            BigDecimal prePaymentAmt = convertAmount(
                                    new BigDecimal(prepayments[j].prepaidamt).negate(), false, DateAcct,
                                    TABLEID_Payment, prepayments[j].finPaymentId,
                                    m_payments[i].C_Currency_ID_From, as.m_C_Currency_ID, m_payments[i], as,
                                    fact, Fact_Acct_Group_ID, nextSeqNo(SeqNo), conn);
                            fact.createLine(m_payments[i],
                                    getAccountBPartner(C_BPartner_ID, as, false, true, conn),
                                    m_payments[i].C_Currency_ID_From, prePaymentAmt.toString(), "",
                                    Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
                        }
                    } catch (ServletException e) {
                        log4jDocInvoice.warn(e);
                    }
                }
            }
        if ((m_payments == null || m_payments.length == 0)
                && (m_debt_payments == null || m_debt_payments.length == 0)) {
            fact.createLine(null, getAccountBPartner(C_BPartner_ID, as, false, false, conn), this.C_Currency_ID,
                    Amounts[AMTTYPE_Gross], "", Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
        }
        // Charge CR
        fact.createLine(null, getAccount(AcctServer.ACCTTYPE_Charge, as, conn), this.C_Currency_ID, "",
                getAmount(AcctServer.AMTTYPE_Charge), Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType, conn);
        // TaxCredit CR
        for (int i = 0; m_taxes != null && i < m_taxes.length; i++) {
            // New docLine created to assign C_Tax_ID value to the entry
            DocLine docLine = new DocLine(DocumentType, Record_ID, "");
            docLine.m_C_Tax_ID = m_taxes[i].m_C_Tax_ID;
            if (m_taxes[i].m_isTaxUndeductable) {
                computeTaxUndeductableLine(conn, as, fact, docLine, Fact_Acct_Group_ID, m_taxes[i].m_C_Tax_ID,
                        m_taxes[i].getAmount());

            } else {
                BigDecimal percentageFinalAccount = CashVATUtil._100;
                final BigDecimal taxesAmountTotal = new BigDecimal(
                        StringUtils.isBlank(m_taxes[i].getAmount()) ? "0" : m_taxes[i].getAmount());
                BigDecimal taxToTransAccount = BigDecimal.ZERO;
                if (isCashVAT && m_taxes[i].m_isCashVAT) {
                    percentageFinalAccount = CashVATUtil
                            .calculatePrepaidPercentageForCashVATTax(m_taxes[i].m_C_Tax_ID, Record_ID);
                    taxToTransAccount = CashVATUtil.calculatePercentageAmount(
                            CashVATUtil._100.subtract(percentageFinalAccount), taxesAmountTotal, C_Currency_ID);
                    fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit_Trans, as, conn),
                            this.C_Currency_ID, "", taxToTransAccount.toString(), Fact_Acct_Group_ID,
                            nextSeqNo(SeqNo), DocumentType, conn);
                }
                final BigDecimal taxToFinalAccount = taxesAmountTotal.subtract(taxToTransAccount);
                fact.createLine(docLine, m_taxes[i].getAccount(DocTax.ACCTTYPE_TaxCredit, as, conn),
                        this.C_Currency_ID, "", taxToFinalAccount.toString(), Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            }
        }
        // Expense CR
        for (int i = 0; p_lines != null && i < p_lines.length; i++) {
            String amount = p_lines[i].getAmount();
            String amountConverted = "";
            ConversionRateDoc conversionRateCurrentDoc = getConversionRateDoc(TABLEID_Invoice, Record_ID,
                    C_Currency_ID, as.m_C_Currency_ID);
            if (conversionRateCurrentDoc != null) {
                amountConverted = applyRate(new BigDecimal(p_lines[i].getAmount()), conversionRateCurrentDoc,
                        true).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
            } else {
                amountConverted = getConvertedAmt(p_lines[i].getAmount(), C_Currency_ID, as.m_C_Currency_ID,
                        DateAcct, "", AD_Client_ID, AD_Org_ID, conn);
            }
            Account account = ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_Expense, as,
                    conn);
            if (((DocLine_Invoice) p_lines[i]).isDeferred()) {
                amount = createAccDefExpenseFact(fact, (DocLine_Invoice) p_lines[i], account,
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefExpense, as, conn),
                        amountConverted, as.m_C_Currency_ID, conn);
                fact.createLine(p_lines[i], account, as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            } else {
                fact.createLine(p_lines[i], account, this.C_Currency_ID, "", amount, Fact_Acct_Group_ID,
                        nextSeqNo(SeqNo), DocumentType, conn);
            }
            // If expense has been deferred
            if (((DocLine_Invoice) p_lines[i]).isDeferred() && !amount.equals(amountConverted)) {
                amount = new BigDecimal(amountConverted).subtract(new BigDecimal(amount)).toString();
                fact.createLine(p_lines[i],
                        ((DocLine_Invoice) p_lines[i]).getAccount(ProductInfo.ACCTTYPE_P_DefExpense, as, conn),
                        as.m_C_Currency_ID, "", amount, Fact_Acct_Group_ID, nextSeqNo(SeqNo), DocumentType,
                        conn);
            }

        }
        // Set Locations
        FactLine[] fLines = fact.getLines();
        for (int i = 0; fLines != null && i < fLines.length; i++) {
            if (fLines[i] != null) {
                fLines[i].setLocationFromBPartner(C_BPartner_Location_ID, true, conn); // from Loc
                fLines[i].setLocationFromOrg(fLines[i].getAD_Org_ID(conn), false, conn); // to Loc
            }
        }
    } else {
        log4jDocInvoice.warn("Doc_Invoice - DocumentType unknown: " + this.DocumentType);
        fact = null;
    }
    SeqNo = "0";
    return fact;
}

From source file:pe.gob.mef.gescon.web.ui.PreguntaMB.java

public void onListTipoConocimientoChange(AjaxBehaviorEvent event) {
    try {/*from ww  w  .  j  ava2 s  .c om*/
        if (event != null) {
            final BigDecimal id = (BigDecimal) ((SelectOneMenu) event.getSource()).getValue();
            this.setIdTipoConocimiento(id);
            if (id != null) {
                HashMap filters = new HashMap();
                filters.put("ntipoconocimientoid", id);
                ConocimientoService service = (ConocimientoService) ServiceFinder
                        .findBean("ConocimientoService");
                if (this.getSelectedPregunta() != null) {
                    filters.put("nconocimientoid", this.getSelectedPregunta().getNpreguntaid().toString());
                    this.setListaTargetVinculos(new ArrayList());
                    List<Consulta> lista = service.getConcimientosVinculados(filters);
                    Collections.sort(lista, Consulta.Comparators.ID);
                    if (id.equals(Constante.BASELEGAL)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBL(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBL().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosPR(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosPR().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosWK(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosWK().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosCT(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosCT().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBP(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBP().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosOM(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosOM().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosOM());
                    }
                } else {
                    if (id.equals(Constante.BASELEGAL)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosOM());
                    }
                }
                if (org.apache.commons.collections.CollectionUtils.isNotEmpty(this.getListaTargetVinculos())) {
                    List<String> ids = new ArrayList<String>();
                    for (Consulta c : this.getListaTargetVinculos()) {
                        ids.add(c.getIdconocimiento().toString());
                    }
                    String filter = StringUtils.join(ids, ',');
                    if (id.equals(Constante.WIKI)) {
                        filter = filter.concat(",")
                                .concat(this.getSelectedPregunta().getNpreguntaid().toString());
                    }
                    filters.put("nconocimientovinc", filter);
                }
                if (id.equals(Constante.BASELEGAL)) {
                    this.setListaSourceVinculosBL(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBL());
                } else if (id.equals(Constante.PREGUNTAS)) {
                    this.setListaSourceVinculosPR(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosPR());
                } else if (id.equals(Constante.WIKI)) {
                    this.setListaSourceVinculosWK(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosWK());
                } else if (id.equals(Constante.CONTENIDO)) {
                    this.setListaSourceVinculosCT(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosCT());
                } else if (id.equals(Constante.BUENAPRACTICA)) {
                    this.setListaSourceVinculosBP(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBP());
                } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                    this.setListaSourceVinculosOM(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosOM());
                }
                this.setPickListPregunta(new DualListModel<Consulta>(this.getListaSourceVinculos(),
                        this.getListaTargetVinculos()));
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
}

From source file:pe.gob.mef.gescon.web.ui.ContenidoMB.java

public void onListTipoConocimientoChange(AjaxBehaviorEvent event) {
    try {//  ww  w.  j  ava 2 s. co m
        if (event != null) {
            final BigDecimal id = (BigDecimal) ((SelectOneMenu) event.getSource()).getValue();
            this.setIdTipoConocimiento(id);
            if (id != null) {
                HashMap filters = new HashMap();
                filters.put("ntipoconocimientoid", id);
                ConocimientoService service = (ConocimientoService) ServiceFinder
                        .findBean("ConocimientoService");
                if (this.getSelectedContenido() != null) {
                    filters.put("nconocimientoid", this.getSelectedContenido().getNconocimientoid().toString());
                    this.setListaTargetVinculos(new ArrayList());
                    List<Consulta> lista = service.getConcimientosVinculados(filters);
                    Collections.sort(lista, Consulta.Comparators.ID);
                    if (id.equals(Constante.BASELEGAL)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBL(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBL().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosPR(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosPR().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosWK(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosWK().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosCT(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosCT().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosBP(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosBP().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        for (Consulta ele : lista) {
                            int pos = Collections.binarySearch(this.getListaTargetVinculosOM(), ele,
                                    Consulta.Comparators.ID);
                            if (pos < 0) {
                                this.getListaTargetVinculosOM().add(ele);
                            }
                        }
                        this.getListaTargetVinculos().addAll(this.getListaTargetVinculosOM());
                    }
                } else {
                    if (id.equals(Constante.BASELEGAL)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBL());
                    } else if (id.equals(Constante.PREGUNTAS)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosPR());
                    } else if (id.equals(Constante.WIKI)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosWK());
                    } else if (id.equals(Constante.CONTENIDO)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosCT());
                    } else if (id.equals(Constante.BUENAPRACTICA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosBP());
                    } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                        this.setListaTargetVinculos(this.getListaTargetVinculosOM());
                    }
                }
                if (CollectionUtils.isNotEmpty(this.getListaTargetVinculos())) {
                    List<String> ids = new ArrayList<String>();
                    for (Consulta c : this.getListaTargetVinculos()) {
                        ids.add(c.getIdconocimiento().toString());
                    }
                    String filter = StringUtils.join(ids, ',');
                    if (id.equals(Constante.WIKI)) {
                        filter = filter.concat(",")
                                .concat(this.getSelectedContenido().getNconocimientoid().toString());
                    }
                    filters.put("nconocimientovinc", filter);
                }
                if (id.equals(Constante.BASELEGAL)) {
                    this.setListaSourceVinculosBL(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBL());
                } else if (id.equals(Constante.PREGUNTAS)) {
                    this.setListaSourceVinculosPR(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosPR());
                } else if (id.equals(Constante.WIKI)) {
                    this.setListaSourceVinculosWK(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosWK());
                } else if (id.equals(Constante.CONTENIDO)) {
                    this.setListaSourceVinculosCT(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosCT());
                } else if (id.equals(Constante.BUENAPRACTICA)) {
                    this.setListaSourceVinculosBP(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosBP());
                } else if (id.equals(Constante.OPORTUNIDADMEJORA)) {
                    this.setListaSourceVinculosOM(service.getConcimientosDisponibles(filters));
                    this.setListaSourceVinculos(this.getListaSourceVinculosOM());
                }
                this.setPickList(new DualListModel<Consulta>(this.getListaSourceVinculos(),
                        this.getListaTargetVinculos()));
            }
        }
    } catch (Exception e) {
        e.getMessage();
        e.printStackTrace();
    }
}