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.CreateItemUnitPriceLimitForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
022import com.echothree.model.control.item.server.control.ItemControl;
023import com.echothree.model.control.uom.server.control.UomControl;
024import com.echothree.model.data.user.common.pk.UserVisitPK;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.server.control.BaseSimpleCommand;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032import javax.inject.Inject;
033
034@Dependent
035public class CreateItemUnitPriceLimitCommand
036        extends BaseSimpleCommand<CreateItemUnitPriceLimitForm> {
037    
038    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
039    
040    static {
041        FORM_FIELD_DEFINITIONS = List.of(
042                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
043                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
044                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("MinimumUnitPrice", FieldType.UNSIGNED_PRICE_UNIT, false, null, null),
047                new FieldDefinition("MaximumUnitPrice", FieldType.UNSIGNED_PRICE_UNIT, false, null, null)
048        );
049    }
050
051    @Inject
052    AccountingControl accountingControl;
053
054    @Inject
055    InventoryConditionControl inventoryConditionControl;
056
057    @Inject
058    ItemControl itemControl;
059
060    @Inject
061    UomControl uomControl;
062
063    
064    /** Creates a new instance of CreateItemUnitPriceLimitCommand */
065    public CreateItemUnitPriceLimitCommand() {
066        super(null, FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var itemName = form.getItemName();
072        var item = itemControl.getItemByName(itemName);
073        
074        if(item != null) {
075            var inventoryConditionName = form.getInventoryConditionName();
076            var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
077            
078            if(inventoryCondition != null) {
079                var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
080                var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(item.getLastDetail().getUnitOfMeasureKind(), unitOfMeasureTypeName);
081                
082                if(unitOfMeasureType != null) {
083                    var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType);
084                    
085                    if(itemUnitOfMeasureType != null) {
086                        var currencyIsoName = form.getCurrencyIsoName();
087                        var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
088                        
089                        if(currency != null) {
090                            var itemUnitPriceLimit = itemControl.getItemUnitPriceLimit(item, inventoryCondition, unitOfMeasureType, currency);
091                            
092                            if(itemUnitPriceLimit == null) {
093                                var strMinimumUnitPrice = form.getMinimumUnitPrice();
094                                var minimumUnitPrice = strMinimumUnitPrice == null ? null : Long.valueOf(strMinimumUnitPrice);
095                                var strMaximumUnitPrice = form.getMaximumUnitPrice();
096                                var maximumUnitPrice = strMaximumUnitPrice == null ? null : Long.valueOf(strMaximumUnitPrice);
097                                
098                                if(minimumUnitPrice != null && maximumUnitPrice != null) {
099                                    if(maximumUnitPrice < minimumUnitPrice) {
100                                        addExecutionError(ExecutionErrors.MaximumUnitPriceLessThanMinimumUnitPrice.name());
101                                    }
102                                }
103
104                                if(!hasExecutionErrors()) {
105                                    itemControl.createItemUnitPriceLimit(item, inventoryCondition, unitOfMeasureType, currency, minimumUnitPrice, maximumUnitPrice, getPartyPK());
106                                }
107                            } else {
108                                addExecutionError(ExecutionErrors.DuplicateItemUnitPriceLimit.name());
109                            }
110                        } else {
111                            addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
112                        }
113                    } else {
114                        addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName);
115                    }
116                } else {
117                    addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
118                }
119            } else {
120                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
121            }
122        } else {
123            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
124        }
125        
126        return null;
127    }
128    
129}