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