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.accounting.server.transfer;
018
019import com.echothree.model.control.accounting.common.transfer.ItemAccountingCategoryTransfer;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.data.accounting.server.entity.ItemAccountingCategory;
022import com.echothree.model.data.user.server.entity.UserVisit;
023import com.echothree.util.server.persistence.Session;
024import javax.enterprise.context.RequestScoped;
025
026@RequestScoped
027public class ItemAccountingCategoryTransferCache
028        extends BaseAccountingTransferCache<ItemAccountingCategory, ItemAccountingCategoryTransfer> {
029
030    AccountingControl accountingControl = Session.getModelController(AccountingControl.class);
031
032    /** Creates a new instance of ItemAccountingCategoryTransferCache */
033    protected ItemAccountingCategoryTransferCache() {
034        super();
035        
036        setIncludeEntityInstance(true);
037    }
038    
039    @Override
040    public ItemAccountingCategoryTransfer getTransfer(UserVisit userVisit, ItemAccountingCategory itemAccountingCategory) {
041        var itemAccountingCategoryTransfer = get(itemAccountingCategory);
042        
043        if(itemAccountingCategoryTransfer == null) {
044            var itemAccountingCategoryDetail = itemAccountingCategory.getLastDetail();
045            var itemAccountingCategoryName = itemAccountingCategoryDetail.getItemAccountingCategoryName();
046            var parentItemAccountingCategory = itemAccountingCategoryDetail.getParentItemAccountingCategory();
047            var parentItemAccountingCategoryTransfer = parentItemAccountingCategory == null ? null : getTransfer(userVisit, parentItemAccountingCategory);
048            var inventoryGlAccount = itemAccountingCategoryDetail.getInventoryGlAccount();
049            var inventoryGlAccountTransfer = inventoryGlAccount == null? null: accountingControl.getGlAccountTransfer(userVisit, inventoryGlAccount);
050            var salesGlAccount = itemAccountingCategoryDetail.getSalesGlAccount();
051            var salesGlAccountTransfer = salesGlAccount == null? null: accountingControl.getGlAccountTransfer(userVisit, salesGlAccount);
052            var returnsGlAccount = itemAccountingCategoryDetail.getReturnsGlAccount();
053            var returnsGlAccountTransfer = returnsGlAccount == null? null: accountingControl.getGlAccountTransfer(userVisit, returnsGlAccount);
054            var cogsGlAccount = itemAccountingCategoryDetail.getCogsGlAccount();
055            var cogsGlAccountTransfer = cogsGlAccount == null? null: accountingControl.getGlAccountTransfer(userVisit, cogsGlAccount);
056            var returnsCogsGlAccount = itemAccountingCategoryDetail.getReturnsCogsGlAccount();
057            var returnsCogsGlAccountTransfer = returnsCogsGlAccount == null? null: accountingControl.getGlAccountTransfer(userVisit, returnsCogsGlAccount);
058            var isDefault = itemAccountingCategoryDetail.getIsDefault();
059            var sortOrder = itemAccountingCategoryDetail.getSortOrder();
060            var description = accountingControl.getBestItemAccountingCategoryDescription(itemAccountingCategory, getLanguage(userVisit));
061            
062            itemAccountingCategoryTransfer = new ItemAccountingCategoryTransfer(itemAccountingCategoryName,
063                    parentItemAccountingCategoryTransfer, inventoryGlAccountTransfer, salesGlAccountTransfer,
064                    returnsGlAccountTransfer, cogsGlAccountTransfer, returnsCogsGlAccountTransfer, isDefault, sortOrder,
065                    description);
066            put(userVisit, itemAccountingCategory, itemAccountingCategoryTransfer);
067        }
068        
069        return itemAccountingCategoryTransfer;
070    }
071    
072}