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.model.control.wishlist.server.logic; 018 019import com.echothree.model.control.item.common.ItemPriceTypes; 020import com.echothree.model.control.offer.server.control.OfferItemControl; 021import com.echothree.model.control.order.common.OrderRoleTypes; 022import com.echothree.model.control.order.common.OrderTypes; 023import com.echothree.model.control.order.server.control.OrderRoleControl; 024import com.echothree.model.control.order.server.logic.OrderLogic; 025import com.echothree.model.control.party.server.control.PartyControl; 026import com.echothree.model.control.sales.server.logic.SalesOrderLineLogic; 027import com.echothree.model.control.wishlist.server.control.WishlistControl; 028import com.echothree.model.data.offer.server.entity.OfferItemPrice; 029import com.echothree.model.data.offer.server.entity.OfferUse; 030import com.echothree.model.data.offer.server.entity.Source; 031import com.echothree.model.data.party.common.pk.PartyPK; 032import com.echothree.model.data.party.server.entity.Party; 033import com.echothree.model.data.user.server.entity.UserVisit; 034import com.echothree.model.data.wishlist.server.entity.WishlistPriority; 035import com.echothree.model.data.wishlist.server.entity.WishlistType; 036import com.echothree.util.common.message.ExecutionErrors; 037import com.echothree.util.server.control.BaseLogic; 038import com.echothree.util.server.message.ExecutionErrorAccumulator; 039import com.echothree.util.server.persistence.Session; 040import javax.enterprise.context.ApplicationScoped; 041import javax.enterprise.inject.spi.CDI; 042import javax.inject.Inject; 043 044@ApplicationScoped 045public class WishlistLogic 046 extends BaseLogic { 047 048 @Inject 049 OfferItemControl offerItemControl; 050 051 @Inject 052 OrderRoleControl orderRoleControl; 053 054 @Inject 055 PartyControl partyControl; 056 057 @Inject 058 WishlistControl wishlistControl; 059 060 @Inject 061 OrderLogic orderLogic; 062 063 @Inject 064 SalesOrderLineLogic salesOrderLineLogic; 065 066 protected WishlistLogic() { 067 super(); 068 } 069 070 public static WishlistLogic getInstance() { 071 return CDI.current().select(WishlistLogic.class).get(); 072 } 073 074 private OfferUse getOrderOfferUse(final UserVisit userVisit, final OfferUse offerUse, final Party companyParty) { 075 var orderOfferUse = userVisit.getOfferUse(); 076 077 if(orderOfferUse == null) { 078 orderOfferUse = offerUse; 079 } else { 080 var orderDepartmentParty = offerUse.getLastDetail().getOffer().getLastDetail().getDepartmentParty(); 081 var orderPartyDepartment = partyControl.getPartyDepartment(orderDepartmentParty); 082 var orderDivisionParty = orderPartyDepartment.getDivisionParty(); 083 var orderPartyDivision = partyControl.getPartyDivision(orderDivisionParty); 084 var orderCompanyParty = orderPartyDivision.getCompanyParty(); 085 086 if(!orderCompanyParty.equals(companyParty)) { 087 orderOfferUse = offerUse; 088 } 089 } 090 091 return orderOfferUse; 092 } 093 094 public void createWishlistLine(final Session session, final ExecutionErrorAccumulator ema, final UserVisit userVisit, final Party party, final Source source, 095 final OfferItemPrice offerItemPrice, final WishlistType wishlistType, final WishlistPriority wishlistPriority, final Long quantity, 096 final String comment, final PartyPK createdBy) { 097 var item = offerItemPrice.getOfferItem().getItem(); 098 var itemPriceTypeName = item.getLastDetail().getItemPriceType().getItemPriceTypeName(); 099 100 if(itemPriceTypeName.equals(ItemPriceTypes.FIXED.name())) { 101 var currency = offerItemPrice.getCurrency(); 102 var offerUse = source.getLastDetail().getOfferUse(); 103 var departmentParty = offerUse.getLastDetail().getOffer().getLastDetail().getDepartmentParty(); 104 var partyDepartment = partyControl.getPartyDepartment(departmentParty); 105 var divisionParty = partyDepartment.getDivisionParty(); 106 var partyDivision = partyControl.getPartyDivision(divisionParty); 107 var companyParty = partyDivision.getCompanyParty(); 108 var order = wishlistControl.getWishlist(companyParty, party, wishlistType, currency); 109 110 if(order == null) { 111 var orderType = orderLogic.getOrderTypeByName(ema, OrderTypes.WISHLIST.name()); 112 113 if(!ema.hasExecutionErrors()) { 114 order = orderLogic.createOrder(ema, orderType, null, null, currency, null, null, null, null, null, null, null, null, null, null, null, createdBy); 115 116 if(!ema.hasExecutionErrors()) { 117 wishlistControl.createWishlist(order, getOrderOfferUse(userVisit, offerUse, companyParty), wishlistType, createdBy); 118 119 orderRoleControl.createOrderRoleUsingNames(order, companyParty, OrderRoleTypes.BILL_FROM.name(), createdBy); 120 orderRoleControl.createOrderRoleUsingNames(order, party, OrderRoleTypes.BILL_TO.name(), createdBy); 121 } 122 } 123 } 124 125 if(!ema.hasExecutionErrors()) { 126 var inventoryCondition = offerItemPrice.getInventoryCondition(); 127 var unitOfMeasureType = offerItemPrice.getUnitOfMeasureType(); 128 var orderLine = wishlistControl.getWishlistLineByItemForUpdate(order, item, inventoryCondition, unitOfMeasureType); 129 130 if(orderLine == null) { 131 var offerItemFixedPrice = offerItemControl.getOfferItemFixedPrice(offerItemPrice); 132 var unitAmount = offerItemFixedPrice.getUnitPrice(); 133 var associateReferral = userVisit.getAssociateReferral(); 134 135 orderLine = salesOrderLineLogic.createOrderLine(session, ema, order, null, null, null, item, inventoryCondition, unitOfMeasureType, 136 quantity, unitAmount, null, null, null, null, createdBy); 137 138 if(!ema.hasExecutionErrors()) { 139 wishlistControl.createWishlistLine(orderLine, offerUse, wishlistPriority, associateReferral, comment, createdBy); 140 } 141 } else { 142 // TODO: add to wishlist quantity? 143 } 144 } 145 } else { 146 addExecutionError(ema, ExecutionErrors.InvalidItemPriceType.name(), itemPriceTypeName); 147 } 148 } 149 150 public void deleteWishlistLine() { 151 // TODO 152 // deleteWishlistLine(orderLine); 153 // deleteOrderLine(orderLine); 154 } 155 156}