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.inventory.server.command;
018
019import com.echothree.control.user.inventory.common.form.CreateInventoryDispositionAdjustmentForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.logic.InventoryDispositionAdjustmentLogic;
022import com.echothree.model.control.inventory.server.logic.InventoryTransactionTypeLogic;
023import com.echothree.model.control.inventory.server.logic.InventoryDispositionLogic;
024import com.echothree.model.control.inventory.server.logic.InventoryAdjustmentTypeLogic;
025import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.data.inventory.server.entity.InventoryDispositionAdjustment;
030import com.echothree.model.data.user.common.pk.UserVisitPK;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseSimpleCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
041
042@Dependent
043public class CreateInventoryDispositionAdjustmentCommand
044        extends BaseSimpleCommand<CreateInventoryDispositionAdjustmentForm> {
045    
046    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
047    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
048    
049    static {
050        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
051                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
052                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
053                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryDispositionAdjustment.name(), SecurityRoles.Create.name())
054                ))
055        ));
056        
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("InventoryTransactionTypeName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("InventoryDispositionName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("InventoryDispositionAdjustmentName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("InventoryAdjustmentTypeName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, true, null, null),
063                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
064                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
065                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
066        );
067    }
068
069    @Inject
070    InventoryDispositionAdjustmentLogic inventoryDispositionAdjustmentLogic;
071
072    @Inject
073    InventoryTransactionTypeLogic inventoryTransactionTypeLogic;
074
075    @Inject
076    InventoryDispositionLogic inventoryDispositionLogic;
077
078    @Inject
079    InventoryAdjustmentTypeLogic inventoryAdjustmentTypeLogic;
080
081    @Inject
082    InventoryBucketTypeLogic inventoryBucketTypeLogic;
083
084    /** Creates a new instance of CreateInventoryDispositionAdjustmentCommand */
085    public CreateInventoryDispositionAdjustmentCommand() {
086        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
087    }
088    
089    @Override
090    protected BaseResult execute() {
091        var result = InventoryResultFactory.getCreateInventoryDispositionAdjustmentResult();
092        var inventoryTransactionTypeName = form.getInventoryTransactionTypeName();
093        var inventoryTransactionType = inventoryTransactionTypeLogic.getInventoryTransactionTypeByName(this, inventoryTransactionTypeName);
094        InventoryDispositionAdjustment inventoryDispositionAdjustment = null;
095
096        if(!hasExecutionErrors()) {
097            var inventoryDispositionAdjustmentName = form.getInventoryDispositionAdjustmentName();
098            var inventoryDispositionName = form.getInventoryDispositionName();
099            var inventoryDisposition = inventoryDispositionLogic.getInventoryDispositionByName(this, inventoryTransactionType,
100                    inventoryDispositionName);
101
102            if(!hasExecutionErrors()) {
103                var inventoryAdjustmentType = inventoryAdjustmentTypeLogic.getInventoryAdjustmentTypeByName(this,
104                        form.getInventoryAdjustmentTypeName());
105                var inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this,
106                        form.getInventoryBucketTypeName());
107                var isDefault = Boolean.valueOf(form.getIsDefault());
108                var sortOrder = Integer.valueOf(form.getSortOrder());
109                var description = form.getDescription();
110                var partyPK = getPartyPK();
111
112                if(!hasExecutionErrors()) {
113                    inventoryDispositionAdjustment = inventoryDispositionAdjustmentLogic.createInventoryDispositionAdjustment(this,
114                            inventoryDisposition, inventoryDispositionAdjustmentName, inventoryAdjustmentType, inventoryBucketType,
115                            isDefault, sortOrder, getPreferredLanguage(), description, partyPK);
116                }
117            }
118        }
119
120        if(inventoryDispositionAdjustment != null) {
121            var inventoryDispositionAdjustmentDetail = inventoryDispositionAdjustment.getLastDetail();
122
123            result.setEntityRef(inventoryDispositionAdjustment.getPrimaryKey().getEntityRef());
124            result.setInventoryTransactionTypeName(inventoryTransactionType.getLastDetail().getInventoryTransactionTypeName());
125            result.setInventoryDispositionName(inventoryDispositionAdjustmentDetail.getInventoryDisposition().getLastDetail().getInventoryDispositionName());
126            result.setInventoryDispositionAdjustmentName(inventoryDispositionAdjustmentDetail.getInventoryDispositionAdjustmentName());
127        }
128
129        return result;
130    }
131    
132}