Vue Js - Calculation Criteria in Reverse - javascript

Am Having situation where i had to reverse a calculation criteria to make it clear :
i have sub_total field which is editable and user can change the value of it
first the pre tax is applied to this sub_total and give me pre_total
second the vato tax is applied to the sum of ( sub_total and pre_total ) and give me vato_total
third the tx tax is applied to the sub_total and give me tx_total
the final step is the grand total which is the sum of sub_total + pre_total + vato_total + tx_total .
and here is the demonstration code of the above steps :
let subtotal_with_ewa = Number((this.subtotal_price / 100) * this.unit.reservation.prices.ewa_parentage) + Number(this.subtotal_price);
this.total_ewa = parseFloat((this.subtotal_price / 100) * this.unit.reservation.prices.ewa_parentage).toFixed(2)
this.total_vat = parseFloat((subtotal_with_ewa / 100 ) * this.unit.reservation.prices.vat_parentage).toFixed(2);
this.total_ttx = parseFloat((this.subtotal_price / 100) * this.unit.reservation.prices.tourism_percentage).toFixed(2);
this.total_price = parseFloat(subtotal_with_ewa + Number(this.total_vat) + Number(this.total_tourism)).toFixed(2);
this.unit.reservation.prices.total_price_raw = parseFloat(subtotal_with_ewa + Number(this.total_vat) + Number(this.total_ttx) ).toFixed(2);
this.unit.reservation.prices.total_price = this.unit.reservation.prices.total_price_raw ;
this.total_price = this.unit.reservation.prices.total_price ;
this.unit.reservation.prices.price = this.subtotal_price
and it's working perfect as the following screen shot
now the question is : what do i do to make the whole operation as reverse i mean what i had to do if i changed the grand total value i need to re-calcuate those values till get the sub_total again .... any ideas ?

You need to make computed properties they will recompute any time a dependancy is changed, for example:
subtotal_with_ewa(){
return Number((this.subtotal_price / 100) *
this.unit.reservation.prices.ewa_parentage) + Number(this.subtotal_price);
}
You can use getters and setters with computed properties or separate them from the bound v-model on the fields.

The below functions should solve the problem.
the function accepts the percentage of each tax as argument and returns a json will all the values.
function fromSubTotal(subTotal, pre, vato, tx) {
const preAmount = subTotal * pre / 100
const vatoAmount = (subTotal + pre) * vato / 100
const txAmount = subTotal * tx / 100
const total = subTotal + preAmount + vatoAmount + txAmount
return {subTotal, preAmount, vatoAmount, txAmount, total}
}
function fromTotal(total, pre, vato, tx) {
const subTotal = (total * 100) / (100 + pre + tx + (1.01 * pre * vato))
return fromSubTotal(subTotal, pre, vato, tx)
}
// eg.
console.log(fromSubTotal(100,1,1,1))
console.log(fromTotal(103.01,1,1,1))

Related

Is there any problem with converting this excel formula to javascript?

Here is the excel formula:
=E2-(J2*2*G2)-(K2*E2*2)/30+IF((L2+M2)>60,((L2+M2)-60)*H2+60*G2,(L2+M2)*G2)+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here what I tried (Javascript):
var E2 = $("#sentence").val();
var J2 = $("#deduction").val();
var G2 = 55145;
var K2 = $("#absence").val();
var L2 = $("#overtime").val();
var M2 = 0;
var H2 = 50050;
var N2 = $("#transportation").val();
var sixty;
if ((L2 + M2) > 60) {
sixty = ((L2 + M2) - 60) * H2 + 60 * G2;
} else {
sixty = (L2 + M2) * G2;
};
var result = E2 - (J2 * 2 * G2) - (K2 * E2 * 2) / 30 + sixty;
I couldn't find the way to conver this part of formula:
+(N2/$AE$1)*$AE$2+(Q2*$AE$5+P2*$AE$4+O2*$AE$3)
Here I found the problem:
Even if one of the variables sets to null, then the formula does not work properly.
It looks like some pretty basic math.
let total = (N2 / AE1) * AE2 + (Q2 * AE5 + P2 * AE4 + O2 * AE3 )
This is basically impossible to translate without seeing the actual spreadsheet but that should get you started. Also, make sure to take into consideration order of operations because the computer is going to evaluate it from left to right unless there are parenthesis (where it will evaluate those first).

Compound Interest with Monthly Contributions Calculation isn't Producing Correct Results

I'm trying to calculate compound interest of a principle amount with monthly contributions. I can't seem to figure out why my calculation aren't working. Any suggestion would be appreciated thanks.
//Add Data to the Arrays
while (i <= years) {
nums.push(i + " Years");
let newAmount = (principle * Math.pow(1 + (convertRate / frequency), frequency * i)) + ((mContribute * Math.pow(1 + (convertRate / frequency), frequency * i)) - 1) / (convertRate / frequency);
let contributionBalance = principle + mContribute;
let formatNum = newAmount.toFixed(2);
balance.push(formatNum);
contributions.push(contributionBalance);
i++;
}

Modelling a numeric result against a change in percentage

I have a calculation which returns a percentage
TotalDelegatedStake / circulatingSupply * 100 = Y%
And I have a different calculation that also uses the first variable
let emissions = numberinput4.value;
let stake = numberinput2.value / TotalDelegatedStake;
let penalty = numberinput3.value <= 98.5 ? '0' : numberinput3.value / 100;
let fee = numberinput5.value == 0 ? 1 : 1 - (numberinput5.value / 100);
let calc = emissions * stake * penalty * fee;
return calc;
I would like to be able to model what the result of calc would be if Y% increased or decreased in value.
Eg. it's 30% now what would the result of calc be if it was 40%.
thanks in advance

What is the right way to add percentages to a service/product?

I have to add up the service charge(10%) plus VAT(8%) to a service, and I'm not sure the right way to increase a number by percentages.
For example: The service costs $1000 and I need to add to it 10% plus 8%. I'm struggling with three different ways to achieve that. Which way is the right one?
var service_charge = 10; // percent
var vat = 8; // percent
var price = 1000;
// OPTION 1
var tmp1 = price * (service_charge / 100);
var tmp2 = price * (vat / 100);
var total_1 = price + tmp1 + tmp2;
// OPTION 2
var tmp3 = price * (service_charge / 100);
var sub_total = price + tmp3;
var tmp4 = sub_total * (vat / 100);
var total_2 = sub_total + tmp4;
// OPTION 3
var tmp5 = price * ((service_charge + vat) / 100);
var total_3 = price + tmp5;
console.log(total_1);
console.log(total_2);
console.log(total_3);
https://jsbin.com/povama/edit?js,console
I think "option 2" is the right one, but I really want to be sure.
If VAT is to be applied after service charge, option 2 is the good one. If you want to summarize it in one single line it would be something like this:
var total = (price*(1+(service_charge/100)))*(1+(vat/100));

How to applying percentage to price javascript?

percentage = 10%;
price = €50;
total = price + percent
How to apply percentage to price?
I tried to apply this
(percent / 100) +1 * price
but it is not correct in this case
First of all, you need numbers and then calculate with these values the totel value.
The result could have some digits who are unwanted. In this case apply Number#toFixed for an output of a string with the wanted digits.
var percent = 10,
price = 50,
total = price * (percent / 100 + 1);
document.write(total + '<br>');
document.write(total.toFixed(2));
1% is 1/100 of initial value, I guess what you want to do is this:
(percent / 100) * price + price

Categories

Resources