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