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.InventoryConditionControl;
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 java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
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    @Inject
065    AccountingControl accountingControl;
066
067    @Inject
068    InventoryConditionControl inventoryConditionControl;
069
070    @Inject
071    ItemControl itemControl;
072
073    @Inject
074    UomControl uomControl;
075
076    
077    /** Creates a new instance of DeleteItemPriceCommand */
078    public DeleteItemPriceCommand() {
079        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
080    }
081    
082    @Override
083    protected BaseResult execute() {
084        var itemName = form.getItemName();
085        var item = itemControl.getItemByName(itemName);
086        
087        if(item != null) {
088            var inventoryConditionName = form.getInventoryConditionName();
089            var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
090            
091            if(inventoryCondition != null) {
092                var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
093                var itemDetail = item.getLastDetail();
094                var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
095                var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
096                
097                if(unitOfMeasureType != null) {
098                    var currencyIsoName = form.getCurrencyIsoName();
099                    var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
100                    
101                    if(currency != null) {
102                        var itemPrice = itemControl.getItemPriceForUpdate(item, inventoryCondition, unitOfMeasureType, currency);
103                        
104                        if(itemPrice != null) {
105                            BasePK deletedBy = getPartyPK();
106                            
107                            itemControl.deleteItemPrice(itemPrice, deletedBy);
108                            
109                            if(itemControl.getItemPrices(item, inventoryCondition, unitOfMeasureType).isEmpty()) {
110                                itemControl.deleteItemKitMembers(itemControl.getItemKitMembersFromMemberItemForUpdate(item,
111                                        inventoryCondition, unitOfMeasureType), deletedBy);
112                            }
113                        } else {
114                            addExecutionError(ExecutionErrors.UnknownItemPrice.name(), itemName, inventoryConditionName,
115                                    unitOfMeasureTypeName, currencyIsoName);
116                        }
117                    } else {
118                        addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
119                    }
120                } else {
121                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
122                }
123            } else {
124                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
125            }
126        } else {
127            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
128        }
129        
130        return null;
131    }
132    
133}