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.UnknownCustomerTypeShippingMethodException;
020import com.echothree.model.control.customer.server.control.CustomerControl;
021import com.echothree.model.control.order.server.control.OrderShipmentGroupControl;
022import com.echothree.model.control.order.server.logic.BaseOrderShipmentGroupLogic;
023import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
024import com.echothree.model.data.customer.server.entity.CustomerType;
025import com.echothree.model.data.item.server.entity.ItemDeliveryType;
026import com.echothree.model.data.order.server.entity.Order;
027import com.echothree.model.data.order.server.entity.OrderShipmentGroup;
028import com.echothree.model.data.order.server.value.OrderShipmentGroupDetailValue;
029import com.echothree.model.data.party.common.pk.PartyPK;
030import com.echothree.model.data.shipping.server.entity.ShippingMethod;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.message.ExecutionErrorAccumulator;
034import com.echothree.util.server.persistence.Session;
035import javax.enterprise.context.ApplicationScoped;
036import javax.enterprise.inject.spi.CDI;
037import javax.inject.Inject;
038
039@ApplicationScoped
040public class SalesOrderShipmentGroupLogic
041        extends BaseOrderShipmentGroupLogic {
042
043    @Inject
044    CustomerControl customerControl;
045
046    @Inject
047    OrderShipmentGroupControl orderShipmentGroupControl;
048
049    @Inject
050    SalesOrderLogic salesOrderLogic;
051
052    protected SalesOrderShipmentGroupLogic() {
053        super();
054    }
055
056    public static SalesOrderShipmentGroupLogic getInstance() {
057        return CDI.current().select(SalesOrderShipmentGroupLogic.class).get();
058    }
059    
060    /**
061     * Verify that the CustomerType is authorized to use the ShippingMethod. If there are no CustomerTypeShippingMethods
062     * for any ShippingMethod then it is assumed they're authorized.
063     *
064     * @param eea Required.
065     * @param customerType Required.
066     * @param shippingMethod Required.
067     */
068    public void checkCustomerTypeShippingMethod(final ExecutionErrorAccumulator eea, final CustomerType customerType,
069            final ShippingMethod shippingMethod) {
070        if(!customerControl.getCustomerTypeShippingMethodExists(customerType, shippingMethod)
071                && customerControl.countCustomerTypeShippingMethodsByCustomerType(customerType) != 0) {
072            handleExecutionError(UnknownCustomerTypeShippingMethodException.class, eea, ExecutionErrors.UnknownCustomerTypeShippingMethod.name(),
073                    customerType.getLastDetail().getCustomerTypeName(), shippingMethod.getLastDetail().getShippingMethodName());
074        }
075    }
076
077    /**
078     * Create a new Order Shipment Group for a given Order.
079     *
080     * @param session Required.
081     * @param eea Required.
082     * @param order Required.
083     * @param orderShipmentGroupSequence Optional.
084     * @param itemDeliveryType Required.
085     * @param isDefault Required.
086     * @param partyContactMechanism Optional.
087     * @param shippingMethod Optional.
088     * @param holdUntilComplete Required.
089     * @param createdBy Required.
090     * @return The newly created OrderShipmentGroup, or null if there was an error.
091     */
092    public OrderShipmentGroup createSalesOrderShipmentGroup(final Session session, final ExecutionErrorAccumulator eea,
093            final Order order, Integer orderShipmentGroupSequence, final ItemDeliveryType itemDeliveryType, final Boolean isDefault,
094            final PartyContactMechanism partyContactMechanism, final ShippingMethod shippingMethod, final Boolean holdUntilComplete,
095            final PartyPK createdBy) {
096        OrderShipmentGroup orderShipmentGroup = null;
097
098        salesOrderLogic.checkOrderAvailableForModification(session, eea, order, createdBy);
099
100        if(eea == null || !eea.hasExecutionErrors()) {
101            orderShipmentGroup = createOrderShipmentGroup(eea, order, orderShipmentGroupSequence, itemDeliveryType, isDefault, partyContactMechanism,
102                    shippingMethod, holdUntilComplete, createdBy);
103        }
104
105        return orderShipmentGroup;
106    }
107
108    public void updateSalesOrderShipmentGroupFromValue(final ExecutionErrorAccumulator eea, final OrderShipmentGroupDetailValue orderShipmentGroupDetailValue,
109            final BasePK updatedBy) {
110        orderShipmentGroupControl.updateOrderShipmentGroupFromValue(orderShipmentGroupDetailValue, updatedBy);
111    }
112
113}