Fix progress bar bug in fetching values from contributions.
authorVimal Joseph <vimal.joseph@zyxware.com>
Sun, 14 Dec 2014 17:00:11 +0000 (22:30 +0530)
committerVimal Joseph <vimal.joseph@zyxware.com>
Sun, 14 Dec 2014 17:00:11 +0000 (22:30 +0530)
Now the progressbar can fetch values with in a date range.
It will only consider completed contributions.
It fetch values from the net amount.

CRM/Wci/BAO/ProgressBar.php
CRM/Wci/DAO/ProgressBarFormula.php
CRM/Wci/Form/ProgressBar.php
CRM/Wci/Upgrader.php
js/addmore.js
sql/install.sql
templates/CRM/Wci/Form/ProgressBar.tpl

index 01fbf8d34db12d1d6b95fe97daf9631a3f395656..58fc5cc5834ecc6511f9f9be21ae0607bb83dcce 100644 (file)
@@ -60,30 +60,22 @@ class CRM_Wci_BAO_ProgressBar extends CRM_Wci_DAO_ProgressBar {
    * @access public
    */
   public static function getPBCollectedAmount($pbId) {
-    $bp = 0;
-    $query = "SELECT * FROM civicrm_wci_progress_bar_formula WHERE progress_bar_id =" . $pbId;
+    $amount = 0;
+    $query = "SELECT sum((civicontrib.net_amount * wcipb.percentage) / 100) as amount
+      FROM civicrm_wci_progress_bar_formula wcipb
+      JOIN civicrm_contribution civicontrib
+      ON wcipb.contribution_page_id = civicontrib.contribution_page_id
+      WHERE
+        civicontrib.contribution_status_id = 1
+        AND (DATE(civicontrib.receive_date) >= if(wcipb.start_date is not NULL, wcipb.start_date, '0000-00-00')
+        AND DATE(civicontrib.receive_date) <= if(wcipb.end_date is not NULL, wcipb.end_date, DATE(now())))
+        AND wcipb.progress_bar_id =" . $pbId;
     $params = array();
-
-    $daoPbf = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
-      while ($daoPbf->fetch()) {
-        $for_page[$daoPbf->id] = array();
-        CRM_Core_DAO::storeValues($daoPbf, $for_page[$daoPbf->id]);
-        $px = $for_page[$daoPbf->id]['percentage'];
-
-        $query = "SELECT * FROM civicrm_contribution where contribution_page_id =" . $for_page[$daoPbf->id]['contribution_page_id'];
-        $params = array();
-
-        $daoCon = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Contribute_DAO_Contribution');
-
-        while ($daoCon->fetch()) {
-          $contributions[$daoCon->id] = array();
-          CRM_Core_DAO::storeValues($daoCon, $contributions[$daoCon->id]);
-          $bx = $contributions[$daoCon->id]['total_amount'];
-
-          $bp += $bx * $px / 100;
-        }
-     }
-     return floor($bp);
+    $daoPbf = CRM_Core_DAO::executeQuery($query, $params);
+    if ($daoPbf->fetch()) {
+      $amount = $daoPbf->amount;
+    }
+    return round($amount);
   }
 
   public static function getProgressbarInfo($pbId) {
index a6982f78d362358ad5f9282ee1089bc00271f87a..412414daf16490d451db757dfc6d438fdcc31298 100644 (file)
@@ -104,6 +104,10 @@ class CRM_Wci_DAO_ProgressBarFormula extends CRM_Core_DAO
    * @var float
    */
   public $percentage;
+
+  public $start_date;
+
+  public $end_date;
   function __construct()
   {
     $this->__table = 'civicrm_wci_progress_bar_formula';
@@ -154,12 +158,22 @@ class CRM_Wci_DAO_ProgressBarFormula extends CRM_Core_DAO
           'title' => ts('Progress Bar Reference Id', array('domain' => 'com.zyxware.civiwci')) ,
           'required' => true,
         ) ,
+        'start_date' => array(
+          'name' => 'start_date',
+          'type' => CRM_Utils_Type::T_DATE,
+          'title' => ts('Start date', array('domain' => 'com.zyxware.civiwci'))
+        ),
+        'end_date' => array(
+          'name' => 'end_date',
+          'type' => CRM_Utils_Type::T_DATE,
+          'title' => ts('End date', array('domain' => 'com.zyxware.civiwci'))
+        ),
         'percentage' => array(
           'name' => 'percentage',
           'type' => CRM_Utils_Type::T_FLOAT,
           'title' => ts('Percentage Amount', array('domain' => 'com.zyxware.civiwci')) ,
           'required' => true,
-        ) ,
+        )
       );
     }
     return self::$_fields;
@@ -178,7 +192,9 @@ class CRM_Wci_DAO_ProgressBarFormula extends CRM_Core_DAO
         'id' => 'progress_bar_formula_id',
         'contribution_page_id' => 'contribution_page_id',
         'progress_bar_id' => 'progress_bar_id',
-        'percentage' => 'percentage',
+        'start_date' => 'start_date',
+        'end_date' => 'end_date',
+        'percentage' => 'percentage'
       );
     }
     return self::$_fieldKeys;
index 3c36aea52befeaf46eec712597077186103276b5..55acfa88fdc5a5decc21279a8c492c70476d7337 100644 (file)
@@ -64,11 +64,7 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
       $params = array(1 => array($this->_id, 'Integer'));
 
       $dao = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Wci_DAO_ProgressBarFormula');
-
       while ($dao->fetch()) {
-        $for_page[$dao->id] = array();
-        CRM_Core_DAO::storeValues($dao, $for_page[$dao->id]);
-
         $this->add(
           'select', // field type
           'contribution_page_'.$count, // field name
@@ -76,6 +72,16 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
           getContributionPageOptions(), // list of options
           false // is required
         );
+        $this->add(
+          'text',
+          'contribution_start_date_' . $count ,
+          ts('Start Date')
+        );
+        $this->add(
+          'text',
+          'contribution_end_date_' . $count,
+          ts('End Date')
+        );
         $this->add(
           'text', // field type
           'percentage_'.$count, // field name
@@ -83,13 +89,17 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
           false // is required
         );
         //save formula id
-        $this->addElement('hidden', 'contrib_elem_'.$count , $for_page[$dao->id]['id']);
+        $this->addElement('hidden', 'contrib_elem_'.$count , $dao->id);
 
         $this->setDefaults(array(
-              'contribution_page_'.$count => $for_page[$dao->id]['contribution_page_id']));
+              'contribution_page_'.$count => $dao->contribution_page_id));
         $this->setDefaults(array(
-              'percentage_'.$count => $for_page[$dao->id]['percentage']));
-
+              'percentage_'.$count => $dao->percentage));
+        $this->setDefaults(array(
+              'contribution_start_date_'.$count => $dao->start_date));
+        $this->setDefaults(array(
+              'contribution_end_date_'.$count => $dao->end_date));
+        //set default for start date and end date.
         $count++;
       }
       CRM_Utils_System::setTitle(ts('Edit Progress Bar'));
@@ -104,6 +114,16 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
         getContributionPageOptions(), // list of options
         true // is required
       );
+      $this->add(
+        'text',
+        'contribution_start_date_1',
+        ts('Start Date')
+      );
+      $this->add(
+        'text',
+        'contribution_end_date_1',
+        ts('End Date')
+      );
       $this->add(
         'text', // field type
         'percentage_1', // field name
@@ -177,18 +197,28 @@ class CRM_Wci_Form_ProgressBar extends CRM_Core_Form {
         for($i = 1; $i <= (int)$_REQUEST['contrib_count']; $i++) {
           $page = 'contribution_page_' . (string)$i;
           $perc = 'percentage_' . (string)$i;
+          $sdate = 'contribution_start_date_' . (string)$i;
+          $edate = 'contribution_end_date_' . (string)$i;
 
           $sql = "INSERT INTO civicrm_wci_progress_bar_formula
-            (contribution_page_id, progress_bar_id, percentage)
-            VALUES (%1, %2, %3)";
-
+            (contribution_page_id, progress_bar_id, start_date, end_date, percentage)
+            VALUES (%1, %2, %3, %4, %5)";
+            $start = NULL;
+            $end = NULL;
+            if (!empty($_REQUEST[$sdate])) {
+              $start = CRM_Utils_Date::processDate($_REQUEST[$sdate], NULL, FALSE, "Ymd");
+            }
+            if (!empty($_REQUEST[$edate])) {
+              $end = CRM_Utils_Date::processDate($_REQUEST[$edate], NULL, FALSE, "Ymd");
+            }
           CRM_Core_DAO::executeQuery($sql,
             array(1 => array($_REQUEST[$page], 'Integer'),
-            2 => array($this->_id, 'Integer'),
-            3 => array($_REQUEST[$perc], 'Float'),
-          ));
+              2 => array($this->_id, 'Integer'),
+              3 => array($start, 'Date'),
+              4 => array($end, 'Date'),
+              5 => array($_REQUEST[$perc], 'Float')
+            ));
         }
-
         $transaction->commit();
         CRM_Wci_BAO_WidgetCache::deleteWidgetCacheByProgressbar($this->_id);
         CRM_Core_Session::setStatus(ts('Progress bar created successfully'), '', 'success');
index 947b16c5972a4fe5e8b10053543affa6a6aa7231..4188ee1e83bb92d91c9e77846b71e49a717fbc9f 100644 (file)
@@ -96,4 +96,21 @@ class CRM_Wci_Upgrader extends CRM_Wci_Upgrader_Base {
     return TRUE;
   }
 
+   public function upgrade_1002() {
+    $this->ctx->log->info('Applying update 1002');
+    CRM_Core_DAO::executeQuery('
+      ALTER TABLE `civicrm_wci_progress_bar_formula`
+      ADD `start_date` DATE NULL DEFAULT NULL
+      COMMENT "Contribtuion start date"
+      AFTER `progress_bar_id`
+    ');
+    CRM_Core_DAO::executeQuery('
+      ALTER TABLE `civicrm_wci_progress_bar_formula`
+      ADD `end_date` DATE NULL DEFAULT NULL
+      COMMENT "Contribtuion end date"
+      AFTER `start_date`
+    ');
+    return TRUE;
+  }
+
 }
index dddf1f998ccedbcb4e8f7fb0caf3c17edb47824f..c1252fa8b4538a8a69378bf61df71b4a39d58a0b 100644 (file)
@@ -30,6 +30,8 @@ cj(function ( $ ) {
       '<a id=\"remove_link\" class=\"form-link\" href=\"remove\" name=\"remove_link-' + i + '\"> Remove</a>');
       $('#' + "contribution_page_" + i).parent().parent().attr("id", "crm-section-con-" + i);
       $('#' + "percentage_" + i).parent().parent().attr("id", 'crm-section-per-' + i);
+      $('#' + "contribution_start_date_" + i).parent().parent().attr("id", 'crm-section-startdate-' + i);
+      $('#' + "contribution_end_date_" + i).parent().parent().attr("id", 'crm-section-enddate-' + i);
     }
     $('#goal_amount').parent().after('<div class="crm-wci-pb"><hr></div><label><SMALL>Progressbar shows the sum of each percentage of contributions done on each selected contribution page</SMALL></label>');
   });
@@ -75,6 +77,26 @@ cj(function ( $ ) {
     $('#' + id_content).append('<a id=\"remove_link\" class=\"form-link\" href=\"remove\" name=\"remove_link-' + count + '\"> Remove</a>');
     $('#' + id_section).append("</div>");
 
+    id_section = "crm-section-startdate-" + count;
+    sect_tag = "<div class=\"crm-section\" id=" + id_section + "> <div class=\"label\"><label>Start Date</label>";
+    $('#addmore_link').parent().parent().before(sect_tag);
+
+    id_content = "content_startdate-" + count;
+    $('#' + id_section).append("<div class=\"content\" id="+ id_content + ">");
+    $('#' + id_content).append('<input type="text" size="20" id = "startdate_' + count + '" name="startdate_' + count +'" value="" />');
+    $('#' + id_content).append('<span class=\"description\">(Format YYYY-MM-DD)</span><br><span class=\"description\">Date from which contributions to be added to this progress bar. Keep it empty to select contributions from the beginning.</span>');
+    $('#' + id_section).append("</div");
+
+    id_section = "crm-section-enddate-" + count;
+    sect_tag = "<div class=\"crm-section\" id=" + id_section + "> <div class=\"label\"><label>End Date</label>";
+    $('#addmore_link').parent().parent().before(sect_tag);
+
+    id_content = "content_enddate-" + count;
+    $('#' + id_section).append("<div class=\"content\" id="+ id_content + ">");
+    $('#' + id_content).append('<input type="text" size="20" id = "enddate_' + count + '" name="enddate_' + count +'" value="" />');
+    $('#' + id_content).append('<span class=\"description\">(Format YYYY-MM-DD)</span><br><span class=\"description\">Date to which contributions to be added to this progress bar. Keep it empty to select contributions up to today</span>');
+    $('#' + id_section).append("</div");
+
     id_section = "crm-section-per-" + count;
     sect_tag = "<div class=\"crm-section\" id=" + id_section + "> <div class=\"label\"><label>Percentage</label>";
     $('#addmore_link').parent().parent().before(sect_tag);
@@ -107,6 +129,8 @@ cj(function ( $ ) {
     var contri_page = "\"#percentage_" + rem_name_ar[1] + "\"";
     $('#crm-section-con-'+ rem_name_ar[1] +'').remove();
     $('#crm-section-per-'+ rem_name_ar[1] +'').remove();
+    $('#crm-section-startdate-'+ rem_name_ar[1] +'').remove();
+    $('#crm-section-enddate-'+ rem_name_ar[1] +'').remove();
     var count = parseInt($('input[name=contrib_count]').val());
     count--;
     $('input[name=contrib_count]').val(count);
index a80194ed0a623f46f0968c215c5905e035ef46ab..8bb280406694f8b52acbb8f1304b90fbf23b3d81 100644 (file)
@@ -13,6 +13,8 @@ CREATE TABLE IF NOT EXISTS civicrm_wci_progress_bar_formula (
   id int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'Formula entry Id.',
   contribution_page_id int(10) unsigned NOT NULL COMMENT 'Reference contribution page id.',
   progress_bar_id int(10) unsigned DEFAULT NULL COMMENT 'Custom Progress bar reference id.',
+  start_date DATE NULL DEFAULT NULL COMMENT 'Contribtuion start date.',
+  end_date DATE NULL DEFAULT NULL COMMENT 'Contribtuion end date.',
   percentage float unsigned NULL COMMENT 'Percentage amount.',
   PRIMARY KEY (`id`),
   CONSTRAINT FK_civicrm_wci_progress_bar_formula_progress_bar_id FOREIGN KEY (`progress_bar_id`) REFERENCES `civicrm_wci_progress_bar`(`id`) ON DELETE SET NULL
index e61315857640cd2b02c7222b251803e9661b290e..c51d33181f3fbac47e899b96746ebd49aacbd5dd 100644 (file)
   {include file="CRM/common/formButtons.tpl" location="top"}
   </div>
 
-  {* FIELD EXAMPLE: OPTION 1 (AUTOMATIC LAYOUT) *}
-
   {foreach from=$elementNames item=elementName}
     <div class="crm-section">
       <div class="label">{$form.$elementName.label}</div>
-      <div class="content">{$form.$elementName.html}</div>
+      {if substr($elementName, 0, 23)  eq 'contribution_start_date'}
+        <div class="content">{$form.$elementName.html}
+          <span class="description">(Format YYYY-MM-DD)</span>
+          <br>
+          <span class="description">{ts}Date from which contributions to be added to this progressbar. Keep it empty to select contributions from the beginning.{/ts}</span>
+        </div>
+      {elseif substr($elementName, 0, 21)  eq 'contribution_end_date'}
+        <div class="content">{$form.$elementName.html}
+          <span class="description">(Format YYYY-MM-DD)</span>
+          <br>
+          <span class="description">{ts}Date to which contributions to be added to this progressbar. Keep it empty to select contributions up to today{/ts}</span>
+        </div>
+      {else}
+        <div class="content">{$form.$elementName.html}</div>
+      {/if}
       <div class="clear"></div>
     </div>
   {/foreach}
 
-  {* FIELD EXAMPLE: OPTION 2 (MANUAL LAYOUT)
-
-    <div>
-      <span>{$form.favorite_color.label}</span>
-      <span>{$form.favorite_color.html}</span>
-    </div>
 
   {* FOOTER *}
   <div class="crm-submit-buttons">