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.result.EditTaxResult;
022import com.echothree.control.user.tax.common.result.TaxResultFactory;
023import com.echothree.control.user.tax.common.spec.TaxUniversalSpec;
024import com.echothree.model.control.accounting.server.logic.GlAccountLogic;
025import com.echothree.model.control.contact.common.ContactMechanismTypes;
026import com.echothree.model.control.contact.server.logic.ContactMechanismPurposeLogic;
027import com.echothree.model.control.tax.server.control.TaxControl;
028import com.echothree.model.control.tax.server.logic.TaxLogic;
029import com.echothree.model.data.accounting.server.entity.GlAccount;
030import com.echothree.model.data.contact.server.entity.ContactMechanismPurpose;
031import com.echothree.model.data.tax.server.entity.Tax;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseEditCommand;
036import com.echothree.util.server.string.PercentUtils;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class EditTaxCommand
043        extends BaseEditCommand<TaxUniversalSpec, TaxEdit, EditTaxResult, Tax, Tax> {
044
045    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
046    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
047
048    static {
049        SPEC_FIELD_DEFINITIONS = List.of(
050                new FieldDefinition("TaxName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null),
052                new FieldDefinition("Uuid", FieldType.UUID, false, null, null)
053        );
054
055        EDIT_FIELD_DEFINITIONS = List.of(
056                new FieldDefinition("TaxName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("ContactMechanismPurposeName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("IncludeShippingCharge", FieldType.BOOLEAN, true, null, null),
060                new FieldDefinition("IncludeProcessingCharge", FieldType.BOOLEAN, true, null, null),
061                new FieldDefinition("IncludeInsuranceCharge", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("Percent", FieldType.FRACTIONAL_PERCENT, true, null, null),
063                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, true, 1L, 132L)
066        );
067    }
068
069    private ContactMechanismPurpose contactMechanismPurpose;
070    private GlAccount glAccount;
071
072    @Inject
073    TaxControl taxControl;
074
075    @Inject
076    ContactMechanismPurposeLogic contactMechanismPurposeLogic;
077
078    @Inject
079    GlAccountLogic glAccountLogic;
080
081    @Inject
082    TaxLogic taxLogic;
083
084    /** Creates a new instance of EditTaxCommand */
085    public EditTaxCommand() {
086        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
087    }
088
089    @Override
090    public EditTaxResult getResult() {
091        return TaxResultFactory.getEditTaxResult();
092    }
093
094    @Override
095    public TaxEdit getEdit() {
096        return TaxEditFactory.getTaxEdit();
097    }
098
099    @Override
100    public Tax getEntity(EditTaxResult result) {
101        return taxLogic.getTaxByUniversalSpec(this, spec, false, editModeToEntityPermission(editMode));
102    }
103
104    @Override
105    public Tax getLockEntity(Tax tax) {
106        return tax;
107    }
108
109    @Override
110    public void fillInResult(EditTaxResult result, Tax tax) {
111        result.setTax(taxControl.getTaxTransfer(getUserVisit(), tax));
112    }
113
114    @Override
115    public void doLock(TaxEdit edit, Tax tax) {
116        var taxDescription = taxControl.getTaxDescription(tax, getPreferredLanguage());
117        var taxDetail = tax.getLastDetail();
118
119        edit.setTaxName(taxDetail.getTaxName());
120        edit.setContactMechanismPurposeName(taxDetail.getContactMechanismPurpose().getContactMechanismPurposeName());
121        edit.setGlAccountName(taxDetail.getGlAccount().getLastDetail().getGlAccountName());
122        edit.setIncludeShippingCharge(taxDetail.getIncludeShippingCharge().toString());
123        edit.setIncludeProcessingCharge(taxDetail.getIncludeProcessingCharge().toString());
124        edit.setIncludeInsuranceCharge(taxDetail.getIncludeInsuranceCharge().toString());
125        edit.setPercent(PercentUtils.getInstance().formatFractionalPercent(taxDetail.getPercent()));
126        edit.setIsDefault(taxDetail.getIsDefault().toString());
127        edit.setSortOrder(taxDetail.getSortOrder().toString());
128
129        if(taxDescription != null) {
130            edit.setDescription(taxDescription.getDescription());
131        }
132    }
133
134    @Override
135    public void canUpdate(Tax tax) {
136        var taxName = edit.getTaxName();
137        var duplicateTax = taxControl.getTaxByName(taxName);
138        var contactMechanismPurposeName = edit.getContactMechanismPurposeName();
139        var glAccountName = edit.getGlAccountName();
140
141        if(duplicateTax != null && !tax.equals(duplicateTax)) {
142            addExecutionError(ExecutionErrors.DuplicateTaxName.name(), taxName);
143        }
144
145        contactMechanismPurpose = contactMechanismPurposeLogic.getContactMechanismPurposeByName(this, contactMechanismPurposeName);
146        glAccount = glAccountLogic.getGlAccountByName(this, glAccountName);
147
148        if(!hasExecutionErrors()) {
149            if(!ContactMechanismTypes.POSTAL_ADDRESS.name().equals(contactMechanismPurpose.getContactMechanismType().getContactMechanismTypeName())) {
150                addExecutionError(ExecutionErrors.InvalidContactMechanismPurpose.name(), contactMechanismPurposeName);
151            }
152        }
153    }
154
155    @Override
156    public void doUpdate(Tax tax) {
157        var partyPK = getPartyPK();
158        var taxDetailValue = taxControl.getTaxDetailValueForUpdate(tax);
159        var taxDescription = taxControl.getTaxDescriptionForUpdate(tax, getPreferredLanguage());
160        var description = edit.getDescription();
161
162        taxDetailValue.setTaxName(edit.getTaxName());
163        taxDetailValue.setContactMechanismPurposePK(contactMechanismPurpose.getPrimaryKey());
164        taxDetailValue.setGlAccountPK(glAccount.getPrimaryKey());
165        taxDetailValue.setPercent(Integer.valueOf(edit.getPercent()));
166        taxDetailValue.setIncludeShippingCharge(Boolean.valueOf(edit.getIncludeShippingCharge()));
167        taxDetailValue.setIncludeProcessingCharge(Boolean.valueOf(edit.getIncludeProcessingCharge()));
168        taxDetailValue.setIncludeInsuranceCharge(Boolean.valueOf(edit.getIncludeInsuranceCharge()));
169        taxDetailValue.setIsDefault(Boolean.valueOf(edit.getIsDefault()));
170        taxDetailValue.setSortOrder(Integer.valueOf(edit.getSortOrder()));
171
172        taxControl.updateTaxFromValue(taxDetailValue, partyPK);
173
174        if(taxDescription == null && description != null) {
175            taxControl.createTaxDescription(tax, getPreferredLanguage(), description, partyPK);
176        } else if(taxDescription != null && description == null) {
177            taxControl.deleteTaxDescription(taxDescription, partyPK);
178        } else if(taxDescription != null && description != null) {
179            var taxDescriptionValue = taxControl.getTaxDescriptionValue(taxDescription);
180
181            taxDescriptionValue.setDescription(description);
182
183            taxControl.updateTaxDescriptionFromValue(taxDescriptionValue, partyPK);
184        }
185    }
186
187}