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.GetGlAccountsForm; 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.GlAccountCategoryLogic; 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.accounting.server.entity.GlAccountCategory; 028import com.echothree.model.data.user.common.pk.UserVisitPK; 029import com.echothree.util.common.command.BaseResult; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 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.Collection; 038import java.util.List; 039import javax.enterprise.context.RequestScoped; 040 041@RequestScoped 042public class GetGlAccountsCommand 043 extends BasePaginatedMultipleEntitiesCommand<GlAccount, GetGlAccountsForm> { 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.GlAccount.name(), SecurityRoles.List.name()) 053 )) 054 )); 055 056 FORM_FIELD_DEFINITIONS = List.of( 057 new FieldDefinition("GlAccountCategoryName", FieldType.ENTITY_NAME, false, null, null) 058 ); 059 } 060 061 /** Creates a new instance of GetGlAccountsCommand */ 062 public GetGlAccountsCommand() { 063 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 064 } 065 066 GlAccountCategory glAccountCategory = null; 067 068 @Override 069 protected void handleForm() { 070 var glAccountCategoryName = form.getGlAccountCategoryName(); 071 072 if(glAccountCategoryName != null) { 073 glAccountCategory = GlAccountCategoryLogic.getInstance().getGlAccountCategoryByName(this, glAccountCategoryName); 074 } 075 } 076 077 @Override 078 protected Long getTotalEntities() { 079 var accountingControl = Session.getModelController(AccountingControl.class); 080 081 return hasExecutionErrors() ? null : 082 glAccountCategory == null ? 083 accountingControl.countGlAccounts() : 084 accountingControl.countGlAccountsByGlAccountCategory(glAccountCategory); 085 } 086 087 @Override 088 protected Collection<GlAccount> getEntities() { 089 Collection<GlAccount> glAccounts = null; 090 091 if(!hasExecutionErrors()) { 092 var accountingControl = Session.getModelController(AccountingControl.class); 093 094 glAccounts = glAccountCategory == null ? 095 accountingControl.getGlAccounts() : 096 accountingControl.getGlAccountsByGlAccountCategory(glAccountCategory); 097 } 098 099 return glAccounts; 100 } 101 102 @Override 103 protected BaseResult getResult(Collection<GlAccount> entities) { 104 var result = AccountingResultFactory.getGetGlAccountsResult(); 105 106 if(entities != null) { 107 var accountingControl = Session.getModelController(AccountingControl.class); 108 109 if(glAccountCategory == null) { 110 result.setGlAccountCount(accountingControl.countGlAccounts()); 111 result.setGlAccounts(accountingControl.getGlAccountTransfers(getUserVisit(), entities)); 112 } else { 113 var userVisit = getUserVisit(); 114 115 result.setGlAccountCount(accountingControl.countGlAccountsByGlAccountCategory(glAccountCategory)); 116 result.setGlAccountCategory(accountingControl.getGlAccountCategoryTransfer(userVisit, glAccountCategory)); 117 result.setGlAccounts(accountingControl.getGlAccountTransfers(userVisit, entities)); 118 } 119 } 120 121 return result; 122 } 123 124}