001// -------------------------------------------------------------------------------- 002// Copyright 2002-2024 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.accounting.server.entity.Currency; 029import com.echothree.model.data.associate.server.entity.AssociateReferral; 030import com.echothree.model.data.inventory.server.entity.InventoryCondition; 031import com.echothree.model.data.item.server.entity.Item; 032import com.echothree.model.data.offer.server.entity.OfferItemFixedPrice; 033import com.echothree.model.data.offer.server.entity.OfferItemPrice; 034import com.echothree.model.data.offer.server.entity.OfferUse; 035import com.echothree.model.data.offer.server.entity.Source; 036import com.echothree.model.data.order.server.entity.Order; 037import com.echothree.model.data.order.server.entity.OrderLine; 038import com.echothree.model.data.order.server.entity.OrderType; 039import com.echothree.model.data.party.common.pk.PartyPK; 040import com.echothree.model.data.party.server.entity.Party; 041import com.echothree.model.data.party.server.entity.PartyDepartment; 042import com.echothree.model.data.party.server.entity.PartyDivision; 043import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 044import com.echothree.model.data.user.server.entity.UserVisit; 045import com.echothree.model.data.wishlist.server.entity.WishlistType; 046import com.echothree.model.data.wishlist.server.entity.WishlistPriority; 047import com.echothree.util.common.message.ExecutionErrors; 048import com.echothree.util.server.control.BaseLogic; 049import com.echothree.util.server.message.ExecutionErrorAccumulator; 050import com.echothree.util.server.persistence.Session; 051 052public class WishlistLogic 053 extends BaseLogic { 054 055 private WishlistLogic() { 056 super(); 057 } 058 059 private static class WishlistLogicHolder { 060 static WishlistLogic instance = new WishlistLogic(); 061 } 062 063 public static WishlistLogic getInstance() { 064 return WishlistLogicHolder.instance; 065 } 066 067 private OfferUse getOrderOfferUse(final UserVisit userVisit, final OfferUse offerUse, final Party companyParty) { 068 var partyControl = Session.getModelController(PartyControl.class); 069 OfferUse orderOfferUse = userVisit.getOfferUse(); 070 071 if(orderOfferUse == null) { 072 orderOfferUse = offerUse; 073 } else { 074 Party orderDepartmentParty = offerUse.getLastDetail().getOffer().getLastDetail().getDepartmentParty(); 075 PartyDepartment orderPartyDepartment = partyControl.getPartyDepartment(orderDepartmentParty); 076 Party orderDivisionParty = orderPartyDepartment.getDivisionParty(); 077 PartyDivision orderPartyDivision = partyControl.getPartyDivision(orderDivisionParty); 078 Party orderCompanyParty = orderPartyDivision.getCompanyParty(); 079 080 if(!orderCompanyParty.equals(companyParty)) { 081 orderOfferUse = offerUse; 082 } 083 } 084 085 return orderOfferUse; 086 } 087 088 public void createWishlistLine(final Session session, final ExecutionErrorAccumulator ema, final UserVisit userVisit, final Party party, final Source source, 089 final OfferItemPrice offerItemPrice, final WishlistType wishlistType, final WishlistPriority wishlistPriority, final Long quantity, 090 final String comment, final PartyPK createdBy) { 091 Item item = offerItemPrice.getOfferItem().getItem(); 092 String itemPriceTypeName = item.getLastDetail().getItemPriceType().getItemPriceTypeName(); 093 094 if(itemPriceTypeName.equals(ItemPriceTypes.FIXED.name())) { 095 var partyControl = Session.getModelController(PartyControl.class); 096 var wishlistControl = Session.getModelController(WishlistControl.class); 097 OrderLogic orderLogic = OrderLogic.getInstance(); 098 Currency currency = offerItemPrice.getCurrency(); 099 OfferUse offerUse = source.getLastDetail().getOfferUse(); 100 Party departmentParty = offerUse.getLastDetail().getOffer().getLastDetail().getDepartmentParty(); 101 PartyDepartment partyDepartment = partyControl.getPartyDepartment(departmentParty); 102 Party divisionParty = partyDepartment.getDivisionParty(); 103 PartyDivision partyDivision = partyControl.getPartyDivision(divisionParty); 104 Party companyParty = partyDivision.getCompanyParty(); 105 Order order = wishlistControl.getWishlist(companyParty, party, wishlistType, currency); 106 107 if(order == null) { 108 OrderType orderType = orderLogic.getOrderTypeByName(ema, OrderTypes.WISHLIST.name()); 109 110 if(!ema.hasExecutionErrors()) { 111 order = orderLogic.createOrder(ema, orderType, null, null, currency, null, null, null, null, null, null, null, null, null, null, null, createdBy); 112 113 if(!ema.hasExecutionErrors()) { 114 var orderRoleControl = Session.getModelController(OrderRoleControl.class); 115 116 wishlistControl.createWishlist(order, getOrderOfferUse(userVisit, offerUse, companyParty), wishlistType, createdBy); 117 118 orderRoleControl.createOrderRoleUsingNames(order, companyParty, OrderRoleTypes.BILL_FROM.name(), createdBy); 119 orderRoleControl.createOrderRoleUsingNames(order, party, OrderRoleTypes.BILL_TO.name(), createdBy); 120 } 121 } 122 } 123 124 if(!ema.hasExecutionErrors()) { 125 InventoryCondition inventoryCondition = offerItemPrice.getInventoryCondition(); 126 UnitOfMeasureType unitOfMeasureType = offerItemPrice.getUnitOfMeasureType(); 127 OrderLine orderLine = wishlistControl.getWishlistLineByItemForUpdate(order, item, inventoryCondition, unitOfMeasureType); 128 129 if(orderLine == null) { 130 var offerItemControl = Session.getModelController(OfferItemControl.class); 131 OfferItemFixedPrice offerItemFixedPrice = offerItemControl.getOfferItemFixedPrice(offerItemPrice); 132 Long unitAmount = offerItemFixedPrice.getUnitPrice(); 133 AssociateReferral associateReferral = userVisit.getAssociateReferral(); 134 135 orderLine = SalesOrderLineLogic.getInstance().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}