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