|
| 1 | +/* |
| 2 | + * This program is part of the OpenLMIS logistics management information system platform software. |
| 3 | + * Copyright © 2017 VillageReach |
| 4 | + * |
| 5 | + * This program is free software: you can redistribute it and/or modify it under the terms |
| 6 | + * of the GNU Affero General Public License as published by the Free Software Foundation, either |
| 7 | + * version 3 of the License, or (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 10 | + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + * See the GNU Affero General Public License for more details. You should have received a copy of |
| 12 | + * the GNU Affero General Public License along with this program. If not, see |
| 13 | + * http://www.gnu.org/licenses. For additional information contact info@OpenLMIS.org. |
| 14 | + */ |
| 15 | + |
| 16 | +package org.openlmis.stockmanagement.service; |
| 17 | + |
| 18 | +import static org.apache.commons.collections.CollectionUtils.isNotEmpty; |
| 19 | +import static org.openlmis.stockmanagement.i18n.MessageKeys.ERRRO_EVENT_SOH_EXCEEDS_LIMIT; |
| 20 | + |
| 21 | +import org.openlmis.stockmanagement.domain.card.StockCard; |
| 22 | +import org.openlmis.stockmanagement.domain.card.StockCardLineItem; |
| 23 | +import org.openlmis.stockmanagement.domain.reason.StockCardLineItemReason; |
| 24 | +import org.openlmis.stockmanagement.exception.ValidationMessageException; |
| 25 | +import org.openlmis.stockmanagement.util.Message; |
| 26 | +import org.slf4j.Logger; |
| 27 | +import org.slf4j.LoggerFactory; |
| 28 | +import org.springframework.stereotype.Service; |
| 29 | + |
| 30 | +/** |
| 31 | + * The type Stock card line item service. |
| 32 | + */ |
| 33 | +@Service |
| 34 | +public class StockCardLineItemService { |
| 35 | + |
| 36 | + private static final Logger LOGGER = LoggerFactory |
| 37 | + .getLogger(StockCardLineItemService.class); |
| 38 | + |
| 39 | + /** |
| 40 | + * Populate stock on hand line items. |
| 41 | + * |
| 42 | + * @param stockCard the stockCard |
| 43 | + */ |
| 44 | + public void populateStockOnHandLineItems(StockCard stockCard) { |
| 45 | + int previousSoH = 0; |
| 46 | + |
| 47 | + if (isNotEmpty(stockCard.getLineItems())) { |
| 48 | + stockCard.reorderLineItems(); |
| 49 | + for (StockCardLineItem lineItem : stockCard.getLineItems()) { |
| 50 | + previousSoH = populateStockOnHandLineItems(lineItem, previousSoH); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private Integer populateStockOnHandLineItems(StockCardLineItem stockCardLineItem, |
| 56 | + int previousSoH) { |
| 57 | + if (stockCardLineItem.isPhysicalInventory()) { |
| 58 | + stockCardLineItem.setReason( |
| 59 | + determineStockCardLineItemReasonByQuantity(stockCardLineItem, previousSoH)); |
| 60 | + stockCardLineItem.setStockOnHand(stockCardLineItem.getQuantity()); |
| 61 | + stockCardLineItem.setQuantity(Math.abs(stockCardLineItem.getStockOnHand() - previousSoH)); |
| 62 | + LOGGER.debug("Physical inventory: {}", stockCardLineItem.getStockOnHand()); |
| 63 | + return stockCardLineItem.getStockOnHand(); |
| 64 | + } else if (stockCardLineItem.isPositive()) { |
| 65 | + return tryIncrease(stockCardLineItem, previousSoH); |
| 66 | + } else { |
| 67 | + return tryDecrease(stockCardLineItem, previousSoH); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private Integer tryDecrease(StockCardLineItem stockCardLineItem, int previousSoH) { |
| 72 | + try { |
| 73 | + int difference = Math.subtractExact(previousSoH, stockCardLineItem.getQuantity()); |
| 74 | + LOGGER.debug("try decrease soh: {} - {} = {}", previousSoH, stockCardLineItem.getQuantity(), |
| 75 | + difference); |
| 76 | + stockCardLineItem.setStockOnHand(difference); |
| 77 | + } catch (ArithmeticException ex) { |
| 78 | + throw new ValidationMessageException( |
| 79 | + new Message(ERRRO_EVENT_SOH_EXCEEDS_LIMIT, previousSoH, stockCardLineItem.getQuantity(), |
| 80 | + ex)); |
| 81 | + } |
| 82 | + |
| 83 | + return stockCardLineItem.getStockOnHand(); |
| 84 | + } |
| 85 | + |
| 86 | + private Integer tryIncrease(StockCardLineItem stockCardLineItem, int previousSoH) { |
| 87 | + try { |
| 88 | + int sum = Math.addExact(previousSoH, stockCardLineItem.getQuantity()); |
| 89 | + LOGGER.debug("try increase soh: {} + {} = {}", previousSoH, stockCardLineItem.getQuantity(), |
| 90 | + sum); |
| 91 | + stockCardLineItem.setStockOnHand(sum); |
| 92 | + } catch (ArithmeticException ex) { |
| 93 | + throw new ValidationMessageException( |
| 94 | + new Message(ERRRO_EVENT_SOH_EXCEEDS_LIMIT, previousSoH, stockCardLineItem.getQuantity(), |
| 95 | + ex)); |
| 96 | + } |
| 97 | + return stockCardLineItem.getStockOnHand(); |
| 98 | + } |
| 99 | + |
| 100 | + private StockCardLineItemReason determineStockCardLineItemReasonByQuantity( |
| 101 | + StockCardLineItem stockCardLineItem, |
| 102 | + int previousSoH) { |
| 103 | + if (stockCardLineItem.getQuantity() > previousSoH) { |
| 104 | + return StockCardLineItemReason.physicalCredit(); |
| 105 | + } else if (stockCardLineItem.getQuantity() < previousSoH) { |
| 106 | + return StockCardLineItemReason.physicalDebit(); |
| 107 | + } else { |
| 108 | + return StockCardLineItemReason.physicalBalance(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments