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-20610 add a form for editing payment details #10774

Merged
merged 2 commits into from
Jul 27, 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
24 changes: 23 additions & 1 deletion CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4133,7 +4133,7 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
SELECT GROUP_CONCAT(fa.`name`) as financial_account,
ft.total_amount,
ft.payment_instrument_id,
ft.trxn_date, ft.trxn_id, ft.status_id, ft.check_number, ft.currency, ft.pan_truncation, ft.card_type_id
ft.trxn_date, ft.trxn_id, ft.status_id, ft.check_number, ft.currency, ft.pan_truncation, ft.card_type_id, ft.id

FROM civicrm_contribution con
LEFT JOIN civicrm_entity_financial_trxn eft ON (eft.entity_id = con.id AND eft.entity_table = 'civicrm_contribution')
Expand Down Expand Up @@ -4163,6 +4163,27 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
}
$paidByLabel .= " ({$creditCardType}{$pantruncation})";
}

// show payment edit link only for payments done via backoffice form
$paymentEditLink = '';
if (empty($resultDAO->payment_processor_id) && CRM_Core_Permission::check('edit contributions')) {
$links = array(
CRM_Core_Action::UPDATE => array(
'name' => "<i class='crm-i fa-pencil'></i>",
'url' => 'civicrm/payment/edit',
'qs' => "reset=1&id=%%id%%",
'title' => ts('Edit Payment'),
),
);
$paymentEditLink = CRM_Core_Action::formLink(
$links,
CRM_Core_Action::mask(array(CRM_Core_Permission::EDIT)),
array(
'id' => $resultDAO->id,
)
);
}

$val = array(
'total_amount' => $resultDAO->total_amount,
'financial_type' => $resultDAO->financial_account,
Expand All @@ -4171,6 +4192,7 @@ public static function getPaymentInfo($id, $component, $getTrxnInfo = FALSE, $us
'trxn_id' => $resultDAO->trxn_id,
'status' => $statuses[$resultDAO->status_id],
'currency' => $resultDAO->currency,
'action' => $paymentEditLink,
);
if ($paidByName == 'Check') {
$val['check_number'] = $resultDAO->check_number;
Expand Down
3 changes: 1 addition & 2 deletions CRM/Contribute/Form/AdditionalPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,14 @@ public function preProcess() {

if ($this->_view == 'transaction' && ($this->_action & CRM_Core_Action::BROWSE)) {
$paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($this->_id, $this->_component, TRUE);
$transactionRows = $paymentInfo['transaction'];
$title = ts('View Payment');
if ($this->_component == 'event') {
$info = CRM_Event_BAO_Participant::participantDetails($this->_id);
$title .= " - {$info['title']}";
}
CRM_Utils_System::setTitle($title);
$this->assign('transaction', TRUE);
$this->assign('rows', $transactionRows);
$this->assign('payments', $paymentInfo['transaction']);
return;
}
$this->_fromEmails = CRM_Core_BAO_Email::getFromEmail();
Expand Down
6 changes: 6 additions & 0 deletions CRM/Core/xml/Menu/payment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@
<is_public>true</is_public>
<weight>0</weight>
</item>
<item>
<path>civicrm/payment/edit</path>
<page_callback>CRM_Financial_Form_PaymentEdit</page_callback>
<access_arguments>access CiviContribute</access_arguments>
<component>CiviContribute</component>
</item>
</menu>
7 changes: 4 additions & 3 deletions CRM/Financial/Form/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,18 @@ public function setDefaultValues() {
* Add JS to show icons for the accepted credit cards.
*
* @param int $paymentProcessorID
* @param string $region
*/
public static function addCreditCardJs($paymentProcessorID = NULL) {
public static function addCreditCardJs($paymentProcessorID = NULL, $region = 'billing-block') {
$creditCards = CRM_Financial_BAO_PaymentProcessor::getCreditCards($paymentProcessorID);
$creditCardTypes = CRM_Core_Payment_Form::getCreditCardCSSNames($creditCards);
CRM_Core_Resources::singleton()
// CRM-20516: add BillingBlock script on billing-block region
// to support this feature in payment form snippet too.
->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10, 'billing-block', FALSE)
->addScriptFile('civicrm', 'templates/CRM/Core/BillingBlock.js', 10, $region, FALSE)
// workaround for CRM-13634
// ->addSetting(array('config' => array('creditCardTypes' => $creditCardTypes)));
->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';', '-9999', 'billing-block');
->addScript('CRM.config.creditCardTypes = ' . json_encode($creditCardTypes) . ';', '-9999', $region);
}

}
221 changes: 221 additions & 0 deletions CRM/Financial/Form/PaymentEdit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,221 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.7 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
*
* @package CRM
* @copyright CiviCRM LLC (c) 2004-2017
*/
class CRM_Financial_Form_PaymentEdit extends CRM_Core_Form {

/**
* The id of the financial trxn.
*
* @var int
*/
protected $_id;

/**
* The variable which holds the information of a financial transaction
*
* @var array
*/
protected $_values;

/**
* Explicitly declare the form context.
*/
public function getDefaultContext() {
return 'create';
}
/**
* Set variables up before form is built.
*/
public function preProcess() {
$this->_action = CRM_Core_Action::UPDATE;
parent::preProcess();
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$this->assign('id', $this->_id);

$this->_values = civicrm_api3('FinancialTrxn', 'getsingle', array('id' => $this->_id));
if (!empty($this->_values['payment_processor_id'])) {
CRM_Core_Error::statusBounce(ts('You cannot update this payment'));
}
}

/**
* Set default values.
*
* @return array
*/
public function setDefaultValues() {
$defaults = $this->_values;
if (!empty($defaults['card_type_id'])) {
// See comments in getPaymentFields function about why this nastiness exists.
$defaults['credit_card_type'] = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'card_type_id', $defaults['card_type_id']);
}

return $defaults;
}

/**
* Build quickForm.
*/
public function buildQuickForm() {
CRM_Utils_System::setTitle(ts('Update Payment details'));

$paymentFields = $this->getPaymentFields();
$this->assign('paymentFields', $paymentFields);
foreach ($paymentFields as $name => $paymentField) {
$this->add($paymentField['htmlType'],
$paymentField['name'],
$paymentField['title'],
$paymentField['attributes'],
TRUE
);
if (!empty($paymentField['rules'])) {
foreach ($paymentField['rules'] as $rule) {
$this->addRule($name,
$rule['rule_message'],
$rule['rule_name'],
$rule['rule_parameters']
);
}
}
}

$this->assign('currency', CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', $this->_values['currency'], 'symbol', 'name'));
$this->addButtons(array(
array(
'type' => 'submit',
'name' => ts('Update'),
'isDefault' => TRUE,
),
array(
'type' => 'cancel',
'name' => ts('Cancel'),
),
));
}

/**
* Process the form submission.
*/
public function postProcess() {
$params = array(
'id' => $this->_id,
'check_number' => CRM_Utils_Array::value('check_number', $this->_submitValues),
'pan_truncation' => CRM_Utils_Array::value('pan_truncation', $this->_submitValues),
'trxn_date' => CRM_Utils_Array::value('trxn_date', $this->_submitValues, date('YmdHis')),
);
if (!empty($this->_submitValues['credit_card_type'])) {
// See comments in getPaymentFields function about why this nastiness exists.
$params['card_type_id'] = CRM_Core_PseudoConstant::getKey(
'CRM_Financial_DAO_FinancialTrxn',
'card_type_id',
$this->_submitValues['credit_card_type']
);
}
// update the financial trxn
civicrm_api3('FinancialTrxn', 'create', $params);
CRM_Core_Session::singleton()->pushUserContext(CRM_Utils_System::url(CRM_Utils_System::currentPath()));
}

/**
* Get payment fields
*/
public function getPaymentFields() {
$paymentFields = array();
$paymentInstrument = CRM_Core_PseudoConstant::getName('CRM_Financial_DAO_FinancialTrxn', 'payment_instrument_id', $this->_values['payment_instrument_id']);
if ($paymentInstrument == 'Check') {
$paymentFields['check_number'] = array(
'htmlType' => 'text',
'name' => 'check_number',
'title' => ts('Check Number'),
'is_required' => FALSE,
'attributes' => NULL,
);
}
elseif ($paymentInstrument == 'Credit Card') {
// Ideally we would use $this->addField('card_type_id', array('entity' => 'FinancialTrxn'));
// to assign this field (& other fields on this form). However, the addCreditCardJs
// adds some 'pretty' to the credit card selection. The 'cost' of this is that because it
// was originally written to comply with front end needs (use of the word rather than the id)
// we have to do a lot of wrangling with our fields to make the BillingBlock.js code work.
// If you are reading this it means it's time to fix the BillingBlock.js to work with
// card_type_id and the pseudoconstant options & set this code free. No returns.
CRM_Financial_Form_Payment::addCreditCardJs(NULL, 'payment-edit-block');
$paymentFields['credit_card_type'] = array(
'htmlType' => 'select',
'name' => 'credit_card_type',
'title' => ts('Card Type'),
'attributes' => array('' => ts('- select -')) + CRM_Contribute_PseudoConstant::creditCard(),
);
$paymentFields['pan_truncation'] = array(
'htmlType' => 'text',
'name' => 'pan_truncation',
'title' => ts('Last 4 digits of the card'),
'attributes' => array(
'size' => 4,
'maxlength' => 4,
'minlength' => 4,
'autocomplete' => 'off',
),
'rules' => array(
array(
'rule_message' => ts('Please enter valid last 4 digit card number.'),
'rule_name' => 'numeric',
'rule_parameters' => NULL,
),
),
);
}
$paymentFields += array(
'trxn_date' => array(
'htmlType' => 'datepicker',
'name' => 'trxn_date',
'title' => ts('Transaction Date'),
'attributes' => array(
'date' => 'yyyy-mm-dd',
'time' => 24,
),
),
'total_amount' => array(
'htmlType' => 'text',
'name' => 'total_amount',
'title' => ts('Total Amount'),
'attributes' => array(
'readonly' => TRUE,
'size' => 6,
),
),
);

return $paymentFields;
}

}
30 changes: 1 addition & 29 deletions templates/CRM/Contribute/Form/AdditionalPayment.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,7 @@
+--------------------------------------------------------------------+
*}
{if $transaction}
{if !empty($rows)}
<table id='info'>
<tr class="columnheader">
<th>{ts}Amount{/ts}</th>
<th>{ts}Type{/ts}</th>
<th>{ts}Payment Method{/ts}</th>
<th>{ts}Received{/ts}</th>
<th>{ts}Transaction ID{/ts}</th>
<th>{ts}Status{/ts}</th>
</tr>
{foreach from=$rows item=row}
<tr>
<td>{$row.total_amount|crmMoney:$row.currency}</td>
<td>{$row.financial_type}</td>
<td>{$row.payment_instrument}{if $row.check_number} (#{$row.check_number}){/if}</td>
<td>{$row.receive_date|crmDate}</td>
<td>{$row.trxn_id}</td>
<td>{$row.status}</td>
</tr>
{/foreach}
<table>
{else}
{if $component eq 'event'}
{assign var='entity' value='participant'}
{else}
{assign var='entity' value=$component}
{/if}
{ts 1=$entity}No payments found for this %1 record{/ts}
{/if}
{include file="CRM/Contribute/Form/PaymentInfoBlock.tpl"}
{if !$suppressPaymentFormButtons}
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl"}
Expand Down
Loading