Commit 9ca7eb38 by Tuukka Kivilahti

Merge branch 'discountSort' into 'master'

Discount sort field

Add possibility to sort discount rows

See merge request !353
2 parents 15db7c0d 198da055
...@@ -372,7 +372,8 @@ public class BillBean implements BillBeanLocal { ...@@ -372,7 +372,8 @@ public class BillBean implements BillBeanLocal {
BigDecimal discountPrice = product.getPrice(); BigDecimal discountPrice = product.getPrice();
for (Discount disc : discountBean.getActiveDiscountsByProduct(product, count, bill.getSentDate(), bill.getUser())) { for (Discount disc : discountBean.getActiveDiscountsByProduct(product, count, bill.getSentDate(), bill.getUser())) {
BillLine line = new BillLine(bill, product, disc,discountPrice, count); BillLine line = new BillLine(bill, product, disc,discountPrice, count);
discountPrice = discountPrice.subtract(line.getUnitPrice()); // unitPrice is negative, so add. Do not subtract
discountPrice = discountPrice.add(line.getUnitPrice());
bill.getBillLines().add(line); bill.getBillLines().add(line);
} }
......
...@@ -422,6 +422,10 @@ public class BootstrapBean implements BootstrapBeanLocal { ...@@ -422,6 +422,10 @@ public class BootstrapBean implements BootstrapBeanLocal {
"ALTER TABLE products ADD COLUMN min_buy_count INTEGER default 0;" "ALTER TABLE products ADD COLUMN min_buy_count INTEGER default 0;"
}); });
dbUpdates.add(new String[] {
"ALTER TABLE discounts ADD COLUMN sort INTEGER NOT NULL default 10;"
});
} }
......
...@@ -80,6 +80,7 @@ public class DiscountBean implements DiscountBeanLocal { ...@@ -80,6 +80,7 @@ public class DiscountBean implements DiscountBeanLocal {
} }
} }
} }
ret.sort(Discount.SORT_COMPARATOR);
return ret; return ret;
} }
......
...@@ -21,6 +21,7 @@ package fi.codecrew.moya.model; ...@@ -21,6 +21,7 @@ package fi.codecrew.moya.model;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
...@@ -42,12 +43,26 @@ import javax.persistence.TemporalType; ...@@ -42,12 +43,26 @@ import javax.persistence.TemporalType;
@Entity @Entity
@Table(name = "discounts") @Table(name = "discounts")
public class Discount extends GenericEntity { public class Discount extends GenericEntity {
public static final String SORT_FIELD = "sort";
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private static final String EVENT_ID = "event_id"; 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 SCALE4_ZERO = BigDecimal.ZERO.setScale(4, RoundingMode.HALF_UP);
private static final BigDecimal SCALE6_ZERO = BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP); private static final BigDecimal SCALE6_ZERO = BigDecimal.ZERO.setScale(6, RoundingMode.HALF_UP);
public static Comparator<Discount> SORT_COMPARATOR = new Comparator<Discount>(){
@Override
public int compare(Discount o1, Discount o2) {
return Integer.compare(o1.getSort(), o2.getSort());
}
};
@Column(name = SORT_FIELD, nullable = false)
private int sort = 10;
@ManyToOne() @ManyToOne()
@JoinColumn(name = EVENT_ID, nullable = false) @JoinColumn(name = EVENT_ID, nullable = false)
private LanEvent event; private LanEvent event;
...@@ -61,6 +76,7 @@ public class Discount extends GenericEntity { ...@@ -61,6 +76,7 @@ public class Discount extends GenericEntity {
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@Column(name = "valid_from") @Column(name = "valid_from")
private Date validFrom; private Date validFrom;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)
@Column(name = "valid_to") @Column(name = "valid_to")
private Date validTo; private Date validTo;
...@@ -240,4 +256,12 @@ public class Discount extends GenericEntity { ...@@ -240,4 +256,12 @@ public class Discount extends GenericEntity {
this.validFrom = validFrom; this.validFrom = validFrom;
} }
public int getSort() {
return sort;
}
public void setSort(int sort) {
this.sort = sort;
}
} }
...@@ -40,6 +40,7 @@ import javax.persistence.Lob; ...@@ -40,6 +40,7 @@ import javax.persistence.Lob;
import javax.persistence.ManyToMany; import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table; import javax.persistence.Table;
import javax.persistence.Transient; import javax.persistence.Transient;
import javax.persistence.UniqueConstraint; import javax.persistence.UniqueConstraint;
...@@ -127,6 +128,7 @@ public class Product extends GenericEntity { ...@@ -127,6 +128,7 @@ public class Product extends GenericEntity {
joinColumns = { joinColumns = {
@JoinColumn(name = PRODUCTFLAG_TABLE_PRODUCTID, referencedColumnName = Product.ID_COLUMN) @JoinColumn(name = PRODUCTFLAG_TABLE_PRODUCTID, referencedColumnName = Product.ID_COLUMN)
}) })
@OrderBy(Discount.SORT_FIELD+" ASC")
private List<Discount> discounts; private List<Discount> discounts;
@Column(name = "vat", nullable = false, precision = 4, scale = Bill.VAT_SCALE) @Column(name = "vat", nullable = false, precision = 4, scale = Bill.VAT_SCALE)
......
...@@ -79,7 +79,7 @@ ...@@ -79,7 +79,7 @@
<p:commandButton <p:commandButton
rendered="#{ajaxMapView.canUserBuy()}" rendered="#{ajaxMapView.canUserBuy()}"
value="#{i18n['mapPlacechange.commitMove']}" value="#{i18n['mapPlacechange.commitMove']}"
actionListener="#{mapPlacechangeView.commitMove()}" /> action="#{mapPlacechangeView.commitMove()}" />
</h:form> </h:form>
......
...@@ -16,6 +16,14 @@ ...@@ -16,6 +16,14 @@
<f:param name="productid" value="#{productView.product.id}" /> <f:param name="productid" value="#{productView.product.id}" />
</h:link> </h:link>
<h:panelGrid columns="3"> <h:panelGrid columns="3">
<h:outputLabel for="id" value="#{i18n['discount.id']}:" />
<h:outputText id="id" value="#{productView.discount.id}" />
<h:message for="id" />
<h:outputLabel for="sort" value="#{i18n['discount.sort']}:" />
<h:inputText id="sort" value="#{productView.discount.sort}" />
<h:message for="sort" />
<h:outputLabel for="shortdesc" value="#{i18n['discount.shortdesc']}:" /> <h:outputLabel for="shortdesc" value="#{i18n['discount.shortdesc']}:" />
<h:inputText id="shortdesc" value="#{productView.discount.shortdesc}" /> <h:inputText id="shortdesc" value="#{productView.discount.shortdesc}" />
<h:message for="shortdesc" /> <h:message for="shortdesc" />
......
...@@ -107,6 +107,9 @@ public class MapPlacechangeView extends GenericCDIView { ...@@ -107,6 +107,9 @@ public class MapPlacechangeView extends GenericCDIView {
} }
} }
placebean.movePlaces(change); placebean.movePlaces(change);
slots = null;
initView();
} }
public void toggleDstPlace() { public void toggleDstPlace() {
......
...@@ -190,7 +190,9 @@ public class ProductView extends GenericCDIView { ...@@ -190,7 +190,9 @@ public class ProductView extends GenericCDIView {
public ListDataModel<Discount> getProductDiscounts() public ListDataModel<Discount> getProductDiscounts()
{ {
productDiscounts = new ListDataModel<Discount>(product.getDiscounts()); List<Discount> discs = product.getDiscounts();
discs.sort(Discount.SORT_COMPARATOR);
productDiscounts = new ListDataModel<Discount>(discs);
return productDiscounts; return productDiscounts;
} }
......
...@@ -45,18 +45,22 @@ public class ProductShopItemHelper extends GenericCDIView { ...@@ -45,18 +45,22 @@ public class ProductShopItemHelper extends GenericCDIView {
public void updateProductShopItemCount(ProductShopItem item) { public void updateProductShopItemCount(ProductShopItem item) {
if (item.getProduct().getMinBuyCount() > 0 && item.getCount().compareTo(BigDecimal.ZERO) > 0
&& item.getCount().compareTo(BigDecimal.valueOf(item.getProduct().getMinBuyCount())) < 0) {
item.setCount(BigDecimal.valueOf(item.getProduct().getMinBuyCount()));
}
// Discounts or overridden price, you cannot get both // Discounts or overridden price, you cannot get both
if (item.isOverrideUnitPrice()) { if (item.isOverrideUnitPrice()) {
item.setInternalPrice(item.getOverriddenUnitPrice().multiply(item.getCount())); item.setInternalPrice(item.getOverriddenUnitPrice().multiply(item.getCount()));
} else { } else {
item.setInternalPrice(item.getProduct().getPrice().abs().multiply(item.getCount())); item.setInternalPrice(item.getProduct().getPrice().abs().multiply(item.getCount()));
item.setInternalDiscounts(discountBean.getActiveDiscountsByProduct(item.getProduct(), item.getCount(), new Date(), item.getUser())); item.setInternalDiscounts(discountBean.getActiveDiscountsByProduct(item.getProduct(), item.getCount(),
new Date(), item.getUser()));
item.setInternalDiscountValues(new HashMap<Integer, BigDecimal>()); item.setInternalDiscountValues(new HashMap<Integer, BigDecimal>());
for (Discount d : item.getDiscounts()) for (Discount d : item.getDiscounts()) {
{
BigDecimal newprice = item.getPrice().multiply(d.getPercentage()); BigDecimal newprice = item.getPrice().multiply(d.getPercentage());
item.getInternalDiscountValues().put(d.getId(), item.getPrice().subtract(newprice)); item.getInternalDiscountValues().put(d.getId(), item.getPrice().subtract(newprice));
item.setInternalPrice(newprice); item.setInternalPrice(newprice);
...@@ -67,14 +71,10 @@ public class ProductShopItemHelper extends GenericCDIView { ...@@ -67,14 +71,10 @@ public class ProductShopItemHelper extends GenericCDIView {
public void setProductShopItemCount(ProductShopItem item, BigDecimal count) { public void setProductShopItemCount(ProductShopItem item, BigDecimal count) {
if (count == null || count.compareTo(BigDecimal.ZERO) < 0) if (count == null || count.compareTo(BigDecimal.ZERO) < 0) {
{
count = BigDecimal.ZERO; count = BigDecimal.ZERO;
} else if(item.getProduct().getMinBuyCount() > 0 && count.compareTo(BigDecimal.ZERO) > 0 && count.compareTo(BigDecimal.valueOf(item.getProduct().getMinBuyCount())) < 0) {
count = BigDecimal.valueOf(item.getProduct().getMinBuyCount());
} }
item.setCount(count); item.setCount(count);
updateProductShopItemCount(item); updateProductShopItemCount(item);
......
...@@ -149,6 +149,9 @@ create = Luo ...@@ -149,6 +149,9 @@ create = Luo
delete = Poista delete = Poista
discount.id = ID
discount.sort = Sort nr.
edit = Muokkaa edit = Muokkaa
error = Virhe error = Virhe
......
...@@ -341,6 +341,7 @@ discount.code = Discount code ...@@ -341,6 +341,7 @@ discount.code = Discount code
discount.create = Create new discount.create = Create new
discount.details = Details discount.details = Details
discount.edit = Edit discount.edit = Edit
discount.id = ID
discount.maxNum = Max no of discounts discount.maxNum = Max no of discounts
discount.perUser = Discounts per user discount.perUser = Discounts per user
discount.percentage = Discount multiplier discount.percentage = Discount multiplier
...@@ -348,6 +349,7 @@ discount.products = Products ...@@ -348,6 +349,7 @@ discount.products = Products
discount.role = Role discount discount.role = Role discount
discount.save = Save discount.save = Save
discount.shortdesc = Description discount.shortdesc = Description
discount.sort = Sort nr.
discount.validFrom = Valid from discount.validFrom = Valid from
discount.validTo = Valid until discount.validTo = Valid until
......
...@@ -342,6 +342,7 @@ discount.code = Alennuskoodi ...@@ -342,6 +342,7 @@ discount.code = Alennuskoodi
discount.create = Luo uusi discount.create = Luo uusi
discount.details = Tiedot discount.details = Tiedot
discount.edit = Muokkaa discount.edit = Muokkaa
discount.id = ID
discount.maxNum = Alennusten enimm\u00E4islkm discount.maxNum = Alennusten enimm\u00E4islkm
discount.perUser = Alennuksia per k\u00E4ytt\u00E4j\u00E4 discount.perUser = Alennuksia per k\u00E4ytt\u00E4j\u00E4
discount.percentage = Alennuskerroin discount.percentage = Alennuskerroin
...@@ -349,6 +350,7 @@ discount.products = Tuotteet ...@@ -349,6 +350,7 @@ discount.products = Tuotteet
discount.role = Roolialennus discount.role = Roolialennus
discount.save = Tallenna discount.save = Tallenna
discount.shortdesc = Kuvaus discount.shortdesc = Kuvaus
discount.sort = J\u00E4rjestysnumero
discount.validFrom = Voimassa alkaen discount.validFrom = Voimassa alkaen
discount.validTo = Voimassa asti discount.validTo = Voimassa asti
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!