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