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