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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.CreatePartyInventoryLevelForm;
020import com.echothree.model.control.inventory.server.control.InventoryControl;
021import com.echothree.model.control.item.server.control.ItemControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
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.persistence.Session;
030import java.util.List;
031import javax.enterprise.context.Dependent;
032
033@Dependent
034public class CreatePartyInventoryLevelCommand
035        extends BasePartyInventoryLevelCommand<CreatePartyInventoryLevelForm> {
036    
037    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
038    
039    static {
040        FORM_FIELD_DEFINITIONS = List.of(
041                new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
042                new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
043                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
044                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
046                new FieldDefinition("MinimumInventoryUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("MinimumInventory", FieldType.UNSIGNED_LONG, false, null, null),
048                new FieldDefinition("MaximumInventoryUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
049                new FieldDefinition("MaximumInventory", FieldType.UNSIGNED_LONG, false, null, null),
050                new FieldDefinition("ReorderQuantityUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
051                new FieldDefinition("ReorderQuantity", FieldType.UNSIGNED_LONG, false, null, null)
052                );
053    }
054    
055    /** Creates a new instance of CreatePartyInventoryLevelCommand */
056    public CreatePartyInventoryLevelCommand() {
057        super(FORM_FIELD_DEFINITIONS);
058    }
059    
060    @Override
061    protected BaseResult execute() {
062        var inventoryControl = Session.getModelController(InventoryControl.class);
063        var party = getParty(form);
064        
065        if(party != null) {
066            var itemControl = Session.getModelController(ItemControl.class);
067            var itemName = form.getItemName();
068            var item = itemControl.getItemByName(itemName);
069            
070            if(item != null) {
071                var partyTypeName = getPartyTypeName(party);
072                
073                if(partyTypeName.equals(PartyTypes.COMPANY.name())) {
074                    if(!party.equals(item.getLastDetail().getCompanyParty())) {
075                        addExecutionError(ExecutionErrors.InvalidCompany.name());
076                    }
077                }
078                
079                if(!hasExecutionErrors()) {
080                    var inventoryConditionName = form.getInventoryConditionName();
081                    var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
082                    
083                    if(inventoryCondition != null) {
084                        var unitOfMeasureTypeLogic = UnitOfMeasureTypeLogic.getInstance();
085                        var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
086                        var minimumInventory = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind,
087                                form.getMinimumInventory(), form.getMinimumInventoryUnitOfMeasureTypeName(),
088                                null, ExecutionErrors.MissingRequiredMinimumInventory.name(), null, ExecutionErrors.MissingRequiredMinimumInventoryUnitOfMeasureTypeName.name(),
089                                null, ExecutionErrors.UnknownMinimumInventoryUnitOfMeasureTypeName.name());
090
091                        if(!hasExecutionErrors()) {
092                            var maximumInventory = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind,
093                                    form.getMaximumInventory(), form.getMaximumInventoryUnitOfMeasureTypeName(),
094                                    null, ExecutionErrors.MissingRequiredMaximumInventory.name(), null, ExecutionErrors.MissingRequiredMaximumInventoryUnitOfMeasureTypeName.name(),
095                                    null, ExecutionErrors.UnknownMaximumInventoryUnitOfMeasureTypeName.name());
096
097                            if(!hasExecutionErrors()) {
098                                var reorderQuantity = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind,
099                                        form.getReorderQuantity(), form.getReorderQuantityUnitOfMeasureTypeName(),
100                                        null, ExecutionErrors.MissingRequiredReorderQuantity.name(), null, ExecutionErrors.MissingRequiredReorderQuantityUnitOfMeasureTypeName.name(),
101                                        null, ExecutionErrors.UnknownReorderQuantityUnitOfMeasureTypeName.name());
102
103                                if(!hasExecutionErrors()) {
104                                    var partyInventoryLevel = inventoryControl.getPartyInventoryLevel(party, item, inventoryCondition);
105                                    
106                                    if(partyInventoryLevel == null) {
107                                        inventoryControl.createPartyInventoryLevel(party, item, inventoryCondition, minimumInventory, maximumInventory,
108                                                reorderQuantity, getPartyPK());
109                                    } else {
110                                        addExecutionError(ExecutionErrors.DuplicatePartyInventoryLevel.name(), party.getLastDetail().getPartyName(), itemName,
111                                                inventoryConditionName);
112                                    }
113                                }
114                            }
115                        }
116                    } else {
117                        addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
118                    }
119                }
120            } else {
121                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
122            }
123        }
124        
125        return null;
126    }
127    
128}