use of com.qcadoo.mes.operationTimeCalculations.OperationWorkTime in project mes by qcadoo.
the class OperationDurationDetailsInOrderListeners method generateRealizationTime.
@Transactional
public void generateRealizationTime(final ViewDefinitionState viewDefinitionState, final ComponentState state, final String[] args) {
FormComponent orderForm = (FormComponent) viewDefinitionState.getComponentByReference(QcadooViewConstants.L_FORM);
FieldComponent startTimeField = (FieldComponent) viewDefinitionState.getComponentByReference(L_START_TIME);
LookupComponent prodLine = (LookupComponent) viewDefinitionState.getComponentByReference(OrderFields.PRODUCTION_LINE);
if (!StringUtils.hasText((String) startTimeField.getFieldValue())) {
startTimeField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
if (prodLine.isEmpty()) {
prodLine.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
FieldComponent plannedQuantityField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PLANNED_QUANTITY);
FieldComponent productionLineLookup = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFields.PRODUCTION_LINE);
FieldComponent generatedEndDateField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.GENERATED_END_DATE);
FieldComponent includeTpzField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_TPZ);
FieldComponent includeAdditionalTimeField = (FieldComponent) viewDefinitionState.getComponentByReference(OrderFieldsPS.INCLUDE_ADDITIONAL_TIME);
boolean isGenerated = false;
Entity productionLine = dataDefinitionService.get(ProductionLinesConstants.PLUGIN_IDENTIFIER, ProductionLinesConstants.MODEL_PRODUCTION_LINE).get((Long) productionLineLookup.getFieldValue());
Entity order = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_ORDER).get(orderForm.getEntity().getId());
// copy of technology from order
Entity technology = order.getBelongsToField(OrderFields.TECHNOLOGY);
Validate.notNull(technology, "technology is null");
BigDecimal quantity = orderRealizationTimeService.getBigDecimalFromField(plannedQuantityField.getFieldValue(), viewDefinitionState.getLocale());
// Included in work time
boolean includeTpz = "1".equals(includeTpzField.getFieldValue());
boolean includeAdditionalTime = "1".equals(includeAdditionalTimeField.getFieldValue());
final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();
productQuantitiesService.getProductComponentQuantities(technology, quantity, operationRuns);
operationWorkTimeService.deleteOperCompTimeCalculations(order);
OperationWorkTime workTime = operationWorkTimeService.estimateTotalWorkTimeForOrder(order, operationRuns, includeTpz, includeAdditionalTime, true);
fillWorkTimeFields(viewDefinitionState, workTime);
order = getActualOrderWithChanges(order);
int maxPathTime = orderRealizationTimeService.estimateMaxOperationTimeConsumptionForWorkstation(order, technology.getTreeField(TechnologyFields.OPERATION_COMPONENTS).getRoot(), quantity, includeTpz, includeAdditionalTime, productionLine);
if (maxPathTime > OrderRealizationTimeService.MAX_REALIZATION_TIME) {
state.addMessage("orders.validate.global.error.RealizationTimeIsToLong", MessageType.FAILURE);
generatedEndDateField.setFieldValue(null);
} else {
order.setField(OrderFieldsPS.REALIZATION_TIME, maxPathTime);
Date startTime = order.getDateField(OrderFields.DATE_FROM);
if (startTime == null) {
startTimeField.addMessage("orders.validate.global.error.dateFromIsNull", MessageType.FAILURE);
} else {
if (maxPathTime == 0) {
orderForm.addMessage("productionScheduling.timenorms.isZero", MessageType.FAILURE, false);
generatedEndDateField.setFieldValue(null);
} else {
productionSchedulingService.scheduleOrder(order.getId());
isGenerated = true;
}
orderForm.addMessage("orders.dateFrom.info.dateFromSetToFirstPossible", MessageType.INFO, false);
}
}
generatedEndDateField.requestComponentUpdateState();
if (isGenerated) {
order = getActualOrderWithChanges(order);
Entity orderTimeCalculation = dataDefinitionService.get(TimeNormsConstants.PLUGIN_PRODUCTION_SCHEDULING_IDENTIFIER, TimeNormsConstants.MODEL_ORDER_TIME_CALCULATION).find().add(SearchRestrictions.belongsTo(OrderTimeCalculationFields.ORDER, order)).setMaxResults(1).uniqueResult();
order.setField(OrderFields.START_DATE, orderRealizationTimeService.setDateToField(orderTimeCalculation.getDateField(OrderTimeCalculationFields.EFFECTIVE_DATE_FROM)));
order.setField(OrderFieldsPS.GENERATED_END_DATE, orderRealizationTimeService.setDateToField(orderTimeCalculation.getDateField(OrderTimeCalculationFields.EFFECTIVE_DATE_TO)));
order = order.getDataDefinition().save(order);
orderForm.setEntity(order);
orderForm.addMessage("productionScheduling.info.calculationGenerated", MessageType.SUCCESS);
}
}
use of com.qcadoo.mes.operationTimeCalculations.OperationWorkTime in project mes by qcadoo.
the class OrderTimePredictionListeners method changeRealizationTime.
@Transactional
public void changeRealizationTime(final ViewDefinitionState view, final ComponentState state, final String[] args) {
FormComponent orderForm = (FormComponent) view.getComponentByReference(QcadooViewConstants.L_FORM);
FieldComponent technologyLookup = (FieldComponent) view.getComponentByReference(OrderFields.TECHNOLOGY);
FieldComponent plannedQuantityField = (FieldComponent) view.getComponentByReference(OrderFields.PLANNED_QUANTITY);
FieldComponent dateFromField = (FieldComponent) view.getComponentByReference(OrderFields.DATE_FROM);
FieldComponent dateToField = (FieldComponent) view.getComponentByReference(OrderFields.DATE_TO);
FieldComponent productionLineLookup = (FieldComponent) view.getComponentByReference(OrderFields.PRODUCTION_LINE);
boolean isGenerated = false;
if (technologyLookup.getFieldValue() == null) {
technologyLookup.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
if (!StringUtils.hasText((String) dateFromField.getFieldValue())) {
dateFromField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
if (!StringUtils.hasText((String) plannedQuantityField.getFieldValue())) {
plannedQuantityField.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
if (productionLineLookup.getFieldValue() == null) {
productionLineLookup.addMessage(L_PRODUCTION_SCHEDULING_ERROR_FIELD_REQUIRED, MessageType.FAILURE);
return;
}
BigDecimal quantity;
Object value = plannedQuantityField.getFieldValue();
if (value instanceof BigDecimal) {
quantity = (BigDecimal) value;
} else if (value.toString().matches(".*[a-zA-Z].*")) {
plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidNumericFormat", MessageType.FAILURE);
return;
} else {
try {
ParsePosition parsePosition = new ParsePosition(0);
String trimmedValue = value.toString().replaceAll(" ", "");
DecimalFormat formatter = (DecimalFormat) NumberFormat.getNumberInstance(view.getLocale());
formatter.setParseBigDecimal(true);
quantity = new BigDecimal(String.valueOf(formatter.parseObject(trimmedValue, parsePosition)));
} catch (NumberFormatException e) {
plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidNumericFormat", MessageType.FAILURE);
return;
}
}
int scale = quantity.scale();
if (scale > MAX) {
plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidScale.max", MessageType.FAILURE, MAX.toString());
return;
}
int presicion = quantity.precision() - scale;
if (presicion > MAX) {
plannedQuantityField.addMessage("qcadooView.validate.field.error.invalidPrecision.max", MessageType.FAILURE, MAX.toString());
return;
}
if (BigDecimal.ZERO.compareTo(quantity) >= 0) {
plannedQuantityField.addMessage("qcadooView.validate.field.error.outOfRange.toSmall", MessageType.FAILURE);
return;
}
Entity technology = dataDefinitionService.get(TechnologiesConstants.PLUGIN_IDENTIFIER, TechnologiesConstants.MODEL_TECHNOLOGY).get((Long) technologyLookup.getFieldValue());
Validate.notNull(technology, "technology is null");
if (technology.getStringField(TechnologyFields.STATE).equals(TechnologyState.DRAFT.getStringValue()) || technology.getStringField(TechnologyFields.STATE).equals(TechnologyState.OUTDATED.getStringValue())) {
technologyLookup.addMessage("productionScheduling.technology.incorrectState", MessageType.FAILURE);
return;
}
FieldComponent laborWorkTimeField = (FieldComponent) view.getComponentByReference(OrderFieldsPS.LABOR_WORK_TIME);
FieldComponent machineWorkTimeField = (FieldComponent) view.getComponentByReference(OrderFieldsPS.MACHINE_WORK_TIME);
FieldComponent includeTpzField = (FieldComponent) view.getComponentByReference(OrderFieldsPS.INCLUDE_TPZ);
FieldComponent includeAdditionalTimeField = (FieldComponent) view.getComponentByReference(OrderFieldsPS.INCLUDE_ADDITIONAL_TIME);
boolean includeTpz = "1".equals(includeTpzField.getFieldValue());
boolean includeAdditionalTime = "1".equals(includeAdditionalTimeField.getFieldValue());
Entity productionLine = dataDefinitionService.get(ProductionLinesConstants.PLUGIN_IDENTIFIER, ProductionLinesConstants.MODEL_PRODUCTION_LINE).get((Long) productionLineLookup.getFieldValue());
final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();
productQuantitiesService.getProductComponentQuantities(technology, quantity, operationRuns);
OperationWorkTime workTime = operationWorkTimeService.estimateTotalWorkTimeForTechnology(technology, operationRuns, includeTpz, includeAdditionalTime, true);
laborWorkTimeField.setFieldValue(workTime.getLaborWorkTime());
machineWorkTimeField.setFieldValue(workTime.getMachineWorkTime());
int maxPathTime = orderRealizationTimeService.estimateOperationTimeConsumption(technology.getTreeField(TechnologyFields.OPERATION_COMPONENTS).getRoot(), quantity, includeTpz, includeAdditionalTime, productionLine);
if (maxPathTime > OrderRealizationTimeService.MAX_REALIZATION_TIME) {
state.addMessage("orders.validate.global.error.RealizationTimeIsToLong", MessageType.FAILURE);
dateToField.setFieldValue(null);
} else {
Date startTime = DateUtils.parseDate(dateFromField.getFieldValue());
if (startTime == null) {
dateFromField.addMessage("orders.validate.global.error.dateFromIsNull", MessageType.FAILURE);
} else {
if (maxPathTime == 0) {
orderForm.addMessage("productionScheduling.timenorms.isZero", MessageType.FAILURE, false);
dateToField.setFieldValue(null);
} else {
Date stopTime = shiftsService.findDateToForProductionLine(startTime, maxPathTime, productionLine);
dateToField.setFieldValue(orderRealizationTimeService.setDateToField(stopTime));
Optional<DateTime> startTimeOptional = shiftsService.getNearestWorkingDate(new DateTime(startTime), productionLine);
startTimeOptional.ifPresent(e -> {
scheduleOperationComponents(technology.getId(), startTime, productionLine);
orderForm.addMessage("orders.dateFrom.info.dateFromSetToFirstPossible", MessageType.INFO, false);
});
isGenerated = true;
}
}
}
laborWorkTimeField.requestComponentUpdateState();
machineWorkTimeField.requestComponentUpdateState();
dateFromField.requestComponentUpdateState();
dateToField.requestComponentUpdateState();
orderForm.setEntity(orderForm.getEntity());
state.performEvent(view, "refresh");
if (isGenerated) {
orderForm.addMessage("productionScheduling.info.calculationGenerated", MessageType.SUCCESS);
}
}
use of com.qcadoo.mes.operationTimeCalculations.OperationWorkTime in project mes by qcadoo.
the class ScheduleDetailsListenersPS method getOperations.
@Transactional
public void getOperations(final ViewDefinitionState view, final ComponentState state, final String[] args) {
GridComponent ordersGrid = (GridComponent) view.getComponentByReference(ScheduleFields.ORDERS);
List<Entity> orders = ordersGrid.getEntities();
FormComponent formComponent = (FormComponent) state;
Entity schedule = formComponent.getEntity();
boolean includeTpz = schedule.getBooleanField(ScheduleFields.INCLUDE_TPZ);
DataDefinition schedulePositionDD = dataDefinitionService.get(OrdersConstants.PLUGIN_IDENTIFIER, OrdersConstants.MODEL_SCHEDULE_POSITION);
List<Entity> positions = Lists.newArrayList();
for (Entity order : orders) {
Entity technology = order.getBelongsToField(OrderFields.TECHNOLOGY);
if (technology == null) {
continue;
}
final Map<Long, BigDecimal> operationRuns = Maps.newHashMap();
OperationProductComponentWithQuantityContainer operationProductComponentWithQuantityContainer = productQuantitiesService.getProductComponentQuantities(technology, order.getDecimalField(OrderFields.PLANNED_QUANTITY), operationRuns);
List<Entity> operationComponents = technology.getHasManyField(TechnologyFields.OPERATION_COMPONENTS);
for (Entity operationComponent : operationComponents) {
BigDecimal operationComponentRuns = BigDecimalUtils.convertNullToZero(operationRuns.get(operationComponent.getId()));
BigDecimal staffFactor = getStaffFactor(operationComponent);
OperationWorkTime operationWorkTime = operationWorkTimeService.estimateTechOperationWorkTime(operationComponent, operationComponentRuns, includeTpz, false, false, staffFactor);
Entity schedulePosition = createSchedulePosition(schedule, schedulePositionDD, order, operationComponent, operationWorkTime, operationProductComponentWithQuantityContainer, operationComponentRuns);
positions.add(schedulePosition);
}
}
schedule.setField(ScheduleFields.POSITIONS, positions);
schedule = schedule.getDataDefinition().save(schedule);
formComponent.setEntity(schedule);
view.addMessage("productionScheduling.info.schedulePositionsGenerated", ComponentState.MessageType.SUCCESS);
}
use of com.qcadoo.mes.operationTimeCalculations.OperationWorkTime in project mes by qcadoo.
the class ScheduleDetailsListenersPS method getWorkstationsNewFinishDate.
private boolean getWorkstationsNewFinishDate(Map<Long, Date> workstationsFinishDates, Date scheduleStartTime, Entity position, List<Entity> workstations, Map<Long, PositionNewData> operationWorkstationsPositionNewData) {
Entity schedule = position.getBelongsToField(SchedulePositionFields.SCHEDULE);
boolean allMachineWorkTimesEqualsZero = true;
for (Entity workstation : workstations) {
Integer laborWorkTime = position.getIntegerField(SchedulePositionFields.LABOR_WORK_TIME);
Integer machineWorkTime = position.getIntegerField(SchedulePositionFields.MACHINE_WORK_TIME);
Integer additionalTime = position.getIntegerField(SchedulePositionFields.ADDITIONAL_TIME);
Optional<Entity> techOperCompWorkstationTime = getTechOperCompWorkstationTime(position, workstation);
if (techOperCompWorkstationTime.isPresent()) {
Entity technologyOperationComponent = position.getBelongsToField(SchedulePositionFields.TECHNOLOGY_OPERATION_COMPONENT);
BigDecimal staffFactor = getStaffFactor(technologyOperationComponent);
OperationWorkTime operationWorkTime = operationWorkTimeService.estimateTechOperationWorkTimeForWorkstation(technologyOperationComponent, position.getDecimalField(SchedulePositionFields.OPERATION_RUNS), schedule.getBooleanField(ScheduleFields.INCLUDE_TPZ), false, techOperCompWorkstationTime.get(), staffFactor);
laborWorkTime = operationWorkTime.getLaborWorkTime();
machineWorkTime = operationWorkTime.getMachineWorkTime();
additionalTime = techOperCompWorkstationTime.get().getIntegerField(TechOperCompWorkstationTimeFields.TIME_NEXT_OPERATION);
}
if (machineWorkTime == 0) {
continue;
} else {
allMachineWorkTimesEqualsZero = false;
}
Date finishDate = getFinishDate(workstationsFinishDates, scheduleStartTime, schedule, workstation);
finishDate = getFinishDateWithChildren(position, finishDate);
DateTime finishDateTime = new DateTime(finishDate);
Entity productionLine = workstation.getBelongsToField(WorkstationFieldsPL.PRODUCTION_LINE);
Date newStartDate = shiftsService.getNearestWorkingDate(finishDateTime, productionLine).orElse(finishDateTime).toDate();
Date newFinishDate = shiftsService.findDateToForProductionLine(newStartDate, machineWorkTime, productionLine);
if (schedule.getBooleanField(ScheduleFields.ADDITIONAL_TIME_EXTENDS_OPERATION)) {
newFinishDate = Date.from(newFinishDate.toInstant().plusSeconds(additionalTime));
}
PositionNewData positionNewData = new PositionNewData(laborWorkTime, machineWorkTime, additionalTime, newStartDate, newFinishDate);
operationWorkstationsPositionNewData.put(workstation.getId(), positionNewData);
}
return allMachineWorkTimesEqualsZero;
}
use of com.qcadoo.mes.operationTimeCalculations.OperationWorkTime in project mes by qcadoo.
the class OperationsCostCalculationServiceImpl method estimateHourlyCostCalculationForSingleOperation.
private Map<String, BigDecimal> estimateHourlyCostCalculationForSingleOperation(final OperationTimes operationTimes, boolean hourlyCostFromOperation, Entity costCalculation) {
Map<String, BigDecimal> costs = Maps.newHashMap();
MathContext mathContext = numberService.getMathContext();
Entity technologyOperationComponent = operationTimes.getOperation();
OperationWorkTime operationWorkTimes = operationTimes.getTimes();
BigDecimal machineHourlyCost;
BigDecimal laborHourlyCost;
if (hourlyCostFromOperation) {
machineHourlyCost = BigDecimalUtils.convertNullToZero(technologyOperationComponent.getField(TechnologyOperationComponentFieldsCNFO.MACHINE_HOURLY_COST));
laborHourlyCost = BigDecimalUtils.convertNullToZero(technologyOperationComponent.getField(TechnologyOperationComponentFieldsCNFO.LABOR_HOURLY_COST));
} else {
machineHourlyCost = BigDecimalUtils.convertNullToZero(costCalculation.getDecimalField("averageMachineHourlyCost"));
laborHourlyCost = BigDecimalUtils.convertNullToZero(costCalculation.getDecimalField("averageLaborHourlyCost"));
}
BigDecimal durationMachine = BigDecimal.valueOf(operationWorkTimes.getMachineWorkTime());
BigDecimal durationLabor = BigDecimal.valueOf(operationWorkTimes.getLaborWorkTime());
BigDecimal durationMachineInHours = durationMachine.divide(BigDecimal.valueOf(3600), mathContext);
BigDecimal durationLaborInHours = durationLabor.divide(BigDecimal.valueOf(3600), mathContext);
BigDecimal operationMachineCost = durationMachineInHours.multiply(machineHourlyCost, mathContext);
BigDecimal operationLaborCost = durationLaborInHours.multiply(laborHourlyCost, mathContext);
BigDecimal operationCost = operationMachineCost.add(operationLaborCost, mathContext);
costs.put(CalculationOperationComponentFields.MACHINE_HOURLY_COST, numberService.setScaleWithDefaultMathContext(machineHourlyCost));
costs.put(CalculationOperationComponentFields.LABOR_HOURLY_COST, numberService.setScaleWithDefaultMathContext(laborHourlyCost));
costs.put(CalculationOperationComponentFields.TOTAL_MACHINE_OPERATION_COST, numberService.setScaleWithDefaultMathContext(operationMachineCost));
costs.put(CalculationOperationComponentFields.TOTAL_LABOR_OPERATION_COST, numberService.setScaleWithDefaultMathContext(operationLaborCost));
costs.put(CalculationOperationComponentFields.OPERATION_COST, numberService.setScaleWithDefaultMathContext(operationCost));
return costs;
}
Aggregations