Skip to content

Commit

Permalink
Merge pull request civicrm#11161 from agileware/CRM-20787
Browse files Browse the repository at this point in the history
CRM-20787: For a repeating Event series. Changing price set of main event, it should be copied to all repeating events if we select 'Every event' mode.
  • Loading branch information
eileenmcnaughton authored and sluc23 committed Jan 10, 2018
2 parents 04d7463 + 543aa76 commit 79b0737
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 26 deletions.
65 changes: 65 additions & 0 deletions CRM/Core/BAO/RecurringEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ class CRM_Core_BAO_RecurringEntity extends CRM_Core_DAO_RecurringEntity {
),
);

//Define global CLASS CONSTANTS for recurring entity mode types
const MODE_THIS_ENTITY_ONLY = 1;
const MODE_NEXT_ALL_ENTITY = 2;
const MODE_ALL_ENTITY_IN_SERIES = 3;

/**
* Getter for status.
*
Expand Down Expand Up @@ -1174,4 +1179,64 @@ public static function updateModeLinkedEntity($entityId, $linkedEntityTable, $ma
return $result;
}

/**
* Update mode in civicrm_recurring_entity table for event related data and price set in civicrm_price_set_entity.
*
* @param int $entityId
* Event id .
* @param string $entityTable
* @param string $mode
* @param string $linkedEntityTable
* Linked entity table name for this event .
* @param string $priceSet
* Price set of the event .
*
* @return array
*/
public static function updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet) {
$finalResult = array();

if (!empty($linkedEntityTable)) {
$result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $linkedEntityTable, $entityTable);
}

$dao = new CRM_Core_DAO_RecurringEntity();
if (!empty($result)) {
$dao->entity_id = $result['entityId'];
$dao->entity_table = $result['entityTable'];
}
else {
$dao->entity_id = $entityId;
$dao->entity_table = $entityTable;
}

if ($dao->find(TRUE)) {
$dao->mode = $mode;
$dao->save();

//CRM-20787 Fix
//I am not sure about other fields, if mode = 3 apply for an event then other fields
//should be save for all other series events or not so applying for price set only for now here.
if (CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES === $mode) {

//Step-1: Get all events of series
$seriesEventRecords = CRM_Core_BAO_RecurringEntity::getEntitiesFor($entityId, $entityTable);
foreach ($seriesEventRecords as $event) {
//Step-3: Save price set in other series events
if (CRM_Price_BAO_PriceSet::removeFrom($event['table'], $event['id'])) {//Remove existing priceset
CRM_Core_BAO_Discount::del($event['id'], $event['table']);
CRM_Price_BAO_PriceSet::addTo($event['table'], $event['id'], $priceSet); //Add new price set
}
}
}
//CRM-20787 - Fix end
$finalResult['status'] = 'Done';
}
else {
$finalResult['status'] = 'Error';
}

return $finalResult;
}

}
28 changes: 4 additions & 24 deletions CRM/Core/Page/AJAX/RecurringEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,14 @@ class CRM_Core_Page_AJAX_RecurringEntity {

public static function updateMode() {
$finalResult = array();
if (CRM_Utils_Array::value('mode', $_REQUEST) && CRM_Utils_Array::value('entityId', $_REQUEST) && CRM_Utils_Array::value('entityTable', $_REQUEST)) {
if (CRM_Utils_Array::value('mode', $_REQUEST) && CRM_Utils_Array::value('entityId', $_REQUEST) && CRM_Utils_Array::value('entityTable', $_REQUEST) && CRM_Utils_Array::value('priceSet', $_REQUEST)) {

$mode = CRM_Utils_Type::escape($_REQUEST['mode'], 'Integer');
$entityId = CRM_Utils_Type::escape($_REQUEST['entityId'], 'Integer');
$entityTable = CRM_Utils_Type::escape($_REQUEST['entityTable'], 'String');

if (!empty($_REQUEST['linkedEntityTable'])) {
$result = CRM_Core_BAO_RecurringEntity::updateModeLinkedEntity($entityId, $_REQUEST['linkedEntityTable'], $entityTable);
}

$dao = new CRM_Core_DAO_RecurringEntity();
if (!empty($result)) {
$dao->entity_id = $result['entityId'];
$dao->entity_table = $result['entityTable'];
}
else {
$dao->entity_id = $entityId;
$dao->entity_table = $entityTable;
}

if ($dao->find(TRUE)) {
$dao->mode = $mode;
$dao->save();
$finalResult['status'] = 'Done';
}
else {
$finalResult['status'] = 'Error';
}
$priceSet = CRM_Utils_Type::escape($_REQUEST['priceSet'], 'String');
$linkedEntityTable = $_REQUEST['linkedEntityTable'];
$finalResult = CRM_Core_BAO_RecurringEntity::updateModeAndPriceSet($entityId, $entityTable, $mode, $linkedEntityTable, $priceSet);
}
CRM_Utils_JSON::output($finalResult);
}
Expand Down
5 changes: 3 additions & 2 deletions templates/CRM/Event/Form/ManageEvent/ConfirmRepeatMode.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@
function updateMode() {
var mode = $('input[name=recur_mode]:checked', this).val(),
entityID = parseInt('{/literal}{$entityID}{literal}'),
entityTable = '{/literal}{$entityTable}{literal}';
entityTable = '{/literal}{$entityTable}{literal}',
priceSet = $('#price_set_id').val();
if (entityID != "" && mode && mapper.hasOwnProperty(formClass) && entityTable !="") {
$.getJSON(CRM.url("civicrm/ajax/recurringentity/update-mode",
{mode: mode, entityId: entityID, entityTable: entityTable, linkedEntityTable: mapper[formClass]})
{mode: mode, entityId: entityID, entityTable: entityTable, linkedEntityTable: mapper[formClass], priceSet: priceSet})
).done(function (result) {
if (result.status != "" && result.status == 'Done') {
$form.submit();
Expand Down
63 changes: 63 additions & 0 deletions tests/phpunit/CRM/Core/BAO/RecurringEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,69 @@ public function testActivityGeneration() {

}

/**
* Creating action schedule
*/
private function createActionSchedule($entity_id, $entity_table) {
$params = array(
"used_for" => $entity_table,
"entity_value" => $entity_id,
"start_action_date" => date("YmdHis"),
"repetition_frequency_unit" => "week",
"repetition_frequency_interval" => "3",
"start_action_condition" => "monday,tuesday,wednesday,thursday,friday,saturday",
"start_action_offset" => "2",
);
$actionScheduleObj = CRM_Core_BAO_ActionSchedule::add($params);
return $actionScheduleObj;
}

/**
* Creating recurring entities
*/
private function createRecurringEntities($actionScheduleObj, $entity_id, $entity_table) {
$recursion = new CRM_Core_BAO_RecurringEntity();
$recursion->dateColumns = array(
"start_date",
);
$recursion->scheduleId = $actionScheduleObj->id;
$recursion->entity_id = $entity_id;
$recursion->entity_table = $entity_table;
$recursion->linkedEntities = array(
array(
"table" => "civicrm_price_set_entity",
"findCriteria" => array(
"entity_id" => $entity_id,
"entity_table" => $entity_table,
),
"linkedColumns" => array(
"entity_id",
),
"isRecurringEntityRecord" => FALSE,
),
);
return $recursion->generate();
}

/**
* Testing Event Generation through Entity Recursion.
*/
public function testRepeatEventCreation() {
$event = $this->eventCreate();
$entity_table = "civicrm_event";
$entity_id = $event["id"];
CRM_Price_BAO_PriceSet::addTo($entity_table, $entity_id, 1);
$actionScheduleObj = $this->createActionSchedule($entity_id, $entity_table);
$recurringEntities = $this->createRecurringEntities($actionScheduleObj, $entity_id, $entity_table);
$finalResult = CRM_Core_BAO_RecurringEntity::updateModeAndPriceSet($entity_id, $entity_table, CRM_Core_BAO_RecurringEntity::MODE_ALL_ENTITY_IN_SERIES, array(), 2);
$this->assertEquals(2, count($recurringEntities["civicrm_event"]), "Recurring events not created.");
$this->assertEquals(2, count($recurringEntities["civicrm_price_set_entity"]), "Recurring price sets not created.");
$priceSetOne = CRM_Price_BAO_PriceSet::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][0]);
$priceSetTwo = CRM_Price_BAO_PriceSet::getFor($entity_table, $recurringEntities["civicrm_price_set_entity"][1]);
$this->assertEquals(2, $priceSetOne, "Price set id of the recurring event is not updated.");
$this->assertEquals(2, $priceSetTwo, "Price set id of the recurring event is not updated.");
}

/**
* Testing Event Generation through Entity Recursion.
*/
Expand Down

0 comments on commit 79b0737

Please sign in to comment.