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.model.control.accounting.server.graphql;
018
019import com.echothree.model.control.accounting.server.control.AccountingControl;
020import com.echothree.model.control.graphql.server.graphql.BaseEntityInstanceObject;
021import com.echothree.model.control.graphql.server.util.BaseGraphQl;
022import com.echothree.model.control.user.server.control.UserControl;
023import com.echothree.model.data.accounting.server.entity.GlAccount;
024import com.echothree.model.data.accounting.server.entity.GlAccountDetail;
025import com.echothree.util.server.persistence.Session;
026import graphql.annotations.annotationTypes.GraphQLDescription;
027import graphql.annotations.annotationTypes.GraphQLField;
028import graphql.annotations.annotationTypes.GraphQLName;
029import graphql.annotations.annotationTypes.GraphQLNonNull;
030import graphql.schema.DataFetchingEnvironment;
031
032@GraphQLDescription("GL account object")
033@GraphQLName("GlAccount")
034public class GlAccountObject
035        extends BaseEntityInstanceObject {
036    
037    private final GlAccount glAccount; // Always Present
038    
039    public GlAccountObject(GlAccount glAccount) {
040        super(glAccount.getPrimaryKey());
041        
042        this.glAccount = glAccount;
043    }
044
045    private GlAccountDetail glAccountDetail; // Optional, use getGlAccountDetail()
046    
047    private GlAccountDetail getGlAccountDetail() {
048        if(glAccountDetail == null) {
049            glAccountDetail = glAccount.getLastDetail();
050        }
051        
052        return glAccountDetail;
053    }
054    
055    @GraphQLField
056    @GraphQLDescription("gl account name")
057    @GraphQLNonNull
058    public String getGlAccountName() {
059        return getGlAccountDetail().getGlAccountName();
060    }
061
062    @GraphQLField
063    @GraphQLDescription("parent gl account")
064    public GlAccountObject getParentGlAccount() {
065        GlAccount parentGlAccount = getGlAccountDetail().getParentGlAccount();
066        
067        return parentGlAccount == null ? null : new GlAccountObject(parentGlAccount);
068    }
069
070    @GraphQLField
071    @GraphQLDescription("GL account type")
072    public GlAccountTypeObject getGlAccountType(final DataFetchingEnvironment env) {
073        var glAccountType = getGlAccountDetail().getGlAccountType();
074
075        return glAccountType == null ? null : AccountingSecurityUtils.getHasGlAccountTypeAccess(env) ?
076                new GlAccountTypeObject(glAccountType) : null;
077    }
078
079    @GraphQLField
080    @GraphQLDescription("GL account class")
081    public GlAccountClassObject getGlAccountClass(final DataFetchingEnvironment env) {
082        var glAccountClass = getGlAccountDetail().getGlAccountClass();
083
084        return glAccountClass == null ? null : AccountingSecurityUtils.getHasGlAccountClassAccess(env) ?
085                new GlAccountClassObject(glAccountClass) : null;
086    }
087
088    @GraphQLField
089    @GraphQLDescription("GL account category")
090    public GlAccountCategoryObject getGlAccountCategory(final DataFetchingEnvironment env) {
091        var glAccountCategory = getGlAccountDetail().getGlAccountCategory();
092
093        return glAccountCategory == null ? null : AccountingSecurityUtils.getHasGlAccountCategoryAccess(env) ?
094                new GlAccountCategoryObject(glAccountCategory) : null;
095    }
096
097    @GraphQLField
098    @GraphQLDescription("GL account type")
099    public GlResourceTypeObject getGlResourceType(final DataFetchingEnvironment env) {
100        var glResourceType = getGlAccountDetail().getGlResourceType();
101
102        return glResourceType == null ? null : AccountingSecurityUtils.getHasGlResourceTypeAccess(env) ?
103                new GlResourceTypeObject(glResourceType) : null;
104    }
105
106    @GraphQLField
107    @GraphQLDescription("currency")
108    public CurrencyObject getCurrency(final DataFetchingEnvironment env) {
109        return AccountingSecurityUtils.getHasCurrencyAccess(env) ? new CurrencyObject(getGlAccountDetail().getCurrency()) : null;
110    }
111
112    @GraphQLField
113    @GraphQLDescription("is default")
114    public Boolean getIsDefault() {
115        return getGlAccountDetail().getIsDefault();
116    }
117    
118    @GraphQLField
119    @GraphQLDescription("description")
120    @GraphQLNonNull
121    public String getDescription(final DataFetchingEnvironment env) {
122        var accountingControl = Session.getModelController(AccountingControl.class);
123        var userControl = Session.getModelController(UserControl.class);
124
125        return accountingControl.getBestGlAccountDescription(glAccount, userControl.getPreferredLanguageFromUserVisit(BaseGraphQl.getUserVisit(env)));
126    }
127
128}