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