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.control.user.item.server.command;
018
019import com.echothree.control.user.item.common.form.DeleteItemKitMemberForm;
020import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
021import com.echothree.model.control.item.common.ItemTypes;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.uom.server.control.UomControl;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.server.control.BaseSimpleCommand;
029import java.util.List;
030import javax.enterprise.context.Dependent;
031import javax.inject.Inject;
032
033@Dependent
034public class DeleteItemKitMemberCommand
035        extends BaseSimpleCommand<DeleteItemKitMemberForm> {
036    
037    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
038    
039    static {
040        FORM_FIELD_DEFINITIONS = List.of(
041                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
042                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("UnitOfMeasureTypeName", FieldType.PERCENT, true, null, null),
044                new FieldDefinition("MemberItemName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("MemberInventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("MemberUnitOfMeasureTypeName", FieldType.PERCENT, true, null, null)
047        );
048    }
049
050    @Inject
051    InventoryConditionControl inventoryConditionControl;
052
053    @Inject
054    ItemControl itemControl;
055
056    @Inject
057    UomControl uomControl;
058
059    
060    /** Creates a new instance of DeleteItemKitMemberCommand */
061    public DeleteItemKitMemberCommand() {
062        super(null, FORM_FIELD_DEFINITIONS, false);
063    }
064    
065    @Override
066    protected BaseResult execute() {
067        var itemName = form.getItemName();
068        var item = itemControl.getItemByName(itemName);
069        
070        if(item != null) {
071            var itemDetail = item.getLastDetail();
072            var itemTypeName = itemDetail.getItemType().getItemTypeName();
073            
074            if(itemTypeName.equals(ItemTypes.KIT.name())) {
075                var inventoryConditionName = form.getInventoryConditionName();
076                var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
077                
078                if(inventoryCondition != null) {
079                    var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
080                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(itemDetail.getUnitOfMeasureKind(), unitOfMeasureTypeName);
081                    
082                    if(unitOfMeasureType != null) {
083                        var memberItemName = form.getMemberItemName();
084                        var memberItem = itemControl.getItemByName(memberItemName);
085                        
086                        if(memberItem != null) {
087                            var memberInventoryConditionName = form.getMemberInventoryConditionName();
088                            var memberInventoryCondition = inventoryConditionControl.getInventoryConditionByName(memberInventoryConditionName);
089                            
090                            if(memberInventoryCondition != null) {
091                                var memberUnitOfMeasureTypeName = form.getMemberUnitOfMeasureTypeName();
092                                var memberUnitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(memberItem.getLastDetail().getUnitOfMeasureKind(), memberUnitOfMeasureTypeName);
093                                
094                                if(memberUnitOfMeasureType != null) {
095                                    var itemKitMember = itemControl.getItemKitMemberForUpdate(item, inventoryCondition,
096                                            unitOfMeasureType, memberItem, memberInventoryCondition, memberUnitOfMeasureType);
097                                    
098                                    if(itemKitMember != null) {
099                                        itemControl.deleteItemKitMember(itemKitMember, getPartyPK());
100                                    } else {
101                                        addExecutionError(ExecutionErrors.UnknownItemKitMember.name());
102                                    }
103                                } else {
104                                    addExecutionError(ExecutionErrors.UnknownMemberUnitOfMeasureTypeName.name(), memberUnitOfMeasureTypeName);
105                                }
106                            } else {
107                                addExecutionError(ExecutionErrors.UnknownMemberInventoryConditionName.name(), memberInventoryConditionName);
108                            }
109                        } else {
110                            addExecutionError(ExecutionErrors.UnknownMemberItemName.name(), memberItemName);
111                        }
112                    } else {
113                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
114                    }
115                } else {
116                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
117                }
118            } else {
119                addExecutionError(ExecutionErrors.InvalidItemType.name(), itemTypeName);
120            }
121        } else {
122            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
123        }
124        
125        return null;
126    }
127    
128}