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.server.control.InventoryTransactionTimeControl;
020import com.echothree.model.data.inventory.server.entity.InventoryTransactionLine;
021import com.echothree.model.data.inventory.server.entity.InventoryTransactionLineTime;
022import com.echothree.model.data.inventory.server.entity.InventoryTransactionTimeType;
023import com.echothree.util.common.persistence.BasePK;
024import com.echothree.util.server.control.BaseLogic;
025import com.echothree.util.server.message.ExecutionErrorAccumulator;
026import javax.enterprise.context.ApplicationScoped;
027import javax.inject.Inject;
028
029@ApplicationScoped
030public class InventoryTransactionLineTimeLogic
031        extends BaseLogic {
032
033    @Inject
034    InventoryTransactionTimeControl inventoryTransactionTimeControl;
035
036    @Inject
037    InventoryTransactionTimeTypeLogic inventoryTransactionTimeTypeLogic;
038
039    protected InventoryTransactionLineTimeLogic() {
040        super();
041    }
042
043    public InventoryTransactionLineTime createInventoryTransactionLineTime(final ExecutionErrorAccumulator eea,
044            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionTimeTypeName,
045            final Long time, final BasePK createdBy) {
046        var inventoryTransactionTimeType = getInventoryTransactionTimeTypeByName(eea, inventoryTransactionLine, inventoryTransactionTimeTypeName);
047        InventoryTransactionLineTime inventoryTransactionLineTime = null;
048
049        if(eea == null || !eea.hasExecutionErrors()) {
050            inventoryTransactionLineTime = createInventoryTransactionLineTime(inventoryTransactionLine, inventoryTransactionTimeType, time, createdBy);
051        }
052
053        return inventoryTransactionLineTime;
054    }
055
056    public InventoryTransactionLineTime createInventoryTransactionLineTime(final InventoryTransactionLine inventoryTransactionLine,
057            final InventoryTransactionTimeType inventoryTransactionTimeType, final Long time, final BasePK createdBy) {
058        return inventoryTransactionTimeControl.createInventoryTransactionLineTime(inventoryTransactionLine, inventoryTransactionTimeType, time, createdBy);
059    }
060
061    private InventoryTransactionTimeType getInventoryTransactionTimeTypeByName(final ExecutionErrorAccumulator eea,
062            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionTimeTypeName) {
063        return inventoryTransactionTimeTypeLogic.getInventoryTransactionTimeTypeByName(eea,
064                inventoryTransactionLine.getLastDetail().getInventoryTransaction().getLastDetail().getInventoryTransactionType(), inventoryTransactionTimeTypeName);
065    }
066
067    public void createOrUpdateInventoryTransactionLineTime(final ExecutionErrorAccumulator eea,
068            final InventoryTransactionLine inventoryTransactionLine, final String inventoryTransactionTimeTypeName,
069            final Long time, final BasePK createdBy) {
070        var inventoryTransactionTimeType = getInventoryTransactionTimeTypeByName(eea, inventoryTransactionLine, inventoryTransactionTimeTypeName);
071
072        if(eea == null || !eea.hasExecutionErrors()) {
073            var inventoryTransactionLineTime = inventoryTransactionTimeControl.getInventoryTransactionLineTime(inventoryTransactionLine, inventoryTransactionTimeType);
074
075            if(inventoryTransactionLineTime == null) {
076                inventoryTransactionTimeControl.createInventoryTransactionLineTime(inventoryTransactionLine, inventoryTransactionTimeType, time, createdBy);
077            } else {
078                var inventoryTransactionLineTimeValue = inventoryTransactionTimeControl.getInventoryTransactionLineTimeValue(inventoryTransactionLineTime);
079
080                inventoryTransactionLineTimeValue.setTime(time);
081
082                inventoryTransactionTimeControl.updateInventoryTransactionLineTimeFromValue(inventoryTransactionLineTimeValue, createdBy);
083            }
084        }
085    }
086
087    public void deleteInventoryTransactionLineTime(final ExecutionErrorAccumulator eea, final InventoryTransactionLineTime inventoryTransactionLineTime,
088            final BasePK deletedBy) {
089        inventoryTransactionTimeControl.deleteInventoryTransactionLineTime(inventoryTransactionLineTime, deletedBy);
090    }
091
092}