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