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.edit.VendorEditFactory; 020import com.echothree.control.user.vendor.common.edit.VendorItemCostEdit; 021import com.echothree.control.user.vendor.common.result.EditVendorItemCostResult; 022import com.echothree.control.user.vendor.common.result.VendorResultFactory; 023import com.echothree.control.user.vendor.common.spec.VendorItemCostSpec; 024import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 025import com.echothree.model.control.party.common.PartyTypes; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.uom.server.control.UomControl; 029import com.echothree.model.control.vendor.server.control.VendorControl; 030import com.echothree.model.data.vendor.server.entity.VendorItem; 031import com.echothree.model.data.vendor.server.entity.VendorItemCost; 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.BaseAbstractEditCommand; 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.string.AmountUtils; 040import com.echothree.util.server.validation.Validator; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043import javax.inject.Inject; 044 045@Dependent 046public class EditVendorItemCostCommand 047 extends BaseAbstractEditCommand<VendorItemCostSpec, VendorItemCostEdit, EditVendorItemCostResult, VendorItemCost, VendorItem> { 048 049 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 050 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 051 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 052 053 static { 054 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.VendorItemCost.name(), SecurityRoles.Edit.name()) 058 )) 059 )); 060 061 SPEC_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("VendorItemName", FieldType.ENTITY_NAME, true, null, null), 064 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 065 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null) 066 ); 067 068 EDIT_FIELD_DEFINITIONS = List.of( 069 new FieldDefinition("UnitCost", FieldType.UNSIGNED_COST_UNIT, true, null, null) 070 ); 071 } 072 073 @Inject 074 InventoryConditionControl inventoryConditionControl; 075 076 @Inject 077 UomControl uomControl; 078 079 @Inject 080 VendorControl vendorControl; 081 082 083 /** Creates a new instance of EditVendorItemCostCommand */ 084 public EditVendorItemCostCommand() { 085 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 086 } 087 088 @Override 089 protected void setupValidatorForEdit(Validator validator, com.echothree.util.common.form.BaseForm specForm) { 090 var vendorName = spec.getVendorName(); 091 var vendor = vendorControl.getVendorByName(vendorName); 092 093 if(vendor != null) { 094 var vendorParty = vendor.getParty(); 095 096 validator.setCurrency(getPreferredCurrency(vendorParty)); 097 } 098 } 099 100 @Override 101 protected EditVendorItemCostResult getResult() { 102 return VendorResultFactory.getEditVendorItemCostResult(); 103 } 104 105 @Override 106 protected VendorItemCostEdit getEdit() { 107 return VendorEditFactory.getVendorItemCostEdit(); 108 } 109 110 @Override 111 protected VendorItemCost getEntity(EditVendorItemCostResult result) { 112 VendorItemCost vendorItemCost = null; 113 var vendorName = spec.getVendorName(); 114 var vendor = vendorControl.getVendorByName(vendorName); 115 116 if(vendor != null) { 117 var vendorParty = vendor.getParty(); 118 var vendorItemName = spec.getVendorItemName(); 119 var vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName); 120 121 if(vendorItem != null) { 122 var inventoryConditionName = spec.getInventoryConditionName(); 123 var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName); 124 125 if(inventoryCondition != null) { 126 var unitOfMeasureKind = vendorItem.getLastDetail().getItem().getLastDetail().getUnitOfMeasureKind(); 127 var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName(); 128 var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 129 130 if(unitOfMeasureType != null) { 131 vendorItemCost = vendorControl.getVendorItemCost(vendorItem, inventoryCondition, unitOfMeasureType, 132 editModeToEntityPermission(editMode)); 133 134 if(vendorItemCost == null) { 135 addExecutionError(ExecutionErrors.UnknownVendorItemCost.name(), 136 vendor.getVendorName(), vendorItem.getLastDetail().getVendorItemName(), 137 inventoryCondition.getLastDetail().getInventoryConditionName(), 138 unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName()); 139 } 140 } else { 141 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 142 } 143 } else { 144 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.UnknownVendorItemName.name(), vendor.getVendorName(), vendorItemName); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 151 } 152 153 return vendorItemCost; 154 } 155 156 @Override 157 protected VendorItem getLockEntity(VendorItemCost vendorItemCost) { 158 return vendorItemCost.getVendorItem(); 159 } 160 161 @Override 162 protected void fillInResult(EditVendorItemCostResult result, VendorItemCost vendorItemCost) { 163 result.setVendorItemCost(vendorControl.getVendorItemCostTransfer(getUserVisit(), vendorItemCost)); 164 } 165 166 @Override 167 protected void doLock(VendorItemCostEdit edit, VendorItemCost vendorItemCost) { 168 var vendorParty = vendorItemCost.getVendorItem().getLastDetail().getVendorParty(); 169 170 edit.setUnitCost(AmountUtils.getInstance().formatCostUnit(getPreferredCurrency(vendorParty), 171 vendorItemCost.getUnitCost())); 172 } 173 174 @Override 175 protected void doUpdate(VendorItemCost vendorItemCost) { 176 var vendorItemCostValue = vendorControl.getVendorItemCostValue(vendorItemCost); 177 178 vendorItemCostValue.setUnitCost(Long.valueOf(edit.getUnitCost())); 179 180 vendorControl.updateVendorItemCostFromValue(vendorItemCostValue, getPartyPK()); 181 } 182 183}