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