From d3881db0811cbc4cc3c643888df5e7edddc53a1c Mon Sep 17 00:00:00 2001 From: Franta Pribyl Date: Fri, 3 Oct 2014 13:13:05 +0200 Subject: [PATCH] =?UTF-8?q?P=C5=99i=20maz=C3=A1n=C3=AD=20polo=C5=BEek=20se?= =?UTF-8?q?=20aktualizuje=20grid=20okam=C5=BEit=C4=9B.=20V=C3=BDpo=C4=8Dty?= =?UTF-8?q?=20polo=C5=BEek=20a=20celkov=C3=A9=20=C4=8D=C3=A1stky=20p=C5=99?= =?UTF-8?q?esunuty=20do=20RequirementService.=20BigDecimal=20converter=20s?= =?UTF-8?q?jednocen=20s=20b=C3=A1z=C3=AD.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit closes #146 --- .../requirement/RequirementService.java | 12 ++ .../requirement/RequirementServiceImpl.java | 134 ++++++++++++++---- .../info/bukova/isspst/ui/FormViewModel.java | 2 +- .../main/orders/material/ReqMaterialForm.java | 2 +- .../orders/requirements/RequirementForm.java | 62 +++----- .../main/orders/requirements/reqForm.zul | 10 +- 6 files changed, 147 insertions(+), 75 deletions(-) diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java index 1b841c8d..61c91e6e 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementService.java @@ -1,8 +1,20 @@ package info.bukova.isspst.services.requirement; import info.bukova.isspst.data.Requirement; +import info.bukova.isspst.data.RequirementItem; + +import java.math.BigDecimal; +import java.util.List; public interface RequirementService extends RequirementBaseService { public void loadGroups(Requirement req); + + public BigDecimal calcTotalFromItem(RequirementItem item); + + public RequirementItem calcTotalInItem(RequirementItem item); + + public RequirementItem calcItemValuesFromItemTotal(RequirementItem item); + + public BigDecimal calcSumTotalFromItems(List items); } diff --git a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java index 8eef0ae1..542e375f 100644 --- a/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java +++ b/src/main/java/info/bukova/isspst/services/requirement/RequirementServiceImpl.java @@ -17,9 +17,8 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; -public class RequirementServiceImpl extends - RequirementBaseServiceImpl implements RequirementService, - RequirementBaseService { +public class RequirementServiceImpl extends RequirementBaseServiceImpl implements RequirementService, RequirementBaseService +{ @Autowired private RequirementTypeService reqTypeService; @@ -27,7 +26,8 @@ public class RequirementServiceImpl extends private OrderService orderService; @Override - protected Requirement createEntity() { + protected Requirement createEntity() + { Requirement entity = new Requirement(); entity.setReqDate(new Date()); @@ -38,44 +38,51 @@ public class RequirementServiceImpl extends } @Override - protected boolean autoApprove(Requirement entity) { + protected boolean autoApprove(Requirement entity) + { List approvers = this.getNextApprover(entity); Workflow nextWf = this.getNextWorkflow(entity); - - if (approvers == null || approvers.isEmpty() || nextWf == null || nextWf.getLimit() == null) { + + if (approvers == null || approvers.isEmpty() || nextWf == null || nextWf.getLimit() == null) + { return false; } - - if (entity.getSumTotal().compareTo(nextWf.getLimit()) == -1) { + + if (entity.getSumTotal().compareTo(nextWf.getLimit()) == -1) + { approve(entity, approvers.get(0)); return true; } - + return false; } - + @Override - protected boolean canAdd(Requirement entity) { - if (entity.getWorkgroup() != null - && entity.getWorkgroup().getLimit() != null - && entity.getWorkgroup().getLimit().compareTo(BigDecimal.ZERO) != 0) { - + protected boolean canAdd(Requirement entity) + { + if (entity.getWorkgroup() != null && entity.getWorkgroup().getLimit() != null && entity.getWorkgroup().getLimit().compareTo(BigDecimal.ZERO) != 0) + { + BigDecimal total = orderService.totalOrderedForWorkgroup(entity.getWorkgroup()); - - if (total == null) { + + if (total == null) + { total = BigDecimal.ZERO; } - + total = total.add(entity.getSumTotal()); - - if (total.compareTo(entity.getWorkgroup().getLimit()) <= 0) { + + if (total.compareTo(entity.getWorkgroup().getLimit()) <= 0) + { return true; - } - - } else { + } + + } + else + { return true; } - + throw new AddRequirementException("LimitExceeded"); } @@ -97,9 +104,9 @@ public class RequirementServiceImpl extends { return; } - + Requirement reqDb = this.getById(req.getId()); - + // Načíst položky požadavku List items = reqDb.getItems(); @@ -128,4 +135,77 @@ public class RequirementServiceImpl extends // materiálů a služeb konzistentní req.setItems(items); } + + @Override + public BigDecimal calcTotalFromItem(RequirementItem item) + { + BigDecimal total = BigDecimal.ZERO; + + if (item != null) + { + total = item.getQuantity().multiply(item.getUnitPrice()); + } + + return total; + } + + @Override + public RequirementItem calcTotalInItem(RequirementItem item) + { + RequirementItem newItem = item; + + if (newItem != null) + { + BigDecimal total = this.calcTotalFromItem(newItem); + newItem.setTotal(total); + } + + return newItem; + } + + @Override + public RequirementItem calcItemValuesFromItemTotal(RequirementItem item) + { + RequirementItem 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 (RequirementItem 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/FormViewModel.java b/src/main/java/info/bukova/isspst/ui/FormViewModel.java index 45faff34..da5b3dcd 100644 --- a/src/main/java/info/bukova/isspst/ui/FormViewModel.java +++ b/src/main/java/info/bukova/isspst/ui/FormViewModel.java @@ -30,7 +30,7 @@ public class FormViewModel extends DocumentViewModel private ServiceConstraint constraint; @Init - public void init(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) + public void initFormViewModel(@ExecutionArgParam("selected") T selected, @ExecutionArgParam("service") Service service) { super.initDocumentViewModel(); diff --git a/src/main/java/info/bukova/isspst/ui/main/orders/material/ReqMaterialForm.java b/src/main/java/info/bukova/isspst/ui/main/orders/material/ReqMaterialForm.java index 37819778..a3cedcca 100644 --- a/src/main/java/info/bukova/isspst/ui/main/orders/material/ReqMaterialForm.java +++ b/src/main/java/info/bukova/isspst/ui/main/orders/material/ReqMaterialForm.java @@ -25,7 +25,7 @@ public class ReqMaterialForm extends RequirementForm @SuppressWarnings("unchecked") @Init(superclass = true) - public void init() + public void initReqMaterialForm() { // Získat seznam všech skupin materiálu pro výběr z comba this.setRequirementGroups((List) (List) materialService.getAll()); diff --git a/src/main/java/info/bukova/isspst/ui/main/orders/requirements/RequirementForm.java b/src/main/java/info/bukova/isspst/ui/main/orders/requirements/RequirementForm.java index b55f8607..2c36d4fe 100644 --- a/src/main/java/info/bukova/isspst/ui/main/orders/requirements/RequirementForm.java +++ b/src/main/java/info/bukova/isspst/ui/main/orders/requirements/RequirementForm.java @@ -9,7 +9,6 @@ import info.bukova.isspst.services.requirement.RequirementService; import info.bukova.isspst.services.requirement.RequirementTypeService; import info.bukova.isspst.services.users.UserService; import info.bukova.isspst.services.workgroups.WorkgroupService; -import info.bukova.isspst.ui.BigDecimalConverter; import info.bukova.isspst.ui.FormViewModel; import info.bukova.isspst.validators.RequirementFormValidator; @@ -64,8 +63,6 @@ public class RequirementForm extends FormViewModel private int selItemIndex; - private BigDecimalConverter bigDecimalConverter; - private RequirementFormValidator requirementFormValidator; private List syncItems; @@ -97,16 +94,6 @@ public class RequirementForm extends FormViewModel this.selItemIndex = selItemIndex; } - public BigDecimalConverter getBigDecimalConverter() - { - return bigDecimalConverter; - } - - public void setBigDecimalConverter(BigDecimalConverter bigDecimalConverter) - { - this.bigDecimalConverter = bigDecimalConverter; - } - public RequirementFormValidator getRequirementFormValidator() { return requirementFormValidator; @@ -121,7 +108,6 @@ public class RequirementForm extends FormViewModel public void initRequirementForm() { this.setSelItemIndex(-1); - this.setBigDecimalConverter(new BigDecimalConverter()); this.setRequirementFormValidator(new RequirementFormValidator()); this.setSyncItems(this.getDataBean().getItems()); requirementService.loadType(getDataBean()); @@ -150,12 +136,26 @@ public class RequirementForm extends FormViewModel } } + protected void calcAndUpdateFormTotalPrice(SimpleForm form) + { + if (form != null) + { + BigDecimal sumTotal = requirementService.calcSumTotalFromItems(this.getDataBean().getItems()); + form.setField("sumTotal", sumTotal); + BindUtils.postNotifyChange(null, null, form, "*"); + } + } + @Command - @NotifyChange({ "dataBean", "selItemIndex" }) - public void removeItem(@BindingParam("item") RequirementItem item) + @NotifyChange({ "syncItems", "selItemIndex" }) + public void removeItem(@BindingParam("form") SimpleForm form, @BindingParam("item") RequirementItem item) { - this.getDataBean().getItems().remove(item); - this.setSelItemIndex(-1); + if (item != null) + { + this.getDataBean().getItems().remove(item); + this.setSelItemIndex(-1); + this.calcAndUpdateFormTotalPrice(form); + } } @GlobalCommand("insertSelectedItem") @@ -207,35 +207,15 @@ public class RequirementForm extends FormViewModel // Recalculate selected item if ((source != null) && (source.equals("total"))) { - BigDecimal quantity = this.selectedItem.getQuantity(); - - if (quantity.equals(BigDecimal.ZERO)) - { - this.selectedItem.setUnitPrice(BigDecimal.ZERO); - } - else - { - this.selectedItem.setUnitPrice(this.selectedItem.getTotal().divide(quantity, 2, BigDecimal.ROUND_HALF_UP)); - } + this.selectedItem = requirementService.calcItemValuesFromItemTotal(this.selectedItem); } else { - this.selectedItem.setTotal(this.selectedItem.getQuantity().multiply(this.selectedItem.getUnitPrice())); + this.selectedItem = requirementService.calcTotalInItem(this.selectedItem); } // Calculate total price at form - if (form != null) - { - BigDecimal sumTotal = new BigDecimal(0); - - for (RequirementItem item : this.getDataBean().getItems()) - { - sumTotal = sumTotal.add(item.getTotal()); - } - - form.setField("sumTotal", sumTotal); - BindUtils.postNotifyChange(null, null, form, "*"); - } + this.calcAndUpdateFormTotalPrice(form); } @Command diff --git a/src/main/webapp/main/orders/requirements/reqForm.zul b/src/main/webapp/main/orders/requirements/reqForm.zul index 3b871242..9a2a10f2 100644 --- a/src/main/webapp/main/orders/requirements/reqForm.zul +++ b/src/main/webapp/main/orders/requirements/reqForm.zul @@ -64,7 +64,7 @@ id="idSumTotal" readonly="true" width="150px" - value="@bind(fx.sumTotal) @converter(vm.bigDecimalConverter)" /> + value="@bind(fx.sumTotal) @converter(vm.standardBigDecimalConverter)" /> @@ -168,7 +168,7 @@ sclass="grid-textbox-max-right" onFocus="@command('onFocusItem', item=each, ctrl=self)" onChange="@command('recalculate', form=fx, changed='quantity')" - value="@bind(each.quantity) @converter(vm.bigDecimalConverter)" /> + value="@bind(each.quantity) @converter(vm.standardBigDecimalConverter)" /> + value="@bind(each.unitPrice) @converter(vm.standardBigDecimalConverter)" /> + value="@bind(each.total) @converter(vm.standardBigDecimalConverter)" />