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.InventoryConditionControl;
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 java.util.List;
039import javax.enterprise.context.Dependent;
040import javax.inject.Inject;
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    @Inject
070    AccountingControl accountingControl;
071
072    @Inject
073    InventoryConditionControl inventoryConditionControl;
074
075    @Inject
076    ItemControl itemControl;
077
078    @Inject
079    UomControl uomControl;
080
081    
082    /** Creates a new instance of CreateItemPriceCommand */
083    public CreateItemPriceCommand() {
084        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
085    }
086    
087    @Override
088    protected BaseResult execute() {
089        var itemName = form.getItemName();
090        var item = itemControl.getItemByName(itemName);
091        
092        if(item != null) {
093            var inventoryConditionName = form.getInventoryConditionName();
094            var inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
095            
096            if(inventoryCondition != null) {
097                var inventoryConditionUseType = inventoryConditionControl.getInventoryConditionUseTypeByName(InventoryConditionUseTypes.SALES_ORDER.name());
098                var inventoryConditionUse = inventoryConditionControl.getInventoryConditionUse(inventoryConditionUseType,
099                        inventoryCondition);
100                
101                if(inventoryConditionUse != null) {
102                    var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
103                    var itemDetail = item.getLastDetail();
104                    var unitOfMeasureKind = itemDetail.getUnitOfMeasureKind();
105                    var unitOfMeasureType = uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
106                    
107                    if(unitOfMeasureType != null) {
108                        var itemUnitOfMeasureType = itemControl.getItemUnitOfMeasureType(item, unitOfMeasureType);
109                        
110                        if(itemUnitOfMeasureType != null) {
111                            var currencyIsoName = form.getCurrencyIsoName();
112                            var currency = accountingControl.getCurrencyByIsoName(currencyIsoName);
113                            
114                            if(currency != null) {
115                                var itemPrice = itemControl.getItemPrice(item, inventoryCondition, unitOfMeasureType, currency);
116                                
117                                if(itemPrice == null) {
118                                    var itemPriceTypeName = itemDetail.getItemPriceType().getItemPriceTypeName();
119                                    BasePK createdBy = getPartyPK();
120                                    
121                                    if(itemPriceTypeName.equals(ItemPriceTypes.FIXED.name())) {
122                                        var strUnitPrice = form.getUnitPrice();
123                                        
124                                        if(strUnitPrice != null) {
125                                            var unitPrice = Long.valueOf(strUnitPrice);
126                                            
127                                            itemPrice = itemControl.createItemPrice(item, inventoryCondition, unitOfMeasureType,
128                                                    currency, createdBy);
129                                            itemControl.createItemFixedPrice(itemPrice, unitPrice, createdBy);
130                                        } else {
131                                            addExecutionError(ExecutionErrors.MissingUnitPrice.name());
132                                        }
133                                    } else if(itemPriceTypeName.equals(ItemPriceTypes.VARIABLE.name())) {
134                                        var strMinimumUnitPrice = form.getMinimumUnitPrice();
135                                        Long minimumUnitPrice = null;
136                                        var strMaximumUnitPrice = form.getMaximumUnitPrice();
137                                        Long maximumUnitPrice = null;
138                                        var strUnitPriceIncrement = form.getUnitPriceIncrement();
139                                        Long unitPriceIncrement = null;
140                                        
141                                        if(strMinimumUnitPrice != null) {
142                                            minimumUnitPrice = Long.valueOf(strMinimumUnitPrice);
143                                        } else {
144                                            addExecutionError(ExecutionErrors.MissingMinimumUnitPrice.name());
145                                        }
146                                        
147                                        if(strMaximumUnitPrice != null) {
148                                            maximumUnitPrice = Long.valueOf(strMaximumUnitPrice);
149                                        } else {
150                                            addExecutionError(ExecutionErrors.MissingMaximumUnitPrice.name());
151                                        }
152                                        
153                                        if(strUnitPriceIncrement != null) {
154                                            unitPriceIncrement = Long.valueOf(strUnitPriceIncrement);
155                                        } else {
156                                            addExecutionError(ExecutionErrors.MissingUnitPriceIncrement.name());
157                                        }
158                                        
159                                        if(minimumUnitPrice != null && maximumUnitPrice != null && unitPriceIncrement != null) {
160                                            itemPrice = itemControl.createItemPrice(item, inventoryCondition, unitOfMeasureType,
161                                                    currency, createdBy);
162                                            itemControl.createItemVariablePrice(itemPrice, minimumUnitPrice, maximumUnitPrice,
163                                                    unitPriceIncrement, createdBy);
164                                        }
165                                    } else {
166                                        addExecutionError(ExecutionErrors.UnknownItemPriceType.name(), itemPriceTypeName);
167                                    }
168                                } else {
169                                    addExecutionError(ExecutionErrors.DuplicateItemPrice.name());
170                                }
171                            } else {
172                                addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
173                            }
174                        } else {
175                            addExecutionError(ExecutionErrors.UnknownItemUnitOfMeasureType.name(), itemName, unitOfMeasureTypeName);
176                        }
177                    } else {
178                        addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
179                    }
180                } else {
181                    addExecutionError(ExecutionErrors.InvalidInventoryCondition.name(), inventoryConditionName);
182                }
183            } else {
184                addExecutionError(ExecutionErrors.UnknownInventoryConditionName.name(), inventoryConditionName);
185            }
186        } else {
187            addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
188        }
189        
190        return null;
191    }
192    
193}