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.control.user.tax.server.command; 018 019import com.echothree.control.user.tax.common.edit.TaxEdit; 020import com.echothree.control.user.tax.common.edit.TaxEditFactory; 021import com.echothree.control.user.tax.common.form.EditTaxForm; 022import com.echothree.control.user.tax.common.result.TaxResultFactory; 023import com.echothree.control.user.tax.common.spec.TaxSpec; 024import com.echothree.model.control.accounting.server.control.AccountingControl; 025import com.echothree.model.control.contact.common.ContactMechanismTypes; 026import com.echothree.model.control.contact.server.control.ContactControl; 027import com.echothree.model.control.tax.server.control.TaxControl; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.common.command.BaseResult; 033import com.echothree.util.common.command.EditMode; 034import com.echothree.util.server.control.BaseEditCommand; 035import com.echothree.util.server.string.PercentUtils; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class EditTaxCommand 042 extends BaseEditCommand<TaxSpec, TaxEdit> { 043 044 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 045 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 046 047 static { 048 SPEC_FIELD_DEFINITIONS = List.of( 049 new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null) 050 ); 051 052 EDIT_FIELD_DEFINITIONS = List.of( 053 new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null), 054 new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null), 055 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null), 056 new FieldDefinition("IncludeShippingCharge", FieldType.BOOLEAN, true, null, null), 057 new FieldDefinition("IncludeProcessingCharge", FieldType.BOOLEAN, true, null, null), 058 new FieldDefinition("IncludeInsuranceCharge", FieldType.BOOLEAN, true, null, null), 059 new FieldDefinition("Percent", FieldType.FRACTIONAL_PERCENT, true, null, null), 060 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 061 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 062 new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L) 063 ); 064 } 065 066 @Inject 067 AccountingControl accountingControl; 068 069 @Inject 070 ContactControl contactControl; 071 072 @Inject 073 TaxControl taxControl; 074 075 076 /** Creates a new instance of EditTaxCommand */ 077 public EditTaxCommand() { 078 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 079 } 080 081 @Override 082 protected BaseResult execute() { 083 var result = TaxResultFactory.getEditTaxResult(); 084 085 if(editMode.equals(EditMode.LOCK)) { 086 var taxName = spec.getTaxName(); 087 var tax = taxControl.getTaxByName(taxName); 088 089 if(tax != null) { 090 result.setTax(taxControl.getTaxTransfer(getUserVisit(), tax)); 091 092 if(lockEntity(tax)) { 093 var taxDescription = taxControl.getTaxDescription(tax, getPreferredLanguage()); 094 var edit = TaxEditFactory.getTaxEdit(); 095 var taxDetail = tax.getLastDetail(); 096 097 result.setEdit(edit); 098 edit.setTaxName(taxDetail.getTaxName()); 099 edit.setContactMechanismPurposeName(taxDetail.getContactMechanismPurpose().getContactMechanismPurposeName()); 100 edit.setGlAccountName(taxDetail.getGlAccount().getLastDetail().getGlAccountName()); 101 edit.setIncludeShippingCharge(taxDetail.getIncludeShippingCharge().toString()); 102 edit.setIncludeProcessingCharge(taxDetail.getIncludeProcessingCharge().toString()); 103 edit.setIncludeInsuranceCharge(taxDetail.getIncludeInsuranceCharge().toString()); 104 edit.setPercent(PercentUtils.getInstance().formatFractionalPercent(taxDetail.getPercent())); 105 edit.setIsDefault(taxDetail.getIsDefault().toString()); 106 edit.setSortOrder(taxDetail.getSortOrder().toString()); 107 108 if(taxDescription != null) { 109 edit.setDescription(taxDescription.getDescription()); 110 } 111 } else { 112 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 113 } 114 115 result.setEntityLock(getEntityLockTransfer(tax)); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownTaxName.name(), taxName); 118 } 119 } else if(editMode.equals(EditMode.UPDATE)) { 120 var taxName = spec.getTaxName(); 121 var tax = taxControl.getTaxByNameForUpdate(taxName); 122 123 if(tax != null) { 124 taxName = edit.getTaxName(); 125 var duplicateTax = taxControl.getTaxByName(taxName); 126 127 if(duplicateTax == null || tax.equals(duplicateTax)) { 128 var contactMechanismPurposeName = edit.getContactMechanismPurposeName(); 129 var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName); 130 131 if(contactMechanismPurpose != null) { 132 var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.POSTAL_ADDRESS.name()); 133 134 if(contactMechanismPurpose.getContactMechanismType().equals(contactMechanismType)) { 135 var glAccountName = edit.getGlAccountName(); 136 var glAccount = accountingControl.getGlAccountByName(glAccountName); 137 138 if(glAccount != null) { 139 if(lockEntityForUpdate(tax)) { 140 try { 141 var partyPK = getPartyPK(); 142 var taxDetailValue = taxControl.getTaxDetailValueForUpdate(tax); 143 var taxDescription = taxControl.getTaxDescriptionForUpdate(tax, getPreferredLanguage()); 144 var description = edit.getDescription(); 145 146 taxDetailValue.setTaxName(edit.getTaxName()); 147 taxDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey()); 148 taxDetailValue.setGlAccountPK(glAccount.getPrimaryKey()); 149 taxDetailValue.setPercent(Integer.valueOf(edit.getPercent())); 150 taxDetailValue.setIncludeShippingCharge(Boolean.valueOf(edit.getIncludeShippingCharge())); 151 taxDetailValue.setIncludeProcessingCharge(Boolean.valueOf(edit.getIncludeProcessingCharge())); 152 taxDetailValue.setIncludeInsuranceCharge(Boolean.valueOf(edit.getIncludeInsuranceCharge())); 153 taxDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault())); 154 taxDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder())); 155 156 taxControl.updateTaxFromValue(taxDetailValue, partyPK); 157 158 if(taxDescription == null && description != null) { 159 taxControl.createTaxDescription(tax, getPreferredLanguage(), description, partyPK); 160 } else if(taxDescription != null && description == null) { 161 taxControl.deleteTaxDescription(taxDescription, partyPK); 162 } else if(taxDescription != null && description != null) { 163 var taxDescriptionValue = taxControl.getTaxDescriptionValue(taxDescription); 164 165 taxDescriptionValue.setDescription(description); 166 taxControl.updateTaxDescriptionFromValue(taxDescriptionValue, partyPK); 167 } 168 } finally { 169 unlockEntity(tax); 170 } 171 } else { 172 addExecutionError(ExecutionErrors.EntityLockStale.name()); 173 } 174 } else { 175 addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName); 176 } 177 } else { 178 addExecutionError(ExecutionErrors.InvalidContactMechanismPurpose.name(), contactMechanismPurposeName); 179 } 180 } else { 181 addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName); 182 } 183 } else { 184 addExecutionError(ExecutionErrors.DuplicateTaxName.name(), taxName); 185 } 186 } else { 187 addExecutionError(ExecutionErrors.UnknownTaxName.name(), taxName); 188 } 189 190 if(hasExecutionErrors()) { 191 result.setTax(taxControl.getTaxTransfer(getUserVisit(), tax)); 192 result.setEntityLock(getEntityLockTransfer(tax)); 193 } 194 } 195 196 return result; 197 } 198 199}