diff --git a/src/main/java/info/bukova/isspst/services/approved/OrderService.java b/src/main/java/info/bukova/isspst/services/approved/OrderService.java index c0aed201..d1cc5636 100644 --- a/src/main/java/info/bukova/isspst/services/approved/OrderService.java +++ b/src/main/java/info/bukova/isspst/services/approved/OrderService.java @@ -1,16 +1,25 @@ package info.bukova.isspst.services.approved; -import java.math.BigDecimal; -import java.util.List; - import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.Order; +import info.bukova.isspst.data.OrderItem; import info.bukova.isspst.data.Workgroup; import info.bukova.isspst.services.Service; +import java.math.BigDecimal; +import java.util.List; + public interface OrderService extends Service { public Order createOrder(List items); public BigDecimal totalOrderedForWorkgroup(Workgroup workgroup); + public BigDecimal calcTotalFromItem(OrderItem item); + + public OrderItem calcTotalInItem(OrderItem item); + + public OrderItem calcItemValuesFromItemTotal(OrderItem item); + + public BigDecimal calcSumTotalFromItems(List items); + } diff --git a/src/main/java/info/bukova/isspst/services/approved/OrderServiceImpl.java b/src/main/java/info/bukova/isspst/services/approved/OrderServiceImpl.java index 9ddf678b..0c55466a 100644 --- a/src/main/java/info/bukova/isspst/services/approved/OrderServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/approved/OrderServiceImpl.java @@ -4,6 +4,7 @@ import info.bukova.isspst.data.AddressEmb; import info.bukova.isspst.data.JoinedItem; import info.bukova.isspst.data.Order; import info.bukova.isspst.data.OrderItem; +import info.bukova.isspst.data.Workgroup; import info.bukova.isspst.services.AbstractOwnedService; import info.bukova.isspst.services.LazyLoader; import info.bukova.isspst.services.settings.GlobalSettingsService; @@ -17,8 +18,6 @@ import org.hibernate.Query; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.transaction.annotation.Transactional; - -import info.bukova.isspst.data.Workgroup; public class OrderServiceImpl extends AbstractOwnedService implements OrderService { @@ -99,4 +98,77 @@ public class OrderServiceImpl extends AbstractOwnedService implements Hibernate.initialize(o.getItems()); order.setItems(o.getItems()); } + + @Override + public BigDecimal calcTotalFromItem(OrderItem item) + { + BigDecimal total = BigDecimal.ZERO; + + if (item != null) + { + total = item.getQuantity().multiply(item.getUnitPrice()); + } + + return total; + } + + @Override + public OrderItem calcTotalInItem(OrderItem item) + { + OrderItem newItem = item; + + if (newItem != null) + { + BigDecimal total = this.calcTotalFromItem(newItem); + newItem.setTotal(total); + } + + return newItem; + } + + @Override + public OrderItem calcItemValuesFromItemTotal(OrderItem item) + { + OrderItem newItem = item; + + if (newItem != null) + { + BigDecimal quantity = newItem.getQuantity(); + + if (quantity.equals(BigDecimal.ZERO)) + { + newItem.setUnitPrice(BigDecimal.ZERO); + } + else + { + newItem.setUnitPrice(newItem.getTotal().divide(quantity, 2, BigDecimal.ROUND_HALF_UP)); + } + } + + return newItem; + } + + @Override + public BigDecimal calcSumTotalFromItems(List items) + { + BigDecimal sumTotal = BigDecimal.ZERO; + + if (items != null) + { + for (OrderItem item : items) + { + if (item != null) + { + BigDecimal total = item.getTotal(); + + if (total != null) + { + sumTotal = sumTotal.add(total); + } + } + } + } + + return sumTotal; + } } diff --git a/src/main/java/info/bukova/isspst/ui/main/orders/created/OrderForm.java b/src/main/java/info/bukova/isspst/ui/main/orders/created/OrderForm.java index 499aaa09..3905a8aa 100644 --- a/src/main/java/info/bukova/isspst/ui/main/orders/created/OrderForm.java +++ b/src/main/java/info/bukova/isspst/ui/main/orders/created/OrderForm.java @@ -5,22 +5,27 @@ import info.bukova.isspst.data.AddressEmb; import info.bukova.isspst.data.Order; import info.bukova.isspst.data.OrderItem; import info.bukova.isspst.services.addressbook.AdbService; +import info.bukova.isspst.services.approved.OrderService; import info.bukova.isspst.services.settings.GlobalSettingsService; import info.bukova.isspst.ui.FormViewModel; import info.bukova.isspst.validators.OrderFormValidator; +import java.math.BigDecimal; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.zkoss.bind.BindUtils; +import org.zkoss.bind.SimpleForm; +import org.zkoss.bind.annotation.BindingParam; import org.zkoss.bind.annotation.Command; import org.zkoss.bind.annotation.Init; import org.zkoss.bind.annotation.NotifyChange; import org.zkoss.zk.ui.select.annotation.WireVariable; +import org.zkoss.zul.impl.InputElement; public class OrderForm extends FormViewModel { - @SuppressWarnings("unused") private final static Logger log = LoggerFactory.getLogger(OrderForm.class.getName()); @WireVariable @@ -29,6 +34,9 @@ public class OrderForm extends FormViewModel @WireVariable protected GlobalSettingsService settingsService; + @WireVariable + protected OrderService orderService; + protected OrderFormValidator orderFormValidator; protected OrderItem selectedItem; @@ -41,6 +49,8 @@ public class OrderForm extends FormViewModel protected String deliveryCompany; + protected List syncOrderItems; + @Init(superclass = true) public void initOrderForm() { @@ -66,6 +76,8 @@ public class OrderForm extends FormViewModel { this.getDataBean().setAddress(new AddressEmb()); } + + this.syncOrderItems = this.getDataBean().getItems(); } public OrderFormValidator getOrderFormValidator() @@ -143,6 +155,16 @@ public class OrderForm extends FormViewModel this.deliveryCompany = deliveryCompany; } + public List getSyncOrderItems() + { + return syncOrderItems; + } + + public void setSyncOrderItems(List syncOrderItems) + { + this.syncOrderItems = syncOrderItems; + } + @Command @NotifyChange("dataBean") public void doFillSuppAddress() @@ -184,4 +206,50 @@ public class OrderForm extends FormViewModel this.getDataBean().setDeliveryAddress(addr); } + + @Command + public void onFocusItem(@BindingParam("item") OrderItem item, @BindingParam("ctrl") InputElement ctrl) + { + // this.selItemIndex = this.getDataBean().getItems().indexOf(item); + this.selectedItem = item; + + if (ctrl != null) + { + ctrl.select(); + } + } + + protected void calcAndUpdateFormTotalPrice(SimpleForm form) + { + if (form != null) + { + BigDecimal sumTotal = orderService.calcSumTotalFromItems(this.getDataBean().getItems()); + form.setField("total", sumTotal); + BindUtils.postNotifyChange(null, null, form, "*"); + } + } + + @Command + @NotifyChange({ "selectedItem", "syncOrderItems" }) + public void recalculate(@BindingParam("form") SimpleForm form, @BindingParam("changed") String source) + { + if (this.selectedItem == null) + { + log.warn("Zavolat z formuláře onFocus pro nastavení vybrané položky!"); + return; + } + + // Recalculate selected item + if ((source != null) && (source.equals("total"))) + { + this.selectedItem = orderService.calcItemValuesFromItemTotal(this.selectedItem); + } + else + { + this.selectedItem = orderService.calcTotalInItem(this.selectedItem); + } + + // Calculate total price at form + this.calcAndUpdateFormTotalPrice(form); + } } diff --git a/src/main/webapp/main/orders/created/orderForm.zul b/src/main/webapp/main/orders/created/orderForm.zul index 7adb2f24..ac52e743 100644 --- a/src/main/webapp/main/orders/created/orderForm.zul +++ b/src/main/webapp/main/orders/created/orderForm.zul @@ -1,4 +1,6 @@ + + + model="@load(vm.syncOrderItems)"> - + + + - - + + + + + +