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.inventory.server.logic;
018
019import com.echothree.model.control.inventory.common.exception.DuplicatePartyInventoryCostingMethodException;
020import com.echothree.model.control.inventory.common.exception.UnknownPartyInventoryCostingMethodException;
021import com.echothree.model.control.inventory.server.control.InventoryCostingMethodControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.party.server.logic.PartyLogic;
024import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod;
025import com.echothree.model.data.inventory.server.entity.PartyInventoryCostingMethod;
026import com.echothree.model.data.inventory.server.value.PartyInventoryCostingMethodValue;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.persistence.BasePK;
030import com.echothree.util.server.control.BaseLogic;
031import com.echothree.util.server.message.ExecutionErrorAccumulator;
032import com.echothree.util.server.persistence.EntityPermission;
033import javax.enterprise.context.ApplicationScoped;
034import javax.enterprise.inject.spi.CDI;
035import javax.inject.Inject;
036
037@ApplicationScoped
038public class PartyInventoryCostingMethodLogic
039        extends BaseLogic {
040
041    @Inject
042    InventoryCostingMethodControl inventoryCostingMethodControl;
043
044    @Inject
045    InventoryCostingMethodLogic inventoryCostingMethodLogic;
046
047    @Inject
048    PartyLogic partyLogic;
049
050    protected PartyInventoryCostingMethodLogic() {
051        super();
052    }
053
054    public PartyInventoryCostingMethod createPartyInventoryCostingMethod(final ExecutionErrorAccumulator eea,
055            final Party party, final InventoryCostingMethod inventoryCostingMethod, final BasePK createdBy) {
056        partyLogic.checkPartyType(eea, party, PartyTypes.COMPANY.name());
057
058        var partyInventoryCostingMethod = eea == null || !eea.hasExecutionErrors()
059                ? inventoryCostingMethodControl.getPartyInventoryCostingMethod(party) : null;
060
061        if(partyInventoryCostingMethod == null) {
062            if(eea == null || !eea.hasExecutionErrors()) {
063                partyInventoryCostingMethod = inventoryCostingMethodControl.createPartyInventoryCostingMethod(party,
064                        inventoryCostingMethod, createdBy);
065            }
066        } else {
067            handleExecutionError(DuplicatePartyInventoryCostingMethodException.class, eea,
068                    ExecutionErrors.DuplicatePartyInventoryCostingMethod.name(), party.getLastDetail().getPartyName());
069        }
070
071        return partyInventoryCostingMethod;
072    }
073
074    public PartyInventoryCostingMethod getPartyInventoryCostingMethod(final ExecutionErrorAccumulator eea,
075            final Party party, final boolean allowDefault, final BasePK createdBy,
076            final EntityPermission entityPermission) {
077        var partyInventoryCostingMethod = inventoryCostingMethodControl.getPartyInventoryCostingMethod(party,
078                entityPermission);
079
080        if(partyInventoryCostingMethod == null) {
081            if(allowDefault) {
082                partyLogic.checkPartyType(eea, party, PartyTypes.COMPANY.name());
083
084                if(eea == null || !eea.hasExecutionErrors()) {
085                    var inventoryCostingMethod = inventoryCostingMethodLogic.getDefaultInventoryCostingMethod(eea);
086
087                    if(inventoryCostingMethod != null) {
088                        partyInventoryCostingMethod = createPartyInventoryCostingMethod(eea, party,
089                                inventoryCostingMethod, createdBy);
090                    }
091                }
092            } else {
093                handleExecutionError(UnknownPartyInventoryCostingMethodException.class, eea,
094                        ExecutionErrors.UnknownPartyInventoryCostingMethod.name(), party.getLastDetail().getPartyName());
095            }
096        }
097
098        return partyInventoryCostingMethod;
099    }
100
101    public PartyInventoryCostingMethod getPartyInventoryCostingMethod(final ExecutionErrorAccumulator eea,
102            final Party party, final boolean allowDefault, final BasePK createdBy) {
103        return getPartyInventoryCostingMethod(eea, party, allowDefault, createdBy, EntityPermission.READ_ONLY);
104    }
105
106    public PartyInventoryCostingMethod getPartyInventoryCostingMethodForUpdate(final ExecutionErrorAccumulator eea,
107            final Party party, final boolean allowDefault, final BasePK createdBy) {
108        return getPartyInventoryCostingMethod(eea, party, allowDefault, createdBy, EntityPermission.READ_WRITE);
109    }
110
111    public void updatePartyInventoryCostingMethodFromValue(
112            final PartyInventoryCostingMethodValue partyInventoryCostingMethodValue, final BasePK updatedBy) {
113        inventoryCostingMethodControl.updatePartyInventoryCostingMethodFromValue(partyInventoryCostingMethodValue, updatedBy);
114    }
115
116    public void deletePartyInventoryCostingMethod(final PartyInventoryCostingMethod partyInventoryCostingMethod,
117            final BasePK deletedBy) {
118        inventoryCostingMethodControl.deletePartyInventoryCostingMethod(partyInventoryCostingMethod, deletedBy);
119    }
120}