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 java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
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    @Inject
056    AccountingControl accountingControl;
057
058    @Inject
059    ContactControl contactControl;
060
061    @Inject
062    TaxControl taxControl;
063
064    
065    /** Creates a new instance of CreateTaxCommand */
066    public CreateTaxCommand() {
067        super(null, FORM_FIELD_DEFINITIONS, false);
068    }
069    
070    @Override
071    protected BaseResult execute() {
072        var taxName = form.getTaxName();
073        var tax = taxControl.getTaxByName(taxName);
074        
075        if(tax == null) {
076            var contactMechanismPurposeName = form.getContactMechanismPurposeName();
077            var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName);
078            
079            if(contactMechanismPurpose != null) {
080                var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.POSTAL_ADDRESS.name());
081                
082                if(contactMechanismPurpose.getContactMechanismType().equals(contactMechanismType)) {
083                    var glAccountName = form.getGlAccountName();
084                    var glAccount = accountingControl.getGlAccountByName(glAccountName);
085                    
086                    if(glAccount != null) {
087                        var partyPK = getPartyPK();
088                        var includeShippingCharge = Boolean.valueOf(form.getIncludeShippingCharge());
089                        var includeProcessingCharge = Boolean.valueOf(form.getIncludeProcessingCharge());
090                        var includeInsuranceCharge = Boolean.valueOf(form.getIncludeInsuranceCharge());
091                        var percent = Integer.valueOf(form.getPercent());
092                        var isDefault = Boolean.valueOf(form.getIsDefault());
093                        var sortOrder = Integer.valueOf(form.getSortOrder());
094                        var description = form.getDescription();
095                        
096                        // TODO: Check glAccount's glAccountCategory to make sure it is appropriate for tax
097                        
098                        tax = taxControl.createTax(taxName, contactMechanismPurpose, glAccount, includeShippingCharge,
099                                includeProcessingCharge, includeInsuranceCharge, percent, isDefault, sortOrder, partyPK);
100                        
101                        if(description != null) {
102                            taxControl.createTaxDescription(tax, getPreferredLanguage(), description, partyPK);
103                        }
104                    } else {
105                        addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
106                    }
107                } else {
108                    addExecutionError(ExecutionErrors.InvalidContactMechanismPurpose.name(), contactMechanismPurposeName);
109                }
110            } else {
111                addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName);
112            }
113        } else {
114            addExecutionError(ExecutionErrors.DuplicateTaxName.name(), taxName);
115        }
116        
117        return null;
118    }
119    
120}