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