001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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.inventory.common.transfer.InventoryLayerTransfer;
020import com.echothree.model.control.inventory.server.control.InventoryCostingPoolControl;
021import com.echothree.model.control.party.server.control.PartyControl;
022import com.echothree.model.data.inventory.server.entity.InventoryLayer;
023import com.echothree.model.data.user.server.entity.UserVisit;
024import com.echothree.util.server.string.AmountUtils;
025import javax.enterprise.context.RequestScoped;
026import javax.inject.Inject;
027
028@RequestScoped
029public class InventoryLayerTransferCache
030        extends BaseInventoryTransferCache<InventoryLayer, InventoryLayerTransfer> {
031
032    @Inject
033    InventoryCostingPoolControl inventoryCostingPoolControl;
034
035    @Inject
036    PartyControl partyControl;
037
038    protected InventoryLayerTransferCache() {
039        super();
040
041        setIncludeEntityInstance(true);
042    }
043
044    @Override
045    public InventoryLayerTransfer getTransfer(UserVisit userVisit, InventoryLayer inventoryLayer) {
046        var transfer = get(inventoryLayer);
047
048        if(transfer == null) {
049            var inventoryLayerDetail = inventoryLayer.getLastDetail();
050            var inventoryCostingPool = inventoryLayerDetail.getInventoryCostingPool();
051            var inventoryCostingPoolDetail = inventoryCostingPool.getLastDetail();
052            var inventoryCostingPoolTransfer = inventoryCostingPoolControl.getInventoryCostingPoolTransfer(userVisit, inventoryCostingPool);
053            var inventoryLayerSequence = inventoryLayerDetail.getInventoryLayerSequence();
054            var unformattedReceiptQuantity = inventoryLayerDetail.getReceiptQuantity();
055            var receiptQuantity = formatUnitOfMeasure(userVisit, inventoryCostingPoolDetail.getItem().getLastDetail().getUnitOfMeasureKind(),
056                    unformattedReceiptQuantity);
057            var unformattedUnitCost = inventoryLayerDetail.getUnitCost();
058            var unitCost = unformattedUnitCost == null ? null : AmountUtils.getInstance().formatCostUnit(
059                    partyControl.getPreferredCurrency(inventoryCostingPoolDetail.getCompanyParty()), unformattedUnitCost);
060            var description = inventoryLayerDetail.getDescription();
061
062            // TODO: Include the InventoryTransactionLineTransfer when transaction-line transfers are implemented.
063            transfer = new InventoryLayerTransfer(inventoryCostingPoolTransfer, inventoryLayerSequence, unformattedReceiptQuantity,
064                    receiptQuantity, unformattedUnitCost, unitCost, description);
065            put(userVisit, inventoryLayer, transfer);
066        }
067
068        return transfer;
069    }
070
071}