PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Charts/code/Chart_my_pipeline_by_sales_stage.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 494 lines | 374 code | 54 blank | 66 comment | 53 complexity | b113bc62d9939b9c88de397e0dbf4281 MD5 | raw file
Possible License(s): LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. <?php
  2. if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
  3. /*********************************************************************************
  4. * SugarCRM Community Edition is a customer relationship management program developed by
  5. * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it under
  8. * the terms of the GNU Affero General Public License version 3 as published by the
  9. * Free Software Foundation with the addition of the following permission added
  10. * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
  11. * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
  12. * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  16. * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License along with
  20. * this program; if not, see http://www.gnu.org/licenses or write to the Free
  21. * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  22. * 02110-1301 USA.
  23. *
  24. * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
  25. * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
  26. *
  27. * The interactive user interfaces in modified source and object code versions
  28. * of this program must display Appropriate Legal Notices, as required under
  29. * Section 5 of the GNU Affero General Public License version 3.
  30. *
  31. * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
  32. * these Appropriate Legal Notices must retain the display of the "Powered by
  33. * SugarCRM" logo. If the display of the logo is not reasonably feasible for
  34. * technical reasons, the Appropriate Legal Notices must display the words
  35. * "Powered by SugarCRM".
  36. ********************************************************************************/
  37. /*********************************************************************************
  38. * Description: returns HTML for client-side image map.
  39. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
  40. * All Rights Reserved.
  41. * Contributor(s): ______________________________________..
  42. ********************************************************************************/
  43. require_once("include/charts/Charts.php");
  44. require_once("modules/Charts/code/Chart_pipeline_by_sales_stage.php");
  45. global $app_list_strings, $current_language, $sugar_config, $currentModule, $action, $current_user, $theme, $timedate;
  46. $user_dateFormat = $timedate->get_date_format();
  47. $current_module_strings = return_module_language($current_language, 'Charts');
  48. global $timedate;
  49. if (isset($_REQUEST['mypbss_refresh'])) { $refresh = $_REQUEST['mypbss_refresh']; }
  50. else { $refresh = false; }
  51. //get the dates to display
  52. $user_date_start = $current_user->getPreference('mypbss_date_start');
  53. if (!empty($user_date_start) && !isset($_REQUEST['mypbss_date_start'])) {
  54. $date_start = $user_date_start;
  55. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
  56. $GLOBALS['log']->debug($user_date_start);
  57. }
  58. elseif (isset($_REQUEST['mypbss_date_start']) && $_REQUEST['mypbss_date_start'] != '') {
  59. $date_start = $_REQUEST['mypbss_date_start'];
  60. $current_user->setPreference('mypbss_date_start', $_REQUEST['mypbss_date_start']);
  61. $GLOBALS['log']->debug("_REQUEST['mypbss_date_start'] is:");
  62. $GLOBALS['log']->debug($_REQUEST['mypbss_date_start']);
  63. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
  64. $GLOBALS['log']->debug($current_user->getPreference('mypbss_date_start'));
  65. }
  66. else {
  67. $date_start = $timedate->nowDate();
  68. }
  69. $user_date_end = $current_user->getPreference('mypbss_date_end');
  70. if (!empty($user_date_end) && !isset($_REQUEST['mypbss_date_end'])) {
  71. $date_end = $user_date_end;
  72. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
  73. $GLOBALS['log']->debug($user_date_end);
  74. }
  75. elseif (isset($_REQUEST['mypbss_date_end']) && $_REQUEST['mypbss_date_end'] != '') {
  76. $date_end = $_REQUEST['mypbss_date_end'];
  77. $current_user->setPreference('mypbss_date_end', $_REQUEST['mypbss_date_end']);
  78. $GLOBALS['log']->debug("_REQUEST['mypbss_date_end'] is:");
  79. $GLOBALS['log']->debug($_REQUEST['mypbss_date_end']);
  80. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
  81. $GLOBALS['log']->debug( $current_user->getPreference('mypbss_date_end'));
  82. }
  83. else {
  84. $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
  85. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
  86. }
  87. // cn: format date_start|end to user's preferred
  88. $dateStartDisplay = strftime($timedate->get_user_date_format(), strtotime($date_start));
  89. $dateEndDisplay = strftime($timedate->get_user_date_format(), strtotime($date_end));
  90. $seps = array("-", "/");
  91. $dates = array($date_start, $date_end);
  92. $dateFileNameSafe = str_replace($seps, "_", $dates);
  93. $dateXml[0] = $timedate->swap_formats($date_start, $user_dateFormat, $timedate->dbDayFormat);
  94. $dateXml[1] = $timedate->swap_formats($date_end, $user_dateFormat, $timedate->dbDayFormat);
  95. $tempx = array();
  96. $datax = array();
  97. $selected_datax = array();
  98. //get list of sales stage keys to display
  99. $user_sales_stage = $current_user->getPreference('mypbss_sales_stages');
  100. if (!empty($user_sales_stage) && count($user_sales_stage) > 0 && !isset($_REQUEST['mypbss_sales_stages'])) {
  101. $tempx = $user_sales_stage;
  102. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_sales_stages'] is:");
  103. $GLOBALS['log']->debug($user_sales_stage);
  104. }
  105. elseif (isset($_REQUEST['mypbss_sales_stages']) && count($_REQUEST['mypbss_sales_stages']) > 0) {
  106. $tempx = $_REQUEST['mypbss_sales_stages'];
  107. $current_user->setPreference('mypbss_sales_stages', $_REQUEST['mypbss_sales_stages']);
  108. $GLOBALS['log']->debug("_REQUEST['mypbss_sales_stages'] is:");
  109. $GLOBALS['log']->debug($_REQUEST['mypbss_sales_stages']);
  110. $GLOBALS['log']->debug("USER PREFRENCES['mypbss_sales_stages'] is:");
  111. $GLOBALS['log']->debug($current_user->getPreference('mypbss_sales_stages'));
  112. }
  113. //set $datax using selected sales stage keys
  114. if (count($tempx) > 0) {
  115. foreach ($tempx as $key) {
  116. $datax[$key] = $app_list_strings['sales_stage_dom'][$key];
  117. array_push($selected_datax, $key);
  118. }
  119. }
  120. else {
  121. $datax = $app_list_strings['sales_stage_dom'];
  122. $selected_datax = array_keys($app_list_strings['sales_stage_dom']);
  123. }
  124. $GLOBALS['log']->debug("datax is:");
  125. $GLOBALS['log']->debug($datax);
  126. $ids = array($current_user->id);
  127. //create unique prefix based on selected users for image files
  128. $id_hash = '1';
  129. if (isset($ids)) {
  130. sort($ids);
  131. $id_hash = crc32(implode('',$ids));
  132. if($id_hash < 0)
  133. {
  134. $id_hash = $id_hash * -1;
  135. }
  136. }
  137. $GLOBALS['log']->debug("ids is:");
  138. $GLOBALS['log']->debug($ids);
  139. $id_md5 = substr(md5($current_user->id),0,9);
  140. $seps = array("-", "/");
  141. $dates = array($dateStartDisplay, $dateEndDisplay);
  142. $dateFileNameSafe = str_replace($seps, "_", $dates);
  143. $cache_file_name = sugar_cached("xml/").$current_user->getUserPrivGuid()."_".$theme."_my_pipeline_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
  144. $GLOBALS['log']->debug("cache file name is: $cache_file_name");
  145. $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&mypbss_refresh=true" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('refresh','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_REFRESH']).'&nbsp;'.$current_module_strings['LBL_REFRESH'].'</a>&nbsp;&nbsp;<a href="javascript: toggleDisplay(\'my_pipeline_edit\');" class="tabFormAdvLink">'.SugarThemeRegistry::current()->getImage('edit','border="0" align="absmiddle"',null,null,'.gif',$mod_strings['LBL_EDIT']).'&nbsp;'. $current_module_strings['LBL_EDIT'].'</a></div>';
  146. ?>
  147. <?php echo get_form_header($mod_strings['LBL_PIPELINE_FORM_TITLE'], $tools , false);?>
  148. <?php
  149. global $timedate;
  150. $cal_lang = "en";
  151. $cal_dateformat = $timedate->get_cal_date_format();
  152. ?>
  153. <p>
  154. <div id='my_pipeline_edit' style='display: none;'>
  155. <form name='my_pipeline' action="index.php" method="post" >
  156. <input type="hidden" name="module" value="<?php echo $currentModule;?>">
  157. <input type="hidden" name="action" value="<?php echo $action;?>">
  158. <input type="hidden" name="mypbss_refresh" value="true">
  159. <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
  160. <tr>
  161. <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_START']?> </b><br><i><?php echo $timedate->get_user_date_format();?></i></td>
  162. <td valign='top' ><input onblur="parseDate(this, '<?php echo $cal_dateformat ?>');" class="text" name="mypbss_date_start" size='12' maxlength='10' id='date_start' value='<?php echo $date_start; ?>'> <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_start_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </td>
  163. </tr>
  164. <tr>
  165. <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_DATE_END'];?></b><br><i><?php echo $timedate->get_user_date_format();?></i></td>
  166. <td valign='top' ><input onblur="parseDate(this, '<?php echo $cal_dateformat ?>');" class="text" name="mypbss_date_end" size='12' maxlength='10' id='date_end' value='<?php echo $date_end; ?>'> <?php echo SugarThemeRegistry::current()->getImage('jscalendar', 'id="date_end_trigger" align="absmiddle"', null, null, ".gif", $app_strings['LBL_ENTER_DATE']); ?> </td>
  167. </tr>
  168. <tr>
  169. <td valign='top' nowrap><b><?php echo $current_module_strings['LBL_SALES_STAGES'];?></b></td>
  170. <td valign='top' ><select name="mypbss_sales_stages[]" multiple size='3'><?php echo get_select_options_with_id($app_list_strings['sales_stage_dom'],$selected_datax); ?></select></td>
  171. </tr>
  172. <tr>
  173. <td align="right" colspan="2"><input class="button" onclick="return verify_chart_data(my_pipeline);" type="submit" title="<?php echo $app_strings['LBL_SELECT_BUTTON_TITLE']; ?>" value="<?php echo $app_strings['LBL_SELECT_BUTTON_LABEL']?>" /><input class="button" onClick="javascript: toggleDisplay('my_pipeline_edit');" type="button" title="<?php echo $app_strings['LBL_CANCEL_BUTTON_TITLE']; ?>" accessKey="<?php echo $app_strings['LBL_CANCEL_BUTTON_KEY'];?>" value="<?php echo $app_strings['LBL_CANCEL_BUTTON_LABEL']?>"/></td>
  174. </tr>
  175. </table>
  176. </form>
  177. <script type="text/javascript">
  178. Calendar.setup ({
  179. inputField : "date_start", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_start_trigger", singleClick : true, step : 1, weekNumbers:false
  180. });
  181. Calendar.setup ({
  182. inputField : "date_end", ifFormat : "<?php echo $cal_dateformat ?>", showsTime : false, button : "date_end_trigger", singleClick : true, step : 1, weekNumbers:false
  183. });
  184. </script>
  185. </div>
  186. </p>
  187. <?php
  188. echo "<p align='center'>".gen_xml_pipeline_by_sales_stage($datax, $dateXml[0], $dateXml[1], $ids, $cache_file_name, $refresh,'hBarS',$current_module_strings)."</p>";
  189. echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_PIPELINE_FORM_TITLE_DESC']."</span></P>";
  190. if (file_exists($cache_file_name)) {
  191. $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($cache_file_name)));
  192. }
  193. else {
  194. $file_date = '';
  195. }
  196. ?>
  197. <span class='chartFootnote'>
  198. <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
  199. </span>
  200. <?php
  201. echo get_validate_chart_js();
  202. /**
  203. * Creates opportunity pipeline image as a HORIZONTAL accumlated BAR GRAPH for multiple users.
  204. * param $datax- the sales stage data to display in the x-axis
  205. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
  206. * All Rights Reserved..
  207. * Contributor(s): ______________________________________..
  208. */
  209. function gen_xml_pipeline_by_sales_stage($datax=array('foo','bar'), $date_start='2071-10-15', $date_end='2071-10-15', $user_id=array('1'), $cache_file_name='a_file', $refresh=false,$chart_size='hBarF',$current_module_strings) {
  210. global $app_strings, $charset, $lang, $barChartColors, $current_user;
  211. $kDelim = $current_user->getPreference('num_grp_sep');
  212. global $timedate;
  213. if (!file_exists($cache_file_name) || $refresh == true) {
  214. $GLOBALS['log']->debug("starting pipeline chart");
  215. $GLOBALS['log']->debug("datax is:");
  216. $GLOBALS['log']->debug($datax);
  217. $GLOBALS['log']->debug("user_id is: ");
  218. $GLOBALS['log']->debug($user_id);
  219. $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
  220. $opp = new Opportunity;
  221. $where="";
  222. //build the where clause for the query that matches $user
  223. $count = count($user_id);
  224. $id = array();
  225. $user_list = get_user_array(false);
  226. foreach ($user_id as $key) {
  227. $new_ids[$key] = $user_list[$key];
  228. }
  229. if ($count>0) {
  230. foreach ($new_ids as $the_id=>$the_name) {
  231. $id[] = "'".$the_id."'";
  232. }
  233. $ids = join(",",$id);
  234. $where .= "opportunities.assigned_user_id IN ($ids) ";
  235. }
  236. //build the where clause for the query that matches $datax
  237. $count = count($datax);
  238. $dataxArr = array();
  239. if ($count>0) {
  240. foreach ($datax as $key=>$value) {
  241. $dataxArr[] = "'".$key."'";
  242. }
  243. $dataxArr = join(",",$dataxArr);
  244. $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
  245. }
  246. //build the where clause for the query that matches $date_start and $date_end
  247. $where .= " AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
  248. AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
  249. $where .= " AND opportunities.assigned_user_id = users.id AND opportunities.deleted=0 ";
  250. //Now do the db queries
  251. //query for opportunity data that matches $datax and $user
  252. $query = " SELECT opportunities.sales_stage,
  253. users.user_name,
  254. opportunities.assigned_user_id,
  255. count( * ) AS opp_count,
  256. sum(amount_usdollar/1000) AS total
  257. FROM users,opportunities ";
  258. $query .= "WHERE " .$where;
  259. $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
  260. $result = $opp->db->query($query, true);
  261. //build pipeline by sales stage data
  262. $total = 0;
  263. $div = 1;
  264. global $sugar_config;
  265. $symbol = $sugar_config['default_currency_symbol'];
  266. global $current_user;
  267. if($current_user->getPreference('currency') ){
  268. $currency = new Currency();
  269. $currency->retrieve($current_user->getPreference('currency'));
  270. $div = $currency->conversion_rate;
  271. $symbol = $currency->symbol;
  272. }
  273. // cn: adding user-pref date handling
  274. $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
  275. $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
  276. $fileContents = ' <yData defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'">'."\n";
  277. $stageArr = array();
  278. $usernameArr = array();
  279. $rowTotalArr = array();
  280. $rowTotalArr[] = 0;
  281. while($row = $opp->db->fetchByAssoc($result, false))
  282. {
  283. if($row['total']*$div<=100){
  284. $sum = round($row['total']*$div, 2);
  285. } else {
  286. $sum = round($row['total']*$div);
  287. }
  288. if(!isset($stageArr[$row['sales_stage']]['row_total'])) {$stageArr[$row['sales_stage']]['row_total']=0;}
  289. $stageArr[$row['sales_stage']][$row['assigned_user_id']]['opp_count'] = $row['opp_count'];
  290. $stageArr[$row['sales_stage']][$row['assigned_user_id']]['total'] = $sum;
  291. $stageArr[$row['sales_stage']]['people'][$row['assigned_user_id']] = $row['user_name'];
  292. $stageArr[$row['sales_stage']]['row_total'] += $sum;
  293. $usernameArr[$row['assigned_user_id']] = $row['user_name'];
  294. $total += $sum;
  295. }
  296. foreach ($datax as $key=>$translation) {
  297. if(isset($stageArr[$key]['row_total'])){$rowTotalArr[]=$stageArr[$key]['row_total'];}
  298. if(isset($stageArr[$key]['row_total']) && $stageArr[$key]['row_total']>100) {
  299. $stageArr[$key]['row_total'] = round($stageArr[$key]['row_total']);
  300. }
  301. $fileContents .= ' <dataRow title="'.$translation.'" endLabel="';
  302. if(isset($stageArr[$key]['row_total'])){$fileContents .= $stageArr[$key]['row_total'];}
  303. $fileContents .= '">'."\n";
  304. if(isset($stageArr[$key]['people'])){
  305. asort($stageArr[$key]['people']);
  306. reset($stageArr[$key]['people']);
  307. foreach ($stageArr[$key]['people'] as $nameKey=>$nameValue) {
  308. $fileContents .= ' <bar id="'.$nameKey.'" totalSize="'.$stageArr[$key][$nameKey]['total'].'" altText="'.$nameValue.': '.$stageArr[$key][$nameKey]['opp_count'].' '.$current_module_strings['LBL_OPPS_WORTH'].' '.currency_format_number($stageArr[$key][$nameKey]['total'], array('currency_symbol'=>true)).$current_module_strings['LBL_OPP_THOUSANDS'].' '.$current_module_strings['LBL_OPPS_IN_STAGE'].' '.$translation.'" url="index.php?module=Opportunities&action=index&assigned_user_id[]='.$nameKey.'&sales_stage='.urlencode($key).'&date_start='.$date_start.'&date_closed='.$date_end.'&query=true&searchFormTab=advanced_search"/>'."\n";
  309. }
  310. }
  311. $fileContents .= ' </dataRow>'."\n";
  312. }
  313. $fileContents .= ' </yData>'."\n";
  314. $max = get_max($rowTotalArr);
  315. if($chart_size=='hBarF'){
  316. $length = "10";
  317. }else{
  318. $length = "4";
  319. }
  320. $fileContents .= ' <xData min="0" max="'.$max.'" length="'.$length.'" kDelim="'.$kDelim.'" prefix="'.$symbol.'" suffix=""/>'."\n";
  321. $fileContents .= ' <colorLegend status="on">'."\n";
  322. $i=0;
  323. asort($new_ids);
  324. foreach ($new_ids as $key=>$value) {
  325. $color = generate_graphcolor($key,$i);
  326. $fileContents .= ' <mapping id="'.$key.'" name="'.$value.'" color="'.$color.'"/>'."\n";
  327. $i++;
  328. }
  329. $fileContents .= ' </colorLegend>'."\n";
  330. $fileContents .= ' <graphInfo>'."\n";
  331. $fileContents .= ' <![CDATA['.$current_module_strings['LBL_DATE_RANGE'].' '.$dateStartDisplay.' '.$current_module_strings['LBL_DATE_RANGE_TO'].' '.$dateEndDisplay.'<BR/>'.$current_module_strings['LBL_OPP_SIZE'].' '.$symbol.'1'.$current_module_strings['LBL_OPP_THOUSANDS'].']]>'."\n";
  332. $fileContents .= ' </graphInfo>'."\n";
  333. $fileContents .= ' <chartColors ';
  334. foreach ($barChartColors as $key => $value) {
  335. $fileContents .= ' '.$key.'='.'"'.$value.'" ';
  336. }
  337. $fileContents .= ' />'."\n";
  338. $fileContents .= '</graphData>'."\n";
  339. $total = $total;
  340. $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
  341. $fileContents = $title.$fileContents;
  342. save_xml_file($cache_file_name, $fileContents);
  343. }
  344. if($chart_size=='hBarF'){
  345. $width = "800";
  346. $height = "400";
  347. } else {
  348. $width = "350";
  349. $height = "400";
  350. }
  351. $return = create_chart($chart_size,$cache_file_name,$width,$height);
  352. return $return;
  353. }
  354. function constructQuery(){
  355. global $current_user;
  356. global $timedate;
  357. //get the dates to display
  358. $user_date_start = $current_user->getPreference('mypbss_date_start');
  359. if (!empty($user_date_start) && !isset($_REQUEST['mypbss_date_start'])) {
  360. $date_start = $user_date_start;
  361. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
  362. $GLOBALS['log']->debug($user_date_start);
  363. }
  364. elseif (isset($_REQUEST['mypbss_date_start']) && $_REQUEST['mypbss_date_start'] != '') {
  365. $date_start = $_REQUEST['mypbss_date_start'];
  366. $current_user->setPreference('mypbss_date_start', $_REQUEST['mypbss_date_start']);
  367. $GLOBALS['log']->debug("_REQUEST['mypbss_date_start'] is:");
  368. $GLOBALS['log']->debug($_REQUEST['mypbss_date_start']);
  369. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_start'] is:");
  370. $GLOBALS['log']->debug($current_user->getPreference('mypbss_date_start'));
  371. }
  372. else {
  373. $date_start = $timedate->nowDate();
  374. }
  375. $user_date_end = $current_user->getPreference('mypbss_date_end');
  376. if (!empty($user_date_end) && !isset($_REQUEST['mypbss_date_end'])) {
  377. $date_end = $user_date_end;
  378. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
  379. $GLOBALS['log']->debug($user_date_end);
  380. }
  381. elseif (isset($_REQUEST['mypbss_date_end']) && $_REQUEST['mypbss_date_end'] != '') {
  382. $date_end = $_REQUEST['mypbss_date_end'];
  383. $current_user->setPreference('mypbss_date_end', $_REQUEST['mypbss_date_end']);
  384. $GLOBALS['log']->debug("_REQUEST['mypbss_date_end'] is:");
  385. $GLOBALS['log']->debug($_REQUEST['mypbss_date_end']);
  386. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] is:");
  387. $GLOBALS['log']->debug( $current_user->getPreference('mypbss_date_end'));
  388. }
  389. else {
  390. $date_end = $timedate->asUserDate($timedate->fromString("2010-01-01"));
  391. $GLOBALS['log']->debug("USER PREFERENCES['mypbss_date_end'] not found. Using: ".$date_end);
  392. }
  393. $user_id = array($current_user->id);
  394. $opp = new Opportunity;
  395. $where="";
  396. //build the where clause for the query that matches $user
  397. $count = count($user_id);
  398. $id = array();
  399. $user_list = get_user_array(false);
  400. foreach ($user_id as $key) {
  401. $new_ids[$key] = $user_list[$key];
  402. }
  403. if ($count>0) {
  404. foreach ($new_ids as $the_id=>$the_name) {
  405. $id[] = "'".$the_id."'";
  406. }
  407. $ids = join(",",$id);
  408. $where .= "opportunities.assigned_user_id IN ($ids) ";
  409. }
  410. //build the where clause for the query that matches $datax
  411. $count = count($datax);
  412. $dataxArr = array();
  413. if ($count>0) {
  414. foreach ($datax as $key=>$value) {
  415. $dataxArr[] = "'".$key."'";
  416. }
  417. $dataxArr = join(",",$dataxArr);
  418. $where .= "AND opportunities.sales_stage IN ($dataxArr) ";
  419. }
  420. //build the where clause for the query that matches $date_start and $date_end
  421. $where .= " AND opportunities.date_closed >= ". db_convert("'".$date_start."'",'date'). "
  422. AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date') ;
  423. $where .= " AND opportunities.assigned_user_id = users.id AND opportunities.deleted=0 ";
  424. //Now do the db queries
  425. //query for opportunity data that matches $datax and $user
  426. $query = " SELECT opportunities.sales_stage,
  427. users.user_name,
  428. opportunities.assigned_user_id,
  429. count( * ) AS opp_count,
  430. sum(amount_usdollar/1000) AS total
  431. FROM users,opportunities ";
  432. $query .= "WHERE " .$where;
  433. $query .= " GROUP BY opportunities.sales_stage,users.user_name,opportunities.assigned_user_id";
  434. return $query;
  435. }
  436. function constructGroupBy(){
  437. return array('sales_stage');
  438. }
  439. ?>