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.edit.TaxEdit;
020import com.echothree.control.user.tax.common.edit.TaxEditFactory;
021import com.echothree.control.user.tax.common.form.EditTaxForm;
022import com.echothree.control.user.tax.common.result.TaxResultFactory;
023import com.echothree.control.user.tax.common.spec.TaxSpec;
024import com.echothree.model.control.accounting.server.control.AccountingControl;
025import com.echothree.model.control.contact.common.ContactMechanismTypes;
026import com.echothree.model.control.contact.server.control.ContactControl;
027import com.echothree.model.control.tax.server.control.TaxControl;
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.common.command.EditMode;
034import com.echothree.util.server.control.BaseEditCommand;
035import com.echothree.util.server.persistence.Session;
036import com.echothree.util.server.string.PercentUtils;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039
040@Dependent
041public class EditTaxCommand
042        extends BaseEditCommand<TaxSpec, TaxEdit> {
043    
044    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
045    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
046    
047    static {
048        SPEC_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null)
050                );
051        
052        EDIT_FIELD_DEFINITIONS = List.of(
053                new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null),
056                new FieldDefinition("IncludeShippingCharge", FieldType.BOOLEAN, true, null, null),
057                new FieldDefinition("IncludeProcessingCharge", FieldType.BOOLEAN, true, null, null),
058                new FieldDefinition("IncludeInsuranceCharge", FieldType.BOOLEAN, true, null, null),
059                new FieldDefinition("Percent", FieldType.FRACTIONAL_PERCENT, true, null, null),
060                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
061                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
062                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
063                );
064    }
065    
066    /** Creates a new instance of EditTaxCommand */
067    public EditTaxCommand() {
068        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var taxControl = Session.getModelController(TaxControl.class);
074        var result = TaxResultFactory.getEditTaxResult();
075        
076        if(editMode.equals(EditMode.LOCK)) {
077            var taxName = spec.getTaxName();
078            var tax = taxControl.getTaxByName(taxName);
079            
080            if(tax != null) {
081                result.setTax(taxControl.getTaxTransfer(getUserVisit(), tax));
082                
083                if(lockEntity(tax)) {
084                    var taxDescription = taxControl.getTaxDescription(tax, getPreferredLanguage());
085                    var edit = TaxEditFactory.getTaxEdit();
086                    var taxDetail = tax.getLastDetail();
087                    
088                    result.setEdit(edit);
089                    edit.setTaxName(taxDetail.getTaxName());
090                    edit.setContactMechanismPurposeName(taxDetail.getContactMechanismPurpose().getContactMechanismPurposeName());
091                    edit.setGlAccountName(taxDetail.getGlAccount().getLastDetail().getGlAccountName());
092                    edit.setIncludeShippingCharge(taxDetail.getIncludeShippingCharge().toString());
093                    edit.setIncludeProcessingCharge(taxDetail.getIncludeProcessingCharge().toString());
094                    edit.setIncludeInsuranceCharge(taxDetail.getIncludeInsuranceCharge().toString());
095                    edit.setPercent(PercentUtils.getInstance().formatFractionalPercent(taxDetail.getPercent()));
096                    edit.setIsDefault(taxDetail.getIsDefault().toString());
097                    edit.setSortOrder(taxDetail.getSortOrder().toString());
098                    
099                    if(taxDescription != null) {
100                        edit.setDescription(taxDescription.getDescription());
101                    }
102                } else {
103                    addExecutionError(ExecutionErrors.EntityLockFailed.name());
104                }
105                
106                result.setEntityLock(getEntityLockTransfer(tax));
107            } else {
108                addExecutionError(ExecutionErrors.UnknownTaxName.name(), taxName);
109            }
110        } else if(editMode.equals(EditMode.UPDATE)) {
111            var taxName = spec.getTaxName();
112            var tax = taxControl.getTaxByNameForUpdate(taxName);
113            
114            if(tax != null) {
115                taxName = edit.getTaxName();
116                var duplicateTax = taxControl.getTaxByName(taxName);
117                
118                if(duplicateTax == null || tax.equals(duplicateTax)) {
119                    var contactControl = Session.getModelController(ContactControl.class);
120                    var contactMechanismPurposeName = edit.getContactMechanismPurposeName();
121                    var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(contactMechanismPurposeName);
122                    
123                    if(contactMechanismPurpose != null) {
124                        var contactMechanismType = contactControl.getContactMechanismTypeByName(ContactMechanismTypes.POSTAL_ADDRESS.name());
125                        
126                        if(contactMechanismPurpose.getContactMechanismType().equals(contactMechanismType)) {
127                            var accountingControl = Session.getModelController(AccountingControl.class);
128                            var glAccountName = edit.getGlAccountName();
129                            var glAccount = accountingControl.getGlAccountByName(glAccountName);
130                            
131                            if(glAccount != null) {
132                                if(lockEntityForUpdate(tax)) {
133                                    try {
134                                        var partyPK = getPartyPK();
135                                        var taxDetailValue = taxControl.getTaxDetailValueForUpdate(tax);
136                                        var taxDescription = taxControl.getTaxDescriptionForUpdate(tax, getPreferredLanguage());
137                                        var description = edit.getDescription();
138                                        
139                                        taxDetailValue.setTaxName(edit.getTaxName());
140                                        taxDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey());
141                                        taxDetailValue.setGlAccountPK(glAccount.getPrimaryKey());
142                                        taxDetailValue.setPercent(Integer.valueOf(edit.getPercent()));
143                                        taxDetailValue.setIncludeShippingCharge(Boolean.valueOf(edit.getIncludeShippingCharge()));
144                                        taxDetailValue.setIncludeProcessingCharge(Boolean.valueOf(edit.getIncludeProcessingCharge()));
145                                        taxDetailValue.setIncludeInsuranceCharge(Boolean.valueOf(edit.getIncludeInsuranceCharge()));
146                                        taxDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
147                                        taxDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
148                                        
149                                        taxControl.updateTaxFromValue(taxDetailValue, partyPK);
150                                        
151                                        if(taxDescription == null && description != null) {
152                                            taxControl.createTaxDescription(tax, getPreferredLanguage(), description, partyPK);
153                                        } else if(taxDescription != null && description == null) {
154                                            taxControl.deleteTaxDescription(taxDescription, partyPK);
155                                        } else if(taxDescription != null && description != null) {
156                                            var taxDescriptionValue = taxControl.getTaxDescriptionValue(taxDescription);
157                                            
158                                            taxDescriptionValue.setDescription(description);
159                                            taxControl.updateTaxDescriptionFromValue(taxDescriptionValue, partyPK);
160                                        }
161                                    } finally {
162                                        unlockEntity(tax);
163                                    }
164                                } else {
165                                    addExecutionError(ExecutionErrors.EntityLockStale.name());
166                                }
167                            } else {
168                                addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
169                            }
170                        } else {
171                            addExecutionError(ExecutionErrors.InvalidContactMechanismPurpose.name(), contactMechanismPurposeName);
172                        }
173                    } else {
174                        addExecutionError(ExecutionErrors.UnknownContactMechanismPurposeName.name(), contactMechanismPurposeName);
175                    }
176                } else {
177                    addExecutionError(ExecutionErrors.DuplicateTaxName.name(), taxName);
178                }
179            } else {
180                addExecutionError(ExecutionErrors.UnknownTaxName.name(), taxName);
181            }
182            
183            if(hasExecutionErrors()) {
184                result.setTax(taxControl.getTaxTransfer(getUserVisit(), tax));
185                result.setEntityLock(getEntityLockTransfer(tax));
186            }
187        }
188        
189        return result;
190    }
191    
192}