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