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.accounting.server.transfer;
018
019import javax.inject.Inject;
020import com.echothree.model.control.accounting.common.AccountingProperties;
021import com.echothree.model.control.accounting.common.transfer.CurrencyTransfer;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.data.accounting.server.entity.Currency;
024import com.echothree.model.data.user.server.entity.UserVisit;
025import com.echothree.util.common.form.TransferProperties;
026import javax.enterprise.context.RequestScoped;
027
028@RequestScoped
029public class CurrencyTransferCache
030        extends BaseAccountingTransferCache<Currency, CurrencyTransfer> {
031
032    @Inject
033    AccountingControl accountingControl;
034
035    TransferProperties transferProperties;
036    boolean filterCurrencyIsoName;
037    boolean filterSymbol;
038    boolean filterSymbolPosition;
039    boolean filterSymbolOnListStart;
040    boolean filterSymbolOnListMember;
041    boolean filterSymbolOnSubtotal;
042    boolean filterSymbolOnTotal;
043    boolean filterGroupingSeparator;
044    boolean filterGroupingSize;
045    boolean filterFractionSeparator;
046    boolean filterDefaultFractionDigits;
047    boolean filterPriceUnitFractionDigits;
048    boolean filterPriceLineFractionDigits;
049    boolean filterCostUnitFractionDigits;
050    boolean filterCostLineFractionDigits;
051    boolean filterMinusSign;
052    boolean filterisDefault;
053    boolean filterSortOrder;
054    boolean filterDescription;
055    
056    /** Creates a new instance of CurrencyTransferCache */
057    protected CurrencyTransferCache() {
058        super();
059
060        transferProperties = session.getTransferProperties();
061        if(transferProperties != null) {
062            var properties = transferProperties.getProperties(CurrencyTransfer.class);
063            
064            if(properties != null) {
065                filterCurrencyIsoName = !properties.contains(AccountingProperties.CURRENCY_ISO_NAME);
066                filterSymbol = !properties.contains(AccountingProperties.SYMBOL);
067                filterSymbolPosition = !properties.contains(AccountingProperties.SYMBOL_POSITION);
068                filterSymbolOnListStart = !properties.contains(AccountingProperties.SYMBOL_ON_LIST_START);
069                filterSymbolOnListMember = !properties.contains(AccountingProperties.SYMBOL_ON_LIST_MEMBER);
070                filterSymbolOnSubtotal = !properties.contains(AccountingProperties.SYMBOL_ON_SUBTOTAL);
071                filterSymbolOnTotal = !properties.contains(AccountingProperties.SYMBOL_ON_TOTAL);
072                filterGroupingSeparator = !properties.contains(AccountingProperties.GROUPING_SEPARATOR);
073                filterGroupingSize = !properties.contains(AccountingProperties.GROUPING_SIZE);
074                filterFractionSeparator = !properties.contains(AccountingProperties.FRACTION_SEPARATOR);
075                filterDefaultFractionDigits = !properties.contains(AccountingProperties.DEFAULT_FRACTION_DIGITS);
076                filterPriceUnitFractionDigits = !properties.contains(AccountingProperties.PRICE_UNIT_FRACTION_DIGITS);
077                filterPriceLineFractionDigits = !properties.contains(AccountingProperties.PRICE_LINE_FRACTION_DIGITS);
078                filterCostUnitFractionDigits = !properties.contains(AccountingProperties.COST_UNIT_FRACTION_DIGITS);
079                filterCostLineFractionDigits = !properties.contains(AccountingProperties.COST_LINE_FRACTION_DIGITS);
080                filterMinusSign = !properties.contains(AccountingProperties.MINUS_SIGN);
081                filterisDefault = !properties.contains(AccountingProperties.IS_DEFAULT);
082                filterSortOrder = !properties.contains(AccountingProperties.SORT_ORDER);
083                filterDescription = !properties.contains(AccountingProperties.DESCRIPTION);
084            }
085        }
086    }
087    
088    @Override
089    public CurrencyTransfer getTransfer(UserVisit userVisit, Currency currency) {
090        var currencyTransfer = get(currency);
091        
092        if(currencyTransfer == null) {
093            var currencyIsoName = filterCurrencyIsoName ? null : currency.getCurrencyIsoName();
094            var symbol = filterSymbol ? null : currency.getSymbol();
095            var symbolPosition = filterSymbolPosition ? null : accountingControl.getSymbolPositionTransfer(userVisit, currency.getSymbolPosition());
096            var symbolOnListStart = filterSymbolOnListStart ? null : currency.getSymbolOnListStart();
097            var symbolOnListMember = filterSymbolOnListMember ? null : currency.getSymbolOnListMember();
098            var symbolOnSubtotal = filterSymbolOnSubtotal ? null : currency.getSymbolOnSubtotal();
099            var symbolOnTotal = filterSymbolOnTotal ? null :  currency.getSymbolOnTotal();
100            var groupingSeparator = filterGroupingSeparator ? null : currency.getGroupingSeparator();
101            var groupingSize = filterGroupingSize ? null : currency.getGroupingSize();
102            var fractionSeparator = filterFractionSeparator ? null : currency.getFractionSeparator();
103            var defaultFractionDigits = filterDefaultFractionDigits ? null : currency.getDefaultFractionDigits();
104            var priceUnitFractionDigits = filterPriceUnitFractionDigits ? null : currency.getPriceUnitFractionDigits();
105            var priceLineFractionDigits = filterPriceLineFractionDigits ? null : currency.getPriceLineFractionDigits();
106            var costUnitFractionDigits = filterCostUnitFractionDigits ? null : currency.getCostUnitFractionDigits();
107            var costLineFractionDigits = filterCostLineFractionDigits ? null : currency.getCostLineFractionDigits();
108            var minusSign = filterMinusSign ? null : currency.getMinusSign();
109            var isDefault = filterisDefault ? null : currency.getIsDefault();
110            var sortOrder = filterSortOrder ? null : currency.getSortOrder();
111            var description = filterDescription ? null : accountingControl.getBestCurrencyDescription(currency, getLanguage(userVisit));
112            
113            currencyTransfer = new CurrencyTransfer(currencyIsoName, symbol, symbolPosition, symbolOnListStart, symbolOnListMember,
114                    symbolOnSubtotal, symbolOnTotal, groupingSeparator, groupingSize, fractionSeparator, defaultFractionDigits,
115                    priceUnitFractionDigits, priceLineFractionDigits, costUnitFractionDigits, costLineFractionDigits, minusSign,
116                    isDefault, sortOrder, description);
117            put(userVisit, currency, currencyTransfer);
118        }
119        return currencyTransfer;
120    }
121    
122}