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.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;
037
038@ApplicationScoped
039public class SalesOrderShipmentGroupLogic
040        extends BaseOrderShipmentGroupLogic {
041
042    protected SalesOrderShipmentGroupLogic() {
043        super();
044    }
045
046    public static SalesOrderShipmentGroupLogic getInstance() {
047        return CDI.current().select(SalesOrderShipmentGroupLogic.class).get();
048    }
049    
050    /**
051     * Verify that the CustomerType is authorized to use the ShippingMethod. If there are no CustomerTypeShippingMethods
052     * for any ShippingMethod then it is assumed they're authorized.
053     *
054     * @param eea Required.
055     * @param customerType Required.
056     * @param shippingMethod Required.
057     */
058    public void checkCustomerTypeShippingMethod(final ExecutionErrorAccumulator eea, final CustomerType customerType,
059            final ShippingMethod shippingMethod) {
060        var customerControl = Session.getModelController(CustomerControl.class);
061
062        if(!customerControl.getCustomerTypeShippingMethodExists(customerType, shippingMethod)
063                && customerControl.countCustomerTypeShippingMethodsByCustomerType(customerType) != 0) {
064            handleExecutionError(UnknownCustomerTypeShippingMethodException.class, eea, ExecutionErrors.UnknownCustomerTypeShippingMethod.name(),
065                    customerType.getLastDetail().getCustomerTypeName(), shippingMethod.getLastDetail().getShippingMethodName());
066        }
067    }
068
069    /**
070     * Create a new Order Shipment Group for a given Order.
071     *
072     * @param session Required.
073     * @param eea Required.
074     * @param order Required.
075     * @param orderShipmentGroupSequence Optional.
076     * @param itemDeliveryType Required.
077     * @param isDefault Required.
078     * @param partyContactMechanism Optional.
079     * @param shippingMethod Optional.
080     * @param holdUntilComplete Required.
081     * @param createdBy Required.
082     * @return The newly created OrderShipmentGroup, or null if there was an error.
083     */
084    public OrderShipmentGroup createSalesOrderShipmentGroup(final Session session, final ExecutionErrorAccumulator eea,
085            final Order order, Integer orderShipmentGroupSequence, final ItemDeliveryType itemDeliveryType, final Boolean isDefault,
086            final PartyContactMechanism partyContactMechanism, final ShippingMethod shippingMethod, final Boolean holdUntilComplete,
087            final PartyPK createdBy) {
088        OrderShipmentGroup orderShipmentGroup = null;
089
090        SalesOrderLogic.getInstance().checkOrderAvailableForModification(session, eea, order, createdBy);
091
092        if(eea == null || !eea.hasExecutionErrors()) {
093            orderShipmentGroup = createOrderShipmentGroup(eea, order, orderShipmentGroupSequence, itemDeliveryType, isDefault, partyContactMechanism,
094                    shippingMethod, holdUntilComplete, createdBy);
095        }
096
097        return orderShipmentGroup;
098    }
099
100    public void updateSalesOrderShipmentGroupFromValue(final ExecutionErrorAccumulator eea, final OrderShipmentGroupDetailValue orderShipmentGroupDetailValue,
101            final BasePK updatedBy) {
102        var orderShipmentGroupControl = Session.getModelController(OrderShipmentGroupControl.class);
103
104        orderShipmentGroupControl.updateOrderShipmentGroupFromValue(orderShipmentGroupDetailValue, updatedBy);
105    }
106
107}