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