Search in sources :

Example 26 with Money

use of org.javamoney.moneta.Money in project rulesservice by genny-project.

the class FeeCalculationTest method driverTest.

// @Test
public void driverTest() {
    Scanner scanner = new Scanner(System.in);
    System.out.print("Enter DRIVER Price   ::   ");
    String ownerPriceString = scanner.nextLine();
    Double driverPriceDouble = Double.parseDouble(ownerPriceString);
    Money driverPrice = Money.of(driverPriceDouble, DEFAULT_CURRENCY_AUD);
    Money driverFeeExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverFeeIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money ownerPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceIncGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    Money driverPriceExcGST = Money.of(0, DEFAULT_CURRENCY_AUD);
    QRules rules = new QRules(null, null, null);
    /* fee calculation */
    driverFeeExcGST = rules.calcOwnerFee(driverPrice);
    if (driverPrice.compareTo(Money.of(300, DEFAULT_CURRENCY_AUD)) > 0) {
        if (driverFeeExcGST.compareTo(Money.of(100, DEFAULT_CURRENCY_AUD)) < 0) {
            driverFeeExcGST = Money.of(100, DEFAULT_CURRENCY_AUD);
        }
    }
    driverFeeIncGST = rules.includeGSTMoney(driverFeeExcGST);
    /* price calculation */
    driverPriceExcGST = driverPrice;
    driverPriceIncGST = rules.includeGSTMoney(driverPriceExcGST);
    ownerPriceExcGST = driverPriceExcGST.add(driverFeeExcGST);
    ownerPriceIncGST = rules.includeGSTMoney(ownerPriceExcGST);
    System.out.println("-------------------------------------------");
    System.out.println("DRIVER");
    System.out.println("-------------------------------------------");
    System.out.println("FEES");
    System.out.println("driverFeeExcGST  ::  " + driverFeeExcGST.getNumber().doubleValue());
    System.out.println("driverFeeIncGST  ::  " + driverFeeIncGST.getNumber().doubleValue());
    System.out.println("\n");
    System.out.println("PRICES");
    System.out.println("driverPrice         ::  " + driverPrice.getNumber().doubleValue());
    System.out.println("ownerPriceExcGST   ::  " + ownerPriceExcGST.getNumber().doubleValue());
    System.out.println("ownerPriceIncGST   ::  " + ownerPriceIncGST.getNumber().doubleValue());
    System.out.println("driverPriceExcGST  ::  " + driverPriceExcGST.getNumber().doubleValue());
    System.out.println("driverPriceIncGST  ::  " + driverPriceIncGST.getNumber().doubleValue());
}
Also used : Scanner(java.util.Scanner) Money(org.javamoney.moneta.Money) QRules(life.genny.rules.QRules)

Example 27 with Money

use of org.javamoney.moneta.Money in project rulesservice by genny-project.

the class QRules method excludeGSTMoney.

public Money excludeGSTMoney(Money price) {
    Money gstPrice = Money.of(0, price.getCurrency());
    if (price.compareTo(gstPrice) > 0) {
        Money priceToBeIncluded = price.multiply(0.1);
        gstPrice = price.subtract(priceToBeIncluded);
    }
    return gstPrice;
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money)

Example 28 with Money

use of org.javamoney.moneta.Money in project rulesservice by genny-project.

the class QRules method acceptJob.

public void acceptJob(QEventBtnClickMessage m) {
    /* Get beg.getCode(), username, userCode, userFullName */
    // Get Baseentity once so we don't need to keep
    BaseEntity beg = getBaseEntityByCode(m.getItemCode());
    // fetching...
    println("beg.getCode()  ::   " + beg.getCode());
    String userCode = getUser().getCode();
    println("usercode   ::   " + getUser().getCode());
    String userName = getAsString("preferred_username");
    println("username   ::   " + userName);
    String userFullName = getFullName(getUser());
    println("user fullName   ::   " + userFullName);
    String linkCode = "LNK_BEG";
    String linkOffer = "OFFER";
    String linkQuoter = "QUOTER";
    String linkOwner = "OWNER";
    String linkCreator = "CREATOR";
    Optional<String> optOwnerCode = beg.getValue("PRI_AUTHOR");
    String ownerCode = null;
    if (optOwnerCode.isPresent()) {
        ownerCode = optOwnerCode.get();
    } else {
        ownerCode = QwandaUtils.getSourceOrTargetForGroupLink("GRP_NEW_ITEMS", linkCode, beg.getCode(), linkOwner, false, getToken());
    }
    /* get BEG PRICEs */
    println("BEG Prices   ::   ");
    Money begPrice = beg.getLoopValue("PRI_PRICE", Money.of(0.00, "AUD"));
    Money ownerPriceExcGST = beg.getLoopValue("PRI_OWNER_PRICE_EXC_GST", Money.of(0.00, "AUD"));
    Money ownerPriceIncGST = beg.getLoopValue("PRI_OWNER_PRICE_INC_GST", Money.of(0.00, "AUD"));
    Money driverPriceExcGST = beg.getLoopValue("PRI_DRIVER_PRICE_EXC_GST", Money.of(0.00, "AUD"));
    Money driverPriceIncGST = beg.getLoopValue("PRI_DRIVER_PRICE_INC_GST", Money.of(0.00, "AUD"));
    Money feePriceExcGST = beg.getLoopValue("PRI_FEE_EXC_GST", Money.of(0.00, "AUD"));
    Money feePriceIncGST = beg.getLoopValue("PRI_FEE_INC_GST", Money.of(0.00, "AUD"));
    /* Create Offer BE */
    BaseEntity offer = createBaseEntityByCode(getUser().getCode(), "OFR", "Offer");
    println("OFFER CODE   ::   " + offer.getCode());
    RulesUtils.ruleLogger("OFFER Base Entity", offer);
    /* Send beg to driver and owner should see it as part of beg link */
    VertxUtils.subscribe(realm(), offer, getUser().getCode());
    VertxUtils.subscribe(realm(), offer, ownerCode);
    /* Save attributes for OFFER as answer */
    List<Answer> answerList = new ArrayList<Answer>();
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_PRICE", JsonUtils.toJson(begPrice)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_EXC_GST", JsonUtils.toJson(ownerPriceExcGST)));
    /*
		 * answerList .add(new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_INC_GST",
		 * JsonUtils.toJson(ownerPriceIncGST)));
		 */
    Answer owIncGST = new Answer(getUser(), offer, "PRI_OFFER_OWNER_PRICE_INC_GST", JsonUtils.toJson(ownerPriceIncGST));
    owIncGST.setChangeEvent(false);
    answerList.add(owIncGST);
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DRIVER_PRICE_EXC_GST", JsonUtils.toJson(driverPriceExcGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DRIVER_PRICE_INC_GST", JsonUtils.toJson(driverPriceIncGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_FEE_EXC_GST", JsonUtils.toJson(feePriceExcGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_FEE_INC_GST", JsonUtils.toJson(feePriceIncGST)));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_CODE", offer.getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_CODE", getUser().getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_USERNAME", userName));
    answerList.add(new Answer(getUser(), offer, "PRI_QUOTER_FULLNAME", userFullName));
    answerList.add(new Answer(getUser(), offer, "PRI_BEG_CODE", beg.getCode()));
    answerList.add(new Answer(getUser(), offer, "PRI_NEXT_ACTION", linkOwner));
    answerList.add(new Answer(getUser(), offer, "PRI_OFFER_DATE", getCurrentLocalDateTime()));
    saveAnswers(answerList);
    /* Update the number of offers for BEG */
    Integer offerCount = beg.getLoopValue("PRI_OFFER_COUNT", 0);
    offerCount = offerCount + 1;
    println("Offer Count is   ::   " + offerCount);
    saveAnswer(new Answer(beg.getCode(), beg.getCode(), "PRI_OFFER_COUNT", offerCount.toString()));
    /* Determine the recipient code */
    String[] recipients = VertxUtils.getSubscribers(realm(), beg.getCode());
    println("BEG subscribers   ::   " + Arrays.toString(recipients));
    /* link BEG and OFFER BE || OFFER */
    createLink(beg.getCode(), offer.getCode(), linkCode, linkOffer, 1.0);
    /* link BEG and QUOTER BE || QUOTER */
    createLink(beg.getCode(), getUser().getCode(), linkCode, linkQuoter, 1.0);
    /* link OFFER and QUOTER BE || CREATOR */
    createLink(offer.getCode(), getUser().getCode(), "LNK_OFR", linkCreator, 1.0);
    /* set Status of the job */
    /* get Owner of the job */
    BaseEntity owner = getChildren(beg.getCode(), "LNK_BEG", "OWNER");
    // updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_STATUS",
    // "#FFA500");
    updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_" + getUser().getCode(), Status.NEEDS_ACTION.value());
    updateBaseEntityAttribute(getUser().getCode(), beg.getCode(), "STA_" + owner.getCode(), Status.NEEDS_ACTION.value());
    /* SEND (OFFER, QUOTER, BEG) BaseEntitys to recipients */
    String[] offerRecipients = VertxUtils.getSubscribers(realm(), offer.getCode());
    println("OFFER subscribers   ::   " + Arrays.toString(offerRecipients));
    publishBaseEntityByCode(offer.getCode(), beg.getCode(), "LNK_BEG", offerRecipients);
    publishBaseEntityByCode(getUser().getCode(), beg.getCode(), "LNK_BEG", offerRecipients);
    publishBaseEntityByCode(beg.getCode(), "GRP_NEW_ITEMS", "LNK_CORE", offerRecipients);
    /* Messages */
    /* OWNER config */
    HashMap<String, String> contextMap = new HashMap<String, String>();
    contextMap.put("QUOTER", getUser().getCode());
    contextMap.put("JOB", beg.getCode());
    contextMap.put("OFFER", offer.getCode());
    RulesUtils.println("owner code ::" + ownerCode);
    String[] recipientArr = { ownerCode };
    /* Sending toast message to owner frontend */
    sendMessage("", recipientArr, contextMap, "MSG_CH40_ACCEPT_QUOTE_OWNER", "TOAST");
    /* QUOTER config */
    HashMap<String, String> contextMapForDriver = new HashMap<String, String>();
    contextMapForDriver.put("JOB", beg.getCode());
    contextMapForDriver.put("OWNER", ownerCode);
    contextMapForDriver.put("OFFER", offer.getCode());
    contextMapForDriver.put("QUOTER", getUser().getCode());
    String[] recipientArrForDriver = { getUser().getCode() };
    /* Sending toast message to driver frontend */
    sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_ACCEPT_QUOTE_DRIVER", "TOAST");
}
Also used : AttributeInteger(life.genny.qwanda.attribute.AttributeInteger) AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) Answer(life.genny.qwanda.Answer) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList)

Example 29 with Money

use of org.javamoney.moneta.Money in project rulesservice by genny-project.

the class QRules method calcDriverFee.

public Money calcDriverFee(Money input) {
    // TODO, why is this here?
    CurrencyUnit DEFAULT_CURRENCY_TYPE = input.getCurrency();
    Number inputNum = input.getNumber();
    Money driverFee = Money.of(0, DEFAULT_CURRENCY_TYPE);
    Number RANGE_1 = 999.99;
    Number RANGE_2 = 2999.99;
    Number RANGE_3 = 4999.99;
    Number FEE_1 = 0.15;
    Number FEE_2 = 0.125;
    Number FEE_3 = 0.09;
    Number FEE_4 = 0.05;
    Number ONE = 1;
    // const REVERSE_FEE_MULTIPLIER_1 = ( RANGE_2 - RANGE_1 ) * FEE_2;
    // const REVERSE_FEE_MULTIPLIER_2 = ( RANGE_3 - RANGE_2 ) * FEE_3;
    Number subtract01 = MoneyHelper.sub(RANGE_2, RANGE_1);
    Number subtract02 = MoneyHelper.sub(RANGE_3, RANGE_2);
    Number REVERSE_FEE_MULTIPLIER_1 = MoneyHelper.mul(subtract01, FEE_2);
    Number REVERSE_FEE_MULTIPLIER_2 = MoneyHelper.mul(subtract02, FEE_3);
    // const REVERSE_FEE_BOUNDARY_1 = RANGE_1 - ( RANGE_1 * FEE_1 );
    // const REVERSE_FEE_BOUNDARY_2 = RANGE_2 - REVERSE_FEE_MULTIPLIER_1 - ( RANGE_1
    // * FEE_1 );
    // const REVERSE_FEE_BOUNDARY_3 = RANGE_3 - REVERSE_FEE_MULTIPLIER_2 -
    // REVERSE_FEE_MULTIPLIER_1 - ( RANGE_1 * FEE_1 );
    Number multiply01 = MoneyHelper.mul(RANGE_1, FEE_1);
    Number REVERSE_FEE_BOUNDARY_1 = MoneyHelper.sub(RANGE_1, multiply01);
    Number subtract03 = MoneyHelper.sub(RANGE_2, REVERSE_FEE_MULTIPLIER_1);
    Number REVERSE_FEE_BOUNDARY_2 = MoneyHelper.sub(subtract03, multiply01);
    Number subtract04 = MoneyHelper.sub(RANGE_3, REVERSE_FEE_MULTIPLIER_2);
    Number subtract05 = MoneyHelper.sub(subtract04, REVERSE_FEE_MULTIPLIER_1);
    Number REVERSE_FEE_BOUNDARY_3 = MoneyHelper.sub(subtract05, multiply01);
    if (inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_1.doubleValue()) {
        // return calcOwnerFee( inputNum * (1 / (1 - FEE_1)));
        Number subtract = MoneyHelper.sub(ONE, FEE_1);
        Number divide = MoneyHelper.div(ONE, subtract);
        Money multiply = MoneyHelper.mul(input, divide);
        driverFee = calcOwnerFee(multiply);
        println("zone 1 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_1.doubleValue() && inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_2.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) + (( input
        // - REVERSE_FEE_BOUNDARY_1 ) * FEE_2 )) / input )));
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_1);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_2);
        Number multiply2 = MoneyHelper.mul(FEE_1, REVERSE_FEE_BOUNDARY_1);
        Money addition1 = MoneyHelper.add(multiply1, multiply2);
        Money divide1 = MoneyHelper.div(addition1, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 2 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_2.doubleValue() && inputNum.doubleValue() < REVERSE_FEE_BOUNDARY_3.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) +
        // REVERSE_FEE_MULTIPLIER_1 + (( input - REVERSE_FEE_BOUNDARY_2 ) * FEE_3 )) /
        // input )))
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_2);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_3);
        Number multiply2 = MoneyHelper.mul(REVERSE_FEE_BOUNDARY_1, FEE_1);
        Number addition1 = MoneyHelper.add(multiply2, REVERSE_FEE_MULTIPLIER_1);
        Money addition2 = MoneyHelper.add(multiply1, addition1);
        Money divide1 = MoneyHelper.div(addition2, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 3 ");
    }
    if (inputNum.doubleValue() >= REVERSE_FEE_BOUNDARY_3.doubleValue()) {
        // calcFee(( input ) * (1 / (1 - (( REVERSE_FEE_BOUNDARY_1 * FEE_1 ) +
        // REVERSE_FEE_MULTIPLIER_1 + REVERSE_FEE_MULTIPLIER_2 + (( input -
        // REVERSE_FEE_BOUNDARY_3 ) * FEE_4 )) / input )))
        Money subtract1 = MoneyHelper.sub(input, REVERSE_FEE_BOUNDARY_3);
        Money multiply1 = MoneyHelper.mul(subtract1, FEE_4);
        Number multiply2 = MoneyHelper.mul(REVERSE_FEE_BOUNDARY_1, FEE_1);
        Number addition1 = MoneyHelper.add(multiply2, REVERSE_FEE_MULTIPLIER_1);
        Number addition2 = MoneyHelper.add(addition1, REVERSE_FEE_MULTIPLIER_2);
        Money addition3 = MoneyHelper.add(multiply1, addition2);
        Money divide1 = MoneyHelper.div(addition3, input);
        Money subtract2 = MoneyHelper.sub(ONE, divide1);
        Money divide2 = MoneyHelper.div(ONE, subtract2);
        Money multiply3 = MoneyHelper.mul(input, divide2);
        driverFee = calcOwnerFee(multiply3);
        println("zone 4 ");
    }
    return driverFee;
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit)

Example 30 with Money

use of org.javamoney.moneta.Money in project tutorials by eugenp.

the class JavaMoneyUnitManualTest method givenAmounts_whenStringified_thanEquals.

@Test
public void givenAmounts_whenStringified_thanEquals() {
    CurrencyUnit usd = Monetary.getCurrency("USD");
    MonetaryAmount fstAmtUSD = Monetary.getDefaultAmountFactory().setCurrency(usd).setNumber(200).create();
    Money moneyof = Money.of(12, usd);
    FastMoney fastmoneyof = FastMoney.of(2, usd);
    assertEquals("USD", usd.toString());
    assertEquals("USD 200", fstAmtUSD.toString());
    assertEquals("USD 12", moneyof.toString());
    assertEquals("USD 2.00000", fastmoneyof.toString());
}
Also used : FastMoney(org.javamoney.moneta.FastMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit) MonetaryAmount(javax.money.MonetaryAmount) FastMoney(org.javamoney.moneta.FastMoney) Test(org.junit.Test)

Aggregations

Money (org.javamoney.moneta.Money)31 AttributeMoney (life.genny.qwanda.attribute.AttributeMoney)7 CurrencyUnit (javax.money.CurrencyUnit)4 MonetaryAmount (javax.money.MonetaryAmount)4 FastMoney (org.javamoney.moneta.FastMoney)4 Test (org.junit.Test)4 BaseEntity (life.genny.qwanda.entity.BaseEntity)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Scanner (java.util.Scanner)2 Answer (life.genny.qwanda.Answer)2 QRules (life.genny.rules.QRules)2 Test (org.testng.annotations.Test)2 IOException (java.io.IOException)1 HashSet (java.util.HashSet)1 CurrencyConversion (javax.money.convert.CurrencyConversion)1 ExchangeRateProvider (javax.money.convert.ExchangeRateProvider)1 MonetaryAmountFormat (javax.money.format.MonetaryAmountFormat)1 AttributeBoolean (life.genny.qwanda.attribute.AttributeBoolean)1