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.payment.server.logic;
018
019import com.echothree.model.control.contact.common.ContactMechanismPurposes;
020import com.echothree.model.control.contact.server.control.ContactControl;
021import com.echothree.model.control.payment.common.BillingAccountRoleTypes;
022import com.echothree.model.control.payment.server.control.BillingControl;
023import com.echothree.model.data.accounting.server.entity.Currency;
024import com.echothree.model.data.contact.server.entity.PartyContactMechanism;
025import com.echothree.model.data.party.server.entity.Party;
026import com.echothree.model.data.payment.server.entity.BillingAccount;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.persistence.BasePK;
029import com.echothree.util.server.message.ExecutionErrorAccumulator;
030import com.echothree.util.server.persistence.Session;
031import javax.enterprise.context.ApplicationScoped;
032import javax.enterprise.inject.spi.CDI;
033
034@ApplicationScoped
035public class BillingAccountLogic {
036
037    protected BillingAccountLogic() {
038        super();
039    }
040
041    public static BillingAccountLogic getInstance() {
042        return CDI.current().select(BillingAccountLogic.class).get();
043    }
044
045    public BillingAccount getBillingAccount(final ExecutionErrorAccumulator ema, final Party billFrom, PartyContactMechanism billFromPartyContactMechanism,
046            final Party billTo, PartyContactMechanism billToPartyContactMechanism, final Currency currency, final String reference, final String description, final BasePK createdBy) {
047        var billingControl = Session.getModelController(BillingControl.class);
048        var billingAccount = billingControl.getBillingAccount(billFrom, billTo, currency);
049
050        // If the BillingAccount was found, the billFromPartyContactMechanism and billToPartyContactMechanism parameters are ignored. They're used only
051        // when a new BillingAccount needs to be created.
052        if(billingAccount == null) {
053            if(billFromPartyContactMechanism == null || billToPartyContactMechanism == null) {
054                var contactControl = Session.getModelController(ContactControl.class);
055                var contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(ContactMechanismPurposes.PHYSICAL_BILLING.name());
056                
057                if(billFromPartyContactMechanism == null) {
058                    var partyContactMechanismPurpose = contactControl.getDefaultPartyContactMechanismPurpose(billFrom, contactMechanismPurpose);
059                    
060                    if(partyContactMechanismPurpose != null) {
061                        billFromPartyContactMechanism = partyContactMechanismPurpose.getLastDetail().getPartyContactMechanism();
062                    } else {
063                        ema.addExecutionError(ExecutionErrors.UnknownBillFromPartyContactMechanismPurpose.name());
064                    }
065                } else {
066                    // Make sure that the manually chosen PartyContactMechanism is marked with the PHYSICAL_BILLING ContactMechanismPurpose
067                    var partyContactMechanismPurpose = contactControl.getPartyContactMechanismPurpose(billFromPartyContactMechanism, contactMechanismPurpose);
068                    
069                    if(partyContactMechanismPurpose == null) {
070                        contactControl.createPartyContactMechanismPurpose(billFromPartyContactMechanism, contactMechanismPurpose, false, 1, createdBy);
071                    }
072                }
073
074                if(billToPartyContactMechanism == null) {
075                    var partyContactMechanismPurpose = contactControl.getDefaultPartyContactMechanismPurpose(billTo, contactMechanismPurpose);
076                    
077                    if(partyContactMechanismPurpose != null) {
078                        billToPartyContactMechanism = partyContactMechanismPurpose.getLastDetail().getPartyContactMechanism();
079                    } else {
080                        ema.addExecutionError(ExecutionErrors.UnknownBillToPartyContactMechanismPurpose.name());
081                    }
082                } else {
083                    // Make sure that the manually chosen PartyContactMechanism is marked with the PHYSICAL_BILLING ContactMechanismPurpose
084                    var partyContactMechanismPurpose = contactControl.getPartyContactMechanismPurpose(billToPartyContactMechanism, contactMechanismPurpose);
085                    
086                    if(partyContactMechanismPurpose == null) {
087                        contactControl.createPartyContactMechanismPurpose(billToPartyContactMechanism, contactMechanismPurpose, false, 1, createdBy);
088                    }
089                }
090            }
091            
092            if(!ema.hasExecutionErrors()) {
093                billingAccount = billingControl.createBillingAccount(billFrom, currency, reference, description, createdBy);
094                billingControl.createBillingAccountRoleUsingNames(billingAccount, billFrom, billFromPartyContactMechanism, BillingAccountRoleTypes.BILL_FROM.name(), createdBy);
095                billingControl.createBillingAccountRoleUsingNames(billingAccount, billTo, billToPartyContactMechanism, BillingAccountRoleTypes.BILL_TO.name(), createdBy);
096            }
097        }
098
099        return billingAccount;
100    }
101}