Search in sources :

Example 1 with Money

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

the class QRules method calcOwnerFee.

public Money calcOwnerFee(Money input) {
    CurrencyUnit DEFAULT_CURRENCY_TYPE = input.getCurrency();
    Number inputNum = input.getNumber();
    Money ownerFee = 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 RANGE_1_COMPONENT = MoneyHelper.mul(inputNum, FEE_1);
    Number RANGE_2_COMPONENT = MoneyHelper.mul(RANGE_1, FEE_1);
    ;
    Number RANGE_3_COMPONENT = MoneyHelper.mul(MoneyHelper.sub(RANGE_2, RANGE_1), FEE_2);
    Number RANGE_4_COMPONENT = MoneyHelper.mul(MoneyHelper.sub(RANGE_3, RANGE_2), FEE_3);
    if (inputNum.doubleValue() <= RANGE_1.doubleValue()) {
        // RANGE_1_COMPONENT
        ownerFee = Money.of(RANGE_1_COMPONENT, DEFAULT_CURRENCY_TYPE);
        println("range 1 ");
    }
    if (inputNum.doubleValue() > RANGE_1.doubleValue() && inputNum.doubleValue() <= RANGE_2.doubleValue()) {
        // RANGE_2_COMPONENT + (input - RANGE_1) * FEE_2
        println(input);
        Money subtract = MoneyHelper.sub(input, RANGE_1);
        println(subtract);
        Money multiply = MoneyHelper.mul(subtract, FEE_2);
        println(multiply);
        ownerFee = MoneyHelper.add(multiply, RANGE_2_COMPONENT);
        println(ownerFee);
        println("range 2 ");
    }
    if (inputNum.doubleValue() > RANGE_2.doubleValue() && inputNum.doubleValue() <= RANGE_3.doubleValue()) {
        // RANGE_2_COMPONENT + RANGE_3_COMPONENT + (input - RANGE_2) * FEE_3
        Number addition1 = MoneyHelper.add(RANGE_2_COMPONENT, RANGE_3_COMPONENT);
        Money subtract = MoneyHelper.sub(input, RANGE_2);
        Money multiply = MoneyHelper.mul(subtract, FEE_3);
        Money addition2 = MoneyHelper.add(multiply, addition1);
        ownerFee = addition2;
        println("range 3 ");
    }
    if (inputNum.doubleValue() > RANGE_3.doubleValue()) {
        // RANGE_2_COMPONENT + RANGE_3_COMPONENT + RANGE_4_COMPONENT + ( input - RANGE_3
        // ) * FEE_4
        Number addition1 = MoneyHelper.add(RANGE_2_COMPONENT, RANGE_3_COMPONENT);
        Number addition2 = MoneyHelper.add(addition1, RANGE_4_COMPONENT);
        Money subtract = MoneyHelper.sub(input, RANGE_3);
        Money multiply = MoneyHelper.mul(subtract, FEE_4);
        Money addition3 = MoneyHelper.add(multiply, addition2);
        ownerFee = addition3;
        println("range 4 ");
    }
    /*
		 * To prevent exponential values from appearing in amount. Not 1.7E+2, We need
		 * 170
		 */
    ownerFee = MoneyHelper.round(ownerFee);
    ownerFee = Money.of(ownerFee.getNumber().doubleValue(), DEFAULT_CURRENCY_TYPE);
    Number amount = ownerFee.getNumber().doubleValue();
    Money fee = Money.of(amount.doubleValue(), DEFAULT_CURRENCY_TYPE);
    println("From QRules " + fee);
    return fee;
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) CurrencyUnit(javax.money.CurrencyUnit)

Example 2 with Money

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

the class QRules method includeGSTMoney.

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

Example 3 with Money

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

the class QRules method makePayment.

public void makePayment(QDataAnswerMessage m) {
    /* Save Payment-related answers as user/BEG attributes */
    String userCode = getUser().getCode();
    BaseEntity userBe = getBaseEntityByCode(userCode);
    String begCode = PaymentUtils.processPaymentAnswers(getQwandaServiceUrl(), m, getToken());
    String assemblyAuthKey = PaymentUtils.getAssemblyAuthKey();
    String assemblyId = userBe.getValue("PRI_ASSEMBLY_USER_ID", null);
    if (begCode != null && assemblyId != null) {
        /* GET beg Base Entity */
        BaseEntity beg = getBaseEntityByCode(begCode);
        String offerCode = beg.getLoopValue("STT_HOT_OFFER", null);
        if (offerCode != null) {
            /* Make payment */
            showLoading("Processing payment...");
            Boolean isMakePaymentSucceeded = PaymentUtils.makePayment(getQwandaServiceUrl(), offerCode, begCode, assemblyAuthKey, getToken());
            println("isMakePaymentSucceeded ::" + isMakePaymentSucceeded);
            /* GET offer Base Entity */
            BaseEntity offer = getBaseEntityByCode(offerCode);
            String quoterCode = offer.getLoopValue("PRI_QUOTER_CODE", null);
            if (!isMakePaymentSucceeded) {
                /* TOAST :: FAIL */
                println("Sending error toast since make payment failed");
                HashMap<String, String> contextMap = new HashMap<String, String>();
                contextMap.put("DRIVER", quoterCode);
                contextMap.put("JOB", begCode);
                contextMap.put("QUOTER", quoterCode);
                String[] recipientArr = { userCode };
                /* Need to display error toast if make payment fails */
                sendMessage(null, recipientArr, contextMap, "MSG_CH40_MAKE_PAYMENT_FAILED", "TOAST");
                /* sending cmd BUCKETVIEW */
                drools.setFocus("SendLayoutsAndData");
            }
            if (isMakePaymentSucceeded) {
                /* GET attributes of OFFER BE */
                Money offerPrice = offer.getLoopValue("PRI_OFFER_PRICE", null);
                Money ownerPriceExcGST = offer.getLoopValue("PRI_OFFER_OWNER_PRICE_EXC_GST", null);
                Money ownerPriceIncGST = offer.getLoopValue("PRI_OFFER_OWNER_PRICE_INC_GST", null);
                Money driverPriceExcGST = offer.getLoopValue("PRI_OFFER_DRIVER_PRICE_EXC_GST", null);
                Money driverPriceIncGST = offer.getLoopValue("PRI_OFFER_DRIVER_PRICE_INC_GST", null);
                Money feePriceExcGST = offer.getLoopValue("PRI_OFFER_FEE_EXC_GST", null);
                Money feePriceIncGST = offer.getLoopValue("PRI_OFFER_FEE_INC_GST", null);
                /* Update BEG's prices with offer's prices */
                /*
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_PRICE",
					 * QwandaUtils.getMoneyString(offerPrice)); updateBaseEntityAttribute(begCode,
					 * begCode, "PRI_OWNER_PRICE_EXC_GST",
					 * QwandaUtils.getMoneyString(ownerPriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_OWNER_PRICE_INC_GST",
					 * QwandaUtils.getMoneyString(ownerPriceIncGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_DRIVER_PRICE_EXC_GST",
					 * QwandaUtils.getMoneyString(driverPriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_DRIVER_PRICE_INC_GST",
					 * QwandaUtils.getMoneyString(driverPriceIncGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_FEE_EXC_GST",
					 * QwandaUtils.getMoneyString(feePriceExcGST));
					 * updateBaseEntityAttribute(begCode, begCode, "PRI_FEE_INC_GST",
					 * QwandaUtils.getMoneyString(feePriceIncGST));
					 */
                List<Answer> answers = new ArrayList<Answer>();
                answers.add(new Answer(begCode, begCode, "PRI_PRICE", QwandaUtils.getMoneyString(offerPrice)));
                answers.add(new Answer(begCode, begCode, "PRI_OWNER_PRICE_EXC_GST", QwandaUtils.getMoneyString(ownerPriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_OWNER_PRICE_INC_GST", QwandaUtils.getMoneyString(ownerPriceIncGST)));
                answers.add(new Answer(begCode, begCode, "PRI_DRIVER_PRICE_EXC_GST", QwandaUtils.getMoneyString(driverPriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_DRIVER_PRICE_INC_GST", QwandaUtils.getMoneyString(driverPriceIncGST)));
                answers.add(new Answer(begCode, begCode, "PRI_FEE_EXC_GST", QwandaUtils.getMoneyString(feePriceExcGST)));
                answers.add(new Answer(begCode, begCode, "PRI_FEE_INC_GST", QwandaUtils.getMoneyString(feePriceIncGST)));
                // fetch the job to ensure the cache has caught up
                BaseEntity begBe = null;
                try {
                    begBe = QwandaUtils.getBaseEntityByCode(begCode, getToken());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                /* Update BEG to have DRIVER_CODE as an attribute */
                answers.add(new Answer(begCode, begCode, "STT_IN_TRANSIT", quoterCode));
                saveAnswers(answers);
                /* TOAST :: SUCCESS */
                println("Sending success toast since make payment succeeded");
                HashMap<String, String> contextMap = new HashMap<String, String>();
                contextMap.put("DRIVER", quoterCode);
                contextMap.put("JOB", begCode);
                contextMap.put("QUOTER", quoterCode);
                contextMap.put("OFFER", offer.getCode());
                String[] recipientArr = { userCode };
                /* TOAST :: PAYMENT SUCCESS */
                sendMessage("", recipientArr, contextMap, "MSG_CH40_MAKE_PAYMENT_SUCCESS", "TOAST");
                // sendMessage("", recipientArr, contextMap, "MSG_CH40_CONFIRM_QUOTE_OWNER",
                // "TOAST");
                sendMessage("", recipientArr, contextMap, "MSG_CH40_CONFIRM_QUOTE_OWNER", "EMAIL");
                /* QUOTER config */
                HashMap<String, String> contextMapForDriver = new HashMap<String, String>();
                contextMapForDriver.put("JOB", begCode);
                contextMapForDriver.put("OWNER", userCode);
                contextMapForDriver.put("OFFER", offer.getCode());
                String[] recipientArrForDriver = { quoterCode };
                /* Sending messages to DRIVER - Email and sms enabled */
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "TOAST");
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "SMS");
                sendMessage("", recipientArrForDriver, contextMapForDriver, "MSG_CH40_CONFIRM_QUOTE_DRIVER", "EMAIL");
                /* Update link between BEG and OFFER to weight= 0 */
                // updateLink(begCode, offerCode, "LNK_BEG", "OFFER", 1.0);
                /* Allocate QUOTER as Driver */
                updateLink(begCode, quoterCode, "LNK_BEG", "DRIVER", 1.0);
                /* Update link between BEG and Accepted OFFER to weight= 100 */
                updateLink(begCode, offerCode, "LNK_BEG", "OFFER", 100.0);
                /* Set PRI_NEXT_ACTION to Disabled for all other Offers */
                // get all offers
                List<BaseEntity> offers = getAllChildrens(begCode, "LNK_BEG", "OFFER");
                println("All the Offers for the load " + begCode + " are: " + offers.toString());
                for (BaseEntity be : offers) {
                    if (!(be.getCode().equals(offerCode))) {
                        println("The BE is : " + be.getCode());
                        /* Update PRI_NEXT_ACTION to Disabled */
                        updateBaseEntityAttribute(getUser().getCode(), be.getCode(), "PRI_NEXT_ACTION", "DISABLED");
                    }
                }
                /* SEND (OFFER, QUOTER, BEG) BaseEntitys to recipients */
                String[] offerRecipients = VertxUtils.getSubscribers(realm(), offer.getCode());
                println("OFFER subscribers   ::   " + Arrays.toString(offerRecipients));
                publishBaseEntityByCode(userCode, begCode, "LNK_BEG", offerRecipients);
                /* OWNER */
                publishBaseEntityByCode(quoterCode, begCode, "LNK_BEG", offerRecipients);
                publishBaseEntityByCode(offerCode, begCode, "LNK_BEG", offerRecipients);
                publishBaseEntityByCode("GRP_NEW_ITEMS", begCode, "LNK_CORE", offerRecipients);
                /* Set progression of LOAD delivery to 0 */
                Answer updateProgressAnswer = new Answer(begCode, begCode, "PRI_PROGRESS", Double.toString(0.0));
                saveAnswer(updateProgressAnswer);
                /* We ask FE to monitor GPS */
                geofenceJob(begCode, getUser().getCode(), 10.0);
                /* GET all the driver subsribers */
                String[] begRecipients = VertxUtils.getSubscribers(realm(), "GRP_NEW_ITEMS");
                println("ALL BEG subscribers   ::   " + Arrays.toString(begRecipients));
                println("quoter code ::" + quoterCode);
                Set<String> unsubscribeSet = new HashSet<>();
                for (String begRecipient : begRecipients) {
                    if (!begRecipient.equals(quoterCode)) {
                        unsubscribeSet.add(begRecipient);
                    }
                }
                println("unsubscribe set ::" + unsubscribeSet);
                String[] unsubscribeArr = new String[unsubscribeSet.size()];
                int i = 0;
                for (String code : unsubscribeSet) {
                    unsubscribeArr[i++] = code;
                }
                println("unsubscribe arr ::" + Arrays.toString(unsubscribeArr));
                /* Move BEG to GRP_APPROVED */
                fastClearBaseEntity(begCode, unsubscribeArr);
                BaseEntity begbe = getBaseEntityByCode(begCode);
                println("be   ::   " + begbe);
                Set<EntityAttribute> attributes = begbe.getBaseEntityAttributes();
                begbe.setBaseEntityAttributes(attributes);
                QDataBaseEntityMessage beMsg = new QDataBaseEntityMessage(begbe);
                beMsg.setDelete(true);
                publishData(beMsg, unsubscribeArr);
                VertxUtils.unsubscribe(realm(), "GRP_NEW_ITEMS", unsubscribeSet);
                moveBaseEntity(begCode, "GRP_NEW_ITEMS", "GRP_APPROVED", "LNK_CORE");
                // publishBaseEntityByCode(begCode, "GRP_NEW_ITEMS", "LNK_CORE",
                // newbegRecipients);
                // publishBaseEntityByCode(begCode, "GRP_APPROVED", "LNK_CORE",
                // newbegRecipients);
                /* Update PRI_NEXT_ACTION = OWNER */
                Answer begNextAction = new Answer(userCode, offerCode, "PRI_NEXT_ACTION", "NONE");
                saveAnswer(begNextAction);
                /* Update the Job status */
                updateBaseEntityAttribute(getUser().getCode(), begCode, "STA_" + quoterCode, Status.NEEDS_ACTION.value());
                updateBaseEntityAttribute(getUser().getCode(), begCode, "STA_" + getUser().getCode(), Status.NEEDS_NO_ACTION.value());
                /* sending cmd BUCKETVIEW */
                drools.setFocus("SendLayoutsAndData");
            /*	List<BaseEntity> listBe = new ArrayList<>();
					listBe.add(getUser());
					listBe.add(getBaseEntityByCode(quoterCode));
					listBe.add(getBaseEntityByCode(offerCode));
					listBe.add(getBaseEntityByCode(begCode));

					publishCmd(listBe, "GRP_ROOT", "LNK_CORE");
					sendSublayout("BUCKET_DASHBOARD", "dashboard_channel40.json", "GRP_DASHBOARD");
					setLastLayout( "BUCKET_DASHBOARD", "GRP_DASHBOARD" );  */
            }
            setState("PAYMENT_DONE");
        }
    }
}
Also used : QDataBaseEntityMessage(life.genny.qwanda.message.QDataBaseEntityMessage) EntityAttribute(life.genny.qwanda.attribute.EntityAttribute) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BaseEntity(life.genny.qwanda.entity.BaseEntity) ArrayList(java.util.ArrayList) IOException(java.io.IOException) AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) Answer(life.genny.qwanda.Answer) AttributeBoolean(life.genny.qwanda.attribute.AttributeBoolean) HashSet(java.util.HashSet)

Example 4 with Money

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

the class QRules method sortOffersInBeg.

/* Sorting Offers of a beg as per the price, lowest price being on top */
public void sortOffersInBeg(final String begCode) {
    List<BaseEntity> offers = getAllChildrens(begCode, "LNK_BEG", "OFFER");
    // println("All the Offers for the load " + begCode + " are: " + offers.toString());
    if (offers.size() > 1) {
        Collections.sort(offers, new Comparator<BaseEntity>() {

            @Override
            public int compare(BaseEntity offer1, BaseEntity offer2) {
                println("The price value of " + offer1.getCode() + " is " + offer1.getValue("PRI_OFFER_OWNER_PRICE_INC_GST", null));
                println("The price value of " + offer2.getCode() + " is " + offer2.getValue("PRI_OFFER_OWNER_PRICE_INC_GST", null));
                Money offer1Money = offer1.getValue("PRI_OFFER_OWNER_PRICE_INC_GST", null);
                Money offer2Money = offer2.getValue("PRI_OFFER_OWNER_PRICE_INC_GST", null);
                Number offer1MoneyValue = offer1Money.getNumber().doubleValue();
                Number offer2MoneyValue = offer2Money.getNumber().doubleValue();
                return ((Double) offer1MoneyValue).compareTo((Double) (offer2MoneyValue));
            }
        });
    }
    // println("The offers in the descendinng order :: " + offers.toString());
    // println("The size of list is :: " + offers.size());
    double maxLinkWeightValue = offers.size();
    double linkWeight = 1;
    for (BaseEntity be : offers) {
        if (linkWeight <= maxLinkWeightValue) {
            updateLink(begCode, be.getCode(), "LNK_BEG", "OFFER", linkWeight);
            linkWeight++;
        }
    }
}
Also used : AttributeMoney(life.genny.qwanda.attribute.AttributeMoney) Money(org.javamoney.moneta.Money) BaseEntity(life.genny.qwanda.entity.BaseEntity)

Example 5 with Money

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

the class MoneyHelper method div.

public static Money div(Number num, Money money) {
    Money result = Money.of(0, DEFAULT_CURRENCY);
    if (money != null && num != null) {
        Number numberFromMoney = money.getNumber();
        Number resultNumber = num.doubleValue() / numberFromMoney.doubleValue();
        result = Money.of(resultNumber, money.getCurrency());
    }
    return result;
}
Also used : Money(org.javamoney.moneta.Money)

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