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.sales.server.logic;
018
019import com.echothree.model.control.customer.common.exception.UnknownCustomerTypePaymentMethodException;
020import com.echothree.model.control.customer.server.control.CustomerControl;
021import com.echothree.model.control.order.server.logic.OrderLogic;
022import com.echothree.model.control.sales.common.exception.BillToPartyMustMatchPartyPaymentMethodsPartyException;
023import com.echothree.model.control.sales.common.exception.BillToRequiredWhenUsingPartyPaymentMethodException;
024import com.echothree.model.data.customer.server.entity.CustomerType;
025import com.echothree.model.data.order.server.entity.Order;
026import com.echothree.model.data.order.server.entity.OrderPaymentPreference;
027import com.echothree.model.data.party.common.pk.PartyPK;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.payment.server.entity.PartyPaymentMethod;
030import com.echothree.model.data.payment.server.entity.PaymentMethod;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.server.message.ExecutionErrorAccumulator;
033import com.echothree.util.server.persistence.Session;
034
035public class SalesOrderPaymentPreferenceLogic
036        extends OrderLogic {
037
038    private SalesOrderPaymentPreferenceLogic() {
039        super();
040    }
041
042    private static class LogicHolder {
043        static SalesOrderPaymentPreferenceLogic instance = new SalesOrderPaymentPreferenceLogic();
044    }
045
046    public static SalesOrderPaymentPreferenceLogic getInstance() {
047        return LogicHolder.instance;
048    }
049    
050    /**
051     * Verify that the CustomerType is authorized to use the PaymentMethod. If there are no CustomerTypePaymentMethods for any PaymentMethod,
052     * then it is assumed they're authorized.
053     * 
054     * @param eea Required.
055     * @param customerType Required.
056     * @param paymentMethod Required.
057     */
058    public void checkCustomerTypePaymentMethod(final ExecutionErrorAccumulator eea, final CustomerType customerType,
059            final PaymentMethod paymentMethod) {
060        var customerControl = Session.getModelController(CustomerControl.class);
061        
062        if(!customerControl.getCustomerTypePaymentMethodExists(customerType, paymentMethod)
063                && customerControl.countCustomerTypePaymentMethodsByCustomerType(customerType) != 0) {
064            handleExecutionError(UnknownCustomerTypePaymentMethodException.class, eea, ExecutionErrors.UnknownCustomerTypePaymentMethod.name(),
065                    customerType.getLastDetail().getCustomerTypeName(), paymentMethod.getLastDetail().getPaymentMethodName());
066        }
067    }
068    
069    /**
070     * Create an Order Payment Preference for a given Order.
071     * 
072     * @param eea Required.
073     * @param order Required.
074     * @param orderPaymentPreferenceSequence Optional.
075     * @param paymentMethod Required for all types except CREDIT_CARDs, GIFT_CARDs, GIFT_CERTIFICATEs.
076     * @param partyPaymentMethod Required for CREDIT_CARDs, GIFT_CARDs, GIFT_CERTIFICATEs, otherwise null.
077     * @param wasPresent Required for CREDIT_CARD, otherwise null.
078     * @param maximumAmount Optional.
079     * @param sortOrder Required.
080     * @param createdBy Required.
081     * @return The newly created OrderPaymentPreference, or null if there was an error.
082     */
083    public OrderPaymentPreference createSalesOrderPaymentPreference(final Session session, final ExecutionErrorAccumulator eea, final Order order,
084            final Integer orderPaymentPreferenceSequence, final PaymentMethod paymentMethod, final PartyPaymentMethod partyPaymentMethod,
085            final Boolean wasPresent, final Long maximumAmount, final Integer sortOrder, final PartyPK createdBy) {
086        var salesOrderLogic = SalesOrderLogic.getInstance();
087        OrderPaymentPreference orderPaymentPreference = null;
088        
089        salesOrderLogic.checkOrderAvailableForModification(session, eea, order, createdBy);
090        
091        if(eea == null || !eea.hasExecutionErrors()) {
092            Party billTo = salesOrderLogic.getOrderBillToParty(order);
093            CustomerType customerType = billTo == null ? null : salesOrderLogic.getCustomerTypeFromParty(billTo);
094
095            if(customerType != null) {
096                checkCustomerTypePaymentMethod(eea, customerType, paymentMethod);
097            }
098
099            if(eea == null || !eea.hasExecutionErrors()) {
100                if(partyPaymentMethod != null) {
101                    if(billTo == null) {
102                        // Order must have a bill to before a payment method that requires a partyPaymentMethod may be set.
103                        handleExecutionError(BillToRequiredWhenUsingPartyPaymentMethodException.class, eea, ExecutionErrors.BillToRequiredWhenUsingPartyPaymentMethod.name());
104                    } else if(!billTo.equals(partyPaymentMethod.getLastDetail().getParty())) {
105                        // Verify partyPaymentMethod belongs to the BILL_TO Party on the order.
106                        handleExecutionError(BillToPartyMustMatchPartyPaymentMethodsPartyException.class, eea, ExecutionErrors.BillToPartyMustMatchPartyPaymentMethodsParty.name());
107                    }
108                }
109
110                if(eea == null || !eea.hasExecutionErrors()) {
111                    orderPaymentPreference = OrderLogic.getInstance().createOrderPaymentPreference(session, eea, order, orderPaymentPreferenceSequence, paymentMethod,
112                            partyPaymentMethod, wasPresent, maximumAmount, sortOrder, createdBy);
113                }
114            }
115        }
116        
117        return orderPaymentPreference;
118    }
119
120}