001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.logic.OrderLogic;
022import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
023import com.echothree.model.data.customer.server.entity.CustomerType;
024import com.echothree.model.data.item.server.entity.ItemDeliveryType;
025import com.echothree.model.data.order.server.entity.Order;
026import com.echothree.model.data.order.server.entity.OrderShipmentGroup;
027import com.echothree.model.data.party.common.pk.PartyPK;
028import com.echothree.model.data.shipping.server.entity.ShippingMethod;
029import com.echothree.util.common.message.ExecutionErrors;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import com.echothree.util.server.persistence.Session;
032
033public class SalesOrderShipmentGroupLogic
034        extends OrderLogic {
035
036    private SalesOrderShipmentGroupLogic() {
037        super();
038    }
039
040    private static class LogicHolder {
041        static SalesOrderShipmentGroupLogic instance = new SalesOrderShipmentGroupLogic();
042    }
043
044    public static SalesOrderShipmentGroupLogic getInstance() {
045        return LogicHolder.instance;
046    }
047    
048    /**
049     * Verify that the CustomerType is authorized to use the ShippingMethod. If there are no CustomerTypeShippingMethods
050     * for any ShippingMethod then it is assumed they're authorized.
051     *
052     * @param eea Required.
053     * @param customerType Required.
054     * @param shippingMethod Required.
055     */
056    public void checkCustomerTypeShippingMethod(final ExecutionErrorAccumulator eea, final CustomerType customerType,
057            final ShippingMethod shippingMethod) {
058        var customerControl = Session.getModelController(CustomerControl.class);
059
060        if(!customerControl.getCustomerTypeShippingMethodExists(customerType, shippingMethod)
061                && customerControl.countCustomerTypeShippingMethodsByCustomerType(customerType) != 0) {
062            handleExecutionError(UnknownCustomerTypeShippingMethodException.class, eea, ExecutionErrors.UnknownCustomerTypeShippingMethod.name(),
063                    customerType.getLastDetail().getCustomerTypeName(), shippingMethod.getLastDetail().getShippingMethodName());
064        }
065    }
066
067    /**
068     * Create a new Order Shipment Group for a given Order.
069     *
070     * @param session Required.
071     * @param eea Required.
072     * @param order Required.
073     * @param orderShipmentGroupSequence Optional.
074     * @param itemDeliveryType Required.
075     * @param isDefault Required.
076     * @param partyContactMechanism Optional.
077     * @param shippingMethod Optional.
078     * @param holdUntilComplete Required.
079     * @param createdBy Required.
080     * @return The newly created OrderShipmentGroup, or null if there was an error.
081     */
082    public OrderShipmentGroup createSalesOrderShipmentGroup(final Session session, final ExecutionErrorAccumulator eea,
083            final Order order, Integer orderShipmentGroupSequence, final ItemDeliveryType itemDeliveryType, final Boolean isDefault,
084            final PartyContactMechanism partyContactMechanism, final ShippingMethod shippingMethod, final Boolean holdUntilComplete,
085            final PartyPK createdBy) {
086        OrderShipmentGroup orderShipmentGroup = null;
087
088        SalesOrderLogic.getInstance().checkOrderAvailableForModification(session, eea, order, createdBy);
089
090        if(eea == null || !eea.hasExecutionErrors()) {
091            orderShipmentGroup = createOrderShipmentGroup(eea, order, orderShipmentGroupSequence, itemDeliveryType, isDefault, partyContactMechanism,
092                    shippingMethod, holdUntilComplete, createdBy);
093        }
094
095        return orderShipmentGroup;
096    }
097
098}