MCPcopy Create free account
hub / github.com/Grashjs/cmms / consumePart

Method consumePart

api/src/main/java/com/grash/service/PartService.java:137–194  ·  view source on GitHub ↗
(Long id, double quantity, WorkOrder workOrder, Locale locale, boolean deleteConsumption)

Source from the content-addressed store, hash-verified

135 }
136
137 @Transactional
138 public void consumePart(Long id, double quantity, WorkOrder workOrder, Locale locale, boolean deleteConsumption) {
139 Part part = findById(id).get();
140 if (part.isNonStock()) return;
141 double previousQuantity = part.getQuantity();
142 part.setQuantity(part.getQuantity() - quantity);
143 if (part.getQuantity() < 0)
144 throw new CustomException("There is not enough of this part", HttpStatus.NOT_ACCEPTABLE);
145 if (quantity <= 0) {
146 PartConsumption partConsumption =
147 Collections.max(partConsumptionService.findByWorkOrderAndPart(workOrder.getId(), part.getId()),
148 new AuditComparator());
149 if (deleteConsumption || quantity == 0d) {
150 partConsumptionService.delete(partConsumption.getId());
151 } else {
152 partConsumption.setQuantity(partConsumption.getQuantity() + quantity);
153 partRepository.save(part);
154 partConsumptionService.save(partConsumption);
155 }
156 } else {
157 String message = messageSource.getMessage("notification_part_low", new Object[]{part.getName()}, locale);
158 if (part.getQuantity() < part.getMinQuantity() && licenseService.hasEntitlement(LicenseEntitlement.LOW_STOCK_ALERTS)) {
159 Map<Long, User> uniqueUsersMap = new TreeMap<>();
160 Stream.concat(
161 userService.findWorkersByCompany(part.getCompany().getId())
162 .stream()
163 .filter(user -> user.getRole().getViewPermissions().contains(PermissionEntity.SETTINGS)),
164 part.getAssignedTo().stream()
165 ).forEach(user -> {
166 if (user.isEnabled()) uniqueUsersMap.put(user.getId(), user);
167 });
168
169 notificationService.createMultiple(uniqueUsersMap.values().stream().map(user ->
170 new Notification(message, user, NotificationType.PART, part.getId())
171 ).collect(Collectors.toList()), true, message);
172
173 Map<String, Object> mailVariables = new HashMap<String, Object>() {{
174 put("partName", part.getName());
175 put("partLink", frontendUrl + "/app/inventory/parts/" + part.getId());
176 }};
177 if (!uniqueUsersMap.isEmpty()) {
178 mailServiceFactory.getMailService().sendMessageUsingThymeleafTemplate(
179 uniqueUsersMap.values().stream().map(User::getEmail).toArray(String[]::new),
180 messageSource.getMessage("low_stock", null, locale),
181 mailVariables,
182 "low-stock.html",
183 locale,
184 null
185 );
186 }
187 }
188 partConsumptionService.create(new PartConsumption(part, workOrder, quantity));
189 partRepository.save(part);
190
191 }
192 dispatchPartQuantityChangeWebhook(part, previousQuantity, part.getQuantity(), workOrder,
193 workOrder.getCompany());
194 }

Callers 3

patchWorkOrderMethod · 0.80
patchMethod · 0.80
deleteMethod · 0.80

Calls 15

findByIdMethod · 0.95
setQuantityMethod · 0.95
getMessageMethod · 0.80
getNameMethod · 0.80
hasEntitlementMethod · 0.80
isEnabledMethod · 0.80
createMultipleMethod · 0.80
isEmptyMethod · 0.80
getMailServiceMethod · 0.80
findWorkersByCompanyMethod · 0.65

Tested by

no test coverage detected