From a7dd4ccc73ef5054b3d20cd72ec7bb202d90f512 Mon Sep 17 00:00:00 2001 From: Vangelis Pantazis Date: Mon, 28 Feb 2022 17:45:14 +0000 Subject: [PATCH] Fixes issue with duplicate is_billing on inline address forms. --- CRM/Core/BAO/Address.php | 1 + CRM/Core/BAO/Block.php | 50 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 09e45a24c0..f6ee39d558 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -66,6 +66,7 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { CRM_Utils_Hook::pre($hook, 'Address', CRM_Utils_Array::value('id', $params), $params); CRM_Core_BAO_Block::handlePrimary($params, get_class()); + CRM_Core_BAO_Block::handleBilling($params, get_class()); // (prevent chaining 1 and 3) CRM-21214 if (isset($params['master_id']) && !CRM_Utils_System::isNull($params['master_id'])) { diff --git a/CRM/Core/BAO/Block.php b/CRM/Core/BAO/Block.php index 190ca1dad1..39519b96f9 100644 --- a/CRM/Core/BAO/Block.php +++ b/CRM/Core/BAO/Block.php @@ -401,6 +401,56 @@ class CRM_Core_BAO_Block { } } + /** + * Handling for is_billing. + * This process is a variation of handlePrimary above + * Find other entries with is_billing = 1 and reset them to 0 + * + * @param array $params + * @param $class + * + * @throws API_Exception + */ + public static function handleBilling(&$params, $class) { + if (isset($params['id']) && CRM_Utils_System::isNull($params['is_billing'] ?? NULL)) { + // if id is set & is_billing isn't we can assume no change) + return; + } + $table = CRM_Core_DAO_AllCoreTables::getTableForClass($class); + if (!$table) { + throw new API_Exception("Failed to locate table for class [$class]"); + } + + // contact_id in params might be empty or the string 'null' so cast to integer + $contactId = (int) ($params['contact_id'] ?? 0); + // If id is set & we haven't been passed a contact_id, retrieve it + if (!empty($params['id']) && !isset($params['contact_id'])) { + $entity = new $class(); + $entity->id = $params['id']; + $entity->find(TRUE); + $contactId = $entity->contact_id; + } + // If entity is not associated with contact, concept of is_billing not relevant + if (!$contactId) { + return; + } + + // if params is_billing then set all others to not be billing & exit out + // if is_billing = 1 + if (!empty($params['is_billing'])) { + $sql = "UPDATE $table SET is_billing = 0 WHERE contact_id = %1"; + $sqlParams = [1 => [$contactId, 'Integer']]; + // we don't want to create unnecessary entries in the log_ tables so exclude the one we are working on + if (!empty($params['id'])) { + $sql .= " AND id <> %2"; + $sqlParams[2] = [$params['id'], 'Integer']; + } + CRM_Core_DAO::executeQuery($sql, $sqlParams); + return; + } + + } + /** * Sort location array so primary element is first. * -- 2.25.1