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