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