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