001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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.form.CreateTaxForm; 020import com.echothree.model.control.accounting.server.control.AccountingControl; 021import com.echothree.model.control.contact.common.ContactMechanismTypes; 022import com.echothree.model.control.contact.server.control.ContactControl; 023import com.echothree.model.control.tax.server.control.TaxControl; 024import com.echothree.model.data.user.common.pk.UserVisitPK; 025import com.echothree.util.common.message.ExecutionErrors; 026import com.echothree.util.common.validation.FieldDefinition; 027import com.echothree.util.common.validation.FieldType; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.server.control.BaseSimpleCommand; 030import com.echothree.util.server.persistence.Session; 031import java.util.Arrays; 032import java.util.Collections; 033import java.util.List; 034import javax.enterprise.context.RequestScoped; 035 036@RequestScoped 037public class CreateTaxCommand 038 extends BaseSimpleCommand<CreateTaxForm> { 039 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 044 new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null), 045 new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null), 046 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null), 047 new FieldDefinition("IncludeShippingCharge", FieldType.BOOLEAN, true, null, null), 048 new FieldDefinition("IncludeProcessingCharge", FieldType.BOOLEAN, true, null, null), 049 new FieldDefinition("IncludeInsuranceCharge", FieldType.BOOLEAN, true, null, null), 050 new FieldDefinition("Percent", FieldType.FRACTIONAL_PERCENT, true, null, null), 051 new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null), 052 new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null), 053 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 054 )); 055 } 056 057 /** Creates a new instance of CreateTaxCommand */ 058 public CreateTaxCommand() { 059 super(null, FORM_FIELD_DEFINITIONS, false); 060 } 061 062 @Override 063 protected BaseResult execute() { 064 var taxControl = Session.getModelController(TaxControl.class); 065 var taxName = form.getTaxName(); 066 var tax = taxControl.getTaxByName(taxName); 067 068 if(tax == null) { 069 var contactControl = Session.getModelController(ContactControl.class); 070 var contactMechanismPurposeName = form.getContactMechanismPurposeName(); 071 var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName); 072 073 if(contactMechanismPurpose != null) { 074 var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.POSTAL_ADDRESS.name()); 075 076 if(contactMechanismPurpose.getContactMechanismType().equals(contactMechanismType)) { 077 var accountingControl = Session.getModelController(AccountingControl.class); 078 var glAccountName = form.getGlAccountName(); 079 var glAccount = accountingControl.getGlAccountByName(glAccountName); 080 081 if(glAccount != null) { 082 var partyPK = getPartyPK(); 083 var includeShippingCharge = Boolean.valueOf(form.getIncludeShippingCharge()); 084 var includeProcessingCharge = Boolean.valueOf(form.getIncludeProcessingCharge()); 085 var includeInsuranceCharge = Boolean.valueOf(form.getIncludeInsuranceCharge()); 086 var percent = Integer.valueOf(form.getPercent()); 087 var isDefault = Boolean.valueOf(form.getIsDefault()); 088 var sortOrder = Integer.valueOf(form.getSortOrder()); 089 var description = form.getDescription(); 090 091 // TODO: Check glAccount's glAccountCategory to make sure it is appropriate for tax 092 093 tax = taxControl.createTax(taxName, contactMechanismPurpose, glAccount, includeShippingCharge, 094 includeProcessingCharge, includeInsuranceCharge, percent, isDefault, sortOrder, partyPK); 095 096 if(description != null) { 097 taxControl.createTaxDescription(tax, getPreferredLanguage(), description, partyPK); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.InvalidContactMechanismPurpose.name(), contactMechanismPurposeName); 104 } 105 } else { 106 addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName); 107 } 108 } else { 109 addExecutionError(ExecutionErrors.DuplicateTaxName.name(), taxName); 110 } 111 112 return null; 113 } 114 115}