Skip to content

Commit

Permalink
Merge pull request #26 from makombe/update-prices
Browse files Browse the repository at this point in the history
Add ability to update item price
  • Loading branch information
ojwanganto authored Aug 16, 2024
2 parents 7b8d2e2 + 32489aa commit 8824fb5
Showing 1 changed file with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,41 @@ public BillableService billableServiceMapper(BillableServiceMapper mapper) {
service.setServicePrices(servicePrices);
return service;
}
private void syncCollection(List<CashierItemPrice> existingPrices, List<CashierItemPrice> newPrices) {
public BillableService billableServiceUpdateMapper(BillableServiceMapper mapper) {
BillableService service = Context.getService(IBillableItemsService.class).getByUuid(mapper.getUuid());
service.setName(mapper.getName());
service.setShortName(mapper.getShortName());
service.setConcept(Context.getConceptService().getConceptByUuid(mapper.getConcept()));
service.setServiceType(Context.getConceptService().getConceptByUuid(mapper.getServiceType()));
service.setServiceCategory(Context.getConceptService().getConceptByUuid(mapper.getServiceCategory()));
service.setServiceStatus(mapper.getServiceStatus());

// Pass the new prices from the mapper to syncCollection
List<CashierItemPrice> updatedPrices = mapper.getServicePrices().stream().map(itemPrice -> {
CashierItemPrice price = new CashierItemPrice();
price.setName(itemPrice.getName());
price.setPrice(itemPrice.getPrice());
price.setPaymentMode(Context.getService(IPaymentModeService.class).getByUuid(itemPrice.getPaymentMode()));
price.setBillableService(service);
return price;
}).collect(Collectors.toList());

service.setServicePrices(syncCollection(service.getServicePrices(), updatedPrices));
return service;
}

private List<CashierItemPrice> syncCollection(List<CashierItemPrice> existingPrices, List<CashierItemPrice> newPrices) {
Map<String, CashierItemPrice> newPricesMap = newPrices.stream()
.collect(Collectors.toMap(
price -> price.getUuid(),
price -> price
price -> price.getPaymentMode().getUuid(),
price -> price,
(existing, replacement) -> replacement
));

Iterator<CashierItemPrice> iterator = existingPrices.iterator();
while (iterator.hasNext()) {
CashierItemPrice existingPrice = iterator.next();
CashierItemPrice newPrice = newPricesMap.remove(existingPrice.getUuid());
CashierItemPrice newPrice = newPricesMap.remove(existingPrice.getPaymentMode().getUuid());

if (newPrice == null) {
iterator.remove();
Expand All @@ -127,21 +151,10 @@ private void syncCollection(List<CashierItemPrice> existingPrices, List<CashierI
existingPrice.setBillableService(newPrice.getBillableService());
}
}

// Add all new prices that weren't matched with existing ones
existingPrices.addAll(newPricesMap.values());
System.out.printf("Synchronized Collection: %s%n", existingPrices);
return existingPrices;
}

public BillableService billableServiceUpdateMapper(BillableServiceMapper mapper) {

BillableService service = Context.getService(IBillableItemsService.class).getByUuid(mapper.getUuid());
service.setName(mapper.getName());
service.setShortName(mapper.getShortName());
service.setConcept(Context.getConceptService().getConceptByUuid(mapper.getConcept()));
service.setServiceType(Context.getConceptService().getConceptByUuid(mapper.getServiceType()));
service.setServiceCategory(Context.getConceptService().getConceptByUuid(mapper.getServiceCategory()));
service.setServiceStatus(mapper.getServiceStatus());
syncCollection(service.getServicePrices(), service.getServicePrices());
return service;
}

}

0 comments on commit 8824fb5

Please sign in to comment.