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.GetItemUnitPriceLimitForm;
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.uom.server.logic.UnitOfMeasureTypeLogic;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import java.util.List;
032import javax.enterprise.context.Dependent;
033import javax.inject.Inject;
034
035@Dependent
036public class GetItemUnitPriceLimitCommand
037        extends BaseSimpleCommand<GetItemUnitPriceLimitForm> {
038
039    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
040    
041    static {
042        FORM_FIELD_DEFINITIONS = List.of(
043                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("UnitOfMeasureTypeName", FieldType.PERCENT, true, null, null),
046                new FieldDefinition("CurrencyIsoName", FieldType.PERCENT, true, null, null)
047        );
048    }
049
050    @Inject
051    ItemControl itemControl;
052
053    @Inject
054    CurrencyLogic currencyLogic;
055
056    @Inject
057    InventoryConditionLogic inventoryConditionLogic;
058
059    @Inject
060    ItemLogic itemLogic;
061
062    @Inject
063    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
064
065    /** Creates a new instance of GetItemUnitPriceLimitCommand */
066    public GetItemUnitPriceLimitCommand() {
067        super(null, FORM_FIELD_DEFINITIONS, false);
068    }
069    
070    @Override
071    protected BaseResult execute() {
072        var result = ItemResultFactory.getGetItemUnitPriceLimitResult();
073        var itemName = form.getItemName();
074        var item = itemLogic.getItemByName(this, itemName);
075
076        if(!hasExecutionErrors()) {
077            var inventoryConditionName = form.getInventoryConditionName();
078            var inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName);
079
080            if(!hasExecutionErrors()) {
081                var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
082                var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
083                var unitOfMeasureType = unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(this, unitOfMeasureKind, unitOfMeasureTypeName);
084
085                if(!hasExecutionErrors()) {
086                    var currencyIsoName = form.getCurrencyIsoName();
087                    var currency = currencyLogic.getCurrencyByName(this, currencyIsoName);
088
089                    if(!hasExecutionErrors()) {
090                        var itemUnitPriceLimit = itemControl.getItemUnitPriceLimit(item, inventoryCondition, unitOfMeasureType, currency);
091
092                        if(itemUnitPriceLimit != null) {
093                            result.setItemUnitPriceLimit(itemControl.getItemUnitPriceLimitTransfer(getUserVisit(), itemUnitPriceLimit));
094                        } else {
095                            addExecutionError(ExecutionErrors.UnknownItemUnitPriceLimit.name(), itemName, inventoryConditionName, unitOfMeasureTypeName,
096                                    currencyIsoName);
097                        }
098                    }
099                }
100            }
101        }
102
103        return result;
104    }
105    
106}