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.DeleteVendorItemCostForm;
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 DeleteVendorItemCostCommand
051        extends BaseSimpleCommand<DeleteVendorItemCostForm> {
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.Delete.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                ));
070    }
071    
072    /** Creates a new instance of DeleteVendorItemCostCommand */
073    public DeleteVendorItemCostCommand(UserVisitPK userVisitPK, DeleteVendorItemCostForm form) {
074        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var vendorControl = Session.getModelController(VendorControl.class);
080        String vendorName = form.getVendorName();
081        Vendor vendor = vendorControl.getVendorByName(vendorName);
082        
083        if(vendor != null) {
084            Party vendorParty = vendor.getParty();
085            String vendorItemName = form.getVendorItemName();
086            VendorItem vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName);
087            
088            if(vendorItem != null) {
089                var inventoryControl = Session.getModelController(InventoryControl.class);
090                String inventoryConditionName = form.getInventoryConditionName();
091                InventoryCondition inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
092                
093                if(inventoryCondition != null) {
094                    InventoryConditionUseType inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConstants.InventoryConditionUseType_PURCHASE_ORDER);
095                    InventoryConditionUse inventoryConditionUse = inventoryControl.getInventoryConditionUse(inventoryConditionUseType,
096                            inventoryCondition);
097                    
098                    if(inventoryConditionUse != null) {
099                        var uomControl = Session.getModelController(UomControl.class);
100                        UnitOfMeasureKind unitOfMeasureKind = vendorItem.getLastDetail().getItem().getLastDetail().getUnitOfMeasureKind();
101                        String unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
102                        UnitOfMeasureType unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
103                        
104                        if(unitOfMeasureType != null) {
105                            VendorItemCost vendorItemCost = vendorControl.getVendorItemCostForUpdate(vendorItem, inventoryCondition,
106                                    unitOfMeasureType);
107                            
108                            if(vendorItemCost != null) {
109                                vendorControl.deleteVendorItemCost(vendorItemCost, getPartyPK());
110                            } else {
111                                addExecutionError(ExecutionErrors.UnknownVendorItemCost.name());
112                            }
113                        } else {
114                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
115                        }
116                    } else {
117                        addExecutionError(ExecutionErrors.InvalidInventoryCondition.name(), inventoryConditionName);
118                    }
119                } else {
120                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
121                }
122            } else {
123                addExecutionError(ExecutionErrors.UnknownVendorItemName.name(), vendorItemName);
124            }
125        } else {
126            addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName);
127        }
128        
129        return null;
130    }
131    
132}