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