Skip to content

Commit

Permalink
Merge pull request GustavoFernandes#48 from MergeMyPullRequest/47-pro…
Browse files Browse the repository at this point in the history
…rate-fee

Prorated fees
  • Loading branch information
jonsmithers authored Jul 29, 2017
2 parents b1090d9 + 81dfdb1 commit f831c42
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
13 changes: 11 additions & 2 deletions chrome_extension/common/order.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ class Order {
}
constructor(config = {tip: 0, tax: 0, untaxedFees: 0, taxedFees: 0, isTipPercentage: false, people: {}}) {

if (config.taxedFees) {
console.warn('Taxed fees have not been tested and probably don\'t work. I was contemplating removing this feature.');
}

// validation
{
var defaults = {
Expand Down Expand Up @@ -124,7 +128,12 @@ class Order {
}

get feesPerPerson() {
return this.fee/this.people.size;
if (!this.hasPeople) return {};
var subtotal = Array.from(this.people.values()).reduce((a,b)=>a+b);
return Array.from(this.people.entries()).reduce((feesPerPerson, [name, price]) => {
feesPerPerson[name] = price/subtotal*this.untaxedFees;
return feesPerPerson;
}, {});
}

get total() {
Expand All @@ -149,7 +158,7 @@ class Order {
let totalForPerson = price;
totalForPerson += price * this.taxPercent;
totalForPerson += price * this.tipPercent;
totalForPerson += this.feesPerPerson;
totalForPerson += this.feesPerPerson[name];
this.totals.set(name, totalForPerson);
}
let totalPrice = Array.from(this.totals.values()).reduce((acc, val) => acc+val);
Expand Down
35 changes: 8 additions & 27 deletions elements/order-split-results-table.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</td>
<td>
+
<span class="chip">$[[_prettifyNumber(order.feesPerPerson)]] fee <paper-tooltip>[[order.feesPerPerson]]</paper-tooltip></span>
<span class="chip">$[[_computeFeesPerPerson(order, item.name)]] fee <paper-tooltip>[[_computeFeesPerPersonTooltip(order, item.name)]]</paper-tooltip></span>
</td>
<td>
+
Expand Down Expand Up @@ -107,12 +107,13 @@
if (!people) {
return [];
}
return Array.from(this.order.people.entries()).map(entry => {
return {
name: entry[0],
price: entry[1]
};
});
return Array.from(this.order.people.entries()).map(([name, price]) => ({ name, price }));
}
_computeFeesPerPerson(order, name) {
return this._prettifyNumber(order.feesPerPerson[name]);
}
_computeFeesPerPersonTooltip(order, name) {
return `${order.untaxedFees} x ${order.people.get(name)} / ${order.subTotal}`;
}
_computePersonTotal(name) {
return this._prettifyNumber(this.order.totals.get(name));
Expand Down Expand Up @@ -180,26 +181,6 @@

return output + '\n' + this._makeUrl(this.order);
}
/**
* Returns a display breaking down the Order split calculations
* @param {Order} order - the Order to breakdown
* @returns {string} A view of the Order breakdown
*/
_makeBreakdownDisplay(order) {
var breakdown = '<table id="breakdown">';
breakdown += '<tr><th>Person</th><th>Item Costs</th><th>Tax</th><th>Tip</th><th>Fees Per Person</th><th>Person Total</th></tr>';
for (var [person, price] of order.people) {
breakdown += '<tr><td>' + person + '</td><td>' +
price + '</td><td> + ' + // item costs
price + ' * ' + order.taxPercent + '</td><td> + ' + // taxes
price + ' * ' + order.tipPercent + '</td><td> + ' + // tip
order.feesPerPerson + '</td><td> = ' +
this._prettifyNumber(order.totals.get(person)) + '</td></tr>';
}

breakdown += '</table>';
return breakdown;
}
_makeUrl(order) {
if (!this.order) {
return;
Expand Down

0 comments on commit f831c42

Please sign in to comment.