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.order.server.logic; 018 019import com.echothree.model.control.order.common.exception.CannotSpecifyPartyPaymentMethodException; 020import com.echothree.model.control.order.common.exception.CannotSpecifyPaymentMethodAndPartyPaymentMethodException; 021import com.echothree.model.control.order.common.exception.CannotSpecifyWasPresentException; 022import com.echothree.model.control.order.common.exception.DuplicateOrderPaymentPreferenceSequenceException; 023import com.echothree.model.control.order.common.exception.MustSpecifyPartyPaymentMethodException; 024import com.echothree.model.control.order.common.exception.MustSpecifyPaymentMethodOrPartyPaymentMethodException; 025import com.echothree.model.control.order.common.exception.MustSpecifyWasPresentException; 026import com.echothree.model.control.order.common.exception.UnknownOrderAliasTypeNameException; 027import com.echothree.model.control.order.common.exception.UnknownOrderNameException; 028import com.echothree.model.control.order.common.exception.UnknownOrderPaymentPreferenceSequenceException; 029import com.echothree.model.control.order.common.exception.UnknownOrderRoleTypeNameException; 030import com.echothree.model.control.order.common.exception.UnknownOrderSequenceException; 031import com.echothree.model.control.order.common.exception.UnknownOrderSequenceTypeException; 032import com.echothree.model.control.order.common.exception.UnknownOrderTypeNameException; 033import com.echothree.model.control.order.server.control.OrderAliasControl; 034import com.echothree.model.control.order.server.control.OrderControl; 035import com.echothree.model.control.order.server.control.OrderLineControl; 036import com.echothree.model.control.order.server.control.OrderPaymentPreferenceControl; 037import com.echothree.model.control.order.server.control.OrderRoleControl; 038import com.echothree.model.control.order.server.control.OrderTypeControl; 039import com.echothree.model.control.payment.common.PaymentMethodTypes; 040import com.echothree.model.control.payment.server.logic.PaymentMethodLogic; 041import com.echothree.model.control.sequence.server.control.SequenceControl; 042import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic; 043import com.echothree.model.control.shipping.server.logic.ShippingMethodLogic; 044import com.echothree.model.data.accounting.server.entity.Currency; 045import com.echothree.model.data.cancellationpolicy.server.entity.CancellationPolicy; 046import com.echothree.model.data.item.server.entity.Item; 047import com.echothree.model.data.order.server.entity.Order; 048import com.echothree.model.data.order.server.entity.OrderAliasType; 049import com.echothree.model.data.order.server.entity.OrderPaymentPreference; 050import com.echothree.model.data.order.server.entity.OrderPriority; 051import com.echothree.model.data.order.server.entity.OrderRoleType; 052import com.echothree.model.data.order.server.entity.OrderShipmentGroup; 053import com.echothree.model.data.order.server.entity.OrderType; 054import com.echothree.model.data.payment.server.entity.PartyPaymentMethod; 055import com.echothree.model.data.payment.server.entity.PaymentMethod; 056import com.echothree.model.data.returnpolicy.server.entity.ReturnPolicy; 057import com.echothree.model.data.sequence.server.entity.Sequence; 058import com.echothree.model.data.sequence.server.entity.SequenceType; 059import com.echothree.model.data.shipment.server.entity.FreeOnBoard; 060import com.echothree.model.data.term.server.entity.Term; 061import com.echothree.util.common.message.ExecutionErrors; 062import com.echothree.util.common.persistence.BasePK; 063import com.echothree.util.server.control.BaseLogic; 064import com.echothree.util.server.message.ExecutionErrorAccumulator; 065import com.echothree.util.server.persistence.EntityPermission; 066import com.echothree.util.server.persistence.Session; 067import java.util.HashSet; 068import java.util.Set; 069import javax.enterprise.context.ApplicationScoped; 070import javax.enterprise.inject.spi.CDI; 071 072public class BaseOrderLogic 073 extends BaseLogic { 074 075 protected BaseOrderLogic() { 076 super(); 077 } 078 079 public Currency getOrderCurrency(final Order order) { 080 return order.getLastDetail().getCurrency(); 081 } 082 083 public OrderType getOrderTypeByName(final ExecutionErrorAccumulator eea, final String orderTypeName) { 084 var orderTypeControl = Session.getModelController(OrderTypeControl.class); 085 var orderType = orderTypeControl.getOrderTypeByName(orderTypeName); 086 087 if(orderType == null) { 088 handleExecutionError(UnknownOrderTypeNameException.class, eea, ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName); 089 } 090 091 return orderType; 092 } 093 094 public OrderRoleType getOrderRoleTypeByName(final ExecutionErrorAccumulator eea, final String orderRoleTypeName) { 095 var orderRoleControl = Session.getModelController(OrderRoleControl.class); 096 var orderRoleType = orderRoleControl.getOrderRoleTypeByName(orderRoleTypeName); 097 098 if(orderRoleType == null) { 099 handleExecutionError(UnknownOrderRoleTypeNameException.class, eea, ExecutionErrors.UnknownOrderRoleTypeName.name(), orderRoleTypeName); 100 } 101 102 return orderRoleType; 103 } 104 105 public SequenceType getOrderSequenceType(final ExecutionErrorAccumulator eea, final OrderType orderType) { 106 var orderTypeDetail = orderType.getLastDetail(); 107 var sequenceType = orderTypeDetail.getOrderSequenceType(); 108 109 if(sequenceType == null) { 110 var sequenceControl = Session.getModelController(SequenceControl.class); 111 112 sequenceType = sequenceControl.getDefaultSequenceType(); 113 } 114 115 if(sequenceType == null) { 116 handleExecutionError(UnknownOrderSequenceTypeException.class, eea, ExecutionErrors.UnknownOrderSequenceType.name(), orderType.getLastDetail().getOrderTypeName()); 117 } 118 119 return sequenceType; 120 } 121 122 public Sequence getOrderSequence(final ExecutionErrorAccumulator eea, final OrderType orderType) { 123 var sequenceType = getOrderSequenceType(eea, orderType); 124 Sequence sequence = null; 125 126 if(eea == null || !eea.hasExecutionErrors()) { 127 var sequenceControl = Session.getModelController(SequenceControl.class); 128 129 sequence = sequenceControl.getDefaultSequence(sequenceType); 130 } 131 132 if(sequence == null) { 133 handleExecutionError(UnknownOrderSequenceException.class, eea, ExecutionErrors.UnknownOrderSequence.name(), orderType.getLastDetail().getOrderTypeName()); 134 } 135 136 return sequence; 137 } 138 139 public String getOrderName(final ExecutionErrorAccumulator eea, final OrderType orderType, Sequence sequence) { 140 String orderName = null; 141 142 if(sequence == null) { 143 sequence = getOrderSequence(eea, orderType); 144 } 145 146 if(eea == null || !eea.hasExecutionErrors()) { 147 orderName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(sequence); 148 } 149 150 return orderName; 151 } 152 153 public Order createOrder(final ExecutionErrorAccumulator eea, OrderType orderType, Sequence sequence, final OrderPriority orderPriority, 154 final Currency currency, final Boolean holdUntilComplete, final Boolean allowBackorders, final Boolean allowSubstitutions, 155 final Boolean allowCombiningShipments, final Term term, final FreeOnBoard freeOnBoard, final String reference, final String description, 156 final CancellationPolicy cancellationPolicy, final ReturnPolicy returnPolicy, final Boolean taxable, final BasePK createdBy) { 157 var orderName = getOrderName(eea, orderType, sequence); 158 Order order = null; 159 160 if(eea == null || !eea.hasExecutionErrors()) { 161 var orderControl = Session.getModelController(OrderControl.class); 162 163 order = orderControl.createOrder(orderType, orderName, orderPriority, currency, holdUntilComplete, allowBackorders, allowSubstitutions, 164 allowCombiningShipments, term, freeOnBoard, reference, description, cancellationPolicy, returnPolicy, taxable, createdBy); 165 } 166 167 return order; 168 } 169 170 public OrderAliasType getOrderAliasTypeByName(final ExecutionErrorAccumulator eea, final OrderType orderType, final String orderAliasTypeName) { 171 var orderAliasControl = Session.getModelController(OrderAliasControl.class); 172 var orderAliasType = orderAliasControl.getOrderAliasTypeByName(orderType, orderAliasTypeName); 173 174 if(orderAliasType == null) { 175 handleExecutionError(UnknownOrderAliasTypeNameException.class, eea, ExecutionErrors.UnknownOrderAliasTypeName.name(), 176 orderType.getLastDetail().getOrderTypeName(), orderAliasTypeName); 177 } 178 179 return orderAliasType; 180 } 181 182 private Order getOrderByName(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName, 183 final EntityPermission entityPermission) { 184 var orderControl = Session.getModelController(OrderControl.class); 185 var orderType = getOrderTypeByName(eea, orderTypeName); 186 Order order = null; 187 188 if(eea == null || !eea.hasExecutionErrors()) { 189 order = orderControl.getOrderByName(orderType, orderName, entityPermission); 190 191 if(order == null) { 192 handleExecutionError(UnknownOrderNameException.class, eea, ExecutionErrors.UnknownOrderName.name(), orderTypeName, orderName); 193 } 194 } 195 196 return order; 197 } 198 199 public Order getOrderByName(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName) { 200 return getOrderByName(eea, orderTypeName, orderName, EntityPermission.READ_ONLY); 201 } 202 203 public Order getOrderByNameForUpdate(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName) { 204 return getOrderByName(eea, orderTypeName, orderName, EntityPermission.READ_WRITE); 205 } 206 207 public Set<Item> getItemsFromOrder(final Order order) { 208 var orderLineControl = Session.getModelController(OrderLineControl.class); 209 var orderLines = orderLineControl.getOrderLinesByOrder(order); 210 var items = new HashSet<Item>(orderLines.size()); 211 212 orderLines.forEach((orderLine) -> { 213 items.add(orderLine.getLastDetail().getItem()); 214 }); 215 216 return items; 217 } 218 219 public OrderPaymentPreference createOrderPaymentPreference(final Session session, final ExecutionErrorAccumulator eea, final Order order, 220 Integer orderPaymentPreferenceSequence, PaymentMethod paymentMethod, final PartyPaymentMethod partyPaymentMethod, 221 final Boolean wasPresent, final Long maximumAmount, final Integer sortOrder, final BasePK createdBy) { 222 var parameterCount = (paymentMethod == null ? 0 : 1) + (partyPaymentMethod == null ? 0 : 1); 223 OrderPaymentPreference orderPaymentPreference = null; 224 225 // Either the paymentMethod or the partyPaymentMethod must be specified. 226 if(parameterCount == 1) { 227 String paymentMethodTypeName; 228 229 if(paymentMethod == null) { 230 paymentMethod = partyPaymentMethod.getLastDetail().getPaymentMethod(); 231 } 232 233 paymentMethodTypeName = paymentMethod.getLastDetail().getPaymentMethodType().getLastDetail().getPaymentMethodTypeName(); 234 235 // If the type is CREDIT_CARD, GIFT_CARD, or GIFT_CERTIFICATE, then the partyPaymentMethod must be specified. 236 if((paymentMethodTypeName.equals(PaymentMethodTypes.CREDIT_CARD.name()) 237 || paymentMethodTypeName.equals(PaymentMethodTypes.GIFT_CARD.name()) 238 || paymentMethodTypeName.equals(PaymentMethodTypes.GIFT_CERTIFICATE.name())) && partyPaymentMethod == null) { 239 handleExecutionError(MustSpecifyPartyPaymentMethodException.class, eea, ExecutionErrors.MustSpecifyPartyPaymentMethod.name()); 240 } else if(partyPaymentMethod != null) { 241 // Otherwise, the partyPaymentMethod should always be null. 242 handleExecutionError(CannotSpecifyPartyPaymentMethodException.class, eea, ExecutionErrors.CannotSpecifyPartyPaymentMethod.name()); 243 } 244 245 // If the type is CREDIT_CARD then wasPresent must be specified. 246 if(paymentMethodTypeName.equals(PaymentMethodTypes.CREDIT_CARD.name()) && wasPresent == null) { 247 handleExecutionError(MustSpecifyWasPresentException.class, eea, ExecutionErrors.MustSpecifyWasPresent.name()); 248 } else if(wasPresent != null) { 249 // Otherwise, the partyPaymentMethod should always be null. 250 handleExecutionError(CannotSpecifyWasPresentException.class, eea, ExecutionErrors.CannotSpecifyWasPresent.name()); 251 } 252 253 if(eea == null || !eea.hasExecutionErrors()) { 254 PaymentMethodLogic.getInstance().checkAcceptanceOfItems(session, eea, paymentMethod, getItemsFromOrder(order), createdBy); 255 256 if(eea == null || !eea.hasExecutionErrors()) { 257 var orderPaymentPreferenceControl = Session.getModelController(OrderPaymentPreferenceControl.class); 258 var orderControl = Session.getModelController(OrderControl.class); 259 var orderStatus = orderControl.getOrderStatusForUpdate(order); 260 261 if(orderPaymentPreferenceSequence == null) { 262 orderPaymentPreferenceSequence = orderStatus.getOrderPaymentPreferenceSequence() + 1; 263 orderStatus.setOrderPaymentPreferenceSequence(orderPaymentPreferenceSequence); 264 } else { 265 orderPaymentPreference = orderPaymentPreferenceControl.getOrderPaymentPreferenceBySequence(order, orderPaymentPreferenceSequence); 266 267 if(orderPaymentPreference == null) { 268 // If the orderPaymentPreferenceSequence is > the last one that was recorded in the OrderStatus, jump the 269 // one in OrderStatus forward - it should always record the greatest orderPaymentPreferenceSequence used. 270 if(orderPaymentPreferenceSequence > orderStatus.getOrderPaymentPreferenceSequence()) { 271 orderStatus.setOrderPaymentPreferenceSequence(orderPaymentPreferenceSequence); 272 } 273 } else { 274 handleExecutionError(DuplicateOrderPaymentPreferenceSequenceException.class, eea, ExecutionErrors.DuplicateOrderPaymentPreferenceSequence.name(), 275 order.getLastDetail().getOrderName(), orderPaymentPreferenceSequence.toString()); 276 } 277 } 278 279 if(orderPaymentPreference == null) { 280 orderPaymentPreference = orderPaymentPreferenceControl.createOrderPaymentPreference(order, orderPaymentPreferenceSequence, paymentMethod, partyPaymentMethod, 281 wasPresent, maximumAmount, sortOrder, createdBy); 282 } 283 } 284 } 285 } else { 286 if(parameterCount == 0) { 287 handleExecutionError(MustSpecifyPaymentMethodOrPartyPaymentMethodException.class, eea, ExecutionErrors.MustSpecifyPaymentMethodOrPartyPaymentMethod.name()); 288 } else { 289 handleExecutionError(CannotSpecifyPaymentMethodAndPartyPaymentMethodException.class, eea, ExecutionErrors.CannotSpecifyPaymentMethodAndPartyPaymentMethod.name()); 290 } 291 } 292 293 return orderPaymentPreference; 294 } 295 296 private OrderPaymentPreference getOrderPaymentPreferenceByName(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName, 297 final String orderPaymentPreferenceSequence, final EntityPermission entityPermission) { 298 var orderPaymentPreferenceControl = Session.getModelController(OrderPaymentPreferenceControl.class); 299 var order = getOrderByName(eea, orderTypeName, orderName); 300 OrderPaymentPreference orderPaymentPreference = null; 301 302 if(eea == null || !eea.hasExecutionErrors()) { 303 orderPaymentPreference = orderPaymentPreferenceControl.getOrderPaymentPreferenceBySequence(order, Integer.valueOf(orderPaymentPreferenceSequence), entityPermission); 304 305 if(orderPaymentPreference == null) { 306 handleExecutionError(UnknownOrderPaymentPreferenceSequenceException.class, eea, ExecutionErrors.UnknownOrderPaymentPreferenceSequence.name(), orderTypeName, 307 orderName, orderPaymentPreferenceSequence); 308 } 309 } 310 311 return orderPaymentPreference; 312 } 313 314 public OrderPaymentPreference getOrderPaymentPreferenceByName(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName, 315 final String orderPaymentPreferenceSequence) { 316 return getOrderPaymentPreferenceByName(eea, orderTypeName, orderName, orderPaymentPreferenceSequence, EntityPermission.READ_ONLY); 317 } 318 319 public OrderPaymentPreference getOrderPaymentPreferenceByNameForUpdate(final ExecutionErrorAccumulator eea, final String orderTypeName, final String orderName, 320 final String orderPaymentPreferenceSequence) { 321 return getOrderPaymentPreferenceByName(eea, orderTypeName, orderName, orderPaymentPreferenceSequence, EntityPermission.READ_WRITE); 322 } 323 324 public void checkItemAgainstOrderPaymentPreferences(final Session session, final ExecutionErrorAccumulator eea, final Order order, final Item item, 325 final BasePK evaluatedBy) { 326 var orderPaymentPreferenceControl = Session.getModelController(OrderPaymentPreferenceControl.class); 327 var orderPaymentPreferences = orderPaymentPreferenceControl.getOrderPaymentPreferencesByOrder(order); 328 329 orderPaymentPreferences.forEach((orderPaymentPreference) -> { 330 PaymentMethodLogic.getInstance().checkAcceptanceOfItem(session, eea, orderPaymentPreference.getLastDetail().getPaymentMethod(), item, evaluatedBy); 331 }); 332 } 333 334 public void checkItemAgainstShippingMethod(final Session session, final ExecutionErrorAccumulator eea, final OrderShipmentGroup orderShipmentGroup, 335 final Item item, final BasePK evaluatedBy) { 336 var shippingMethod = orderShipmentGroup.getLastDetail().getShippingMethod(); 337 338 if(shippingMethod != null) { 339 ShippingMethodLogic.getInstance().checkAcceptanceOfItem(session, eea, shippingMethod, item, evaluatedBy); 340 } 341 } 342 343}