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.trigger;
018
019import com.echothree.model.control.order.server.control.OrderControl;
020import com.echothree.model.control.order.server.trigger.OrderTypeTrigger;
021import com.echothree.model.control.sales.server.logic.SalesOrderLogic;
022import com.echothree.model.control.sales.common.workflow.SalesOrderStatusConstants;
023import com.echothree.model.control.workflow.server.trigger.BaseTrigger;
024import com.echothree.model.data.order.server.entity.Order;
025import com.echothree.model.data.party.common.pk.PartyPK;
026import com.echothree.model.data.workflow.server.entity.WorkflowEntityStatus;
027import com.echothree.util.server.message.ExecutionErrorAccumulator;
028import com.echothree.util.server.persistence.Session;
029
030public class SalesOrderTrigger
031        extends BaseTrigger
032        implements OrderTypeTrigger {
033
034    private void unallocateInventory(final Session session, final WorkflowEntityStatus workflowEntityStatus, final PartyPK triggeredBy) {
035        var orderControl = Session.getModelController(OrderControl.class);
036        Order order = orderControl.getOrderByEntityInstance(workflowEntityStatus.getEntityInstance());
037        
038        SalesOrderLogic.getInstance().setSalesOrderStatus(session, null, order, SalesOrderStatusConstants.WorkflowDestination_ENTRY_ALLOCATED_TO_UNALLOCATED, triggeredBy);
039    }
040    
041    @Override
042    public void handleTrigger(final Session session, final ExecutionErrorAccumulator eea, final WorkflowEntityStatus workflowEntityStatus,
043            final Order order, final PartyPK triggeredBy) {
044        String workflowStepName = getWorkflowStepName(workflowEntityStatus);
045        
046        if(workflowStepName.equals(SalesOrderStatusConstants.WorkflowStep_ENTRY_ALLOCATED)) {
047            unallocateInventory(session, workflowEntityStatus, triggeredBy);
048        }
049    }
050
051}