Commit 095c260d by Tuomas Riihimäki

Make constant from Bill price scale.

1 parent e964b200
......@@ -24,6 +24,7 @@ package fi.codecrew.moya.beans;
import java.io.ByteArrayOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
......@@ -173,7 +174,7 @@ public class PlaceBean implements PlaceBeanLocal {
Map<Product, Integer> mockmap = getPlaceProductcount(places);
BigDecimal total = BigDecimal.ZERO;
BigDecimal total = Bill.BILL_SCALED_ZERO_PRICE;
Calendar now = Calendar.getInstance();
for (Entry<Product, Integer> entry : mockmap.entrySet()) {
......@@ -182,7 +183,7 @@ public class PlaceBean implements PlaceBeanLocal {
total = total.add(productBean.calculateTotal(entry.getKey(), new BigDecimal(entry.getValue()), now, user));
}
}
return total;
return total.setScale(Bill.BILL_PRICE_SCALE, RoundingMode.HALF_UP);
}
private static Map<Product, Integer> getPlaceProductcount(Collection<Place> places) {
......@@ -479,7 +480,6 @@ public class PlaceBean implements PlaceBeanLocal {
return placeFacade.setBuyable(map, like, b);
}
/**
* Release reservation from user
*
......@@ -724,8 +724,6 @@ public class PlaceBean implements PlaceBeanLocal {
return placeFacade.getMapProducts(map);
}
@Override
public List<PlaceSlot> getFreePlaceslots(EventUser user, EventMap map) {
user = eventUserFacade.reload(user);
......
......@@ -19,6 +19,7 @@
package fi.codecrew.moya.model;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
......@@ -54,6 +55,27 @@ import fi.codecrew.moya.utilities.BillUtils;
public class Bill extends GenericEntity {
/**
* <p>
* With how many decimals we want to calculate prices, discounts, etc. If we
* use default scale we will hit rounding problems at some point. By default
* BigDecimal acts as a float and So we get this to be true. Even if we
* calculate prices with scale '4', we most likely want to display the
* prices to users with scale '2'
* <p>
* <p>
* NOTICE! This value exists also in database. If changed, remember to also
* modify the database
* </p>
* <h2>And for future generations, an undying quote</h2> <cite> BigDecimal
* scale 4 should be enough for everybody. - Tuomari<cite>
*/
public static final int BILL_PRICE_SCALE = 4;
public static final BigDecimal BILL_SCALED_ZERO_PRICE = BigDecimal.ZERO.setScale(Bill.BILL_PRICE_SCALE, RoundingMode.HALF_UP);
public static final int VAT_SCALE = 3;
public static final BigDecimal VAT_SCALED_ZERO = BigDecimal.ZERO.setScale(VAT_SCALE, RoundingMode.HALF_UP);
/**
*
*/
private static final long serialVersionUID = -6713643278149221869L;
......@@ -174,7 +196,7 @@ public class Bill extends GenericEntity {
* @return The total sum of the bill ( unitPrice * units * vat )
*/
public BigDecimal totalPrice() {
BigDecimal total = BigDecimal.ZERO;
BigDecimal total = Bill.BILL_SCALED_ZERO_PRICE;
for (BillLine line : getBillLines()) {
total = total.add(line.getLinePrice());
}
......@@ -191,7 +213,7 @@ public class Bill extends GenericEntity {
* @return The total sum of the bill ( unitPrice * units * vat )
*/
public BigDecimal totalVat() {
BigDecimal total = BigDecimal.ZERO;
BigDecimal total = Bill.BILL_SCALED_ZERO_PRICE;
for (BillLine line : getBillLines()) {
total = total.add(line.getLineVat());
}
......@@ -204,7 +226,7 @@ public class Bill extends GenericEntity {
* @return The total VAT-less sum of the bill ( unitPrice * units )
*/
public BigDecimal totalPriceVatless() {
BigDecimal total = BigDecimal.ZERO;
BigDecimal total = Bill.BILL_SCALED_ZERO_PRICE;
for (BillLine line : getBillLines()) {
total = total.add(line.getLinePriceVatless());
}
......@@ -227,12 +249,12 @@ public class Bill extends GenericEntity {
public Bill(LanEvent event, EventUser user, long expireTimeHours) {
this(event, user, Calendar.getInstance());
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours*60*60 * 1000 )));
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours * 60 * 60 * 1000)));
}
public Bill(LanEvent event, long expireTimeHours) {
this(event, Calendar.getInstance());
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours*60*60 * 1000 )));
this.expires.setTimeInMillis((System.currentTimeMillis() + (expireTimeHours * 60 * 60 * 1000)));
}
public Bill() {
......@@ -391,7 +413,7 @@ public class Bill extends GenericEntity {
}
public void setPaidDate(Date paidDate) {
if(paidDate != null)
if (paidDate != null)
expires = null;
this.paidDate = paidDate;
......@@ -462,14 +484,14 @@ public class Bill extends GenericEntity {
}
public boolean isExpired() {
if(isPaid() || expires == null)
if (isPaid() || expires == null)
return false;
return Calendar.getInstance().after(expires);
}
public void markExpired() {
if(isExpired() || isPaid())
if (isExpired() || isPaid())
return;
expires = Calendar.getInstance();
......@@ -479,12 +501,12 @@ public class Bill extends GenericEntity {
public BigDecimal getTotalQuantity() {
BigDecimal total = BigDecimal.ZERO;
for(BillLine l : getBillLines()) {
if(l == null || l.getQuantity() == null)
for (BillLine l : getBillLines()) {
if (l == null || l.getQuantity() == null)
continue;
total = total.add(l.getQuantity());
}
}
return total;
}
......@@ -493,11 +515,11 @@ public class Bill extends GenericEntity {
public String getProductSummary() {
String summary = "";
for(BillLine l : getBillLines()) {
if(l == null || l.getQuantity() == null)
for (BillLine l : getBillLines()) {
if (l == null || l.getQuantity() == null)
continue;
if(!summary.isEmpty()) {
if (!summary.isEmpty()) {
summary += ", ";
}
summary += l.getName();
......@@ -506,10 +528,3 @@ public class Bill extends GenericEntity {
return summary;
}
}
......@@ -42,7 +42,6 @@ import javax.persistence.Table;
public class BillLine extends GenericEntity {
private static final long serialVersionUID = 2L;
private static final BigDecimal DEFAULT_VAT = BigDecimal.ZERO;
/**
* Which bill this bill line belongs to
*/
......@@ -67,8 +66,8 @@ public class BillLine extends GenericEntity {
* How much one(1) unit of this product costs
*
*/
@Column(name = "unit_price", nullable = false, precision = 24, scale = 4)
private BigDecimal unitPrice = BigDecimal.ZERO;
@Column(name = "unit_price", nullable = false, precision = 24, scale = Bill.BILL_PRICE_SCALE)
private BigDecimal unitPrice = Bill.BILL_SCALED_ZERO_PRICE;
/**
*
......@@ -79,8 +78,8 @@ public class BillLine extends GenericEntity {
/**
* How much VAT this product contains ( 0, 0.22 ) etc
*/
@Column(name = "vat", nullable = false, precision = 4, scale = 3)
private BigDecimal vat = DEFAULT_VAT;
@Column(name = "vat", nullable = false, precision = 4, scale = Bill.VAT_SCALE)
private BigDecimal vat = Bill.VAT_SCALED_ZERO;
@JoinColumn(name = "lineProduct_id", referencedColumnName = "id", nullable = true, updatable = false)
@OneToOne
......@@ -111,7 +110,7 @@ public class BillLine extends GenericEntity {
*/
public BigDecimal getLinePriceVatless() {
BigDecimal vatMultiplicand = BigDecimal.ONE.add(getVat());
return getLinePrice().divide(vatMultiplicand, 2, RoundingMode.HALF_UP);
return getLinePrice().divide(vatMultiplicand, Bill.BILL_PRICE_SCALE, RoundingMode.HALF_UP);
}
public BillLine() {
......@@ -157,7 +156,7 @@ public class BillLine extends GenericEntity {
public BillLine(Bill bill2, Product product, Discount disc, BigDecimal count) {
super();
this.bill = bill2;
BigDecimal unitPrice = product.getPrice().subtract(product.getPrice().multiply(disc.getPercentage())).negate().setScale(2, RoundingMode.HALF_UP);
BigDecimal unitPrice = product.getPrice().subtract(product.getPrice().multiply(disc.getPercentage())).negate().setScale(Bill.BILL_PRICE_SCALE, RoundingMode.HALF_UP);
this.name = disc.getShortdesc();
this.unitName = product.getUnitName();
......
......@@ -19,6 +19,7 @@
package fi.codecrew.moya.model;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
......@@ -45,13 +46,15 @@ public class Discount extends GenericEntity {
private static final long serialVersionUID = 1L;
private static final String EVENT_ID = "event_id";
private static final BigDecimal SCALE4_ZERO = BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP);
private static final BigDecimal SCALE6_ZERO = BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP);
@ManyToOne()
@JoinColumn(name = EVENT_ID, nullable = false)
private LanEvent event;
@Column(name = "percentage", nullable = false, precision = 9, scale = 6)
private BigDecimal percentage = BigDecimal.ZERO;
private BigDecimal percentage = SCALE6_ZERO;
@Column(name = "code")
private String code;
......@@ -71,22 +74,22 @@ public class Discount extends GenericEntity {
private String shortdesc;
@Column(name = "amount_min", nullable = false, precision = 24, scale = 4)
private BigDecimal amountMin = BigDecimal.ZERO;
private BigDecimal amountMin = SCALE4_ZERO;
@Column(name = "amount_max", nullable = false, precision = 24, scale = 4)
private BigDecimal amountMax = BigDecimal.ZERO;
private BigDecimal amountMax = SCALE4_ZERO;
@Column(name = "active", nullable = false)
private boolean active = false;
@Column(name = "max_num", nullable = false, precision = 24, scale = 4)
private BigDecimal maxNum = BigDecimal.ZERO;
private BigDecimal maxNum = SCALE4_ZERO;
@Column(name = "per_user", nullable = false, precision = 24, scale = 4)
private BigDecimal perUser = BigDecimal.ZERO;
private BigDecimal perUser = SCALE4_ZERO;
@Column(name = "total_count", nullable = false, precision = 24, scale = 4)
private BigDecimal totalCount = BigDecimal.ZERO;
private BigDecimal totalCount = SCALE4_ZERO;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "discount")
private List<DiscountInstance> discountInstances;
......
......@@ -22,8 +22,8 @@
*/
package fi.codecrew.moya.model;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
......@@ -58,11 +58,11 @@ public class Product extends GenericEntity {
@Lob
private String description;
@Column(name = "price", nullable = false, precision = 24, scale = 4)
private BigDecimal price = BigDecimal.ZERO;
@Column(name = "price", nullable = false, precision = 24, scale = Bill.BILL_PRICE_SCALE)
private BigDecimal price = Bill.BILL_SCALED_ZERO_PRICE;
@Column(name = "buy_in_price", nullable = true, precision = 24, scale = 4)
private BigDecimal buyInPrice = BigDecimal.ZERO;
@Column(name = "buy_in_price", nullable = true, precision = 24, scale = Bill.BILL_PRICE_SCALE)
private BigDecimal buyInPrice = Bill.BILL_SCALED_ZERO_PRICE;
@Column(name = "sort", nullable = false)
private int sort;
......@@ -70,11 +70,9 @@ public class Product extends GenericEntity {
@Column(name = "barcode")
private String barcode;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "product")
private List<LicenseTarget> licenseTargets;
@Enumerated(EnumType.STRING)
@PrivateOwned
@ElementCollection()
......@@ -110,8 +108,8 @@ public class Product extends GenericEntity {
})
private List<Discount> discounts;
@Column(name = "vat", nullable = false, precision = 4, scale = 3)
private BigDecimal vat = BigDecimal.ZERO;
@Column(name = "vat", nullable = false, precision = 4, scale = Bill.VAT_SCALE)
private BigDecimal vat = Bill.VAT_SCALED_ZERO;
private String unitName = "";
@ManyToMany()
......@@ -189,6 +187,7 @@ public class Product extends GenericEntity {
/**
* Get product price, includes vat
*
* @return
*/
public BigDecimal getPrice() {
......@@ -197,6 +196,7 @@ public class Product extends GenericEntity {
/**
* Set price, including vat
*
* @param price
*/
public void setPrice(BigDecimal price) {
......@@ -253,6 +253,7 @@ public class Product extends GenericEntity {
/**
* Set product vat-%, value between 0 and 1
*
* @param vat
*/
public void setVat(BigDecimal vat) {
......@@ -261,6 +262,7 @@ public class Product extends GenericEntity {
/**
* Get product vat-%, value between 0 and 1
*
* @return
*/
public BigDecimal getVat() {
......@@ -312,10 +314,10 @@ public class Product extends GenericEntity {
Set<ProductFlag> flags = getProductFlags();
if(flags == null)
if (flags == null)
flags = new HashSet<ProductFlag>();
if(!flags.contains(flag)) {
if (!flags.contains(flag)) {
flags.add(flag);
setProductFlags(flags);
}
......@@ -361,7 +363,6 @@ public class Product extends GenericEntity {
this.licenseTargets = licenseTargets;
}
public boolean isUsershopAutoproduct() {
return getProductFlags().contains(ProductFlag.USERSHOP_AUTOPRODUCT);
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!