Skip to content

Commit

Permalink
Merge pull request #9729 from eileenmcnaughton/cust_date
Browse files Browse the repository at this point in the history
CRM-20012, CRM-19490 (now merged & closed), CRM-18387(merged& closed), CRM-20011 (in separate pr), CRM-15948(resolved by arlready-merged-part), CRM-19911 profile date fixes & code improvement
  • Loading branch information
colemanw authored Mar 29, 2017
2 parents 59aed41 + 3d927ee commit e6b51f2
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 145 deletions.
19 changes: 1 addition & 18 deletions CRM/Batch/Form/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,12 @@ public function setDefaultValues() {

// for add mode set smart defaults
if ($this->_action & CRM_Core_Action::ADD) {
list($currentDate, $currentTime) = CRM_Utils_Date::setDateDefaults(NULL, 'activityDateTime');
$currentDate = date('Y-m-d H-i-s');

$completeStatus = CRM_Contribute_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');
$specialFields = array(
'join_date' => date('Y-m-d'),
'receive_date' => $currentDate,
'receive_date_time' => $currentTime,
'contribution_status_id' => $completeStatus,
);

Expand Down Expand Up @@ -447,12 +446,6 @@ public function postProcess() {
* @return bool
*/
private function processContribution(&$params) {
$dates = array(
'receive_date',
'receipt_date',
'thankyou_date',
'cancel_date',
);

// get the price set associated with offline contribution record.
$priceSetId = CRM_Core_DAO::getFieldValue('CRM_Price_DAO_PriceSet', 'default_contribution_amount', 'id', 'name');
Expand Down Expand Up @@ -504,12 +497,6 @@ private function processContribution(&$params) {
'Contribution'
);

foreach ($dates as $val) {
if (!empty($value[$val])) {
$value[$val] = CRM_Utils_Date::processDate($value[$val], $value[$val . '_time'], TRUE);
}
}

if (!empty($value['send_receipt'])) {
$value['receipt_date'] = date('Y-m-d His');
}
Expand Down Expand Up @@ -715,10 +702,6 @@ private function processMembership(&$params) {
}
}

if (!empty($value['receive_date'])) {
$value['receive_date'] = CRM_Utils_Date::processDate($value['receive_date'], $value['receive_date_time'], TRUE);
}

$params['actualBatchTotal'] += $value['total_amount'];

unset($value['financial_type']);
Expand Down
61 changes: 20 additions & 41 deletions CRM/Contribute/Form/Task/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,11 @@ public function buildQuickForm() {
);
}

// It is possible to have fields that are required in CiviCRM not be required in the
// profile. Overriding that here. Perhaps a better approach would be to
// make them required in the schema & read that up through getFields functionality.
$requiredFields = array('receive_date');

//fix for CRM-2752
$customFields = CRM_Core_BAO_CustomField::getFields('Contribution');
foreach ($this->_contributionIds as $contributionId) {
Expand All @@ -160,6 +165,9 @@ public function buildQuickForm() {
}
else {
// handle non custom fields
if (in_array($field['name'], $requiredFields)) {
$field['is_required'] = TRUE;
}
CRM_Core_BAO_UFGroup::buildProfile($this, $field, NULL, $contributionId);
}
}
Expand Down Expand Up @@ -198,58 +206,29 @@ public function setDefaultValues() {
*/
public function postProcess() {
$params = $this->exportValues();
$dates = array(
'receive_date',
'receipt_date',
'thankyou_date',
'cancel_date',
);
if (isset($params['field'])) {
foreach ($params['field'] as $key => $value) {
foreach ($params['field'] as $contributionID => $value) {

$value['custom'] = CRM_Core_BAO_CustomField::postProcess($value,
$key,
'Contribution'
);

$ids['contribution'] = $key;
foreach ($dates as $val) {
if (isset($value[$val])) {
$value[$val] = CRM_Utils_Date::processDate($value[$val]);
}
}
$value['id'] = $contributionID;
if (!empty($value['financial_type'])) {
$value['financial_type_id'] = $value['financial_type'];
}

if (!empty($value['payment_instrument'])) {
$value['payment_instrument_id'] = $value['payment_instrument'];
}

if (!empty($value['contribution_source'])) {
$value['source'] = $value['contribution_source'];
}

unset($value['financial_type']);
unset($value['contribution_source']);
$contribution = CRM_Contribute_BAO_Contribution::add($value, $ids);
$value['options'] = array(
'reload' => 1,
);
$contribution = civicrm_api3('Contribution', 'create', $value);
$contribution = $contribution['values'][$contributionID];

// @todo add check as to whether the status is updated.
if (!empty($value['contribution_status_id'])) {
CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution->id,
// @todo - use completeorder api or make api call do this.
CRM_Contribute_BAO_Contribution::transitionComponentWithReturnMessage($contribution['id'],
$value['contribution_status_id'],
CRM_Utils_Array::value("field[{$key}][contribution_status_id]",
$this->_defaultValues
),
$contribution->receive_date
CRM_Utils_Array::value("field[{$contributionID}][contribution_status_id]", $this->_defaultValues),
$contribution['receive_date']
);
}

// add custom field values
if (!empty($value['custom']) &&
is_array($value['custom'])
) {
CRM_Core_BAO_CustomValueTable::store($value['custom'], 'civicrm_contribution', $contribution->id);
}
}
CRM_Core_Session::setStatus(ts("Your updates have been saved."), ts('Saved'), 'success');
}
Expand Down
62 changes: 1 addition & 61 deletions CRM/Core/BAO/UFGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,9 +519,6 @@ protected static function formatUFField(
);

$formattedField = CRM_Utils_Date::addDateMetadataToField($fieldMetaData, $formattedField);
if (in_array($name, array_keys(self::getNonUpgradedDateFields()))) {
$formattedField['is_legacy_date'] = 1;
}

//adding custom field property
if (substr($field->field_name, 0, 6) == 'custom' ||
Expand Down Expand Up @@ -1888,7 +1885,6 @@ public static function buildProfile(
$addressOptions = CRM_Core_BAO_Setting::valueOptions(CRM_Core_BAO_Setting::SYSTEM_PREFERENCES_NAME,
'address_options', TRUE, NULL, TRUE
);
$legacyHandledDateFields = self::getNonUpgradedDateFields();

if (substr($fieldName, 0, 14) === 'state_province') {
$form->addChainSelect($name, array('label' => $title, 'required' => $required));
Expand Down Expand Up @@ -1951,9 +1947,6 @@ public static function buildProfile(
}
}
}
elseif (isset($legacyHandledDateFields[$fieldName])) {
$form->addDate($name, $title, $required, array('formatType' => $legacyHandledDateFields[$fieldName]));
}
elseif (CRM_Utils_Array::value('name', $field) == 'membership_type') {
list($orgInfo, $types) = CRM_Member_BAO_MembershipType::getMembershipTypeInfo();
$sel = &$form->addElement('hierselect', $name, $title);
Expand Down Expand Up @@ -2315,23 +2308,6 @@ public static function buildProfile(
}
}

/**
* Get fields that have not been upgraded to use datepicker.
*
* Fields that use the old code have jcalendar in the tpl and
* the form uses a customised format. We are moving towards datepicker
* which among other things passes dates back and forth using a standardised
* format. Remove fields from here as they are tested and converted.
*/
static public function getNonUpgradedDateFields() {
return array(
'receive_date' => 'activityDateTime',
'receipt_date' => 'activityDateTime',
'thankyou_date' => 'activityDateTime',
'cancel_date' => 'activityDateTime',
);
}

/**
* Set profile defaults.
*
Expand Down Expand Up @@ -2437,34 +2413,6 @@ public static function setProfileDefaults(
}
break;

case 'Select Date':
if (!in_array($name, array_keys(self::getNonUpgradedDateFields()))) {
$defaults[$fldName] = $details[$name];
}
else {
// Do legacy handling.
// CRM-6681, set defult values according to date and time format (if any).
$dateFormat = NULL;
if (!empty($customFields[$customFieldId]['date_format'])) {
$dateFormat = $customFields[$customFieldId]['date_format'];
}

if (empty($customFields[$customFieldId]['time_format'])) {
list($defaults[$fldName]) = CRM_Utils_Date::setDateDefaults($details[$name], NULL,
$dateFormat
);
}
else {
$timeElement = $fldName . '_time';
if (substr($fldName, -1) == ']') {
$timeElement = substr($fldName, 0, -1) . '_time]';
}
list($defaults[$fldName], $defaults[$timeElement]) = CRM_Utils_Date::setDateDefaults($details[$name],
NULL, $dateFormat, $customFields[$customFieldId]['time_format']);
}
}
break;

default:
$defaults[$fldName] = $details[$name];
break;
Expand Down Expand Up @@ -3278,18 +3226,10 @@ public static function setComponentDefaults(&$fields, $componentId, $component,
}

$formattedGroupTree = array();
// @todo - as we put these on datepicker they need to be removed from here.
$dateTimeFields = array_keys(self::getNonUpgradedDateFields());

foreach ($fields as $name => $field) {
$fldName = $isStandalone ? $name : "field[$componentId][$name]";
if (in_array($name, $dateTimeFields)) {
$timefldName = $isStandalone ? "{$name}_time" : "field[$componentId][{$name}_time]";
if (!empty($values[$name])) {
list($defaults[$fldName], $defaults[$timefldName]) = CRM_Utils_Date::setDateDefaults($values[$name]);
}
}
elseif (array_key_exists($name, $values)) {
if (array_key_exists($name, $values)) {
$defaults[$fldName] = $values[$name];
}
elseif ($name == 'participant_note') {
Expand Down
8 changes: 1 addition & 7 deletions templates/CRM/Batch/Form/Entry.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@
{/if}
{foreach from=$fields item=field key=fieldName}
{assign var=n value=$field.name}
{if in_array( $n, array( 'thankyou_date', 'cancel_date', 'receipt_date', 'receive_date') ) }
<div class="compressed crm-grid-cell">
<span class="crm-batch-{$n}-{$rowNumber}">
{include file="CRM/common/jcalendar.tpl" elementName=$n elementIndex=$rowNumber batchUpdate=1}
</span>
</div>
{elseif $n eq 'soft_credit'}
{if $n eq 'soft_credit'}
<div class="compressed crm-grid-cell">
{$form.soft_credit_contact_id.$rowNumber.html|crmAddClass:big}
{$form.soft_credit_amount.$rowNumber.label}&nbsp;{$form.soft_credit_amount.$rowNumber.html|crmAddClass:eight}
Expand Down
6 changes: 1 addition & 5 deletions templates/CRM/Contribute/Form/Task/Batch.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,7 @@

{foreach from=$fields item=field key=fieldName}
{assign var=n value=$field.name}
{if ( $n eq 'thankyou_date' ) or ( $n eq 'cancel_date' ) or ( $n eq 'receipt_date' ) or ( $n eq 'receive_date' )}
<td class="compressed">{include file="CRM/common/jcalendar.tpl" elementName=$n elementIndex=$cid batchUpdate=1}</td>
{else}
<td class="compressed">{$form.field.$cid.$n.html}</td>
{/if}
<td class="compressed">{$form.field.$cid.$n.html}</td>
{/foreach}
</tr>
{/foreach}
Expand Down
2 changes: 0 additions & 2 deletions templates/CRM/UF/Form/Fields.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@
value="{$form.$profileFieldName.value}" id="{$form.$profileFieldName.name}"
>
</span>
{elseif $field.is_legacy_date}
{include file="CRM/common/jcalendar.tpl" elementName=$profileFieldName}
{elseif $profileFieldName|substr:0:5 eq 'phone'}
{assign var="phone_ext_field" value=$profileFieldName|replace:'phone':'phone_ext'}
{if $prefix}{$form.$prefix.$profileFieldName.html}{else}{$form.$profileFieldName.html}{/if}
Expand Down
22 changes: 11 additions & 11 deletions tests/phpunit/CRM/Batch/Form/EntryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ public function testMembershipRenewalDates() {

// explicitly specify start and end dates
$params['field'][2]['membership_type'] = array(0 => $this->_orgContactID2, 1 => $this->_membershipTypeID2);
$params['field'][2]['membership_start_date'] = "04/01/2016";
$params['field'][2]['membership_end_date'] = "03/31/2017";
$params['field'][2]['receive_date'] = "04/01/2016";
$params['field'][2]['membership_start_date'] = "2016-04-01";
$params['field'][2]['membership_end_date'] = "2017-03-31";
$params['field'][2]['receive_date'] = "2016-04-01";

$this->assertTrue($form->testProcessMembership($params));
$result = $this->callAPISuccess('membership', 'get', array());
Expand Down Expand Up @@ -268,13 +268,13 @@ public function getMembershipData() {
'field' => array(
1 => array(
'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
'join_date' => '07/22/2013',
'join_date' => '2013-07-22',
'membership_start_date' => NULL,
'membership_end_date' => NULL,
'membership_source' => NULL,
'financial_type' => 2,
'total_amount' => 1,
'receive_date' => '07/24/2013',
'receive_date' => '2013-07-24',
'receive_date_time' => NULL,
'payment_instrument' => 1,
'trxn_id' => 'TX101',
Expand All @@ -283,13 +283,13 @@ public function getMembershipData() {
),
2 => array(
'membership_type' => array(0 => $this->_orgContactID, 1 => $this->_membershipTypeID),
'join_date' => '07/03/2013',
'membership_start_date' => '02/03/2013',
'join_date' => '2013-07-03',
'membership_start_date' => '2013-02-03',
'membership_end_date' => NULL,
'membership_source' => NULL,
'financial_type' => 2,
'total_amount' => 1,
'receive_date' => '07/17/2013',
'receive_date' => '2013-07-17',
'receive_date_time' => NULL,
'payment_instrument' => NULL,
'trxn_id' => 'TX102',
Expand All @@ -305,7 +305,7 @@ public function getMembershipData() {
'membership_source' => NULL,
'financial_type' => 2,
'total_amount' => 1,
'receive_date' => '07/17/2013',
'receive_date' => '2013-07-17',
'receive_date_time' => NULL,
'payment_instrument' => NULL,
'trxn_id' => 'TX103',
Expand Down Expand Up @@ -335,7 +335,7 @@ public function getContributionData() {
1 => array(
'financial_type' => 1,
'total_amount' => 15,
'receive_date' => '07/24/2013',
'receive_date' => '2013-07-24',
'receive_date_time' => NULL,
'payment_instrument' => 1,
'check_number' => NULL,
Expand All @@ -344,7 +344,7 @@ public function getContributionData() {
2 => array(
'financial_type' => 1,
'total_amount' => 15,
'receive_date' => '07/24/2013',
'receive_date' => '2013-07-24',
'receive_date_time' => NULL,
'payment_instrument' => 1,
'check_number' => NULL,
Expand Down

0 comments on commit e6b51f2

Please sign in to comment.