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.inject.Inject;
040
041@ApplicationScoped
042public class TaxLogic
043        extends BaseLogic {
044
045    @Inject
046    TaxControl taxControl;
047
048    @Inject
049    EntityInstanceLogic entityInstanceLogic;
050
051    protected TaxLogic() {
052        super();
053    }
054
055    public Tax createTax(final ExecutionErrorAccumulator eea, final String taxName,
056            final ContactMechanismPurpose contactMechanismPurpose, final GlAccount glAccount,
057            final Boolean includeShippingCharge, final Boolean includeProcessingCharge,
058            final Boolean includeInsuranceCharge, final Integer percent, final Boolean isDefault,
059            final Integer sortOrder, final Language language, final String description, final BasePK createdBy) {
060        var tax = taxControl.getTaxByName(taxName);
061
062        if(tax == null) {
063            tax = taxControl.createTax(taxName, contactMechanismPurpose, glAccount, includeShippingCharge,
064                    includeProcessingCharge, includeInsuranceCharge, percent, isDefault, sortOrder, createdBy);
065
066            if(description != null) {
067                taxControl.createTaxDescription(tax, language, description, createdBy);
068            }
069        } else {
070            handleExecutionError(DuplicateTaxNameException.class, eea, ExecutionErrors.DuplicateTaxName.name(), taxName);
071        }
072
073        return tax;
074    }
075
076    public Tax getTaxByName(final ExecutionErrorAccumulator eea, final String taxName,
077            final EntityPermission entityPermission) {
078        var tax = taxControl.getTaxByName(taxName, entityPermission);
079
080        if(tax == null) {
081            handleExecutionError(UnknownTaxNameException.class, eea, ExecutionErrors.UnknownTaxName.name(), taxName);
082        }
083
084        return tax;
085    }
086
087    public Tax getTaxByName(final ExecutionErrorAccumulator eea, final String taxName) {
088        return getTaxByName(eea, taxName, EntityPermission.READ_ONLY);
089    }
090
091    public Tax getTaxByNameForUpdate(final ExecutionErrorAccumulator eea, final String taxName) {
092        return getTaxByName(eea, taxName, EntityPermission.READ_WRITE);
093    }
094
095    public Tax getTaxByUniversalSpec(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
096            final boolean allowDefault, final EntityPermission entityPermission) {
097        var taxName = universalSpec.getTaxName();
098        Tax tax = null;
099        var parameterCount = (taxName == null ? 0 : 1) + entityInstanceLogic.countPossibleEntitySpecs(universalSpec);
100
101        switch(parameterCount) {
102            case 0 -> {
103                if(allowDefault) {
104                    tax = taxControl.getDefaultTax(entityPermission);
105
106                    if(tax == null) {
107                        handleExecutionError(UnknownDefaultTaxException.class, eea, ExecutionErrors.UnknownDefaultTax.name());
108                    }
109                } else {
110                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
111                }
112            }
113            case 1 -> {
114                if(taxName == null) {
115                    var entityInstance = entityInstanceLogic.getEntityInstance(eea, universalSpec,
116                            ComponentVendors.ECHO_THREE.name(), EntityTypes.Tax.name());
117
118                    if(eea == null || !eea.hasExecutionErrors()) {
119                        tax = taxControl.getTaxByEntityInstance(entityInstance, entityPermission);
120                    }
121                } else {
122                    tax = getTaxByName(eea, taxName, entityPermission);
123                }
124            }
125            default ->
126                    handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
127        }
128
129        return tax;
130    }
131
132    public Tax getTaxByUniversalSpec(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
133            final boolean allowDefault) {
134        return getTaxByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_ONLY);
135    }
136
137    public Tax getTaxByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea, final TaxUniversalSpec universalSpec,
138            final boolean allowDefault) {
139        return getTaxByUniversalSpec(eea, universalSpec, allowDefault, EntityPermission.READ_WRITE);
140    }
141
142    public void updateTaxFromValue(final TaxDetailValue taxDetailValue, final BasePK updatedBy) {
143        taxControl.updateTaxFromValue(taxDetailValue, updatedBy);
144    }
145
146    public void deleteTax(final ExecutionErrorAccumulator eea, final Tax tax, final BasePK deletedBy) {
147        taxControl.deleteTax(tax, deletedBy);
148    }
149
150}