From: Vimal Joseph Date: Sun, 14 Dec 2014 17:00:11 +0000 (+0530) Subject: Fix progress bar bug in fetching values from contributions. X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=db6df0529d3e8fb4977070fd707b8d54fc178ef8;p=com.zyxware.civiwci.git Fix progress bar bug in fetching values from contributions. Now the progressbar can fetch values with in a date range. It will only consider completed contributions. It fetch values from the net amount. --- diff --git a/CRM/Wci/BAO/ProgressBar.php b/CRM/Wci/BAO/ProgressBar.php index 01fbf8d..58fc5cc 100644 --- a/CRM/Wci/BAO/ProgressBar.php +++ b/CRM/Wci/BAO/ProgressBar.php @@ -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) { diff --git a/CRM/Wci/DAO/ProgressBarFormula.php b/CRM/Wci/DAO/ProgressBarFormula.php index a6982f7..412414d 100644 --- a/CRM/Wci/DAO/ProgressBarFormula.php +++ b/CRM/Wci/DAO/ProgressBarFormula.php @@ -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; diff --git a/CRM/Wci/Form/ProgressBar.php b/CRM/Wci/Form/ProgressBar.php index 3c36aea..55acfa8 100644 --- a/CRM/Wci/Form/ProgressBar.php +++ b/CRM/Wci/Form/ProgressBar.php @@ -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'); diff --git a/CRM/Wci/Upgrader.php b/CRM/Wci/Upgrader.php index 947b16c..4188ee1 100644 --- a/CRM/Wci/Upgrader.php +++ b/CRM/Wci/Upgrader.php @@ -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; + } + } diff --git a/js/addmore.js b/js/addmore.js index dddf1f9..c1252fa 100644 --- a/js/addmore.js +++ b/js/addmore.js @@ -30,6 +30,8 @@ cj(function ( $ ) { ' Remove'); $('#' + "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('

'); }); @@ -75,6 +77,26 @@ cj(function ( $ ) { $('#' + id_content).append(' Remove'); $('#' + id_section).append(""); + id_section = "crm-section-startdate-" + count; + sect_tag = "
"; + $('#addmore_link').parent().parent().before(sect_tag); + + id_content = "content_startdate-" + count; + $('#' + id_section).append("
"); + $('#' + id_content).append(''); + $('#' + id_content).append('(Format YYYY-MM-DD)
Date from which contributions to be added to this progress bar. Keep it empty to select contributions from the beginning.'); + $('#' + id_section).append("
"; + $('#addmore_link').parent().parent().before(sect_tag); + + id_content = "content_enddate-" + count; + $('#' + id_section).append("
"); + $('#' + id_content).append(''); + $('#' + id_content).append('(Format YYYY-MM-DD)
Date to which contributions to be added to this progress bar. Keep it empty to select contributions up to today'); + $('#' + id_section).append("
"; $('#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); diff --git a/sql/install.sql b/sql/install.sql index a80194e..8bb2804 100644 --- a/sql/install.sql +++ b/sql/install.sql @@ -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 diff --git a/templates/CRM/Wci/Form/ProgressBar.tpl b/templates/CRM/Wci/Form/ProgressBar.tpl index e613158..c51d331 100644 --- a/templates/CRM/Wci/Form/ProgressBar.tpl +++ b/templates/CRM/Wci/Form/ProgressBar.tpl @@ -36,22 +36,28 @@ {include file="CRM/common/formButtons.tpl" location="top"}
- {* FIELD EXAMPLE: OPTION 1 (AUTOMATIC LAYOUT) *} - {foreach from=$elementNames item=elementName}
{$form.$elementName.label}
-
{$form.$elementName.html}
+ {if substr($elementName, 0, 23) eq 'contribution_start_date'} +
{$form.$elementName.html} + (Format YYYY-MM-DD) +
+ {ts}Date from which contributions to be added to this progressbar. Keep it empty to select contributions from the beginning.{/ts} +
+ {elseif substr($elementName, 0, 21) eq 'contribution_end_date'} +
{$form.$elementName.html} + (Format YYYY-MM-DD) +
+ {ts}Date to which contributions to be added to this progressbar. Keep it empty to select contributions up to today{/ts} +
+ {else} +
{$form.$elementName.html}
+ {/if}
{/foreach} - {* FIELD EXAMPLE: OPTION 2 (MANUAL LAYOUT) - -
- {$form.favorite_color.label} - {$form.favorite_color.html} -
{* FOOTER *}