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