=== modified file 'uc_reports/uc_reports.install'
--- uc_reports/uc_reports.install	2008-03-20 23:59:58 +0000
+++ uc_reports/uc_reports.install	2009-02-06 21:20:51 +0000
@@ -1,29 +1,11 @@
 <?php
 // $Id: uc_reports.install,v 1.1 2008/02/08 22:22:40 rszrama Exp $
 
+ /**
+  * @file
+  * Install hooks for uc_reports.module.
+  */
+
 function uc_reports_install() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      break;
-    case 'pgsql':
-      db_query("CREATE OR REPLACE FUNCTION from_unixtime(integer) RETURNS timestamp AS 'SELECT $1::abstime::timestamp without time zone AS result' LANGUAGE 'SQL';");
-      db_query("CREATE OR REPLACE FUNCTION unix_timestamp() RETURNS integer AS 'SELECT ROUND(EXTRACT( EPOCH FROM abstime(now()) ))::int4 AS result;' LANGUAGE 'SQL';");
-      db_query("CREATE OR REPLACE FUNCTION unix_timestamp(timestamp with time zone) RETURNS integer AS 'SELECT ROUND(EXTRACT( EPOCH FROM ABSTIME($1) ))::int4 AS result;' LANGUAGE 'SQL';");
-      break;
-  }
-}
-
-function uc_reports_uninstall() {
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      break;
-    case 'pgsql':
-      db_query("DROP FUNCTION from_unixtime(integer);");
-      db_query("DROP FUNCTION unix_timestamp();");
-      db_query("DROP FUNCTION unix_timestamp(timestamp with time zone);");
-      break;
-  }
 }
 

=== modified file 'uc_reports/uc_reports.module'
--- uc_reports/uc_reports.module	2009-01-16 21:02:52 +0000
+++ uc_reports/uc_reports.module	2009-02-07 20:41:00 +0000
@@ -371,6 +371,8 @@
  * Display the product reports
  */
 function uc_reports_products_custom() {
+  $timezone = _uc_reports_timezone_offset();
+  $timezone_offset = time() + $timezone;
   $statistics = db_result(db_query("SELECT status FROM {system} WHERE name = 'statistics'"));
   $count_views = variable_get('statistics_count_content_views', FALSE);
   $page = (!is_null($_GET['page'])) ? intval($_GET['page']) : 0;
@@ -385,8 +387,8 @@
   // Use default report parameters if we don't detect values in the URL.
   if (arg(5) == '') {
     $args = array(
-      'start_date' => gmmktime(0, 0, 0, format_date(time(), 'custom', 'n'), 1, format_date(time(), 'custom', 'Y') - 1) - $timezone_offset,
-      'end_date' => time() - $timezone_offset,
+      'start_date' => gmmktime(0, 0, 0, gmdate('n', $timezone_offset), 1, gmdate('Y', $timezone_offset) - 1),
+      'end_date' => time(),
       'status' => FALSE,
     );
   }
@@ -533,7 +535,7 @@
 function uc_reports_products_custom_form($values, $statuses) {
   $form['search'] = array(
     '#type' => 'fieldset',
-    '#title' => t('Customize sales report parameters'),
+    '#title' => t('Customize product report parameters'),
     '#description' => t('Adjust these values and update the report to build your custom product report. Once submitted, the report may be bookmarked for easy reference in the future.'),
     '#collapsible' => TRUE,
     '#collapsed' => TRUE,
@@ -595,8 +597,8 @@
   $timezone_offset = _uc_reports_timezone_offset();
 
   // Build the start and end dates from the form.
-  $start_date = gmmktime(0, 0, 0, $form_values['start_date']['month'], $form_values['start_date']['day'], $form_values['start_date']['year']) - $timezone_offset;
-  $end_date = gmmktime(23, 59, 59, $form_values['end_date']['month'], $form_values['end_date']['day'], $form_values['end_date']['year']) - $timezone_offset;
+  $start_date = gmmktime(0, 0, 0, $form_values['start_date']['month'], $form_values['start_date']['day'], $form_values['start_date']['year']);
+  $end_date = gmmktime(23, 59, 59, $form_values['end_date']['month'], $form_values['end_date']['day'], $form_values['end_date']['year']);
 
   $args = array(
     $start_date,
@@ -610,15 +612,20 @@
 
 // Displays the sales summary report.
 function uc_reports_sales_summary() {
-  $timezone_offset = _uc_reports_timezone_offset();
+  $timezone_offset = time() + _uc_reports_timezone_offset();
   $order_statuses = _uc_reports_order_statuses();
   $format = variable_get('uc_date_format_default', 'm/d/Y');
 
-  $month_start = gmmktime(0, 0, 0, date('n'), 1, date('Y')) - $timezone_offset;
-  $month_end = gmmktime(23, 59, 59, date('n'), date('t'), date('Y')) - $timezone_offset;
-
-  $today_start = gmmktime(0, 0, 0, date('n'), date('j'), date('Y')) - $timezone_offset;
-  $today_end = gmmktime(23, 59, 59, date('n'), date('j'), date('Y')) - $timezone_offset;
+  $date_month = gmdate('n', $timezone_offset);
+  $date_year = gmdate('Y', $timezone_offset);
+  $date_day_of_month = gmdate('j', $timezone_offset);
+  $date_days_in_month = gmdate('t', $timezone_offset);
+
+  $month_start = gmmktime(0, 0, 0, $date_month, 1, $date_year);
+  $month_end = gmmktime(23, 59, 59, $date_month, $date_days_in_month, $date_year);
+
+  $today_start = gmmktime(0, 0, 0, $date_month, $date_day_of_month, $date_year);
+  $today_end = gmmktime(23, 59, 59, $date_month, $date_day_of_month, $date_year);
 
   // Build the report table header.
   $header = array(t('Sales data'), t('Number of orders'), t('Total revenue'), t('Average order'));
@@ -627,7 +634,7 @@
   $today = _uc_reports_get_sales($today_start);
 
   $rows[] = array(
-    l(t('Today, !date', array('!date' => format_date($today_start, 'custom', $format))), 'admin/store/orders/search/results/0/0/0/0/0/0/'. $today_start .'/'. $today_end),
+    l(t('Today, !date', array('!date' => format_date($today_start, 'custom', $format, 0))), 'admin/store/orders/search/results/0/0/0/0/0/0/'. $today_start .'/'. $today_end),
     $today['total'],
     uc_currency_format($today['income']),
     uc_currency_format($today['average']),
@@ -637,7 +644,7 @@
   $yesterday = _uc_reports_get_sales($today_start - 86400);
 
   $rows[] = array(
-    l(t('Yesterday, !date', array('!date' => format_date($today_start - 86400, 'custom', $format))), 'admin/store/orders/search/results/0/0/0/0/0/0/'. ($today_start - 86400) .'/'. ($today_end - 86400)),
+    l(t('Yesterday, !date', array('!date' => format_date($today_start - 86400, 'custom', $format, 0))), 'admin/store/orders/search/results/0/0/0/0/0/0/'. ($today_start - 86400) .'/'. ($today_end - 86400)),
     $yesterday['total'],
     uc_currency_format($yesterday['income']),
     uc_currency_format($yesterday['average']),
@@ -656,8 +663,8 @@
   );
 
   // Calculate the daily averages for the month.
-  $daily_orders = round($month['total'] / format_date(time(), 'custom', 'j'), 2);
-  $daily_revenue = round($month['income'] / format_date(time(), 'custom', 'j'), 2);
+  $daily_orders = round($month['total'] / $date_day_of_month, 2);
+  $daily_revenue = round($month['income'] / $date_day_of_month, 2);
 
   if ($daily_orders > 0) {
     $daily_average = round($daily_revenue / $daily_orders, 2);
@@ -675,7 +682,7 @@
   );
 
   // Store the number of days remaining in the month.
-  $remaining_days = format_date($today_start, 'custom', 't') - format_date($today_start, 'custom', 'j');
+  $remaining_days = $date_days_in_month - $date_day_of_month;
 
   // Add the projected totals for the month to the report table.
   $rows[] = array(
@@ -707,7 +714,7 @@
   $unknown = 0;
 
   // Loop through the order statuses with their total number of orders.
-  $result = db_query("SELECT s.order_status_id, s.title, s.weight, COUNT(o.order_status) as order_count FROM {uc_orders} as o LEFT JOIN {uc_order_statuses} as s ON s.order_status_id = o.order_status GROUP BY order_status, s.title, s.weight ORDER BY s.weight DESC");
+  $result = db_query("SELECT s.order_status_id, order_status, s.title, s.weight, COUNT(o.order_status) as order_count FROM {uc_orders} as o LEFT JOIN {uc_order_statuses} as s ON s.order_status_id = o.order_status GROUP BY s.order_status_id, order_status, s.title, s.weight ORDER BY s.weight DESC");
   while ($status = db_fetch_array($result)) {
     if (!empty($status['title'])) {
       // Add the total number of orders with this status to the table.
@@ -738,12 +745,12 @@
 
 // Displays the yearly sales report form and table.
 function uc_reports_sales_year() {
-  $timezone_offset = _uc_reports_timezone_offset();
+  $timezone_offset = time() + _uc_reports_timezone_offset();
   $order_statuses = _uc_reports_order_statuses();
 
   // Get the year for the report from the URL.
   if (intval(arg(5)) == 0) {
-    $year = format_date(time(), 'custom', 'Y');
+    $year = format_date($timezone_offset, 'custom', 'Y', 0);
   }
   else {
     $year = arg(5);
@@ -758,8 +765,8 @@
   // For each month of the year...
   for ($i = 1; $i <= 12; $i++) {
     // Calculate the start and end timestamps for the month in local time.
-    $month_start = gmmktime(0, 0, 0, $i, 1, $year) - $timezone_offset;
-    $month_end = gmmktime(23, 59, 59, $i + 1, 0, $year) - $timezone_offset;
+    $month_start = gmmktime(0, 0, 0, $i, 1, $year);
+    $month_end = gmmktime(23, 59, 59, $i + 1, 0, $year);
 
     // Get the sales report for the month.
     $month_sales = _uc_reports_get_sales($month_start, 'month');
@@ -790,8 +797,8 @@
   }
 
   // Calculate the start and end timestamps for the year in local time.
-  $year_start = gmmktime(0, 0, 0, 1, 1, $year) - $timezone_offset;
-  $year_end = gmmktime(23, 59, 59, 1, 0, $year + 1) - $timezone_offset;
+  $year_start = gmmktime(0, 0, 0, 1, 1, $year);
+  $year_end = gmmktime(23, 59, 59, 1, 0, $year + 1);
 
   // Get the sales report for the year.
   $year_sales = _uc_reports_get_sales($year_start, 'year');
@@ -859,14 +866,15 @@
 
 // Displays the custom sales report form and table.
 function uc_reports_sales_custom() {
-  $timezone_offset = _uc_reports_timezone_offset();
+  $timezone = _uc_reports_timezone_offset();
+  $timezone_offset = time() + $timezone;
   $format = variable_get('uc_date_format_default', 'm/d/Y');
 
   // Use default report parameters if we don't detect values in the URL.
   if (arg(5) == '') {
     $args = array(
-      'start_date' => gmmktime(0, 0, 0, format_date(time(), 'custom', 'n'), 1, format_date(time(), 'custom', 'Y') - 1) - $timezone_offset,
-      'end_date' => time() - $timezone_offset,
+      'start_date' => gmmktime(0, 0, 0, gmdate('n', $timezone_offset), 1, gmdate('Y', $timezone_offset) - 1),
+      'end_date' => time(),
       'length' => 'month',
       'status' => FALSE,
       'detail' => FALSE,
@@ -908,14 +916,14 @@
 
     // Create the date title for the subreport.
     if ($args['length'] == 'day') {
-      $date = format_date($subreport['start'] - $timezone_offset, 'custom', $format .' - D');
+      $date = format_date($subreport['start'], 'custom', $format .' - D', 0);
     }
     else {
-      $date = format_date($subreport['start'] - $timezone_offset, 'custom', $format) .' - '. format_date($subreport['end'], 'custom', $format);
+      $date = format_date($subreport['start'], 'custom', $format, 0) .' - '. format_date($subreport['end'], 'custom', $format, 0);
     }
 
     // Build the order data for the subreport.
-    $result = db_query("SELECT COUNT(*) as count, title FROM {uc_orders} LEFT JOIN {uc_order_statuses} ON order_status_id = order_status WHERE %d <= created AND created <= %d AND order_status IN $order_statuses GROUP BY order_status ORDER BY weight ASC", $subreport['start'], $subreport['end']);
+    $result = db_query("SELECT COUNT(*) as count, title FROM {uc_orders} LEFT JOIN {uc_order_statuses} ON order_status_id = order_status WHERE %d <= created AND created <= %d AND order_status IN $order_statuses GROUP BY order_status, uc_order_statuses.title, uc_order_statuses.weight ORDER BY weight ASC", $subreport['start'], $subreport['end']);
     $statuses = array();
 
     // Put the order counts into an array by status.
@@ -1007,18 +1015,18 @@
     '#type' => 'date',
     '#title' => t('Start date'),
     '#default_value' => array(
-      'month' => format_date($values['start_date'], 'custom', 'n'),
-      'day' => format_date($values['start_date'], 'custom', 'j'),
-      'year' => format_date($values['start_date'], 'custom', 'Y'),
+      'month' => format_date($values['start_date'], 'custom', 'n', 0),
+      'day' => format_date($values['start_date'], 'custom', 'j', 0),
+      'year' => format_date($values['start_date'], 'custom', 'Y', 0),
     ),
   );
   $form['search']['end_date'] = array(
     '#type' => 'date',
     '#title' => t('End date'),
     '#default_value' => array(
-      'month' => format_date($values['end_date'], 'custom', 'n'),
-      'day' => format_date($values['end_date'], 'custom', 'j'),
-      'year' => format_date($values['end_date'], 'custom', 'Y'),
+      'month' => format_date($values['end_date'], 'custom', 'n', 0),
+      'day' => format_date($values['end_date'], 'custom', 'j', 0),
+      'year' => format_date($values['end_date'], 'custom', 'Y', 0),
     ),
   );
 
@@ -1078,8 +1086,8 @@
   $timezone_offset = _uc_reports_timezone_offset();
 
   // Build the start and end dates from the form.
-  $start_date = gmmktime(0, 0, 0, $form_values['start_date']['month'], $form_values['start_date']['day'], $form_values['start_date']['year']) - $timezone_offset;
-  $end_date = gmmktime(23, 59, 59, $form_values['end_date']['month'], $form_values['end_date']['day'], $form_values['end_date']['year']) - $timezone_offset;
+  $start_date = gmmktime(0, 0, 0, $form_values['start_date']['month'], $form_values['start_date']['day'], $form_values['start_date']['year']);
+  $end_date = gmmktime(23, 59, 59, $form_values['end_date']['month'], $form_values['end_date']['day'], $form_values['end_date']['year']);
 
   $args = array(
     $start_date,
@@ -1202,54 +1210,39 @@
  *   - "total" => The total number of orders completed during the time period
  *   - "average" => The average revenue produced for each order
  */
-function _uc_reports_get_sales($time, $period = 'day') {
-  $output = array();
-  $output['income'] = 0;
+ function _uc_reports_get_sales($time, $period = 'day') {
+  $timezone = _uc_reports_timezone_offset();
+
+  // Get the current date markers.
+  $date = array(
+    'day' => format_date($time, 'custom', 'j', 0),
+    'month' => format_date($time, 'custom', 'n', 0),
+    'year' => format_date($time, 'custom', 'Y', 0),
+  );
+
+  // Add one to the granularity chosen, and use it to calc the new time.
+  $date[$period] += 1;
+  $new_time = gmmktime(0, 0, 0, $date['month'], $date['day'], $date['year']);
+
+  // Set up the default SQL for getting orders with the proper status
+  // within this period.
   $order_statuses = _uc_reports_order_statuses();
-
-  switch ($GLOBALS['db_type']) {
-    case 'mysqli':
-    case 'mysql':
-      switch ($period) {
-        default:
-        case 'day':
-          $output['date'] = format_date($time, 'custom', 'n') .'-'. format_date($time, 'custom', 'j');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE \"". format_date($time, 'custom', 'Y') .'-'. format_date($time, 'custom', 'm') .'-'. format_date($time, 'custom', 'd') ."%%\"");
-          break;
-        case 'month':
-          $output['date'] = format_date($time, 'custom', 'n');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE \"". format_date($time, 'custom', 'Y') .'-'. format_date($time, 'custom', 'm') ."%%\"");
-          break;
-        case 'year':
-          $output['date'] = format_date($time, 'custom', 'Y');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE \"". format_date($time, 'custom', 'Y') ."-%%\"");
-          break;
-    }
-    case 'pgsql':
-      switch ($period) {
-        default:
-        case 'day':
-          $output['date'] = format_date($time, 'custom', 'n') .'-'. format_date($time, 'custom', 'j');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE '". format_date($time, 'custom', 'Y') .'-'. format_date($time, 'custom', 'm') .'-'. format_date($time, 'custom', 'd') ."%%'");
-          break;
-        case 'month':
-          $output['date'] = format_date($time, 'custom', 'n');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE '". format_date($time, 'custom', 'Y') .'-'. format_date($time, 'custom', 'm') ."%%'");
-          break;
-        case 'year':
-          $output['date'] = format_date($time, 'custom', 'Y');
-          $orders = db_query("SELECT o.order_total FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND FROM_UNIXTIME(created) LIKE '". format_date($time, 'custom', 'Y') ."-%%'");
-          break;
-      }
-      break;
-  }
-
+  $sql_frag = " FROM {uc_orders} as o WHERE o.order_status IN $order_statuses AND created >= $time and created < $new_time";
+
+  // Get the total value of the orders.
+  $output = array('income' => 0);
+  $orders = db_query("SELECT o.order_total ". $sql_frag);
   while ($order = db_fetch_object($orders)) {
     $output['income'] += $order->order_total;
   }
-  $output['total'] = db_num_rows($orders);
-  $output['total'] = (!empty($output['total'])) ? $output['total'] : 0;
-  $output['average'] = ($output['total'] != 0) ? round($output['income'] / $output['total'], 2) : 0;
+ 
+  // Get the total amount of orders.
+  $count = db_result(db_query("SELECT COUNT(o.order_total) ". $sql_frag));
+  $output['total'] = $count;
+
+  // Average for this period.
+  $output['average'] = ($count != 0) ? round($output['income'] / $count, 2) : 0;
+ 
   return $output;
 }
 
@@ -1309,12 +1302,19 @@
 
   $temp = strtotime('+1 '. $interval, $time) - 1;
 
-  //if (format_date($time, 'custom', 'H') != format_date($temp, 'custom', 'H')) {
-  if (format_date($time, 'custom', 'H') == format_date($temp, 'custom', 'H')) {
-    //$temp = (format_date($time, 'custom', 'm') == 3) ? $temp + 3600 : $temp - 3600;
-    if (format_date($temp, 'custom', 'H') == '00') {
-      $temp -= 3600;
-    }
+  switch ($interval) {
+    case 'day':
+      $temp = gmmktime(0, 0, -1, gmdate('n', $time), gmdate('j', $time) + 1, gmdate('Y', $time));
+      break;
+    case 'week':
+      $temp = gmmktime(0, 0, -1, gmdate('n', $time), gmdate('j', $time) + 7, gmdate('Y', $time));
+      break;
+    case 'month':
+      $temp = gmmktime(23, 59, 59, gmdate('n', $time) + 1, 0, gmdate('Y', $time));
+      break;
+    case 'year':
+      $temp = gmmktime(23, 59, 59, gmdate('n', $time), 0, gmdate('Y', $time) + 1);
+      break;
   }
 
   return $temp;

