diff --git a/CRM/Grant/BAO/GrantPayment.php b/CRM/Grant/BAO/GrantPayment.php index e5764bae7aceac23e333d9f76161971cf0003eb5..073f36267ecb81ba0053ecbb598a1fc3a498b0d5 100755 --- a/CRM/Grant/BAO/GrantPayment.php +++ b/CRM/Grant/BAO/GrantPayment.php @@ -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; diff --git a/CRM/Grant/BAO/GrantProgram.php b/CRM/Grant/BAO/GrantProgram.php index 2f04a413f3a4d3f004a69bbf8033aa025849a20b..18a1022a4bde1d244396247fc932fb2cd5002706 100755 --- a/CRM/Grant/BAO/GrantProgram.php +++ b/CRM/Grant/BAO/GrantProgram.php @@ -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; + } + } diff --git a/CRM/Grant/BAO/PaymentSearch.php b/CRM/Grant/BAO/PaymentSearch.php index edbb3454c7a90729ccf6ffef13ea71663ba952e2..09fa7b9ee4327412f4e4e0eeef2e873bbf0abd8f 100755 --- a/CRM/Grant/BAO/PaymentSearch.php +++ b/CRM/Grant/BAO/PaymentSearch.php @@ -1,4 +1,4 @@ -_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; - + if (!$op) { $op = '='; } - $n = trim($value); + $n = trim($value); if (strtolower($n) == 'odd') { $this->_where[$grouping][] = " ( civicrm_payment.payment_batch_number % 2 = 1 )"; $this->_qill[$grouping][] = ts('Payment Batch Number is odd'); - } + } elseif (strtolower($n) == 'even') { $this->_where[$grouping][] = " ( civicrm_payment.payment_batch_number % 2 = 0 )"; $this->_qill[$grouping][] = ts('Payment Batch Number is even'); - } + } else { $value = strtolower(CRM_Core_DAO::escapeString($n)); $value = "'$value'"; @@ -570,32 +570,32 @@ class CRM_Grant_BAO_PaymentSearch { $this->_qill[$grouping][] = ts('Payment Batch Number') . " $op '$n'"; } - $this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1; + $this->_tables['civicrm_payment'] = $this->_whereTables['civicrm_payment'] = 1; } function grantID(&$values) { $this->_where[$grouping][] = " ( civicrm_entity_payment.entity_id = $values )"; - $this->_tables['civicrm_entity_payment'] = $this->_whereTables['civicrm_entity_payment'] = 1; + $this->_tables['civicrm_entity_payment'] = $this->_whereTables['civicrm_entity_payment'] = 1; } - /** - * build select for CiviGrant - * - * @return void - * @access public + /** + * build select for CiviGrant + * + * @return void + * @access public */ - function select() { - $this->_select['id'] = 'civicrm_payment.id as id'; - $this->_select['payable_to_name'] = 'civicrm_payment.payable_to_name as payable_to_name'; - $this->_select['payment_batch_number'] = 'civicrm_payment.payment_batch_number as payment_batch_number'; - $this->_select['payment_number'] = 'civicrm_payment.payment_number as payment_number'; - $this->_select['payment_status_id'] = 'civicrm_payment.payment_status_id as payment_status_id'; - $this->_select['payment_created_date'] = 'civicrm_payment.payment_created_date as payment_created_date'; - $this->_select['amount'] = 'civicrm_payment.amount as amount'; + 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'; return $this->_select; } - - function element() { + + function element() { $this->_element['id'] = 1; $this->_element['payable_to_name'] = 1; $this->_element['payment_batch_number'] = 1; @@ -606,14 +606,14 @@ class CRM_Grant_BAO_PaymentSearch { return $this->_select; return $this->_element; } - - /** + + /** * Given a list of conditions in params generate the required * where clause - * - * @return void - * @access public - */ + * + * @return void + * @access public + */ static function where(&$query) { foreach (array_keys($query->_params) as $id) { if (substr($query->_params[$id][0], 0, 6) == 'grant_') { @@ -621,7 +621,7 @@ class CRM_Grant_BAO_PaymentSearch { } } } - + /** * getter for the qill object * @@ -631,9 +631,9 @@ class CRM_Grant_BAO_PaymentSearch { function qill() { return (isset($this->_qill)) ? $this->_qill : ""; } - + static function defaultReturnProperties($mode, - $includeCustomFields = TRUE + $includeCustomFields = TRUE ) { $properties = NULL; if ($mode & CRM_Grant_BAO_PaymentSearch::MODE_GRANT_PAYMENT) { @@ -653,41 +653,50 @@ class CRM_Grant_BAO_PaymentSearch { /** * add all the elements shared between grant search and advanaced search * - * @access public + * @access public * @return void * @static - */ - + */ + static function addShowHide(&$showHide) { $showHide->addHide('grantForm'); $showHide->addShow('grantForm_show'); } - + static function searchAction(&$row, $id) { } static function tableNames(&$tables) { } - + + function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE) { $select = 'SELECT '; if (!$count) { if (! empty($this->_select)) { $select .= implode(', ', $this->_select); } - } + } else { $select .= "count( DISTINCT ".$this->_distinctComponentClause." ) "; } - $from = ''; - if (!empty($this->_fromClause)) { - $from = $this->_fromClause; - } + $from = " FROM civicrm_payment p + LEFT JOIN civicrm_financial_trxn ft ON ft.id = p.financial_trxn_id + LEFT JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_grant' + LEFT JOIN civicrm_grant g ON g.id = eft.entity_id + LEFT JOIN civicrm_entity_financial_trxn eft1 ON eft1.financial_trxn_id = ft.id AND eft1.entity_table = 'civicrm_financial_item' + LEFT JOIN civicrm_financial_item fi ON fi.id = eft1.entity_id + LEFT JOIN civicrm_grant_program gp ON gp.id = g.grant_program_id + LEFT JOIN civicrm_entity_batch eb ON eb.entity_id = ft.id AND eb.entity_table = 'civicrm_financial_trxn' + LEFT JOIN civicrm_batch b ON eb.batch_id = b.id + LEFT JOIN civicrm_contact cc ON cc.id = fi.contact_id + "; + $where = ''; if (!empty($this->_whereClause)) { $where = "WHERE {$this->_whereClause}"; } - + $having = ''; if (!empty($this->_having)) { foreach ($this->_having as $havingsets) { @@ -699,34 +708,34 @@ class CRM_Grant_BAO_PaymentSearch { } return array($select, $from, $where, $having); } - - function searchQuery( - $offset = 0, - $rowCount = 0, - $sort = NULL, - $count = FALSE, + + function searchQuery( + $offset = 0, + $rowCount = 0, + $sort = NULL, + $count = FALSE, $includeContactIds = FALSE, - $sortByChar = FALSE, + $sortByChar = FALSE, $groupContacts = FALSE, $returnQuery = FALSE, - $additionalWhereClause = NULL, + $additionalWhereClause = NULL, $sortOrder = NULL, $additionalFromClause = NULL, - $skipOrderAndLimit = FALSE + $skipOrderAndLimit = FALSE ) { - + list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts); $order = $orderBy = $limit = ''; if (!$count) { - + $config = CRM_Core_Config::singleton(); if ($config->includeOrderByClause || isset($this->_distinctComponentClause)) { - + if ($sort) { if (is_string($sort)) { $orderBy = $sort; - } + } else { $orderBy = trim($sort->orderBy()); } @@ -736,36 +745,40 @@ class CRM_Grant_BAO_PaymentSearch { $order .= " $sortOrder"; } } - } - elseif ($sortByChar) { - $order = " ORDER BY UPPER(LEFT(civicrm_payment.payable_to_name, 1)) asc"; - } + } + elseif ($sortByChar) { + $order = " ORDER BY UPPER(LEFT(cc.display_name, 1)) asc"; + } else { - $order = " ORDER BY civicrm_payment.payable_to_name asc, civicrm_payment.id"; + $order = " ORDER BY cc.display_name asc, ft.id"; } } if ($rowCount > 0 && $offset >= 0) { $limit = " LIMIT $offset, $rowCount "; - + if (isset($this->_distinctComponentClause)) { $limitSelect = "SELECT {$this->_distinctComponentClause}"; - } + } else { $limitSelect = ($this->_useDistinct) ? - 'SELECT DISTINCT(civicrm_payment.id) as id' : - 'SELECT civicrm_payment.id as id'; + 'SELECT DISTINCT(ft.id) as id' : + 'SELECT ft.id as id'; } } - $groupBy = 'GROUP BY civicrm_payment.id'; + $groupBy = 'GROUP BY ft.id'; $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) { $ids = array(); @@ -804,14 +817,14 @@ class CRM_Grant_BAO_PaymentSearch { } if (!$skipWhere) { - $skipWhere = array( - 'task', - 'radio_ts', + $skipWhere = array( + 'task', + 'radio_ts', 'uf_group_id', - 'component_mode', - 'qfKey', + 'component_mode', + 'qfKey', 'operator', - 'display_relationship_type' + 'display_relationship_type' ); } @@ -828,10 +841,10 @@ class CRM_Grant_BAO_PaymentSearch { if (!$useEquals && in_array($id, $likeNames)) { $result = array($id, 'LIKE', $values, 0, 1); - } + } elseif (is_string($values) && strpos($values, '%') !== FALSE) { $result = array($id, 'LIKE', $values, 0, 0); - } + } elseif ($id == 'group') { if (is_array($values)) { foreach ($values as $groupIds => $val) { @@ -843,7 +856,7 @@ class CRM_Grant_BAO_PaymentSearch { } } } - } + } else { $groupIds = explode(',', $values); unset($values); @@ -852,7 +865,7 @@ class CRM_Grant_BAO_PaymentSearch { } } $result = array($id, 'IN', $values, 0, 0); - } + } elseif ($id == 'contact_tags' || $id == 'tag') { if (!is_array($values)) { $tagIds = explode(',', $values); @@ -862,7 +875,7 @@ class CRM_Grant_BAO_PaymentSearch { } } $result = array($id, 'IN', $values, 0, 0); - } + } else { $result = array($id, '=', $values, 0, $wildcard); } @@ -876,4 +889,4 @@ class CRM_Grant_BAO_PaymentSearch { } $this->_operator = $operator; } -} \ No newline at end of file +} diff --git a/CRM/Grant/DAO/GrantPayment.php b/CRM/Grant/DAO/GrantPayment.php index 8640274aa6c4d5101dc6231485f7184ec813b6f6..55fa146e0abb3d1e7108f3e8d68aad9d75ab5521 100644 --- a/CRM/Grant/DAO/GrantPayment.php +++ b/CRM/Grant/DAO/GrantPayment.php @@ -86,30 +86,7 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO * @var int unsigned */ public $id; - /** - * Payment Batch Nnumber - * - * @var int unsigned - */ - public $payment_batch_number; - /** - * Payment Number - * - * @var int unsigned - */ - public $payment_number; - /** - * Financial Type ID - * - * @var int unsigned - */ - public $financial_type_id; - /** - * Contact ID - * - * @var int unsigned - */ - public $contact_id; + public $financial_trxn_id; /** * Payment Created Date. * @@ -122,35 +99,6 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO * @var date */ public $payment_date; - /** - * Payable To Name. - * - * @var string - */ - public $payable_to_name; - /** - * Payable To Address. - * - * @var string - */ - public $payable_to_address; - /** - * Requested grant amount, in default currency. - * - * @var float - */ - public $amount; - /** - * 3 character string, value from config setting or input via user. - * - * @var string - */ - public $currency; - /** - * Payment Reason. - * - * @var string - */ public $payment_reason; /** * Payment Status ID @@ -196,46 +144,13 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO 'dataPattern' => '', 'export' => true, ) , - 'payment_batch_number' => array( - 'name' => 'payment_batch_number', + 'financial_trxn_id' => array( + 'name' => 'financial_trxn_id', 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Payment Batch Nnumber') , + 'title' => ts('Financial Trxn ID') , 'required' => true, 'import' => true, - 'where' => 'civicrm_payment.payment_batch_number', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'payment_number' => array( - 'name' => 'payment_number', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Payment Number') , - 'required' => true, - 'import' => true, - 'where' => 'civicrm_payment.payment_number', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'financial_type_id' => array( - 'name' => 'financial_type_id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Financial Type ID') , - 'required' => true, - 'import' => true, - 'where' => 'civicrm_payment.financial_type_id', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'contact_id' => array( - 'name' => 'contact_id', - 'type' => CRM_Utils_Type::T_INT, - 'title' => ts('Contact ID') , - 'required' => true, - 'import' => true, - 'where' => 'civicrm_payment.contact_id', + 'where' => 'civicrm_payment.financial_trxn_id', 'headerPattern' => '', 'dataPattern' => '', 'export' => true, @@ -250,63 +165,6 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO 'dataPattern' => '', 'export' => true, ) , - 'payment_date' => array( - 'name' => 'payment_date', - 'type' => CRM_Utils_Type::T_DATE, - 'title' => ts('Payment Date') , - 'import' => true, - 'where' => 'civicrm_payment.payment_date', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'payable_to_name' => array( - 'name' => 'payable_to_name', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Payable To Name') , - 'maxlength' => 255, - 'size' => CRM_Utils_Type::HUGE, - 'import' => true, - 'where' => 'civicrm_payment.payable_to_name', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'payable_to_address' => array( - 'name' => 'payable_to_address', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Payable To Address') , - 'maxlength' => 255, - 'size' => CRM_Utils_Type::HUGE, - 'import' => true, - 'where' => 'civicrm_payment.payable_to_address', - 'headerPattern' => '', - 'dataPattern' => '', - 'export' => true, - ) , - 'amount' => array( - 'name' => 'amount', - 'type' => CRM_Utils_Type::T_MONEY, - 'title' => ts('Amount') , - 'required' => true, - 'import' => true, - 'where' => 'civicrm_payment.amount', - 'headerPattern' => '', - 'dataPattern' => '/^\d+(\.\d{2})?$/', - 'export' => true, - ) , - 'currency' => array( - 'name' => 'currency', - 'type' => CRM_Utils_Type::T_STRING, - 'title' => ts('Currency') , - 'maxlength' => 3, - 'size' => CRM_Utils_Type::FOUR, - 'import' => true, - 'where' => 'civicrm_payment.currency', - 'headerPattern' => '/cur(rency)?/i', - 'dataPattern' => '/^[A-Z]{3}$/i', - 'export' => true, - ) , 'payment_reason' => array( 'name' => 'payment_reason', 'type' => CRM_Utils_Type::T_STRING, @@ -323,6 +181,9 @@ class CRM_Grant_DAO_GrantPayment extends CRM_Core_DAO 'name' => 'payment_status_id', 'type' => CRM_Utils_Type::T_INT, 'title' => ts('Payment Status') , + 'pseudoconstant' => [ + 'optionGroupName' => 'grant_payment_status', + ] ) , 'replaces_payment_id' => array( 'name' => 'replaces_payment_id', diff --git a/CRM/Grant/Form/Task/GrantPayment.php b/CRM/Grant/Form/Task/GrantPayment.php index 0b627ad5754d90ab93482b304fe50b866fd51f97..74e8d09a5f684ab3a6cf5dac6a6a4c7e3e6cd23b 100755 --- a/CRM/Grant/Form/Task/GrantPayment.php +++ b/CRM/Grant/Form/Task/GrantPayment.php @@ -38,14 +38,12 @@ * This class generates form components for Payments * */ -class CRM_Grant_Form_Task_GrantPayment extends CRM_Core_Form -{ +class CRM_Grant_Form_Task_GrantPayment extends CRM_Core_Form { protected $_id = null; protected $_fields = null; - function preProcess( ) { - parent::preProcess( ); - $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this ); + function preProcess() { + $this->_action = CRM_Utils_Request::retrieve('action', 'String', $this ); $this->_prid = CRM_Utils_Request::retrieve('prid', 'Positive', $this ); if ( $this->_prid ) { $session = CRM_Core_Session::singleton(); @@ -54,104 +52,308 @@ class CRM_Grant_Form_Task_GrantPayment extends CRM_Core_Form } } - function setDefaultValues( ) - { - $defaults = array(); - $paymentNumbers = CRM_Grant_BAO_GrantPayment::getMaxPayementBatchNumber( ); - $defaults['payment_date'] = strftime("%m/%d/%Y", strtotime( date('Y/m/d') )); - $defaults['payment_number'] = $paymentNumbers['payment_number'] + 1; - $defaults['payment_batch_number'] = $paymentNumbers['payment_batch_number'] + 1; - - return $defaults; + /** + * Get payment fields + */ + public function getPaymentFields() { + return array( + 'check_number' => array( + 'is_required' => TRUE, + 'add_field' => TRUE, + ), + 'trxn_id' => array( + 'add_field' => TRUE, + 'is_required' => FALSE, + ), + 'description' => array( + 'htmlType' => 'textarea', + 'name' => 'description', + 'title' => ts('Payment reason'), + 'is_required' => FALSE, + 'attributes' => [], + ), + 'trxn_date' => array( + 'htmlType' => 'datepicker', + 'name' => 'trxn_date', + 'title' => ts('Payment date to appear on cheques'), + 'is_required' => TRUE, + 'attributes' => array( + 'date' => 'yyyy-mm-dd', + 'time' => 24, + 'context' => 'create', + 'action' => 'create', + ), + ), + 'contribution_batch_id' => [ + 'htmlType' => 'select', + 'name' => 'contribution_batch_id', + 'title' => ts('Assign to Batch'), + 'attributes' => ['' => ts('None')] + CRM_Contribute_PseudoConstant::batch(), + 'is_required' => TRUE, + ], + ); } + /** * Function to build the form * * @return None * @access public */ - public function buildQuickForm( $check = false ) - { - parent::buildQuickForm( ); - if ( $this->_action & CRM_Core_Action::DELETE ) { - - $this->addButtons( array( - array ( 'type' => 'next', - 'name' => ts('Delete'), - 'isDefault' => true ), - - array ( 'type' => 'cancel', - 'name' => ts('Cancel') ), - ) - ); + public function buildQuickForm(){ + if ($this->_action & CRM_Core_Action::DELETE) { + $this->addButtons([ + [ + 'type' => 'next', + 'name' => ts('Delete'), + 'isDefault' => TRUE, + ], + [ + 'type' => 'cancel', + 'name' => ts('Cancel'), + ], + ]); return; } - $this->applyFilter('__ALL__','trim'); - $attributes = CRM_Core_DAO::getAttribute( 'CRM_Grant_DAO_GrantProgram' ); + $paymentFields = $this->getPaymentFields(); + $this->assign('paymentFields', $paymentFields); + foreach ($paymentFields as $name => $paymentField) { + if (!empty($paymentField['add_field'])) { + $attributes = array( + 'entity' => 'FinancialTrxn', + 'name' => $name, + 'context' => 'create', + 'action' => 'create', + ); + $this->addField($name, $attributes, $paymentField['is_required']); + } + else { + $this->add($paymentField['htmlType'], + $name, + $paymentField['title'], + $paymentField['attributes'], + $paymentField['is_required'] + ); + } + } - $this->_contributionTypes = CRM_Grant_BAO_GrantProgram::contributionTypes(); - $this->add('select', 'financial_type_id', ts( 'From account' ), - array( '' => ts( '- select -' ) ) + $this->_contributionTypes , true); + $buttonName = $this->_prid ? 'Reprint Checks and CSV Export' : 'Create Checks and CSV Export'; + $this->addButtons([ + [ + 'type' => 'upload', + 'name' => ts($buttonName), + 'isDefault' => TRUE, + ], + [ + 'type' => 'cancel', + 'name' => ts('Cancel'), + ], + ]); + } - $this->add( 'text', 'payment_batch_number', ts( 'Payment Batch number' ), - $attributes['label'], true ); + public function postProcess() { + $values = $this->controller->exportValues($this->_name); + $approvedGrants = $this->get('approvedGrants'); + $approvedGrantIDs = array_keys($approvedGrants); + $grantPrograms = CRM_Grant_BAO_GrantProgram::getGrantPrograms(); + $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); + $financialItemStatus = CRM_Core_PseudoConstant::accountOptionValues('financial_item_status'); + $checkID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'payment_instrument_id', 'Check'); + $mailParams = $printedRows = $files = []; + $totalAmount = $counter = 0; + $maxLimit = CRM_Utils_Array::value('Maximum number of checks per pdf file', CRM_Core_OptionGroup::values('grant_thresholds', TRUE)); + $config = CRM_Core_Config::singleton(); + $entityFileDAO = new CRM_Core_DAO_EntityFile(); - $this->add( 'text', 'payment_number', ts( 'Starting cheque number' ), - $attributes['label'], true ); + $dao = CRM_Core_DAO::executeQuery(sprintf(" + SELECT ft.id as ft_id, g.id as grant_id, fi.id as fi_id, g.financial_type_id, ft.to_financial_account_id, fi.currency, gp.is_auto_email, ft.total_amount, fi.contact_id, g.grant_program_id + FROM civicrm_entity_financial_trxn eft + INNER JOIN civicrm_financial_trxn ft ON ft.id = eft.financial_trxn_id AND eft.entity_table = 'civicrm_grant' + INNER JOIN civicrm_grant g ON g.id = eft.entity_id + INNER JOIN civicrm_entity_financial_trxn eft1 ON eft1.financial_trxn_id = ft.id AND eft1.entity_table = 'civicrm_financial_item' + INNER JOIN civicrm_financial_item fi ON fi.id = eft1.entity_id + INNER JOIN civicrm_grant_program gp ON gp.id = g.grant_program_id + WHERE g.id IN (%s) GROUP BY ft.id ", implode(', ', $approvedGrantIDs))); + while($dao->fetch()) { + $totalAmount += $dao->total_amount; + $grantID = $dao->grant_id; + $financialTrxnParams = [ + 'from_financial_account_id' => $dao->to_financial_account_id, + 'to_financial_account_id' => CRM_Contribute_PseudoConstant::getRelationalFinancialAccount($dao->financial_type_id, 'Asset Account is') ?: CRM_Grant_BAO_GrantProgram::getAssetFinancialAccountID(), + 'trxn_date' => CRM_Utils_Array::value('trxn_date', $values, date('YmdHis')), + 'trxn_id' => CRM_Utils_Array::value('trxn_id', $values), + 'total_amount' => $dao->total_amount, + 'currency' => $dao->currency, + 'check_number' => $values['check_number'], + 'payment_instrument_id' => $checkID, + 'status_id' => array_search('Completed', $contributionStatuses), + 'entity_table' => 'civicrm_grant', + 'entity_id' => $grantID, + ]; + $trxnID = civicrm_api3('FinancialTrxn', 'create', $financialTrxnParams)['id']; + + $description = CRM_Utils_Array::value('description', $values, $grantPrograms[$dao->grant_program_id]); + $financialParams = ['description' => $description, 'status_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_FinancialItem', 'status_id', 'Paid'), 'amount' => $dao->total_amount]; + $ids = ['id' => $dao->fi_id]; + $trxnIDs = ['id' => $trxnID]; + CRM_Financial_BAO_FinancialItem::create($financialParams, $ids, $trxnIDs); + + civicrm_api3('EntityBatch', 'create', [ + 'entity_table' => 'civicrm_financial_trxn', + 'entity_id' => $trxnID, + 'batch_id' => $values['contribution_batch_id'], + ]); + + if ($dao->is_auto_email) { + $mailParams = [ + 'is_auto_email' => TRUE, + 'amount_total' => $dao->total_amount, + 'grant_type_id' => $approvedGrants[$grantID]['grant_type_id'], + 'grant_program_id' => $approvedGrants[$grantID]['grant_program_id'], + 'contact_id' => $dao->contact_id, + 'tplParams' => ['grant' => ['grant_programs' => $grantPrograms[$dao->grant_program_id]]], + ]; + CRM_Grant_BAO_GrantProgram::sendMail($dao->contact_id, $mailParams, 'Paid', $grantID, 'Approved for Payment'); + } - $this->addDate( 'payment_date', ts('Payment date to appear on cheques'), false, array( 'formatType' => 'custom') ); - $buttonName = "Create Checks and CSV Export"; - if ( $this->_prid ) { - $buttonName = "Reprint Checks and CSV Export"; + $grantPaymentRecord = [ + 'financial_trxn_id' => $trxnID, + 'payment_created_date' => date('Y-m-d'), + 'payment_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Grant_DAO_GrantPayment', 'payment_status_id', 'Printed'), + 'payment_reason' => $description, + ]; + CRM_Grant_BAO_GrantPayment::add($grantPaymentRecord); + + $printedRows[$grantID] = [ + 'contact_id' => $dao->contact_id, + 'financial_type_id' => $dao->financial_type_id, + 'payment_batch_number' => $values['contribution_batch_id'], + 'payment_number' => $values['check_number'], + 'payment_date' => date("Y-m-d", strtotime($values['trxn_date'])), + 'payment_created_date' => $grantPaymentRecord['payment_created_date'], + // TODO remove CRM_Grant_BAO_GrantProgram::getDisplayName + 'payable_to_name' => CRM_Contact_BAO_Contact::displayName($dao->contact_id), + 'payable_to_address' => CRM_Utils_Array::value('address', CRM_Grant_BAO_GrantProgram::getAddress($dao->contact_id, NULL, TRUE)), + 'amount' => $dao->total_amount, + 'curreny' => $dao->currency, + 'payment_reason' => CRM_Utils_Array::value('description', $values, $grantPrograms[$dao->grant_program_id]), + 'payment_status_id' => array_search('Completed', $contributionStatuses), + 'replaces_payment_id' => NULL, + 'payment_details' => sprintf( + '%s
{$form.financial_type_id.label} | -{$form.financial_type_id.html} | + {foreach from=$paymentFields key=fieldName item=paymentField} + {assign var='name' value=$fieldName} +
{$form.$name.label} | +{$form.$name.html} |
{$form.payment_batch_number.label} | -{$form.payment_batch_number.html} | -
{$form.number_checks.label} | -{$form.number_checks.html} | -
{$form.payment_number.label} | -{$form.payment_number.html} | -
{$form.payment_date.label} | -- {if $hideCalendar neq true} - {include file="CRM/common/jcalendar.tpl" elementName=payment_date} - {else} - {$form.payment_date.html|crmDate} - {/if} - | -
{$form.download_file.label} | -{$form.download_file.html} | -