From 5f5cb51efb306620a65a58bfb5dd12aec09d9830 Mon Sep 17 00:00:00 2001
From: Edsel <edsel.lopez@jmaconsulting.biz>
Date: Fri, 31 Jan 2014 15:49:24 +0530
Subject: [PATCH] GEP-16 Added default grant program to fix compatibility with
 grant application extension

---
 CRM/Grant/BAO/Query.php       |  5 ++++
 grantprograms.php             | 12 ++++++++--
 sql/grantprograms_install.sql | 45 +++++++++++++++++++++++------------
 3 files changed, 45 insertions(+), 17 deletions(-)

diff --git a/CRM/Grant/BAO/Query.php b/CRM/Grant/BAO/Query.php
index 59edb06..f430a18 100644
--- a/CRM/Grant/BAO/Query.php
+++ b/CRM/Grant/BAO/Query.php
@@ -193,6 +193,11 @@ class CRM_Grant_BAO_Query {
         $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
         return;
 
+      case 'grant_id':
+        $query->_where[$grouping][] = "civicrm_grant.id = $value";
+        $query->_tables['civicrm_grant'] = $query->_whereTables['civicrm_grant'] = 1;
+        return;
+
       case 'grant_type_id':
 
         $value = $strtolower(CRM_Core_DAO::escapeString(trim($value)));
diff --git a/grantprograms.php b/grantprograms.php
index 2c6d731..2c61956 100644
--- a/grantprograms.php
+++ b/grantprograms.php
@@ -90,6 +90,9 @@ function grantprograms_civicrm_managed(&$entities) {
  *
  */
 function grantprograms_civicrm_grantAssessment(&$params) {
+  if (!CRM_Utils_Array::value('grant_program_id', $params)) {
+    return;
+  }
   $grantProgramParams['id'] = $params['grant_program_id'];
   $grantProgram = CRM_Grant_BAO_GrantProgram::retrieve($grantProgramParams, CRM_Core_DAO::$_nullArray);
   if (!empty($grantProgram->grant_program_id)) {
@@ -657,7 +660,7 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
     $params = $config->_params;
     // added by JMA fixme in module
     $grantProgram  = new CRM_Grant_DAO_GrantProgram();
-    $grantProgram->id = $params['grant_program_id'];
+    $grantProgram->id = isset($params['grant_program_id']) ? $params['grant_program_id'] : NULL;
     $page = new CRM_Core_Page();
     if ($grantProgram->find(TRUE)) {
       $params['is_auto_email'] = $grantProgram->is_auto_email;
@@ -712,7 +715,12 @@ function grantprograms_civicrm_post($op, $objectName, $objectId, &$objectRef) {
       $grantStatuses = $grantStatus = CRM_Core_OptionGroup::values('grant_status');
       $grantPrograms = CRM_Grant_BAO_GrantProgram::getGrantPrograms();
       $grantTypes = CRM_Core_OptionGroup::values('grant_type');
-      $grantProgram = $grantPrograms[$params['grant_program_id']];
+      if (CRM_Utils_Array::value('grant_program_id', $params)) {
+        $grantProgram = $grantPrograms[$params['grant_program_id']];
+      }
+      else {
+        $grantProgram = '';
+      }
       $grantType = $grantTypes[$params['grant_type_id']];
       $grantStatus = $grantStatus[$params['status_id']];
       $grantIneligibleReasons = CRM_Core_OptionGroup::values('reason_grant_ineligible');
diff --git a/sql/grantprograms_install.sql b/sql/grantprograms_install.sql
index 8fcaf10..c80d856 100755
--- a/sql/grantprograms_install.sql
+++ b/sql/grantprograms_install.sql
@@ -98,18 +98,6 @@ ALTER TABLE `civicrm_grant_program`
   ADD CONSTRAINT `FK_civicrm_grant_program_grant_type_id` FOREIGN KEY (`grant_type_id`) REFERENCES `civicrm_option_value` (`id`),
   ADD CONSTRAINT `FK_civicrm_grant_program_status_id` FOREIGN KEY (`status_id`) REFERENCES `civicrm_option_value` (`id`);
 
--- add columns to civicrm_grant
-ALTER TABLE `civicrm_grant` 
-  ADD `grant_program_id` INT( 10 ) UNSIGNED NOT NULL COMMENT 'Grant Program ID of grant program record given grant belongs to.' AFTER `contact_id`,
-  ADD `grant_rejected_reason_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL COMMENT 'Id of Grant Rejected Reason.' AFTER `status_id` ,
-  ADD `grant_incomplete_reason_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL COMMENT 'Id of Grant Incomplete Reason.' AFTER `grant_rejected_reason_id` ,
-  ADD `assessment` VARCHAR( 655 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `grant_incomplete_reason_id`;
-
---
--- Constraints for table `civicrm_grant`
-ALTER TABLE `civicrm_grant`
-  ADD CONSTRAINT `FK_civicrm_grant_grant_program_id` FOREIGN KEY (`grant_program_id`) REFERENCES `civicrm_grant_program` (`id`) ON DELETE CASCADE;
-
 -- add option groups and option values
 
 -- Grant Payment Status
@@ -150,9 +138,9 @@ SELECT @opv3 := id FROM civicrm_option_value WHERE  name = 'Allocation Finalized
 
 INSERT IGNORE INTO `civicrm_option_value` (`id`, `option_group_id`, `label`, `value`, `name`, `grouping`, `filter`, `is_default`, `weight`, `description`, `is_optgroup`, `is_reserved`, `is_active`, `component_id`, `domain_id`, `visibility_id`) 
  VALUES
-(@opv1, @opGId, 'Accepting Applications', '1', NULL, NULL, 0, 0, 1, NULL, 0, 0, 1, NULL, NULL, NULL),
-(@opv2, @opGId, 'Trial Allocation', '2', NULL, NULL, 0, 0, 2, NULL, 0, 0, 1, NULL, NULL, NULL),
-(@opv3, @opGId, 'Allocation Finalized', '3', NULL, NULL, 0, 0, 3, NULL, 0, 0, 1, NULL, NULL, NULL);
+(@opv1, @opGId, 'Accepting Applications', '1', 'Accepting Applications', NULL, 0, 0, 1, NULL, 0, 0, 1, NULL, NULL, NULL),
+(@opv2, @opGId, 'Trial Allocation', '2', 'Trial Allocation', NULL, 0, 0, 2, NULL, 0, 0, 1, NULL, NULL, NULL),
+(@opv3, @opGId, 'Allocation Finalized', '3', 'Allocation Finalized', NULL, 0, 0, 3, NULL, 0, 0, 1, NULL, NULL, NULL);
 
 -- Grant Program Allocation Algorithm
 SET @opGId := '';
@@ -355,3 +343,30 @@ INSERT INTO civicrm_option_value(option_group_id, label, value, name, grouping,
 VALUES (@activityType, 'Grant Status Change', @value, 'grant_status_change', NULL, 0, 0, @value, 'Grant status change', 0, 1, 1, 5, NULL),
        (@activityType, 'Grant Payment', @value + 1, 'grant_payment', NULL, 0, 0, @value + 1, 'Grant payment', 0, 1, 1, 5, NULL);
 
+
+-- Enter a default grant program
+SET @opGId := '';
+SET @statusId := '';
+SELECT @opGId := id FROM `civicrm_option_group` WHERE `name` = 'grant_program_status';
+SELECT @statusId := id FROM `civicrm_option_value` WHERE  `name` = 'Accepting Applications' AND `option_group_id` = @opGId;
+
+SET @optGId := '';
+SET @algId := '';
+SELECT @optGId := id FROM `civicrm_option_group` WHERE `name` = 'allocation_algorithm';
+SELECT @algId := id FROM `civicrm_option_value` WHERE  `name` = 'Best To Worst, Fully Funded' AND `option_group_id` = @optGId;
+
+INSERT INTO `civicrm_grant_program` (`label`, `name`, `grant_type_id`, `total_amount`, `remainder_amount`, `financial_type_id`, `status_id`, `applications_start_date`, `applications_end_date`, `allocation_date`, `is_active`, `is_auto_email`, `allocation_algorithm`, `grant_program_id`) VALUES
+('Default Grant Program', 'Default Grant Program', 1, 1000000.00, 1000000.00, 1, @statusId, NULL, NULL, NULL, 1, 1, @algId, 0);
+
+-- add columns to civicrm_grant
+ALTER TABLE `civicrm_grant` 
+  ADD `grant_program_id` INT( 10 ) UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Grant Program ID of grant program record given grant belongs to.' AFTER `contact_id`,
+  ADD `grant_rejected_reason_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL COMMENT 'Id of Grant Rejected Reason.' AFTER `status_id` ,
+  ADD `grant_incomplete_reason_id` INT( 10 ) UNSIGNED NULL DEFAULT NULL COMMENT 'Id of Grant Incomplete Reason.' AFTER `grant_rejected_reason_id` ,
+  ADD `assessment` VARCHAR( 655 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL AFTER `grant_incomplete_reason_id`;
+
+--
+-- Constraints for table `civicrm_grant`
+ALTER TABLE `civicrm_grant`
+  ADD CONSTRAINT `FK_civicrm_grant_grant_program_id` FOREIGN KEY (`grant_program_id`) REFERENCES `civicrm_grant_program` (`id`) ON DELETE CASCADE;
+
-- 
GitLab