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