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