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.CreateGlAccountForm; 020import com.echothree.control.user.accounting.common.result.AccountingResultFactory; 021import com.echothree.model.control.accounting.server.control.AccountingControl; 022import com.echothree.model.control.accounting.server.logic.GlAccountLogic; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.security.common.SecurityRoleGroups; 025import com.echothree.model.control.security.common.SecurityRoles; 026import com.echothree.model.data.accounting.server.entity.GlAccount; 027import com.echothree.model.data.user.common.pk.UserVisitPK; 028import com.echothree.util.common.command.BaseResult; 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.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class CreateGlAccountCommand 042 extends BaseSimpleCommand<CreateGlAccountForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 051 new SecurityRoleDefinition(SecurityRoleGroups.GlAccount.name(), SecurityRoles.Create.name()) 052 )) 053 )); 054 055 FORM_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null), 057 new FieldDefinition("ParentGlAccountName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("GlAccountTypeName", FieldType.ENTITY_NAME, true, null, null), 059 new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("GlResourceTypeName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("IsDefault", FieldType.BOOLEAN, false, null, null), 064 new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L) 065 ); 066 } 067 068 @Inject 069 AccountingControl accountingControl; 070 071 @Inject 072 GlAccountLogic glAccountLogic; 073 074 075 /** Creates a new instance of CreateGlAccountCommand */ 076 public CreateGlAccountCommand() { 077 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 078 } 079 080 @Override 081 protected BaseResult execute() { 082 var result = AccountingResultFactory.getCreateGlAccountResult(); 083 GlAccount glAccount = null; 084 var parentGlAccountName = form.getParentGlAccountName(); 085 GlAccount parentGlAccount = null; 086 087 if(parentGlAccountName != null) { 088 parentGlAccount = accountingControl.getGlAccountByName(parentGlAccountName); 089 } 090 091 if(parentGlAccountName == null || parentGlAccount != null) { 092 var glAccountTypeName = form.getGlAccountTypeName(); 093 var glAccountType = accountingControl.getGlAccountTypeByName(glAccountTypeName); 094 095 if(glAccountType != null) { 096 var glAccountClassName = form.getGlAccountClassName(); 097 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 098 099 if(glAccountClass != null) { 100 var glAccountCategoryName = form.getGlAccountCategoryName(); 101 var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 102 103 if(glAccountCategoryName == null || glAccountCategory != null) { 104 var glResourceTypeName = form.getGlResourceTypeName(); 105 var glResourceType = accountingControl.getGlResourceTypeByName(glResourceTypeName); 106 107 if(glResourceType != null) { 108 var currencyIsoName = form.getCurrencyIsoName(); 109 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 110 111 if(currency != null) { 112 var glAccountName = form.getGlAccountName(); 113 var isDefault = glAccountCategory == null? null: Boolean.valueOf(form.getIsDefault()); 114 var description = form.getDescription(); 115 var partyPK = getPartyPK(); 116 117 glAccount = glAccountLogic.createGlAccount(this, glAccountName, parentGlAccount, 118 glAccountType, glAccountClass, glAccountCategory, glResourceType, currency, isDefault, 119 getPreferredLanguage(), description, partyPK); 120 } else { 121 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownGlResourceTypeName.name(), glResourceTypeName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 131 } 132 } else { 133 addExecutionError(ExecutionErrors.UnknownGlAccountTypeName.name(), glAccountTypeName); 134 } 135 } else { 136 addExecutionError(ExecutionErrors.UnknownParentGlAccountName.name(), parentGlAccountName); 137 } 138 139 if(glAccount != null) { 140 result.setGlAccountName(glAccount.getLastDetail().getGlAccountName()); 141 result.setEntityRef(glAccount.getPrimaryKey().getEntityRef()); 142 } 143 144 return result; 145 } 146 147}