001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.inventory.server.transfer;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.inventory.common.InventoryOptions;
021import com.echothree.model.control.inventory.common.transfer.LotAliasTransfer;
022import com.echothree.model.control.inventory.common.transfer.LotTimeTransfer;
023import com.echothree.model.control.inventory.common.transfer.LotTransfer;
024import com.echothree.model.control.inventory.server.control.InventoryControl;
025import com.echothree.model.control.inventory.server.control.LotAliasControl;
026import com.echothree.model.control.inventory.server.control.LotTimeControl;
027import com.echothree.model.control.item.server.control.ItemControl;
028import com.echothree.model.control.party.server.control.PartyControl;
029import com.echothree.model.control.uom.server.control.UomControl;
030import com.echothree.model.data.inventory.server.entity.Lot;
031import com.echothree.model.data.user.server.entity.UserVisit;
032import com.echothree.util.common.transfer.MapWrapper;
033import com.echothree.util.server.persistence.Session;
034import com.echothree.util.server.string.AmountUtils;
035import java.util.Set;
036
037public class LotTransferCache
038        extends BaseInventoryTransferCache<Lot, LotTransfer> {
039
040    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
041    ItemControl itemControl = Session.getModelController(ItemControl.class);
042    PartyControl partyControl = Session.getModelController(PartyControl.class);
043    UomControl uomControl = Session.getModelController(UomControl.class);
044    LotAliasControl lotAliasControl = Session.getModelController(LotAliasControl.class);
045    LotTimeControl lotTimeControl = Session.getModelController(LotTimeControl.class);
046
047    boolean includeLotAliases;
048    boolean includeLotTimes;
049
050    /** Creates a new instance of LotTransferCache */
051    public LotTransferCache(UserVisit userVisit, InventoryControl inventoryControl) {
052        super(userVisit, inventoryControl);
053
054        var options = session.getOptions();
055        if(options != null) {
056            setIncludeKey(options.contains(InventoryOptions.LotIncludeKey));
057            setIncludeGuid(options.contains(InventoryOptions.LotIncludeGuid));
058            includeLotAliases = options.contains(InventoryOptions.LotIncludeLotAliases);
059            includeLotTimes = options.contains(InventoryOptions.LotIncludeLotTimes);
060        }
061
062        setIncludeEntityInstance(true);
063    }
064    
065    @Override
066    public LotTransfer getTransfer(Lot lot) {
067        var lotTransfer = get(lot);
068        
069        if(lotTransfer == null) {
070            var lotDetail = lot.getLastDetail();
071
072            var lotName = lotDetail.getLotName();
073            var ownerParty = partyControl.getPartyTransfer(userVisit, lotDetail.getOwnerParty());
074            var item = itemControl.getItemTransfer(userVisit, lotDetail.getItem());
075            var inventoryCondition = inventoryControl.getInventoryConditionTransfer(userVisit, lotDetail.getInventoryCondition());
076            var unitOfMeasureType = uomControl.getUnitOfMeasureTypeTransfer(userVisit, lotDetail.getUnitOfMeasureType());
077            var quantity = lotDetail.getQuantity();
078            var currency = lotDetail.getCurrency();
079            var currencyTransfer = currency == null ? null : accountingControl.getCurrencyTransfer(userVisit, currency);
080            var unformattedUnitCost = lotDetail.getUnitCost();
081            var unitCost = currency == null || unformattedUnitCost == null ? null : AmountUtils.getInstance().formatCostUnit(currency, unformattedUnitCost);
082
083            lotTransfer = new LotTransfer(lotName, ownerParty, item, inventoryCondition, unitOfMeasureType, quantity,
084                    currencyTransfer, unformattedUnitCost, unitCost);
085            put(lot, lotTransfer);
086
087            if(includeLotAliases) {
088                var lotAliasTransfers = lotAliasControl.getLotAliasTransfersByLot(userVisit, lot);
089                var lotAliases = new MapWrapper<LotAliasTransfer>(lotAliasTransfers.size());
090
091                lotAliasTransfers.forEach((lotAliasTransfer) -> {
092                    lotAliases.put(lotAliasTransfer.getLotAliasType().getLotAliasTypeName(), lotAliasTransfer);
093                });
094
095                lotTransfer.setLotAliases(lotAliases);
096            }
097
098            if(includeLotTimes) {
099                var lotTimeTransfers = lotTimeControl.getLotTimeTransfersByLot(userVisit, lot);
100                var lotTimes = new MapWrapper<LotTimeTransfer>(lotTimeTransfers.size());
101
102                lotTimeTransfers.forEach((lotTimeTransfer) -> {
103                    lotTimes.put(lotTimeTransfer.getLotTimeType().getLotTimeTypeName(), lotTimeTransfer);
104                });
105
106                lotTransfer.setLotTimes(lotTimes);
107            }
108        }
109        
110        return lotTransfer;
111    }
112    
113}