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.model.control.customer.server.graphql;
018
019import com.echothree.model.control.accounting.server.graphql.AccountingSecurityUtils;
020import com.echothree.model.control.accounting.server.graphql.GlAccountObject;
021import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicyObject;
022import com.echothree.model.control.cancellationpolicy.server.graphql.CancellationPolicySecurityUtils;
023import com.echothree.model.control.customer.server.control.CustomerControl;
024import com.echothree.model.control.offer.server.graphql.OfferSecurityUtils;
025import com.echothree.model.control.offer.server.graphql.OfferUseObject;
026import com.echothree.model.control.party.server.graphql.BasePartyObject;
027import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicyObject;
028import com.echothree.model.control.returnpolicy.server.graphql.ReturnPolicySecurityUtils;
029import com.echothree.model.data.customer.server.entity.Customer;
030import com.echothree.model.data.party.server.entity.Party;
031import com.echothree.util.server.persistence.Session;
032import graphql.annotations.annotationTypes.GraphQLDescription;
033import graphql.annotations.annotationTypes.GraphQLField;
034import graphql.annotations.annotationTypes.GraphQLName;
035import graphql.annotations.annotationTypes.GraphQLNonNull;
036import graphql.schema.DataFetchingEnvironment;
037
038@GraphQLDescription("customer object")
039@GraphQLName("Customer")
040public class CustomerObject
041        extends BasePartyObject {
042
043    public CustomerObject(Party party) {
044        super(party);
045    }
046
047    public CustomerObject(Customer customer) {
048        super(customer.getParty());
049
050        this.customer = customer;
051    }
052
053    private Customer customer;  // Optional, use getCustomer()
054
055    protected Customer getCustomer() {
056        if(customer == null) {
057            var customerControl = Session.getModelController(CustomerControl.class);
058
059            customer = customerControl.getCustomer(party);
060        }
061
062        return customer;
063    }
064
065    @GraphQLField
066    @GraphQLDescription("customer name")
067    @GraphQLNonNull
068    public String getCustomerName() {
069        return getCustomer().getCustomerName();
070    }
071
072    @GraphQLField
073    @GraphQLDescription("customer type")
074    public CustomerTypeObject getCustomerType(final DataFetchingEnvironment env) {
075        return CustomerSecurityUtils.getHasCustomerTypeAccess(env) ?
076                new CustomerTypeObject(getCustomer().getCustomerType()) : null;
077    }
078
079    @GraphQLField
080    @GraphQLDescription("initial offer use")
081    public OfferUseObject getInitialOfferUse(final DataFetchingEnvironment env) {
082        return OfferSecurityUtils.getHasOfferUseAccess(env) ?
083                new OfferUseObject(getCustomer().getInitialOfferUse()) : null;
084    }
085
086    @GraphQLField
087    @GraphQLDescription("cancellation policy")
088    public CancellationPolicyObject getCancellationPolicy(final DataFetchingEnvironment env) {
089        var cancellationPolicy = getCustomer().getCancellationPolicy();
090
091        return cancellationPolicy == null ? null : CancellationPolicySecurityUtils.getHasCancellationPolicyAccess(env) ?
092                new CancellationPolicyObject(cancellationPolicy) : null;
093    }
094
095    @GraphQLField
096    @GraphQLDescription("return policy")
097    public ReturnPolicyObject getReturnPolicy(final DataFetchingEnvironment env) {
098        var returnPolicy = getCustomer().getReturnPolicy();
099
100        return returnPolicy == null ? null : ReturnPolicySecurityUtils.getHasReturnPolicyAccess(env) ?
101                new ReturnPolicyObject(returnPolicy) : null;
102    }
103
104    @GraphQLField
105    @GraphQLDescription("AR GL account")
106    public GlAccountObject getArGlAccount(final DataFetchingEnvironment env) {
107        var arGlAccount = getCustomer().getArGlAccount();
108
109        return arGlAccount == null ? null : AccountingSecurityUtils.getHasGlAccountAccess(env) ?
110                new GlAccountObject(getCustomer().getArGlAccount()) : null;
111    }
112
113    @GraphQLField
114    @GraphQLDescription("hold until complete")
115    @GraphQLNonNull
116    public boolean getHoldUntilComplete() {
117        return getCustomer().getHoldUntilComplete();
118    }
119
120    @GraphQLField
121    @GraphQLDescription("allow backorders")
122    @GraphQLNonNull
123    public boolean getAllowBackorders() {
124        return getCustomer().getAllowBackorders();
125    }
126
127    @GraphQLField
128    @GraphQLDescription("allow substitutions")
129    @GraphQLNonNull
130    public boolean getAllowSubstitutions() {
131        return getCustomer().getAllowSubstitutions();
132    }
133
134    @GraphQLField
135    @GraphQLDescription("allow combining shipments")
136    @GraphQLNonNull
137    public boolean getAllowCombiningShipments() {
138        return getCustomer().getAllowCombiningShipments();
139    }
140
141    @GraphQLField
142    @GraphQLDescription("require reference")
143    @GraphQLNonNull
144    public boolean getRequireReference() {
145        return getCustomer().getRequireReference();
146    }
147
148    @GraphQLField
149    @GraphQLDescription("allow reference duplicates")
150    @GraphQLNonNull
151    public boolean getAllowReferenceDuplicates() {
152        return getCustomer().getAllowReferenceDuplicates();
153    }
154
155    @GraphQLField
156    @GraphQLDescription("reference validation pattern")
157    public String getReferenceValidationPattern() {
158        return getCustomer().getReferenceValidationPattern();
159    }
160
161}