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