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