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