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