PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/Charts/code/Chart_outcome_by_month.php

https://bitbucket.org/cviolette/sugarcrm
PHP | 459 lines | 339 code | 58 blank | 62 comment | 61 complexity | 2f33c79c2e9917fa38736146c6c72d74 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. ********************************************************************************/
  40. require_once('include/charts/Charts.php');
  41. class Chart_outcome_by_month
  42. {
  43. var $modules = array('Opportunities');
  44. var $order = 0;
  45. function Chart_outcome_by_month()
  46. {
  47. }
  48. function draw($extra_tools)
  49. {
  50. global $app_list_strings, $current_language, $sugar_config, $currentModule, $action, $theme;
  51. $current_module_strings = return_module_language($current_language, 'Charts');
  52. if (isset($_REQUEST['obm_refresh'])) { $refresh = $_REQUEST['obm_refresh']; }
  53. else { $refresh = false; }
  54. $date_start = array();
  55. $datax = array();
  56. //get the dates to display
  57. global $current_user;
  58. $user_date_start = $current_user->getPreference('obm_date_start');
  59. if (!empty($user_date_start) && !isset($_REQUEST['obm_date_start'])) {
  60. $date_start =$user_date_start;
  61. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_start'] is:");
  62. $GLOBALS['log']->debug($user_date_start);
  63. }
  64. elseif (isset($_REQUEST['obm_year']) && $_REQUEST['obm_year'] != '') {
  65. $date_start = $_REQUEST['obm_year'].'-01-01';
  66. $current_user->setPreference('obm_date_start', $date_start);
  67. $GLOBALS['log']->debug("_REQUEST['obm_date_start'] is:");
  68. $GLOBALS['log']->debug($_REQUEST['obm_date_start']);
  69. $GLOBALS['log']->debug("_SESSION['obm_date_start'] is:");
  70. $GLOBALS['log']->debug($current_user->getPreference('obm_date_start'));
  71. }
  72. else {
  73. $date_start = date('Y').'-01-01';
  74. }
  75. $user_date_end = $current_user->getPreference('obm_date_end');
  76. if (!empty($user_date_end) && !isset($_REQUEST['obm_date_end'])) {
  77. $date_end =$user_date_end;
  78. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_end'] is:");
  79. $GLOBALS['log']->debug($date_end);
  80. }
  81. elseif (isset($_REQUEST['obm_year']) && $_REQUEST['obm_year'] != '') {
  82. $date_end = $_REQUEST['obm_year'].'-12-31';
  83. $current_user->setPreference('obm_date_end', $date_end );
  84. $GLOBALS['log']->debug("_REQUEST['obm_date_end'] is:");
  85. $GLOBALS['log']->debug($_REQUEST['obm_date_end']);
  86. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_end'] is:");
  87. $GLOBALS['log']->debug($current_user->getPreference('obm_date_end'));
  88. }
  89. else {
  90. $date_end = date('Y').'-12-31';
  91. }
  92. $ids = array();
  93. //get list of user ids for which to display data
  94. $user_ids = $current_user->getPreference('obm_ids');
  95. if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['obm_ids'])) {
  96. $ids = $user_ids;
  97. $GLOBALS['log']->debug("USER PREFERENCES['obm_ids'] is:");
  98. $GLOBALS['log']->debug($user_ids);
  99. }
  100. elseif (isset($_REQUEST['obm_ids']) && count($_REQUEST['obm_ids']) > 0) {
  101. $ids = $_REQUEST['obm_ids'];
  102. $current_user->setPreference('obm_ids', $_REQUEST['obm_ids']);
  103. $GLOBALS['log']->debug("_REQUEST['obm_ids'] is:");
  104. $GLOBALS['log']->debug($_REQUEST['obm_ids']);
  105. $GLOBALS['log']->debug("USER PREFRENCES['obm_ids'] is:");
  106. $GLOBALS['log']->debug($current_user->getPreference('obm_ids'));
  107. }
  108. else {
  109. $ids = get_user_array(false);
  110. $ids = array_keys($ids);
  111. }
  112. //create unique prefix based on selected users for image files
  113. $id_hash = '1';
  114. if (isset($ids)) {
  115. sort($ids);
  116. $id_hash = crc32(implode('',$ids));
  117. if($id_hash < 0)
  118. {
  119. $id_hash = $id_hash * -1;
  120. }
  121. }
  122. $GLOBALS['log']->debug("ids is:");
  123. $GLOBALS['log']->debug($ids);
  124. $id_md5 = substr(md5($current_user->id),0,9);
  125. // cn: format date_start|end to user's preferred
  126. global $timedate;
  127. $dateDisplayStart = strftime($timedate->get_user_date_format(), strtotime($date_start));
  128. $dateDisplayEnd = strftime($timedate->get_user_date_format(), strtotime($date_end));
  129. $seps = array("-", "/");
  130. $dates = array($date_start, $date_end);
  131. $dateFileNameSafe = str_replace($seps, "_", $dates);
  132. $cache_file_name = sugar_cached("xml/").$current_user->getUserPrivGuid()."_outcome_by_month_".$dateFileNameSafe[0]."_".$dateFileNameSafe[1].".xml";
  133. $GLOBALS['log']->debug("cache file name is: $cache_file_name");
  134. global $app_strings;
  135. $tools='<div align="right"><a href="index.php?module='.$currentModule.'&action='. $action .'&obm_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(\'outcome_by_month_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>&nbsp;&nbsp;'.$extra_tools.'</div>';
  136. ?>
  137. <?php echo '<span onmouseover="this.style.cursor=\'move\'" id="chart_handle_' . $this->order . '">' . get_form_header($current_module_strings['LBL_YEAR_BY_OUTCOME'],$tools,false) . '</span>';?>
  138. <?php
  139. $cal_lang = "en";
  140. $cal_dateformat = parse_calendardate($app_strings['NTC_DATE_FORMAT']);
  141. if (empty($_SESSION['obm_ids'])) $_SESSION['obm_ids'] = "";
  142. ?>
  143. <p>
  144. <div id='outcome_by_month_edit' style='display: none;'>
  145. <form name="outcome_by_month" action="index.php" method="post" >
  146. <input type="hidden" name="module" value="<?php echo $currentModule;?>">
  147. <input type="hidden" name="action" value="<?php echo $action;?>">
  148. <input type="hidden" name="obm_refresh" value="true">
  149. <input type="hidden" name="obm_date_start" value="<?php if (isset($_SESSION['obm_date_start'])) echo $_SESSION['obm_date_start']?>">
  150. <input type="hidden" name="obm_date_end" value="<?php if (isset($_SESSION['obm_date_end'])) echo $_SESSION['obm_date_end']?>">
  151. <table cellpadding="0" cellspacing="0" border="0" class="edit view" align="center">
  152. <tr>
  153. <td valign='top' nowrap ><b><?php echo $current_module_strings['LBL_YEAR']?></b><br><span class="dateFormat"><?php echo $app_strings['NTC_YEAR_FORMAT']?></span></td>
  154. <td valign='top' ><input class="text" name="obm_year" size='12' maxlength='10' id='obm_year' value='<?php if (isset($date_start)) echo substr($date_start,0,4)?>'>&nbsp;&nbsp;</td>
  155. <td valign='top'><b><?php echo $current_module_strings['LBL_USERS'];?></b></td>
  156. <td valign='top'><select name="obm_ids[]" multiple size='3'><?php echo get_select_options_with_id(get_user_array(false),$ids); ?></select></td>
  157. <td align="right" valign="top"><input class="button" onclick="return verify_chart_data_outcome_by_month();" 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('outcome_by_month_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>
  158. </tr>
  159. </table>
  160. </form>
  161. </div>
  162. </p>
  163. <?php
  164. // draw chart
  165. echo "<p align='center'>".$this->gen_xml($date_start, $date_end, $ids, $cache_file_name, $refresh,$current_module_strings)."</p>";
  166. echo "<P align='center'><span class='chartFootnote'>".$current_module_strings['LBL_MONTH_BY_OUTCOME_DESC']."</span></P>";
  167. ?>
  168. <?php
  169. if (file_exists($cache_file_name)) {
  170. $file_date = $timedate->asUser($timedate->fromTimestamp(filemtime($cache_file_name)));
  171. }
  172. else {
  173. $file_date = '';
  174. }
  175. ?>
  176. <span class='chartFootnote'>
  177. <p align="right"><i><?php echo $current_module_strings['LBL_CREATED_ON'].' '.$file_date; ?></i></p>
  178. </span>
  179. <?php
  180. echo get_validate_chart_js();
  181. }
  182. /**
  183. * Creates opportunity pipeline image as a VERTICAL accumlated bar graph for multiple users.
  184. * param $datax- the month data to display in the x-axis
  185. * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
  186. * All Rights Reserved..
  187. * Contributor(s): ______________________________________..
  188. */
  189. function gen_xml($date_start='1971-10-15', $date_end='2010-10-15', $user_id=array('1'), $cache_file_name='a_file', $refresh=false,$current_module_strings) {
  190. global $app_strings, $app_list_strings, $charset, $lang, $barChartColors, $current_user;
  191. $kDelim = $current_user->getPreference('num_grp_sep');
  192. global $timedate;
  193. if (!file_exists($cache_file_name) || $refresh == true) {
  194. $GLOBALS['log']->debug("date_start is: $date_start");
  195. $GLOBALS['log']->debug("date_end is: $date_end");
  196. $GLOBALS['log']->debug("user_id is: ");
  197. $GLOBALS['log']->debug($user_id);
  198. $GLOBALS['log']->debug("cache_file_name is: $cache_file_name");
  199. $where = "";
  200. //build the where clause for the query that matches $user
  201. $count = count($user_id);
  202. $id = array();
  203. if ($count>0) {
  204. foreach ($user_id as $the_id) {
  205. $id[] = "'".$the_id."'";
  206. }
  207. $ids = join(",",$id);
  208. $where .= "opportunities.assigned_user_id IN ($ids) ";
  209. }
  210. // cn: adding user-pref date handling
  211. $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
  212. $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
  213. $opp = new Opportunity();
  214. //build the where clause for the query that matches $date_start and $date_end
  215. $where .= "AND opportunities.date_closed >= ".db_convert("'".$date_start."'",'date')." AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date')." AND opportunities.deleted=0";
  216. $query = "SELECT sales_stage,".db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'"))." as m, sum(amount_usdollar/1000) as total, count(*) as opp_count FROM opportunities ";
  217. $query .= "WHERE ".$where;
  218. $query .= " GROUP BY sales_stage,".db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'"))."ORDER BY m";
  219. //Now do the db queries
  220. //query for opportunity data that matches $datay and $user
  221. //_pp($query);
  222. $result = $opp->db->query($query, true);
  223. //build pipeline by sales stage data
  224. $total = 0;
  225. $div = 1;
  226. global $sugar_config;
  227. $symbol = $sugar_config['default_currency_symbol'];
  228. $other = $current_module_strings['LBL_LEAD_SOURCE_OTHER'];
  229. $rowTotalArr = array();
  230. $rowTotalArr[] = 0;
  231. global $current_user;
  232. $salesStages = array("Closed Lost"=>$app_list_strings['sales_stage_dom']["Closed Lost"],"Closed Won"=>$app_list_strings['sales_stage_dom']["Closed Won"],"Other"=>$other);
  233. if($current_user->getPreference('currency') ){
  234. $currency = new Currency();
  235. $currency->retrieve($current_user->getPreference('currency'));
  236. $div = $currency->conversion_rate;
  237. $symbol = $currency->symbol;
  238. }
  239. $months = array();
  240. $monthArr = array();
  241. while($row = $opp->db->fetchByAssoc($result, false))
  242. {
  243. if($row['total']*$div<=100){
  244. $sum = round($row['total']*$div, 2);
  245. } else {
  246. $sum = round($row['total']*$div);
  247. }
  248. if($row['sales_stage'] == 'Closed Won' || $row['sales_stage'] == 'Closed Lost'){
  249. $salesStage = $row['sales_stage'];
  250. $salesStageT = $app_list_strings['sales_stage_dom'][$row['sales_stage']];
  251. } else {
  252. $salesStage = "Other";
  253. $salesStageT = $other;
  254. }
  255. $months[$row['m']] = $row['m'];
  256. if(!isset($monthArr[$row['m']]['row_total'])) {$monthArr[$row['m']]['row_total']=0;}
  257. $monthArr[$row['m']][$salesStage]['opp_count'][] = $row['opp_count'];
  258. $monthArr[$row['m']][$salesStage]['total'][] = $sum;
  259. $monthArr[$row['m']]['outcome'][$salesStage]=$salesStageT;
  260. $monthArr[$row['m']]['row_total'] += $sum;
  261. $total += $sum;
  262. }
  263. $fileContents = ' <xData length="20">'."\n";
  264. if (!empty($months)) {
  265. foreach ($months as $month){
  266. $rowTotalArr[]=$monthArr[$month]['row_total'];
  267. if($monthArr[$month]['row_total']>100)
  268. {
  269. $monthArr[$month]['row_total']=round($monthArr[$month]['row_total']);
  270. }
  271. $fileContents .= ' <dataRow title="'.$month.'" endLabel="'.currency_format_number($monthArr[$month]['row_total'], array('currency_symbol' => true)).'">'."\n";
  272. arsort($salesStages);
  273. foreach ($salesStages as $outcome=>$outcome_translation){
  274. if(isset($monthArr[$month][$outcome])) {
  275. $fileContents .= ' <bar id="'.$outcome.'" totalSize="'.array_sum($monthArr[$month][$outcome]['total']).'" altText="'.$month.': '.format_number(array_sum($monthArr[$month][$outcome]['opp_count']), 0, 0).' '.$current_module_strings['LBL_OPPS_WORTH'].' '.currency_format_number(array_sum($monthArr[$month][$outcome]['total']),array('currency_symbol' => true)).$current_module_strings['LBL_OPP_THOUSANDS'].' '.$current_module_strings['LBL_OPPS_OUTCOME'].' '.$outcome_translation.'" url="index.php?module=Opportunities&action=index&date_closed='.$month.'&sales_stage='.urlencode($outcome).'&query=true&searchFormTab=advanced_search"/>'."\n";
  276. }
  277. }
  278. $fileContents .= ' </dataRow>'."\n";
  279. }
  280. } else {
  281. $fileContents .= ' <dataRow title="" endLabel="">'."\n";
  282. $fileContents .= ' <bar id="" totalSize="0" altText="" url=""/>'."\n";
  283. $fileContents .= ' </dataRow>'."\n";
  284. $rowTotalArr[] = 1000;
  285. }
  286. $fileContents .= ' </xData>'."\n";
  287. $max = get_max($rowTotalArr);
  288. $fileContents .= ' <yData min="0" max="'.$max.'" length="10" prefix="'.$symbol.'" suffix="" kDelim="'.$kDelim.'" defaultAltText="'.$current_module_strings['LBL_ROLLOVER_DETAILS'].'"/>'."\n";
  289. $fileContents .= ' <colorLegend status="on">'."\n";
  290. $i=0;
  291. asort($salesStages);
  292. foreach ($salesStages as $outcome=>$outcome_translation) {
  293. $color = generate_graphcolor($outcome,$i);
  294. $fileContents .= ' <mapping id="'.$outcome.'" name="'.$outcome_translation.'" color="'.$color.'"/>'."\n";
  295. $i++;
  296. }
  297. $fileContents .= ' </colorLegend>'."\n";
  298. $fileContents .= ' <graphInfo>'."\n";
  299. $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";
  300. $fileContents .= ' </graphInfo>'."\n";
  301. $fileContents .= ' <chartColors ';
  302. foreach ($barChartColors as $key => $value) {
  303. $fileContents .= ' '.$key.'='.'"'.$value.'" ';
  304. }
  305. $fileContents .= ' />'."\n";
  306. $fileContents .= '</graphData>'."\n";
  307. $total = round($total, 2);
  308. $title = '<graphData title="'.$current_module_strings['LBL_TOTAL_PIPELINE'].currency_format_number($total, array('currency_symbol' => true)).$app_strings['LBL_THOUSANDS_SYMBOL'].'">'."\n";
  309. $fileContents = $title.$fileContents;
  310. //echo $fileContents;
  311. save_xml_file($cache_file_name, $fileContents);
  312. }
  313. $return = create_chart('vBarF',$cache_file_name);
  314. return $return;
  315. }
  316. function constructQuery(){
  317. global $current_user;
  318. global $timedate;
  319. $user_date_start = $current_user->getPreference('obm_date_start');
  320. if (!empty($user_date_start) && !isset($_REQUEST['obm_date_start'])) {
  321. $date_start =$user_date_start;
  322. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_start'] is:");
  323. $GLOBALS['log']->debug($user_date_start);
  324. }
  325. elseif (isset($_REQUEST['obm_year']) && $_REQUEST['obm_year'] != '') {
  326. $date_start = $_REQUEST['obm_year'].'-01-01';
  327. $current_user->setPreference('obm_date_start', $date_start);
  328. $GLOBALS['log']->debug("_REQUEST['obm_date_start'] is:");
  329. $GLOBALS['log']->debug($_REQUEST['obm_date_start']);
  330. $GLOBALS['log']->debug("_SESSION['obm_date_start'] is:");
  331. $GLOBALS['log']->debug($current_user->getPreference('obm_date_start'));
  332. }
  333. else {
  334. $date_start = date('Y').'-01-01';
  335. }
  336. $user_date_end = $current_user->getPreference('obm_date_end');
  337. if (!empty($user_date_end) && !isset($_REQUEST['obm_date_end'])) {
  338. $date_end =$user_date_end;
  339. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_end'] is:");
  340. $GLOBALS['log']->debug($date_end);
  341. }
  342. elseif (isset($_REQUEST['obm_year']) && $_REQUEST['obm_year'] != '') {
  343. $date_end = $_REQUEST['obm_year'].'-12-31';
  344. $current_user->setPreference('obm_date_end', $date_end );
  345. $GLOBALS['log']->debug("_REQUEST['obm_date_end'] is:");
  346. $GLOBALS['log']->debug($_REQUEST['obm_date_end']);
  347. $GLOBALS['log']->debug("USER PREFERENCES['obm_date_end'] is:");
  348. $GLOBALS['log']->debug($current_user->getPreference('obm_date_end'));
  349. }
  350. else {
  351. $date_end = date('Y').'-12-31';
  352. }
  353. $ids = array();
  354. //get list of user ids for which to display data
  355. $user_ids = $current_user->getPreference('obm_ids');
  356. if (!empty($user_ids) && count($user_ids) != 0 && !isset($_REQUEST['obm_ids'])) {
  357. $ids = $user_ids;
  358. $GLOBALS['log']->debug("USER PREFERENCES['obm_ids'] is:");
  359. $GLOBALS['log']->debug($user_ids);
  360. }
  361. elseif (isset($_REQUEST['obm_ids']) && count($_REQUEST['obm_ids']) > 0) {
  362. $ids = $_REQUEST['obm_ids'];
  363. $current_user->setPreference('obm_ids', $_REQUEST['obm_ids']);
  364. $GLOBALS['log']->debug("_REQUEST['obm_ids'] is:");
  365. $GLOBALS['log']->debug($_REQUEST['obm_ids']);
  366. $GLOBALS['log']->debug("USER PREFRENCES['obm_ids'] is:");
  367. $GLOBALS['log']->debug($current_user->getPreference('obm_ids'));
  368. }
  369. else {
  370. $ids = get_user_array(false);
  371. $ids = array_keys($ids);
  372. }
  373. $user_id = $ids;
  374. $where = "";
  375. //build the where clause for the query that matches $user
  376. $count = count($user_id);
  377. $id = array();
  378. if ($count>0) {
  379. foreach ($user_id as $the_id) {
  380. $id[] = "'".$the_id."'";
  381. }
  382. $ids = join(",",$id);
  383. $where .= "opportunities.assigned_user_id IN ($ids) ";
  384. }
  385. // cn: adding user-pref date handling
  386. $dateStartDisplay = $timedate->asUserDate($timedate->fromString($date_start));
  387. $dateEndDisplay = $timedate->asUserDate($timedate->fromString($date_end));
  388. $opp = new Opportunity();
  389. //build the where clause for the query that matches $date_start and $date_end
  390. $where .= "AND opportunities.date_closed >= ".db_convert("'".$date_start."'",'date')." AND opportunities.date_closed <= ".db_convert("'".$date_end."'",'date')." AND opportunities.deleted=0";
  391. $query = "SELECT sales_stage,".db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'"))." as m, sum(amount_usdollar/1000) as total, count(*) as opp_count FROM opportunities ";
  392. $query .= "WHERE ".$where;
  393. $query .= " GROUP BY sales_stage,".db_convert('opportunities.date_closed','date_format',array("'%Y-%m'"),array("'YYYY-MM'"))."ORDER BY m";
  394. return $query;
  395. }
  396. function constructGroupBy(){
  397. return array( 'm', 'sales_stage', );
  398. }
  399. }
  400. ?>