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.invoice.server.logic;
018
019import com.echothree.model.control.invoice.server.control.InvoiceControl;
020import com.echothree.model.data.invoice.server.entity.Invoice;
021import com.echothree.model.data.invoice.server.entity.InvoiceType;
022import com.echothree.util.common.message.ExecutionErrors;
023import com.echothree.util.common.persistence.BasePK;
024import com.echothree.util.server.message.ExecutionErrorAccumulator;
025import javax.enterprise.context.ApplicationScoped;
026import javax.inject.Inject;
027
028@ApplicationScoped
029public class InvoiceTimeLogic {
030
031    @Inject
032    InvoiceControl invoiceControl;
033
034    protected InvoiceTimeLogic() {
035        super();
036    }
037
038    private String getInvoiceTypeName(InvoiceType invoiceType) {
039        return invoiceType.getLastDetail().getInvoiceTypeName();
040    }
041
042    public void createOrUpdateInvoiceTimeIfNotNull(final ExecutionErrorAccumulator ema, final Invoice invoice, final String invoiceTimeTypeName, final Long time,
043            final BasePK partyPK) {
044        if(time != null) {
045            createOrUpdateInvoiceTime(ema, invoice, invoiceTimeTypeName, time, partyPK);
046        }
047    }
048
049    public void createOrUpdateInvoiceTime(final ExecutionErrorAccumulator ema, final Invoice invoice, final String invoiceTimeTypeName, final Long time,
050            final BasePK partyPK) {
051        var invoiceDetail = invoice.getLastDetail();
052        var invoiceType = invoiceDetail.getInvoiceType();
053        var invoiceTimeType = invoiceControl.getInvoiceTimeTypeByName(invoiceType, invoiceTimeTypeName);
054
055        if(invoiceTimeType == null) {
056            if(ema != null) {
057                ema.addExecutionError(ExecutionErrors.UnknownInvoiceTimeTypeName.name(), getInvoiceTypeName(invoiceType), invoiceTimeTypeName);
058            }
059        } else {
060            var invoiceTimeValue = invoiceControl.getInvoiceTimeValueForUpdate(invoice, invoiceTimeType);
061
062            if(invoiceTimeValue == null) {
063                invoiceControl.createInvoiceTime(invoice, invoiceTimeType, time, partyPK);
064            } else {
065                invoiceTimeValue.setTime(time);
066                invoiceControl.updateInvoiceTimeFromValue(invoiceTimeValue, partyPK);
067            }
068        }
069    }
070
071    public Long getInvoiceTime(final ExecutionErrorAccumulator ema, final Invoice invoice, final String invoiceTimeTypeName) {
072        var invoiceDetail = invoice.getLastDetail();
073        var invoiceType = invoiceDetail.getInvoiceType();
074        var invoiceTimeType = invoiceControl.getInvoiceTimeTypeByName(invoiceType, invoiceTimeTypeName);
075        Long result = null;
076
077        if(invoiceTimeType == null) {
078            if(ema != null) {
079                ema.addExecutionError(ExecutionErrors.UnknownInvoiceTimeTypeName.name(), getInvoiceTypeName(invoiceType), invoiceTimeTypeName);
080            }
081        } else {
082            var invoiceTime = invoiceControl.getInvoiceTime(invoice, invoiceTimeType);
083
084            if(invoiceTime == null) {
085                if(ema != null) {
086                    ema.addExecutionError(ExecutionErrors.UnknownInvoiceTime.name(), getInvoiceTypeName(invoiceType), invoiceDetail.getInvoiceName(), invoiceTimeTypeName);
087                }
088            } else {
089                result = invoiceTime.getTime();
090            }
091        }
092
093        return result;
094    }
095
096    public void deleteInvoiceTime(final ExecutionErrorAccumulator ema, final Invoice invoice, final String invoiceTimeTypeName, final BasePK deletedBy) {
097        var invoiceDetail = invoice.getLastDetail();
098        var invoiceType = invoiceDetail.getInvoiceType();
099        var invoiceTimeType = invoiceControl.getInvoiceTimeTypeByName(invoiceType, invoiceTimeTypeName);
100
101        if(invoiceTimeType == null) {
102            if(ema != null) {
103                ema.addExecutionError(ExecutionErrors.UnknownInvoiceTimeTypeName.name(), getInvoiceTypeName(invoiceType), invoiceTimeTypeName);
104            }
105        } else {
106            var invoiceTime = invoiceControl.getInvoiceTimeForUpdate(invoice, invoiceTimeType);
107
108            if(invoiceTime == null) {
109                if(ema != null) {
110                    ema.addExecutionError(ExecutionErrors.UnknownInvoiceTime.name(), getInvoiceTypeName(invoiceType), invoiceDetail.getInvoiceName(), invoiceTimeTypeName);
111                }
112            } else {
113                invoiceControl.deleteInvoiceTime(invoiceTime, deletedBy);
114            }
115        }
116    }
117
118}