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.invoice.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.invoice.common.InvoiceLineUseTypes;
021import com.echothree.model.control.invoice.common.transfer.InvoiceLineTransfer;
022import com.echothree.model.control.invoice.server.control.InvoiceControl;
023import com.echothree.model.data.invoice.server.entity.InvoiceLine;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.server.string.AmountUtils;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class InvoiceLineTransferCache
030        extends BaseInvoiceTransferCache<InvoiceLine, InvoiceLineTransfer> {
031
032    @Inject
033    InvoiceControl invoiceControl;
034
035    /** Creates a new instance of InvoiceLineTransferCache */
036    protected InvoiceLineTransferCache() {
037        super();
038        
039        setIncludeEntityInstance(true);
040    }
041    
042    public InvoiceLineTransfer getInvoiceLineTransfer(UserVisit userVisit, InvoiceLine invoiceLine) {
043        var invoiceLineTransfer = get(invoiceLine);
044        
045        if(invoiceLineTransfer == null) {
046            var invoiceLineDetail = invoiceLine.getLastDetail();
047            var invoice = invoiceLineDetail.getInvoice();
048            var invoiceTransfer = invoiceControl.getInvoiceTransfer(userVisit, invoice);
049            var invoiceLineSequence = invoiceLineDetail.getInvoiceLineSequence();
050            var parentInvoiceLine = invoiceLineDetail.getParentInvoiceLine();
051            var parentInvoiceLineTransfer = parentInvoiceLine == null ? null : getInvoiceLineTransfer(userVisit, parentInvoiceLine);
052            var invoiceLineTypeTransfer = invoiceControl.getInvoiceLineTypeTransfer(userVisit, invoiceLineDetail.getInvoiceLineType());
053            var invoiceLineUseTypeTransfer = invoiceControl.getInvoiceLineUseTypeTransfer(userVisit, invoiceLineDetail.getInvoiceLineUseType());
054            var description = invoiceLineDetail.getDescription();
055
056            var currency = invoice.getLastDetail().getBillingAccount().getLastDetail().getCurrency();
057            var unformattedAmount = invoiceLineDetail.getAmount();
058            var amount = AmountUtils.getInstance().formatCostUnit(currency, unformattedAmount);
059            
060            invoiceLineTransfer = new InvoiceLineTransfer(invoiceTransfer, invoiceLineSequence, parentInvoiceLineTransfer, invoiceLineTypeTransfer, invoiceLineUseTypeTransfer, amount,
061                    unformattedAmount, description);
062            put(userVisit, invoiceLine, invoiceLineTransfer);
063
064            var invoiceLineUseTypeName = invoiceLineUseTypeTransfer.getInvoiceLineUseTypeName();
065            if(invoiceLineUseTypeName.equals(InvoiceLineUseTypes.ITEM.name())) {
066                invoiceLineTransfer.setInvoiceLineItem(invoiceControl.getInvoiceLineItemTransfer(userVisit, invoiceControl.getInvoiceLineItem(invoiceLine)));
067            } else if(invoiceLineUseTypeName.equals(InvoiceLineUseTypes.GL_ACCOUNT.name())) {
068                invoiceLineTransfer.setInvoiceLineGlAccount(invoiceControl.getInvoiceLineGlAccountTransfer(userVisit, invoiceControl.getInvoiceLineGlAccount(invoiceLine)));
069            }
070        }
071        
072        return invoiceLineTransfer;
073    }
074    
075}