Skip to content

Commit

Permalink
Merge branch 'master' into 4.7.19-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton authored Apr 27, 2017
2 parents 670a76d + a78982f commit 3c8e8aa
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 91 deletions.
12 changes: 0 additions & 12 deletions CRM/Case/BAO/Case.php
Original file line number Diff line number Diff line change
Expand Up @@ -1019,18 +1019,6 @@ public static function getCaseActivity($caseID, &$params, $contactID, $context =
$deleteUrl = "{$url}&action=delete{$contextUrl}";
$restoreUrl = "{$url}&action=renew{$contextUrl}";
$viewTitle = ts('View activity');
$statusTitle = ts('Edit Status');

$emailActivityTypeIDs = array(
'Email' => CRM_Core_OptionGroup::getValue('activity_type',
'Email',
'name'
),
'Inbound Email' => CRM_Core_OptionGroup::getValue('activity_type',
'Inbound Email',
'name'
),
);

$emailActivityTypeIDs = array(
'Email' => CRM_Core_OptionGroup::getValue('activity_type',
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Page/Inline/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function run() {
$entityBlock = array('contact_id' => $contactId);
$emails = CRM_Core_BAO_Email::getValues($entityBlock);
if (!empty($emails)) {
foreach ($emails as $key => & $value) {
foreach ($emails as &$value) {
$value['location_type'] = $locationTypes[$value['location_type_id']];
}
}
Expand Down
123 changes: 89 additions & 34 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -3439,41 +3439,38 @@ public static function recordFinancialAccounts(&$params, $financialTrxnValues =
* @param string $context
* Update scenarios.
*
* @param null $skipTrxn
*
*/
public static function updateFinancialAccounts(&$params, $context = NULL, $skipTrxn = NULL) {
$itemAmount = $trxnID = NULL;
//get all the statuses
$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
public static function updateFinancialAccounts(&$params, $context = NULL) {
$trxnID = NULL;
$inputParams = $params;
$isARefund = FALSE;
$currentContributionStatus = CRM_Core_PseudoConstant::getLabel('CRM_Contribute_BAO_Contribution', 'contribution_status_id', $params['contribution']->contribution_status_id);
$previousContributionStatus = CRM_Contribute_PseudoConstant::contributionStatus($params['prevContribution']->contribution_status_id, 'name');

if (($previousContributionStatus == 'Pending'
|| $previousContributionStatus == 'In Progress')
&& $params['contribution']->contribution_status_id == array_search('Completed', $contributionStatus)
&& $currentContributionStatus == 'Completed'
&& $context == 'changePaymentInstrument'
) {
return;
}
if ((($previousContributionStatus == 'Partially paid'
&& $params['contribution']->contribution_status_id == array_search('Completed', $contributionStatus))
&& $currentContributionStatus == 'Completed')
|| ($previousContributionStatus == 'Pending' && $params['prevContribution']->is_pay_later == TRUE
&& $params['contribution']->contribution_status_id == array_search('Partially paid', $contributionStatus)))
&& $currentContributionStatus == 'Partially paid'))
&& $context == 'changedStatus'
) {
return;
}
if ($context == 'changedAmount' || $context == 'changeFinancialType') {
$itemAmount = $params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = $params['total_amount'] - $params['prevContribution']->total_amount;
$params['trxnParams']['total_amount'] = $params['trxnParams']['net_amount'] = ($params['total_amount'] - $params['prevContribution']->total_amount);
}
if ($context == 'changedStatus') {
//get all the statuses
$contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
$cancelledTaxAmount = 0;
if ($previousContributionStatus == 'Completed'
&& (self::isContributionStatusNegative($params['contribution']->contribution_status_id))
) {
$isARefund = TRUE;
$params['trxnParams']['total_amount'] = -$params['total_amount'];
$cancelledTaxAmount = CRM_Utils_Array::value('tax_amount', $params, '0.00');
if (empty($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {
$creditNoteId = self::createCreditNoteId();
CRM_Core_DAO::setFieldValue('CRM_Contribute_DAO_Contribution', $params['contribution']->id, 'creditnote_id', $creditNoteId);
Expand All @@ -3485,7 +3482,7 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
$financialTypeID = CRM_Utils_Array::value('financial_type_id', $params) ? $params['financial_type_id'] : $params['prevContribution']->financial_type_id;
$arAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($financialTypeID, 'Accounts Receivable Account is');

if ($params['contribution']->contribution_status_id == array_search('Cancelled', $contributionStatus)) {
if ($currentContributionStatus == 'Cancelled') {
$params['trxnParams']['to_financial_account_id'] = $arAccountId;
$params['trxnParams']['total_amount'] = -$params['total_amount'];
if (is_null($params['contribution']->creditnote_id) || $params['contribution']->creditnote_id == "null") {
Expand All @@ -3497,7 +3494,6 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
$params['trxnParams']['from_financial_account_id'] = $arAccountId;
}
}
$itemAmount = $params['trxnParams']['total_amount'] + $cancelledTaxAmount;
}
elseif ($context == 'changePaymentInstrument') {
$params['trxnParams']['net_amount'] = $params['trxnParams']['total_amount'];
Expand All @@ -3521,7 +3517,7 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
if ($context == 'changedStatus') {
if (($previousContributionStatus == 'Pending'
|| $previousContributionStatus == 'In Progress')
&& ($params['contribution']->contribution_status_id == array_search('Completed', $contributionStatus))
&& ($currentContributionStatus == 'Completed')
) {
if (empty($params['line_item'])) {
//CRM-15296
Expand Down Expand Up @@ -3583,39 +3579,26 @@ public static function updateFinancialAccounts(&$params, $context = NULL, $skipT
if ($params['contribution']->currency) {
$currency = $params['contribution']->currency;
}
$diff = 1;
if ($context == 'changeFinancialType' || self::isContributionStatusNegative($params['contribution']->contribution_status_id)) {
$diff = -1;
}
if (!empty($params['is_quick_config'])) {
$amount = $itemAmount;
if (!$amount) {
$amount = $params['total_amount'];
}
}
else {
$amount = $diff * $lineItemDetails['line_total'];
}

$itemParams = array(
'transaction_date' => $receiveDate,
'contact_id' => $params['prevContribution']->contact_id,
'currency' => $currency,
'amount' => $amount,
'amount' => self::getFinancialItemAmountFromParams($inputParams, $context, $lineItemDetails, $isARefund),
'description' => $prevFinancialItem['description'],
'status_id' => $prevFinancialItem['status_id'],
'financial_account_id' => $financialAccount,
'entity_table' => 'civicrm_line_item',
'entity_id' => $lineItemDetails['id'],
);
$financialItem = CRM_Financial_BAO_FinancialItem::create($itemParams, NULL, $trxnIds);
$params['line_item'][$fieldId][$fieldValueId]['deferred_line_total'] = $amount;
$params['line_item'][$fieldId][$fieldValueId]['deferred_line_total'] = $itemParams['amount'];
$params['line_item'][$fieldId][$fieldValueId]['financial_item_id'] = $financialItem->id;

if ($lineItemDetails['tax_amount']) {
if ($lineItemDetails['tax_amount'] && $lineItemDetails['tax_amount'] !== 'null') {
$invoiceSettings = Civi::settings()->get('contribution_invoice_settings');
$taxTerm = CRM_Utils_Array::value('tax_term', $invoiceSettings);
$itemParams['amount'] = $diff * $lineItemDetails['tax_amount'];
$itemParams['amount'] = self::getMultiplier($params['contribution']->contribution_status_id, $context) * $lineItemDetails['tax_amount'];
$itemParams['description'] = $taxTerm;
if ($lineItemDetails['financial_type_id']) {
$itemParams['financial_account_id'] = self::getFinancialAccountId($lineItemDetails['financial_type_id']);
Expand Down Expand Up @@ -5258,6 +5241,78 @@ private static function getOriginalContribution($contributionID) {
return self::getValues(array('id' => $contributionID), CRM_Core_DAO::$_nullArray, CRM_Core_DAO::$_nullArray);
}

/**
* Get the amount for the financial item row.
*
* Helper function to start to break down recordFinancialTransactions for readability.
*
* The logic is more historical than .. logical. Paths other than the deprecated one are tested.
*
* Codewise, several somewhat disimmilar things have been squished into recordFinancialAccounts
* for historical reasons. Going forwards we can hope to add tests & improve readibility
* of that function
*
* @todo move recordFinancialAccounts & helper functions to their own class?
*
* @param array $params
* Params as passed to contribution.create
*
* @param string $context
* changeFinancialType| changedAmount
* @param array $lineItemDetails
* Line items.
* @param bool $isARefund
* Is this a refund / negative transaction.
*
* @return float
*/
protected static function getFinancialItemAmountFromParams($params, $context, $lineItemDetails, $isARefund) {
if ($context == 'changedAmount' || $context == 'changeFinancialType') {
return $params['total_amount'] - $params['prevContribution']->total_amount;
}
elseif ($context == 'changedStatus') {
$cancelledTaxAmount = 0;
if ($isARefund) {
$cancelledTaxAmount = CRM_Utils_Array::value('tax_amount', $params, '0.00');
}
return self::getMultiplier($params['contribution']->contribution_status_id, $context) * ($params['trxnParams']['total_amount'] + $cancelledTaxAmount);
}
elseif ($context === NULL) {
// erm, yes because? but, hey, it's tested.
return $params['total_amount'];
}
elseif (empty($lineItemDetails['line_total'])) {
// follow legacy code path
Civi::log()
->warning('Deprecated bit of code, please log a ticket explaining how you got here!', array('civi.tag' => 'deprecated'));
return $params['total_amount'];
}
else {
return self::getMultiplier($params['contribution']->contribution_status_id, $context) * $lineItemDetails['line_total'];
}
}

/**
* Get the multiplier for adjusting rows.
*
* If we are dealing with a refund or cancellation then it will be a negative
* amount to reflect the negative transaction.
*
* If we are changing Financial Type it will be a negative amount to
* adjust down the old type.
*
* @param int $contribution_status_id
* @param string $context
*
* @return int
*/
protected static function getMultiplier($contribution_status_id, $context) {
if ($context == 'changeFinancialType' || self::isContributionStatusNegative($contribution_status_id)) {
return -1;
}
return 1;
}

/**
* Assign Test Value.
*
Expand Down
9 changes: 4 additions & 5 deletions CRM/Core/BAO/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,9 @@ public static function add(&$params) {
* @param array $entityBlock
* Input parameters to find object.
*
* @return bool
* @return array
*/
public static function &getValues($entityBlock) {
public static function getValues($entityBlock) {
return CRM_Core_BAO_Block::getValues('email', $entityBlock);
}

Expand Down Expand Up @@ -283,8 +283,7 @@ public static function holdEmail(&$email) {
* an array of email ids
*/
public static function getFromEmail() {
$session = CRM_Core_Session::singleton();
$contactID = $session->get('userID');
$contactID = CRM_Core_Session::singleton()->getLoggedInContactID();
$fromEmailValues = array();

// add all configured FROM email addresses
Expand All @@ -299,7 +298,7 @@ public static function getFromEmail() {
$contactEmails = self::allEmails($contactID);
$fromDisplayName = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $contactID, 'display_name');

foreach ($contactEmails as $emailId => $emailVal) {
foreach ($contactEmails as $emailVal) {
$email = trim($emailVal['email']);
if (!$email || $emailVal['on_hold']) {
continue;
Expand Down
31 changes: 12 additions & 19 deletions CRM/Core/BAO/UFField.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,30 +119,23 @@ public static function del($id) {
*
* @param array $params
* An associative array with field and values.
* @param $ids
*
* @return mixed
* @ids array $ids array that containd ids
*
* @return bool
*/
public static function duplicateField($params, $ids) {
public static function duplicateField($params) {
$ufField = new CRM_Core_DAO_UFField();
$ufField->uf_group_id = CRM_Utils_Array::value('uf_group', $ids);
$ufField->field_type = $params['field_name'][0];
$ufField->field_name = $params['field_name'][1];
if ($params['field_name'][1] == 'url') {
$ufField->website_type_id = CRM_Utils_Array::value(2, $params['field_name'], NULL);
}
else {
$ufField->location_type_id = (CRM_Utils_Array::value(2, $params['field_name'])) ? $params['field_name'][2] : 'NULL';
}
$ufField->phone_type_id = CRM_Utils_Array::value(3, $params['field_name']);

if (!empty($ids['uf_field'])) {
$ufField->whereAdd("id <> " . CRM_Utils_Array::value('uf_field', $ids));
$ufField->uf_group_id = CRM_Utils_Array::value('uf_group_id', $params);
$ufField->field_type = $params['field_type'];
$ufField->field_name = $params['field_name'];
$ufField->website_type_id = CRM_Utils_Array::value('website_type_id', $params);
$ufField->location_type_id = CRM_Utils_Array::value('location_type_id', $params);
$ufField->phone_type_id = CRM_Utils_Array::value('phone_type_id', $params);;

if (!empty($params['id'])) {
$ufField->whereAdd("id <> " . $params['id']);
}

return $ufField->find(TRUE);
return ($ufField->find(TRUE) ? 1 : 0);
}

/**
Expand Down
5 changes: 4 additions & 1 deletion CRM/Event/Form/Registration/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,7 @@ public static function processContribution(

if ($paymentProcessor) {
$contribParams['payment_instrument_id'] = $paymentProcessor['payment_instrument_id'];
$contribParams['payment_processor'] = $paymentProcessor['id'];
}

if (!$pending && $result) {
Expand Down Expand Up @@ -1037,7 +1038,6 @@ public static function processContribution(
$contribParams['address_id'] = CRM_Contribute_BAO_Contribution::createAddress($params, $form->_bltID);
}

$contribParams['payment_processor'] = CRM_Utils_Array::value('payment_processor', $params);
$contribParams['skipLineItem'] = 1;
// create contribution record
$contribution = CRM_Contribute_BAO_Contribution::add($contribParams, $ids);
Expand Down Expand Up @@ -1305,6 +1305,9 @@ public static function testSubmit($params) {
$eventParams = array('id' => $params['id']);
CRM_Event_BAO_Event::retrieve($eventParams, $form->_values['event']);
$form->set('registerByID', $params['registerByID']);
if (!empty($params['paymentProcessorObj'])) {
$form->_paymentProcessor = $params['paymentProcessorObj'];
}
$form->postProcess();
}

Expand Down
21 changes: 18 additions & 3 deletions CRM/UF/Form/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,12 +537,27 @@ public function postProcess() {
}

//check for duplicate fields
if ($params["field_name"][0] != "Formatting" && CRM_Core_BAO_UFField::duplicateField($params, array('uf_group' => $params['uf_group_id'], 'uf_field' => $params['id']))) {
CRM_Core_Session::setStatus(ts('The selected field already exists in this profile.'), ts('Field Not Added'), 'error');
return;
$apiFormattedParams = $params;
$apiFormattedParams['field_type'] = $params['field_name'][0];
$apiFormattedParams['field_name'] = $params['field_name'][1];
if (!empty($params['field_name'][2])) {
if ($apiFormattedParams['field_name'] === 'url') {
$apiFormattedParams['website_type_id'] = $params['field_name'][2];
}
else {
$apiFormattedParams['location_type_id'] = $params['field_name'][2];
}
}
if (!empty($params['field_name'][3])) {
$apiFormattedParams['phone_type_id'] = $params['field_name'][3];
}

if ($apiFormattedParams['field_type'] != "Formatting" && CRM_Core_BAO_UFField::duplicateField($apiFormattedParams)) {
CRM_Core_Error::statusBounce(ts('The selected field already exists in this profile.'), NULL, ts('Field Not Added'));
}
else {
$params['weight'] = CRM_Core_BAO_UFField::autoWeight($params);
// @todo use api & $apiFormatter
$ufField = CRM_Core_BAO_UFField::add($params);

//reset other field is searchable and in selector settings, CRM-4363
Expand Down
Loading

0 comments on commit 3c8e8aa

Please sign in to comment.