Skip to content

Commit

Permalink
Fixes issue with duplicate is_billing on inline address forms.
Browse files Browse the repository at this point in the history
  • Loading branch information
VangelisP committed Feb 28, 2022
1 parent 7abc056 commit a7dd4cc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions CRM/Core/BAO/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static function add(&$params, $fixAddress = FALSE) {
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'])) {
Expand Down
50 changes: 50 additions & 0 deletions CRM/Core/BAO/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,56 @@ public static function handlePrimary(&$params, $class) {
}
}

/**
* 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.
*
Expand Down

0 comments on commit a7dd4cc

Please sign in to comment.