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.vendor.server.command; 018 019import com.echothree.control.user.vendor.common.form.GetVendorItemCostForm; 020import com.echothree.control.user.vendor.common.result.VendorResultFactory; 021import com.echothree.model.control.inventory.common.InventoryConditionUseTypes; 022import com.echothree.model.control.inventory.server.control.InventoryControl; 023import com.echothree.model.control.party.common.PartyTypes; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.control.security.common.SecurityRoleGroups; 026import com.echothree.model.control.security.common.SecurityRoles; 027import com.echothree.model.control.uom.server.control.UomControl; 028import com.echothree.model.control.vendor.server.control.VendorControl; 029import com.echothree.model.data.vendor.server.entity.Vendor; 030import com.echothree.model.data.vendor.server.entity.VendorItemCost; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseSingleEntityCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import java.util.List; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class GetVendorItemCostCommand 045 extends BaseSingleEntityCommand<VendorItemCost, GetVendorItemCostForm> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 049 050 static { 051 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 052 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 053 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 054 new SecurityRoleDefinition(SecurityRoleGroups.VendorItemCost.name(), SecurityRoles.Review.name()) 055 )) 056 )); 057 058 FORM_FIELD_DEFINITIONS = List.of( 059 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("VendorItemName", FieldType.ENTITY_NAME, true, null, null), 062 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null) 064 ); 065 } 066 067 /** Creates a new instance of GetVendorItemCostCommand */ 068 public GetVendorItemCostCommand() { 069 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 070 } 071 072 @Override 073 protected VendorItemCost getEntity() { 074 var vendorName = form.getVendorName(); 075 var partyName = form.getPartyName(); 076 var parameterCount = (vendorName == null ? 0 : 1) + (partyName == null ? 0 : 1); 077 VendorItemCost entity = null; 078 079 if(parameterCount == 1) { 080 var vendorControl = Session.getModelController(VendorControl.class); 081 Vendor vendor = null; 082 083 if(vendorName != null) { 084 vendor = vendorControl.getVendorByName(vendorName); 085 086 if(vendor == null) { 087 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 088 } 089 } else if(partyName != null) { 090 var partyControl = Session.getModelController(PartyControl.class); 091 var party = partyControl.getPartyByName(partyName); 092 093 if(party != null) { 094 if(party.getLastDetail().getPartyType().getPartyTypeName().equals(PartyTypes.VENDOR.name())) { 095 vendor = vendorControl.getVendor(party); 096 } else { 097 addExecutionError(ExecutionErrors.InvalidPartyType.name()); 098 } 099 } else { 100 addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName); 101 } 102 } 103 104 if(vendor != null) { 105 var vendorParty = vendor.getParty(); 106 var vendorItemName = form.getVendorItemName(); 107 var vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName); 108 109 if(vendorItem != null) { 110 var inventoryControl = Session.getModelController(InventoryControl.class); 111 var inventoryConditionName = form.getInventoryConditionName(); 112 var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 113 114 if(inventoryCondition != null) { 115 var inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConditionUseTypes.PURCHASE_ORDER.name()); 116 var inventoryConditionUse = inventoryControl.getInventoryConditionUse(inventoryConditionUseType, 117 inventoryCondition); 118 119 if(inventoryConditionUse != null) { 120 var uomControl = Session.getModelController(UomControl.class); 121 var unitOfMeasureKind = vendorItem.getLastDetail().getItem().getLastDetail().getUnitOfMeasureKind(); 122 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 123 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 124 125 if(unitOfMeasureType != null) { 126 entity = vendorControl.getVendorItemCost(vendorItem, inventoryCondition, unitOfMeasureType); 127 128 if(entity == null) { 129 addExecutionError(ExecutionErrors.UnknownVendorItemCost.name(), 130 vendor.getVendorName(), vendorItem.getLastDetail().getVendorItemName(), 131 inventoryCondition.getLastDetail().getInventoryConditionName(), 132 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName()); 133 } 134 } else { 135 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 136 } 137 } else { 138 addExecutionError(ExecutionErrors.InvalidInventoryCondition.name(), inventoryCondition.getLastDetail().getInventoryConditionName()); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownVendorItemName.name(), vendor.getVendorName(), vendorItemName); 145 } 146 } 147 } else { 148 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 149 } 150 151 return entity; 152 } 153 154 @Override 155 protected BaseResult getResult(VendorItemCost entity) { 156 var vendorControl = Session.getModelController(VendorControl.class); 157 var result = VendorResultFactory.getGetVendorItemCostResult(); 158 159 if(entity != null) { 160 result.setVendorItemCost(vendorControl.getVendorItemCostTransfer(getUserVisit(), entity)); 161 } 162 163 return result; 164 } 165 166}