Example usage for java.math BigDecimal abs

List of usage examples for java.math BigDecimal abs

Introduction

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

Prototype

public BigDecimal abs() 

Source Link

Document

Returns a BigDecimal whose value is the absolute value of this BigDecimal , and whose scale is this.scale() .

Usage

From source file:nl.strohalm.cyclos.services.elements.ElementServiceImpl.java

private void checkNewGroup(final Member member, final MemberGroup newGroup) {
    Collection<MemberAccountType> accountTypes = newGroup.getAccountTypes();
    if (accountTypes == null) {
        accountTypes = Collections.emptyList();
    }/*from w w  w  .j a  v a  2s  . c  om*/

    // Check if the member has any open loans
    final LoanQuery lQuery = new LoanQuery();
    lQuery.fetch(RelationshipHelper.nested(Loan.Relationships.TRANSFER, Payment.Relationships.TYPE));
    lQuery.setStatus(Loan.Status.OPEN);
    lQuery.setMember(member);
    AccountType accType;
    for (final Loan loan : loanService.search(lQuery)) {
        final LoanParameters params = loan.getTransferType().getLoan();
        if (!accountTypes.contains(getFrom(params.getRepaymentType()))) {
            throw new MemberHasPendingLoansException(newGroup);
        }

        if (params.getType() == Loan.Type.WITH_INTEREST) {
            if ((accType = getFrom(params.getMonthlyInterestRepaymentType())) != null
                    && !accountTypes.contains(accType)) {
                throw new MemberHasPendingLoansException(newGroup);
            }
            if ((accType = getFrom(params.getGrantFeeRepaymentType())) != null
                    && !accountTypes.contains(accType)) {
                throw new MemberHasPendingLoansException(newGroup);
            }
            if ((accType = getFrom(params.getExpirationFeeRepaymentType())) != null
                    && !accountTypes.contains(accType)) {
                throw new MemberHasPendingLoansException(newGroup);
            }
            if ((accType = getFrom(params.getExpirationDailyInterestRepaymentType())) != null
                    && !accountTypes.contains(accType)) {
                throw new MemberHasPendingLoansException(newGroup);
            }
        }
    }

    // Check if the member has any open invoices
    final InvoiceQuery invoiceQuery = new InvoiceQuery();
    invoiceQuery.setDirection(InvoiceQuery.Direction.INCOMING);
    invoiceQuery.setOwner(member);
    invoiceQuery.setStatus(Invoice.Status.OPEN);
    for (final Invoice invoice : invoiceService.search(invoiceQuery)) {
        boolean found = false;
        final Iterator<MemberAccountType> accIt = accountTypes.iterator();
        while (!found && accIt.hasNext()) {
            accType = accIt.next();
            final Iterator<TransferType> ttIt = accType.getFromTransferTypes().iterator();
            while (!found && ttIt.hasNext()) {
                final TransferType tt = ttIt.next();
                if (tt.getTo().equals(invoice.getDestinationAccountType())) {
                    found = true;
                }
            }
        }
        if (!found) {
            throw new MemberHasOpenInvoicesException(newGroup);
        }
    }

    invoiceQuery.setDirection(InvoiceQuery.Direction.OUTGOING);
    for (final Invoice invoice : invoiceService.search(invoiceQuery)) {
        if (!accountTypes.contains(invoice.getDestinationAccountType())) {
            throw new MemberHasOpenInvoicesException(newGroup);
        }
    }

    // Cancel all incoming and outgoing scheduled payments and notify payers/receivers
    // We cancel the payments here and not in the changeGroup method because we must check the balance after the cancellation
    // to ensure we take in to account the reserved amount (if any) that will be returned to the from account.
    cancelScheduledPaymentsAndNotify(member, newGroup);

    // Check the account balance
    final BigDecimal minimumPayment = paymentService.getMinimumPayment();
    for (final Account account : accountService.getAccounts(member)) {
        final BigDecimal balance = accountService.getBalance(new AccountDateDTO(account));
        if (!accountTypes.contains(account.getType()) && (balance.abs().compareTo(minimumPayment) > 0)) {
            throw new MemberHasBalanceException(newGroup, (MemberAccountType) account.getType());
        }
    }
}

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

@SuppressWarnings("unchecked")
@Override/*from www . ja  v a2 s.  c o m*/
public void runAccrual(String principalId, DateTime startDate, DateTime endDate, boolean recordRanData,
        String runAsPrincipalId) {
    List<LeaveBlock> accrualLeaveBlocks = new ArrayList<LeaveBlock>();
    Map<String, BigDecimal> accumulatedAccrualCatToAccrualAmounts = new HashMap<String, BigDecimal>();
    Map<String, BigDecimal> accumulatedAccrualCatToNegativeAccrualAmounts = new HashMap<String, BigDecimal>();

    if (startDate != null && endDate != null) {
        LOG.info("AccrualServiceImpl.runAccrual() STARTED with Principal: " + principalId + " Start: "
                + startDate.toString() + " End: " + endDate.toString());
    }
    if (startDate.isAfter(endDate)) {
        LOG.error("Start Date " + startDate.toString() + " should not be later than End Date "
                + endDate.toString());
        return;
        //         throw new RuntimeException("Start Date " + startDate.toString() + " should not be later than End Date " + endDate.toString());
    }
    //Inactivate all previous accrual-generated entries for this span of time
    deactivateOldAccruals(principalId, startDate, endDate, runAsPrincipalId);

    //Build a rate range aggregate with appropriate information for this period of time detailing Rate Ranges for job
    //entries for this range of time
    RateRangeAggregate rrAggregate = this.buildRateRangeAggregate(principalId, startDate, endDate);
    PrincipalHRAttributes phra = null;
    PrincipalHRAttributes endPhra = null;
    LeavePlanContract lp = null;
    List<? extends AccrualCategoryContract> accrCatList = null;

    //Iterate over every day in span 
    DateTime currentDate = startDate;
    while (!currentDate.isAfter(endDate)) {
        RateRange currentRange = rrAggregate.getRateOnDate(currentDate);
        if (currentRange == null) {
            currentDate = currentDate.plusDays(1);
            continue;
        }

        phra = currentRange.getPrincipalHRAttributes();
        if (phra == null || currentDate.toLocalDate().isBefore(phra.getServiceLocalDate())) {
            currentDate = currentDate.plusDays(1);
            continue;
        }

        // use the effectiveDate of this principalHRAttribute to search for inactive entries for this principalId
        // If there's an inactive entry, it means the job is going to end on the effectiveDate of the inactive entry
        // used for minimumPercentage and proration
        endPhra = currentRange.getEndPrincipalHRAttributes();
        if (endPhra != null && currentDate.toLocalDate().isAfter(endPhra.getEffectiveLocalDate())) {
            currentDate = currentDate.plusDays(1);
            continue;
        }

        // if the date range is before the service date of this employee, do not calculate accrual
        if (endDate.toLocalDate().isBefore(phra.getServiceLocalDate())) {
            return;
        }
        lp = currentRange.getLeavePlan();
        accrCatList = currentRange.getAcList();
        // if the employee status is changed, create an empty leave block on the currentDate
        if (currentRange.isStatusChanged()) {
            this.createEmptyLeaveBlockForStatusChange(principalId, accrualLeaveBlocks,
                    currentDate.toLocalDate());
        }
        // if no job found for the employee on the currentDate, do nothing
        if (CollectionUtils.isEmpty(currentRange.getJobs())) {
            currentDate = currentDate.plusDays(1);
            continue;
        }

        BigDecimal ftePercentage = currentRange.getAccrualRatePercentageModifier();
        BigDecimal totalOfStandardHours = currentRange.getStandardHours();
        boolean fullFteGranted = false;
        for (AccrualCategoryContract anAC : accrCatList) {
            if (anAC == null)
                continue;

            fullFteGranted = false;
            if (!currentDate.toLocalDate().isBefore(phra.getEffectiveLocalDate())
                    && !anAC.getAccrualEarnInterval().equals("N")) { // "N" means no accrual
                boolean prorationFlag = this.isProrationFlag(anAC.getProration());
                // get the accrual rule 
                AccrualCategoryRuleContract currentAcRule = this
                        .getRuleForAccrualCategory(currentRange.getAcRuleList(), anAC);

                // check if accrual category rule changed
                if (currentAcRule != null) {
                    DateTime ruleStartDate = getRuleStartDate(currentAcRule.getServiceUnitOfTime(),
                            phra.getServiceLocalDate(), currentAcRule.getStart());
                    DateTime previousIntervalDay = this.getPrevIntervalDate(ruleStartDate,
                            anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap());
                    DateTime nextIntervalDay = this.getNextIntervalDate(ruleStartDate,
                            anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap());

                    RateRangeContract previousRange = rrAggregate.getRateOnDate(previousIntervalDay);
                    AccrualCategoryRuleContract previousAcRule = null;
                    if (previousRange != null) {
                        previousAcRule = this.getRuleForAccrualCategory(previousRange.getAcRuleList(), anAC);
                    }
                    // rule changed
                    if (previousAcRule != null && !previousAcRule.getLmAccrualCategoryRuleId()
                            .equals(currentAcRule.getLmAccrualCategoryRuleId())) {
                        if (currentDate.toLocalDate().compareTo(previousIntervalDay.toLocalDate()) >= 0
                                && currentDate.toLocalDate().compareTo(nextIntervalDay.toLocalDate()) <= 0) {
                            int workDaysInBetween = TKUtils.getWorkDays(ruleStartDate, nextIntervalDay);
                            boolean minReachedFlag = minimumPercentageReachedForPayPeriod(
                                    anAC.getMinPercentWorked(), anAC.getAccrualEarnInterval(),
                                    workDaysInBetween, nextIntervalDay, phra.getPayCalendar(),
                                    rrAggregate.getCalEntryMap());
                            if (prorationFlag) {
                                if (minReachedFlag) {
                                    // min reached, proration=true, rule changed, then use actual work days of currentRule for calculation
                                    // so nothing special needs to be done here                        
                                } else {
                                    //minimum percentage NOT reached, proration = true, rule changed, then use previousRule for the whole pay period
                                    currentAcRule = previousAcRule;
                                }
                            } else {
                                if (minReachedFlag) {
                                    // min reached, proration = false, rule changed, then accrual the whole fte of the new rule for this pay interval
                                    accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(),
                                            currentAcRule.getAccrualRate());
                                    fullFteGranted = true;
                                } else {
                                    //min NOT reached, proration = false, rule changed, then accrual the whole fte of the previous rule for this pay interval
                                    accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(),
                                            previousAcRule.getAccrualRate());
                                    fullFteGranted = true;
                                }
                            }
                        }
                    }
                }

                // check for first pay period of principal attributes considering minimum percentage and proration   
                DateTime firstIntervalDate = this.getNextIntervalDate(
                        phra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), anAC.getAccrualEarnInterval(),
                        phra.getPayCalendar(), rrAggregate.getCalEntryMap());
                if (!currentDate.toLocalDate().isBefore(phra.getEffectiveLocalDate())
                        && !currentDate.toLocalDate().isAfter(firstIntervalDate.toLocalDate())) {
                    int workDaysInBetween = TKUtils.getWorkDays(
                            phra.getEffectiveLocalDate().toDateTimeAtStartOfDay(), firstIntervalDate);
                    boolean minReachedFlag = minimumPercentageReachedForPayPeriod(anAC.getMinPercentWorked(),
                            anAC.getAccrualEarnInterval(), workDaysInBetween, firstIntervalDate,
                            phra.getPayCalendar(), rrAggregate.getCalEntryMap());

                    if (prorationFlag) {
                        if (minReachedFlag) {
                            // minimum reached, proration = true, first pay period, then use actual work days of currentRule for calculation
                            // so nothing special needs to be done here
                        } else {
                            // min NOT reached, proration = true, first pay period, then no accrual for this pay period
                            accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                            accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                            continue;
                        }
                    } else {
                        if (minReachedFlag && currentAcRule != null) {
                            //  minimum reached, proration = false, first pay period, then accrual the whole fte of current AC rule for this pay interval
                            accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(),
                                    currentAcRule.getAccrualRate());
                            fullFteGranted = true;
                        } else {
                            // min NOT reached, proration = false, first pay period, then no accrual for this pay period
                            accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                            accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                            continue;
                        }
                    }
                }
                // last accrual interval
                if (endPhra != null) { // the employment is going to end on the effectiveDate of enPhra
                    DateTime previousIntervalDate = this.getPrevIntervalDate(
                            endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay(),
                            anAC.getAccrualEarnInterval(), phra.getPayCalendar(), rrAggregate.getCalEntryMap());
                    // currentDate is between the end date and the last interval date, so we are in the last interval
                    if (!currentDate.toLocalDate().isAfter(endPhra.getEffectiveLocalDate())
                            && currentDate.toLocalDate().isAfter(previousIntervalDate.toLocalDate())) {
                        DateTime lastIntervalDate = this.getNextIntervalDate(
                                endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay(),
                                anAC.getAccrualEarnInterval(), phra.getPayCalendar(),
                                rrAggregate.getCalEntryMap());
                        int workDaysInBetween = TKUtils.getWorkDays(previousIntervalDate,
                                endPhra.getEffectiveLocalDate().toDateTimeAtStartOfDay());
                        boolean minReachedFlag = minimumPercentageReachedForPayPeriod(
                                anAC.getMinPercentWorked(), anAC.getAccrualEarnInterval(), workDaysInBetween,
                                lastIntervalDate, phra.getPayCalendar(), rrAggregate.getCalEntryMap());
                        if (prorationFlag) {
                            if (minReachedFlag) {
                                // minimum reached, proration = true, first pay period, then use actual work days of currentRule for calculation
                                // so nothing special needs to be done here
                            } else {
                                // min NOT reached, proration = true, first pay period, then no accrual for this pay period
                                accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                                accumulatedAccrualCatToNegativeAccrualAmounts
                                        .remove(anAC.getLmAccrualCategoryId());
                                continue;
                            }
                        } else {
                            if (minReachedFlag) {
                                //  minimum reached, proration = false, first pay period, then accrual the whole fte of current AC rule for this pay interval
                                accumulatedAccrualCatToAccrualAmounts.put(anAC.getLmAccrualCategoryId(),
                                        currentAcRule.getAccrualRate());
                                fullFteGranted = true;
                            } else {
                                // min NOT reached, proration = false, first pay period, then no accrual for this pay period
                                accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                                accumulatedAccrualCatToNegativeAccrualAmounts
                                        .remove(anAC.getLmAccrualCategoryId());
                                continue;
                            }
                        }
                    }
                }

                if (currentAcRule == null) {
                    accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                    accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId());
                    continue;
                }

                //Fetch the accrual rate based on rate range for today(Rate range is the accumulated list of jobs and accrual rate for today)
                BigDecimal accrualRate = currentAcRule.getAccrualRate();
                int numberOfWorkDays = this.getWorkDaysInInterval(currentDate, anAC.getAccrualEarnInterval(),
                        phra.getPayCalendar(), rrAggregate.getCalEntryMap());
                BigDecimal dayRate = numberOfWorkDays > 0
                        ? accrualRate.divide(new BigDecimal(numberOfWorkDays), 6, BigDecimal.ROUND_HALF_UP)
                        : new BigDecimal(0);

                //get not eligible for accrual hours based on leave block on this day
                BigDecimal noAccrualHours = getNotEligibleForAccrualHours(principalId,
                        currentDate.toLocalDate());
                if (noAccrualHours != null && noAccrualHours.compareTo(BigDecimal.ZERO) != 0
                        && totalOfStandardHours.compareTo(BigDecimal.ZERO) != 0) {
                    BigDecimal dayHours = totalOfStandardHours.divide(new BigDecimal(5), 6,
                            BigDecimal.ROUND_HALF_UP);
                    // if leave hours on the day is more than the standard hours, use the standard hour to calculate the adjustment
                    if (noAccrualHours.abs().compareTo(dayHours.abs()) == 1) {
                        noAccrualHours = dayHours.abs().negate();
                    }
                    BigDecimal noAccrualRate = dayRate.multiply(noAccrualHours.divide(dayHours));
                    this.calculateHours(anAC.getLmAccrualCategoryId(), ftePercentage, noAccrualRate,
                            accumulatedAccrualCatToNegativeAccrualAmounts);
                }

                // only accrual on work days
                if (!TKUtils.isWeekend(currentDate) && !fullFteGranted) {
                    //Add to total accumulatedAccrualCatToAccrualAmounts
                    //use rule and ftePercentage to calculate the hours            
                    this.calculateHours(anAC.getLmAccrualCategoryId(), ftePercentage, dayRate,
                            accumulatedAccrualCatToAccrualAmounts);
                }
                //Determine if we are at the accrual earn interval in the span, if so add leave block for accumulated accrual amount to list
                //and reset accumulatedAccrualCatToAccrualAmounts and accumulatedAccrualCatToNegativeAccrualAmounts for this accrual category
                if (this.isDateAnIntervalDate(currentDate.toLocalDate(), anAC.getAccrualEarnInterval(),
                        phra.getPayCalendar(), rrAggregate.getCalEntryMap())) {
                    BigDecimal acHours = accumulatedAccrualCatToAccrualAmounts
                            .get(anAC.getLmAccrualCategoryId());

                    if (acHours != null) {
                        createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), acHours,
                                anAC, null, true, currentRange.getLeaveCalendarDocumentId(), null);
                        accumulatedAccrualCatToAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); // reset accumulatedAccrualCatToAccrualAmounts
                        fullFteGranted = false;
                    }

                    BigDecimal adjustmentHours = accumulatedAccrualCatToNegativeAccrualAmounts
                            .get(anAC.getLmAccrualCategoryId());
                    if (adjustmentHours != null && adjustmentHours.compareTo(BigDecimal.ZERO) != 0) {
                        // do not create leave block if the ajustment amount is 0
                        createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(),
                                adjustmentHours, anAC, null, false, currentRange.getLeaveCalendarDocumentId(),
                                null);
                        accumulatedAccrualCatToNegativeAccrualAmounts.remove(anAC.getLmAccrualCategoryId()); // reset accumulatedAccrualCatToNegativeAccrualAmounts
                    }
                }
            }
        }
        //Determine if today is a system scheduled time off and accrue holiday if so.
        SystemScheduledTimeOffContract ssto = currentRange.getSysScheTimeOff();
        if (ssto != null) {
            AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService()
                    .getAccrualCategory(ssto.getAccrualCategory(), ssto.getEffectiveLocalDate());
            if (anAC == null) {
                LOG.error("Cannot find Accrual Category for system scheduled time off "
                        + ssto.getLmSystemScheduledTimeOffId());
                return;
                //               throw new RuntimeException("Cannot find Accrual Category for system scheduled time off " + ssto.getLmSystemScheduledTimeOffId());
            }
            BigDecimal hrs = ssto.getAmountofTime().multiply(ftePercentage);
            // system scheduled time off leave block
            createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(), hrs, anAC,
                    ssto.getLmSystemScheduledTimeOffId(), true, currentRange.getLeaveCalendarDocumentId(),
                    null);
            // we only need to create usage leave block for ssto if there is a scheduled time off date
            if (ssto.getScheduledTimeOffDate() != null) {
                // usage leave block with negative amount. Assign primary leave assignment information to ssto usage leave block
                createLeaveBlock(principalId, accrualLeaveBlocks, ssto.getScheduledTimeOffLocalDate(),
                        hrs.negate(), anAC, ssto.getLmSystemScheduledTimeOffId(), true,
                        currentRange.getLeaveCalendarDocumentId(), currentRange.getPrimaryLeaveAssignmentId());
            }
        }
        // if today is the last day of the employment, create leave blocks if there's any hours available
        if (endPhra != null && currentDate.toLocalDate().equals(endPhra.getEffectiveLocalDate())) {
            // accumulated accrual amount
            if (!accumulatedAccrualCatToAccrualAmounts.isEmpty()) {
                for (Map.Entry<String, BigDecimal> entry : accumulatedAccrualCatToAccrualAmounts.entrySet()) {
                    if (entry.getValue() != null && entry.getValue().compareTo(BigDecimal.ZERO) != 0) {
                        AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService()
                                .getAccrualCategory(entry.getKey());
                        createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(),
                                entry.getValue(), anAC, null, true, currentRange.getLeaveCalendarDocumentId(),
                                null);
                    }
                }
                accumulatedAccrualCatToAccrualAmounts = new HashMap<String, BigDecimal>(); // reset accumulatedAccrualCatToAccrualAmounts
            }
            // negative/adjustment accrual amount
            if (!accumulatedAccrualCatToNegativeAccrualAmounts.isEmpty()) {
                for (Map.Entry<String, BigDecimal> entry : accumulatedAccrualCatToNegativeAccrualAmounts
                        .entrySet()) {
                    if (entry.getValue() != null && entry.getValue().compareTo(BigDecimal.ZERO) != 0) {
                        AccrualCategory anAC = HrServiceLocator.getAccrualCategoryService()
                                .getAccrualCategory(entry.getKey());
                        createLeaveBlock(principalId, accrualLeaveBlocks, currentDate.toLocalDate(),
                                entry.getValue(), anAC, null, true, currentRange.getLeaveCalendarDocumentId(),
                                null);
                    }
                }
                accumulatedAccrualCatToNegativeAccrualAmounts = new HashMap<String, BigDecimal>(); // reset accumulatedAccrualCatToNegativeAccrualAmounts
            }
            phra = null; // reset principal attribute so new value will be retrieved
            endPhra = null; // reset end principal attribute so new value will be retrieved
        }

        currentDate = currentDate.plusDays(1);
    }

    //Save accrual leave blocks at the very end
    LmServiceLocator.getLeaveBlockService().saveLeaveBlocks(accrualLeaveBlocks);

    // record timestamp of this accrual run in database
    if (recordRanData) {
        LmServiceLocator.getPrincipalAccrualRanService().updatePrincipalAccrualRanInfo(principalId);
    }

}

From source file:org.efaps.esjp.accounting.transaction.Recalculate_Base.java

/**
 * Method for recalculate and return string.
 *
 * @param _parameter Parameter as passed from the eFaps API.
 * @param _docInst Instance of the document selected.
 * @return String./*from w w  w.  j a v a2 s .  c o  m*/
 * @throws EFapsException on error.
 */
protected String getRecalculateInfo(final Parameter _parameter, final Instance _docInst) throws EFapsException {
    final StringBuilder html = new StringBuilder();
    final PrintQuery print = new PrintQuery(_docInst);
    print.addAttribute(CISales.DocumentSumAbstract.RateCrossTotal, CISales.DocumentSumAbstract.CrossTotal,
            CISales.DocumentSumAbstract.RateCurrencyId, CISales.DocumentSumAbstract.CurrencyId,
            CISales.DocumentSumAbstract.Date, CISales.DocumentSumAbstract.Name);
    print.execute();

    final BigDecimal rateCross = print.<BigDecimal>getAttribute(CISales.DocumentSumAbstract.RateCrossTotal);
    final BigDecimal crossTotal = print.<BigDecimal>getAttribute(CISales.DocumentSumAbstract.CrossTotal);
    final String nameDoc = print.<String>getAttribute(CISales.DocumentSumAbstract.Name);
    final Instance targetCurrInst = Instance.get(CIERP.Currency.getType(),
            print.<Long>getAttribute(CISales.DocumentSumAbstract.RateCurrencyId));
    final Instance currentInst = Instance.get(CIERP.Currency.getType(),
            print.<Long>getAttribute(CISales.DocumentSumAbstract.CurrencyId));
    final CurrencyInst tarCurr = new CurrencyInst(targetCurrInst);
    final CurrencyInst curr = new CurrencyInst(currentInst);

    final PriceUtil priceUtil = new PriceUtil();
    final BigDecimal[] rates = priceUtil.getRates(_parameter, targetCurrInst, currentInst);
    final BigDecimal rate = rates[2];

    final BigDecimal newCrossTotal = rateCross.compareTo(BigDecimal.ZERO) == 0 ? BigDecimal.ZERO
            : rateCross.divide(rate, BigDecimal.ROUND_HALF_UP);
    final BigDecimal gainloss = newCrossTotal.subtract(crossTotal);

    final Map<String, String[]> map = validateInfo(_parameter, gainloss);
    final String[] accs = map.get("accs");
    final String[] check = map.get("check");

    html.append("<table>").append("<tr>").append("<td>").append(DBProperties.getProperty("Sales_Invoice.Label"))
            .append("</td>").append("<td colspan=\"2\">").append(nameDoc).append("</td>").append("</tr>")
            .append("<td>").append(DBProperties.getProperty("Sales_DocumentAbstract/RateCrossTotal.Label"))
            .append("</td>").append("<td>").append(rateCross).append(" ").append(tarCurr.getSymbol())
            .append("</td>").append("<td>").append(crossTotal).append(" ").append(curr.getSymbol())
            .append("</td>").append("</tr>").append("<tr>").append("<td>")
            .append(DBProperties.getProperty("Accounting_TransactionRecalculateForm.newTotal.Label"))
            .append("</td>").append("<td colspan=\"2\" align=\"right\">").append(newCrossTotal).append(" ")
            .append(curr.getSymbol()).append("</td>").append("</tr>").append("<tr>").append("<td>");
    if (gainloss.compareTo(BigDecimal.ZERO) == -1) {
        html.append(DBProperties.getProperty("Accounting_TransactionRecalculateForm.loss.Label"));
    } else {
        html.append(DBProperties.getProperty("Accounting_TransactionRecalculateForm.gain.Label"));
    }
    html.append("</td>").append("<td colspan=\"2\" align=\"right\">").append(gainloss.abs()).append(" ")
            .append(curr.getSymbol()).append("</td>").append("</tr>").append("<tr>").append("<td>")
            .append(DBProperties.getProperty("Accounting_TransactionPositionDebit.Label")).append("</td>")
            .append("<td colspan=\"2\" align=\"right\">");
    if (checkAccounts(accs, 0, check).length() > 0) {
        html.append(checkAccounts(accs, 0, check));
    } else {
        html.append(DBProperties.getProperty("Accounting_TransactionRecalculateForm.reviseConfig.Label"));
    }
    html.append("</td>").append("</tr>").append("<tr>").append("<td>")
            .append(DBProperties.getProperty("Accounting_TransactionPositionCredit.Label")).append("</td>")
            .append("<td colspan=\"2\" align=\"right\">");
    if (checkAccounts(accs, 1, check).length() > 0) {
        html.append(checkAccounts(accs, 1, check));
    } else {
        html.append(DBProperties.getProperty("Accounting_TransactionRecalculateForm.reviseConfig.Label"));
    }
    html.append("</td>").append("</tr>").append("</table>");
    return html.toString();
}

From source file:org.openbravo.costing.CostingRuleProcess.java

protected void createCostingRuleInits(String ruleId, Set<String> childOrgs, Date date) {
    CostingRule rule = OBDal.getInstance().get(CostingRule.class, ruleId);
    ScrollableResults stockLines = getStockLines(childOrgs, date);
    // The key of the Map is the concatenation of orgId and warehouseId
    Map<String, String> initLines = new HashMap<String, String>();
    Map<String, Long> maxLineNumbers = new HashMap<String, Long>();
    InventoryCountLine closingInventoryLine = null;
    InventoryCountLine openInventoryLine = null;
    int i = 1;//from  w  w w.j a va  2  s  .  co  m
    try {
        while (stockLines.next()) {
            Object[] stockLine = stockLines.get();
            String productId = (String) stockLine[0];
            String attrSetInsId = (String) stockLine[1];
            String uomId = (String) stockLine[2];
            String orderUOMId = (String) stockLine[3];
            String locatorId = (String) stockLine[4];
            String warehouseId = (String) stockLine[5];
            BigDecimal qty = (BigDecimal) stockLine[6];
            BigDecimal orderQty = (BigDecimal) stockLine[7];

            String criId = initLines.get(warehouseId);
            CostingRuleInit cri = null;
            if (criId == null) {
                cri = createCostingRuleInitLine(rule, warehouseId, date);

                initLines.put(warehouseId, cri.getId());
            } else {
                cri = OBDal.getInstance().get(CostingRuleInit.class, criId);
            }
            Long lineNo = (maxLineNumbers.get(criId) == null ? 0L : maxLineNumbers.get(criId)) + 10L;
            maxLineNumbers.put(criId, lineNo);

            if (BigDecimal.ZERO.compareTo(qty) < 0) {
                // Do not insert negative values in Inventory lines, instead reverse the Quantity Count
                // and the Book Quantity. For example:
                // Instead of CountQty=0 and BookQty=-5 insert CountQty=5 and BookQty=0
                // By doing so the difference between both quantities remains the same and no negative
                // values have been inserted.

                openInventoryLine = insertInventoryLine(cri.getInitInventory(), productId, attrSetInsId, uomId,
                        orderUOMId, locatorId, qty, BigDecimal.ZERO, orderQty, BigDecimal.ZERO, lineNo, null);
                insertInventoryLine(cri.getCloseInventory(), productId, attrSetInsId, uomId, orderUOMId,
                        locatorId, BigDecimal.ZERO, qty, BigDecimal.ZERO, orderQty, lineNo, openInventoryLine);

            } else {
                openInventoryLine = insertInventoryLine(cri.getInitInventory(), productId, attrSetInsId, uomId,
                        orderUOMId, locatorId, BigDecimal.ZERO, qty.abs(), BigDecimal.ZERO,
                        orderQty == null ? null : orderQty.abs(), lineNo, closingInventoryLine);
                insertInventoryLine(cri.getCloseInventory(), productId, attrSetInsId, uomId, orderUOMId,
                        locatorId, qty == null ? null : qty.abs(), BigDecimal.ZERO,
                        orderQty == null ? null : orderQty.abs(), BigDecimal.ZERO, lineNo, openInventoryLine);

            }

            if ((i % 100) == 0) {
                OBDal.getInstance().flush();
                OBDal.getInstance().getSession().clear();
                // Reload rule after clear session.
                rule = OBDal.getInstance().get(CostingRule.class, ruleId);
            }
            i++;
        }
    } finally {
        stockLines.close();
    }
    // Process closing physical inventories.
    for (CostingRuleInit cri : rule.getCostingRuleInitList()) {
        new InventoryCountProcess().processInventory(cri.getCloseInventory(), false);
    }
}

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

/**
 * Create Facts (the accounting logic) for ARI, ARC, ARF, API, APC.
 * //from   www  .ja  va2  s  .co 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:nl.strohalm.cyclos.aop.MessageAspect.java

/**
 * Notify the owner of the given account if it is getting low units. In a separated method as it need to be synchronized
 *//*  w ww. j a  v  a2  s.c o m*/
private synchronized void doSendLowUnitsNotification(final MemberAccount account,
        final MemberGroupAccountSettings mgas) {
    final Calendar currentDate = DateHelper.truncate(Calendar.getInstance());
    if (lastPaymentDate == null || !lastPaymentDate.equals(currentDate)) {
        lastPaymentDate = currentDate;
        sentLowUnits.clear();
    }
    final Member fromOwner = account.getOwner();
    final BigDecimal lowUnits = mgas.getLowUnits() == null ? BigDecimal.ZERO : mgas.getLowUnits();
    final BigDecimal availableBalance = accountService.getStatus(new GetTransactionsDTO(account))
            .getAvailableBalance();
    final BigDecimal creditLimit = account.getCreditLimit();
    final Long memberId = fromOwner.getId();
    // ... check if the balance is smaller than low units, and ...
    if (availableBalance.add(creditLimit.abs()).compareTo(lowUnits) != 1) {
        // ... send the personal message only once a day (controlled by the sentLowUnits set)
        if (!sentLowUnits.contains(memberId)) {
            // Get the message settings
            final MessageSettings messageSettings = settingsService.getMessageSettings();
            final String subject = messageSettings.getLowUnitsSubject();
            final String body = messageSettings.getLowUnitsMessage();
            final String sms = messageSettings.getLowUnitsSms();

            // Process message content
            final LocalSettings localSettings = settingsService.getLocalSettings();
            final UnitsConverter converter = localSettings
                    .getUnitsConverter(account.getType().getCurrency().getPattern());
            final Map<String, Object> variables = new HashMap<String, Object>();
            variables.putAll(fromOwner.getVariableValues(localSettings));
            variables.putAll(account.getVariableValues(localSettings));
            variables.put("balance", converter.toString(availableBalance));
            final String processedSubject = MessageProcessingHelper.processVariables(subject, variables);
            final String processedBody = MessageProcessingHelper.processVariables(body, variables);
            final String processedSms = MessageProcessingHelper.processVariables(sms, variables);

            // Create the DTO
            final SendMessageFromSystemDTO message = new SendMessageFromSystemDTO();
            message.setType(Message.Type.ACCOUNT);
            message.setToMember(fromOwner);
            message.setSubject(processedSubject);
            message.setBody(processedBody);
            message.setSms(processedSms);

            // Send the message
            messageService.sendFromSystem(message);

            // Update table that controls duplicates
            sentLowUnits.add(memberId);
        }
    } else {
        sentLowUnits.remove(memberId);
    }
}

From source file:nl.strohalm.cyclos.services.accounts.AccountServiceImpl.java

@Override
public void setCreditLimit(final Member owner, final CreditLimitDTO limits) {
    validate(owner, limits);// w  ww  .j  av  a 2  s .  com

    Map<? extends AccountType, BigDecimal> limitPerType = limits.getLimitPerType();
    final Map<AccountType, BigDecimal> newLimitPerType = new HashMap<AccountType, BigDecimal>();
    if (limitPerType != null) {
        for (AccountType accountType : limitPerType.keySet()) {
            final BigDecimal limit = limitPerType.get(accountType);
            accountType = fetchService.fetch(accountType);
            newLimitPerType.put(accountType, limit);
        }
    }
    limitPerType = newLimitPerType;
    limits.setLimitPerType(limitPerType);

    Map<? extends AccountType, BigDecimal> upperLimitPerType = limits.getUpperLimitPerType();
    final Map<AccountType, BigDecimal> newUpperLimitPerType = new HashMap<AccountType, BigDecimal>();
    if (upperLimitPerType != null) {
        for (AccountType accountType : upperLimitPerType.keySet()) {
            final BigDecimal limit = upperLimitPerType.get(accountType);
            accountType = fetchService.fetch(accountType);
            newUpperLimitPerType.put(accountType, limit);
        }
    }
    upperLimitPerType = newUpperLimitPerType;
    limits.setUpperLimitPerType(upperLimitPerType);

    final List<Entry> entries = limits.getEntries();
    for (final Entry entry : entries) {
        final AccountType type = entry.getAccountType();
        final BigDecimal limit = entry.getCreditLimit();
        final BigDecimal upperLimit = entry.getUpperCreditLimit();
        if (limit == null && upperLimit == null) {
            continue;
        }
        List<? extends Account> accts;
        if (owner == null) {
            accts = getAccounts(type);
        } else {
            accts = Arrays.asList(getAccount(new AccountDTO(owner, type)));
        }
        for (Account account : accts) {

            boolean limitHasChanged = false;

            if (limit != null && !account.getCreditLimit().equals(limit.abs())) {
                account.setCreditLimit(limit.abs());
                limitHasChanged = true;
            }
            if (upperLimit != null && (account.getUpperCreditLimit() == null
                    || !account.getUpperCreditLimit().equals(upperLimit.abs()))) {
                account.setUpperCreditLimit(upperLimit.abs());
                limitHasChanged = true;
            }

            if (limitHasChanged) {
                // Update the account
                account = accountDao.update(account);

                // Generate the log
                AccountLimitLog log = new AccountLimitLog();
                log.setAccount(account);
                log.setBy((Administrator) LoggedUser.element());
                log.setDate(Calendar.getInstance());
                log.setCreditLimit(limit);
                log.setUpperCreditLimit(upperLimit);
                accountLimitLogDao.insert(log);
            }
        }
    }
}

From source file:net.pms.util.Rational.java

/**
 * Returns a {@link Rational} whose value is {@code (this / value)}.
 *
 * @param value the value by which this {@link Rational} is to be divided.
 * @return The division result.//w  ww.  j a v a2s  . c  om
 */
@Nullable
public Rational divide(@Nullable BigDecimal value) {
    if (value == null) {
        return null;
    }
    if (isNaN()) {
        return NaN;
    }

    if (value.signum() == 0) {
        if (signum() == 0) {
            return NaN;
        }
        return signum() > 0 ? POSITIVE_INFINITY : NEGATIVE_INFINITY;
    }

    if (signum() == 0 || isInfinite() || BigDecimal.ONE.equals(value.abs())) {
        return value.signum() < 0 ? negate() : this;
    }

    return divide(valueOf(value));
}

From source file:org.efaps.esjp.accounting.transaction.evaluation.DocumentInfo_Base.java

/**
 * Apply exchange gain loss.//from  w w  w .  jav a  2 s . c  o m
 *
 * @param _parameter Parameter as passed by the eFaps API
 * @throws EFapsException on error
 */
public void applyExchangeGainLoss(final Parameter _parameter) throws EFapsException {
    final AccountInfo gainAcc = AccountInfo.get4Config(_parameter, AccountingSettings.PERIOD_EXCHANGEGAIN);
    final AccountInfo lossAcc = AccountInfo.get4Config(_parameter, AccountingSettings.PERIOD_EXCHANGELOSS);

    if (gainAcc != null && lossAcc != null) {
        final QueryBuilder queryBldr = new QueryBuilder(CISales.Payment);
        queryBldr.addWhereAttrEqValue(CISales.Payment.TargetDocument, getInstance());
        final MultiPrintQuery multi = queryBldr.getPrint();
        final SelectBuilder selDocInst = new SelectBuilder().linkto(CISales.Payment.FromAbstractLink)
                .instance();
        final SelectBuilder selCurInst = new SelectBuilder().linkto(CISales.Payment.CurrencyLink).instance();
        multi.addSelect(selDocInst, selCurInst);
        multi.addAttribute(CISales.Payment.Amount, CISales.Payment.Date);
        multi.execute();
        while (multi.next()) {
            final Instance docInst = multi.getSelect(selDocInst);
            final PrintQuery print = new PrintQuery(docInst);
            final SelectBuilder selDocCurInst = new SelectBuilder()
                    .linkto(CISales.DocumentSumAbstract.RateCurrencyId).instance();
            print.addSelect(selDocCurInst);
            print.addAttribute(CIERP.DocumentAbstract.Date);
            print.execute();
            final Instance curInst = multi.getSelect(selCurInst);
            final Instance docCurInst = print.getSelect(selDocCurInst);
            final DateTime docDate = print.getAttribute(CIERP.DocumentAbstract.Date);
            final DateTime dateTmp = multi.getAttribute(CISales.Payment.Date);
            final BigDecimal amountTmp = multi.getAttribute(CISales.Payment.Amount);

            if (!curInst.equals(Currency.getBaseCurrency()) || !docCurInst.equals(Currency.getBaseCurrency())) {
                final Currency currency = new Currency();
                final RateInfo[] rateInfos1 = currency.evaluateRateInfos(_parameter, dateTmp, curInst,
                        docCurInst);
                final RateInfo[] rateInfos2 = currency.evaluateRateInfos(_parameter, docDate, curInst,
                        docCurInst);
                final int idx;
                // payment in BaseCurreny ==> Document was not BaseCurrency therefore current against target
                if (curInst.equals(Currency.getBaseCurrency())) {
                    idx = 2;
                    // Document in  BaseCurrency ==> payment was not BaseCurrency therefore current against base
                } else if (docCurInst.equals(Currency.getBaseCurrency())) {
                    idx = 0;
                    // neither Document nor payment are BaseCurrency but are the same
                } else if (curInst.equals(docCurInst)) {
                    idx = 0;
                } else {
                    idx = 0;
                }

                final BigDecimal rate1 = RateInfo.getRate(_parameter, rateInfos1[idx],
                        docInst.getType().getName());
                final BigDecimal rate2 = RateInfo.getRate(_parameter, rateInfos2[idx],
                        docInst.getType().getName());
                if (rate1.compareTo(rate2) != 0) {
                    final BigDecimal amount1 = amountTmp.divide(rate1, BigDecimal.ROUND_HALF_UP);
                    final BigDecimal amount2 = amountTmp.divide(rate2, BigDecimal.ROUND_HALF_UP);
                    BigDecimal gainLoss = amount1.subtract(amount2);
                    if (idx == 2) {
                        gainLoss = gainLoss.multiply(rate1);
                    }
                    if (gainLoss.compareTo(BigDecimal.ZERO) != 0) {
                        final boolean out = getInstance().getType()
                                .isKindOf(CISales.PaymentDocumentOutAbstract);
                        if (out) {
                            final boolean gain = gainLoss.compareTo(BigDecimal.ZERO) > 0;
                            for (final AccountInfo accinfo : getCreditAccounts()) {
                                if (accinfo.getDocLink() != null && accinfo.getDocLink().equals(docInst)) {
                                    final BigDecimal accAmount;
                                    if (accinfo.getRateInfo().getCurrencyInstance()
                                            .equals(Currency.getBaseCurrency())) {
                                        accAmount = gainLoss;
                                    } else {
                                        accAmount = gainLoss.multiply(accinfo.getRate(_parameter));
                                    }
                                    accinfo.addAmount(accAmount.negate());
                                }
                            }
                            if (gain) {
                                gainAcc.setAmount(gainLoss.abs()).setRateInfo(RateInfo.getDummyRateInfo(),
                                        getInstance().getType().getName());
                                addCredit(gainAcc);
                            } else {
                                lossAcc.setAmount(gainLoss.abs()).setRateInfo(RateInfo.getDummyRateInfo(),
                                        getInstance().getType().getName());
                                addDebit(lossAcc);
                            }
                        } else {
                            final boolean gain = gainLoss.compareTo(BigDecimal.ZERO) < 0;
                            for (final AccountInfo accinfo : getDebitAccounts()) {
                                if (accinfo.getDocLink() != null && accinfo.getDocLink().equals(docInst)) {
                                    final BigDecimal accAmount;
                                    if (!accinfo.getRateInfo().getCurrencyInstance()
                                            .equals(Currency.getBaseCurrency())) {
                                        accAmount = gainLoss;
                                    } else {
                                        accAmount = gainLoss.multiply(accinfo.getRate(_parameter));
                                    }
                                    accinfo.addAmount(accAmount);
                                }
                            }
                            if (gain) {
                                gainAcc.setAmount(gainLoss.abs()).setRateInfo(RateInfo.getDummyRateInfo(),
                                        getInstance().getType().getName());
                                addDebit(gainAcc);
                            } else {
                                lossAcc.setAmount(gainLoss.abs()).setRateInfo(RateInfo.getDummyRateInfo(),
                                        getInstance().getType().getName());
                                addCredit(lossAcc);
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:nl.strohalm.cyclos.services.transactions.PaymentServiceImpl.java

/**
 * Validates the given amount//www . ja  v a  2  s .  c o  m
 */
private void validateAmount(final BigDecimal amount, Account fromAccount, final Account toAccount,
        final Transfer transfer) {
    // Validate the from account credit limit ...
    final LocalSettings localSettings = settingsService.getLocalSettings();
    if (fromAccount != null) {
        final BigDecimal creditLimit = fromAccount.getCreditLimit();
        if (creditLimit != null) {
            // ... only if not unlimited
            final AccountStatus fromStatus = accountService.getCurrentStatus(new AccountDTO(fromAccount));
            if (creditLimit.abs().floatValue() > -PRECISION_DELTA) {
                final BigDecimal available = localSettings.round(fromStatus.getAvailableBalance());
                if (available.subtract(amount).floatValue() < -PRECISION_DELTA) {
                    final boolean isOriginalAccount = transfer == null ? true
                            : fromAccount.equals(transfer.getRootTransfer().getFrom());
                    fromAccount = fetchService.fetch(fromAccount, Account.Relationships.TYPE);
                    throw new NotEnoughCreditsException(fromAccount, amount, isOriginalAccount);
                }
            }
        }
    }

    // Validate the to account upper credit limit
    if (toAccount != null) {
        final BigDecimal upperCreditLimit = toAccount.getUpperCreditLimit();
        if (upperCreditLimit != null && upperCreditLimit.floatValue() > PRECISION_DELTA) {
            final BigDecimal balance = accountService.getBalance(new AccountDateDTO(toAccount));
            if (upperCreditLimit.subtract(balance).subtract(amount).floatValue() < -PRECISION_DELTA) {
                throw new UpperCreditLimitReachedException(
                        localSettings.getUnitsConverter(toAccount.getType().getCurrency().getPattern())
                                .toString(toAccount.getUpperCreditLimit()),
                        toAccount, amount);
            }
        }
    }
}