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