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