001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.payment.server.transfer;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.payment.common.PaymentOptions;
021import com.echothree.model.control.payment.common.transfer.BillingAccountRoleTransfer;
022import com.echothree.model.control.payment.common.transfer.BillingAccountTransfer;
023import com.echothree.model.control.payment.server.control.BillingControl;
024import com.echothree.model.data.payment.server.entity.BillingAccount;
025import com.echothree.model.data.user.server.entity.UserVisit;
026import com.echothree.util.common.transfer.MapWrapper;
027import com.echothree.util.server.persistence.Session;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class BillingAccountTransferCache
032        extends BasePaymentTransferCache<BillingAccount, BillingAccountTransfer> {
033
034    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
035    BillingControl billingControl = Session.getModelController(BillingControl.class);
036    boolean includeRoles;
037
038    /** Creates a new instance of BillingAccountTransferCache */
039    protected BillingAccountTransferCache() {
040        super();
041
042        var options = session.getOptions();
043        if(options != null) {
044            includeRoles = options.contains(PaymentOptions.BillingAccountIncludeRoles);
045        }
046        
047        setIncludeEntityInstance(true);
048    }
049
050    @Override
051    public BillingAccountTransfer getTransfer(UserVisit userVisit, BillingAccount billingAccount) {
052        var billingAccountTransfer = get(billingAccount);
053
054        if(billingAccountTransfer == null) {
055            var billingAccountDetail = billingAccount.getLastDetail();
056            var billingAccountName = billingAccountDetail.getBillingAccountName();
057            var currency = billingAccountDetail.getCurrency();
058            var currencyTransfer = accountingControl.getCurrencyTransfer(userVisit, currency);
059            var reference = billingAccountDetail.getReference();
060            var description = billingAccountDetail.getDescription();
061            String creditLimit = null;
062            String potentialCreditLimit = null;
063//            BillingAccountStatus billingAccountStatus = paymentControl.getBillingAccountStatus(billingAccount);
064//            Integer rawCreditLimit = billingAccountStatus.getCreditLimit();
065//            Integer rawPotentialCreditLimit = billingAccountStatus.getPotentialCreditLimit();
066//
067//            String partyTypeName = fromParty.getLastDetail().getPartyType().getPartyTypeName();
068//            if(PartyTypes.CUSTOMER.name().equals(partyTypeName)) {
069//                creditLimit = rawCreditLimit == null? null: AmountUtils.getInstance().formatPriceLine(currency, rawCreditLimit);
070//                potentialCreditLimit = rawPotentialCreditLimit == null? null: AmountUtils.getInstance().formatPriceLine(currency, rawPotentialCreditLimit);
071//            } else if(PartyTypes.COMPANY.name().equals(partyTypeName)) {
072//                creditLimit = rawCreditLimit == null? null: AmountUtils.getInstance().formatCostLine(currency, rawCreditLimit);
073//                potentialCreditLimit = rawPotentialCreditLimit == null? null: AmountUtils.getInstance().formatCostLine(currency, rawPotentialCreditLimit);
074//            }
075            
076            billingAccountTransfer = new BillingAccountTransfer(billingAccountName, currencyTransfer, reference, description, creditLimit, potentialCreditLimit);
077            put(userVisit, billingAccount, billingAccountTransfer);
078            
079            if(includeRoles) {
080                var billingAccountRoleTransfers = billingControl.getBillingAccountRoleTransfersByBillingAccount(userVisit, billingAccount);
081                var billingAccountRoles = new MapWrapper<BillingAccountRoleTransfer>(billingAccountRoleTransfers.size());
082
083                billingAccountRoleTransfers.forEach((billingAccountRoleTransfer) -> {
084                    billingAccountRoles.put(billingAccountRoleTransfer.getBillingAccountRoleType().getBillingAccountRoleTypeName(), billingAccountRoleTransfer);
085                });
086
087                billingAccountTransfer.setBillingAccountRoles(billingAccountRoles);
088            }
089        }
090        
091        return billingAccountTransfer;
092    }
093    
094}