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.inventory.server.logic;
018
019import com.echothree.control.user.inventory.common.spec.LotUniversalSpec;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.common.exception.InvalidParameterCountException;
023import com.echothree.model.control.core.server.logic.EntityInstanceLogic;
024import com.echothree.model.control.inventory.common.exception.DuplicateLotNameException;
025import com.echothree.model.control.inventory.common.exception.UnknownLotNameException;
026import com.echothree.model.control.inventory.server.control.LotControl;
027import com.echothree.model.control.sequence.common.SequenceTypes;
028import com.echothree.model.control.sequence.server.logic.SequenceGeneratorLogic;
029import com.echothree.model.data.accounting.server.entity.Currency;
030import com.echothree.model.data.core.server.entity.EntityInstance;
031import com.echothree.model.data.inventory.server.entity.InventoryCondition;
032import com.echothree.model.data.inventory.server.entity.Lot;
033import com.echothree.model.data.item.server.entity.Item;
034import com.echothree.model.data.party.server.entity.Party;
035import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.persistence.BasePK;
038import com.echothree.util.server.control.BaseLogic;
039import com.echothree.util.server.message.ExecutionErrorAccumulator;
040import com.echothree.util.server.persistence.EntityPermission;
041import com.echothree.util.server.persistence.Session;
042
043public class LotLogic
044        extends BaseLogic {
045
046    private LotLogic() {
047        super();
048    }
049
050    private static class LotTimeLogicHolder {
051        static LotLogic instance = new LotLogic();
052    }
053
054    public static LotLogic getInstance() {
055        return LotTimeLogicHolder.instance;
056    }
057
058    public Lot createLot(final ExecutionErrorAccumulator eea, String lotName, final Party ownerParty, final Item item,
059            final UnitOfMeasureType unitOfMeasureType, final InventoryCondition inventoryCondition, final Long quantity,
060            final Currency currency, final Long unitCost, final BasePK createdBy) {
061        Lot lot = null;
062
063        if(lotName == null) {
064            lotName = SequenceGeneratorLogic.getInstance().getNextSequenceValue(eea, SequenceTypes.LOT.name());
065        }
066
067        if(!eea.hasExecutionErrors()) {
068            var lotControl = Session.getModelController(LotControl.class);
069
070            lot = lotControl.getLotByName(lotName);
071            if(lot == null) {
072                lot = lotControl.createLot(lotName, ownerParty, item, unitOfMeasureType, inventoryCondition, quantity,
073                        currency, unitCost, createdBy);
074            } else {
075                handleExecutionError(DuplicateLotNameException.class, eea, ExecutionErrors.DuplicateLotName.name(), lotName);
076            }
077        }
078
079        return lot;
080    }
081
082    public Lot getLotByName(final ExecutionErrorAccumulator eea, final String lotName,
083            final EntityPermission entityPermission) {
084        var lotControl = Session.getModelController(LotControl.class);
085        Lot lot = lotControl.getLotByName(lotName, entityPermission);
086
087        if(lot == null) {
088            handleExecutionError(UnknownLotNameException.class, eea, ExecutionErrors.UnknownLotName.name(), lotName);
089        }
090
091        return lot;
092    }
093
094    public Lot getLotByName(final ExecutionErrorAccumulator eea, final String lotName) {
095        return getLotByName(eea, lotName, EntityPermission.READ_ONLY);
096    }
097
098    public Lot getLotByNameForUpdate(final ExecutionErrorAccumulator eea, final String lotName) {
099        return getLotByName(eea, lotName, EntityPermission.READ_WRITE);
100    }
101
102    public Lot getLotByUniversalSpec(final ExecutionErrorAccumulator eea, final LotUniversalSpec universalSpec,
103            final EntityPermission entityPermission) {
104        Lot lot = null;
105        var lotControl = Session.getModelController(LotControl.class);
106        String lotName = universalSpec.getLotName();
107        var parameterCount = (lotName == null ? 0 : 1) + EntityInstanceLogic.getInstance().countPossibleEntitySpecs(universalSpec);
108
109        switch(parameterCount) {
110            case 1:
111                if(lotName == null) {
112                    var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(eea, universalSpec,
113                            ComponentVendors.ECHO_THREE.name(), EntityTypes.Lot.name());
114
115                    if(!eea.hasExecutionErrors()) {
116                        lot = lotControl.getLotByEntityInstance(entityInstance, entityPermission);
117                    }
118                } else {
119                    lot = getLotByName(eea, lotName, entityPermission);
120                }
121                break;
122            default:
123                handleExecutionError(InvalidParameterCountException.class, eea, ExecutionErrors.InvalidParameterCount.name());
124                break;
125        }
126
127        return lot;
128    }
129
130    public Lot getLotByUniversalSpec(final ExecutionErrorAccumulator eea,
131            final LotUniversalSpec universalSpec) {
132        return getLotByUniversalSpec(eea, universalSpec, EntityPermission.READ_ONLY);
133    }
134
135    public Lot getLotByUniversalSpecForUpdate(final ExecutionErrorAccumulator eea,
136            final LotUniversalSpec universalSpec) {
137        return getLotByUniversalSpec(eea, universalSpec, EntityPermission.READ_WRITE);
138    }
139
140    public void deleteLot(final ExecutionErrorAccumulator eea, final Lot lot,
141            final BasePK deletedBy) {
142        var lotControl = Session.getModelController(LotControl.class);
143
144        lotControl.deleteLot(lot, deletedBy);
145    }
146
147}