Admin Dashboard Order Statistics - Day, Week, Month & Year

I have created a new thread for this issue and removed the post from this thread which explains how to get the ‘old’ order statistics back.



This thread covers the summary at the very top, which currently shows Day (and Previous), Week (and Previous) and Month (and Previous) - to which I am looking to add Year (and Previous).



Here’s how I achieved displaying the last Years statistics, which has also been removed in 2.2.3 or previous. Please check the next and subsequent post in this thread to hopefully assist and resolve why the Previous statistics do not display. It appear CSC left some of the code which goes a long way to generate this, but have removed other parts which means the Previous Year total is not calculated at all in 2.2.3, I believe some of the code was removed in a previous release.



In /skins/basic/admin/views/index/index.tpl replace:



Below:


<br />
<a href="{"orders.manage?time_from=`$date.month`&time_to=`$date.TIME`&period=C"|fn_url}" class="section"><span class="price">{include  file="common_templates/price.tpl" value=$orders_stats.monthly_orders.totals.total_paid} </span><u>{$lang.month}</u> <span class="block">{$lang.prev} {include  file="common_templates/price.tpl" value=$orders_stats.monthly_orders.prev_totals.total_paid} <span class="percent-{if $orders_stats.monthly_orders.totals.profit >=  0}up{else}down{/if}">{if $orders_stats.monthly_orders.totals.profit}({if $orders_stats.monthly_orders.totals.profit >=  0}+{/if}{$orders_stats.monthly_orders.totals.profit}%){/if}</span></span></a>
```<br />
<br />
Before the closing </div>, add:<br />
```php
<br />
<a href="{"orders.manage?time_from=`$date.year`&time_to=`$date.TIME`&period=C"|fn_url}" class="section last"><span class="price">{include  file="common_templates/price.tpl" value=$orders_stats.year_orders.totals.total_paid} </span><u>{$lang.year}</u> <span class="block">{$lang.prev} {include  file="common_templates/price.tpl" value=$orders_stats.year_orders.prev_totals.total_paid} <span class="percent-{if $orders_stats.year_orders.totals.profit >=  0}up{else}down{/if}">{if $orders_stats.year_orders.totals.profit}({if $orders_stats.year_orders.totals.profit >=  0}+{/if}{$orders_stats.year_orders.totals.profit}%){/if}</span></span></a><br />

```<br />
<br />
In [b]/skins/basic/admin/styles.css[/b] replace:<br />
<br />
```php
<br />
.statistics-box.overall .statistics-body .section {<br />
	display: inline-block;<br />
	width: 33%;<br />
	margin: 0 6px;<br />
	white-space: normal;<br />
	border-right: 1px dotted #ccc;<br />
	text-decoration: none;<br />
}<br />

```<br />
<br />
With<br />
```php
<br />
.statistics-box.overall .statistics-body .section {<br />
	display: inline-block;<br />
	width: 24%;<br />
	margin: 0 1px;<br />
	white-space: normal;<br />
	border-right: 1px dotted #ccc;<br />
	text-decoration: none;<br />
}<br />

```<br />
<br />
Refresh the cache (domain.com/admin.php?cc), now your Orders totals will display the Year total in the summary.<br />
<br />
<br />
Now, I'm not sure if it is an issue with the way all my orders have been imported into CS-Cart from Interspire, but the variable does not pull a starting point, but does produce todays date, so when I click Year, all the orders from my starting point up until today are shown, as opposed to between 23/10/10 - 23/10/11.<br />
<br />
This is the code which produces the URL:<br />
<br />
```php
orders.manage?time_from=`$date.year`&time_to=`$date.TIME`&period=C"|fn_url}
```<br />
<br />
I thought, since the 'year' variable is programmed elsewhere in the cart and does show statistics for the total Year value, this would hopefully work, but the URL string it produces is:<br />
<br />
```php
http://www.domain.com/admin.php?dispatch=orders.manage&time_from=&time_to=23/10/2011&period=C
```<br />
Note the missing time_from= output. Check the attachment for confirmation.<br />
<br />
I have found if I change &period=C to &period=Y, clicking Year takes you to the orders page showing all orders between January 1st and today. I also noticed in 2.2.3 that day, week and month already had the &period=C set, where as in your code, you have &period=D,&period=W and &period=M. Why is this piece of the URL still included in 2.2.3 if it has no relevance?! They are definitely defined as D, W, M and Y in /core/fn.common.php - as well as 'day', 'week', 'month' and 'year' so can't figure out why '$date.year' does not work.<br />
<br />
Any idea what the problem is? Thanks in advance.<p><a href="127.0.0.1/uploads/monthly_10_2011/post-17808-0-57183500-1319391666.jpg">admin-dashboard.jpg</a></p>

[quote name='tbirnseth' timestamp='1319312655' post='124323']

What does a:

{$date|print_r:true}


Show you for the date array. That will tell you what the elements are.
[/quote]
[quote]
Array
(
[TIME] => 23/10/2011
[today] => 23/10/2011
[week] => 17/10/2011
[month] => 01/10/2011
)[/quote]

No 'year' variable is shown in the output, however, I find this strange that I searched the entire CS-Cart file system and found 'year' is also documented in /core/fn.common.php - any idea where (else) this may be controlled and/or a dirty hack to get around this? Thanks for your help.

[quote name='tbirnseth' timestamp='1319351093' post='124345']
Looks like the $date variable (for this controller anyway) is returning the data of today, 1 week ago and one month ago. Looks like it skipped the year.

You can always search the 'controllers' tree for "assign('date" and see what you come up with...
[/quote]

In /controllers/admin/index.tpl

```php $view->assign('date', $date); ```

A search for "$date" only brings up /core/fn.common.php, in relation to (removed irrelevent code for other unrelated functions):

```php
function fn_this_day_begin()
{
$current_date = 0;
$current_date = time();
$_date_year = strftime("%Y", $current_date);
$_date_month = strftime("%m", $current_date);
$_date_day = strftime("%d", $current_date);
return mktime(0, 0, 0, $_date_month, $_date_day, $_date_year);
}

function fn_create_periods($params)
{
$today = getdate(TIME);
$period = !empty($params['period']) ? $params['period'] : null;

$time_from = !empty($params['time_from']) ? fn_parse_date($params['time_from']) : 0;
$time_to = !empty($params['time_to']) ? fn_parse_date($params['time_to'], true) : TIME;

// Current dates
if ($period == 'D') {
$time_from = mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']);
$time_to = TIME;

} elseif ($period == 'W') {
$wday = empty($today['wday']) ? "6" : (($today['wday'] == 1) ? "0" : $today['wday'] - 1);
$wstart = getdate(strtotime("-$wday day"));
$time_from = mktime(0, 0, 0, $wstart['mon'], $wstart['mday'], $wstart['year']);
$time_to = TIME;

} elseif ($period == 'M') {
$time_from = mktime(0, 0, 0, $today['mon'], 1, $today['year']);
$time_to = TIME;

} elseif ($period == 'Y') {
$time_from = mktime(0, 0, 0, 1, 1, $today['year']);
$time_to = TIME;

// Last dates
} elseif ($period == 'LD') {
$today = getdate(strtotime("-1 day"));
$time_from = mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year']);
$time_to = mktime(23, 59, 59, $today['mon'], $today['mday'], $today['year']);

} elseif ($period == 'LW') {
$today = getdate(strtotime("-1 week"));
$wday = empty($today['wday']) ? 6 : (($today['wday'] == 1) ? 0 : $today['wday'] - 1);
$wstart = getdate(strtotime("-$wday day", mktime(0, 0, 0, $today['mon'], $today['mday'], $today['year'])));
$time_from = mktime(0, 0, 0, $wstart['mon'], $wstart['mday'], $wstart['year']);

$wend = getdate(strtotime("+6 day", $time_from));
$time_to = mktime(23, 59, 59, $wend['mon'], $wend['mday'], $wend['year']);

} elseif ($period == 'LM') {
$today = getdate(strtotime("-1 month"));
$time_from = mktime(0, 0, 0, $today['mon'], 1, $today['year']);
$time_to = mktime(23, 59, 59, $today['mon'], date('t', strtotime("-1 month")), $today['year']);

} elseif ($period == 'LY') {
$today = getdate(strtotime("-1 year"));
$time_from = mktime(0, 0, 0, 1, 1, $today['year']);
$time_to = mktime(23, 59, 59, 12, 31, $today['year']);

// Last dates
} elseif ($period == 'HH') {
$today = getdate(strtotime("-23 hours"));
$time_from = mktime($today['hours'], $today['minutes'], $today['seconds'], $today['mon'], $today['mday'], $today['year']);
$time_to = TIME;

} elseif ($period == 'HW') {
$today = getdate(strtotime("-6 day"));
$time_from = mktime($today['hours'], $today['minutes'], $today['seconds'], $today['mon'], $today['mday'], $today['year']);
$time_to = TIME;

} elseif ($period == 'HM') {
$today = getdate(strtotime("-29 day"));
$time_from = mktime($today['hours'], $today['minutes'], $today['seconds'], $today['mon'], $today['mday'], $today['year']);
$time_to = TIME;

} elseif ($period == 'HC') {
$today = getdate(strtotime('-' . $params['last_days'] . ' day'));
$time_from = mktime($today['hours'], $today['minutes'], $today['seconds'], $today['mon'], $today['mday'], $today['year']);
$time_to = TIME;
}

Registry::get('view')->assign('time_from', $time_from);
Registry::get('view')->assign('time_to', $time_to);

return array($time_from, $time_to);
}

function fn_parse_date($timestamp, $end_time = false)
{
if (!empty($timestamp)) {
if (is_numeric($timestamp)) {
return $timest
}

$ts = explode('/', $timestamp);
$ts = array_map('intval', $ts);
if (empty($ts[2])) {
$ts[2] = date('Y');
}
if (count($ts) == 3) {
list($h, $m, $s) = $end_time ? array(23, 59, 59) : array(0, 0, 0);
if (Registry::get('settings.Appearance.calendar_date_format') == 'month_first') {
$timestamp = mktime($h, $m, $s, $ts[0], $ts[1], $ts[2]);
} else {
$timestamp = mktime($h, $m, $s, $ts[1], $ts[0], $ts[2]);
}
} else {
$timestamp = TIME;
}
}

return !empty($timestamp) ? $timestamp : TIME;
}

function fn_date_format($timestamp, $format = '%b %e, %Y')
{
if (substr(PHP_OS,0,3) == 'WIN') {
$hours = strftime('%I', $timestamp);
$short_hours = ($hours < 10) ? substr($hours, -1) : $hours;
$_win_from = array ('%e', '%T', '%D', '%l');
$_win_to = array ('%d', '%H:%M:%S', '%m/%d/%y', $short_hours);
$format = str_replace($_win_from, $_win_to, $format);
}

$date = getdate($timestamp);
$m = $date['mon'];
$d = $date['mday'];
$y = $date['year'];
$w = $date['wday'];
$hr = $date['hours'];
$pm = ($hr >= 12);
$ir = ($pm) ? ($hr - 12) : $hr;
$dy = $date['yday'];
$fd = getdate(mktime(0, 0, 0, 1, 1, $y)); // first day of year
$wn = (int) (($dy + $fd['wday']) / 7);
if ($ir == 0) {
$ir = 12;
}
$min = $date['minutes'];
$sec = $date['seconds'];
```
So, from this I hazard a guess that 'LY' should pull the previous year, right? Well, it doesn't work either - it doesn't work using:
```php ```
When the link is clicked, it shows the orders from 01/01/2011 until today. This doesn't explain why day, week and month all use "period=C" especially when there the period is defined in fn.common.php as LD,LW,LM, etc.

I guess there probably is a way to manipulate the code above to say, in part pseudo
```php
```

In /controllers/admin/index.php, this code creates last_month (similar is used for last week, but the code shown for year is all there is, there isn't any $last_year code.
```php // Monthly orders
$date['month'] = date($date_format, mktime(0, 0, 0, $today['mon'], 1, $today['year']));
$last_month = $today;
if ($last_month['mon'] == 1) {
$last_month['mon'] = 12;
$last_month['year']--;
} else {
$last_month['mon']--;
}

$orders_stats['monthly_orders'] = db_get_hash_array("SELECT status, COUNT(*) as amount, SUM(total) as total FROM ?:orders WHERE timestamp >= ?i AND timestamp <= ?i $condition " . fn_get_company_condition() . " GROUP BY status", 'status', mktime(0, 0, 0, $today['mon'], 1, $today['year']), TIME);
$orders_stats['monthly_orders']['totals'] = db_get_row("SELECT SUM(IF(status IN (?a), total, 0)) as total_paid, SUM(total) as total, COUNT(*) as amount FROM ?:orders WHERE timestamp >= ?i AND timestamp <= ?i $condition " . fn_get_company_condition(), $paid_statuses, mktime(0, 0, 0, $today['mon'], 1, $today['year']), TIME);
$orders_stats['monthly_orders']['prev_totals'] = db_get_row("SELECT SUM(IF(status IN (?a), total, 0)) as total_paid, SUM(total) as total, COUNT(*) as amount FROM ?:orders WHERE timestamp >= ?i AND timestamp <= ?i $condition " . fn_get_company_condition(), $paid_statuses, mktime(0, 0, 0, $last_month['mon'], 1, $last_month['year']), mktime(0, 0, 0, $last_month['mon'], 1, $last_month['year']) + (TIME - mktime(0, 0, 0, $today['mon'], 1, $today['year'])));

if ($orders_stats['monthly_orders']['prev_totals']['total_paid'] > 0) {
$orders_stats['monthly_orders']['totals']['profit'] = intval($orders_stats['monthly_orders']['totals']['total_paid'] * 100 / $orders_stats['monthly_orders']['prev_totals']['total_paid']) - 100;
}

// Yearly orders
$orders_stats['year_orders'] = db_get_hash_array("SELECT status, COUNT(*) as amount, SUM(total) as total FROM ?:orders WHERE timestamp >= ?i AND timestamp <= ?i $condition " . fn_get_company_condition() . " GROUP BY status", 'status', mktime(0, 0, 0, 1, 1, $today['year']), TIME);
$orders_stats['year_orders']['totals'] = db_get_row("SELECT SUM(IF(status IN (?a), total, 0)) as total_paid, SUM(total) as total, COUNT(*) as amount FROM ?:orders WHERE timestamp >= ?i AND timestamp <= ?i $condition " . fn_get_company_condition(), $paid_statuses, mktime(0, 0, 0, 1, 1, $today['year']), TIME);
```
But I'll be damned if I can find out...any thoughts? Thanks again.

[quote name='The Tool' timestamp='1319357486' post='124349']
No, the code I posted has nothing to do with the top statistics (comparing day, week, month order totals). It only deals with the "Order statistics". Look at the image in the original post and you will see a statistic at the top (code you referred to in your post) and then further down you will see "Order statistics".

Maybe that is the reason you cannot get it to work?
[/quote]
The code posted references the $orders_stats.year_orders variable, which is what I am looking to manipulate. There's only one other thread, which isn't as relative to this.

Thank you.

Hello !!! And how are you? I was reading the marvellous work you have done on forum: Admin Dashboard Order Statistics - Day, Week, Month & Year - Hints & Modifications - CS-Cart Community Forums and I was thinking if you had succeeded finally to get the last year's variable to work. I am trying to do the same for all the variables, that is last year's same day, last year's same week etc. and I was thinking if you can help me out there. Just upgraded to 2.2.5 from 2.1.4 and I find very interesting the percentage indicators for my sales guys.