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.InventoryControl;
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    InventoryControl inventoryControl;
075
076    @Inject
077    UomControl uomControl;
078
079    @Inject
080    VendorControl vendorControl;
081    
082    /** Creates a new instance of EditVendorItemCostCommand */
083    public EditVendorItemCostCommand() {
084        super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
085    }
086    
087    @Override
088    protected void setupValidatorForEdit(Validator validator, com.echothree.util.common.form.BaseForm specForm) {
089        var vendorName = spec.getVendorName();
090        var vendor = vendorControl.getVendorByName(vendorName);
091        
092        if(vendor != null) {
093            var vendorParty = vendor.getParty();
094            
095            validator.setCurrency(getPreferredCurrency(vendorParty));
096        }
097    }
098    
099    @Override
100    protected EditVendorItemCostResult getResult() {
101        return VendorResultFactory.getEditVendorItemCostResult();
102    }
103
104    @Override
105    protected VendorItemCostEdit getEdit() {
106        return VendorEditFactory.getVendorItemCostEdit();
107    }
108
109    @Override
110    protected VendorItemCost getEntity(EditVendorItemCostResult result) {
111        VendorItemCost vendorItemCost = null;
112        var vendorName = spec.getVendorName();
113        var vendor = vendorControl.getVendorByName(vendorName);
114
115        if(vendor != null) {
116            var vendorParty = vendor.getParty();
117            var vendorItemName = spec.getVendorItemName();
118            var vendorItem = vendorControl.getVendorItemByVendorPartyAndVendorItemName(vendorParty, vendorItemName);
119
120            if(vendorItem != null) {
121                var inventoryConditionName = spec.getInventoryConditionName();
122                var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
123
124                if(inventoryCondition != null) {
125                    var unitOfMeasureKind = vendorItem.getLastDetail().getItem().getLastDetail().getUnitOfMeasureKind();
126                    var unitOfMeasureTypeName = spec.getUnitOfMeasureTypeName();
127                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
128
129                    if(unitOfMeasureType != null) {
130                        vendorItemCost = vendorControl.getVendorItemCost(vendorItem, inventoryCondition, unitOfMeasureType,
131                                editModeToEntityPermission(editMode));
132
133                        if(vendorItemCost == null) {
134                            addExecutionError(ExecutionErrors.UnknownVendorItemCost.name(),
135                                    vendor.getVendorName(), vendorItem.getLastDetail().getVendorItemName(),
136                                    inventoryCondition.getLastDetail().getInventoryConditionName(),
137                                    unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName());
138                        }
139                    } else {
140                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
141                    }
142                } else {
143                    addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
144                }
145            } else {
146                addExecutionError(ExecutionErrors.UnknownVendorItemName.name(), vendor.getVendorName(), vendorItemName);
147            }
148        } else {
149            addExecutionError(ExecutionErrors.UnknownVendorName.name(), vendorName);
150        }
151
152        return vendorItemCost;
153    }
154
155    @Override
156    protected VendorItem getLockEntity(VendorItemCost vendorItemCost) {
157        return vendorItemCost.getVendorItem();
158    }
159
160    @Override
161    protected void fillInResult(EditVendorItemCostResult result, VendorItemCost vendorItemCost) {
162        result.setVendorItemCost(vendorControl.getVendorItemCostTransfer(getUserVisit(), vendorItemCost));
163    }
164
165    @Override
166    protected void doLock(VendorItemCostEdit edit, VendorItemCost vendorItemCost) {
167        var vendorParty = vendorItemCost.getVendorItem().getLastDetail().getVendorParty();
168
169        edit.setUnitCost(AmountUtils.getInstance().formatCostUnit(getPreferredCurrency(vendorParty),
170                vendorItemCost.getUnitCost()));
171    }
172
173    @Override
174    protected void doUpdate(VendorItemCost vendorItemCost) {
175        var vendorItemCostValue = vendorControl.getVendorItemCostValue(vendorItemCost);
176
177        vendorItemCostValue.setUnitCost(Long.valueOf(edit.getUnitCost()));
178
179        vendorControl.updateVendorItemCostFromValue(vendorItemCostValue, getPartyPK());
180    }
181
182}