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.DeleteItemPriceForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.inventory.server.control.InventoryControl;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.party.common.PartyTypes;
024import com.echothree.model.control.security.common.SecurityRoleGroups;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.control.uom.server.control.UomControl;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.persistence.BasePK;
033import com.echothree.util.server.control.BaseSimpleCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import com.echothree.util.server.persistence.Session;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class DeleteItemPriceCommand
043        extends BaseSimpleCommand<DeleteItemPriceForm> {
044
045    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
050                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
051                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
052                        new SecurityRoleDefinition(SecurityRoleGroups.ItemPrice.name(), SecurityRoles.Delete.name())
053                ))
054        ));
055
056        FORM_FIELD_DEFINITIONS = List.of(
057                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
058                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null)
061                );
062    }
063    
064    /** Creates a new instance of DeleteItemPriceCommand */
065    public DeleteItemPriceCommand() {
066        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var itemControl = Session.getModelController(ItemControl.class);
072        var itemName = form.getItemName();
073        var item = itemControl.getItemByName(itemName);
074        
075        if(item != null) {
076            var inventoryControl = Session.getModelController(InventoryControl.class);
077            var inventoryConditionName = form.getInventoryConditionName();
078            var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
079            
080            if(inventoryCondition != null) {
081                var uomControl = Session.getModelController(UomControl.class);
082                var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
083                var itemDetail = item.getLastDetail();
084                var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
085                var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
086                
087                if(unitOfMeasureType != null) {
088                    var accountingControl = Session.getModelController(AccountingControl.class);
089                    var currencyIsoName = form.getCurrencyIsoName();
090                    var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
091                    
092                    if(currency != null) {
093                        var itemPrice = itemControl.getItemPriceForUpdate(item, inventoryCondition, unitOfMeasureType, currency);
094                        
095                        if(itemPrice != null) {
096                            BasePK deletedBy = getPartyPK();
097                            
098                            itemControl.deleteItemPrice(itemPrice, deletedBy);
099                            
100                            if(itemControl.getItemPrices(item, inventoryCondition, unitOfMeasureType).isEmpty()) {
101                                itemControl.deleteItemKitMembers(itemControl.getItemKitMembersFromMemberItemForUpdate(item,
102                                        inventoryCondition, unitOfMeasureType), deletedBy);
103                            }
104                        } else {
105                            addExecutionError(ExecutionErrors.UnknownItemPrice.name(), itemName, inventoryConditionName,
106                                    unitOfMeasureTypeName, currencyIsoName);
107                        }
108                    } else {
109                        addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
110                    }
111                } else {
112                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
113                }
114            } else {
115                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
116            }
117        } else {
118            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
119        }
120        
121        return null;
122    }
123    
124}