Implemented initial version for the budget calculation algorithm

FEA: ItEr77S17AutomaticBudgeting
This commit is contained in:
Lorenzo Tilve Álvaro 2013-06-10 12:54:15 +02:00
parent a3a237b9aa
commit 5df152a9bd
5 changed files with 115 additions and 15 deletions

View file

@ -23,6 +23,7 @@ package org.libreplan.business.costcategories.entities;
import static org.libreplan.business.i18n.I18nHelper._; import static org.libreplan.business.i18n.I18nHelper._;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; 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() { public void incrementLastHourCostSequenceCode() {
if (lastHourCostSequenceCode == null) { if (lastHourCostSequenceCode == null) {
lastHourCostSequenceCode = 0; lastHourCostSequenceCode = 0;

View file

@ -21,6 +21,7 @@
package org.libreplan.web.orders; package org.libreplan.web.orders;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -53,6 +54,6 @@ public interface IOrderElementModel {
boolean isCodeAutogenerated(); boolean isCodeAutogenerated();
String getTotalBudget(); BigDecimal getTotalBudget();
} }

View file

@ -21,6 +21,7 @@
package org.libreplan.web.orders; package org.libreplan.web.orders;
import java.math.BigDecimal;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -29,6 +30,8 @@ import java.util.Set;
import org.libreplan.business.common.daos.IConfigurationDAO; import org.libreplan.business.common.daos.IConfigurationDAO;
import org.libreplan.business.common.exceptions.InstanceNotFoundException; 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.costcategories.entities.TypeOfWorkHours;
import org.libreplan.business.orders.daos.IOrderDAO; import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.daos.IOrderElementDAO; import org.libreplan.business.orders.daos.IOrderElementDAO;
@ -62,10 +65,10 @@ public class OrderElementModel implements IOrderElementModel {
private IOrderDAO orderDAO; private IOrderDAO orderDAO;
@Autowired @Autowired
private ICriterionTypeDAO criterionTypeDao; private ICriterionTypeDAO criterionTypeDAO;
@Autowired @Autowired
private ICriterionTypeDAO criterionTypeDAO; private ICostCategoryDAO costCategoryDAO;
private Map<String, CriterionType> mapCriterionTypes = new HashMap<String, CriterionType>(); private Map<String, CriterionType> mapCriterionTypes = new HashMap<String, CriterionType>();
@ -138,7 +141,7 @@ public class OrderElementModel implements IOrderElementModel {
@Transactional(readOnly = true) @Transactional(readOnly = true)
public CriterionType getCriterionType(Criterion criterion) { public CriterionType getCriterionType(Criterion criterion) {
CriterionType criterionType = criterion.getType(); CriterionType criterionType = criterion.getType();
criterionTypeDao.reattach(criterionType); criterionTypeDAO.reattach(criterionType);
criterionType.getName(); criterionType.getName();
return criterionType; return criterionType;
} }
@ -165,24 +168,61 @@ public class OrderElementModel implements IOrderElementModel {
@Override @Override
@Transactional(readOnly = true) @Transactional(readOnly = true)
public String getTotalBudget() { public BigDecimal getTotalBudget() {
String autobudget = ""; BigDecimal totalBudget = new BigDecimal(0);
BigDecimal costPerHour = new BigDecimal(0);
TypeOfWorkHours typeofWorkHours = configurationDAO.getConfiguration() TypeOfWorkHours typeofWorkHours = configurationDAO.getConfiguration()
.getBudgetDefaultTypeOfWorkHours(); .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()) { .getCriterionRequirements()) {
BigDecimal hours = new BigDecimal(getOrderElement().getWorkHours());
try { try {
autobudget += criterionRequirement.getCriterion() totalBudget = totalBudget.add(costPerHour.multiply(hours));
.getCostCategory().getName();
autobudget += typeofWorkHours.getName(); costPerHour = requirement.getCriterion().getCostCategory()
// getHourCostByCode(defaultCode) .getHourCostByCode(typeofWorkHours.getCode())
// .getPriceCost().toBigInteger(); .getPriceCost();
} catch (Exception e) { totalBudget = totalBudget.add(costPerHour.multiply(hours));
} catch (InstanceNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace(); 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;
} }
} }

View file

@ -84,6 +84,7 @@ import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox; import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Constraint; import org.zkoss.zul.Constraint;
import org.zkoss.zul.Datebox; import org.zkoss.zul.Datebox;
import org.zkoss.zul.Label;
import org.zkoss.zul.Popup; import org.zkoss.zul.Popup;
import org.zkoss.zul.Tab; import org.zkoss.zul.Tab;
import org.zkoss.zul.Textbox; import org.zkoss.zul.Textbox;
@ -624,7 +625,10 @@ public class OrderElementTreeController extends TreeController<OrderElement> {
public void addAutoBudgetCell(OrderElement currentElement) { public void addAutoBudgetCell(OrderElement currentElement) {
IOrderElementModel model = orderModel IOrderElementModel model = orderModel
.getOrderElementModel(currentElement); .getOrderElementModel(currentElement);
addCell(new Textbox(model.getTotalBudget())); Textbox autoBudgetCell = new Textbox(Util.addCurrencySymbol(model
.getTotalBudget()));
autoBudgetCell.setDisabled(true);
addCell(autoBudgetCell);
} }
} }

View file

@ -39,6 +39,12 @@ import org.libreplan.business.common.IAdHocTransactionService;
import org.libreplan.business.common.IOnTransaction; import org.libreplan.business.common.IOnTransaction;
import org.libreplan.business.common.daos.IEntitySequenceDAO; import org.libreplan.business.common.daos.IEntitySequenceDAO;
import org.libreplan.business.common.entities.EntityNameEnum; 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.labels.entities.Label;
import org.libreplan.business.orders.daos.IOrderDAO; import org.libreplan.business.orders.daos.IOrderDAO;
import org.libreplan.business.orders.entities.HoursGroup; 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.IMoneyCostCalculator;
import org.libreplan.business.planner.entities.ResourceAllocation; import org.libreplan.business.planner.entities.ResourceAllocation;
import org.libreplan.business.planner.entities.ResourceAllocation.IVisitor; 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.SpecificResourceAllocation;
import org.libreplan.business.planner.entities.StretchesFunction; import org.libreplan.business.planner.entities.StretchesFunction;
import org.libreplan.business.planner.entities.SubcontractorDeliverDate; import org.libreplan.business.planner.entities.SubcontractorDeliverDate;
@ -167,6 +174,12 @@ public class PlanningStateCreator {
@Autowired @Autowired
private IEntitySequenceDAO entitySequenceDAO; private IEntitySequenceDAO entitySequenceDAO;
@Autowired
private ICostCategoryDAO costCategoryDAO;
@Autowired
private IHourCostDAO hourCostDAO;
@Autowired @Autowired
private TaskElementAdapter taskElementAdapterCreator; private TaskElementAdapter taskElementAdapterCreator;
@ -270,6 +283,18 @@ public class PlanningStateCreator {
Scenario currentScenario = scenarioManager.getCurrent(); Scenario currentScenario = scenarioManager.getCurrent();
final List<Resource> allResources = resourceDAO.list(Resource.class); final List<Resource> allResources = resourceDAO.list(Resource.class);
criterionDAO.list(Criterion.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); forceLoadOfOrderAssociatedData(orderReloaded);
TaskGroup rootTask = orderReloaded.getAssociatedTaskElement(); TaskGroup rootTask = orderReloaded.getAssociatedTaskElement();
if (rootTask != null) { if (rootTask != null) {
@ -314,14 +339,27 @@ public class PlanningStateCreator {
c.getCalculatedConsolidatedValues().size(); c.getCalculatedConsolidatedValues().size();
} }
} }
for (CriterionRequirement requirement : each
.getCriterionRequirements()) {
requirement.getCriterion().getCostCategory().getHourCosts()
.size();
}
for (HoursGroup hours : each.getHoursGroups()) { for (HoursGroup hours : each.getHoursGroups()) {
for (CriterionRequirement requirement : hours for (CriterionRequirement requirement : hours
.getCriterionRequirements()) { .getCriterionRequirements()) {
// attach cost categories
if (requirement.getCriterion().getCostCategory() != null) {
requirement.getCriterion().getCostCategory()
.getHourCosts().size();
requirement.ensureDataLoaded(); requirement.ensureDataLoaded();
} }
hours.getValidCriterions().size(); hours.getValidCriterions().size();
} }
} }
}
} }
private void forceLoadDayAssignments(Set<Resource> resources) { private void forceLoadDayAssignments(Set<Resource> resources) {