diff --git a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/CostCategory.java b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/CostCategory.java index e8b5faa36..ddc976623 100644 --- a/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/CostCategory.java +++ b/libreplan-business/src/main/java/org/libreplan/business/costcategories/entities/CostCategory.java @@ -23,6 +23,7 @@ package org.libreplan.business.costcategories.entities; import static org.libreplan.business.i18n.I18nHelper._; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashSet; import java.util.List; @@ -302,6 +303,22 @@ public class CostCategory extends IntegrationEntity implements IHumanIdentifiabl } } + public BigDecimal getPriceCostByTypeOfWorkHour(String code) + throws InstanceNotFoundException { + + if (StringUtils.isBlank(code)) { + throw new InstanceNotFoundException(code, HourCost.class.getName()); + } + + for (HourCost c : this.hourCosts) { + if (c.getType().getCode().equalsIgnoreCase(StringUtils.trim(code))) { + return c.getPriceCost(); + } + } + + throw new InstanceNotFoundException(code, HourCost.class.getName()); + } + public void incrementLastHourCostSequenceCode() { if (lastHourCostSequenceCode == null) { lastHourCostSequenceCode = 0; diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/orders/IOrderElementModel.java b/libreplan-webapp/src/main/java/org/libreplan/web/orders/IOrderElementModel.java index 39cba6b28..c71f8b0c4 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/orders/IOrderElementModel.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/orders/IOrderElementModel.java @@ -21,6 +21,7 @@ package org.libreplan.web.orders; +import java.math.BigDecimal; import java.util.List; import java.util.Set; @@ -53,6 +54,6 @@ public interface IOrderElementModel { boolean isCodeAutogenerated(); - String getTotalBudget(); + BigDecimal getTotalBudget(); } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementModel.java b/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementModel.java index 15a0eaec1..498ec0bd5 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementModel.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementModel.java @@ -21,6 +21,7 @@ package org.libreplan.web.orders; +import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -29,6 +30,8 @@ import java.util.Set; import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.exceptions.InstanceNotFoundException; +import org.libreplan.business.costcategories.daos.ICostCategoryDAO; +import org.libreplan.business.costcategories.entities.CostCategory; import org.libreplan.business.costcategories.entities.TypeOfWorkHours; import org.libreplan.business.orders.daos.IOrderDAO; import org.libreplan.business.orders.daos.IOrderElementDAO; @@ -62,10 +65,10 @@ public class OrderElementModel implements IOrderElementModel { private IOrderDAO orderDAO; @Autowired - private ICriterionTypeDAO criterionTypeDao; + private ICriterionTypeDAO criterionTypeDAO; @Autowired - private ICriterionTypeDAO criterionTypeDAO; + private ICostCategoryDAO costCategoryDAO; private Map mapCriterionTypes = new HashMap(); @@ -138,7 +141,7 @@ public class OrderElementModel implements IOrderElementModel { @Transactional(readOnly = true) public CriterionType getCriterionType(Criterion criterion) { CriterionType criterionType = criterion.getType(); - criterionTypeDao.reattach(criterionType); + criterionTypeDAO.reattach(criterionType); criterionType.getName(); return criterionType; } @@ -165,24 +168,61 @@ public class OrderElementModel implements IOrderElementModel { @Override @Transactional(readOnly = true) - public String getTotalBudget() { - String autobudget = ""; + public BigDecimal getTotalBudget() { + BigDecimal totalBudget = new BigDecimal(0); + BigDecimal costPerHour = new BigDecimal(0); TypeOfWorkHours typeofWorkHours = configurationDAO.getConfiguration() .getBudgetDefaultTypeOfWorkHours(); - for (CriterionRequirement criterionRequirement : getOrderElement() + + if (typeofWorkHours == null) { + return totalBudget; + } + + // FIXME: This workarounds LazyException when adding new + // criteria but disables the refresh on changes + for (CriterionRequirement requirement : getOrderElement() .getCriterionRequirements()) { + BigDecimal hours = new BigDecimal(getOrderElement().getWorkHours()); try { - autobudget += criterionRequirement.getCriterion() - .getCostCategory().getName(); - autobudget += typeofWorkHours.getName(); - // getHourCostByCode(defaultCode) - // .getPriceCost().toBigInteger(); - } catch (Exception e) { + totalBudget = totalBudget.add(costPerHour.multiply(hours)); + + costPerHour = requirement.getCriterion().getCostCategory() + .getHourCostByCode(typeofWorkHours.getCode()) + .getPriceCost(); + totalBudget = totalBudget.add(costPerHour.multiply(hours)); + + } catch (InstanceNotFoundException e) { + // TODO Auto-generated catch block e.printStackTrace(); } + totalBudget = totalBudget.add(costPerHour.multiply(hours)); } - return autobudget; + + for (HoursGroup hoursGroup : getOrderElement().getHoursGroups()) { + BigDecimal hours = new BigDecimal(hoursGroup.getWorkingHours()); + + for (CriterionRequirement crit : hoursGroup + .getCriterionRequirements()) { + CostCategory costcat = crit.getCriterion().getCostCategory(); + + + if (costcat != null) { + try { + costPerHour = costcat + .getPriceCostByTypeOfWorkHour(typeofWorkHours + .getCode()); + } catch (InstanceNotFoundException e) { + // Default values to 0 + costPerHour = new BigDecimal(0); + } + } + } + totalBudget = totalBudget.add(costPerHour.multiply(hours)); + } + + + return totalBudget; } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementTreeController.java b/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementTreeController.java index 37ce8c8f3..a84ca5038 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementTreeController.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/orders/OrderElementTreeController.java @@ -84,6 +84,7 @@ import org.zkoss.zul.Button; import org.zkoss.zul.Checkbox; import org.zkoss.zul.Constraint; import org.zkoss.zul.Datebox; +import org.zkoss.zul.Label; import org.zkoss.zul.Popup; import org.zkoss.zul.Tab; import org.zkoss.zul.Textbox; @@ -624,7 +625,10 @@ public class OrderElementTreeController extends TreeController { public void addAutoBudgetCell(OrderElement currentElement) { IOrderElementModel model = orderModel .getOrderElementModel(currentElement); - addCell(new Textbox(model.getTotalBudget())); + Textbox autoBudgetCell = new Textbox(Util.addCurrencySymbol(model + .getTotalBudget())); + autoBudgetCell.setDisabled(true); + addCell(autoBudgetCell); } } diff --git a/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java b/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java index 3adcc18d6..49ff69f2c 100644 --- a/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java +++ b/libreplan-webapp/src/main/java/org/libreplan/web/planner/order/PlanningStateCreator.java @@ -39,6 +39,12 @@ import org.libreplan.business.common.IAdHocTransactionService; import org.libreplan.business.common.IOnTransaction; import org.libreplan.business.common.daos.IEntitySequenceDAO; import org.libreplan.business.common.entities.EntityNameEnum; +import org.libreplan.business.common.exceptions.InstanceNotFoundException; +import org.libreplan.business.costcategories.daos.CostCategoryDAO; +import org.libreplan.business.costcategories.daos.ICostCategoryDAO; +import org.libreplan.business.costcategories.daos.IHourCostDAO; +import org.libreplan.business.costcategories.entities.CostCategory; +import org.libreplan.business.costcategories.entities.HourCost; import org.libreplan.business.labels.entities.Label; import org.libreplan.business.orders.daos.IOrderDAO; import org.libreplan.business.orders.entities.HoursGroup; @@ -59,6 +65,7 @@ import org.libreplan.business.planner.entities.GenericResourceAllocation; import org.libreplan.business.planner.entities.IMoneyCostCalculator; import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.ResourceAllocation.IVisitor; +import org.libreplan.business.planner.entities.HoursCostCalculator; import org.libreplan.business.planner.entities.SpecificResourceAllocation; import org.libreplan.business.planner.entities.StretchesFunction; import org.libreplan.business.planner.entities.SubcontractorDeliverDate; @@ -167,6 +174,12 @@ public class PlanningStateCreator { @Autowired private IEntitySequenceDAO entitySequenceDAO; + @Autowired + private ICostCategoryDAO costCategoryDAO; + + @Autowired + private IHourCostDAO hourCostDAO; + @Autowired private TaskElementAdapter taskElementAdapterCreator; @@ -270,6 +283,18 @@ public class PlanningStateCreator { Scenario currentScenario = scenarioManager.getCurrent(); final List allResources = resourceDAO.list(Resource.class); criterionDAO.list(Criterion.class); + + // Attaching cost categories with their hourcosts and types + for (CostCategory costCategory : costCategoryDAO + .list(CostCategory.class)) { + if (costCategory.getHourCosts() != null) { + for (HourCost hourCost : costCategory.getHourCosts()) { + hourCost.getPriceCost(); + hourCost.getType().getDefaultPrice(); + } + } + } + forceLoadOfOrderAssociatedData(orderReloaded); TaskGroup rootTask = orderReloaded.getAssociatedTaskElement(); if (rootTask != null) { @@ -314,14 +339,27 @@ public class PlanningStateCreator { c.getCalculatedConsolidatedValues().size(); } } + + for (CriterionRequirement requirement : each + .getCriterionRequirements()) { + requirement.getCriterion().getCostCategory().getHourCosts() + .size(); + } for (HoursGroup hours : each.getHoursGroups()) { for (CriterionRequirement requirement : hours .getCriterionRequirements()) { + + // attach cost categories + if (requirement.getCriterion().getCostCategory() != null) { + requirement.getCriterion().getCostCategory() + .getHourCosts().size(); + requirement.ensureDataLoaded(); } hours.getValidCriterions().size(); } } + } } private void forceLoadDayAssignments(Set resources) {