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.InventoryConditionControl;
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 java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
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    @Inject
063    AccountingControl accountingControl;
064
065    @Inject
066    InventoryConditionControl inventoryConditionControl;
067
068    @Inject
069    ItemControl itemControl;
070
071    @Inject
072    OfferItemControl offerItemControl;
073
074    @Inject
075    PartyControl partyControl;
076
077    @Inject
078    SourceControl sourceControl;
079
080    @Inject
081    UomControl uomControl;
082
083    @Inject
084    WishlistControl wishlistControl;
085
086    @Inject
087    WishlistLogic wishlistLogic;
088
089    
090    /** Creates a new instance of CreateWishlistLineCommand */
091    public CreateWishlistLineCommand() {
092        super(null, FORM_FIELD_DEFINITIONS, false);
093    }
094    
095    @Override
096    protected BaseResult execute() {
097        var partyName = form.getPartyName();
098        var party = partyName == null? getParty(): partyControl.getPartyByName(partyName);
099        
100        if(party != null) {
101            var partyTypeName = party.getLastDetail().getPartyType().getPartyTypeName();
102            
103            if(partyTypeName.equals(PartyTypes.CUSTOMER.name())) {
104                var wishlistTypeName = form.getWishlistTypeName();
105                var wishlistType = wishlistTypeName == null? wishlistControl.getDefaultWishlistType():
106                    wishlistControl.getWishlistTypeByName(wishlistTypeName);
107                
108                if(wishlistType != null) {
109                    var wishlistPriorityName = form.getWishlistPriorityName();
110                    var wishlistPriority = wishlistPriorityName == null? wishlistControl.getDefaultWishlistPriority(wishlistType):
111                        wishlistControl.getWishlistPriorityByName(wishlistType, wishlistPriorityName);
112                    
113                    if(wishlistPriority != null) {
114                        var currencyIsoName = form.getCurrencyIsoName();
115                        var currency = currencyIsoName == null? accountingControl.getDefaultCurrency():
116                            accountingControl.getCurrencyByIsoName(currencyIsoName);
117                        
118                        if(currency != null) {
119                            var sourceName = form.getSourceName();
120                            var source = sourceName == null? sourceControl.getDefaultSource(): sourceControl.getSourceByName(sourceName);
121                            
122                            if(source != null) {
123                                var itemName = form.getItemName();
124                                var item = itemControl.getItemByName(itemName);
125                                
126                                if(item != null) {
127                                    var offer = source.getLastDetail().getOfferUse().getLastDetail().getOffer();
128                                    var offerItem = offerItemControl.getOfferItem(offer, item);
129                                    
130                                    if(offerItem != null) {
131                                        var inventoryConditionName = form.getInventoryConditionName();
132                                        var inventoryConditionUseType = inventoryConditionControl.getInventoryConditionUseTypeByName(InventoryConditionUseTypes.SALES_ORDER.name());
133                                        InventoryCondition inventoryCondition = null;
134                                        
135                                        if(inventoryConditionName == null) {
136                                            var inventoryConditionUse = inventoryConditionControl.getDefaultInventoryConditionUse(inventoryConditionUseType);
137                                            
138                                            if(inventoryConditionUse == null) {
139                                                addExecutionError(ExecutionErrors.MissingDefaultInventoryConditionUse.name());
140                                            } else {
141                                                inventoryCondition = inventoryConditionUse.getInventoryCondition();
142                                            }
143                                        } else {
144                                            inventoryCondition = inventoryConditionControl.getInventoryConditionByName(inventoryConditionName);
145                                            
146                                            if(inventoryConditionControl.getInventoryConditionUse(inventoryConditionUseType, inventoryCondition) == null) {
147                                                addExecutionError(ExecutionErrors.InvalidInventoryCondition.name());
148                                            }
149                                        }
150                                        
151                                        if(!hasExecutionErrors()) {
152                                            var unitOfMeasureKind = item.getLastDetail().getUnitOfMeasureKind();
153                                            var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
154                                            var unitOfMeasureType = unitOfMeasureTypeName == null? uomControl.getDefaultUnitOfMeasureType(unitOfMeasureKind):
155                                                uomControl.getUnitOfMeasureTypeByName(unitOfMeasureKind, unitOfMeasureTypeName);
156                                            
157                                            if(unitOfMeasureType != null) {
158                                                var offerItemPrice = offerItemControl.getOfferItemPrice(offerItem, inventoryCondition, unitOfMeasureType, currency);
159                                                
160                                                if(offerItemPrice != null) {
161                                                    var strQuantity = form.getQuantity();
162                                                    Long quantity = strQuantity == null ? 1L : Long.valueOf(strQuantity);
163                                                    var comment = form.getComment();
164
165                                                    wishlistLogic.createWishlistLine(session, this, getUserVisit(), party, source, offerItemPrice,
166                                                            wishlistType, wishlistPriority, quantity, comment, getPartyPK());
167                                                } else {
168                                                    addExecutionError(ExecutionErrors.UnknownOfferItemPrice.name(),
169                                                            offerItem.getOffer().getLastDetail().getOfferName(),
170                                                            item.getLastDetail().getItemName(),
171                                                            inventoryCondition.getLastDetail().getInventoryConditionName(),
172                                                            unitOfMeasureType.getLastDetail().getUnitOfMeasureTypeName(),
173                                                            currency.getCurrencyIsoName());
174                                                }
175                                            } else {
176                                                addExecutionError(ExecutionErrors.UnknownUnitOfMeasureTypeName.name(), unitOfMeasureTypeName);
177                                            }
178                                        }
179                                    } else {
180                                        addExecutionError(ExecutionErrors.UnknownOfferItem.name(), offer.getLastDetail().getOfferName(), itemName);
181                                    }
182                                } else {
183                                    addExecutionError(ExecutionErrors.UnknownItemName.name(), itemName);
184                                }
185                            } else {
186                                addExecutionError(ExecutionErrors.UnknownSourceName.name(), sourceName);
187                            }
188                        } else {
189                            addExecutionError(ExecutionErrors.UnknownCurrencyIsoName.name(), currencyIsoName);
190                        }
191                    } else {
192                        addExecutionError(ExecutionErrors.UnknownWishlistPriorityName.name(), wishlistPriorityName);
193                    }
194                } else {
195                    addExecutionError(ExecutionErrors.UnknownWishlistTypeName.name(), wishlistTypeName);
196                }
197            } else {
198                addExecutionError(ExecutionErrors.InvalidPartyType.name(), partyTypeName);
199            }
200        } else {
201            addExecutionError(ExecutionErrors.UnknownPartyName.name(), partyName);
202        }
203        
204        return null;
205    }
206
207}