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