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