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.CreateTransactionGlAccountCategoryForm;
020import com.echothree.control.user.accounting.common.result.AccountingResultFactory;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
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.accounting.server.entity.TransactionGlAccountCategory;
026import com.echothree.model.data.user.common.pk.UserVisitPK;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSimpleCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import com.echothree.util.server.control.SecurityRoleDefinition;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class CreateTransactionGlAccountCategoryCommand
043        extends BaseSimpleCommand<CreateTransactionGlAccountCategoryForm> {
044    
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.TransactionGlAccountCategory.name(), SecurityRoles.Create.name())
053                ))
054        ));
055        
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("TransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("TransactionGlAccountCategoryName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null),
060                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
061                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
063        );
064    }
065
066    @Inject
067    AccountingControl accountingControl;
068
069    
070    /** Creates a new instance of CreateTransactionGlAccountCategoryCommand */
071    public CreateTransactionGlAccountCategoryCommand() {
072        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
073    }
074    
075    @Override
076    protected BaseResult execute() {
077        var result = AccountingResultFactory.getCreateTransactionGlAccountCategoryResult();
078        TransactionGlAccountCategory transactionGlAccountCategory = null;
079        var transactionTypeName = form.getTransactionTypeName();
080        var transactionType = accountingControl.getTransactionTypeByName(transactionTypeName);
081        
082        if(transactionType != null) {
083            var transactionGlAccountCategoryName = form.getTransactionGlAccountCategoryName();
084
085            transactionGlAccountCategory = accountingControl.getTransactionGlAccountCategoryByName(transactionType, transactionGlAccountCategoryName);
086            
087            if(transactionGlAccountCategory == null) {
088                var glAccountCategoryName = form.getGlAccountCategoryName();
089                var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
090                
091                if(glAccountCategoryName == null || glAccountCategory != null) {
092                    var glAccountName = form.getGlAccountName();
093                    var glAccount = glAccountName == null? null: accountingControl.getGlAccountByName(glAccountName);
094
095                    if(glAccountName == null || glAccount != null) {
096                        if(glAccountCategory == null || glAccount == null? true: glAccountCategory.equals(glAccount.getLastDetail().getGlAccountCategory())) {
097                            var sortOrder = Integer.valueOf(form.getSortOrder());
098                            var description = form.getDescription();
099                            var partyPK = getPartyPK();
100
101                            transactionGlAccountCategory = accountingControl.createTransactionGlAccountCategory(transactionType, transactionGlAccountCategoryName, glAccountCategory, sortOrder, partyPK);
102                            
103                            if(glAccount != null) {
104                                accountingControl.createTransactionGlAccount(transactionGlAccountCategory, glAccount, partyPK);
105                            }
106                            
107                            if(description != null) {
108                                accountingControl.createTransactionGlAccountCategoryDescription(transactionGlAccountCategory, getPreferredLanguage(), description, partyPK);
109                            }
110                        } else {
111                            addExecutionError(ExecutionErrors.InvalidGlAccountCategory.name());
112                        }
113                    } else {
114                        addExecutionError(ExecutionErrors.UnknownGlAccountName.name(), glAccountName);
115                    }
116                } else {
117                    addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
118                }
119            } else {
120                addExecutionError(ExecutionErrors.DuplicateTransactionGlAccountCategoryName.name(), transactionGlAccountCategoryName);
121            }
122        } else {
123            addExecutionError(ExecutionErrors.UnknownTransactionTypeName.name(), transactionTypeName);
124        }
125        
126        if(transactionGlAccountCategory != null) {
127            result.setEntityRef(transactionGlAccountCategory.getPrimaryKey().getEntityRef());
128            result.setTransactionTypeName(transactionGlAccountCategory.getLastDetail().getTransactionType().getLastDetail().getTransactionTypeName());
129            result.setTransactionGlAccountCategoryName(transactionGlAccountCategory.getLastDetail().getTransactionGlAccountCategoryName());
130        }
131
132        return result;
133    }
134    
135}