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