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.tax.server.logic;
018
019import com.echothree.control.user.tax.common.spec.TaxUniversalSpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.tax.common.exception.DuplicateTaxNameException;
025import com.echothree.model.control.tax.common.exception.UnknownDefaultTaxException;
026import com.echothree.model.control.tax.common.exception.UnknownTaxNameException;
027import com.echothree.model.control.tax.server.control.TaxControl;
028import com.echothree.model.data.accounting.server.entity.GlAccount;
029import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
030import com.echothree.model.data.party.server.entity.Language;
031import com.echothree.model.data.tax.server.entity.Tax;
032import com.echothree.model.data.tax.server.value.TaxDetailValue;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.persistence.BasePK;
035import com.echothree.util.server.control.BaseLogic;
036import com.echothree.util.server.message.ExecutionErrorAccumulator;
037import com.echothree.util.server.persistence.EntityPermission;
038import javax.enterprise.context.ApplicationScoped;
039import javax.enterprise.inject.spi.CDI;
040import javax.inject.Inject;
041
042@ApplicationScoped
043public class TaxLogic
044        extends BaseLogic {
045
046    @Inject
047    TaxControl taxControl;
048
049    @Inject
050    EntityInstanceLogic entityInstanceLogic;
051
052    protected TaxLogic() {
053        super();
054    }
055
056    public static TaxLogic getInstance() {
057        return CDI.current().select(TaxLogic.class).get();
058    }
059
060    public Tax createTax(final ExecutionErrorAccumulator eea, final String taxName,
061            final ContactMechanismPurpose contactMechanismPurpose, final GlAccount glAccount,
062            final Boolean includeShippingCharge, final Boolean includeProcessingCharge,
063            final Boolean includeInsuranceCharge, final Integer percent, final Boolean isDefault,
064            final Integer sortOrder, final Language language, final String description, final BasePK createdBy) {
065        var tax = taxControl.getTaxByName(taxName);
066
067        if(tax == null) {
068            tax = taxControl.createTax(taxName, contactMechanismPurpose, glAccount, includeShippingCharge,
069                    includeProcessingCharge, includeInsuranceCharge, percent, isDefault, sortOrder, createdBy);
070
071            if(description != null) {
072                taxControl.createTaxDescription(tax, language, description, createdBy);
073            }
074        } else {
075            handleExecutionError(DuplicateTaxNameException.class, eea, ExecutionErrors.DuplicateTaxName.name(), taxName);
076        }
077
078        return tax;
079    }
080
081    public Tax getTaxByName(final ExecutionErrorAccumulator eea, final String taxName,
082            final EntityPermission entityPermission) {
083        var tax = taxControl.getTaxByName(taxName, entityPermission);
084
085        if(tax == null) {
086            handleExecutionError(UnknownTaxNameException.class, eea, ExecutionErrors.UnknownTaxName.name(), taxName);
087        }
088
089        return tax;
090    }
091
092    public Tax getTaxByName(final ExecutionErrorAccumulator eea, final String taxName) {
093        return getTaxByName(eea, taxName, EntityPermission.READ_ONLY);
094    }
095
096    public Tax getTaxByNameForUpdate(final ExecutionErrorAccumulator eea, final String taxName) {
097        return getTaxByName(eea, taxName, EntityPermission.READ_WRITE);
098    }
099
100    public Tax getTaxByUniversalSpec(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
101            final boolean allowDefault, final EntityPermission entityPermission) {
102        var taxName = universalSpec.getTaxName();
103        Tax tax = null;
104        var parameterCount = (taxName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
105
106        switch(parameterCount) {
107            case 0 -> {
108                if(allowDefault) {
109                    tax = taxControl.getDefaultTax(entityPermission);
110
111                    if(tax == null) {
112                        handleExecutionError(UnknownDefaultTaxException.class, eea, ExecutionErrors.UnknownDefaultTax.name());
113                    }
114                } else {
115                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
116                }
117            }
118            case 1 -> {
119                if(taxName == null) {
120                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
121                            ComponentVendors.ECHO_THREE.name(), EntityTypes.Tax.name());
122
123                    if(eea == null || !eea.hasExecutionErrors()) {
124                        tax = taxControl.getTaxByEntityInstance(entityInstance, entityPermission);
125                    }
126                } else {
127                    tax = getTaxByName(eea, taxName, entityPermission);
128                }
129            }
130            default ->
131                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
132        }
133
134        return tax;
135    }
136
137    public Tax getTaxByUniversalSpec(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
138            final boolean allowDefault) {
139        return getTaxByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
140    }
141
142    public Tax getTaxByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
143            final boolean allowDefault) {
144        return getTaxByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
145    }
146
147    public void updateTaxFromValue(final TaxDetailValue taxDetailValue, final BasePK updatedBy) {
148        taxControl.updateTaxFromValue(taxDetailValue, updatedBy);
149    }
150
151    public void deleteTax(final ExecutionErrorAccumulator eea, final Tax tax, final BasePK deletedBy) {
152        taxControl.deleteTax(tax, deletedBy);
153    }
154
155}