List of usage examples for java.math BigDecimal abs
public BigDecimal abs()
From source file:org.openvpms.archetype.rules.finance.till.TillRules.java
/** * Adds an adjustment to a till, if required. * * @param balance the till balance act * @param cashFloat the new cash float//from ww w . j a v a 2s . c o m */ private void addAdjustment(FinancialAct balance, BigDecimal cashFloat, Till till) { BigDecimal lastCashFloat = till.getTillFloat(); BigDecimal diff = cashFloat.subtract(lastCashFloat); if (diff.compareTo(BigDecimal.ZERO) != 0) { // need to generate an adjustment, and associate it with the balance boolean credit = (lastCashFloat.compareTo(cashFloat) > 0); Act adjustment = createTillBalanceAdjustment(till.getEntity(), diff.abs(), credit); ActBean balanceBean = new ActBean(balance); balanceBean.addNodeRelationship("items", adjustment); service.save(adjustment); // NOTE that this will trigger TillBalanceRules.addToTill(), but will have no effect TillHelper.updateBalance(balanceBean, service); } }
From source file:org.openbravo.advpaymentmngt.actionHandler.ModifyPaymentPlanActionHandler.java
/** * Given an invoice, checks that all the amounts in the grid are not lower than paid amount or * awaiting execution amount// ww w .ja v a2 s.c o m * * @throws JSONException */ private String validateGridAmounts(JSONArray gridRows, Invoice invoice) throws JSONException { boolean positive = invoice.getFINPaymentScheduleList().get(0).getAmount().compareTo(BigDecimal.ZERO) >= 0; for (int indGrid = 0; indGrid < gridRows.length(); indGrid++) { JSONObject jo = gridRows.getJSONObject(indGrid); BigDecimal outstanding = new BigDecimal(jo.getString("outstanding")); BigDecimal awaitingExecution = new BigDecimal(jo.getString("awaitingExecutionAmount")); if (awaitingExecution.abs().compareTo(outstanding.abs()) > 0) { return "@APRM_AwaitingExecutionAmountError@"; } if (outstanding.compareTo(BigDecimal.ZERO) != 0 && (positive != (outstanding.compareTo(BigDecimal.ZERO) > 0))) { return "@APRM_DifferentSignError@"; } } return null; }
From source file:org.pentaho.di.jdbc.Support.java
/** * Normalize a BigDecimal value so that it fits within the * available precision./*from w w w . j a v a2 s .com*/ * * @param value The decimal value to normalize. * @param maxPrecision The decimal precision supported by the server * (assumed to be a value of either 28 or 38). * @return The possibly normalized decimal value as a <code>BigDecimal</code>. * @throws SQLException If the number is too big. */ static BigDecimal normalizeBigDecimal(BigDecimal value, int maxPrecision) throws SQLException { if (value == null) { return null; } if (value.scale() < 0) { // Java 1.5 BigDecimal allows negative scales. // jTDS cannot send these so re-scale. value = value.setScale(0); } if (value.scale() > maxPrecision) { // This is an optimization to quickly adjust the scale of a // very precise BD value. For example // BigDecimal((double)1.0/3.0) yields a BD 54 digits long! value = value.setScale(maxPrecision, BigDecimal.ROUND_HALF_UP); } BigInteger max = MAX_VALUE_38; while (value.abs().unscaledValue().compareTo(max) > 0) { // OK we need to reduce the scale if possible to preserve // the integer part of the number and still fit within the // available precision. int scale = value.scale() - 1; if (scale < 0) { // Can't do it number just too big throw new SQLException( BaseMessages.getString(PKG, "error.normalize.numtoobig", String.valueOf(maxPrecision)), "22000"); } value = value.setScale(scale, BigDecimal.ROUND_HALF_UP); } return value; }
From source file:org.openvpms.hl7.impl.RDSProcessor.java
/** * Adds an order item.// w w w . j a va 2 s . c om * * @param group the order group * @param state the state */ private void addItem(RDS_O13_ORDER group, State state) { BigDecimal quantity = getQuantity(group); ActBean bean; ActBean itemBean; if (quantity.signum() >= 0) { bean = state.getOrder(); itemBean = state.createOrderItem(); } else { bean = state.getReturn(); itemBean = state.createReturnItem(); quantity = quantity.abs(); } String fillerOrderNumber = group.getORC().getFillerOrderNumber().getEntityIdentifier().getValue(); if (fillerOrderNumber != null) { itemBean.setValue("reference", fillerOrderNumber); } FinancialAct invoiceItem = addInvoiceItem(group.getORC(), bean, itemBean, state); addClinician(group, bean, itemBean, invoiceItem); Product product = addProduct(group, bean, itemBean); if (product != null) { checkSellingUnits(group, bean, product); } itemBean.setValue("quantity", quantity); }
From source file:rems.Program.java
private static boolean postIntoSuspnsAccnt(BigDecimal aeVal, BigDecimal crlVal, int orgID, boolean isspcl, String[] errmsg) {//from w w w.ja v a 2 s. c om try { int suspns_accnt = Global.get_Suspns_Accnt(orgID); int net_accnt = Global.get_Net_Income_Accnt(orgID); int ret_accnt = Global.get_Rtnd_Erngs_Accnt(orgID); if (suspns_accnt == -1) { errmsg[0] += "Please define a suspense Account First before imbalance can be Auto-Corrected!"; return false; } long suspns_batch_id = -999999991; int funcCurrID = Global.getOrgFuncCurID(orgID); BigDecimal dffrnc = aeVal.subtract(crlVal); String incrsDcrs = "D"; if (dffrnc.compareTo(BigDecimal.valueOf(0.00)) < 0) { incrsDcrs = "I"; } BigDecimal imbalAmnt = dffrnc.abs(); double netAmnt = (double) Global.dbtOrCrdtAccntMultiplier(suspns_accnt, incrsDcrs) * imbalAmnt.doubleValue(); String dateStr = Global.getFrmtdDB_Date_time(); if (!Global.isTransPrmttd(suspns_accnt, dateStr, netAmnt, errmsg)) { return false; } if (Global.dbtOrCrdtAccnt(suspns_accnt, incrsDcrs).equals("Debit")) { Global.createTransaction(suspns_accnt, "Correction of Imbalance as at " + dateStr, imbalAmnt.doubleValue(), dateStr, funcCurrID, suspns_batch_id, 0.00, netAmnt, imbalAmnt.doubleValue(), funcCurrID, imbalAmnt.doubleValue(), funcCurrID, (double) 1, (double) 1, "D"); } else { Global.createTransaction(suspns_accnt, "Correction of Imbalance as at " + dateStr, 0.00, dateStr, funcCurrID, suspns_batch_id, imbalAmnt.doubleValue(), netAmnt, imbalAmnt.doubleValue(), funcCurrID, imbalAmnt.doubleValue(), funcCurrID, (double) 1, (double) 1, "C"); } ResultSet dtst = Global.get_Batch_Trns(suspns_batch_id); dtst.last(); int dtstTtl = dtst.getRow(); dtst.beforeFirst(); for (int i = 0; i < dtstTtl; i++) { dtst.next(); boolean hsBnUpdt = Global.hsTrnsUptdAcntBls(Long.parseLong(dtst.getString(1)), dtst.getString(7), Integer.parseInt(dtst.getString(10))); if (hsBnUpdt == false) { double dbt1 = Double.parseDouble(dtst.getString(5)); double crdt1 = Double.parseDouble(dtst.getString(6)); double net1 = Double.parseDouble(dtst.getString(11)); Global.postTransaction(Integer.parseInt(dtst.getString(10)), dbt1, crdt1, net1, dtst.getString(7), Long.parseLong(dtst.getString(1))); Global.chngeTrnsStatus(Long.parseLong(dtst.getString(1)), "1"); } } Program.reloadAcntChrtBals(suspns_batch_id, net_accnt); return true; } catch (SQLException ex) { errmsg[0] += ex.getMessage() + "\r\n\r\n" + Arrays.toString(ex.getStackTrace()); return false; } catch (NumberFormatException ex) { errmsg[0] += ex.getMessage() + "\r\n\r\n" + Arrays.toString(ex.getStackTrace()); return false; } }
From source file:sbml.test.UploadUnzipTest.java
private final boolean tolerable(double expectedVal, double actualVal, double absTol, double relTol) { if (Double.isInfinite(expectedVal)) return (expectedVal == actualVal); else if (Double.isNaN(expectedVal)) return Double.isNaN(actualVal); else if (Double.isNaN(actualVal) || Double.isInfinite(actualVal)) return false; else {//from ww w .j ava 2s.c o m BigDecimal expected = new BigDecimal(expectedVal); MathContext mc = new MathContext(expected.precision()); BigDecimal adjusted = new BigDecimal(actualVal).round(mc); BigDecimal actualDiff = expected.subtract(adjusted).abs(); BigDecimal rtol = new BigDecimal(relTol); BigDecimal atol = new BigDecimal(absTol); BigDecimal allowableDiff = atol.add(rtol.multiply(expected.abs())); return (actualDiff.compareTo(allowableDiff) <= 0); } }
From source file:org.egov.wtms.application.service.collection.ConnectionBillService.java
/** * Creates the advance bill details//from w w w . j av a 2s .co m * * @param billDetails * @param orderMap * @param currentInstallmentDemand * @param demandDetail * @param reason * @param installment */ private void createAdvanceBillDetails(List<EgBillDetails> billDetails, BigDecimal currentInstallmentDemand, EgDemand demand, Billable billable, List<Installment> advanceInstallments, Installment dmdDetInstallment) { /* * Advance will be created with current year second half installment. While fetching advance collection, we will pass * current year second half installment */ BigDecimal advanceCollection = demandGenericDAO.getBalanceByDmdMasterCodeInst(demand, DEMANDRSN_CODE_ADVANCE, moduleService.getModuleByName(MODULE_NAME), dmdDetInstallment); CFinancialYear finYear = financialYearDAO.getFinancialYearByDate(new Date()); if (advanceCollection.signum() < 0) advanceCollection = advanceCollection.abs(); BigDecimal partiallyCollectedAmount = ZERO; if (currentInstallmentDemand.signum() > 0) partiallyCollectedAmount = advanceCollection.remainder(currentInstallmentDemand); if (currentInstallmentDemand.signum() > 0) { Integer noOfAdvancesPaid = advanceCollection.subtract(partiallyCollectedAmount) .divide(currentInstallmentDemand).intValue(); if (LOG.isDebugEnabled()) LOG.debug("getBilldetails - advanceCollection = {}, noOfAdvancesPaid={}", advanceCollection, noOfAdvancesPaid); Installment installment; int j; if (noOfAdvancesPaid < MAX_ADVANCES_ALLOWED) for (int i = noOfAdvancesPaid; i < advanceInstallments.size(); i++) { installment = advanceInstallments.get(i); EgDemandReason reasonmaster = connectionDemandService .getDemandReasonByCodeAndInstallment(DEMANDRSN_CODE_ADVANCE, installment); if (reasonmaster != null) { EgBillDetails billdetail = new EgBillDetails(); billdetail.setDrAmount(ZERO); billdetail.setCrAmount(currentInstallmentDemand); billdetail.setGlcode(GLCODE_FOR_ADVANCE); billdetail.setEgDemandReason(reasonmaster); billdetail.setCreateDate(new Date()); billdetail.setModifiedDate(new Date()); j = billDetails.size() + 1; billdetail.setOrderNo(j); billdetail.setDescription(reasonmaster.getEgDemandReasonMaster().getReasonMaster() + " - " + installment.getDescription()); if (billdetail.getDescription().contains(DEMANDRSN_REASON_ADVANCE)) billdetail.setPurpose(PURPOSE.ADVANCE_AMOUNT.toString()); else if (billdetail.getEgDemandReason().getEgInstallmentMaster().getToDate() .compareTo(finYear.getStartingDate()) < 0) billdetail.setPurpose(PURPOSE.ARREAR_AMOUNT.toString()); else if (billdetail.getEgDemandReason().getEgInstallmentMaster().getFromDate() .compareTo(finYear.getStartingDate()) >= 0 && billdetail.getEgDemandReason().getEgInstallmentMaster().getToDate() .compareTo(finYear.getEndingDate()) >= 0) billdetail.setPurpose(PURPOSE.CURRENT_AMOUNT.toString()); else billdetail.setPurpose(PURPOSE.OTHERS.toString()); billdetail.setFunctionCode(STRING_WCMS_FUCNTION_CODE); billdetail.setAdditionalFlag(0); billDetails.add(billdetail); } } } else if (LOG.isDebugEnabled()) LOG.debug("getBillDetails - All advances are paid..."); }
From source file:rems.Program.java
private static void correctIntrfcImbals(String intrfcTblNm) { try {/*from w w w . jav a 2 s. com*/ int suspns_accnt = Global.get_Suspns_Accnt(Global.UsrsOrg_ID); ResultSet dteDtSt = Global.get_Intrfc_dateSums(intrfcTblNm, Global.UsrsOrg_ID); dteDtSt.last(); int ttlRws = dteDtSt.getRow(); dteDtSt.beforeFirst(); if (ttlRws > 0 && suspns_accnt > 0) { String msg1 = ""; for (int i = 0; i < ttlRws; i++) { dteDtSt.next(); double dlyDbtAmnt = Double.parseDouble(dteDtSt.getString(2)); double dlyCrdtAmnt = Double.parseDouble(dteDtSt.getString(3)); int orgID = Global.UsrsOrg_ID; if (dlyDbtAmnt != dlyCrdtAmnt) { //long suspns_batch_id = glBatchID; int funcCurrID = Global.getOrgFuncCurID(orgID); BigDecimal dffrnc = BigDecimal.valueOf(dlyDbtAmnt - dlyCrdtAmnt); String incrsDcrs = "D"; if (dffrnc.compareTo(BigDecimal.valueOf(0.00)) < 0) { incrsDcrs = "I"; } BigDecimal imbalAmnt = dffrnc.abs(); double netAmnt = (double) Global.dbtOrCrdtAccntMultiplier(suspns_accnt, incrsDcrs) * imbalAmnt.doubleValue(); SimpleDateFormat frmtr = new SimpleDateFormat("dd-MMM-yyyy"); SimpleDateFormat frmtr1 = new SimpleDateFormat("yyyy-MM-dd"); String dateStr1 = frmtr.format(frmtr1.parse(dteDtSt.getString(1)).getTime()) + " 00:00:00"; String dateStr = Global.getFrmtdDB_Date_time(); if (Global.getIntrfcTrnsID(intrfcTblNm, suspns_accnt, netAmnt, dteDtSt.getString(1) + " 00:00:00") > 0) { continue; } if (Global.dbtOrCrdtAccnt(suspns_accnt, incrsDcrs).equals("Debit")) { if (intrfcTblNm.equals("scm.scm_gl_interface")) { Global.createScmGLIntFcLn(suspns_accnt, "Correction of Imbalance in GL Interface Table as at " + dateStr1, imbalAmnt.doubleValue(), dateStr1, funcCurrID, 0, netAmnt, "Imbalance Correction", -1, -1, dateStr, "USR"); } else { Global.createPayGLIntFcLn(suspns_accnt, "Correction of Imbalance in GL Interface Table as at " + dateStr1, imbalAmnt.doubleValue(), dateStr1, funcCurrID, 0, netAmnt, dateStr, "USR"); } } else if (intrfcTblNm.equals("scm.scm_gl_interface")) { Global.createScmGLIntFcLn(suspns_accnt, "Correction of Imbalance in GL Interface Table as at " + dateStr1, 0, dateStr1, funcCurrID, imbalAmnt.doubleValue(), netAmnt, "Imbalance Correction", -1, -1, dateStr, "USR"); } else { Global.createPayGLIntFcLn(suspns_accnt, "Correction of Imbalance in GL Interface Table as at " + dateStr1, imbalAmnt.doubleValue(), dateStr1, funcCurrID, 0, netAmnt, dateStr, "USR"); } } } } else { //Global.mnFrm.cmCde.showMsg("There's no Imbalance to correct!", 0); //return; } } catch (SQLException ex) { } catch (NumberFormatException ex) { } catch (ParseException ex) { } }
From source file:org.openbravo.advpaymentmngt.utility.FIN_Utility.java
public static BigDecimal getDepositAmount(Boolean isReceipt, BigDecimal amount) { BigDecimal deposit = BigDecimal.ZERO; if (isReceipt) { if (amount.compareTo(BigDecimal.ZERO) == 1) { deposit = amount;/* w w w. j av a 2 s. c o m*/ } // else received payment was negative so treat as payment } else { if (amount.compareTo(BigDecimal.ZERO) == -1) { // Negative payment out is a deposit deposit = amount.abs(); } } return deposit; }
From source file:rems.Program.java
public static void validateBatchNPost(long glBatchID, String batchStatus, String batchSource, long msg_id, String log_tbl, String dateStr) { try {//from w w w.ja v a 2 s . c o m SimpleDateFormat frmtr = new SimpleDateFormat("dd-MMM-yyyy"); SimpleDateFormat frmtr1 = new SimpleDateFormat("yyyy-MM-dd"); Global.updtActnPrcss(5); batchStatus = Global.getGnrlRecNm("accb.accb_trnsctn_batches", "batch_id", "batch_status", glBatchID); if (batchStatus.equals("1")) { Global.updateLogMsg(msg_id, "\r\nCannot Post an already Posted Batch of Transactions!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } int suspns_accnt = Global.get_Suspns_Accnt(Global.UsrsOrg_ID); ResultSet dteDtSt = Global.get_Batch_dateSums(glBatchID); dteDtSt.last(); int ttlRows = dteDtSt.getRow(); dteDtSt.beforeFirst(); if (ttlRows > 0 && suspns_accnt > 0) { String msg1 = ""; for (int i = 0; i < ttlRows; i++) { dteDtSt.next(); double dlyDbtAmnt = Double.parseDouble(dteDtSt.getString(2)); double dlyCrdtAmnt = Double.parseDouble(dteDtSt.getString(3)); int orgID = Global.UsrsOrg_ID; if (dlyDbtAmnt != dlyCrdtAmnt) { long suspns_batch_id = glBatchID; int funcCurrID = Global.getOrgFuncCurID(orgID); BigDecimal dffrnc = BigDecimal.valueOf(dlyDbtAmnt - dlyCrdtAmnt); String incrsDcrs = "D"; if (dffrnc.compareTo(BigDecimal.valueOf(0.00)) < 0) { incrsDcrs = "I"; } BigDecimal imbalAmnt = dffrnc.abs(); double netAmnt = (double) Global.dbtOrCrdtAccntMultiplier(suspns_accnt, incrsDcrs) * imbalAmnt.doubleValue(); String dateStr1 = frmtr.format(frmtr1.parse(dteDtSt.getString(1)).getTime()) + " 00:00:00"; if (Global.dbtOrCrdtAccnt(suspns_accnt, incrsDcrs).equals("Debit")) { Global.createTransaction(suspns_accnt, "Correction of Imbalance in GL Batch " + Global.getGnrlRecNm( "accb.accb_trnsctn_batches", "batch_id", "batch_name", glBatchID) + " as at " + dateStr1, imbalAmnt.doubleValue(), dateStr1, funcCurrID, suspns_batch_id, 0.00, netAmnt, imbalAmnt.doubleValue(), funcCurrID, imbalAmnt.doubleValue(), funcCurrID, (double) 1, (double) 1, "D"); } else { Global.createTransaction(suspns_accnt, "Correction of Imbalance in GL Batch " + Global.getGnrlRecNm( "accb.accb_trnsctn_batches", "batch_id", "batch_name", glBatchID) + " as at " + dateStr1, 0.00, dateStr1, funcCurrID, suspns_batch_id, imbalAmnt.doubleValue(), netAmnt, imbalAmnt.doubleValue(), funcCurrID, imbalAmnt.doubleValue(), funcCurrID, (double) 1, (double) 1, "C"); } } } } else { //Global.mnFrm.cmCde.showMsg("There's no Imbalance to correct!", 0); //return; } if (Global.get_Batch_CrdtSum(glBatchID) != Global.get_Batch_DbtSum(glBatchID)) { Global.updateLogMsg(msg_id, "\r\nCannot Post an Unbalanced Batch of Transactions!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } Global.updtActnPrcss(5); ResultSet dtst = Global.get_Batch_Trns_NoStatus(glBatchID); dtst.last(); long ttltrns = dtst.getRow(); dtst.beforeFirst(); if (ttltrns <= 0 && !batchSource.equals("Period Close Process")) { Global.updateLogMsg(msg_id, "\r\nOnly Period Close Process Batches can be posted when the batch has no transactions!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } int ret_accnt = Global.get_Rtnd_Erngs_Accnt(Global.UsrsOrg_ID); int net_accnt = Global.get_Net_Income_Accnt(Global.UsrsOrg_ID); if (ret_accnt == -1) { Global.updateLogMsg(msg_id, "\r\nUntil a Retained Earnings Account is defined\r\n no Transaction can be posted into the Accounting!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } if (net_accnt == -1) { Global.updateLogMsg(msg_id, "\r\nUntil a Net Income Account is defined\r\n no Transaction can be posted into the Accounting!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } Global.updtActnPrcss(5); ResultSet dteDtSt1 = Global.get_Batch_dateSums(glBatchID); dteDtSt1.last(); int dteDtSt1Ttl = dteDtSt1.getRow(); dteDtSt1.beforeFirst(); if (dteDtSt1Ttl > 0) { String msg1 = "Your transactions will cause your Balance Sheet to become Unbalanced on some Days!" + "Please make sure each day has equal debits and credits. Check the ff Days:" + "\r\n"; for (int i = 0; i < dteDtSt1Ttl; i++) { dteDtSt1.next(); msg1 = msg1 + dteDtSt1.getString(1) + "\t DR=" + dteDtSt1.getString(2) + "\t CR=" + dteDtSt1.getString(3) + "\r\n"; } Global.updateLogMsg(msg_id, "\r\n" + msg1 + "!\r\n", log_tbl, dateStr, Global.rnUser_ID); return; } int funCurID = Global.getOrgFuncCurID(Global.UsrsOrg_ID); Global.updtActnPrcss(5); Program.postGLBatch(glBatchID, batchSource, msg_id, log_tbl, dateStr, net_accnt, funCurID); } catch (SQLException ex) { } catch (NumberFormatException ex) { } catch (ParseException ex) { } }