$maxLimit=$grantThresholds['Maximum number of checks per pdf file'];
if($this->_prid){
$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 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->_prid.")";
$countQuery="SELECT COUNT(cp.id) as ids 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->_prid.")";
$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 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->_prid.")";
$countQuery="SELECT COUNT(cp.id) as ids 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->_prid.")";
}else{
$query="SELECT id as grant_id, amount_granted as total_amount, currency, grant_program_id, grant_type_id, contact_id as id FROM civicrm_grant WHERE id IN (".implode(', ',array_keys($this->_approvedGrants)).")";
$query="SELECT id as grant_id, amount_granted as total_amount, currency, grant_program_id, ".
"application_received_date, grant_type_id, contact_id as id FROM civicrm_grant ".
"WHERE id IN (".implode(', ',array_keys($this->_approvedGrants)).")";
$countQuery="SELECT COUNT(id) as grant_id FROM civicrm_grant WHERE id IN (".implode(', ',array_keys($this->_approvedGrants)).")";
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']}");
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']}");