001// -------------------------------------------------------------------------------- 002// Copyright 2002-2025 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 com.echothree.util.server.persistence.Session; 037import java.util.List; 038import javax.enterprise.context.RequestScoped; 039 040@RequestScoped 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 /** Creates a new instance of CreateGlAccountCommand */ 069 public CreateGlAccountCommand() { 070 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 071 } 072 073 @Override 074 protected BaseResult execute() { 075 var result = AccountingResultFactory.getCreateGlAccountResult(); 076 var accountingControl = Session.getModelController(AccountingControl.class); 077 GlAccount glAccount = null; 078 var parentGlAccountName = form.getParentGlAccountName(); 079 GlAccount parentGlAccount = null; 080 081 if(parentGlAccountName != null) { 082 parentGlAccount = accountingControl.getGlAccountByName(parentGlAccountName); 083 } 084 085 if(parentGlAccountName == null || parentGlAccount != null) { 086 var glAccountTypeName = form.getGlAccountTypeName(); 087 var glAccountType = accountingControl.getGlAccountTypeByName(glAccountTypeName); 088 089 if(glAccountType != null) { 090 var glAccountClassName = form.getGlAccountClassName(); 091 var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName); 092 093 if(glAccountClass != null) { 094 var glAccountCategoryName = form.getGlAccountCategoryName(); 095 var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName); 096 097 if(glAccountCategoryName == null || glAccountCategory != null) { 098 var glResourceTypeName = form.getGlResourceTypeName(); 099 var glResourceType = accountingControl.getGlResourceTypeByName(glResourceTypeName); 100 101 if(glResourceType != null) { 102 var currencyIsoName = form.getCurrencyIsoName(); 103 var currency = accountingControl.getCurrencyByIsoName(currencyIsoName); 104 105 if(currency != null) { 106 var glAccountName = form.getGlAccountName(); 107 var isDefault = glAccountCategory == null? null: Boolean.valueOf(form.getIsDefault()); 108 var description = form.getDescription(); 109 var partyPK = getPartyPK(); 110 111 glAccount = GlAccountLogic.getInstance().createGlAccount(this, glAccountName, parentGlAccount, 112 glAccountType, glAccountClass, glAccountCategory, glResourceType, currency, isDefault, 113 getPreferredLanguage(), description, partyPK); 114 } else { 115 addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownGlResourceTypeName.name(), glResourceTypeName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownGlAccountTypeName.name(), glAccountTypeName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownParentGlAccountName.name(), parentGlAccountName); 131 } 132 133 if(glAccount != null) { 134 result.setGlAccountName(glAccount.getLastDetail().getGlAccountName()); 135 result.setEntityRef(glAccount.getPrimaryKey().getEntityRef()); 136 } 137 138 return result; 139 } 140 141}