/[drupal]/contributions/modules/ubercart/uc_reports/uc_reports.module
ViewVC logotype

Contents of /contributions/modules/ubercart/uc_reports/uc_reports.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Thu Jul 10 12:41:04 2008 UTC (16 months, 2 weeks ago) by islandusurper
Branch: MAIN
CVS Tags: DRUPAL-5--1-1, DRUPAL-5--1-2, HEAD
Branch point for: DRUPAL-5, DRUPAL-6--2
Changes since 1.10: +0 -0 lines
File MIME type: text/x-php
Begin the Ubercart 6.x-2.x branch.
1 <?php
2 // $Id$
3
4 /**
5 * @file
6 * Displays reports on sales, customers, and products to store admin
7 *
8 * The reports module creates a few table-based reports for an ubercart store.
9 * Store admins can view reports for sales between different dates, customers
10 * by the products purchased and money spent, and products by their views,
11 * number sold, and revenue produced.
12 *
13 * Development sponsored by the Ubercart project. http://www.ubercart.org
14 */
15
16 define('UC_REPORTS_MAX_RECORDS', PHP_INT_MAX);
17
18 /* ************************************************************************* *
19 * Hook Functions (Drupal) *
20 * ************************************************************************* */
21
22 /**
23 * Implementation of hook_help().
24 */
25 function uc_reports_help($section) {
26 if (!strncmp($section, 'admin/store/reports/', 20)) {
27 $statuses = array();
28 foreach (variable_get('uc_reports_reported_statuses', array('completed')) as $status) {
29 $statuses[] = db_result(db_query("SELECT title FROM {uc_order_statuses} WHERE order_status_id = '%s'", $status));
30 }
31 $order_statuses = t('<b>Order statuses used:</b> @statuses', array('@statuses' => implode(', ', $statuses)));
32
33 switch ($section) {
34 case 'admin/store/reports/customers':
35 return '<p>'. t("The following are total orders, products, sales, and average order totals for each store customer. Clicking on the header links will toggle a descending or ascending order for that column. Clicking on a customer's name will take you to a detailed list of orders that customer has made. Clicking on a customers username will take you to their account page.") .'</p><p>'. $order_statuses .'</p>';
36 case 'admin/store/reports/products':
37 return '<p>'. t('The table lists each product listed in the store, its amount sold, how many times it has been viewed, revenue it has produced, and gross profit it has generated. If you do not see the number of views you must enable the Statistics module on the <a href="!url">module administration page</a>.', array('!url' => url('admin/build/modules'))) .'</p><p>'. $order_statuses .'</p>';
38 case 'admin/store/reports/sales':
39 return '<p>'. t('These are the sales for the last two days, average sales for the month, and the projected sales for the rest of the month. Further down the list you will see other sales statistics.') .'</p><p>'. $order_statuses .'</p>';
40 case 'admin/store/reports/sales/custom':
41 return t('Expand the fieldset below to customize the date range of this report, the statuses of orders displayed, and product display options.');
42 }
43
44 if (strpos($section, 'admin/store/reports/sales/year') === 0) {
45 $year = (is_null($_GET['year'])) ? format_date(time(), 'custom', "Y") : $_GET['year'];
46 return '<p>'. t('This is the monthly break down of sales for the year @year. Clicking on each link will take you to a list of orders during that month.', array('@year' => $year)) .'</p><p>'. $order_statuses .'</p>';
47 }
48 }
49 }
50
51 /**
52 * Implementation of hook_menu().
53 */
54 function uc_reports_menu($may_cache) {
55 global $user;
56 $items = array();
57
58 if ($may_cache) {
59 $items[] = array(
60 'path' => 'admin/store/settings/reports',
61 'title' => t('Report settings'),
62 'description' => t('View the report settings.'),
63 'callback' => 'drupal_get_form',
64 'callback arguments' => array('uc_reports_settings_overview'),
65 'access' => user_access('administer store'),
66 'type' => MENU_NORMAL_ITEM,
67 );
68 $items[] = array(
69 'path' => 'admin/store/reports/customers',
70 'title' => t('Customer reports'),
71 'description' => t('View reports for store customers'),
72 'callback' => 'uc_reports_customers',
73 'access' => user_access('view reports'),
74 'type' => MENU_NORMAL_ITEM,
75 );
76 $items[] = array(
77 'path' => 'admin/store/reports/products',
78 'title' => t('Product reports'),
79 'description' => t('View reports for store products'),
80 'callback' => 'uc_reports_products',
81 'access' => user_access('view reports'),
82 'type' => MENU_NORMAL_ITEM,
83 );
84 $items[] = array(
85 'path' => 'admin/store/reports/sales',
86 'title' => t('Sales reports'),
87 'description' => t('View reports for store sales'),
88 'callback' => 'uc_reports_sales_summary',
89 'access' => user_access('view reports'),
90 'type' => MENU_NORMAL_ITEM,
91 );
92 $items[] = array(
93 'path' => 'admin/store/reports/sales/summary',
94 'title' => t('Sales summary'),
95 'description' => t('View summary of all store sales'),
96 'access' => user_access('view reports'),
97 'type' => MENU_DEFAULT_LOCAL_TASK,
98 'weight' => -10,
99 );
100 $items[] = array(
101 'path' => 'admin/store/reports/sales/year',
102 'title' => t('Sales per year'),
103 'description' => t('View store sales for a particular year'),
104 'callback' => 'uc_reports_sales_year',
105 'access' => user_access('view reports'),
106 'type' => MENU_LOCAL_TASK,
107 'weight' => -7,
108 );
109 $items[] = array(
110 'path' => 'admin/store/reports/sales/custom',
111 'title' => t('Custom sales summary'),
112 'description' => t('View a customized sales summary'),
113 'callback' => 'uc_reports_sales_custom',
114 'access' => user_access('view reports'),
115 'type' => MENU_LOCAL_TASK,
116 'weight' => -1,
117 );
118 }
119 else {
120 $items[] = array(
121 'path' => 'admin/store/reports/getcsv/'. arg(4) .'/'. arg(5),
122 'callback' => '_uc_reports_get_csv',
123 'callback arguments' => array(arg(4), arg(5)),
124 'access' => user_access('view reports'),
125 'type' => MENU_CALLBACK,
126 );
127 drupal_add_css(drupal_get_path('module', 'uc_reports') .'/uc_reports.css');
128 }
129
130 return $items;
131 }
132
133 /**
134 * Implementation of hook_perm
135 */
136 function uc_reports_perm() {
137 return array('view reports');
138 }
139
140 /* ************************************************************************* *
141 * Callback Functions, Forms, and Tables *
142 * ************************************************************************* */
143
144 /**
145 * Display the customer report
146 */
147 function uc_reports_customers() {
148 $address_preference = variable_get('uc_customer_list_address', 'billing');
149 $first_name = ($address_preference == 'billing') ? 'billing_first_name' : 'delivery_first_name';
150 $last_name = ($address_preference == 'billing') ? 'billing_last_name' : 'delivery_last_name';
151 $page = (!is_null($_GET['page'])) ? intval($_GET['page']) : 0;
152 $page_size = (!is_null($_GET['nopage'])) ? UC_REPORTS_MAX_RECORDS : variable_get('uc_reports_table_size', 30);
153 $order_statuses = _uc_reports_order_statuses();
154 $csv_rows = array();
155
156 $header = array(
157 array('data' => t('#')),
158 array('data' => t('Customer'), 'field' => "ou.$last_name"),
159 array('data' => t('Username'), 'field' => "u.name"),
160 array('data' => t('Orders'), 'field' => 'orders'),
161 array('data' => t('Products'), 'field' => 'products'),
162 array('data' => t('Total'), 'field' => 'total', 'sort' => 'desc'),
163 array('data' => t('Average'), 'field' => 'average'),
164 );
165 $csv_rows[] = array(t('#'), t('Customer'), t('Username'), t('Orders'), t('Products'), t('Total'), t('Average'));
166
167 $sql = '';
168 $sql_count = '';
169
170 switch ($GLOBALS['db_type']) {
171 case 'mysqli':
172 case 'mysql':
173 $sql = "SELECT u.uid, u.name, ou.$first_name, ou.$last_name, (SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses) as orders, (SELECT SUM(qty) FROM {uc_order_products} as ps LEFT JOIN {uc_orders} as os ON ps.order_id = os.order_id WHERE os.order_status IN $order_statuses AND os.uid = u.uid) as products, (SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses) as total, ROUND((SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses)/(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses), 2) as average FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0 GROUP BY u.uid";
174 $sql_count = "SELECT COUNT(DISTINCT(u.uid)) FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0";
175 break;
176 case 'pgsql':
177 $sql = "SELECT u.uid, u.name, ou.$first_name, ou.$last_name, (SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses) as orders, (SELECT SUM(qty) FROM {uc_order_products} as ps LEFT JOIN {uc_orders} as os ON ps.order_id = os.order_id WHERE os.order_status IN $order_statuses AND os.uid = u.uid) as products, (SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses) as total, ROUND((SELECT SUM(ot.order_total) FROM {uc_orders} as ot WHERE ot.uid = u.uid AND ot.order_status IN $order_statuses)/(SELECT COUNT(DISTINCT(order_id)) FROM {uc_orders} as o WHERE o.uid = u.uid AND o.order_status IN $order_statuses), 2) as average FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0 GROUP BY u.uid, u.name, ou.$first_name, ou.$last_name";
178 $sql_count = "SELECT COUNT(DISTINCT(u.uid)) FROM {users} as u LEFT JOIN {uc_orders} as ou ON u.uid = ou.uid WHERE u.uid > 0";
179 break;
180 }
181
182 $customers = pager_query($sql . tablesort_sql($header), $page_size, 0, $sql_count);
183
184 while ($customer = db_fetch_array($customers)) {
185 $name = (!empty($customer[$last_name]) || !empty($customer[$first_name])) ? l($customer[$last_name] .', '. $customer[$first_name], 'admin/store/customers/orders/'. $customer['uid']) : l($customer['name'], 'admin/store/customers/orders/'. $customer['uid']);
186 $customer_number = ($page * variable_get('uc_reports_table_size', 30)) + (count($rows) + 1);
187 $customer_order_name = (!empty($customer[$last_name]) || !empty($customer[$first_name])) ? $customer[$last_name] .', '. $customer[$first_name] : $customer['name'];
188 $customer_name = $customer['name'];
189 $orders = (!empty($customer['orders']) ? $customer['orders'] : 0);
190 $products = (!empty($customer['products']) ? $customer['products'] : 0);
191 $total_revenue = uc_currency_format($customer['total']);
192 $average_revenue = uc_currency_format($customer['average']);
193 $rows[] = array(
194 array('data' => $customer_number),
195 array('data' => $name, 'nowrap' => 'nowrap'),
196 array('data' => l($customer_name, 'user/'. $customer['uid']), 'nowrap' => 'nowrap'),
197 array('data' => $orders),
198 array('data' => $products),
199 array('data' => $total_revenue, 'nowrap' => 'nowrap'),
200 array('data' => $average_revenue, 'nowrap' => 'nowrap'),
201 );
202 $csv_rows[] = array($customer_number, $customer_order_name, $customer_name, $orders, $products, $total_revenue, $average_revenue);
203 }
204 if (empty($rows)) {
205 $rows[] = array(array('data' => t('No customers found'), 'colspan' => count($header)));
206 }
207 $csv_data = uc_reports_store_csv('uc_customers', $csv_rows);
208
209 $output = theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table'));
210 $output .= theme_pager(NULL, $page_size);
211 $output .= '<div class="uc-reports-links">'. l(t('Export to CSV file.'), 'admin/store/reports/getcsv/'. $csv_data['report'] .'/'. $csv_data['user']) .'&nbsp;&nbsp;&nbsp;'. ((!is_null($_GET['nopage'])) ? l(t('Show paged records'), 'admin/store/reports/customers') : l(t('Show all records'), 'admin/store/reports/customers', array(), 'nopage=1')) .'</div>';
212
213 return $output;
214 }
215
216 /**
217 * Display the product reports
218 */
219 function uc_reports_products() {
220 $statistics = db_result(db_query("SELECT status FROM {system} WHERE name = 'statistics'"));
221 $count_views = variable_get('statistics_count_content_views', FALSE);
222 $page = (!is_null($_GET['page'])) ? intval($_GET['page']) : 0;
223 $page_size = (!is_null($_GET['nopage'])) ? UC_REPORTS_MAX_RECORDS : variable_get('uc_reports_table_size', 30);
224 $order_statuses = _uc_reports_order_statuses();
225 $product_types = array("'product'");
226 $types = db_query("SELECT DISTINCT(pcid) FROM {uc_product_classes}");
227 $csv_rows = array();
228 while ($type = db_fetch_object($types)) {
229 $product_types[] = "'". $type->pcid ."'";
230 }
231
232 if ($statistics && $count_views) {
233 $header = array(
234 array('data' => t('#')),
235 array('data' => t('Product'), 'field' => 'n.title'),
236 array('data' => t('Views'), 'field' => 'c.totalcount'),
237 array('data' => t('Sold'), 'field' => 'sold'),
238 array('data' => t('Revenue'), 'field' => 'revenue', 'sort' => 'desc'),
239 array('data' => t('Gross'), 'field' => 'gross'),
240 );
241 $csv_rows[] = array(t('#'), t('Product'), t('Views'), t('Sold'), t('Revenue'), t('Gross'));
242
243 $sql = '';
244 switch ($GLOBALS['db_type']) {
245 case 'mysqli':
246 case 'mysql':
247 $sql = "SELECT n.nid, n.title, c.totalcount, (SELECT SUM(qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.nid = n.nid) AS sold, (SELECT (SUM(p2.price * p2.qty)) FROM {uc_order_products} AS p2 LEFT JOIN {uc_orders} AS o2 ON p2.order_id = o2.order_id WHERE o2.order_status IN $order_statuses AND p2.nid = n.nid) AS revenue, (SELECT (SUM(p3.price * p3.qty) - SUM(p3.cost * p3.qty)) FROM {uc_order_products} AS p3 LEFT JOIN {uc_orders} AS o3 ON p3.order_id = o3.order_id WHERE o3.order_status IN $order_statuses AND p3.nid = n.nid) AS gross FROM {node} AS n LEFT JOIN {node_counter} AS c ON n.nid = c.nid WHERE type IN (". implode(", ", $product_types) .") GROUP BY n.nid DESC";
248 break;
249 case 'pgsql':
250 $sql = "SELECT n.nid, n.title, c.totalcount, (SELECT SUM(qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.nid = n.nid) AS sold, (SELECT (SUM(p2.price * p2.qty)) FROM {uc_order_products} AS p2 LEFT JOIN {uc_orders} AS o2 ON p2.order_id = o2.order_id WHERE o2.order_status IN $order_statuses AND p2.nid = n.nid) AS revenue, (SELECT (SUM(p3.price * p3.qty) - SUM(p3.cost * p3.qty)) FROM {uc_order_products} AS p3 LEFT JOIN {uc_orders} AS o3 ON p3.order_id = o3.order_id WHERE o3.order_status IN $order_statuses AND p3.nid = n.nid) AS gross FROM {node} AS n LEFT JOIN {node_counter} AS c ON n.nid = c.nid WHERE type IN (". implode(", ", $product_types) .") GROUP BY n.nid";
251 break;
252 }
253 }
254 else {
255 $header = array(
256 array('data' => t('#')),
257 array('data' => t('Product'), 'field' => 'n.title'),
258 array('data' => t('Sold'), 'field' => 'sold'),
259 array('data' => t('Revenue'), 'field' => 'revenue', 'sort' => 'desc'),
260 array('data' => t('Gross'), 'field' => 'gross'),
261 );
262 $csv_rows[] = array(t('#'), t('Product'), t('Sold'), t('Revenue'), t('Gross'));
263
264
265 switch ($GLOBALS['db_type']) {
266 case 'mysqli':
267 case 'mysql':
268 $sql = "SELECT n.nid, n.title, (SELECT SUM(qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.nid = n.nid) AS sold, (SELECT (SUM(p2.price * p2.qty)) FROM {uc_order_products} AS p2 LEFT JOIN {uc_orders} AS o2 ON p2.order_id = o2.order_id WHERE o2.order_status IN $order_statuses AND p2.nid = n.nid) AS revenue, (SELECT (SUM(p3.price * p3.qty) - SUM(p3.cost * p3.qty)) FROM {uc_order_products} AS p3 LEFT JOIN {uc_orders} AS o3 ON p3.order_id = o3.order_id WHERE o3.order_status IN $order_statuses AND p3.nid = n.nid) AS gross FROM {node} AS n WHERE type IN (". implode(', ', $product_types) .') GROUP BY n.nid DESC';
269 break;
270 case 'pgsql':
271 $sql = "SELECT n.nid, n.title, (SELECT SUM(qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.nid = n.nid) AS sold, (SELECT (SUM(p2.price * p2.qty)) FROM {uc_order_products} AS p2 LEFT JOIN {uc_orders} AS o2 ON p2.order_id = o2.order_id WHERE o2.order_status IN $order_statuses AND p2.nid = n.nid) AS revenue, (SELECT (SUM(p3.price * p3.qty) - SUM(p3.cost * p3.qty)) FROM {uc_order_products} AS p3 LEFT JOIN {uc_orders} AS o3 ON p3.order_id = o3.order_id WHERE o3.order_status IN $order_statuses AND p3.nid = n.nid) AS gross FROM {node} AS n WHERE type IN (". implode(', ', $product_types) .') GROUP BY n.nid, n.title';
272 break;
273 }
274 }
275
276 $sql_count = "SELECT COUNT(nid) FROM {node} WHERE type IN (". implode(", ", $product_types) .")";
277 $products = pager_query($sql . tablesort_sql($header), $page_size, 0, $sql_count);
278
279 while ($product = db_fetch_array($products)) {
280 $row_cell = ($page * variable_get('uc_reports_table_size', 30)) + count($rows) + 1;
281 $product_cell = l($product['title'], 'node/'. ($product['nid']));
282 $product_csv = $product['title'];
283 $sold_cell = (empty($product['sold'])) ? 0 : $product['sold'];
284 $sold_csv = $sold_cell;
285 $revenue_cell = uc_currency_format((empty($product['revenue'])) ? 0 : $product['revenue']);
286 $revenue_csv = $revenue_cell;
287 $gross_cell = uc_currency_format((empty($product['gross'])) ? 0 : $product['gross']);
288 $gross_csv = $gross_cell;
289
290 if (module_exists('uc_attribute')) {
291 $product_models = db_query("SELECT model FROM {uc_product_adjustments} WHERE nid = %d", $product['nid']);
292 $models = array(db_result(db_query("SELECT model FROM {uc_products} WHERE nid = %d", $product['nid'])));
293 unset($breakdown_product, $breakdown_sold, $breakdown_revenue, $breakdown_gross);
294 while ($product_model = db_fetch_object($product_models)) {
295 $models[] = $product_model->model;
296 }
297 foreach ($models as $model) {
298 $sold = db_result(db_query("SELECT SUM(qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.model = '%s' AND p.nid = %d", $model, $product['nid']));
299 $revenue = db_result(db_query("SELECT SUM(p.price * p.qty) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.model = '%s' AND p.nid = %d", $model, $product['nid']));
300 $gross = db_result(db_query("SELECT (SUM(p.price * p.qty) - SUM(p.cost * p.qty)) FROM {uc_order_products} AS p LEFT JOIN {uc_orders} AS o ON p.order_id = o.order_id WHERE o.order_status IN $order_statuses AND p.model = '%s' AND p.nid = %d", $model, $product['nid']));
301 $breakdown_product .= "<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$model";
302 $product_csv .= "\n $model";
303 $breakdown_sold .= "<br/>". ((!empty($sold)) ? $sold : 0);
304 $sold_csv .= "\n ". ((!empty($sold)) ? $sold : 0);
305 $breakdown_revenue .= "<br/>". (uc_currency_format((!empty($revenue)) ? $revenue : 0));
306 $revenue_csv .= "\n ". (uc_currency_format((!empty($revenue)) ? $revenue : 0));
307 $breakdown_gross .= "<br/>". (uc_currency_format((!empty($gross)) ? $gross : 0));
308 $gross_csv .= "\n ". (uc_currency_format((!empty($gross)) ? $gross : 0));
309 }
310 $product_cell = $product_cell . $breakdown_product;
311 $sold_cell = '<strong>'. $sold_cell .'</strong>'. $breakdown_sold;
312 $revenue_cell = '<strong>'. $revenue_cell .'</strong>'. $breakdown_revenue;
313 $gross_cell = '<strong>'. $gross_cell .'</strong>'. $breakdown_gross;
314 }
315 if ($statistics && $count_views) {
316 $views = (empty($product['totalcount'])) ? 0 : $product['totalcount'];
317 $rows[] = array(
318 array('data' => $row_cell),
319 array('data' => $product_cell),
320 array('data' => $views),
321 array('data' => $sold_cell),
322 array('data' => $revenue_cell, 'nowrap' => 'nowrap'),
323 array('data' => $gross_cell, 'nowrap' => 'nowrap'),
324 );
325 $csv_rows[] = array($row_cell, $product_csv, $views, $sold_csv, $revenue_csv, $gross_csv);
326 }
327 else {
328 $rows[] = array(
329 array('data' => $row_cell),
330 array('data' => $product_cell),
331 array('data' => $sold_cell),
332 array('data' => $revenue_cell, 'nowrap' => 'nowrap'),
333 array('data' => $gross_cell, 'nowrap' => 'nowrap'),
334 );
335 $csv_rows[] = array($row_cell, $product_csv, $sold_csv, $revenue_csv, $gross_csv);
336 }
337 }
338 if (empty($rows)) {
339 $rows[] = array(array('data' => t('No products found'), 'colspan' => count($header)));
340 }
341 $csv_data = uc_reports_store_csv('uc_products', $csv_rows);
342
343 $output = theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table'));
344 $output .= theme_pager(NULL, $page_size);
345 $output .= '<div class="uc-reports-links">'. l(t('Export to CSV file.'), 'admin/store/reports/getcsv/'. $csv_data['report'] .'/'. $csv_data['user']) .'&nbsp;&nbsp;&nbsp;'. (!is_null($_GET['nopage']) ? l(t('Show paged records'), 'admin/store/reports/products') : l(t('Show all records'), 'admin/store/reports/products', array(), 'nopage=1')) .'</div>';
346 $output .= '<small>*'. t('Make sure %setting_name is set to %state in the <a href="!url">access log settings page</a> to enable views column.', array('%setting_name' => 'count content views', '%state' => 'enabled', '!url' => url('admin/logs/settings', 'destination=admin/store/reports/products'))) .'</small>';
347
348 return $output;
349 }
350
351 // Displays the sales summary report.
352 function uc_reports_sales_summary() {
353 $timezone_offset = _uc_reports_timezone_offset();
354 $order_statuses = _uc_reports_order_statuses();
355 $format = variable_get('uc_date_format_default', 'm/d/Y');
356
357 $today_start = gmmktime(0, 0, 0, date('n'), date('j'), date('Y')) - $timezone_offset;
358 $today_end = gmmktime(23, 59, 59, date('n'), date('j'), date('Y')) - $timezone_offset;
359
360 // Build the report table header.
361 $header = array(t('Sales data'), t('Number of orders'), t('Total revenue'), t('Average order'));
362
363 // Calculate and add today's sales summary to the report table.
364 $today = _uc_reports_get_sales($today_start);
365
366 $rows[] = array(
367 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),
368 $today['total'],
369 uc_currency_format($today['income']),
370 uc_currency_format($today['average']),
371 );
372
373 // Calculate and add yesterday's sales summary to the report table.
374 $yesterday = _uc_reports_get_sales($today_start - 86400);
375
376 $rows[] = array(
377 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)),
378 $yesterday['total'],
379 uc_currency_format($yesterday['income']),
380 uc_currency_format($yesterday['average']),
381 );
382
383 // Get the sales report for the month.
384 $month = _uc_reports_get_sales($today_start, 'month');
385 $month_title = format_date($today_start, 'custom', 'M Y');
386
387 // Calculate the daily averages for the month.
388 $daily_orders = round($month['total'] / format_date(time(), 'custom', 'j'), 2);
389 $daily_revenue = round($month['income'] / format_date(time(), 'custom', 'j'), 2);
390
391 if ($daily_orders > 0) {
392 $daily_average = round($daily_revenue / $daily_orders, 2);
393 }
394 else {
395 $daily_average = 0;
396 }
397
398 // Add the daily averages for the month to the report table.
399 $rows[] = array(
400 t('Daily average for @month', array('@month' => $month_title)),
401 $daily_orders,
402 uc_currency_format($daily_revenue),
403 uc_currency_format($daily_average),
404 );
405
406 // Store the number of days remaining in the month.
407 $remaining_days = format_date($today_start, 'custom', 't') - format_date($today_start, 'custom', 'j');
408
409 // Add the projected totals for the month to the report table.
410 $rows[] = array(
411 t('Projected totals for @date', array('@date' => $month_title)),
412 round($month['total'] + ($daily_orders * $remaining_days), 2),
413 uc_currency_format(round($month['income'] + ($daily_revenue * $remaining_days), 2)),
414 '',
415 );
416
417 // Add the sales data report table to the output.
418 $output = theme('table', $header, $rows, array('class' => 'uc-sales-table'));
419
420 // Build the header statistics table header.
421 $header = array(array('data' => t('Statistics'), 'width' => '50%'), '');
422
423 $rows = array(
424 array(array('data' => t('Grand total sales')), array('data' => uc_currency_format(db_result(db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE order_status IN $order_statuses"))))),
425 array(array('data' => t('Customers total')), array('data' => db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN $order_statuses")))),
426 array(array('data' => t('New customers today')), array('data' => db_result(db_query("SELECT COUNT(DISTINCT uid) FROM {uc_orders} WHERE order_status IN $order_statuses AND %d >= created AND created >= %d", $today_end, $today_start)))),
427 array(array('data' => t('Online customers')), array('data' => db_result(db_query("SELECT COUNT(DISTINCT s.uid) FROM {sessions} as s LEFT JOIN {uc_orders} as o ON s.uid = o.uid WHERE s.uid > 0 AND o.order_status IN $order_statuses")))),
428 );
429
430 // Add the statistics table to the output.
431 $output .= theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table'));
432
433 // Build the total orders by status table header.
434 $header = array(array('data' => t('Total orders by status'), 'width' => '50%'), '');
435 $rows = array();
436 $unknown = 0;
437
438 // Loop through the order statuses with their total number of orders.
439 $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");
440 while ($status = db_fetch_array($result)) {
441 if (!empty($status['title'])) {
442 // Add the total number of orders with this status to the table.
443 $rows[] = array(
444 l($status['title'], 'admin/store/orders/sort/'. $status['order_status_id']),
445 $status['order_count'],
446 );
447 }
448 else {
449 // Keep track of the count of orders with an unknown status.
450 $unknown += $status['order_count'];
451 }
452 }
453
454 // Add the unknown status count to the table.
455 if ($unknown > 0) {
456 $rows[] = array(
457 t('Unknown status'),
458 $unknown,
459 );
460 }
461
462 // Add the total orders by status table to the output.
463 $output .= theme('table', $header, $rows, array('class' => 'uc-sales-table'));
464
465 return $output;
466 }
467
468 // Displays the yearly sales report form and table.
469 function uc_reports_sales_year() {
470 $timezone_offset = _uc_reports_timezone_offset();
471 $order_statuses = _uc_reports_order_statuses();
472
473 // Get the year for the report from the URL.
474 if (intval(arg(5)) == 0) {
475 $year = format_date(time(), 'custom', 'Y');
476 }
477 else {
478 $year = arg(5);
479 }
480
481 // Build the header for the report table.
482 $header = array(t('Month'), t('Number of orders'), t('Total revenue'), t('Average order'));
483
484 // Build the header to the CSV export.
485 $csv_rows = array(array(t('Month'), t('Number of orders'), t('Total revenue'), t('Average order')));
486
487 // For each month of the year...
488 for ($i = 1; $i <= 12; $i++) {
489 // Calculate the start and end timestamps for the month in local time.
490 $month_start = gmmktime(0, 0, 0, $i, 1, $year) - $timezone_offset;
491 $month_end = gmmktime(23, 59, 59, $i + 1, 0, $year) - $timezone_offset;
492
493 // Get the sales report for the month.
494 $month_sales = _uc_reports_get_sales($month_start, 'month');
495
496 // Calculate the average order total for the month.
497 if ($month_sales['total'] != 0) {
498 $month_average = round($month_sales['income'] / $month_sales['total'], 2);
499 }
500 else {
501 $month_average = 0;
502 }
503
504 // Add the month's row to the report table.
505 $rows[] = array(
506 l(gmdate('M Y', $month_start), 'admin/store/orders/search/results/0/0/0/0/0/0/'. $month_start .'/'. $month_end),
507 $month_sales['total'],
508 uc_currency_format($month_sales['income']),
509 uc_currency_format($month_average),
510 );
511
512 // Add the data to the CSV export.
513 $csv_rows[] = array(
514 gmdate('M Y', $month_start),
515 $month_sales['total'],
516 uc_currency_format($month_sales['income']),
517 uc_currency_format($month_average),
518 );
519 }
520
521 // Calculate the start and end timestamps for the year in local time.
522 $year_start = gmmktime(0, 0, 0, 1, 1, $year) - $timezone_offset;
523 $year_end = gmmktime(23, 59, 59, 1, 0, $year + 1) - $timezone_offset;
524
525 // Get the sales report for the year.
526 $year_sales = _uc_reports_get_sales($year_start, 'year');
527
528 // Calculate the average order total for the year.
529 if ($year_sales['total'] != 0) {
530 $year_average = round($year_sales['income'] / $year_sales['total'], 2);
531 }
532 else {
533 $year_average = 0;
534 }
535
536 // Add the total row to the report table.
537 $rows[] = array(
538 l(t('Total @year', array('@year' => $year)), 'admin/store/orders/search/results/0/0/0/0/0/0/'. $year_start .'/'. $year_end),
539 $year_sales['total'],
540 uc_currency_format($year_sales['income']),
541 uc_currency_format($year_average),
542 );
543
544 // Add the total data to the CSV export.
545 $csv_rows[] = array(
546 t('Total @year', array('@year' => $year)),
547 $year_sales['total'],
548 uc_currency_format($year_sales['income']),
549 uc_currency_format($year_average),
550 );
551
552 // Cache the CSV export.
553 $csv_data = uc_reports_store_csv('uc_sales_yearly', $csv_rows);
554
555 // Build the page output holding the form, table, and CSV export link.
556 $output = drupal_get_form('uc_reports_sales_year_form', $year)
557 . theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table'))
558 .'<div class="uc-reports-links">'. l(t('Export to CSV file.'), 'admin/store/reports/getcsv/'. $csv_data['report'] .'/'. $csv_data['user']) .'</div>';
559
560 return $output;
561 }
562
563 // Form to specify a year for the yearly sales report.
564 function uc_reports_sales_year_form($year) {
565 $form['year'] = array(
566 '#type' => 'textfield',
567 '#title' => t('Sales year'),
568 '#default_value' => $year,
569 '#maxlength' => 4,
570 '#size' => 4,
571 '#prefix' => '<div class="sales-year">',
572 '#suffix' => '</div>',
573 );
574
575 $form['submit'] = array(
576 '#type' => 'submit',
577 '#value' => t('View'),
578 '#prefix' => '<div class="sales-year">',
579 '#suffix' => '</div>',
580 );
581
582 return $form;
583 }
584
585 function uc_reports_sales_year_form_submit($form_id, $form_values) {
586 drupal_goto('admin/store/reports/sales/year/'. $form_values['year']);
587 }
588
589 // Displays the custom sales report form and table.
590 function uc_reports_sales_custom() {
591 $timezone_offset = _uc_reports_timezone_offset();
592 $format = variable_get('uc_date_format_default', 'm/d/Y');
593
594 // Use default report parameters if we don't detect values in the URL.
595 if (arg(5) == '') {
596 $args = array(
597 'start_date' => gmmktime(0, 0, 0, format_date(time(), 'custom', 'n'), 1, format_date(time(), 'custom', 'Y') - 1) - $timezone_offset,
598 'end_date' => time() - $timezone_offset,
599 'length' => 'month',
600 'status' => FALSE,
601 'detail' => FALSE,
602 );
603 }
604 else {
605 $args = array(
606 'start_date' => arg(5),
607 'end_date' => arg(6),
608 'length' => arg(7),
609 'status' => explode(',', urldecode(arg(8))),
610 'detail' => arg(9),
611 );
612 }
613
614 // Pull the order statuses into a SQL friendly array.
615 if ($args['status'] === FALSE) {
616 $order_statuses = _uc_reports_order_statuses();
617 }
618 else {
619 $order_statuses = "('". implode("', '", $args['status']) ."')";
620 }
621
622 // Build the header for the report table.
623 $header = array(t('Date'), t('Number of orders'), t('Products sold'), t('Total revenue'));
624
625 // Build the header to the CSV export.
626 $csv_rows = array(array(t('Date'), t('Number of orders'), t('Products sold'), t('Total revenue')));
627
628 // Grab the subreports based on the date range and the report breakdown.
629 $subreports = _uc_reports_subreport_intervals($args['start_date'], $args['end_date'], $args['length']);
630
631 // Loop through the subreports and build the report table.
632 foreach ($subreports as $subreport) {
633 $product_data = '';
634 $product_csv = '';
635 $order_data = '';
636 $order_csv = '';
637
638 // Create the date title for the subreport.
639 if ($args['length'] == 'day') {
640 $date = format_date($subreport['start'] - $timezone_offset, 'custom', $format .' - D');
641 }
642 else {
643 $date = format_date($subreport['start'] - $timezone_offset, 'custom', $format) .' - '. format_date($subreport['end'], 'custom', $format);
644 }
645
646 // Build the order data for the subreport.
647 $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']);
648 $statuses = array();
649
650 // Put the order counts into an array by status.
651 while ($status = db_fetch_object($result)) {
652 $statuses[] = t('!count - @title', array('!count' => $status->count, '@title' => $status->title));
653 }
654
655 $order_data = implode('<br />', $statuses);
656 $order_csv = implode("\n", $statuses);
657
658 // Build the product data for the subreport.
659 if ($args['detail']) {
660 // Grab the detailed product breakdown if selected.
661 $result = db_query("SELECT SUM(op.qty) as count, n.title, n.nid FROM {uc_order_products} as op LEFT JOIN {uc_orders} as o ON o.order_id = op.order_id LEFT JOIN {node} as n ON n.nid = op.nid WHERE %d <= o.created AND o.created <= %d AND o.order_status IN $order_statuses GROUP BY n.nid ORDER BY count DESC, n.title ASC", $subreport['start'], $subreport['end']);
662 while ($product_breakdown = db_fetch_object($result)) {
663 $product_data .= $product_breakdown->count .' x '. l($product_breakdown->title, 'node/'. $product_breakdown->nid) ."<br/>\n";
664 $product_csv .= $product_breakdown->count .' x '. $product_breakdown->title ."\n";
665 }
666 }
667 else {
668 // Otherwise just display the total number of products sold.
669 $product_data = db_result(db_query("SELECT SUM(qty) FROM {uc_orders} as o LEFT JOIN {uc_order_products} as op ON o.order_id = op.order_id WHERE %d <= created AND created <= %d AND order_status IN $order_statuses", $subreport['start'], $subreport['end']));
670 $product_csv = $product_data;
671 }
672
673 // Tally up the revenue from the orders.
674 $revenue_count = db_result(db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE %d <= created AND created <= %d AND order_status IN $order_statuses", $subreport['start'], $subreport['end']));
675
676 // Add the subreport's row to the report table.
677 $rows[] = array(
678 array('data' => $date, 'nowrap' => 'nowrap'),
679 empty($order_data) ? '0' : $order_data,
680 empty($product_data) ? '0' : $product_data,
681 uc_currency_format($revenue_count),
682 );
683
684 // Add the data to the CSV export.
685 $csv_rows[] = array(
686 $date,
687 empty($order_csv) ? '0' : $order_csv,
688 empty($product_csv) ? '0' : $product_csv,
689 uc_currency_format($revenue_count),
690 );
691 }
692
693 // Calculate the totals for the report.
694 $order_total = db_result(db_query("SELECT COUNT(*) FROM {uc_orders} WHERE %d <= created AND created <= %d AND order_status IN $order_statuses", $args['start_date'], $args['end_date']));
695 $product_total = db_result(db_query("SELECT SUM(qty) FROM {uc_orders} AS o LEFT JOIN {uc_order_products} AS op ON o.order_id = op.order_id WHERE %d <= created AND created <= %d AND order_status IN $order_statuses", $args['start_date'], $args['end_date']));
696 $revenue_total = db_result(db_query("SELECT SUM(order_total) FROM {uc_orders} WHERE %d <= created AND created <= %d AND order_status IN $order_statuses", $args['start_date'], $args['end_date']));
697
698 // Add the total row to the report table.
699 $rows[] = array(
700 t('Total'),
701 $order_total,
702 $product_total,
703 uc_currency_format($revenue_total),
704 );
705
706 // Add the total data to the CSV export.
707 $csv_rows[] = array(
708 t('Total'),
709 $order_total,
710 $product_total,
711 uc_currency_format($revenue_total),
712 );
713
714 // Cache the CSV export.
715 $csv_data = uc_reports_store_csv('uc_sales_custom', $csv_rows);
716
717 // Build the page output holding the form, table, and CSV export link.
718 $output = drupal_get_form('uc_reports_sales_custom_form', $args, $args['status'])
719 . theme('table', $header, $rows, array('width' => '100%', 'class' => 'uc-sales-table'))
720 .'<div class="uc-reports-links">'. l(t('Export to CSV file.'), 'admin/store/reports/getcsv/'. $csv_data['report'] .'/'. $csv_data['user']) .'</div>';
721
722 return $output;
723 }
724
725 // Form builder for the custom sales report.
726 function uc_reports_sales_custom_form($values, $statuses) {
727 $form['search'] = array(
728 '#type' => 'fieldset',
729 '#title' => t('Customize sales report parameters'),
730 '#description' => t('Adjust these values and update the report to build your custom sales summary. Once submitted, the report may be bookmarked for easy reference in the future.'),
731 '#collapsible' => TRUE,
732 '#collapsed' => TRUE,
733 );
734
735 $form['search']['start_date'] = array(
736 '#type' => 'date',
737 '#title' => t('Start date'),
738 '#default_value' => array(
739 'month' => format_date($values['start_date'], 'custom', 'n'),
740 'day' => format_date($values['start_date'], 'custom', 'j'),
741 'year' => format_date($values['start_date'], 'custom', 'Y'),
742 ),
743 );
744 $form['search']['end_date'] = array(
745 '#type' => 'date',
746 '#title' => t('End date'),
747 '#default_value' => array(
748 'month' => format_date($values['end_date'], 'custom', 'n'),
749 'day' => format_date($values['end_date'], 'custom', 'j'),
750 'year' => format_date($values['end_date'], 'custom', 'Y'),
751 ),
752 );
753
754 $form['search']['length'] = array(
755 '#type' => 'select',
756 '#title' => t('Results breakdown'),
757 '#description' => t('Large daily reports may take a long time to display.'),
758 '#options' => array(
759 'day' => t('daily'),
760 'week' => t('weekly'),
761 'month' => t('monthly'),
762 'year' => t('yearly'),
763 ),
764 '#default_value' => $values['length'],
765 );
766
767 $options = array();
768 foreach (uc_order_status_list() as $status) {
769 $options[$status['id']] = $status['title'];
770 }
771
772 if ($statuses === FALSE) {
773 $statuses = variable_get('uc_reports_reported_statuses', array('completed'));
774 }
775
776 $form['search']['status'] = array(
777 '#type' => 'select',
778 '#title' => t('Order statuses'),
779 '#description' => t('Only orders with selected statuses will be included in the report.') .'<br />'. t('Hold Ctrl + click to select multiple statuses.'),
780 '#options' => $options,
781 '#default_value' => $statuses,
782 '#multiple' => TRUE,
783 '#size' => 5,
784 );
785
786 $form['search']['detail'] = array(
787 '#type' => 'checkbox',
788 '#title' => t('Show a detailed list of products ordered.'),
789 '#default_value' => $values['detail'],
790 );
791
792 $form['search']['submit'] = array(
793 '#type' => 'submit',
794 '#value' => t('Update report'),
795 );
796
797 return $form;
798 }
799
800 function uc_reports_sales_custom_form_validate($form_id, $form_values) {
801 if (empty($form_values['status'])) {
802 form_set_error('status', t('You must select at least one order status.'));
803 }
804 }
805
806 function uc_reports_sales_custom_form_submit($form_id, $form_values) {
807 $timezone_offset = _uc_reports_timezone_offset();
808
809 // Build the start and end dates from the form.
810 $start_date = gmmktime(0, 0, 0, $form_values['start_date']['month'], $form_values['start_date']['day'], $form_values['start_date']['year']) - $timezone_offset;
811 $end_date = gmmktime(23, 59, 59, $form_values['end_date']['month'], $form_values['end_date']['day'], $form_values['end_date']['year']) - $timezone_offset;
812
813 $args = array(
814 $start_date,
815 $end_date,
816 $form_values['length'],
817 urlencode(implode(',', array_keys($form_values['status']))),
818 $form_values['detail'],
819 );
820
821 drupal_goto('admin/store/reports/sales/custom/'. implode('/', $args));
822 }
823
824 // Form builder for the admin settings.
825 function uc_reports_settings_overview() {
826 $form['uc_reports_table_size'] = array(
827 '#type' => 'textfield',
828 '#title' => t('Paged table size'),
829 '#description' => t('The maximum number of rows displayed on one page for a report table.'),
830 '#default_value' => variable_get('uc_reports_table_size', 30),
831 );
832
833 $options = array();
834 foreach (uc_order_status_list() as $status) {
835 $options[$status['id']] = $status['title'];
836 }
837
838 $form['uc_reports_reported_statuses'] = array(
839 '#type' => 'select',
840 '#title' => t('Reported statuses'),
841 '#description' => t('Only orders with selected statuses will be included in reports.'),
842 '#options' => $options,
843 '#default_value' => variable_get('uc_reports_reported_statuses', array('completed')),
844 '#multiple' => TRUE,
845 );
846
847 return system_settings_form($form);
848 }
849
850 /******************************************************************************
851 * Module and Helper Functions *
852 ******************************************************************************/
853
854 /**
855 * Store a CSV file for a report in Drupal's cache to be retrieved later
856 * @param $report_id
857 * A unique string that identifies the report of the CSV file
858 * @param $rows
859 * The rows (table header included) that make CSV file
860 * @return:
861 * An array containing the values need to build URL that return the CSV file of
862 * the report and the CSV data itself
863 */
864 function uc_reports_store_csv($report_id, $rows) {
865 global $user;
866 $user_id = (empty($user->uid)) ? session_id() : $user->uid;
867 foreach ($rows as $row) {
868 foreach ($row as $index => $column) {
869 $row[$index] = '"'. str_replace('"','""', $column) .'"';
870 }
871 $csv_output .= implode(',', $row) ."\n";
872 }
873 cache_set('uc_reports_'. $report_id .'_'. $user_id, 'cache', $csv_output, time() + 86400);
874 return array('user' => $user_id, 'report' => $report_id, 'csv' => $csv_output);
875 }
876
877 /**
878 * Retrieve a cached CSV report & send its data
879 *
880 * @param $report_id
881 * A unique string that identifies the specific report CSV to retrieve
882 * @param $user_id
883 * The user id to who's retrieving the report
884 * - (Equals uid for authenticated users)
885 * - (Equals session_id for anonymous users)
886 */
887 function _uc_reports_get_csv($report_id, $user_id) {
888 global $user;
889 $user_check = (empty($user->uid)) ? session_id() : $user->uid;
890 $csv_data = cache_get('uc_reports_'. $report_id .'_'. $user_id, 'cache');
891
892 if (!$csv_data || $user_id != $user_check) {
893 drupal_set_message(t("The CSV data could not be retreived. It's possible the data might have expired. Refresh the report page and try to retrieve the CSV file again."), 'error');
894 drupal_not_found();
895 exit();
896 }
897 else {
898 ob_end_clean();
899 $http_headers = array(
900 'Pragma: no-cache',
901 'Expires: 0',
902 'Cache-Control: no-cache, must-revalidate',
903 'Cache-Control: private',
904 'Content-Transfer-Encoding: binary',
905 'Content-Length:'. strlen($csv_data->data),
906 'Content-Disposition: attachment; filename="'. $report_id .'.csv"',
907 'Content-Type: text/csv'
908 );
909 foreach ($http_headers as $header) {
910 $header = preg_replace('/\r?\n(?!\t| )/', '', $header);
911 drupal_set_header($header);
912 }
913
914 print $csv_data->data;
915 exit();
916 }
917 }
918
919 /**
920 * Given a timestamp and time period function returns sales that occurred in
921 * that time period
922 *
923 * @param $time
924 * A UNIX time stamp representing the time in which to get sales data
925 * @param $period
926 * The amount of time over which to count sales (e.g. [1] day, month, year)
927 * @return:
928 * An associative array containing information about sales:
929 * - "date" => A string representing the day counting was started
930 * - "income" => The total revenue that occurred during the time period
931 * - "total" => The total number of orders completed during the time period
932 * - "average" => The average revenue produced for each order
933 */
934 function _uc_reports_get_sales($time, $period = 'day') {
935 $output = array();
936 $output['income'] = 0;
937 $order_statuses = _uc_reports_order_statuses();
938
939 switch ($GLOBALS['db_type']) {
940 case 'mysqli':
941 case 'mysql':
942 switch ($period) {
943 default:
944 case 'day':
945 $output['date'] = format_date($time, 'custom', 'n') .'-'. format_date($time, 'custom', 'j');
946 $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') ."%%\"");
947 break;
948 case 'month':
949 $output['date'] = format_date($time, 'custom', 'n');
950 $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') ."%%\"");
951 break;
952 case 'year':
953 $output['date'] = format_date($time, 'custom', 'Y');
954 $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') ."-%%\"");
955 break;
956 }
957 case 'pgsql':
958 switch ($period) {
959 default:
960 case 'day':
961 $output['date'] = format_date($time, 'custom', 'n') .'-'. format_