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.GetItemUnitPriceLimitForm;
020import com.echothree.control.user.item.common.result.GetItemUnitPriceLimitResult;
021import com.echothree.control.user.item.common.result.ItemResultFactory;
022import com.echothree.model.control.accounting.server.control.AccountingControl;
023import com.echothree.model.control.inventory.server.control.InventoryControl;
024import com.echothree.model.control.item.server.control.ItemControl;
025import com.echothree.model.control.uom.server.control.UomControl;
026import com.echothree.model.data.accounting.server.entity.Currency;
027import com.echothree.model.data.inventory.server.entity.InventoryCondition;
028import com.echothree.model.data.item.server.entity.Item;
029import com.echothree.model.data.item.server.entity.ItemUnitPriceLimit;
030import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind;
031import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.common.command.BaseResult;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042
043public class GetItemUnitPriceLimitCommand
044        extends BaseSimpleCommand<GetItemUnitPriceLimitForm> {
045    
046    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
047    
048    static {
049        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
050            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
051            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
052            new FieldDefinition("UnitOfMeasureTypeName", FieldType.PERCENT, true, null, null),
053            new FieldDefinition("CurrencyIsoName", FieldType.PERCENT, true, null, null)
054        ));
055    }
056    
057    /** Creates a new instance of GetItemUnitPriceLimitCommand */
058    public GetItemUnitPriceLimitCommand(UserVisitPK userVisitPK, GetItemUnitPriceLimitForm form) {
059        super(userVisitPK, form, null, FORM_FIELD_DEFINITIONS, false);
060    }
061    
062    @Override
063    protected BaseResult execute() {
064        var itemControl = Session.getModelController(ItemControl.class);
065        GetItemUnitPriceLimitResult result = ItemResultFactory.getGetItemUnitPriceLimitResult();
066        String itemName = form.getItemName();
067        Item item = itemControl.getItemByName(itemName);
068        
069        if(item != null) {
070            var inventoryControl = Session.getModelController(InventoryControl.class);
071            String inventoryConditionName = form.getInventoryConditionName();
072            InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
073            
074            if(inventoryCondition != null) {
075                var uomControl = Session.getModelController(UomControl.class);
076                String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
077                UnitOfMeasureKind unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
078                UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
079                
080                if(unitOfMeasureType != null) {
081                    var accountingControl = Session.getModelController(AccountingControl.class);
082                    String currencyIsoName = form.getCurrencyIsoName();
083                    Currency currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
084                    
085                    if(currency != null) {
086                        ItemUnitPriceLimit itemUnitPriceLimit = itemControl.getItemUnitPriceLimit(item, inventoryCondition, unitOfMeasureType, currency);
087                        
088                        if(itemUnitPriceLimit != null) {
089                            result.setItemUnitPriceLimit(itemControl.getItemUnitPriceLimitTransfer(getUserVisit(), itemUnitPriceLimit));
090                        } else {
091                            addExecutionError(ExecutionErrors.UnknownItemUnitPriceLimit.name(), itemName, inventoryConditionName, unitOfMeasureTypeName,
092                                    currencyIsoName);
093                        }
094                    } else {
095                        addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
096                    }
097                } else {
098                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
099                }
100            } else {
101                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
102            }
103        } else {
104            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
105        }
106        
107        return result;
108    }
109    
110}