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.offer.server.command;
018
019import com.echothree.control.user.offer.common.form.GetOfferItemPriceForm;
020import com.echothree.control.user.offer.common.result.OfferResultFactory;
021import com.echothree.model.control.accounting.server.control.AccountingControl;
022import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
023import com.echothree.model.control.item.server.control.ItemControl;
024import com.echothree.model.control.offer.server.control.OfferControl;
025import com.echothree.model.control.offer.server.control.OfferItemControl;
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.control.uom.server.control.UomControl;
030import com.echothree.model.data.offer.server.entity.OfferItemPrice;
031import com.echothree.model.data.user.common.pk.UserVisitPK;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.message.ExecutionErrors;
034import com.echothree.util.common.validation.FieldDefinition;
035import com.echothree.util.common.validation.FieldType;
036import com.echothree.util.server.control.BaseSingleEntityCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetOfferItemPriceCommand
046        extends BaseSingleEntityCommand<OfferItemPrice, GetOfferItemPriceForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.Review.name())
056                ))
057        ));
058        
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("OfferName", FieldType.ENTITY_NAME, true, null, 20L),
061                new FieldDefinition("ItemName", 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                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
065                new FieldDefinition("IncludeHistory", FieldType.BOOLEAN, false, null, null)
066        );
067    }
068
069    @Inject
070    AccountingControl accountingControl;
071
072    @Inject
073    InventoryConditionControl inventoryConditionControl;
074
075    @Inject
076    ItemControl itemControl;
077
078    @Inject
079    OfferControl offerControl;
080
081    @Inject
082    OfferItemControl offerItemControl;
083
084    @Inject
085    UomControl uomControl;
086
087    
088    /** Creates a new instance of GetOfferItemPriceCommand */
089    public GetOfferItemPriceCommand() {
090        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
091    }
092    
093    @Override
094    protected boolean checkOptionalSecurityRoles() {
095        // This occurs before validation, and parseBoolean is more lax than our validation of what's permitted for a FieldType.BOOLEAN.
096        return Boolean.parseBoolean(form.getIncludeHistory()) ? securityRoleLogic.hasSecurityRoleUsingNames(this, getParty(),
097                SecurityRoleGroups.OfferItemPrice.name(), SecurityRoles.History.name()) : true;
098    }
099    
100    @Override
101    protected OfferItemPrice getEntity() {
102        var offerName = form.getOfferName();
103        var offer = offerControl.getOfferByName(offerName);
104        OfferItemPrice offerItemPrice = null;
105
106        if(offer != null) {
107            var itemName = form.getItemName();
108            var item = itemControl.getItemByName(itemName);
109            
110            if(item != null) {
111                var offerItem = offerItemControl.getOfferItem(offer, item);
112
113                if(offerItem != null) {
114                    var inventoryConditionName = form.getInventoryConditionName();
115                    var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
116
117                    if(inventoryCondition != null) {
118                        var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
119                        var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
120                        var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
121
122                        if(unitOfMeasureType != null) {
123                            var currencyIsoName = form.getCurrencyIsoName();
124                            var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
125
126                            if(currency != null) {
127                                offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency);
128
129                                if(offerItemPrice == null) {
130                                    addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(), offer.getLastDetail().getOfferName(),
131                                            item.getLastDetail().getItemName(), inventoryCondition.getLastDetail().getInventoryConditionName(),
132                                            unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(), currency.getCurrencyIsoName());
133                                }
134                            } else {
135                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
136                            }
137                        } else {
138                            addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(),
139                                    unitOfMeasureKind.getLastDetail().getUnitOfMeasureKindName(), unitOfMeasureTypeName);
140                        }
141                    } else {
142                        addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
143                    }
144                } else {
145                    addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(),
146                            item.getLastDetail().getItemName());
147                }
148            } else {
149                addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
150            }
151        } else {
152            addExecutionError(ExecutionErrors.UnknownOfferName.name(), offerName);
153        }
154        
155        return offerItemPrice;
156    }
157    
158    @Override
159    protected BaseResult getResult(OfferItemPrice offerItemPrice) {
160        var result = OfferResultFactory.getGetOfferItemPriceResult();
161
162        if(offerItemPrice != null) {
163            var userVisit = getUserVisit();
164
165            result.setOfferItemPrice(offerItemControl.getOfferItemPriceTransfer(userVisit, offerItemPrice));
166
167            if(Boolean.parseBoolean(form.getIncludeHistory())) {
168                result.setHistory(offerItemControl.getOfferItemPriceHistory(userVisit, offerItemPrice));
169            }
170        }
171        
172        return result;
173    }
174    
175}