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