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