Commit f9c0a2ed authored by Monish Deb's avatar Monish Deb

Refactored task actions

parent c4825c57
......@@ -440,4 +440,20 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
return $summary;
}
public static function actions() {
$params = $_GET;
if ($params['action'] == CRM_Grant_BAO_GrantPayment::STOP) {
$dao = new CRM_Grant_DAO_GrantPayment();
$dao->id = $params['id'];
$dao->payment_status_id = CRM_Core_PseudoConstant::getKey('CRM_Grant_BAO_GrantPayment', 'payment_status_id', 'Stopped');
$dao->save();
CRM_Core_Session::setStatus(ts('Selected Grant Payment has been stopped successfully'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/payment/search', '_qf_PaymentSearch_display=true&force=1&qfKey=' . $params['key']));
}
elseif ($params['action'] == CRM_Grant_BAO_GrantPayment::REPRINT) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/payment/reprint', 'reset=1&prid=' . $params['id']));
}
}
}
......@@ -57,7 +57,7 @@ class CRM_Grant_BAO_PaymentSearch {
$displayRelationshipType = NULL,
$operator = 'AND'
) {
$this->_params =& $params;
$this->_params = $params;
if ($this->_params == NULL) {
$this->_params = array();
......@@ -185,11 +185,15 @@ class CRM_Grant_BAO_PaymentSearch {
function whereClauseSingle(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
switch ($values[0]) {
case 'id':
$this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("p.$name", $op, $value, 'Int');
return;
case 'payment_status_id':
$this->_where[$grouping][] = CRM_Contact_BAO_Query::buildClause("p.$name", $op, $value, 'Int');
$this->_qill[$grouping][] = ts('Payment Status is ') . CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_GrantPayment', 'payment_status_id', $value);
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
return;
case 'payable_to_name':
......@@ -270,13 +274,26 @@ class CRM_Grant_BAO_PaymentSearch {
* @access public
*/
function select() {
$this->_select['id'] = 'p.id as id';
$this->_select['payable_to_name'] = 'cc.display_name as payable_to_name';
$this->_select['payment_batch_number'] = 'b.id as payment_batch_number';
$this->_select['payment_status_id'] = 'p.payment_status_id';
$this->_select['payment_created_date'] = 'p.payment_created_date';
$this->_select['amount'] = 'ft.total_amount as amount';
$this->_select['payment_number'] = 'ft.check_number as payment_number';
$clauses = [
'id' => 'p.id as id',
'contact_id' => 'fi.contact_id',
'payable_to_name' => 'cc.display_name as payable_to_name',
'payment_batch_number' => 'b.id as payment_batch_number',
'payment_status_id' => 'p.payment_status_id',
'payment_created_date' => 'p.payment_created_date',
'amount' => 'ft.total_amount as amount',
'payment_number' => 'CASE WHEN p.replaces_check_number <> \'\' THEN p.replaces_check_number ELSE ft.check_number END AS payment_number',
'payment_created_date' => 'p.payment_created_date',
'payment_date' => 'ft.trxn_date as payment_date',
'currency' => 'fi.currency',
'payment_reason' => 'p.payment_reason',
'replaces_payment_id' => 'p.replaces_payment_id',
];
foreach ($clauses as $property => $clause) {
if (!empty($this->_returnProperties[$property])) {
$this->_select[$property] = $clause;
}
}
return $this->_select;
}
......@@ -288,7 +305,6 @@ class CRM_Grant_BAO_PaymentSearch {
$this->_element['payment_status_id'] = 1;
$this->_element['payment_created_date'] = 1;
$this->_element['amount'] = 1;
return $this->_select;
return $this->_element;
}
......@@ -301,9 +317,7 @@ class CRM_Grant_BAO_PaymentSearch {
*/
static function where(&$query) {
foreach (array_keys($query->_params) as $id) {
if (substr($query->_params[$id][0], 0, 6) == 'grant_') {
self::whereClauseSingle($query->_params[$id], $query);
}
self::whereClauseSingle($query->_params[$id], $query);
}
}
......@@ -317,9 +331,7 @@ class CRM_Grant_BAO_PaymentSearch {
return (isset($this->_qill)) ? $this->_qill : "";
}
static function defaultReturnProperties($mode,
$includeCustomFields = TRUE
) {
static function defaultReturnProperties($mode = CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT, $includeCustomFields = TRUE) {
$properties = NULL;
if ($mode & CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT) {
$properties = array(
......@@ -410,6 +422,7 @@ class CRM_Grant_BAO_PaymentSearch {
) {
list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts);
$groupBy = '';
$order = $orderBy = $limit = '';
if (!$count) {
......@@ -451,18 +464,13 @@ class CRM_Grant_BAO_PaymentSearch {
'SELECT p.id as id';
}
}
$groupBy = 'GROUP BY p.id';
$query = "$select $from $where $having $groupBy $order $limit";
$groupBy = $this->_groupByComponentClause;
}
$query = "$select $from $where $having $groupBy $order $limit";
if ($count) {
$query = "$select $from $where";
return CRM_Core_DAO::singleValueQuery($query);
}
elseif (empty($query)) {
$query = "$select $from $where $having $groupBy $order $limit";
}
//CRM_Core_Error::debug_var('q', $query);
$dao = CRM_Core_DAO::executeQuery($query);
if ($groupContacts) {
......
......@@ -196,6 +196,17 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO
'dataPattern' => '',
'export' => true,
) ,
'replaces_check_number' => array(
'name' => 'replaces_check_number',
'type' => CRM_Utils_Type::T_STRING,
'maxlength' => 20,
'size' => CRM_Utils_Type::EIGHT,
'import' => true,
'where' => 'civicrm_payment.replaces_check_number',
'headerPattern' => '',
'dataPattern' => '',
'export' => true,
) ,
);
}
return self::$_fields;
......
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 4.4 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2011 |
+--------------------------------------------------------------------+
| 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-2011
* $Id$
*
*/
/**
* This class generates form components for processing a Grant
*
*
*/
class CRM_Grant_Form_Payment_View extends CRM_Core_Form {
/**
* Function to set variables up before form is built
*
* @return void
* @access public
/**
* Function to set variables up before form is built
*
* @return void
* @access public
*/
public function preProcess() {
$this->_id = CRM_Utils_Request::retrieve('id', 'Positive', $this);
$context = CRM_Utils_Request::retrieve('context', 'String', $this);
$this->assign('context', $context);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/grant/payment/search', '_qf_PaymentSearch_display=true&force=1&reset=1'));
$values = array();
$params['id'] = $this->_id;
CRM_Grant_BAO_GrantPayment::retrieve( $params, $values);
$paymentStatus = CRM_Core_OptionGroup::values( 'grant_payment_status' );
$contributionTypes = CRM_Grant_BAO_GrantProgram::contributionTypes();
$this->assign('payment_status_id', $paymentStatus[$values['payment_status_id']]);
$this->assign('financial_type_id', $contributionTypes[$values['financial_type_id']]);
$grantTokens = array(
'payment_batch_number',
'payment_number',
'payment_created_date',
'payment_date',
'payable_to_name',
'payable_to_address',
'amount',
'currency',
'payment_reason',
'replaces_payment_id'
);
foreach ($grantTokens as $token) {
$this->assign($token, CRM_Utils_Array::value($token, $values));
$context = CRM_Utils_Request::retrieve('context', 'String', $this);
if (!$this->_action) {
$this->_action = $_REQUEST['action'];
}
$this->assign('id', $this->_id);
$this->assign('context', $context);
$session = CRM_Core_Session::singleton();
$session->pushUserContext(CRM_Utils_System::url('civicrm/grant/payment/search', '_qf_PaymentSearch_display=true&force=1&reset=1'));
}
/**
* Function to build the form
*
......@@ -87,30 +30,63 @@ class CRM_Grant_Form_Payment_View extends CRM_Core_Form {
* @access public
*/
public function buildQuickForm() {
if ($this->_action & CRM_Core_Action::VIEW) {
$this->addButtons(array(
array (
'type' => 'cancel',
'name' => ts('Cancel'),
'isDefault' => TRUE)
)
);
}
elseif (($this->_action & CRM_Core_Action::STOP) || ($this->_action & CRM_Core_Action::REPRINT) || ($this->_action & CRM_Core_Action::WITHDRAW)) {
$this->addButtons(array(
array (
'type' => 'submit',
'name' => ts('OK'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE),
array (
'type' => 'cancel',
'name' => ts('Cancel'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
if ($this->_action == CRM_Core_Action::VIEW) {
$returnProperties = array_merge(
CRM_Grant_BAO_PaymentSearch::defaultReturnProperties(),
[
'id' => 1,
'contact_id' => 1,
'payment_created_date' => 1,
'payment_date' => 1,
'currency' => 1,
'payment_reason' => 1,
'replaces_payment_id' => 1,
]
);
$params = ['id' => $this->_id];
$p = new CRM_Grant_BAO_PaymentSearch(
CRM_Grant_BAO_PaymentSearch::convertFormValues($params),
$returnProperties,
NULL,
FALSE,
FALSE,
CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT
);
$values = $p->searchQuery()->fetchAll()[0];
$values['payment_status_id'] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_DAO_GrantPayment', 'payment_status_id', $values['payment_status_id']);
$values['payable_to_address'] = CRM_Grant_BAO_GrantProgram::getAddress($values['contact_id'], NULL, TRUE);
foreach (array_keys($returnProperties) as $token) {
$this->assign($token, CRM_Utils_Array::value($token, $values));
}
$this->addButtons(array(
array (
'type' => 'cancel',
'name' => ts('Cancel'),
'isDefault' => TRUE)
)
);
}
else {
$this->assign('action1', $this->_action);
if ($this->_action == CRM_Grant_BAO_GrantPayment::STOP) {
CRM_Utils_System::setTitle(ts('Stop Grants Payment'));
CRM_Core_Session::setStatus(ts('Selected Grant Payment has been stopped successfully.'), '', 'no-popup');
}
$this->addButtons(array(
array (
'type' => 'submit',
'name' => ts('OK'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
'isDefault' => TRUE),
array (
'type' => 'cancel',
'name' => ts('Cancel'),
'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
),
));
}
}
public function postProcess() {
......@@ -124,10 +100,10 @@ class CRM_Grant_Form_Payment_View extends CRM_Core_Form {
$dao->save();
CRM_Core_Session::setStatus(ts('Selected Grant Payment has been stopped successfully.'));
CRM_Utils_System::redirect( CRM_Utils_System::url('civicrm/grant/payment/search', 'reset=1&force=1'));
}
}
elseif ($this->_action & CRM_Core_Action::REPRINT) {
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/payment/reprint', 'reset=1&prid=' . $this->_id));
}
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/payment/reprint', 'reset=1&prid=' . $this->_id));
}
elseif ($this->_action & CRM_Core_Action::WITHDRAW) {
$query = "SELECT cp.id as pid, cg.amount_granted as total_amount, cp.currency, cp.payment_reason, cp.contact_id as id, cep.entity_id as grant_id, cg.grant_program_id, cg.grant_type_id FROM civicrm_payment as cp LEFT JOIN civicrm_entity_payment as cep ON cep.payment_id = cp.id LEFT JOIN civicrm_grant as cg ON cg.id = cep.entity_id WHERE cp.id IN (".$this->_id.")";
$grantDao = CRM_Grant_DAO_Grant::executeQuery($query);
......@@ -150,5 +126,5 @@ class CRM_Grant_Form_Payment_View extends CRM_Core_Form {
CRM_Core_Session::setStatus(ts('Selected Grant Payment has been withdraw successfully.'));
CRM_Utils_System::redirect(CRM_Utils_System::url('civicrm/grant/payment/search', 'reset=1&force=1'));
}
}
}
\ No newline at end of file
}
}
This diff is collapsed.
......@@ -127,7 +127,7 @@ class CRM_Grant_Form_PaymentTask extends CRM_Core_Form {
$form->_grantPaymentIds = $form->_componentIds = $ids;
//set the context for redirection for any task actions
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $this);
$qfKey = CRM_Utils_Request::retrieve('qfKey', 'String', $form);
$urlParams = 'force=1';
if (CRM_Utils_Rule::qfKey($qfKey)) {
$urlParams .= "&qfKey=$qfKey";
......@@ -141,7 +141,7 @@ class CRM_Grant_Form_PaymentTask extends CRM_Core_Form {
* since its used for things like send email
*/
public function setContactIDs() {
$this->_contactIds =& CRM_Core_DAO::getContactIDsFromComponent($this->_grantIds,
$this->_contactIds = CRM_Core_DAO::getContactIDsFromComponent($this->_grantIds,
'civicrm_grant'
);
}
......
......@@ -51,7 +51,7 @@ class CRM_Grant_Form_Task_Cancel extends CRM_Grant_Form_PaymentTask {
//check permission for delete.
if (!CRM_Core_Permission::checkActionPermission('CiviGrant', CRM_Core_Action::DELETE)) {
CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
CRM_Core_Error::fatal(ts('You do not have permission to access this page'));
}
}
......@@ -77,23 +77,20 @@ class CRM_Grant_Form_Task_Cancel extends CRM_Grant_Form_PaymentTask {
public function postProcess() {
if (empty($this->_grantPaymentIds)) {
return FALSE;
}
// change status of payment(s) to Cancelled And grant status to Eligible
$query = "UPDATE civicrm_grant cg
LEFT JOIN civicrm_entity_payment cep ON cep.entity_id = cg.id
LEFT JOIN civicrm_payment cp ON cp.id = cep.payment_id
SET cg.status_id = %1,
cp.payment_status_id = %2
WHERE cp.id IN (" . implode(',', $this->_grantPaymentIds) . ") AND cep.entity_table = 'civicrm_grant'";
$params = array(
1 => array(CRM_Core_OptionGroup::getValue('grant_status', 'Eligible', 'name'), 'Integer'),
2 => array(CRM_Core_OptionGroup::getValue('grant_payment_status', 'Cancelled', 'name'), 'Integer'),
}
$sql = sprintf("UPDATE civicrm_payment p
INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = p.financial_trxn_id AND eft.entity_table = 'civicrm_grant'
INNER JOIN civicrm_grant g ON g.id = eft.entity_id
SET g.status_id = %s, p.payment_status_id = %s
WHERE p.id IN (%s) ",
CRM_Core_PseudoConstant::getKey('CRM_Grant_DAO_Grant', 'status_id', 'Eligible'),
CRM_Core_OptionGroup::getValue('grant_payment_status', 'Cancelled', 'name'),
implode(',', $this->_grantPaymentIds)
);
CRM_Core_DAO::executeQuery($query, $params);
CRM_Core_DAO::executeQuery($sql);
CRM_Core_Session::setStatus(ts('Cancel Grant Payments(s): %1', array(1 => count($this->_grantPaymentIds))));
CRM_Core_Session::setStatus(ts('Total Selected Grant Payments(s): %1', array(1 => count($this->_grantPaymentIds))));
}
}
This diff is collapsed.
......@@ -101,6 +101,7 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask {
$financialParams[$attr] = $values[$attr];
}
}
$replacedCheckNumber = CRM_Core_DAO::singleValueQuery('SELECT check_number FROM civicrm_financial_trxn WHERE id = ' . $financialTrxnID);
if (!empty($financialParams)) {
$financialParams['id'] = $financialTrxnID;
civicrm_api3('FinancialTrxn', 'create', $financialParams);
......@@ -132,6 +133,7 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask {
'payment_status_id' => $reprintedStatusID,
'payment_reason' => CRM_Utils_Array::value('payment_reason', $values, $paymentDAO->payment_reason),
'replaces_payment_id' => $paymentId,
'replaces_check_number' => $replacedCheckNumber,
];
CRM_Grant_BAO_GrantPayment::add($grantPaymentRecord);
}
......
......@@ -169,8 +169,8 @@ class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements
CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT
);
$this->_query->_distinctComponentClause = " ft.id";
$this->_query->_groupByComponentClause = " GROUP BY ft.id";
$this->_query->_distinctComponentClause = " p.id ";
$this->_query->_groupByComponentClause = " GROUP BY p.id";
}
......@@ -186,7 +186,6 @@ class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements
*
*/
static function &links($key = NULL) {
$id = CRM_Utils_Request::retrieve('id', 'Integer', $this);
$extraParams = ($key) ? "&key={$key}" : NULL;
if (!(self::$_links)) {
......@@ -199,21 +198,24 @@ class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements
),
CRM_Grant_BAO_GrantPayment::STOP => array(
'name' => ts('Stop'),
'url' => 'civicrm/grant/payment',
'qs' => 'reset=1&action=stop&id=%%id%%&context=%%cxt%%' . $extraParams,
'title' => ts('Edit Grant'),
'url' => 'civicrm/grant-payment/actions',
'extra' => 'onclick="confirm(\'Do you want to stop this payment?\')"',
'qs' => 'reset=1&action=' . CRM_Grant_BAO_GrantPayment::STOP . '&id=%%id%%' . $extraParams,
'title' => ts('Stop Grant'),
),
CRM_Grant_BAO_GrantPayment::REPRINT => array(
'name' => ts('Reprint'),
'url' => 'civicrm/grant/payment',
'qs' => 'reset=1&action=reprint&id=%%id%%&context=%%cxt%%' . $extraParams,
'title' => ts('Edit Grant'),
'url' => 'civicrm/grant-payment/actions',
'extra' => 'onclick="confirm(\'Do you want to reprint this payment?\')"',
'qs' => 'reset=1&action=' . CRM_Grant_BAO_GrantPayment::REPRINT . '&id=%%id%%' . $extraParams,
'title' => ts('Reprint Grant'),
),
CRM_Grant_BAO_GrantPayment::WITHDRAW => array(
'name' => ts('Withdraw'),
'url' => 'civicrm/grant/payment',
'qs' => 'reset=1&action=withdraw&id=%%id%%&context=%%cxt%%' . $extraParams,
'title' => ts('Edit Grant'),
'url' => 'civicrm/grant-payment/actions',
'extra' => 'onclick="confirm(\'Do you want to withdraw this payment?\')"',
'qs' => 'reset=1&action=' . CRM_Grant_BAO_GrantPayment::WITHDRAW . '&id=%%id%%' . $extraParams,
'title' => ts('Withdraw Grant'),
),
);
......@@ -304,13 +306,11 @@ class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements
while ($result->fetch()) {
$row = array();
// the columns we are interested in
foreach (self::$_properties as $property) {
if (isset($result->$property)) {
if ($property == 'payment_status_id') {
$paymentStatus = CRM_Core_OptionGroup::values('grant_payment_status');
$row[$property] = $paymentStatus[$result->$property];
$row[$property] = CRM_Core_PseudoConstant::getLabel('CRM_Grant_BAO_GrantPayment', 'payment_status_id', $result->$property);
}
else {
$row[$property] = $result->$property;
......@@ -322,10 +322,14 @@ class CRM_Grant_Selector_PaymentSearch extends CRM_Core_Selector_Base implements
}
$this->id = $result->id;
$link = self::links( $this->_key);
if ( $result->payment_status_id == 2 || $result->payment_status_id == 4 ) {
unset($link[121]);
unset($link[1212]);
unset($link[12121]);
if (!in_array($result->payment_status_id, [
CRM_Core_PseudoConstant::getKey('CRM_Grant_BAO_GrantPayment', 'payment_status_id', 'Printed'),
CRM_Core_PseudoConstant::getKey('CRM_Grant_BAO_GrantPayment', 'payment_status_id', 'Reprinted')
])
) {
unset($link[CRM_Grant_BAO_GrantPayment::STOP]);
unset($link[CRM_Grant_BAO_GrantPayment::REPRINT]);
unset($link[CRM_Grant_BAO_GrantPayment::WITHDRAW]);
}
$row['action'] = CRM_Core_Action::formLink(
......
......@@ -259,4 +259,29 @@ public static function getPanesMapper(&$panes) {
return $result;
}
public static function buildSearchForm(&$form) {
$paymentStatus = CRM_Core_OptionGroup::values('grant_payment_status');
$form->add('select', 'payment_status_id', ts('Status'),
array('' => ts('- select -')) + $paymentStatus);
$form->add('select', 'payment_batch_number',
ts('Batch'),
// CRM-19325
['' => ts('None')] + CRM_Contribute_PseudoConstant::batch(),
FALSE, array('class' => 'crm-select2')
);
$form->addElement('text', 'payment_number', ts('Payment Number'), array('size' => 10, 'maxlength' => 10));
CRM_Core_Form_Date::buildDateRange($form, 'payment_created_date', 1, '_low', '_high', ts('From:'), FALSE);
$form->addElement('text', 'payable_to_name', ts('Payee name'), CRM_Core_DAO::getAttribute('CRM_Grant_DAO_GrantPayment', 'payable_to_name'));
$form->add('text', 'amount_low', ts('From'), array('size' => 8, 'maxlength' => 8));
$form->addRule('amount_low', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('9.99', ' '))), 'money');
$form->add('text', 'amount_high', ts('To'), array('size' => 8, 'maxlength' => 8));
$form->addRule('amount_high', ts('Please enter a valid money value (e.g. %1).', array(1 => CRM_Utils_Money::format('99.99', ' '))), 'money');
}
}
......@@ -387,6 +387,8 @@ function grantprograms_civicrm_buildForm($formName, &$form) {
$form->add('select', 'status_id', ts('Grant Status'), $grantStatuses, TRUE);
}
CRM_Grant_Form_Task_GrantPayment::buildPaymentBlock($form, FALSE);
if ($form->getVar('_id')) {
if (CRM_Core_Permission::check('administer CiviGrant')) {
$form->add('text', 'assessment', ts('Assessment'));
......@@ -611,8 +613,7 @@ function grantprograms_civicrm_queryObjects(&$queryObjects, $type) {
* @param object $form form object
*
*/
function grantprograms_civicrm_validate($formName, &$fields, &$files, &$form) {
$errors = NULL;
function grantprograms_civicrm_validateForm($formName, &$fields, &$files, &$form, &$errors) {
if ($formName == "CRM_Admin_Form_Options" && ($form->getVar('_action') & CRM_Core_Action::DELETE) && $form->getVar('_gName') == "grant_type") {
$defaults = array();
$valId = $form->getVar('_values');
......@@ -629,7 +630,7 @@ function grantprograms_civicrm_validate($formName, &$fields, &$files, &$form) {
CRM_Core_Session::setStatus(ts('You cannot delete this Grant Type because '. implode(' and ', $error ) .' are currently using it.'), ts("Sorry"), "error");
}
}
if ($formName == 'CRM_Grant_Form_Grant' && ($form->getVar('_action') != CRM_Core_Action::DELETE)) {
if ($formName == 'CRM_Grant_Form_Grant') {
$defaults = array();
$params['id'] = $form->_submitValues['grant_program_id'];
CRM_Grant_BAO_GrantProgram::retrieve($params, $defaults);
......@@ -640,6 +641,22 @@ function grantprograms_civicrm_validate($formName, &$fields, &$files, &$form) {
if (CRM_Utils_Array::value('amount_granted', $fields) && $fields['amount_granted'] > 0 && !CRM_Utils_Array::value('financial_type_id', $fields) && CRM_Utils_Array::value('money_transfer_date', $fields)) {
$errors['financial_type_id'] = ts('Financial Type is a required field if Amount is Granted');
}
if (!empty($fields['status_id'])) {
$grantStatuses = CRM_Core_OptionGroup::values('grant_status');
if (in_array($grantStatuses[$fields['status_id']], ['Paid', 'Withdrawn', 'Approved for Payment']) && empty($fields['financial_type_id'])) {
$errors['financial_type_id'] = ts('Financial Type is a required field');
}
if ($grantStatuses[$fields['status_id']] == 'Paid') {
foreach([
'check_number' => ts('Check Number'),
'contribution_batch_id' => ts('Batch'),
] as $attr => $label) {
if (empty($fields[$attr])) {
$errors[$attr] = ts($label . ' is a required field');
}
}
}
}
}
if ($formName == 'CRM_Grant_Form_Search') {
if (isset($fields['task']) && CRM_Utils_Array::value('task', $fields) == PAY_GRANTS || CRM_Utils_Array::value('task', $fields) == DELETE_GRANTS) {
......@@ -731,7 +748,7 @@ function grantprograms_civicrm_pre($op, $objectName, $id, &$params) {
function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
if ($objectName == 'Grant' && in_array($op, ['edit', 'create'])) {
$params = CRM_Core_BAO_Setting::getItem("grant params", 'grantprograms_civicrm_pre');
$params = CRM_Core_BAO_Cache::getItem("grant params", 'grantprograms_civicrm_pre');
$smarty = CRM_Core_Smarty::singleton();
$previousGrant = $smarty->get_template_vars('previousGrant');
// core bug $op always return 'create'
......@@ -787,7 +804,6 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$financialItemStatus = CRM_Core_PseudoConstant::accountOptionValues('financial_item_status');
$amount = $objectRef->amount_total;
$financialItemStatusID = array_search('Paid', $financialItemStatus);
$params = [];
if ($objectRef->status_id == array_search('Approved for Payment', $grantStatuses)) {
$params['to_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($objectRef->financial_type_id, 'Accounts Receivable Account is');
$financialItemStatusID = array_search('Unpaid', $financialItemStatus);
......@@ -829,6 +845,16 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
$trxnParams['from_financial_account_id'] = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($objectRef->financial_type_id, 'Accounts Receivable Account is');
}
$trxnId = CRM_Core_BAO_FinancialTrxn::create($trxnParams);
if ($objectRef->status_id == array_search('Paid', $grantStatuses)) {
CRM_Grant_Form_Task_GrantPayment::processPaymentDetails([
'trxn_id' => $trxnId->id,
'batch_id' => $params['contribution_batch_id'],
'check_number' => $params['check_number'],
'description' => CRM_Utils_Array::value('description', $params),
]);
}
if ($createItem) {
$financialAccountId = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($objectRef->financial_type_id, 'Accounts Receivable Account is');
$itemParams = array(
......@@ -836,7 +862,7 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
'contact_id' => $objectRef->contact_id,
'currency' => $objectRef->currency,
'amount' => $amount,
'description' => NULL,
'description' => CRM_Utils_Array::value('description', $params),
'status_id' => $financialItemStatusID,
'financial_account_id' => $financialAccountId,
'entity_table' => 'civicrm_grant',
......
......@@ -34,29 +34,10 @@ CREATE TABLE IF NOT EXISTS `civicrm_payment` (
`payment_reason` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Payment Reason.',
`payment_status_id` int(10) unsigned DEFAULT NULL COMMENT 'Payment Status ID',
`replaces_payment_id` varchar(8) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Replaces Payment Id.',
`replaces_check_number` varchar(20) unsigned NOT NULL COMMENT 'Replaced Check Number',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;
-- create civicrm_entity_payment
CREATE TABLE IF NOT EXISTS `civicrm_entity_payment` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
`payment_id` int(10) unsigned NOT NULL COMMENT 'Type of grant. Implicit FK to civicrm_payment.',
`entity_table` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'Entity Table.',
`entity_id` int(10) unsigned NOT NULL COMMENT 'Entity ID',
PRIMARY KEY (`id`),
KEY `FK_civicrm_entity_payment_payment_id` (`payment_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `civicrm_entity_payment`
ALTER TABLE `civicrm_entity_payment`
ADD CONSTRAINT `FK_civicrm_entity_payment_payment_id` FOREIGN KEY (`payment_id`) REFERENCES `civicrm_payment` (`id`);
-- create civicrm_grant_program
CREATE TABLE IF NOT EXISTS `civicrm_grant_program` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Grant Program ID',
......
......@@ -58,7 +58,25 @@
{/if}
</td>
</tr>
<tr class="crm-grant-form-block-batch_id">
<td class="label">{$form.batch_id.label}</td>
<td>{$form.batch_id.html}</td>
</tr>
</tbody></table>
<div class="crm-accordion-wrapper" id="payment-details">
<div class="crm-accordion-header">{ts}Payment Details{/ts}</div><!-- /.crm-accordion-header -->
<div class="crm-accordion-body" style="display: block;">
<table class="form-layout-compressed">
{foreach from=$paymentFields key=fieldName item=paymentField}
{assign var='name' value=$fieldName}
<tr class="crm-container {$name}-section">
<td class="label">{$form.$name.label}</td>
<td class="content">{$form.$name.html}</td>
</tr>
{/foreach}
</table>
</div><!-- /.crm-accordion-body -->
</div>
{/if}
{literal}
......@@ -70,6 +88,7 @@
$('.crm-grant-form-block-assessment').insertAfter('.crm-grant-form-block-amount_requested');
$('.crm-grant-form-block-prev_assessment').insertAfter('.crm-grant-form-block-assessment');
$('.crm-grant-form-block-financial_type').insertAfter('.crm-grant-form-block-money_transfer_date');