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 com.echothree.util.server.persistence.Session; 035import java.util.List; 036import javax.enterprise.context.Dependent; 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 /** Creates a new instance of CreateCurrencyCommand */ 076 public CreateCurrencyCommand() { 077 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var accountingControl = Session.getModelController(AccountingControl.class); 083 var currencyIsoName = form.getCurrencyIsoName(); 084 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 085 086 if(currency == null) { 087 var symbolPosition = SymbolPositionLogic.getInstance().getSymbolPositionByName(this, form.getSymbolPositionName()); 088 089 if(!hasExecutionErrors()) { 090 var groupingSeparator = form.getGroupingSeparator(); 091 var groupingSize = Integer.valueOf(form.getGroupingSize()); 092 var minusSign = form.getMinusSign(); 093 var isDefault = Boolean.valueOf(form.getIsDefault()); 094 var sortOrder = Integer.valueOf(form.getSortOrder()); 095 var symbol = form.getSymbol(); 096 var symbolOnListStart = symbol == null? null: Boolean.valueOf(form.getSymbolOnListStart()); 097 var symbolOnListMember = symbol == null? null: Boolean.valueOf(form.getSymbolOnListMember()); 098 var symbolOnSubtotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnSubtotal()); 099 var symbolOnTotal = symbol == null? null: Boolean.valueOf(form.getSymbolOnTotal()); 100 101 if(symbol == null || (symbol != null && symbolOnListStart != null && symbolOnListMember != null 102 && symbolOnSubtotal != null && symbolOnTotal != null)) { 103 var fractionSeparator = form.getFractionSeparator(); 104 var defaultFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getDefaultFractionDigits()); 105 var priceUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceUnitFractionDigits()); 106 var priceLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getPriceLineFractionDigits()); 107 var costUnitFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostUnitFractionDigits()); 108 var costLineFractionDigits = fractionSeparator == null? null: Integer.valueOf(form.getCostLineFractionDigits()); 109 110 if(fractionSeparator == null || (defaultFractionDigits != null && priceUnitFractionDigits != null 111 && priceLineFractionDigits != null && costUnitFractionDigits != null && costLineFractionDigits != null)) { 112 accountingControl.createCurrency(currencyIsoName, symbol, symbolPosition, symbolOnListStart, symbolOnListMember, 113 symbolOnSubtotal, symbolOnTotal, groupingSeparator, groupingSize, fractionSeparator, defaultFractionDigits, 114 priceUnitFractionDigits, priceLineFractionDigits, costUnitFractionDigits, costLineFractionDigits, minusSign, 115 isDefault, sortOrder, getPartyPK()); 116 } else { 117 if(defaultFractionDigits == null) { 118 addExecutionError(ExecutionErrors.MissingDefaultFractionDigits.name()); 119 } 120 if(priceUnitFractionDigits == null) { 121 addExecutionError(ExecutionErrors.MissingPriceUnitFractionDigits.name()); 122 } 123 if(priceLineFractionDigits == null) { 124 addExecutionError(ExecutionErrors.MissingPriceLineFractionDigits.name()); 125 } 126 if(costUnitFractionDigits == null) { 127 addExecutionError(ExecutionErrors.MissingCostUnitFractionDigits.name()); 128 } 129 if(costLineFractionDigits == null) { 130 addExecutionError(ExecutionErrors.MissingCostLineFractionDigits.name()); 131 } 132 } 133 } else { 134 if(symbolOnListStart == null) { 135 addExecutionError(ExecutionErrors.MissingSymbolOnListStart.name()); 136 } 137 if(symbolOnListMember == null) { 138 addExecutionError(ExecutionErrors.MissingSymbolOnListMember.name()); 139 } 140 if(symbolOnSubtotal == null) { 141 addExecutionError(ExecutionErrors.MissingSymbolOnSubtotal.name()); 142 } 143 if(symbolOnTotal == null) { 144 addExecutionError(ExecutionErrors.MissingSymbolOnTotal.name()); 145 } 146 } 147 } 148 } else { 149 addExecutionError(ExecutionErrors.DuplicateCurrencyIsoName.name(), currencyIsoName); 150 } 151 152 return null; 153 } 154 155}