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