001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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;
038
039public class CreateGlAccountCommand
040        extends BaseSimpleCommand<CreateGlAccountForm> {
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.GlAccount.name(), SecurityRoles.Create.name())
050                ))
051        ));
052        
053        FORM_FIELD_DEFINITIONS = List.of(
054                new FieldDefinition("GlAccountName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("ParentGlAccountName", FieldType.ENTITY_NAME, false, null, null),
056                new FieldDefinition("GlAccountTypeName", FieldType.ENTITY_NAME, true, null, null),
057                new FieldDefinition("GlAccountClassName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null),
059                new FieldDefinition("GlResourceTypeName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("IsDefault", FieldType.BOOLEAN, false, null, null),
062                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
063        );
064    }
065    
066    /** Creates a new instance of CreateGlAccountCommand */
067    public CreateGlAccountCommand(UserVisitPK userVisitPK, CreateGlAccountForm form) {
068        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        var result = AccountingResultFactory.getCreateGlAccountResult();
074        var accountingControl = Session.getModelController(AccountingControl.class);
075        GlAccount glAccount = null;
076        var parentGlAccountName = form.getParentGlAccountName();
077        GlAccount parentGlAccount = null;
078
079        if(parentGlAccountName != null) {
080            parentGlAccount = accountingControl.getGlAccountByName(parentGlAccountName);
081        }
082
083        if(parentGlAccountName == null || parentGlAccount != null) {
084            var glAccountTypeName = form.getGlAccountTypeName();
085            var glAccountType = accountingControl.getGlAccountTypeByName(glAccountTypeName);
086
087            if(glAccountType != null) {
088                var glAccountClassName = form.getGlAccountClassName();
089                var glAccountClass = accountingControl.getGlAccountClassByName(glAccountClassName);
090
091                if(glAccountClass != null) {
092                    var glAccountCategoryName = form.getGlAccountCategoryName();
093                    var glAccountCategory = glAccountCategoryName == null? null: accountingControl.getGlAccountCategoryByName(glAccountCategoryName);
094
095                    if(glAccountCategoryName == null || glAccountCategory != null) {
096                        var glResourceTypeName = form.getGlResourceTypeName();
097                        var glResourceType = accountingControl.getGlResourceTypeByName(glResourceTypeName);
098
099                        if(glResourceType != null) {
100                            var currencyIsoName = form.getCurrencyIsoName();
101                            var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
102
103                            if(currency != null) {
104                                var glAccountName = form.getGlAccountName();
105                                var isDefault = glAccountCategory == null? null: Boolean.valueOf(form.getIsDefault());
106                                var description = form.getDescription();
107                                var partyPK = getPartyPK();
108
109                                glAccount = GlAccountLogic.getInstance().createGlAccount(this, glAccountName, parentGlAccount,
110                                        glAccountType, glAccountClass, glAccountCategory, glResourceType, currency, isDefault,
111                                        getPreferredLanguage(), description, partyPK);
112                            } else {
113                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
114                            }
115                        } else {
116                            addExecutionError(ExecutionErrors.UnknownGlResourceTypeName.name(), glResourceTypeName);
117                        }
118                    } else {
119                        addExecutionError(ExecutionErrors.UnknownGlAccountCategoryName.name(), glAccountCategoryName);
120                    }
121                } else {
122                    addExecutionError(ExecutionErrors.UnknownGlAccountClassName.name(), glAccountClassName);
123                }
124            } else {
125                addExecutionError(ExecutionErrors.UnknownGlAccountTypeName.name(), glAccountTypeName);
126            }
127        } else {
128            addExecutionError(ExecutionErrors.UnknownParentGlAccountName.name(), parentGlAccountName);
129        }
130
131        if(glAccount != null) {
132            result.setGlAccountName(glAccount.getLastDetail().getGlAccountName());
133            result.setEntityRef(glAccount.getPrimaryKey().getEntityRef());
134        }
135
136        return result;
137    }
138    
139}