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.GetItemPriceForm; 020import com.echothree.control.user.item.common.result.ItemResultFactory; 021import com.echothree.model.control.accounting.server.logic.CurrencyLogic; 022import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic; 023import com.echothree.model.control.item.server.control.ItemControl; 024import com.echothree.model.control.item.server.logic.ItemLogic; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 028import com.echothree.model.data.item.server.entity.ItemPrice; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.message.ExecutionErrors; 032import com.echothree.util.common.validation.FieldDefinition; 033import com.echothree.util.common.validation.FieldType; 034import com.echothree.util.server.control.BaseSingleEntityCommand; 035import java.util.List; 036import javax.enterprise.context.Dependent; 037import javax.inject.Inject; 038 039@Dependent 040public class GetItemPriceCommand 041 extends BaseSingleEntityCommand<ItemPrice, GetItemPriceForm> { 042 043 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 044 045 static { 046 FORM_FIELD_DEFINITIONS = List.of( 047 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 048 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 049 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 050 new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null), 051 new FieldDefinition("IncludeHistory", FieldType.BOOLEAN, false, null, null) 052 ); 053 } 054 055 @Inject 056 ItemControl itemControl; 057 058 @Inject 059 CurrencyLogic currencyLogic; 060 061 @Inject 062 InventoryConditionLogic inventoryConditionLogic; 063 064 @Inject 065 ItemLogic itemLogic; 066 067 @Inject 068 UnitOfMeasureTypeLogic unitOfMeasureTypeLogic; 069 070 071 /** Creates a new instance of GetItemPriceCommand */ 072 public GetItemPriceCommand() { 073 super(null, FORM_FIELD_DEFINITIONS, false); 074 } 075 076 @Override 077 protected boolean checkOptionalSecurityRoles() { 078 // This occurs before validation, and parseBoolean is more lax than our validation of what's permitted for a FieldType.BOOLEAN. 079 return Boolean.parseBoolean(form.getIncludeHistory()) ? securityRoleLogic.hasSecurityRoleUsingNames(this, getParty(), 080 SecurityRoleGroups.ItemPrice.name(), SecurityRoles.History.name()) : true; 081 } 082 083 @Override 084 protected ItemPrice getEntity() { 085 var item = itemLogic.getItemByName(this, form.getItemName()); 086 var inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, form.getInventoryConditionName()); 087 var currency = currencyLogic.getCurrencyByName(this, form.getCurrencyIsoName()); 088 ItemPrice itemPrice = null; 089 090 if(!hasExecutionErrors()) { 091 var unitOfMeasureType = unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(this, item.getLastDetail().getUnitOfMeasureKind(), form.getUnitOfMeasureTypeName()); 092 093 if(!hasExecutionErrors()) { 094 itemPrice = itemControl.getItemPrice(item, inventoryCondition, unitOfMeasureType, currency); 095 096 if(itemPrice == null) { 097 addExecutionError(ExecutionErrors.UnknownItemPrice.name(), item.getLastDetail().getItemName(), 098 inventoryCondition.getLastDetail().getInventoryConditionName(), 099 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), currency.getCurrencyIsoName()); 100 } 101 } 102 103 } 104 105 return itemPrice; 106 } 107 108 @Override 109 protected BaseResult getResult(ItemPrice entity) { 110 var result = ItemResultFactory.getGetItemPriceResult(); 111 112 if(entity != null) { 113 var userVisit = getUserVisit(); 114 115 result.setItemPrice(itemControl.getItemPriceTransfer(userVisit, entity)); 116 117 if(Boolean.parseBoolean(form.getIncludeHistory())) { 118 result.setHistory(itemControl.getItemPriceHistory(userVisit, entity)); 119 } 120 } 121 122 return result; 123 } 124 125}