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.control.user.inventory.server.command.common.PartyInventoryLevelUtil; 021import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 022import com.echothree.model.control.inventory.server.control.InventoryLevelControl; 023import com.echothree.model.control.item.server.control.ItemControl; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 028import com.echothree.util.common.command.BaseResult; 029import com.echothree.util.common.message.ExecutionErrors; 030import com.echothree.util.common.validation.FieldDefinition; 031import com.echothree.util.common.validation.FieldType; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import com.echothree.util.server.control.PartyTypeDefinition; 035import com.echothree.util.server.control.SecurityRoleDefinition; 036import java.util.List; 037import javax.enterprise.context.Dependent; 038import javax.inject.Inject; 039 040@Dependent 041public class CreatePartyInventoryLevelCommand 042 extends BaseSimpleCommand<CreatePartyInventoryLevelForm> { 043 044 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 045 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 046 047 static { 048 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 049 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 050 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 051 new SecurityRoleDefinition(SecurityRoleGroups.PartyInventoryLevel.name(), SecurityRoles.Create.name()) 052 )) 053 )); 054 055 FORM_FIELD_DEFINITIONS = List.of( 056 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 058 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 060 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("MinimumInventoryUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("MinimumInventory", FieldType.UNSIGNED_LONG, false, null, null), 063 new FieldDefinition("MaximumInventoryUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("MaximumInventory", FieldType.UNSIGNED_LONG, false, null, null), 065 new FieldDefinition("ReorderQuantityUnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("ReorderQuantity", FieldType.UNSIGNED_LONG, false, null, null) 067 ); 068 } 069 070 @Inject 071 InventoryConditionControl inventoryConditionControl; 072 073 @Inject 074 InventoryLevelControl inventoryLevelControl; 075 076 @Inject 077 ItemControl itemControl; 078 079 @Inject 080 UnitOfMeasureTypeLogic unitOfMeasureTypeLogic; 081 082 @Inject 083 PartyInventoryLevelUtil partyInventoryLevelUtil; 084 085 /** Creates a new instance of CreatePartyInventoryLevelCommand */ 086 public CreatePartyInventoryLevelCommand() { 087 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 088 } 089 090 @Override 091 protected BaseResult execute() { 092 var party = partyInventoryLevelUtil.getParty(this, form); 093 094 if(party != null) { 095 var itemName = form.getItemName(); 096 var item = itemControl.getItemByName(itemName); 097 098 if(item != null) { 099 var partyTypeName = partyInventoryLevelUtil.getPartyTypeName(party); 100 101 if(partyTypeName.equals(PartyTypes.COMPANY.name())) { 102 if(!party.equals(item.getLastDetail().getCompanyParty())) { 103 addExecutionError(ExecutionErrors.InvalidCompany.name()); 104 } 105 } 106 107 if(!hasExecutionErrors()) { 108 var inventoryConditionName = form.getInventoryConditionName(); 109 var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName); 110 111 if(inventoryCondition != null) { 112 var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind(); 113 var minimumInventory = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind, 114 form.getMinimumInventory(), form.getMinimumInventoryUnitOfMeasureTypeName(), 115 null, ExecutionErrors.MissingRequiredMinimumInventory.name(), null, ExecutionErrors.MissingRequiredMinimumInventoryUnitOfMeasureTypeName.name(), 116 null, ExecutionErrors.UnknownMinimumInventoryUnitOfMeasureTypeName.name()); 117 118 if(!hasExecutionErrors()) { 119 var maximumInventory = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind, 120 form.getMaximumInventory(), form.getMaximumInventoryUnitOfMeasureTypeName(), 121 null, ExecutionErrors.MissingRequiredMaximumInventory.name(), null, ExecutionErrors.MissingRequiredMaximumInventoryUnitOfMeasureTypeName.name(), 122 null, ExecutionErrors.UnknownMaximumInventoryUnitOfMeasureTypeName.name()); 123 124 if(!hasExecutionErrors()) { 125 var reorderQuantity = unitOfMeasureTypeLogic.checkUnitOfMeasure(this, unitOfMeasureKind, 126 form.getReorderQuantity(), form.getReorderQuantityUnitOfMeasureTypeName(), 127 null, ExecutionErrors.MissingRequiredReorderQuantity.name(), null, ExecutionErrors.MissingRequiredReorderQuantityUnitOfMeasureTypeName.name(), 128 null, ExecutionErrors.UnknownReorderQuantityUnitOfMeasureTypeName.name()); 129 130 if(!hasExecutionErrors()) { 131 var partyInventoryLevel = inventoryLevelControl.getPartyInventoryLevel(party, item, inventoryCondition); 132 133 if(partyInventoryLevel == null) { 134 inventoryLevelControl.createPartyInventoryLevel(party, item, inventoryCondition, minimumInventory, maximumInventory, 135 reorderQuantity, getPartyPK()); 136 } else { 137 addExecutionError(ExecutionErrors.DuplicatePartyInventoryLevel.name(), party.getLastDetail().getPartyName(), itemName, 138 inventoryConditionName); 139 } 140 } 141 } 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 145 } 146 } 147 } else { 148 addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName); 149 } 150 } 151 152 return null; 153 } 154 155}