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.logic;
018
019import com.echothree.control.user.accounting.common.spec.GlAccountCategoryUniversalSpec;
020import com.echothree.model.control.accounting.common.exception.DuplicateGlAccountCategoryNameException;
021import com.echothree.model.control.accounting.common.exception.UnknownDefaultGlAccountCategoryException;
022import com.echothree.model.control.accounting.common.exception.UnknownGlAccountCategoryNameException;
023import com.echothree.model.control.accounting.server.control.AccountingControl;
024import com.echothree.model.control.core.common.ComponentVendors;
025import com.echothree.model.control.core.common.EntityTypes;
026import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
027import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
028import com.echothree.model.data.accounting.server.entity.GlAccountCategory;
029import com.echothree.model.data.accounting.server.value.GlAccountCategoryDetailValue;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseLogic;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.EntityPermission;
036import com.echothree.util.server.persistence.Session;
037import javax.enterprise.context.ApplicationScoped;
038import javax.enterprise.inject.spi.CDI;
039
040@ApplicationScoped
041public class GlAccountCategoryLogic
042        extends BaseLogic {
043
044    protected GlAccountCategoryLogic() {
045        super();
046    }
047
048    public static GlAccountCategoryLogic getInstance() {
049        return CDI.current().select(GlAccountCategoryLogic.class).get();
050    }
051
052    public GlAccountCategory createGlAccountCategory(final ExecutionErrorAccumulator eea, final String glAccountCategoryName,
053            final GlAccountCategory parentGlAccountCategory, final Boolean isDefault, final Integer sortOrder, final Language language,
054            final String description, final BasePK createdBy) {
055        var accountingControl = Session.getModelController(AccountingControl.class);
056        var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
057
058        if(glAccountCategory == null) {
059            glAccountCategory = accountingControl.createGlAccountCategory(glAccountCategoryName,
060                    parentGlAccountCategory, isDefault, sortOrder, createdBy);
061
062            if(description != null) {
063                accountingControl.createGlAccountCategoryDescription(glAccountCategory, language, description, createdBy);
064            }
065        } else {
066            handleExecutionError(DuplicateGlAccountCategoryNameException.class, eea, ExecutionErrors.DuplicateGlAccountCategoryName.name(),
067                    glAccountCategoryName);
068        }
069
070        return glAccountCategory;
071    }
072
073    public GlAccountCategory getGlAccountCategoryByName(final ExecutionErrorAccumulator eea, final String glAccountCategoryName,
074            final EntityPermission entityPermission) {
075        var accountingControl = Session.getModelController(AccountingControl.class);
076        var glAccountCategory = accountingControl.getGlAccountCategoryByName(glAccountCategoryName, entityPermission);
077
078        if(glAccountCategory == null) {
079            handleExecutionError(UnknownGlAccountCategoryNameException.class, eea, ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
080        }
081
082        return glAccountCategory;
083    }
084
085    public GlAccountCategory getGlAccountCategoryByName(final ExecutionErrorAccumulator eea, final String glAccountCategoryName) {
086        return getGlAccountCategoryByName(eea, glAccountCategoryName, EntityPermission.READ_ONLY);
087    }
088
089    public GlAccountCategory getGlAccountCategoryByNameForUpdate(final ExecutionErrorAccumulator eea, final String glAccountCategoryName) {
090        return getGlAccountCategoryByName(eea, glAccountCategoryName, EntityPermission.READ_WRITE);
091    }
092
093    public GlAccountCategory getGlAccountCategoryByUniversalSpec(final ExecutionErrorAccumulator eea,
094            final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault, final EntityPermission entityPermission) {
095        GlAccountCategory glAccountCategory = null;
096        var accountingControl = Session.getModelController(AccountingControl.class);
097        var glAccountCategoryName = universalSpec.getGlAccountCategoryName();
098        var parameterCount = (glAccountCategoryName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
099
100        switch(parameterCount) {
101            case 0 -> {
102                if(allowDefault) {
103                    glAccountCategory = accountingControl.getDefaultGlAccountCategory(entityPermission);
104
105                    if(glAccountCategory == null) {
106                        handleExecutionError(UnknownDefaultGlAccountCategoryException.class, eea, ExecutionErrors.UnknownDefaultGlAccountCategory.name());
107                    }
108                } else {
109                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
110                }
111            }
112            case 1 -> {
113                if(glAccountCategoryName == null) {
114                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
115                            ComponentVendors.ECHO_THREE.name(), EntityTypes.GlAccountCategory.name());
116
117                    if(!eea.hasExecutionErrors()) {
118                        glAccountCategory = accountingControl.getGlAccountCategoryByEntityInstance(entityInstance, entityPermission);
119                    }
120                } else {
121                    glAccountCategory = getGlAccountCategoryByName(eea, glAccountCategoryName, entityPermission);
122                }
123            }
124            default ->
125                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
126        }
127
128        return glAccountCategory;
129    }
130
131    public GlAccountCategory getGlAccountCategoryByUniversalSpec(final ExecutionErrorAccumulator eea,
132            final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault) {
133        return getGlAccountCategoryByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
134    }
135
136    public GlAccountCategory getGlAccountCategoryByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
137            final GlAccountCategoryUniversalSpec universalSpec, boolean allowDefault) {
138        return getGlAccountCategoryByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
139    }
140
141    public void updateGlAccountCategoryFromValue(GlAccountCategoryDetailValue glAccountCategoryDetailValue, BasePK updatedBy) {
142        var accountingControl = Session.getModelController(AccountingControl.class);
143
144        accountingControl.updateGlAccountCategoryFromValue(glAccountCategoryDetailValue, updatedBy);
145    }
146
147    public void deleteGlAccountCategory(final ExecutionErrorAccumulator eea, final GlAccountCategory glAccountCategory, final BasePK deletedBy) {
148        var accountingControl = Session.getModelController(AccountingControl.class);
149
150        accountingControl.deleteGlAccountCategory(glAccountCategory, deletedBy);
151    }
152
153}