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.inject.Inject; 032 033@ApplicationScoped 034public class BillingAccountLogic { 035 036 @Inject 037 BillingControl billingControl; 038 039 @Inject 040 ContactControl contactControl; 041 042 protected BillingAccountLogic() { 043 super(); 044 } 045 046 public BillingAccount getBillingAccount(final ExecutionErrorAccumulator ema, final Party billFrom, PartyContactMechanism billFromPartyContactMechanism, 047 final Party billTo, PartyContactMechanism billToPartyContactMechanism, final Currency currency, final String reference, final String description, final BasePK createdBy) { 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 contactMechanismPurpose = contactControl.getContactMechanismPurposeByName(ContactMechanismPurposes.PHYSICAL_BILLING.name()); 055 056 if(billFromPartyContactMechanism == null) { 057 var partyContactMechanismPurpose = contactControl.getDefaultPartyContactMechanismPurpose(billFrom, contactMechanismPurpose); 058 059 if(partyContactMechanismPurpose != null) { 060 billFromPartyContactMechanism = partyContactMechanismPurpose.getLastDetail().getPartyContactMechanism(); 061 } else { 062 ema.addExecutionError(ExecutionErrors.UnknownBillFromPartyContactMechanismPurpose.name()); 063 } 064 } else { 065 // Make sure that the manually chosen PartyContactMechanism is marked with the PHYSICAL_BILLING ContactMechanismPurpose 066 var partyContactMechanismPurpose = contactControl.getPartyContactMechanismPurpose(billFromPartyContactMechanism, contactMechanismPurpose); 067 068 if(partyContactMechanismPurpose == null) { 069 contactControl.createPartyContactMechanismPurpose(billFromPartyContactMechanism, contactMechanismPurpose, false, 1, createdBy); 070 } 071 } 072 073 if(billToPartyContactMechanism == null) { 074 var partyContactMechanismPurpose = contactControl.getDefaultPartyContactMechanismPurpose(billTo, contactMechanismPurpose); 075 076 if(partyContactMechanismPurpose != null) { 077 billToPartyContactMechanism = partyContactMechanismPurpose.getLastDetail().getPartyContactMechanism(); 078 } else { 079 ema.addExecutionError(ExecutionErrors.UnknownBillToPartyContactMechanismPurpose.name()); 080 } 081 } else { 082 // Make sure that the manually chosen PartyContactMechanism is marked with the PHYSICAL_BILLING ContactMechanismPurpose 083 var partyContactMechanismPurpose = contactControl.getPartyContactMechanismPurpose(billToPartyContactMechanism, contactMechanismPurpose); 084 085 if(partyContactMechanismPurpose == null) { 086 contactControl.createPartyContactMechanismPurpose(billToPartyContactMechanism, contactMechanismPurpose, false, 1, createdBy); 087 } 088 } 089 } 090 091 if(!ema.hasExecutionErrors()) { 092 billingAccount = billingControl.createBillingAccount(billFrom, currency, reference, description, createdBy); 093 billingControl.createBillingAccountRoleUsingNames(billingAccount, billFrom, billFromPartyContactMechanism, BillingAccountRoleTypes.BILL_FROM.name(), createdBy); 094 billingControl.createBillingAccountRoleUsingNames(billingAccount, billTo, billToPartyContactMechanism, BillingAccountRoleTypes.BILL_TO.name(), createdBy); 095 } 096 } 097 098 return billingAccount; 099 } 100}