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