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.item.server.command;
018
019import com.echothree.control.user.item.common.form.CreateItemPriceForm;
020import com.echothree.model.control.accounting.server.control.AccountingControl;
021import com.echothree.model.control.inventory.common.InventoryConditionUseTypes;
022import com.echothree.model.control.inventory.server.control.InventoryControl;
023import com.echothree.model.control.item.common.ItemPriceTypes;
024import com.echothree.model.control.item.server.control.ItemControl;
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.util.common.message.ExecutionErrors;
030import com.echothree.util.common.validation.FieldDefinition;
031import com.echothree.util.common.validation.FieldType;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.persistence.BasePK;
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 com.echothree.util.server.persistence.Session;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041
042@Dependent
043public class CreateItemPriceCommand
044        extends BaseSimpleCommand<CreateItemPriceForm> {
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.ItemPrice.name(), SecurityRoles.Create.name())
054                ))
055        ));
056
057        FORM_FIELD_DEFINITIONS = List.of(
058                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
059                new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, true, null, null),
062                new FieldDefinition("UnitPrice:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_UNIT, false, null, null),
063                new FieldDefinition("MinimumUnitPrice:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_UNIT, false, null, null),
064                new FieldDefinition("MaximumUnitPrice:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_UNIT, false, null, null),
065                new FieldDefinition("UnitPriceIncrement:CurrencyIsoName,CurrencyIsoName", FieldType.UNSIGNED_PRICE_UNIT, false, null, null)
066                );
067    }
068    
069    /** Creates a new instance of CreateItemPriceCommand */
070    public CreateItemPriceCommand() {
071        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
072    }
073    
074    @Override
075    protected BaseResult execute() {
076        var itemControl = Session.getModelController(ItemControl.class);
077        var itemName = form.getItemName();
078        var item = itemControl.getItemByName(itemName);
079        
080        if(item != null) {
081            var inventoryControl = Session.getModelController(InventoryControl.class);
082            var inventoryConditionName = form.getInventoryConditionName();
083            var inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
084            
085            if(inventoryCondition != null) {
086                var inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConditionUseTypes.SALES_ORDER.name());
087                var inventoryConditionUse = inventoryControl.getInventoryConditionUse(inventoryConditionUseType,
088                        inventoryCondition);
089                
090                if(inventoryConditionUse != null) {
091                    var uomControl = Session.getModelController(UomControl.class);
092                    var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
093                    var itemDetail = item.getLastDetail();
094                    var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
095                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
096                    
097                    if(unitOfMeasureType != null) {
098                        var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType);
099                        
100                        if(itemUnitOfMeasureType != null) {
101                            var accountingControl = Session.getModelController(AccountingControl.class);
102                            var currencyIsoName = form.getCurrencyIsoName();
103                            var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
104                            
105                            if(currency != null) {
106                                var itemPrice = itemControl.getItemPrice(item, inventoryCondition, unitOfMeasureType, currency);
107                                
108                                if(itemPrice == null) {
109                                    var itemPriceTypeName = itemDetail.getItemPriceType().getItemPriceTypeName();
110                                    BasePK createdBy = getPartyPK();
111                                    
112                                    if(itemPriceTypeName.equals(ItemPriceTypes.FIXED.name())) {
113                                        var strUnitPrice = form.getUnitPrice();
114                                        
115                                        if(strUnitPrice != null) {
116                                            var unitPrice = Long.valueOf(strUnitPrice);
117                                            
118                                            itemPrice = itemControl.createItemPrice(item, inventoryCondition, unitOfMeasureType,
119                                                    currency, createdBy);
120                                            itemControl.createItemFixedPrice(itemPrice, unitPrice, createdBy);
121                                        } else {
122                                            addExecutionError(ExecutionErrors.MissingUnitPrice.name());
123                                        }
124                                    } else if(itemPriceTypeName.equals(ItemPriceTypes.VARIABLE.name())) {
125                                        var strMinimumUnitPrice = form.getMinimumUnitPrice();
126                                        Long minimumUnitPrice = null;
127                                        var strMaximumUnitPrice = form.getMaximumUnitPrice();
128                                        Long maximumUnitPrice = null;
129                                        var strUnitPriceIncrement = form.getUnitPriceIncrement();
130                                        Long unitPriceIncrement = null;
131                                        
132                                        if(strMinimumUnitPrice != null) {
133                                            minimumUnitPrice = Long.valueOf(strMinimumUnitPrice);
134                                        } else {
135                                            addExecutionError(ExecutionErrors.MissingMinimumUnitPrice.name());
136                                        }
137                                        
138                                        if(strMaximumUnitPrice != null) {
139                                            maximumUnitPrice = Long.valueOf(strMaximumUnitPrice);
140                                        } else {
141                                            addExecutionError(ExecutionErrors.MissingMaximumUnitPrice.name());
142                                        }
143                                        
144                                        if(strUnitPriceIncrement != null) {
145                                            unitPriceIncrement = Long.valueOf(strUnitPriceIncrement);
146                                        } else {
147                                            addExecutionError(ExecutionErrors.MissingUnitPriceIncrement.name());
148                                        }
149                                        
150                                        if(minimumUnitPrice != null && maximumUnitPrice != null && unitPriceIncrement != null) {
151                                            itemPrice = itemControl.createItemPrice(item, inventoryCondition, unitOfMeasureType,
152                                                    currency, createdBy);
153                                            itemControl.createItemVariablePrice(itemPrice, minimumUnitPrice, maximumUnitPrice,
154                                                    unitPriceIncrement, createdBy);
155                                        }
156                                    } else {
157                                        addExecutionError(ExecutionErrors.UnknownItemPriceType.name(), itemPriceTypeName);
158                                    }
159                                } else {
160                                    addExecutionError(ExecutionErrors.DuplicateItemPrice.name());
161                                }
162                            } else {
163                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
164                            }
165                        } else {
166                            addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName);
167                        }
168                    } else {
169                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
170                    }
171                } else {
172                    addExecutionError(ExecutionErrors.InvalidInventoryCondition.name(), inventoryConditionName);
173                }
174            } else {
175                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
176            }
177        } else {
178            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
179        }
180        
181        return null;
182    }
183    
184}