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