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.CreateVendorItemCostForm; 020import com.echothree.model.control.inventory.common.InventoryConstants; 021import com.echothree.model.control.inventory.server.control.InventoryControl; 022import com.echothree.model.control.party.common.PartyTypes; 023import com.echothree.model.control.security.common.SecurityRoleGroups; 024import com.echothree.model.control.security.common.SecurityRoles; 025import com.echothree.model.control.uom.server.control.UomControl; 026import com.echothree.model.control.vendor.server.control.VendorControl; 027import com.echothree.model.data.inventory.server.entity.InventoryCondition; 028import com.echothree.model.data.inventory.server.entity.InventoryConditionUse; 029import com.echothree.model.data.inventory.server.entity.InventoryConditionUseType; 030import com.echothree.model.data.party.server.entity.Party; 031import com.echothree.model.data.uom.server.entity.UnitOfMeasureKind; 032import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 033import com.echothree.model.data.user.common.pk.UserVisitPK; 034import com.echothree.model.data.vendor.server.entity.Vendor; 035import com.echothree.model.data.vendor.server.entity.VendorItem; 036import com.echothree.model.data.vendor.server.entity.VendorItemCost; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.validation.FieldDefinition; 039import com.echothree.util.common.validation.FieldType; 040import com.echothree.util.common.command.BaseResult; 041import com.echothree.util.server.control.BaseSimpleCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import com.echothree.util.server.persistence.Session; 046import java.util.Arrays; 047import java.util.Collections; 048import java.util.List; 049 050public class CreateVendorItemCostCommand 051 extends BaseSimpleCommand<CreateVendorItemCostForm> { 052 053 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 054 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList( 060 new SecurityRoleDefinition(SecurityRoleGroups.VendorItemCost.name(), SecurityRoles.Create.name()) 061 ))) 062 ))); 063 064 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 065 new FieldDefinition("VendorName", FieldType.ENTITY_NAME, true, null, null), 066 new FieldDefinition("VendorItemName", FieldType.ENTITY_NAME, true, null, null), 067 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null), 068 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null), 069 new FieldDefinition("UnitCost:VendorName,VendorName", FieldType.UNSIGNED_COST_UNIT, true, null, null) 070 )); 071 } 072 073 /** Creates a new instance of CreateVendorItemCostCommand */ 074 public CreateVendorItemCostCommand(UserVisitPK userVisitPK, CreateVendorItemCostForm form) { 075 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 076 } 077 078 @Override 079 protected BaseResult execute() { 080 var vendorControl = Session.getModelController(VendorControl.class); 081 String vendorName = form.getVendorName(); 082 Vendor vendor = vendorControl.getVendorByName(vendorName); 083 084 if(vendor != null) { 085 Party vendorParty = vendor.getParty(); 086 String vendorItemName = form.getVendorItemName(); 087 VendorItem vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName); 088 089 if(vendorItem != null) { 090 var inventoryControl = Session.getModelController(InventoryControl.class); 091 String inventoryConditionName = form.getInventoryConditionName(); 092 InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName); 093 094 if(inventoryCondition != null) { 095 InventoryConditionUseType inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConstants.InventoryConditionUseType_PURCHASE_ORDER); 096 InventoryConditionUse inventoryConditionUse = inventoryControl.getInventoryConditionUse(inventoryConditionUseType, 097 inventoryCondition); 098 099 if(inventoryConditionUse != null) { 100 var uomControl = Session.getModelController(UomControl.class); 101 UnitOfMeasureKind unitOfMeasureKind = vendorItem.getLastDetail().getItem().getLastDetail().getUnitOfMeasureKind(); 102 String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 103 UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName); 104 105 if(unitOfMeasureType != null) { 106 VendorItemCost vendorItemCost = vendorControl.getVendorItemCost(vendorItem, inventoryCondition, 107 unitOfMeasureType); 108 109 if(vendorItemCost == null) { 110 Long unitCost = Long.valueOf(form.getUnitCost()); 111 112 vendorControl.createVendorItemCost(vendorItem, inventoryCondition, unitOfMeasureType, unitCost, 113 getPartyPK()); 114 } else { 115 addExecutionError(ExecutionErrors.DuplicateVendorItemCost.name()); 116 } 117 } else { 118 addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName); 119 } 120 } else { 121 addExecutionError(ExecutionErrors.InvalidInventoryCondition.name(), inventoryConditionName); 122 } 123 } else { 124 addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName); 125 } 126 } else { 127 addExecutionError(ExecutionErrors.UnknownVendorItemName.name(), vendorItemName); 128 } 129 } else { 130 addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName); 131 } 132 133 return null; 134 } 135 136}