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,10 +316,8 @@ 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.'));
}
}
$subject = $grantDao->subject;
$html = $grantDao->html;
$text = $grantDao->text;
......
......@@ -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;
}
}
......@@ -541,7 +541,7 @@ class CRM_Grant_BAO_PaymentSearch {
$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;
......@@ -585,13 +585,13 @@ class CRM_Grant_BAO_PaymentSearch {
* @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';
$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;
}
......@@ -669,6 +669,7 @@ class CRM_Grant_BAO_PaymentSearch {
static function tableNames(&$tables) {
}
function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALSE) {
$select = 'SELECT ';
if (!$count) {
......@@ -679,10 +680,18 @@ class CRM_Grant_BAO_PaymentSearch {
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}";
......@@ -738,10 +747,10 @@ class CRM_Grant_BAO_PaymentSearch {
}
}
elseif ($sortByChar) {
$order = " ORDER BY UPPER(LEFT(civicrm_payment.payable_to_name, 1)) asc";
$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";
}
}
......@@ -753,11 +762,11 @@ class CRM_Grant_BAO_PaymentSearch {
}
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";
}
......@@ -765,6 +774,10 @@ class CRM_Grant_BAO_PaymentSearch {
$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) {
......
......@@ -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',
......
This diff is collapsed.
......@@ -41,8 +41,7 @@ require_once 'CRM/Grant/Form/Task.php';
* participations. This class provides functionality for the actual
* deletion.
*/
class CRM_Grant_Form_Task_Pay extends CRM_Grant_Form_Task
{
class CRM_Grant_Form_Task_Pay extends CRM_Grant_Form_Task {
/**
* Are we operating in "single mode", i.e. deleting one
* specific participation?
......@@ -51,76 +50,39 @@ class CRM_Grant_Form_Task_Pay extends CRM_Grant_Form_Task
*/
protected $_single = false;
/**
* build all the data structures needed to build the form
*
* @return void
* @access public
*/
function preProcess( )
{
parent::preProcess( );
//check permission for delete.
if ( !CRM_Core_Permission::checkActionPermission( 'CiviGrant', CRM_Core_Action::PAY ) ) {
CRM_Core_Error::fatal( ts( 'You do not have permission to access this page' ) );
function preProcess() {
parent::preProcess();
if (!CRM_Core_Permission::check('create payments in CiviGrant')) {
CRM_Core_Error::fatal(ts( 'You do not have permission to access this page'));
}
}
$grantStatus = CRM_Core_OptionGroup::values('grant_status', TRUE, FALSE, FALSE, NULL, 'name');
$paidGrants = $approvedGrants = array();
CRM_Core_PseudoConstant::populate($paidGrants, 'CRM_Grant_DAO_Grant', true, 'status_id', false, " id in (".implode ( ', ' , $this->_grantIds ).") AND status_id = {$grantStatus['Paid']}");
CRM_Core_PseudoConstant::populate($approvedGrants, 'CRM_Grant_DAO_Grant', true, 'status_id', false, " id in (".implode ( ', ' , $this->_grantIds ).") AND status_id = {$grantStatus['Approved for Payment']}");
$this->_paidGrants = $paidGrants;
$this->_notApproved = count($this->_grantIds) - count( $this->_paidGrants ) - count( $approvedGrants );
function buildQuickForm() {
$message = [];
$paidGrantCount = civicrm_api3('Grant', 'getcount', [
'status_id' => 'Paid',
'id' => ['IN' => $this->_grantIds]
]);
$this->_approvedGrants = (array) civicrm_api3('Grant', 'get', [
'status_id' => 'Approved for Payment',
'id' => ['IN' => $this->_grantIds]
])['values'];
foreach ( $approvedGrants as $key => $value ) {
$grantProgram = new CRM_Grant_DAO_Grant( );
$grantArray = array( 'id' => $key );
$grantProgram->copyValues( $grantArray );
$grantProgram->find( true );
$currencyDetails[$grantProgram->contact_id][$grantProgram->currency] = $key;
}
//$this->_currency = $currencyDetails;
$curency = 0;
if ( !empty( $currencyDetails ) ) {
foreach ( $currencyDetails as $key => $value ) {
if ( count($value) > 1 ) {
foreach ( $value as $unsetKey => $unsetVal ) {
unset( $approvedGrants[$unsetVal] );
$curency++;
}
}
}
$this->_curency = $curency;
}
$this->_approvedGrants = $approvedGrants;
}
$notApproved = count($this->_grantIds) - $paidGrantCount - count($this->_approvedGrants);
/**
* Build the form
*
* @access public
* @return void
*/
function buildQuickForm( )
{
$message = "";
$currencyCount = count(array_flip(CRM_Utils_Array::collect('currency', $this->_approvedGrants)));
if (count($this->_approvedGrants)) {
if (count($this->_paidGrants)) {
$message = count( $this->_paidGrants ).' of the '.count($this->_grantIds).' selected grants have already been paid. ';
}
if ($this->_notApproved) {
$message .= $this->_notApproved.' of the '.count($this->_grantIds).' selected grants are not eligible. ';
if ($paidGrantCount) {
$message[] = sprintf("<br> %d of the %d selected grants have already been paid.", $paidGrantCount, count($this->_grantIds));
}
if ($this->_curency) {
$message .= $this->_curency.' of '.count($this->_grantIds).' grants have different currency of same user. ';
if ($notApproved) {
$message[] = sprintf("%d of the %d selected grants are not eligible.", $notApproved, count($this->_grantIds));
}
if (count( $this->_approvedGrants )) {
$message .= 'Would you like to proceed to paying the '.count( $this->_approvedGrants ).' eligible or approved for payment but unpaid grants?';
CRM_Core_Session::setStatus(ts($message), NULL, 'no-popup');
if ($currencyCount > 1) {
$message[] = sprintf("%d of the %d grants have different currency of same user.", $currencyCount, count($this->_grantIds));
}
$message[] = 'Would you like to proceed to paying the ' . count($this->_approvedGrants) . ' eligible or approved for payment but unpaid grants?';
CRM_Core_Session::setStatus(ts(implode('<br>', $message)), 'Payment Details', 'no-popup');
$this->addButtons( array(
array (
......@@ -147,15 +109,8 @@ class CRM_Grant_Form_Task_Pay extends CRM_Grant_Form_Task
}
}
/**
* process the form after the input has been submitted and validated
*
* @access public
* @return None
*/
public function postProcess( )
{
$this->set( 'approvedGrants', $this->_approvedGrants );
$this->controller->resetPage( 'GrantPayment' );
public function postProcess() {
$this->set('approvedGrants', $this->_approvedGrants);
$this->controller->resetPage('GrantPayment');
}
}
......@@ -64,16 +64,7 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask
}
}
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;
}
/**
* Build the form
*
......@@ -92,7 +83,7 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask
}
$selectedPayments = count($this->_grantPaymentIds);
foreach ( $this->_grantPaymentIds as $key => $paymentId ) {
$paymentDAO =& new CRM_Grant_DAO_GrantPayment();
$paymentDAO = new CRM_Grant_DAO_GrantPayment();
$paymentDAO->id = $paymentId;
$paymentDAO->find(true);
if( array_key_exists( $paymentDAO->payment_status_id, $paymentStatus ) ) {
......@@ -157,11 +148,11 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask
$totalAmount = 0;
foreach ( $this->_grantPaymentIds as $paymentId ) {
$paymentDAO =& new CRM_Grant_DAO_GrantPayment();
$paymentDAO = new CRM_Grant_DAO_GrantPayment();
$paymentDAO->id = $paymentId;
$paymentDAO->payment_status_id = CRM_Core_OptionGroup::getValue( 'grant_payment_status', 'Stopped', 'name' );
$paymentDAO->save();
$paymentDAO =& new CRM_Grant_DAO_GrantPayment();
$paymentDAO = new CRM_Grant_DAO_GrantPayment();
$paymentDAO->id = $paymentId;
$paymentDAO->find(true);
......@@ -183,18 +174,18 @@ class CRM_Grant_Form_Task_Reprint extends CRM_Grant_Form_PaymentTask
$newPaymentId = $result->id;
$entityDAO =& new CRM_Grant_DAO_EntityPayment();
$entityDAO = new CRM_Grant_DAO_EntityPayment();
$entityDAO->payment_id = $paymentId;
$entityDAO->find();
while( $entityDAO->fetch() ) {
$newEntityDAO =& new CRM_Grant_DAO_EntityPayment();
$newEntityDAO = new CRM_Grant_DAO_EntityPayment();
//$newEntityDAO->find( true );