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.accounting.server.command;
018
019import com.echothree.control.user.accounting.common.form.CreateCurrencyForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.accounting.server.logic.SymbolPositionLogic;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.data.user.common.pk.UserVisitPK;
026import com.echothree.util.common.message.ExecutionErrors;
027import com.echothree.util.common.validation.FieldDefinition;
028import com.echothree.util.common.validation.FieldType;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.control.CommandSecurityDefinition;
032import com.echothree.util.server.control.PartyTypeDefinition;
033import com.echothree.util.server.control.SecurityRoleDefinition;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class CreateCurrencyCommand
040        extends BaseSimpleCommand<CreateCurrencyForm> {
041    
042    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
047                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
048                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
049                        new SecurityRoleDefinition(SecurityRoleGroups.Currency.name(), SecurityRoles.Create.name())
050                ))
051        ));
052        
053        FORM_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, 3L),
055                new FieldDefinition("Symbol", FieldType.STRING, false, 1L, 20L),
056                new FieldDefinition("SymbolPositionName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("SymbolOnListStart", FieldType.BOOLEAN, false, null, null),
058                new FieldDefinition("SymbolOnListMember", FieldType.BOOLEAN, false, null, null),
059                new FieldDefinition("SymbolOnSubtotal", FieldType.BOOLEAN, false, null, null),
060                new FieldDefinition("SymbolOnTotal", FieldType.BOOLEAN, false, null, null),
061                new FieldDefinition("GroupingSeparator", FieldType.STRING, true, 1L, 1L),
062                new FieldDefinition("GroupingSize", FieldType.SIGNED_INTEGER, true, null, null),
063                new FieldDefinition("FractionSeparator", FieldType.STRING, false, 1L, 1L),
064                new FieldDefinition("DefaultFractionDigits", FieldType.SIGNED_INTEGER, false, null, null),
065                new FieldDefinition("PriceUnitFractionDigits", FieldType.SIGNED_INTEGER, false, null, null),
066                new FieldDefinition("PriceLineFractionDigits", FieldType.SIGNED_INTEGER, false, null, null),
067                new FieldDefinition("CostUnitFractionDigits", FieldType.SIGNED_INTEGER, false, null, null),
068                new FieldDefinition("CostLineFractionDigits", FieldType.SIGNED_INTEGER, false, null, null),
069                new FieldDefinition("MinusSign", FieldType.STRING, true, 1L, 1L),
070                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
071                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null)
072        );
073    }
074
075    @Inject
076    AccountingControl accountingControl;
077
078    @Inject
079    SymbolPositionLogic symbolPositionLogic;
080
081    
082    /** Creates a new instance of CreateCurrencyCommand */
083    public CreateCurrencyCommand() {
084        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var currencyIsoName = form.getCurrencyIsoName();
090        var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
091        
092        if(currency == null) {
093            var symbolPosition = symbolPositionLogic.getSymbolPositionByName(this, form.getSymbolPositionName());
094            
095            if(!hasExecutionErrors()) {
096                var groupingSeparator = form.getGroupingSeparator();
097                var groupingSize = Integer.valueOf(form.getGroupingSize());
098                var minusSign = form.getMinusSign();
099                var isDefault = Boolean.valueOf(form.getIsDefault());
100                var sortOrder = Integer.valueOf(form.getSortOrder());
101                var symbol = form.getSymbol();
102                var symbolOnListStart = symbol == null? null: Boolean.valueOf(form.getSymbolOnListStart());
103                var symbolOnListMember = symbol == null? null: Boolean.valueOf(form.getSymbolOnListMember());
104                var symbolOnSubtotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnSubtotal());
105                var symbolOnTotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnTotal());
106
107                if(symbol == null || (symbol != null && symbolOnListStart != null && symbolOnListMember != null
108                        && symbolOnSubtotal != null && symbolOnTotal != null)) {
109                    var fractionSeparator = form.getFractionSeparator();
110                    var defaultFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getDefaultFractionDigits());
111                    var priceUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceUnitFractionDigits());
112                    var priceLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceLineFractionDigits());
113                    var costUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostUnitFractionDigits());
114                    var costLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostLineFractionDigits());
115
116                    if(fractionSeparator == null || (defaultFractionDigits != null && priceUnitFractionDigits != null
117                            && priceLineFractionDigits != null && costUnitFractionDigits != null && costLineFractionDigits != null)) {
118                        accountingControl.createCurrency(currencyIsoName, symbol, symbolPosition, symbolOnListStart, symbolOnListMember,
119                                symbolOnSubtotal, symbolOnTotal, groupingSeparator, groupingSize, fractionSeparator, defaultFractionDigits,
120                                priceUnitFractionDigits, priceLineFractionDigits, costUnitFractionDigits, costLineFractionDigits, minusSign,
121                                isDefault, sortOrder, getPartyPK());
122                    } else {
123                        if(defaultFractionDigits == null) {
124                            addExecutionError(ExecutionErrors.MissingDefaultFractionDigits.name());
125                        }
126                        if(priceUnitFractionDigits == null) {
127                            addExecutionError(ExecutionErrors.MissingPriceUnitFractionDigits.name());
128                        }
129                        if(priceLineFractionDigits == null) {
130                            addExecutionError(ExecutionErrors.MissingPriceLineFractionDigits.name());
131                        }
132                        if(costUnitFractionDigits == null) {
133                            addExecutionError(ExecutionErrors.MissingCostUnitFractionDigits.name());
134                        }
135                        if(costLineFractionDigits == null) {
136                            addExecutionError(ExecutionErrors.MissingCostLineFractionDigits.name());
137                        }
138                    }
139                } else {
140                    if(symbolOnListStart == null) {
141                        addExecutionError(ExecutionErrors.MissingSymbolOnListStart.name());
142                    }
143                    if(symbolOnListMember == null) {
144                        addExecutionError(ExecutionErrors.MissingSymbolOnListMember.name());
145                    }
146                    if(symbolOnSubtotal == null) {
147                        addExecutionError(ExecutionErrors.MissingSymbolOnSubtotal.name());
148                    }
149                    if(symbolOnTotal == null) {
150                        addExecutionError(ExecutionErrors.MissingSymbolOnTotal.name());
151                    }
152                }
153            }
154        } else {
155            addExecutionError(ExecutionErrors.DuplicateCurrencyIsoName.name(), currencyIsoName);
156        }
157        
158        return null;
159    }
160    
161}