Commit 8ce39eb9 authored by Monish Deb's avatar Monish Deb

Fix 'Find Grant' search form and other refactoring changes

parent 469da524
......@@ -36,7 +36,9 @@
class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
const
STOP = 1,
REPRINT = 2;
/**
* static field for all the grant information that we can potentially export
* @var array
......@@ -93,7 +95,7 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
'data_type' => CRM_Utils_Type::T_INT
),
'payment_number' => array(
'title' => 'Payment Number',
'title' => 'Check Number',
'name' => 'payment_number',
'data_type' => CRM_Utils_Type::T_INT
),
......@@ -170,26 +172,19 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
* @static
* @return object
*/
static function add(&$params, &$ids) {
static function add(&$params, &$ids = []) {
if (empty($params)) {
return;
}
if (isset( $params['total_amount'])) {
if (isset($params['total_amount'])) {
$params[$field] = CRM_Utils_Rule::cleanMoney($params['total_amount']);
}
// convert dates to mysql format
$dates = array(
'payment_date',
'payment_created_date'
);
foreach ($dates as $date) {
if (isset($params[$date])) {
$params[$date] = CRM_Utils_Date::processDate($params[$date], NULL, TRUE);
}
if (isset($params['payment_created_date'])) {
$params['payment_created_date'] = CRM_Utils_Date::processDate($params['payment_created_date'], NULL, TRUE);
}
$grantPayment = new CRM_Grant_DAO_GrantPayment();
$grantPayment->id = CRM_Utils_Array::value('id', $ids);
......@@ -321,9 +316,7 @@ class CRM_Grant_BAO_GrantPayment extends CRM_Grant_DAO_GrantPayment {
$grantDao->fetch();
if (!$grantDao->N) {
if ($params['messageTemplateID']) {
CRM_Core_Error::fatal(ts('No such message template.'));
}
CRM_Core_Error::fatal(ts('No such message template.'));
}
$subject = $grantDao->subject;
$html = $grantDao->html;
......
......@@ -309,14 +309,12 @@ WHERE civicrm_contact.id = $id ";
if (CRM_Utils_Array::value('is_auto_email', $values)) {
list($displayName, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($contactID);
if (isset($email)) {
$grantStatuses = CRM_Core_OptionGroup::values('grant_status', FALSE, FALSE, FALSE, NULL, 'name');
$grantStatuses = $grantStatuses[$values['status_id']];
$valueName = strtolower($grantStatuses);
if ($grantStatuses == 'Awaiting Information') {
$explode = explode(' ', $grantStatuses);
$valueName = strtolower($grantStatus);
if ($grantStatus == 'Awaiting Information') {
$explode = explode(' ', $grantStatuse);
$valueName = strtolower($explode[0]) . '_info';
}
elseif (strstr($grantStatuses, 'Approved')) {
elseif (strstr($grantStatus, 'Approved')) {
$valueName = strtolower('Approved');
}
$sendTemplateParams = array(
......@@ -451,4 +449,121 @@ WHERE civicrm_contact.id = $id ";
);
CRM_Activity_BAO_Activity::create($params);
}
public static function getAssetFinancialAccountID() {
$relationTypeId = key(CRM_Core_PseudoConstant::accountOptionValues('financial_account_type', NULL, " AND v.name LIKE 'Asset' "));
return CRM_Core_DAO::singleValueQuery(
"SELECT id FROM civicrm_financial_account WHERE is_default = 1 AND financial_account_type_id = %1",
[1 => [$relationTypeId, 'Integer']]
);
}
public static function convertNumberToWords($number) {
$hyphen = '-';
$conjunction = ' and ';
$separator = ', ';
$negative = 'negative ';
$decimal = ' and ';
$dictionary = array(
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
10 => 'ten',
11 => 'eleven',
12 => 'twelve',
13 => 'thirteen',
14 => 'fourteen',
15 => 'fifteen',
16 => 'sixteen',
17 => 'seventeen',
18 => 'eighteen',
19 => 'nineteen',
20 => 'twenty',
30 => 'thirty',
40 => 'fourty',
50 => 'fifty',
60 => 'sixty',
70 => 'seventy',
80 => 'eighty',
90 => 'ninety',
100 => 'hundred',
1000 => 'thousand',
1000000 => 'million',
1000000000 => 'billion',
1000000000000 => 'trillion',
1000000000000000 => 'quadrillion',
1000000000000000000 => 'quintillion'
);
if (!is_numeric($number)) {
return FALSE;
}
if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
// overflow
trigger_error(
'convertNumberToWords only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
E_USER_WARNING
);
return FALSE;
}
if ($number < 0) {
return $negative . self::convertNumberToWords(abs($number));
}
$string = $fraction = NULL;
if (strpos($number, '.') !== FALSE) {
list($number, $fraction) = explode('.', $number);
}
switch (TRUE) {
case $number < 21:
$string = $dictionary[$number];
break;
case $number < 100:
$tens = ((int) ($number / 10)) * 10;
$units = $number % 10;
$string = $dictionary[$tens];
if ($units) {
$string .= $hyphen . $dictionary[$units];
}
break;
case $number < 1000:
$hundreds = $number / 100;
$remainder = $number % 100;
$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
if ($remainder) {
$string .= $conjunction . self::convertNumberToWords(abs($remainder));
}
break;
default:
$baseUnit = pow(1000, floor(log($number, 1000)));
$numBaseUnits = (int) ($number / $baseUnit);
$remainder = $number % $baseUnit;
$string = self::convertNumberToWords($numBaseUnits) . ' ' . $dictionary[$baseUnit];
if ($remainder) {
$string .= $remainder < 100 ? $conjunction : $separator;
$string .= self::convertNumberToWords($remainder);
}
break;
}
if (NULL !== $fraction && is_numeric($fraction)) {
$string .= $decimal;
$string .= $fraction . '/100';
}
return $string;
}
}
<?php
<?php
/*
+--------------------------------------------------------------------+
......@@ -34,28 +34,28 @@
*
*/
class CRM_Grant_BAO_PaymentSearch {
class CRM_Grant_BAO_PaymentSearch {
const
MODE_GRANT_PAYMENT = 1;
static function &getFields() {
$fields = array();
$fields = CRM_Grant_BAO_GrantPayment::exportableFields();
return $fields;
}
function __construct($params = NULL,
$returnProperties = NULL,
function __construct($params = NULL,
$returnProperties = NULL,
$fields = NULL,
$includeContactIds = FALSE,
$strict = FALSE,
$includeContactIds = FALSE,
$strict = FALSE,
$mode = 1,
$skipPermission = FALSE,
$skipPermission = FALSE,
$searchDescendentGroups = TRUE,
$smartGroupCache = TRUE,
$smartGroupCache = TRUE,
$displayRelationshipType = NULL,
$operator = 'AND'
$operator = 'AND'
) {
$this->_params =& $params;
......@@ -64,7 +64,7 @@ class CRM_Grant_BAO_PaymentSearch {
}
if (empty($returnProperties)) {
$this->_returnProperties = self::defaultReturnProperties($mode);
}
}
else {
$this->_returnProperties =& $returnProperties;
}
......@@ -75,7 +75,7 @@ class CRM_Grant_BAO_PaymentSearch {
$this->_smartGroupCache = $smartGroupCache;
$this->_displayRelationshipType = $displayRelationshipType;
$this->setOperator($operator);
if ($fields) {
$this->_fields =& $fields;
$this->_search = FALSE;
......@@ -88,11 +88,11 @@ class CRM_Grant_BAO_PaymentSearch {
}
function initialize() {
$this->_select = array();
$this->_element = array();
$this->_select = array();
$this->_element = array();
$this->_tables = array();
$this->_whereTables = array();
$this->_where = array();
$this->_where = array();
$this->_qill = array();
$this->_options = array();
$this->_cfIDs = array();
......@@ -100,7 +100,7 @@ class CRM_Grant_BAO_PaymentSearch {
$this->_having = array();
$this->_customQuery = NULL;
$this->select();
$this->select();
$this->element();
if (!empty($this->_params)) {
$this->buildParamsLookup();
......@@ -108,7 +108,7 @@ class CRM_Grant_BAO_PaymentSearch {
$this->_whereClause = $this->whereClause();
$this->_tables = array('civicrm_payment' => 1);
$this->_whereTables = array('civicrm_payment' => 1);
$this->_fromClause = "FROM civicrm_payment";
$this->_simpleFromClause = "FROM civicrm_payment";
}
......@@ -126,19 +126,19 @@ class CRM_Grant_BAO_PaymentSearch {
}
$this->_cfIDs[$cfID][] = $value;
}
if (!array_key_exists($value[0], $this->_paramLookup)) {
$this->_paramLookup[$value[0]] = array();
}
$this->_paramLookup[$value[0]][] = $value;
}
}
function whereClause() {
$this->_where[0] = array();
$this->_qill[0] = array();
$config = CRM_Core_Config::singleton();
if (!empty( $this->_params)) {
foreach (array_keys($this->_params) as $id) {
......@@ -157,10 +157,10 @@ class CRM_Grant_BAO_PaymentSearch {
}
$this->_qill = CRM_Utils_Array::crmArrayMerge($this->_qill , $this->_customQuery->_qill);
}
$clauses = array();
$andClauses = array();
$validClauses = 0;
if (!empty($this->_where)) {
foreach ($this->_where as $grouping => $values) {
......@@ -169,22 +169,22 @@ class CRM_Grant_BAO_PaymentSearch {
$validClauses++;
}
}
if (!empty($this->_where[0])) {
$andClauses[] = ' ( ' . implode(" AND", $this->_where[0]) . ' ) ';
}
if (!empty($clauses)) {
$andClauses[] = ' ( ' . implode(' OR ', $clauses) . ' ) ';
}
if ($validClauses > 1) {
$this->_useDistinct = TRUE;
}
}
return implode(' AND ', $andClauses);
}
function whereClauseSingle(&$values) {
switch ($values[0]) {
case 'payment_status_id':
......@@ -200,7 +200,7 @@ class CRM_Grant_BAO_PaymentSearch {
$this->payment_number($values);
return;
case 'amount':
$this->amount($values);
$this->amount($values);
return;
case 'payment_created_date_low':
case 'payment_created_date_high':
......@@ -209,74 +209,74 @@ class CRM_Grant_BAO_PaymentSearch {
}
}
function payment_status(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
if (!$op) {
$op = '=';
}
$n = trim($value);
$n = trim($value);
if (strtolower($n) == 'odd') {
$this->_where[$grouping][] = " ( civicrm_payment.payment_status_id % 2 = 1 )";
$this->_qill[$grouping][] = ts( 'Payment Status Id is odd' );
}
}
else if (strtolower($n) == 'even') {
$this->_where[$grouping][] = " ( civicrm_payment.payment_status_id % 2 = 0 )";
$this->_qill[$grouping][] = ts('Payment Status Id is even');
}
}
else {
$value = strtolower(CRM_Core_DAO::escapeString($n));
$value = "'$value'";
$this->_where[$grouping][] = " ( civicrm_payment.payment_status_id $op $value )";
$paymentStatus = CRM_Core_OptionGroup::values('payment');
$n =$paymentStatus[$n];
$this->_qill[$grouping][] = ts('Payment Status') . " $op '$n'";
}
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
}
function payableName(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
$op = 'LIKE';
$newName = $name;
$name = trim($value);
$name = trim($value);
if (empty($name)) {
return;
}
$config = CRM_Core_Config::singleton();
$sub = array();
$sub = array();
//By default, $sub elements should be joined together with OR statements (don't change this variable).
$subGlue = ' OR ';
$strtolower = function_exists('mb_strtolower') ? 'mb_strtolower' : 'strtolower';
if (substr($name, 0, 1) == '"' &&
substr($name, -1, 1) == '"') {
//If name is encased in double quotes, the value should be taken to be the string in entirety and the
//If name is encased in double quotes, the value should be taken to be the string in entirety and the
$value = substr($name, 1, -1);
$value = $strtolower(CRM_Core_DAO::escapeString($value));
$wc = ( $newName == 'payable_to_name') ? 'LOWER(civicrm_payment.payable_to_name)' : 'LOWER(civicrm_payment.payable_to_name)';
$sub[] = " ( $wc = '%$value%' ) ";
}
}
else if (strpos($name, ',') !== FALSE) {
// if we have a comma in the string, search for the entire string
// if we have a comma in the string, search for the entire string
$value = $strtolower(CRM_Core_DAO::escapeString($name));
if ($wildcard) {
if ($config->includeWildCardInName) {
$value = "'%$value%'";
}
}
else {
$value = "'$value%'";
}
$op = 'LIKE';
}
}
else {
$value = "'%$value%'";
}
......@@ -284,11 +284,11 @@ class CRM_Grant_BAO_PaymentSearch {
$wc = ($op != 'LIKE') ? "LOWER(civicrm_payment.payable_to_name)" : "civicrm_payment.payable_to_name";
}
$sub[] = " ( $wc $op $value )";
}
}
else {
//Else, the string should be treated as a series of keywords to be matched with match ANY/ match ALL depending on Civi config settings (see CiviAdmin)
// The Civi configuration setting can be overridden if the string *starts* with the case insenstive strings 'AND:' or 'OR:'
// The Civi configuration setting can be overridden if the string *starts* with the case insenstive strings 'AND:' or 'OR:'
// TO THINK ABOUT: what happens when someone searches for the following "AND: 'a string in quotes'"? - probably nothing - it would make the AND OR variable reduntant because there is only one search string?
// Check to see if the $subGlue is overridden in the search text
......@@ -300,7 +300,7 @@ class CRM_Grant_BAO_PaymentSearch {
$name = substr($name, 3);
$subGlue = ' OR ';
}
$firstChar = substr($name, 0, 1);
$lastChar = substr($name, -1, 1);
$quotes = array("'", '"');
......@@ -309,27 +309,27 @@ class CRM_Grant_BAO_PaymentSearch {
$name = substr($name, 1);
$name = substr($name, 0, -1);
$pieces = array($name);
}
}
else {
$pieces = explode(' ', $name);
}
foreach ($pieces as $piece) {
foreach ($pieces as $piece) {
$value = $strtolower(CRM_Core_DAO::escapeString(trim($piece)));
if (strlen($value)) {
// Added If as a sanitization - without it, when you do an OR search, any string with
// double spaces (i.e. " ") or that has a space after the keyword (e.g. "OR: ") will
// return all contacts because it will include a condition similar to "OR contact
// name LIKE '%'". It might be better to replace this with array_filter.
// name LIKE '%'". It might be better to replace this with array_filter.
$fieldsub = array();
if ($wildcard) {
if ($config->includeWildCardInName) {
$value = "'%$value%'";
}
}
else {
$value = "'$value%'";
}
$op = 'LIKE';
}
}
else {
$value = "'%$value%'";
}
......@@ -341,29 +341,29 @@ class CRM_Grant_BAO_PaymentSearch {
// I seperated the glueing in two. The first stage should always be OR because we are searching for matches in *ANY* of these fields
}
}
}
}
$sub = ' ( ' . implode($subGlue, $sub) . ' ) ';
$this->_where[$grouping][] = $sub;
$this->_qill[$grouping][] = ts('Payee Name like') . " - '$name'";
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
}
function paymentDates($values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
if (($name == 'payment_created_date_low') || ($name == 'payment_created_date_high')) {
$this->dateQueryBuilder(
$this->dateQueryBuilder(
$values,
'civicrm_payment',
'payment_created_date',
'payment_created_date',
ts('Date')
'civicrm_payment',
'payment_created_date',
'payment_created_date',
ts('Date')
);
}
}
}
function &getWhereValues($name, $grouping) {
$result = NULL;
foreach ($this->_params as $id => $values) {
......@@ -374,13 +374,13 @@ class CRM_Grant_BAO_PaymentSearch {
return $result;
}
function dateQueryBuilder(
function dateQueryBuilder(
&$values,
$tableName,
$fieldName,
$dbFieldName,
$tableName,
$fieldName,
$dbFieldName,
$fieldTitle,
$appendTimeStamp = TRUE
$appendTimeStamp = TRUE
) {
list($name, $op, $value, $grouping, $wildcard) = $values;
......@@ -416,7 +416,7 @@ class CRM_Grant_BAO_PaymentSearch {
$secondDate = CRM_Utils_Date::processDate($secondValue);
}
}
}
else if ($name == $fieldName . '_high') {
$firstOP = '<=';
$firstPhrase = 'less than or equal to';
......@@ -455,11 +455,11 @@ class CRM_Grant_BAO_PaymentSearch {
( {$tableName}.{$dbFieldName} $firstOP '$firstDate' ) AND
( {$tableName}.{$dbFieldName} $secondOP '$secondDate' )
";
$this->_qill[$grouping][] =
$this->_qill[$grouping][] =
"$fieldTitle - $firstPhrase \"$firstDateFormat\" " .
ts('AND') .
" $secondPhrase \"$secondDateFormat\"";
}
}
else {
$this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $firstOP '$firstDate'";
$this->_qill[$grouping][] = "$fieldTitle - $firstPhrase \"$firstDateFormat\"";
......@@ -477,7 +477,7 @@ class CRM_Grant_BAO_PaymentSearch {
}
$format = CRM_Utils_Date::customFormat($date);
if ($date) {
$this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op '$date'";
if ($tableName == 'civicrm_log' &&
......@@ -487,7 +487,7 @@ class CRM_Grant_BAO_PaymentSearch {
$addedDateQuery = 'select id from civicrm_log group by entity_id order by id';
$this->_where[$grouping][] = "civicrm_log.id IN ( {$addedDateQuery} )";
}
}
}
else {
$this->_where[$grouping][] = "{$tableName}.{$dbFieldName} $op";
}
......@@ -498,70 +498,70 @@ class CRM_Grant_BAO_PaymentSearch {
function amount(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
if (!$op) {
$op = '=';
}
$n = trim($value);
$n = trim($value);
if (strtolower($n) == 'odd') {
$this->_where[$grouping][] = " ( civicrm_payment.amount % 2 = 1 )";
$this->_qill[$grouping][] = ts('Payment Amount is odd');
}
}
elseif (strtolower($n) == 'even') {
$this->_where[$grouping][] = " ( civicrm_payment.amount % 2 = 0 )";
$this->_qill[$grouping][] = ts('Payment Amount is even');
}
}
else {
$value = strtolower(CRM_Core_DAO::escapeString($n));
$value = "'$value'";
$this->_where[$grouping][] = " ( civicrm_payment.amount $op $value )";
$this->_qill[$grouping][] = ts( 'Payment Amount' ) . " $op '$n'";
}
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
}
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
}
function payment_number(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;
if (!$op) {
$op = '=';
}
$n = trim($value);
$n = trim($value);
if (strtolower($n) == 'odd') {
$this->_where[$grouping][] = " ( civicrm_payment.payment_number % 2 = 1 )";
$this->_qill[$grouping][] = ts( 'Payment Batch Number is odd' );
}
}
elseif (strtolower($n) == 'even') {
$this->_where[$grouping][] = " ( civicrm_payment.payment_number % 2 = 0 )";
$this->_qill[$grouping][] = ts('Payment Batch Number is even');
}
}
else {
$value = strtolower(CRM_Core_DAO::escapeString($n));
$value = "'$value'";
$this->_where[$grouping][] = " ( civicrm_payment.payment_number $op $value )";
$this->_qill[$grouping][] = ts('Payment Number') . " $op '$n'";
$this->_qill[$grouping][] = ts('Check Number') . " $op '$n'";
}
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
$this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1;
}
function payment_batch_number(&$values) {
list($name, $op, $value, $grouping, $wildcard) = $values;