List of usage examples for java.math BigDecimal negate
public BigDecimal negate()
From source file:org.kuali.kpme.tklm.leave.accrual.service.AccrualServiceImpl.java
@SuppressWarnings("unchecked") @Override/*w w w .java2 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.totschnig.myexpenses.activity.ExpenseEdit.java
/** * sets the state of the UI on mTransaction * * @return false if any data is not valid, also informs user through toast *///from w w w . j av a 2s.com protected boolean syncStateAndValidate(boolean forSave) { boolean validP = true; String title; Account account = getCurrentAccount(); if (account == null) return false; BigDecimal amount = validateAmountInput(forSave); if (amount == null) { //Toast is shown in validateAmountInput validP = false; } else { if (mType == EXPENSE) { amount = amount.negate(); } mTransaction.getAmount().setCurrency(account.currency); mTransaction.getAmount().setAmountMajor(amount);//TODO refactor to better respect encapsulation } mTransaction.accountId = account.getId(); mTransaction.comment = mCommentText.getText().toString(); if (!(mTransaction instanceof SplitPartCategory || mTransaction instanceof SplitPartTransfer)) { mTransaction.setDate(mCalendar.getTime()); } if (mOperationType == MyExpenses.TYPE_TRANSACTION) { mTransaction.setCatId(mCatId); } if (mIsMainTransactionOrTemplate) { mTransaction.setPayee(mPayeeText.getText().toString()); long selected = mMethodSpinner.getSelectedItemId(); mTransaction.methodId = (selected != AdapterView.INVALID_ROW_ID && selected > 0) ? selected : null; } if (mOperationType == MyExpenses.TYPE_TRANSFER) { mTransaction.transfer_account = mTransferAccountSpinner.getSelectedItemId(); final Account transferAccount = Account.getInstanceFromDb(mTransferAccountSpinner.getSelectedItemId()); boolean isSame = account.currency.equals(transferAccount.currency); if (mTransaction instanceof Template) { if (!isSame && amount == null) { BigDecimal transferAmount = validateAmountInput(mTransferAmountText, forSave); if (transferAmount != null) { mTransaction.accountId = transferAccount.getId(); mTransaction.transfer_account = account.getId(); if (mType == INCOME) { transferAmount = transferAmount.negate(); } mTransaction.setAmount(new Money(transferAccount.currency, transferAmount)); mAmountText.setError(null); validP = true; //we only need either amount or transfer amount } } } else { mTransaction.getTransferAmount().setCurrency(transferAccount.currency); if (isSame) { if (amount != null) mTransaction.getTransferAmount().setAmountMajor(amount.negate()); } else { BigDecimal transferAmount = validateAmountInput(mTransferAmountText, forSave); if (transferAmount == null) { //Toast is shown in validateAmountInput validP = false; } else { if (mType == INCOME) { transferAmount = transferAmount.negate(); } mTransaction.getTransferAmount().setAmountMajor(transferAmount); } } } } if (mTransaction instanceof Template) { title = mTitleText.getText().toString(); if (title.equals("")) { mTitleText.setError(getString(R.string.no_title_given)); validP = false; } ((Template) mTransaction).setTitle(title); if (mPlan == null) { if (mReccurenceSpinner.getSelectedItemPosition() > 0) { String description = ((Template) mTransaction).compileDescription(ExpenseEdit.this); mPlan = new Plan(mCalendar, ((Plan.Recurrence) mReccurenceSpinner.getSelectedItem()).toRrule(mCalendar), ((Template) mTransaction).getTitle(), description); ((Template) mTransaction).setPlan(mPlan); } } else { mPlan.description = ((Template) mTransaction).compileDescription(ExpenseEdit.this); mPlan.title = title; ((Template) mTransaction).setPlan(mPlan); } } else { mTransaction.referenceNumber = mReferenceNumberText.getText().toString(); if (forSave && !(mTransaction instanceof SplitPartCategory || mTransaction instanceof SplitPartTransfer)) { if (mReccurenceSpinner.getSelectedItemPosition() > 0) { title = TextUtils.isEmpty(mTransaction.payee) ? (TextUtils.isEmpty(mLabel) ? (TextUtils.isEmpty(mTransaction.comment) ? getString(R.string.menu_create_template) : mTransaction.comment) : mLabel) : mTransaction.payee; mTransaction.originTemplate = new Template(mTransaction, title); mTransaction.originTemplate.setPlanExecutionAutomatic(true); String description = mTransaction.originTemplate.compileDescription(ExpenseEdit.this); mTransaction.originTemplate.setPlan(new Plan(mCalendar, ((Plan.Recurrence) mReccurenceSpinner.getSelectedItem()).toRrule(mCalendar), mTransaction.originTemplate.getTitle(), description)); } } } mTransaction.crStatus = (Transaction.CrStatus) mStatusSpinner.getSelectedItem(); mTransaction.setPictureUri(mPictureUri); return validP; }
From source file:org.kuali.ole.module.purap.document.service.impl.PaymentRequestServiceImpl.java
/** * Generates a PurAP accounting line and adds to the specified tax item. * * @param taxItem The specified tax item the accounting line will be associated with. * @param taxableAmount The amount to which tax is computed against. * @return A fully populated PurApAccountingLine instance for the specified tax item. *///from w ww .j av a2s . co m protected PurApAccountingLine addTaxAccountingLine(PurApItem taxItem, BigDecimal taxableAmount) { PaymentRequestDocument preq = taxItem.getPurapDocument(); PurApAccountingLine taxLine = null; try { taxLine = (PurApAccountingLine) taxItem.getAccountingLineClass().newInstance(); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Unable to access sourceAccountingLineClass", e); } catch (InstantiationException e) { throw new IllegalArgumentException("Unable to instantiate sourceAccountingLineClass", e); } // tax item type indicators boolean isFederalTax = ItemTypeCodes.ITEM_TYPE_FEDERAL_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isFederalGross = ItemTypeCodes.ITEM_TYPE_FEDERAL_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isStateTax = ItemTypeCodes.ITEM_TYPE_STATE_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isStateGross = ItemTypeCodes.ITEM_TYPE_STATE_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isFederal = isFederalTax || isFederalGross; // true for federal tax/gross; false for state tax/gross boolean isGross = isFederalGross || isStateGross; // true for federal/state gross, false for federal/state tax // obtain accounting line info according to tax item type code String taxChart = null; String taxAccount = null; String taxObjectCode = null; if (isGross) { // for gross up tax items, use preq's first item's first accounting line, which shall exist at this point AccountingLine line1 = preq.getFirstAccount(); taxChart = line1.getChartOfAccountsCode(); taxAccount = line1.getAccountNumber(); taxObjectCode = line1.getFinancialObjectCode(); } else if (isFederalTax) { // for federal tax item, get chart, account, object code info from parameters taxChart = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, preq.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve federal tax parameters."); throw new RuntimeException("Unable to retrieve federal tax parameters."); } } else if (isStateTax) { // for state tax item, get chart, account, object code info from parameters taxChart = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, preq.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve state tax parameters."); throw new RuntimeException("Unable to retrieve state tax parameters."); } } // calculate tax amount according to gross up indicator and federal/state tax type /* * The formula of tax and gross up amount are as follows: if (not gross up) gross not existing taxFederal/State = - amount * * rateFederal/State otherwise gross up grossFederal/State = amount * rateFederal/State / (1 - rateFederal - rateState) tax * = - gross */ // pick federal/state tax rate BigDecimal taxPercentFederal = preq.getTaxFederalPercent(); BigDecimal taxPercentState = preq.getTaxStatePercent(); BigDecimal taxPercent = isFederal ? taxPercentFederal : taxPercentState; // divider value according to gross up or not BigDecimal taxDivider = new BigDecimal(100); if (preq.getTaxGrossUpIndicator()) { taxDivider = taxDivider.subtract(taxPercentFederal.add(taxPercentState)); } // tax = amount * rate / divider BigDecimal taxAmount = taxableAmount.multiply(taxPercent); taxAmount = taxAmount.divide(taxDivider, 5, BigDecimal.ROUND_HALF_UP); // tax is always negative, since it reduces the total amount; while gross up is always the positive of tax if (!isGross) { taxAmount = taxAmount.negate(); } // populate necessary accounting line fields taxLine.setDocumentNumber(preq.getDocumentNumber()); taxLine.setSequenceNumber(preq.getNextSourceLineNumber()); taxLine.setChartOfAccountsCode(taxChart); taxLine.setAccountNumber(taxAccount); taxLine.setFinancialObjectCode(taxObjectCode); taxLine.setAmount(new KualiDecimal(taxAmount)); // add the accounting line to the item taxLine.setItemIdentifier(taxItem.getItemIdentifier()); taxLine.setPurapItem(taxItem); taxItem.getSourceAccountingLines().add(taxLine); return taxLine; }
From source file:org.kuali.ole.module.purap.document.service.impl.InvoiceServiceImpl.java
/** * Generates a PurAP accounting line and adds to the specified tax item. * * @param taxItem The specified tax item the accounting line will be associated with. * @param taxableAmount The amount to which tax is computed against. * @return A fully populated PurApAccountingLine instance for the specified tax item. *//*from w ww .j a v a2 s .c om*/ protected PurApAccountingLine addTaxAccountingLine(PurApItem taxItem, BigDecimal taxableAmount) { InvoiceDocument prqs = taxItem.getPurapDocument(); PurApAccountingLine taxLine = null; try { taxLine = (PurApAccountingLine) taxItem.getAccountingLineClass().newInstance(); } catch (IllegalAccessException e) { throw new IllegalArgumentException("Unable to access sourceAccountingLineClass", e); } catch (InstantiationException e) { throw new IllegalArgumentException("Unable to instantiate sourceAccountingLineClass", e); } // tax item type indicators boolean isFederalTax = ItemTypeCodes.ITEM_TYPE_FEDERAL_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isFederalGross = ItemTypeCodes.ITEM_TYPE_FEDERAL_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isStateTax = ItemTypeCodes.ITEM_TYPE_STATE_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isStateGross = ItemTypeCodes.ITEM_TYPE_STATE_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isFederal = isFederalTax || isFederalGross; // true for federal tax/gross; false for state tax/gross boolean isGross = isFederalGross || isStateGross; // true for federal/state gross, false for federal/state tax // obtain accounting line info according to tax item type code String taxChart = null; String taxAccount = null; String taxObjectCode = null; if (isGross) { // for gross up tax items, use prqs's first item's first accounting line, which shall exist at this point AccountingLine line1 = prqs.getFirstAccount(); taxChart = line1.getChartOfAccountsCode(); taxAccount = line1.getAccountNumber(); taxObjectCode = line1.getFinancialObjectCode(); } else if (isFederalTax) { // for federal tax item, get chart, account, object code info from parameters taxChart = getParameter(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, OLEConstants.InvoiceDocument.CMPNT_CD, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = getParameter(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, OLEConstants.InvoiceDocument.CMPNT_CD, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(InvoiceDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, prqs.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve federal tax parameters."); throw new RuntimeException("Unable to retrieve federal tax parameters."); } } else if (isStateTax) { // for state tax item, get chart, account, object code info from parameters taxChart = getParameter(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, OLEConstants.InvoiceDocument.CMPNT_CD, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = getParameter(OLEConstants.OptionalModuleNamespaces.PURCHASING_ACCOUNTS_PAYABLE, OLEConstants.InvoiceDocument.CMPNT_CD, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(InvoiceDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, prqs.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve state tax parameters."); throw new RuntimeException("Unable to retrieve state tax parameters."); } } // calculate tax amount according to gross up indicator and federal/state tax type /* * The formula of tax and gross up amount are as follows: if (not gross up) gross not existing taxFederal/State = - amount * * rateFederal/State otherwise gross up grossFederal/State = amount * rateFederal/State / (1 - rateFederal - rateState) tax * = - gross */ // pick federal/state tax rate BigDecimal taxPercentFederal = prqs.getTaxFederalPercent(); BigDecimal taxPercentState = prqs.getTaxStatePercent(); BigDecimal taxPercent = isFederal ? taxPercentFederal : taxPercentState; // divider value according to gross up or not BigDecimal taxDivider = new BigDecimal(100); if (prqs.getTaxGrossUpIndicator()) { taxDivider = taxDivider.subtract(taxPercentFederal.add(taxPercentState)); } // tax = amount * rate / divider BigDecimal taxAmount = taxableAmount.multiply(taxPercent); taxAmount = taxAmount.divide(taxDivider, 5, BigDecimal.ROUND_HALF_UP); // tax is always negative, since it reduces the total amount; while gross up is always the positive of tax if (!isGross) { taxAmount = taxAmount.negate(); } // populate necessary accounting line fields taxLine.setDocumentNumber(prqs.getDocumentNumber()); taxLine.setSequenceNumber(prqs.getNextSourceLineNumber()); taxLine.setChartOfAccountsCode(taxChart); taxLine.setAccountNumber(taxAccount); taxLine.setFinancialObjectCode(taxObjectCode); taxLine.setAmount(new KualiDecimal(taxAmount)); // add the accounting line to the item taxLine.setItemIdentifier(taxItem.getItemIdentifier()); taxLine.setPurapItem(taxItem); taxItem.getSourceAccountingLines().add(taxLine); return taxLine; }
From source file:org.kuali.kfs.module.purap.document.service.impl.PaymentRequestServiceImpl.java
/** * Generates a PurAP accounting line and adds to the specified tax item. * * @param taxItem The specified tax item the accounting line will be associated with. * @param taxableAmount The amount to which tax is computed against. * @return A fully populated PurApAccountingLine instance for the specified tax item. *//* w w w . j ava2s .c o m*/ protected PurApAccountingLine addTaxAccountingLine(PurApItem taxItem, BigDecimal taxableAmount) { PaymentRequestDocument preq = taxItem.getPurapDocument(); PurApAccountingLine taxLine = null; try { taxLine = (PurApAccountingLine) taxItem.getAccountingLineClass().newInstance(); } catch (IllegalAccessException e) { throw new InfrastructureException("Unable to access sourceAccountingLineClass", e); } catch (InstantiationException e) { throw new InfrastructureException("Unable to instantiate sourceAccountingLineClass", e); } // tax item type indicators boolean isFederalTax = ItemTypeCodes.ITEM_TYPE_FEDERAL_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isFederalGross = ItemTypeCodes.ITEM_TYPE_FEDERAL_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isStateTax = ItemTypeCodes.ITEM_TYPE_STATE_TAX_CODE.equals(taxItem.getItemTypeCode()); boolean isStateGross = ItemTypeCodes.ITEM_TYPE_STATE_GROSS_CODE.equals(taxItem.getItemTypeCode()); boolean isFederal = isFederalTax || isFederalGross; // true for federal tax/gross; false for state tax/gross boolean isGross = isFederalGross || isStateGross; // true for federal/state gross, false for federal/state tax // obtain accounting line info according to tax item type code String taxChart = null; String taxAccount = null; String taxObjectCode = null; if (isGross) { // for gross up tax items, use preq's first item's first accounting line, which shall exist at this point AccountingLine line1 = preq.getFirstAccount(); taxChart = line1.getChartOfAccountsCode(); taxAccount = line1.getAccountNumber(); taxObjectCode = line1.getFinancialObjectCode(); } else if (isFederalTax) { // for federal tax item, get chart, account, object code info from parameters taxChart = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.FEDERAL_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, preq.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve federal tax parameters."); throw new RuntimeException("Unable to retrieve federal tax parameters."); } } else if (isStateTax) { // for state tax item, get chart, account, object code info from parameters taxChart = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_CHART_SUFFIX); taxAccount = parameterService.getParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_ACCOUNT_SUFFIX); taxObjectCode = parameterService.getSubParameterValueAsString(PaymentRequestDocument.class, NRATaxParameters.STATE_TAX_PARM_PREFIX + NRATaxParameters.TAX_PARM_OBJECT_BY_INCOME_CLASS_SUFFIX, preq.getTaxClassificationCode()); if (StringUtils.isBlank(taxChart) || StringUtils.isBlank(taxAccount) || StringUtils.isBlank(taxObjectCode)) { LOG.error("Unable to retrieve state tax parameters."); throw new RuntimeException("Unable to retrieve state tax parameters."); } } // calculate tax amount according to gross up indicator and federal/state tax type /* * The formula of tax and gross up amount are as follows: if (not gross up) gross not existing taxFederal/State = - amount * * rateFederal/State otherwise gross up grossFederal/State = amount * rateFederal/State / (1 - rateFederal - rateState) tax * = - gross */ // pick federal/state tax rate BigDecimal taxPercentFederal = preq.getTaxFederalPercent(); BigDecimal taxPercentState = preq.getTaxStatePercent(); BigDecimal taxPercent = isFederal ? taxPercentFederal : taxPercentState; // divider value according to gross up or not BigDecimal taxDivider = new BigDecimal(100); if (preq.getTaxGrossUpIndicator()) { taxDivider = taxDivider.subtract(taxPercentFederal.add(taxPercentState)); } // tax = amount * rate / divider BigDecimal taxAmount = taxableAmount.multiply(taxPercent); taxAmount = taxAmount.divide(taxDivider, 5, BigDecimal.ROUND_HALF_UP); // tax is always negative, since it reduces the total amount; while gross up is always the positive of tax if (!isGross) { taxAmount = taxAmount.negate(); } // populate necessary accounting line fields taxLine.setDocumentNumber(preq.getDocumentNumber()); taxLine.setSequenceNumber(preq.getNextSourceLineNumber()); taxLine.setChartOfAccountsCode(taxChart); taxLine.setAccountNumber(taxAccount); taxLine.setFinancialObjectCode(taxObjectCode); taxLine.setAmount(new KualiDecimal(taxAmount)); // add the accounting line to the item taxLine.setItemIdentifier(taxItem.getItemIdentifier()); taxLine.setPurapItem(taxItem); taxItem.getSourceAccountingLines().add(taxLine); return taxLine; }
From source file:com.prcsteel.platform.account.service.impl.AccountServiceImpl.java
@Override public AccountCreditDto payForCredit(Long accountId, BigDecimal amount, String option, User user) { Date date = new Date(); String code = custGroupingInforService.createCode(); if (Constant.OPERATION_DEDUCTION.equals(option)) { // accountFundService.updateAccountFund(accountId, AssociationType.CREDIT_SERIAL, code, AccountTransApplyType.CREDITLIMIT_TRANSTO_ACCOUNT, amount, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, amount, BigDecimal.ZERO, PayType.BALANCE, user.getId(), user.getName(), date); //? accountFundService.updateAccountFund(accountId, AssociationType.CREDIT_SERIAL, code, AccountTransApplyType.SECONDARY_SETTLEMENT_ACCOUNT_BALANCES, amount.negate(), BigDecimal.ZERO, amount, BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, PayType.BALANCE, user.getId(), user.getName(), date); // } else if (Constant.OPERATION_RESTITUTION.equals(option)) { // accountFundService.updateAccountFund(accountId, AssociationType.CREDIT_SERIAL, code, AccountTransApplyType.INTO_THE_CAPITAL_ACCOUNT, amount, BigDecimal.ZERO, amount.negate(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, PayType.BALANCE, user.getId(), user.getName(), date); // accountFundService.updateAccountFund(accountId, AssociationType.CREDIT_SERIAL, code, AccountTransApplyType.CREDITLIMI_TREPAYMENT, amount.negate(), BigDecimal.ZERO, BigDecimal.ZERO, BigDecimal.ZERO, amount.negate(), BigDecimal.ZERO, PayType.BALANCE, user.getId(), user.getName(), date); // }// ww w . j a v a2s.c om return accountDao.selectActualCreditBalanceById(accountId); }
From source file:org.openbravo.erpCommon.ad_forms.DocInvoice.java
/** * Create Facts (the accounting logic) for ARI, ARC, ARF, API, APC. * //w ww. j a v a 2 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:org.openbravo.advpaymentmngt.actionHandler.AddPaymentActionHandler.java
private OBError processPayment(FIN_Payment payment, String strAction, String strDifferenceAction, BigDecimal refundAmount, BigDecimal exchangeRate, JSONObject jsonparams, String comingFrom) throws Exception { ConnectionProvider conn = new DalConnectionProvider(true); VariablesSecureApp vars = RequestContext.get().getVariablesSecureApp(); AdvPaymentMngtDao dao = new AdvPaymentMngtDao(); BigDecimal assignedAmount = BigDecimal.ZERO; for (FIN_PaymentDetail paymentDetail : payment.getFINPaymentDetailList()) { assignedAmount = assignedAmount.add(paymentDetail.getAmount()); }//w ww . ja v a 2s. c o m if (assignedAmount.compareTo(payment.getAmount()) == -1) { FIN_PaymentScheduleDetail refundScheduleDetail = dao.getNewPaymentScheduleDetail( payment.getOrganization(), payment.getAmount().subtract(assignedAmount)); dao.getNewPaymentDetail(payment, refundScheduleDetail, payment.getAmount().subtract(assignedAmount), BigDecimal.ZERO, false, null); } OBError message = FIN_AddPayment.processPayment(vars, conn, (strAction.equals("PRP") || strAction.equals("PPP")) ? "P" : "D", payment, comingFrom); String strNewPaymentMessage = OBMessageUtils .parseTranslation("@PaymentCreated@" + " " + payment.getDocumentNo()) + "."; if (!"Error".equalsIgnoreCase(message.getType())) { message.setMessage(strNewPaymentMessage + " " + message.getMessage()); message.setType(message.getType().toLowerCase()); } else { conn = new DalConnectionProvider(true); OBDal.getInstance().getSession().clear(); payment = OBDal.getInstance().get(FIN_Payment.class, payment.getId()); addCredit(payment, jsonparams, refundAmount, strDifferenceAction); } if (!strDifferenceAction.equals("refund")) { return message; } boolean newPayment = !payment.getFINPaymentDetailList().isEmpty(); JSONObject creditToUseGrid = jsonparams.getJSONObject("credit_to_use"); JSONArray selectedCreditLines = creditToUseGrid.getJSONArray("_selection"); String strSelectedCreditLinesIds = null; if (selectedCreditLines.length() > 0) { strSelectedCreditLinesIds = getSelectedCreditLinesIds(selectedCreditLines); } FIN_Payment refundPayment = FIN_AddPayment.createRefundPayment(conn, vars, payment, refundAmount.negate(), exchangeRate); // If refunded credit is generated in the same payment, add payment id to // strSelectedCreditLinesIds BigDecimal actualPayment = new BigDecimal(jsonparams.getString("actual_payment")); if (actualPayment.compareTo(BigDecimal.ZERO) != 0) { if (!StringUtils.isEmpty(strSelectedCreditLinesIds)) { strSelectedCreditLinesIds = "(" + payment.getId() + ", " + strSelectedCreditLinesIds.substring(1); } else { strSelectedCreditLinesIds = "(" + payment.getId() + ")"; } } OBError auxMessage = FIN_AddPayment.processPayment(vars, conn, (strAction.equals("PRP") || strAction.equals("PPP")) ? "P" : "D", refundPayment, comingFrom, strSelectedCreditLinesIds); if (newPayment && !"Error".equalsIgnoreCase(auxMessage.getType())) { final String strNewRefundPaymentMessage = OBMessageUtils .parseTranslation("@APRM_RefundPayment@" + ": " + refundPayment.getDocumentNo()) + "."; message.setMessage(strNewRefundPaymentMessage + " " + message.getMessage()); if (payment.getGeneratedCredit().compareTo(BigDecimal.ZERO) != 0) { payment.setDescription(payment.getDescription() + strNewRefundPaymentMessage + "\n"); OBDal.getInstance().save(payment); OBDal.getInstance().flush(); } } else { message = auxMessage; } return message; }
From source file:nl.strohalm.cyclos.services.transactions.PaymentServiceImpl.java
@Override public StatisticalResultDTO getSimulateConversionGraph(final ConversionSimulationDTO input) { final LocalSettings localSettings = settingsService.getLocalSettings(); final byte precision = (byte) localSettings.getPrecision().getValue(); // get series final TransactionFeePreviewForRatesDTO temp = simulateConversion(input); final int series = temp.getFees().size(); // get range of points, but without values for A < 0 BigDecimal initialARate = null; RatesResultDTO rates = new RatesResultDTO(); if (input.isUseActualRates()) { rates = rateService.getRatesForTransferFrom(input.getAccount(), input.getAmount(), null); rates.setDate(input.getDate());//from w ww .jav a2s . c o m initialARate = rates.getaRate(); } else { initialARate = input.getArate(); } // lowerlimit takes care that values for A < 0 are left out of the graph final Double lowerLimit = (initialARate == null) ? null : initialARate.negate().doubleValue(); final Number[] xRange = GraphHelper.getOptimalRangeAround(0, 33, 0, 0.8, lowerLimit); // Data structure to build the table final Number[][] tableCells = new Number[xRange.length][series]; // initialize series names and x labels final String[] seriesNames = new String[series]; final byte[] seriesOrder = new byte[series]; final Calendar[] xPointDates = new Calendar[xRange.length]; final Calendar now = Calendar.getInstance(); BigDecimal inputARate = temp.getARate(); BigDecimal inputDRate = temp.getDRate(); // assign data for (int i = 0; i < xRange.length; i++) { final ConversionSimulationDTO inputPointX = (ConversionSimulationDTO) input.clone(); final Calendar date = (Calendar) ((input.isUseActualRates()) ? input.getDate().clone() : now.clone()); date.add(Calendar.DAY_OF_YEAR, xRange[i].intValue()); xPointDates[i] = date; // Set useActualRates for this input to false, otherwise simulateConversion will use the account's the balance and rates of that date, and // we don't want that. inputPointX.setUseActualRates(false); if (inputARate != null) { final BigDecimal aRate = inputARate.add(new BigDecimal(xRange[i].doubleValue())); inputPointX.setArate(aRate); } if (inputDRate != null) { final BigDecimal dRate = inputDRate.subtract(new BigDecimal(xRange[i].doubleValue())); inputPointX.setDrate(dRate); } final TransactionFeePreviewDTO tempResult = simulateConversion(inputPointX); int j = 0; for (final TransactionFee fee : tempResult.getFees().keySet()) { tableCells[i][j] = new StatisticalNumber(tempResult.getFees().get(fee).doubleValue(), precision); byte index; switch (fee.getChargeType()) { case D_RATE: index = 2; break; case A_RATE: case MIXED_A_D_RATES: index = 3; break; default: index = 1; break; } seriesOrder[j] = index; seriesNames[j++] = fee.getName(); } } // create the graph object final StatisticalResultDTO result = new StatisticalResultDTO(tableCells); result.setBaseKey("conversionSimulation.result.graph"); result.setHelpFile("account_management"); // date labels along x-axis final String[] rowKeys = new String[xRange.length]; Arrays.fill(rowKeys, ""); result.setRowKeys(rowKeys); for (int i = 0; i < rowKeys.length; i++) { final String rowHeader = localSettings.getDateConverterForGraphs().toString(xPointDates[i]); result.setRowHeader(rowHeader, i); } // mark the actual date upon which the x-axis is based as a vertical line final Calendar baseDate = (input.isUseActualRates()) ? (Calendar) input.getDate().clone() : now; final String baseDateString = localSettings.getDateConverterForGraphs().toString(baseDate); final Marker[] markers = new Marker[1]; markers[0] = new CategoryMarker(baseDateString); markers[0].setPaint(Color.ORANGE); final String todayString = localSettings.getDateConverterForGraphs().toString(now); if (todayString.equals(baseDateString)) { markers[0].setLabel("global.today"); } result.setDomainMarkers(markers); // Series labels indicate fee names final String[] columnKeys = new String[series]; Arrays.fill(columnKeys, ""); result.setColumnKeys(columnKeys); for (int j = 0; j < columnKeys.length; j++) { result.setColumnHeader(seriesNames[j], j); } // order the series result.orderSeries(seriesOrder); final TransferType tt = fetchService.fetch(input.getTransferType(), RelationshipHelper.nested(TransferType.Relationships.FROM, AccountType.Relationships.CURRENCY)); result.setYAxisUnits(tt.getCurrency().getSymbol()); result.setShowTable(false); result.setGraphType(StatisticalResultDTO.GraphType.STACKED_AREA); return result; }
From source file:org.ofbiz.accounting.invoice.InvoiceServices.java
public static Map<String, Object> createCommissionInvoices(DispatchContext dctx, Map<String, Object> context) { Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); GenericValue userLogin = (GenericValue) context.get("userLogin"); Locale locale = (Locale) context.get("locale"); List<String> salesInvoiceIds = UtilGenerics.checkList(context.get("invoiceIds")); List<Map<String, String>> invoicesCreated = FastList.newInstance(); Map<String, List<Map<String, Object>>> commissionParties = FastMap.newInstance(); for (String salesInvoiceId : salesInvoiceIds) { List<String> salesRepPartyIds = UtilGenerics.checkList(context.get("partyIds")); BigDecimal amountTotal = InvoiceWorker.getInvoiceTotal(delegator, salesInvoiceId); if (amountTotal.signum() == 0) { Debug.logWarning("Invoice [" + salesInvoiceId + "] has an amount total of [" + amountTotal + "], so no commission invoice will be created", module); return ServiceUtil.returnError(UtilProperties.getMessage(resource, "AccountingInvoiceCommissionZeroInvoiceAmount", locale)); }/*from www . j av a2s . c om*/ BigDecimal appliedFraction = amountTotal.divide(amountTotal, 12, ROUNDING); GenericValue invoice = null; boolean isReturn = false; List<String> billFromVendorInvoiceRoles = new ArrayList<String>(); List<GenericValue> invoiceItems = new ArrayList<GenericValue>(); try { List<EntityExpr> invoiceRoleConds = UtilMisc.toList( EntityCondition.makeCondition("invoiceId", EntityOperator.EQUALS, salesInvoiceId), EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, "BILL_FROM_VENDOR")); EntityQuery roleQuery = EntityQuery.use(delegator).select("partyId").from("InvoiceRole") .where(invoiceRoleConds); billFromVendorInvoiceRoles = EntityUtil.getFieldListFromEntityList(roleQuery.queryList(), "partyId", true); invoiceRoleConds = UtilMisc.toList( EntityCondition.makeCondition("invoiceId", EntityOperator.EQUALS, salesInvoiceId), EntityCondition.makeCondition("roleTypeId", EntityOperator.EQUALS, "SALES_REP")); // if the receiving parties is empty then we will create commission invoices for all sales agent associated to sales invoice. if (UtilValidate.isEmpty(salesRepPartyIds)) { salesRepPartyIds = EntityUtil.getFieldListFromEntityList( roleQuery.where(invoiceRoleConds).queryList(), "partyId", true); if (UtilValidate.isEmpty(salesRepPartyIds)) { return ServiceUtil.returnError(UtilProperties.getMessage(resource, "No party found with role sales representative for sales invoice " + salesInvoiceId, locale)); } } else { List<String> salesInvoiceRolePartyIds = EntityUtil.getFieldListFromEntityList( roleQuery.where(invoiceRoleConds).queryList(), "partyId", true); if (UtilValidate.isNotEmpty(salesInvoiceRolePartyIds)) { salesRepPartyIds = UtilGenerics.checkList( CollectionUtils.intersection(salesRepPartyIds, salesInvoiceRolePartyIds)); } } invoice = EntityQuery.use(delegator).from("Invoice").where("invoiceId", salesInvoiceId).queryOne(); String invoiceTypeId = invoice.getString("invoiceTypeId"); if ("CUST_RTN_INVOICE".equals(invoiceTypeId)) { isReturn = true; } else if (!"SALES_INVOICE".equals(invoiceTypeId)) { Debug.logWarning("This type of invoice has no commission; returning success", module); return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingInvoiceCommissionInvalid", locale)); } invoiceItems = EntityQuery.use(delegator).from("InvoiceItem").where("invoiceId", salesInvoiceId) .queryList(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } // Map of commission Lists (of Maps) for each party. // Determine commissions for various parties. for (GenericValue invoiceItem : invoiceItems) { BigDecimal amount = ZERO; BigDecimal quantity = ZERO; quantity = invoiceItem.getBigDecimal("quantity"); amount = invoiceItem.getBigDecimal("amount"); amount = isReturn ? amount.negate() : amount; String productId = invoiceItem.getString("productId"); String invoiceItemSeqId = invoiceItem.getString("invoiceItemSeqId"); String invoiceId = invoiceItem.getString("invoiceId"); // Determine commission parties for this invoiceItem if (UtilValidate.isNotEmpty(productId)) { Map<String, Object> resultMap = null; try { resultMap = dispatcher.runSync("getCommissionForProduct", UtilMisc.<String, Object>toMap("productId", productId, "invoiceId", invoiceId, "invoiceItemSeqId", invoiceItemSeqId, "invoiceItemTypeId", invoiceItem.getString("invoiceItemTypeId"), "amount", amount, "quantity", quantity, "userLogin", userLogin)); } catch (GenericServiceException e) { return ServiceUtil.returnError(e.getMessage()); } // build a Map of partyIds (both to and from) in a commission and the amounts // Note that getCommissionForProduct returns a List of Maps with a lot values. See services.xml definition for reference. List<Map<String, Object>> itemCommissions = UtilGenerics .checkList(resultMap.get("commissions")); if (UtilValidate.isNotEmpty(itemCommissions)) { for (Map<String, Object> commissionMap : itemCommissions) { commissionMap.put("invoice", invoice); commissionMap.put("appliedFraction", appliedFraction); if (!billFromVendorInvoiceRoles.contains(commissionMap.get("partyIdFrom")) || !salesRepPartyIds.contains(commissionMap.get("partyIdTo"))) { continue; } String partyIdFromTo = (String) commissionMap.get("partyIdFrom") + (String) commissionMap.get("partyIdTo"); if (!commissionParties.containsKey(partyIdFromTo)) { commissionParties.put(partyIdFromTo, UtilMisc.toList(commissionMap)); } else { (commissionParties.get(partyIdFromTo)).add(commissionMap); } } } } } } Timestamp now = UtilDateTime.nowTimestamp(); // Create invoice for each commission receiving party for (Map.Entry<String, List<Map<String, Object>>> commissionParty : commissionParties.entrySet()) { List<GenericValue> toStore = FastList.newInstance(); List<Map<String, Object>> commList = commissionParty.getValue(); // get the billing parties if (UtilValidate.isEmpty(commList)) { continue; } // From and To are reversed between commission and invoice String partyIdBillTo = (String) (commList.get(0)).get("partyIdFrom"); String partyIdBillFrom = (String) (commList.get(0)).get("partyIdTo"); GenericValue invoice = (GenericValue) (commList.get(0)).get("invoice"); BigDecimal appliedFraction = (BigDecimal) (commList.get(0)).get("appliedFraction"); Long days = (Long) (commList.get(0)).get("days"); // create the invoice record // To and From are in commission's sense, opposite for invoice Map<String, Object> createInvoiceMap = FastMap.newInstance(); createInvoiceMap.put("partyId", partyIdBillTo); createInvoiceMap.put("partyIdFrom", partyIdBillFrom); createInvoiceMap.put("invoiceDate", now); // if there were days associated with the commission agreement, then set a dueDate for the invoice. if (days != null) { createInvoiceMap.put("dueDate", UtilDateTime.getDayEnd(now, days)); } createInvoiceMap.put("invoiceTypeId", "COMMISSION_INVOICE"); // start with INVOICE_IN_PROCESS, in the INVOICE_READY we can't change the invoice (or shouldn't be able to...) createInvoiceMap.put("statusId", "INVOICE_IN_PROCESS"); createInvoiceMap.put("currencyUomId", invoice.getString("currencyUomId")); createInvoiceMap.put("userLogin", userLogin); // store the invoice first Map<String, Object> createInvoiceResult = null; try { createInvoiceResult = dispatcher.runSync("createInvoice", createInvoiceMap); } catch (GenericServiceException e) { return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingInvoiceCommissionError", locale), null, null, createInvoiceResult); } String invoiceId = (String) createInvoiceResult.get("invoiceId"); // create the bill-from (or pay-to) contact mech as the primary PAYMENT_LOCATION of the party from the store GenericValue partyContactMechPurpose = null; try { partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose") .where("partyId", partyIdBillTo, "contactMechPurposeTypeId", "BILLING_LOCATION") .queryFirst(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } if (partyContactMechPurpose != null) { GenericValue invoiceContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId, "contactMechId", partyContactMechPurpose.getString("contactMechId"), "contactMechPurposeTypeId", "BILLING_LOCATION")); toStore.add(invoiceContactMech); } try { partyContactMechPurpose = EntityQuery.use(delegator).from("PartyContactMechPurpose") .where("partyId", partyIdBillTo, "contactMechPurposeTypeId", "PAYMENT_LOCATION") .queryFirst(); } catch (GenericEntityException e) { return ServiceUtil.returnError(e.getMessage()); } if (partyContactMechPurpose != null) { GenericValue invoiceContactMech = delegator.makeValue("InvoiceContactMech", UtilMisc.toMap("invoiceId", invoiceId, "contactMechId", partyContactMechPurpose.getString("contactMechId"), "contactMechPurposeTypeId", "PAYMENT_LOCATION")); toStore.add(invoiceContactMech); } // create the item records for (Map<String, Object> commissionMap : commList) { BigDecimal elemAmount = ((BigDecimal) commissionMap.get("commission")).multiply(appliedFraction); BigDecimal quantity = (BigDecimal) commissionMap.get("quantity"); String invoiceIdFrom = (String) commissionMap.get("invoiceId"); String invoiceItemSeqIdFrom = (String) commissionMap.get("invoiceItemSeqId"); elemAmount = elemAmount.setScale(DECIMALS, ROUNDING); Map<String, Object> resMap = null; try { resMap = dispatcher.runSync("createInvoiceItem", UtilMisc.toMap("invoiceId", invoiceId, "productId", commissionMap.get("productId"), "invoiceItemTypeId", "COMM_INV_ITEM", "quantity", quantity, "amount", elemAmount, "userLogin", userLogin)); dispatcher.runSync("createInvoiceItemAssoc", UtilMisc.toMap("invoiceIdFrom", invoiceIdFrom, "invoiceItemSeqIdFrom", invoiceItemSeqIdFrom, "invoiceIdTo", invoiceId, "invoiceItemSeqIdTo", resMap.get("invoiceItemSeqId"), "invoiceItemAssocTypeId", "COMMISSION_INVOICE", "partyIdFrom", partyIdBillFrom, "partyIdTo", partyIdBillTo, "quantity", quantity, "amount", elemAmount, "userLogin", userLogin)); } catch (GenericServiceException e) { return ServiceUtil.returnError(e.getMessage()); } if (ServiceUtil.isError(resMap)) { return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingInvoiceCommissionErrorItem", locale), null, null, resMap); } } // store value objects try { delegator.storeAll(toStore); } catch (GenericEntityException e) { Debug.logError(e, "Entity/data problem creating commission invoice: " + e.toString(), module); return ServiceUtil.returnError( UtilProperties.getMessage(resource, "AccountingInvoiceCommissionEntityDataProblem", UtilMisc.toMap("reason", e.toString()), locale)); } invoicesCreated.add(UtilMisc.<String, String>toMap("commissionInvoiceId", invoiceId, "salesRepresentative ", partyIdBillFrom)); } String invCreated = new Integer(invoicesCreated.size()).toString(); Map<String, Object> result = ServiceUtil.returnSuccess(UtilProperties.getMessage(resource, "AccountingCommissionInvoicesCreated", UtilMisc.toMap("invoicesCreated", invCreated), locale)); Debug.logInfo("Created Commission invoices for each commission receiving parties " + invCreated, module); result.put("invoicesCreated", invoicesCreated); return result; }