From 66bc5236d18cf9f631abd5f72345eb8591d6e86e Mon Sep 17 00:00:00 2001 From: Mathieu Lutfy Date: Tue, 26 Nov 2019 10:26:34 -0500 Subject: [PATCH] dev/financial#109 Fix country/province assignation in the contribution invoice --- CRM/Contribute/Form/Task/Invoice.php | 45 ++++++++++------------ CRM/Core/BAO/Address.php | 6 ++- tests/phpunit/CRM/Core/BAO/AddressTest.php | 3 ++ 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/CRM/Contribute/Form/Task/Invoice.php b/CRM/Contribute/Form/Task/Invoice.php index 9ff683534c..e9193efe7f 100644 --- a/CRM/Contribute/Form/Task/Invoice.php +++ b/CRM/Contribute/Form/Task/Invoice.php @@ -245,26 +245,17 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { $objects['contribution']->receive_date = CRM_Utils_Date::isoToMysql($objects['contribution']->receive_date); - $addressParams = ['contact_id' => $contribution->contact_id]; - $addressDetails = CRM_Core_BAO_Address::getValues($addressParams); - - // to get billing address if present + // Fetch the billing address. getValues should prioritize the billing + // address, otherwise will return the primary address. $billingAddress = []; - foreach ($addressDetails as $address) { - if (($address['is_billing'] == 1) && ($address['is_primary'] == 1) && ($address['contact_id'] == $contribution->contact_id)) { - $billingAddress[$address['contact_id']] = $address; - break; - } - elseif (($address['is_billing'] == 0 && $address['is_primary'] == 1) || ($address['is_billing'] == 1) && ($address['contact_id'] == $contribution->contact_id)) { - $billingAddress[$address['contact_id']] = $address; - } - } - if (!empty($billingAddress[$contribution->contact_id]['state_province_id'])) { - $stateProvinceAbbreviation = CRM_Core_PseudoConstant::stateProvinceAbbreviation($billingAddress[$contribution->contact_id]['state_province_id']); - } - else { - $stateProvinceAbbreviation = ''; + $addressDetails = CRM_Core_BAO_Address::getValues([ + 'contact_id' => $contribution->contact_id, + 'is_billing' => 1, + ]); + + if (!empty($addressDetails)) { + $billingAddress = array_shift($addressDetails); } if ($contribution->contribution_status_id == $refundedStatusId || $contribution->contribution_status_id == $cancelledStatusId) { @@ -405,13 +396,17 @@ class CRM_Contribute_Form_Task_Invoice extends CRM_Contribute_Form_Task { 'contribution_status_id' => $contribution->contribution_status_id, 'contributionStatusName' => CRM_Core_PseudoConstant::getName('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $contribution->contribution_status_id), 'subTotal' => $subTotal, - 'street_address' => CRM_Utils_Array::value('street_address', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), - 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), - 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), - 'supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), - 'city' => CRM_Utils_Array::value('city', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), - 'stateProvinceAbbreviation' => $stateProvinceAbbreviation, - 'postal_code' => CRM_Utils_Array::value('postal_code', CRM_Utils_Array::value($contribution->contact_id, $billingAddress)), + 'street_address' => CRM_Utils_Array::value('street_address', $billingAddress), + 'supplemental_address_1' => CRM_Utils_Array::value('supplemental_address_1', $billingAddress), + 'supplemental_address_2' => CRM_Utils_Array::value('supplemental_address_2', $billingAddress), + 'supplemental_address_3' => CRM_Utils_Array::value('supplemental_address_3', $billingAddress), + 'city' => CRM_Utils_Array::value('city', $billingAddress), + 'postal_code' => CRM_Utils_Array::value('postal_code', $billingAddress), + 'state_province' => CRM_Utils_Array::value('state_province', $billingAddress), + 'state_province_abbreviation' => CRM_Utils_Array::value('state_province_abbreviation', $billingAddress), + // Kept for backwards compatibility + 'stateProvinceAbbreviation' => CRM_Utils_Array::value('state_province_abbreviation', $billingAddress), + 'country' => CRM_Utils_Array::value('country', $billingAddress), 'is_pay_later' => $contribution->is_pay_later, 'organization_name' => $contribution->_relatedObjects['contact']->organization_name, 'domain_organization' => $domain->name, diff --git a/CRM/Core/BAO/Address.php b/CRM/Core/BAO/Address.php index 578df7b89b..4e04384ad5 100644 --- a/CRM/Core/BAO/Address.php +++ b/CRM/Core/BAO/Address.php @@ -495,15 +495,17 @@ class CRM_Core_BAO_Address extends CRM_Core_DAO_Address { if (!empty($address->state_province_id)) { $address->state = CRM_Core_PseudoConstant::stateProvinceAbbreviation($address->state_province_id, FALSE); $address->state_name = CRM_Core_PseudoConstant::stateProvince($address->state_province_id, FALSE); + $values['state_province_abbreviation'] = $address->state; + $values['state_province'] = $address->state_name; } if (!empty($address->country_id)) { $address->country = CRM_Core_PseudoConstant::country($address->country_id); + $values['country'] = $address->country; //get world region $regionId = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_Country', $address->country_id, 'region_id'); - - $address->world_region = CRM_Core_PseudoConstant::worldregion($regionId); + $values['world_region'] = CRM_Core_PseudoConstant::worldregion($regionId); } $address->addDisplay($microformat); diff --git a/tests/phpunit/CRM/Core/BAO/AddressTest.php b/tests/phpunit/CRM/Core/BAO/AddressTest.php index bf2ebbbe6a..634afb0a54 100644 --- a/tests/phpunit/CRM/Core/BAO/AddressTest.php +++ b/tests/phpunit/CRM/Core/BAO/AddressTest.php @@ -261,6 +261,9 @@ class CRM_Core_BAO_AddressTest extends CiviUnitTestCase { $address = CRM_Core_BAO_Address::getValues($entityBlock); $this->assertEquals($address[1]['id'], $addressId); $this->assertEquals($address[1]['contact_id'], $contactId); + $this->assertEquals($address[1]['state_province_abbreviation'], 'AL'); + $this->assertEquals($address[1]['state_province'], 'Alabama'); + $this->assertEquals($address[1]['country'], 'United States'); $this->assertEquals($address[1]['street_address'], 'Oberoi Garden'); $this->contactDelete($contactId); } -- 2.25.1