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.wishlist.server.command;
018
019import com.echothree.control.user.wishlist.common.form.CreateWishlistLineForm;
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.server.control.ItemControl;
024import com.echothree.model.control.offer.server.control.OfferItemControl;
025import com.echothree.model.control.offer.server.control.SourceControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.party.server.control.PartyControl;
028import com.echothree.model.control.uom.server.control.UomControl;
029import com.echothree.model.control.wishlist.server.control.WishlistControl;
030import com.echothree.model.control.wishlist.server.logic.WishlistLogic;
031import com.echothree.model.data.inventory.server.entity.InventoryCondition;
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.BaseSimpleCommand;
037import com.echothree.util.server.persistence.Session;
038import java.util.List;
039import javax.enterprise.context.Dependent;
040
041@Dependent
042public class CreateWishlistLineCommand
043        extends BaseSimpleCommand<CreateWishlistLineForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = List.of(
049            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
050            new FieldDefinition("WishlistTypeName", FieldType.ENTITY_NAME, false, null, null),
051            new FieldDefinition("WishlistPriorityName", FieldType.ENTITY_NAME, false, null, null),
052            new FieldDefinition("CurrencyIsoName", FieldType.ENTITY_NAME, false, null, null),
053            new FieldDefinition("SourceName", FieldType.ENTITY_NAME, false, null, null),
054            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
055            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
056            new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
057            new FieldDefinition("Quantity", FieldType.UNSIGNED_LONG, false, null, null),
058            new FieldDefinition("Comment", FieldType.STRING, false, 1L, 80L)
059        );
060    }
061    
062    /** Creates a new instance of CreateWishlistLineCommand */
063    public CreateWishlistLineCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, false);
065    }
066    
067    @Override
068    protected BaseResult execute() {
069        var partyControl = Session.getModelController(PartyControl.class);
070        var partyName = form.getPartyName();
071        var party = partyName == null? getParty(): partyControl.getPartyByName(partyName);
072        
073        if(party != null) {
074            var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
075            
076            if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) {
077                var wishlistControl = Session.getModelController(WishlistControl.class);
078                var wishlistTypeName = form.getWishlistTypeName();
079                var wishlistType = wishlistTypeName == null? wishlistControl.getDefaultWishlistType():
080                    wishlistControl.getWishlistTypeByName(wishlistTypeName);
081                
082                if(wishlistType != null) {
083                    var wishlistPriorityName = form.getWishlistPriorityName();
084                    var wishlistPriority = wishlistPriorityName == null? wishlistControl.getDefaultWishlistPriority(wishlistType):
085                        wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName);
086                    
087                    if(wishlistPriority != null) {
088                        var accountingControl = Session.getModelController(AccountingControl.class);
089                        var currencyIsoName = form.getCurrencyIsoName();
090                        var currency = currencyIsoName == null? accountingControl.getDefaultCurrency():
091                            accountingControl.getCurrencyByIsoName(currencyIsoName);
092                        
093                        if(currency != null) {
094                            var sourceControl = Session.getModelController(SourceControl.class);
095                            var sourceName = form.getSourceName();
096                            var source = sourceName == null? sourceControl.getDefaultSource(): sourceControl.getSourceByName(sourceName);
097                            
098                            if(source != null) {
099                                var itemControl = Session.getModelController(ItemControl.class);
100                                var itemName = form.getItemName();
101                                var item = itemControl.getItemByName(itemName);
102                                
103                                if(item != null) {
104                                    var offerItemControl = Session.getModelController(OfferItemControl.class);
105                                    var offer = source.getLastDetail().getOfferUse().getLastDetail().getOffer();
106                                    var offerItem = offerItemControl.getOfferItem(offer, item);
107                                    
108                                    if(offerItem != null) {
109                                        var inventoryControl = Session.getModelController(InventoryControl.class);
110                                        var inventoryConditionName = form.getInventoryConditionName();
111                                        var inventoryConditionUseType = inventoryControl.getInventoryConditionUseTypeByName(InventoryConditionUseTypes.SALES_ORDER.name());
112                                        InventoryCondition inventoryCondition = null;
113                                        
114                                        if(inventoryConditionName == null) {
115                                            var inventoryConditionUse = inventoryControl.getDefaultInventoryConditionUse(inventoryConditionUseType);
116                                            
117                                            if(inventoryConditionUse == null) {
118                                                addExecutionError(ExecutionErrors.MissingDefaultInventoryConditionUse.name());
119                                            } else {
120                                                inventoryCondition = inventoryConditionUse.getInventoryCondition();
121                                            }
122                                        } else {
123                                            inventoryCondition = inventoryControl.getInventoryConditionByName(inventoryConditionName);
124                                            
125                                            if(inventoryControl.getInventoryConditionUse(inventoryConditionUseType, inventoryCondition) == null) {
126                                                addExecutionError(ExecutionErrors.InvalidInventoryCondition.name());
127                                            }
128                                        }
129                                        
130                                        if(!hasExecutionErrors()) {
131                                            var uomControl = Session.getModelController(UomControl.class);
132                                            var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
133                                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
134                                            var unitOfMeasureType = unitOfMeasureTypeName == null? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind):
135                                                uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
136                                            
137                                            if(unitOfMeasureType != null) {
138                                                var offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency);
139                                                
140                                                if(offerItemPrice != null) {
141                                                    var strQuantity = form.getQuantity();
142                                                    Long quantity = strQuantity == null ? 1L : Long.valueOf(strQuantity);
143                                                    var comment = form.getComment();
144
145                                                    WishlistLogic.getInstance().createWishlistLine(session, this, getUserVisit(), party, source, offerItemPrice,
146                                                            wishlistType, wishlistPriority, quantity, comment, getPartyPK());
147                                                } else {
148                                                    addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(),
149                                                            offerItem.getOffer().getLastDetail().getOfferName(),
150                                                            item.getLastDetail().getItemName(),
151                                                            inventoryCondition.getLastDetail().getInventoryConditionName(),
152                                                            unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
153                                                            currency.getCurrencyIsoName());
154                                                }
155                                            } else {
156                                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
157                                            }
158                                        }
159                                    } else {
160                                        addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(), itemName);
161                                    }
162                                } else {
163                                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
164                                }
165                            } else {
166                                addExecutionError(ExecutionErrors.UnknownSourceName.name(), sourceName);
167                            }
168                        } else {
169                            addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
170                        }
171                    } else {
172                        addExecutionError(ExecutionErrors.UnknownWishlistPriorityName.name(), wishlistPriorityName);
173                    }
174                } else {
175                    addExecutionError(ExecutionErrors.UnknownWishlistTypeName.name(), wishlistTypeName);
176                }
177            } else {
178                addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName);
179            }
180        } else {
181            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
182        }
183        
184        return null;
185    }
186
187}