Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20913 - Add separate option group for pledge status #10737

Merged
merged 5 commits into from
Jul 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 18 additions & 21 deletions CRM/Pledge/BAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public static function getHonorContacts($honorId) {
'honor_type' => CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_ContributionSoft', 'soft_credit_type_id', $honorDAO->soft_credit_type_id),
'honorId' => $pledgeDAO->contact_id,
'amount' => $pledgeDAO->amount,
'status' => CRM_Contribute_PseudoConstant::contributionStatus($pledgeDAO->status_id),
'status' => CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $pledgeDAO->status_id),
'create_date' => $pledgeDAO->create_date,
'acknowledge_date' => $pledgeDAO->acknowledge_date,
'type' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType',
Expand Down Expand Up @@ -790,7 +790,9 @@ public static function exportableFields($checkPermission = TRUE) {
*/
public static function getContactPledges($contactID) {
$pledgeDetails = array();
$pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name'
);

$status = array();

Expand Down Expand Up @@ -851,20 +853,15 @@ public static function updatePledgeStatus($params) {

$sendReminders = CRM_Utils_Array::value('send_reminders', $params, FALSE);

$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');

// unset statues that we never use for pledges
foreach (array(
'Completed',
'Cancelled',
'Failed',
) as $statusKey) {
if ($key = CRM_Utils_Array::key($statusKey, $allStatus)) {
unset($allStatus[$key]);
}
}
$allStatus = array_flip(CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'));
$allPledgeStatus = CRM_Core_OptionGroup::values('pledge_status',
TRUE, FALSE, FALSE, NULL, 'name', TRUE
);
unset($allPledgeStatus['Completed'], $allPledgeStatus['Cancelled']);
unset($allStatus['Completed'], $allStatus['Cancelled'], $allStatus['Failed']);

$statusIds = implode(',', array_keys($allStatus));
$statusIds = implode(',', $allStatus);
$pledgeStatusIds = implode(',', $allPledgeStatus);
$updateCnt = 0;

$query = "
Expand Down Expand Up @@ -893,7 +890,7 @@ public static function updatePledgeStatus($params) {
) as amount_paid
FROM civicrm_pledge pledge, civicrm_pledge_payment payment
WHERE pledge.id = payment.pledge_id
AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$statusIds} )
AND payment.status_id IN ( {$statusIds} ) AND pledge.status_id IN ( {$pledgeStatusIds} )
GROUP By payment.id
";

Expand Down Expand Up @@ -931,23 +928,23 @@ public static function updatePledgeStatus($params) {

if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($dao->scheduled_date, '%Y%m%d'),
$now
) && $dao->payment_status != array_search('Overdue', $allStatus)
) && $dao->payment_status != $allStatus['Overdue']
) {
$pledgePayments[$dao->pledge_id][$dao->payment_id] = $dao->payment_id;
}
}
$allPledgeStatus = array_flip($allPledgeStatus);

// process the updating script...

foreach ($pledgePayments as $pledgeId => $paymentIds) {
// 1. update the pledge /pledge payment status. returns new status when an update happens
$returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allStatus[$pledgeStatus[$pledgeId]]})";
$returnMessages[] = "Checking if status update is needed for Pledge Id: {$pledgeId} (current status is {$allPledgeStatus[$pledgeStatus[$pledgeId]]})";

$newStatus = CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, $paymentIds,
array_search('Overdue', $allStatus), NULL, 0, FALSE, TRUE
$allStatus['Overdue'], NULL, 0, FALSE, TRUE
);
if ($newStatus != $pledgeStatus[$pledgeId]) {
$returnMessages[] = "- status updated to: {$allStatus[$newStatus]}";
$returnMessages[] = "- status updated to: {$allPledgeStatus[$newStatus]}";
$updateCnt += 1;
}
}
Expand Down
21 changes: 14 additions & 7 deletions CRM/Pledge/BAO/PledgePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ public static function updatePledgePaymentStatus(
$editScheduled = FALSE;

// get all statuses
$allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$allStatus = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name', TRUE
);

// if we get do not get contribution id means we are editing the scheduled payment.
if (!empty($paymentIDs)) {
Expand Down Expand Up @@ -603,10 +605,13 @@ public static function calculateNextScheduledDate(&$params, $paymentNo, $basePay
*/
public static function calculatePledgeStatus($pledgeId) {
$paymentStatusTypes = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$pledgeStatusTypes = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name', TRUE
);

//return if the pledge is cancelled.
$currentPledgeStatus = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_Pledge', $pledgeId, 'status_id', 'id', TRUE);
if ($currentPledgeStatus == array_search('Cancelled', $paymentStatusTypes)) {
if ($currentPledgeStatus == array_search('Cancelled', $pledgeStatusTypes)) {
return $currentPledgeStatus;
}

Expand All @@ -621,18 +626,18 @@ public static function calculatePledgeStatus($pledgeId) {
}

if (array_search('Overdue', $allStatus)) {
$statusId = array_search('Overdue', $paymentStatusTypes);
$statusId = array_search('Overdue', $pledgeStatusTypes);
}
elseif (array_search('Completed', $allStatus)) {
if (count(array_count_values($allStatus)) == 1) {
$statusId = array_search('Completed', $paymentStatusTypes);
$statusId = array_search('Completed', $pledgeStatusTypes);
}
else {
$statusId = array_search('In Progress', $paymentStatusTypes);
$statusId = array_search('In Progress', $pledgeStatusTypes);
}
}
else {
$statusId = array_search('Pending', $paymentStatusTypes);
$statusId = array_search('Pending', $pledgeStatusTypes);
}

return $statusId;
Expand Down Expand Up @@ -722,7 +727,9 @@ public static function updateReminderDetails($paymentId) {
*/
public static function getOldestPledgePayment($pledgeID, $limit = 1) {
// get pending / overdue statuses
$pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name'
);

// get pending and overdue payments
$status[] = array_search('Pending', $pledgeStatuses);
Expand Down
8 changes: 6 additions & 2 deletions CRM/Pledge/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,15 @@ public static function whereClauseSingle(&$values, &$query) {
$tableName = 'civicrm_pledge';
$query->_tables['civicrm_pledge'] = $query->_whereTables['civicrm_pledge'] = 1;
$label = "Pledge Status";
$qillDAO = 'CRM_Pledge_DAO_Pledge';
$qillField = 'status_id';
}
else {
$tableName = 'civicrm_pledge_payment';
$query->_tables['civicrm_pledge_payment'] = $query->_whereTables['civicrm_pledge_payment'] = 1;
$label = "Pledge Payment Status";
$qillDAO = 'CRM_Contribute_DAO_Contribution';
$qillField = 'contribution_status_id';
}
$name = 'status_id';
if (!empty($value) && is_array($value) && !in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) {
Expand All @@ -314,7 +318,7 @@ public static function whereClauseSingle(&$values, &$query) {
$value,
'Integer'
);
list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue('CRM_Contribute_DAO_Contribution', 'contribution_status_id', $value, $op);
list($qillop, $qillVal) = CRM_Contact_BAO_Query::buildQillForFieldValue($qillDAO, $qillField, $value, $op);
$query->_qill[$grouping][] = ts('%1 %2 %3', array(1 => $label, 2 => $qillop, 3 => $qillVal));
return;

Expand Down Expand Up @@ -405,7 +409,7 @@ public static function from($name, $mode, $side) {
break;

case 'pledge_status':
$from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'contribution_status')";
$from .= " $side JOIN civicrm_option_group option_group_pledge_status ON (option_group_pledge_status.name = 'pledge_status')";
$from .= " $side JOIN civicrm_option_value pledge_status ON (civicrm_pledge.status_id = pledge_status.value AND option_group_pledge_status.id = pledge_status.option_group_id ) ";
break;

Expand Down
10 changes: 5 additions & 5 deletions CRM/Pledge/DAO/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* Generated from xml/schema/CRM/Pledge/Pledge.xml
* DO NOT EDIT. Generated by CRM_Core_CodeGen
* (GenCodeChecksum:d323ed81c70c68905fc53ffd849a9e81)
* (GenCodeChecksum:02d420c23b3c72fc2687cd857cc7d178)
*/
require_once 'CRM/Core/DAO.php';
require_once 'CRM/Utils/Type.php';
Expand Down Expand Up @@ -171,7 +171,7 @@ class CRM_Pledge_DAO_Pledge extends CRM_Core_DAO {
*/
public $additional_reminder_day;
/**
* Implicit foreign key to civicrm_option_values in the contribution_status option group.
* Implicit foreign key to civicrm_option_values in the pledge_status option group.
*
* @var int unsigned
*/
Expand Down Expand Up @@ -541,7 +541,7 @@ static function &fields() {
'name' => 'status_id',
'type' => CRM_Utils_Type::T_INT,
'title' => ts('Pledge Status Id') ,
'description' => 'Implicit foreign key to civicrm_option_values in the contribution_status option group.',
'description' => 'Implicit foreign key to civicrm_option_values in the pledge_status option group.',
'import' => true,
'where' => 'civicrm_pledge.status_id',
'headerPattern' => '',
Expand All @@ -552,8 +552,8 @@ static function &fields() {
'bao' => 'CRM_Pledge_BAO_Pledge',
'localizable' => 0,
'pseudoconstant' => array(
'optionGroupName' => 'contribution_status',
'optionEditPath' => 'civicrm/admin/options/contribution_status',
'optionGroupName' => 'pledge_status',
'optionEditPath' => 'civicrm/admin/options/pledge_status',
)
) ,
'pledge_is_test' => array(
Expand Down
2 changes: 1 addition & 1 deletion CRM/Pledge/Form/Pledge.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ public function setDefaultValues() {
}

$pledgeStatus = CRM_Pledge_BAO_Pledge::buildOptions('status_id');
$pledgeStatusNames = CRM_Core_OptionGroup::values('contribution_status',
$pledgeStatusNames = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name', TRUE
);
// get default status label (pending)
Expand Down
8 changes: 5 additions & 3 deletions CRM/Report/Form/Pledge/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class CRM_Report_Form_Pledge_Detail extends CRM_Report_Form {
* Class constructor.
*/
public function __construct() {
$this->_pledgeStatuses = CRM_Contribute_PseudoConstant::contributionStatus();
$this->_pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'label'
);
// Check if CiviCampaign is a) enabled and b) has active campaigns
$config = CRM_Core_Config::singleton();
$campaignEnabled = in_array("CiviCampaign", $config->enableComponents);
Expand Down Expand Up @@ -167,7 +169,7 @@ public function __construct() {
'title' => ts('Pledge Status'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Core_OptionGroup::values('contribution_status'),
'options' => CRM_Core_OptionGroup::values('pledge_status'),
),
'financial_type_id' => array(
'title' => ts('Financial Type'),
Expand Down Expand Up @@ -596,7 +598,7 @@ public function alterDisplay(&$rows) {
//handle status id
if (array_key_exists('civicrm_pledge_status_id', $row)) {
if ($value = $row['civicrm_pledge_status_id']) {
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
}
$entryFound = TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Report/Form/Pledge/Pbnp.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function __construct() {
'title' => ts('Pledge Status'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Core_OptionGroup::values('contribution_status'),
'options' => CRM_Core_OptionGroup::values('pledge_status'),
),
'installments' => array(
'title' => ts('Installments'),
Expand Down Expand Up @@ -372,7 +372,7 @@ public function alterDisplay(&$rows) {
//handle the Status Ids
if (array_key_exists('civicrm_pledge_status_id', $row)) {
if ($value = $row['civicrm_pledge_status_id']) {
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
}
$entryFound = TRUE;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Report/Form/Pledge/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ public function __construct() {
'title' => ts('Pledge Status'),
'type' => CRM_Utils_Type::T_INT,
'operatorType' => CRM_Report_Form::OP_MULTISELECT,
'options' => CRM_Core_OptionGroup::values('contribution_status'),
'options' => CRM_Core_OptionGroup::values('pledge_status'),
),
'financial_type_id' => array(
'title' => ts('Financial Type'),
Expand Down Expand Up @@ -429,7 +429,7 @@ public function alterDisplay(&$rows) {
//handle status id
if (array_key_exists('civicrm_pledge_status_id', $row)) {
if ($value = $row['civicrm_pledge_status_id']) {
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Contribute_PseudoConstant::contributionStatus($value);
$rows[$rowNum]['civicrm_pledge_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Pledge_BAO_Pledge', 'status_id', $value);
}
$entryFound = TRUE;
}
Expand Down
11 changes: 11 additions & 0 deletions CRM/Upgrade/Incremental/sql/4.7.24.mysql.tpl
Original file line number Diff line number Diff line change
@@ -1 +1,12 @@
{* file to handle db changes in 4.7.24 during upgrade *}
--Add pledge status option group
INSERT INTO `civicrm_option_group` ( `name`, {localize field='title'}`title`{/localize}, `is_active` ) VALUES ('pledge_status', {localize}'{ts escape="sql"}Pledge Status{/ts}'{/localize}, 1);

SELECT @option_group_id_ps := MAX(id) FROM `civicrm_option_group` where name = 'pledge_status';

INSERT INTO `civicrm_option_value` (`option_group_id`, {localize field='label'}`label`{/localize}, `value`, `name`, `is_default`, `weight`) VALUES
(@option_group_id_ps, {localize}'{ts escape="sql"}Completed{/ts}'{/localize} , 1, 'Completed', NULL, 1),
(@option_group_id_ps, {localize}'{ts escape="sql"}Pending{/ts}'{/localize} , 2, 'Pending', NULL, 2),
(@option_group_id_ps, {localize}'{ts escape="sql"}Cancelled{/ts}'{/localize} , 3, 'Cancelled', NULL, 3),
(@option_group_id_ps, {localize}'{ts escape="sql"}In Progress{/ts}'{/localize}, 4, 'In Progress', NULL, 4),
(@option_group_id_ps, {localize}'{ts escape="sql"}Overdue{/ts}'{/localize} , 5, 'Overdue', NULL, 5);
68 changes: 34 additions & 34 deletions sql/civicrm_generated.mysql

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions tests/phpunit/api/v3/PledgePaymentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,41 @@ public function testGetSinglePledgePayment() {
$this->assertEquals(1, $result['count'], " in line " . __LINE__);
}

/**
* Test process_pledge job log.
*/
public function testProcessPledgeJob() {
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name'
);
//Make first payment.
$paymentParams = array(
'contact_id' => $this->_individualId,
'pledge_id' => $this->_pledgeID,
'contribution_id' => $this->_contributionID,
'scheduled_date' => date('Ymd', strtotime("-1 days")),
'status_id' => array_search('Pending', $pledgeStatuses),
);
$firstPayment = $this->callAPISuccess('pledge_payment', 'create', $paymentParams);
//Status should be 'Pending' after first incomplete payment.
$checkStatus = $this->callAPISuccess('pledge', 'getsingle', array(
'id' => $this->_pledgeID,
'return' => 'pledge_status',
));
$this->assertEquals('Pending', $checkStatus['pledge_status']);

//Execute process_pledge job log.
$result = $this->callAPISuccess('Job', 'process_pledge', array());
$this->assertEquals("Checking if status update is needed for Pledge Id: {$this->_pledgeID} (current status is Pending)\n\r- status updated to: Overdue\n\r1 records updated.", $result['values']);

//Status should be 'Overdue' after processing.
$statusAfterProcessing = $this->callAPISuccess('pledge', 'getsingle', array(
'id' => $this->_pledgeID,
'return' => 'pledge_status',
));
$this->assertEquals('Overdue', $statusAfterProcessing['pledge_status']);
}

/**
* Test status of pledge on payments and cancellation.
*/
Expand Down
23 changes: 22 additions & 1 deletion tests/phpunit/api/v3/PledgeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,11 @@ public function testGetOverduePledge() {
'start_date' => 'first saturday of march last year',
);
$this->_pledge = $this->callAPISuccess('pledge', 'create', array_merge($this->_params, $overdueParams));
$pledgeStatuses = CRM_Core_OptionGroup::values('pledge_status',
FALSE, FALSE, FALSE, NULL, 'name'
);
$params = array(
'pledge_status_id' => '6',
'pledge_status_id' => array_search('Overdue', $pledgeStatuses),
);
$result = $this->callAPISuccess('pledge', 'get', $params);
$emptyResult = $this->callAPISuccess('pledge', 'get', array(
Expand All @@ -212,6 +215,24 @@ public function testGetOverduePledge() {
$this->assertEquals(0, $emptyResult['count']);
}

/**
* Test pledge_status option group
*/
public function testOptionGroupForPledgeStatus() {
$pledgeOg = $this->callAPISuccess('OptionGroup', 'get', array(
'name' => "pledge_status",
));
$this->assertEquals(1, $pledgeOg['count']);

$pledgeOv = $this->callAPISuccess('OptionValue', 'get', array(
'sequential' => 1,
'option_group_id' => "pledge_status",
));
$this->assertEquals(5, $pledgeOv['count']);
$pledgeStatus = CRM_Utils_Array::collect('name', $pledgeOv['values']);
$expected = array('Completed', 'Pending', 'Cancelled', 'In Progress', 'Overdue');
$this->assertEquals($expected, $pledgeStatus);
}

/**
* Create 2 pledges - see if we can get by status id.
Expand Down
4 changes: 2 additions & 2 deletions xml/schema/Pledge/Pledge.xml
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,9 @@
<export>false</export>
<type>int unsigned</type>
<pseudoconstant>
<optionGroupName>contribution_status</optionGroupName>
<optionGroupName>pledge_status</optionGroupName>
</pseudoconstant>
<comment>Implicit foreign key to civicrm_option_values in the contribution_status option group.</comment>
<comment>Implicit foreign key to civicrm_option_values in the pledge_status option group.</comment>
<add>2.1</add>
</field>
<index>
Expand Down
Loading