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.inventory.server.logic; 018 019import com.echothree.model.control.inventory.common.exception.DuplicateInventoryLayerBucketException; 020import com.echothree.model.control.inventory.common.exception.InventoryLayerBucketInUseException; 021import com.echothree.model.control.inventory.server.control.BucketControl; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.data.inventory.server.entity.InventoryBucketType; 024import com.echothree.model.data.inventory.server.entity.InventoryLayer; 025import com.echothree.model.data.inventory.server.entity.InventoryLayerBucket; 026import com.echothree.model.data.inventory.server.value.InventoryLayerBucketValue; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.persistence.BasePK; 029import com.echothree.util.server.control.BaseLogic; 030import com.echothree.util.server.message.ExecutionErrorAccumulator; 031import java.util.List; 032import javax.enterprise.context.ApplicationScoped; 033import javax.inject.Inject; 034 035@ApplicationScoped 036public class InventoryLayerBucketLogic 037 extends BaseLogic { 038 039 @Inject 040 BucketControl bucketControl; 041 042 @Inject 043 PartyControl partyControl; 044 045 protected InventoryLayerBucketLogic() { 046 super(); 047 } 048 049 public InventoryLayerBucket createInventoryLayerBucket(final ExecutionErrorAccumulator eea, 050 final InventoryLayer inventoryLayer, final InventoryBucketType inventoryBucketType, 051 final Long quantity, final BasePK createdBy) { 052 var inventoryLayerBucket = getInventoryLayerBucket(inventoryLayer, inventoryBucketType); 053 054 if(inventoryLayerBucket == null) { 055 inventoryLayerBucket = bucketControl.createInventoryLayerBucket(inventoryLayer, 056 inventoryBucketType, quantity, createdBy); 057 } else { 058 var inventoryLayerDetail = inventoryLayer.getLastDetail(); 059 var inventoryCostingPoolDetail = inventoryLayerDetail.getInventoryCostingPool().getLastDetail(); 060 061 handleExecutionError(DuplicateInventoryLayerBucketException.class, eea, 062 ExecutionErrors.DuplicateInventoryLayerBucket.name(), 063 partyControl.getPartyCompany(inventoryCostingPoolDetail.getCompanyParty()).getPartyCompanyName(), 064 inventoryCostingPoolDetail.getItem().getLastDetail().getItemName(), 065 inventoryCostingPoolDetail.getInventoryCondition().getLastDetail().getInventoryConditionName(), 066 inventoryLayerDetail.getInventoryLayerSequence(), 067 inventoryBucketType.getLastDetail().getInventoryBucketTypeName()); 068 } 069 070 return inventoryLayerBucket; 071 } 072 073 public InventoryLayerBucket getInventoryLayerBucket(final InventoryLayer inventoryLayer, 074 final InventoryBucketType inventoryBucketType) { 075 return bucketControl.getInventoryLayerBucket(inventoryLayer, inventoryBucketType); 076 } 077 078 public void updateInventoryLayerBucketFromValue(final InventoryLayerBucketValue inventoryLayerBucketValue, 079 final BasePK updatedBy) { 080 bucketControl.updateInventoryLayerBucketFromValue(inventoryLayerBucketValue, updatedBy); 081 } 082 083 private void removeInventoryLayerBuckets(final ExecutionErrorAccumulator eea, 084 final List<InventoryLayerBucket> inventoryLayerBuckets, final BasePK removedBy) { 085 var inventoryLayerBucketInUse = inventoryLayerBuckets.stream() 086 .anyMatch(inventoryLayerBucket -> inventoryLayerBucket.getQuantity() != 0); 087 088 if(inventoryLayerBucketInUse) { 089 handleExecutionError(InventoryLayerBucketInUseException.class, eea, ExecutionErrors.InventoryLayerBucketInUse.name()); 090 } 091 092 if(eea == null || !eea.hasExecutionErrors()) { 093 bucketControl.removeInventoryLayerBuckets(inventoryLayerBuckets, removedBy); 094 } 095 } 096 097 public void removeInventoryLayerBucket(final ExecutionErrorAccumulator eea, 098 final InventoryLayerBucket inventoryLayerBucket, final BasePK removedBy) { 099 var inventoryLayerBucketForUpdate = bucketControl.getInventoryLayerBucketForUpdate( 100 inventoryLayerBucket.getInventoryLayer(), inventoryLayerBucket.getInventoryBucketType()); 101 102 removeInventoryLayerBuckets(eea, List.of(inventoryLayerBucketForUpdate), removedBy); 103 } 104 105}