Permalink
Browse files

- Moved price calculation to EuroAmount

- Replaced hand-made price calculation with the one in EuroAmount
  • Loading branch information...
1 parent 5329d2a commit 6419cbcd963c6c06fc918db36b6fd297353b465d @alexbiehl alexbiehl committed Mar 27, 2013
@@ -3,6 +3,7 @@
import java.util.List;
import org.fhw.asta.kasse.client.common.EuroFormatter;
+import org.fhw.asta.kasse.client.common.PrintCustomsToken;
import org.fhw.asta.kasse.client.place.PrintCustomsPlace;
import org.fhw.asta.kasse.client.widget.HasTopbar;
import org.fhw.asta.kasse.client.widget.print.PrintWidget;
@@ -91,7 +92,7 @@ public void onSuccess(LetterHead result) {
break;
case USERLIST:
- printUSERLIST(printCustomsPlace.getToken().get().getId());
+ printUSERLIST(printCustomsPlace.getToken());
break;
}
}
@@ -230,14 +231,23 @@ private void printBillOrder() {
private void printArticles() {
StringBuilder strb = new StringBuilder();
- int sum = 0;
+ //int sum = 0;
+
+ EuroAmount sum = EuroAmount.ZERO_AMOUNT;
+
strb.append("<br/>");
strb.append("<table class='regtb'><tr class='headline'><td>Menge</td><td class='desc'>Beschreibung</td><td>Rabatt (&#037;)</td><td>E-Preis</td><td>G-Preis</td><td>Abgehohlt</td></tr>");
for (BasketItem art : articles) {
- EuroAmount euroAmount = new EuroAmount((int) Math.round((art
+
+ EuroAmount euroAmount = art.getItemPrice().times(art.getAmount()).withDiscount(art.getDiscount());
+
+/* EuroAmount euroAmount = new EuroAmount((int) Math.round((art
.getItemPrice().getCentAmount() * art.getAmount())
- * ((100.0 - art.getDiscount()) / 100)));
- sum += euroAmount.getCentAmount();
+ * ((100.0 - art.getDiscount()) / 100)));*/
+
+ sum = sum.plus(euroAmount);
+
+// sum += euroAmount.getCentAmount();
strb.append("<tr class='unbreakable'><td>" + art.getAmount()
+ "</td><td class='desc'>" + art.getItemName()
+ "</td><td>" + art.getDiscount() + "</td><td>"
@@ -249,12 +259,11 @@ private void printArticles() {
}
strb.append("<tr class='headline'><td></td><td class='desc'></td><td></td><td></td><td></td><td></td></tr>");
strb.append("<tr><td></td><td class='desc'></td><td></td><td></td><td><strong>Gesamt:</strong></td><td>"
- + EuroFormatter.format(new EuroAmount(sum)) + "</td></tr>");
+ + EuroFormatter.format(sum) + "</td></tr>");
strb.append("<tr><td></td><td class='desc'></td><td></td><td></td><td><strong>Rabatt (&#037;):</strong></td><td>"
+ billOrder.getDiscount() + "</td></tr>");
strb.append("<tr><td></td><td class='desc'></td><td></td><td></td><td><strong>Endsumme:</strong></td><td>"
- + EuroFormatter.format(new EuroAmount((int) Math.round(sum
- * ((100.0 - billOrder.getDiscount()) / 100))))
+ + EuroFormatter.format(sum.withDiscount(billOrder.getDiscount()))
+ "</td></tr>");
strb.append("</table>");
printWidget.addHtml(strb.toString());
@@ -271,13 +280,13 @@ private void printTAX(int year) {
}
- private void printUSERLIST(int id) {
- if (id != -1) {
- userService.getUsersByGroup(String.valueOf(id),
- new UserListCallback());
+ private void printUSERLIST(Optional<PrintCustomsToken> maybeToken) {
+ if (maybeToken.isPresent()) {
+ userService.getUsersByGroup(String.valueOf(maybeToken.get().getId()),
+ new UserListCallback());
} else {
- userService.getAllUsers(new UserListCallback());
- }
+ userService.getAllUsers(new UserListCallback());
+ }
}
@@ -4,15 +4,20 @@
import java.util.Comparator;
import java.util.Iterator;
+import javax.annotation.Nullable;
+
import org.fhw.asta.kasse.client.common.EuroFormatter;
import org.fhw.asta.kasse.client.common.PrintCustomsToken;
import org.fhw.asta.kasse.client.common.PrintCustomsToken.PrintType;
import org.fhw.asta.kasse.client.widget.basket.BasketWidget;
import org.fhw.asta.kasse.shared.basket.BasketItem;
+import org.fhw.asta.kasse.shared.common.EuroAmount;
import org.fhw.asta.kasse.shared.model.Article;
import org.fhw.asta.kasse.shared.service.checkout.CheckoutServiceAsync;
+import com.google.common.base.Function;
import com.google.common.collect.ComparisonChain;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.event.dom.client.ClickEvent;
@@ -104,16 +109,12 @@ public void addBasketPosition(Article article) {
private void flush() {
Collections.sort(this.basketDataProvider.getList(), this.basketComparator);
- int sum = 0;
-
- final Iterator<BasketItem> basketIterator = this.basketDataProvider.getList().iterator();
- while (basketIterator.hasNext()) {
- final BasketItem next = basketIterator.next();
- sum += Math
- .round((next.getItemPrice().getCentAmount() * next.getAmount()) * ((100 - next.getDiscount()) / 100.0));
-
+ EuroAmount sum = EuroAmount.ZERO_AMOUNT;
+
+ for (BasketItem bi : basketDataProvider.getList()) {
+ sum = sum.plus(bi.getItemPrice().times(bi.getAmount()).withDiscount(bi.getDiscount()));
}
-
+
int discount = 0;
try {
@@ -122,7 +123,7 @@ private void flush() {
discount = 0;
}
- this.basketWidget.getSumLabel().setText(EuroFormatter.format((int) Math.round(sum * ((100 - discount) / 100.0))));
+ this.basketWidget.getSumLabel().setText(EuroFormatter.format(sum.withDiscount(discount)));
}
private class CheckoutHandler implements ClickHandler {
@@ -6,8 +6,11 @@
{
private static final long serialVersionUID = 1L;
+ public static final EuroAmount ZERO_AMOUNT = new EuroAmount(0);
+
private int cents;
+
public EuroAmount()
{
this(0);
@@ -43,4 +46,8 @@ public EuroAmount times(final int factor)
return new EuroAmount(this.cents * factor);
}
+ public EuroAmount withDiscount(int discount) {
+ return new EuroAmount((int) Math.round(cents * ((double)(100.0 - discount) / 100)));
+ }
+
}

0 comments on commit 6419cbc

Please sign in to comment.