<?php

defined('COT_CODE') or die('Wrong URL');

list($usr['auth_read'], $usr['auth_write'], $usr['isadmin']) = cot_auth('plug', 'marketorders');
cot_block($usr['id'] > 0 && $usr['auth_read']);

if(!cot_plugin_active('reviews')) cot_block(false);
require_once cot_incfile('reviews', 'plug');

if(cot_plugin_active('marketfav')) require_once cot_incfile('marketfav', 'plug');

$type = cot_import('type', 'G', 'INT');
if(empty($type)) {
  $type = 1;
} elseif($type == -1) {
  $type = 0;
}
if(!in_array($type, array(0, 1, 3, 6, 12))) $type = 1;

/* === Hook === */
foreach (cot_getextplugins('marketorders.analytics.first') as $pl)
{
	include $pl;
}
/* ===== */

$out['subtitle'] = $L['marketorders_analytics'];
$out['head'] .= $R['code_noindex'];

$list_url_path = array('m' => 'analytics');

$urr = $usr['profile'];

$anlt_arr = cot_mk_update_analytics(array($urr), $type, true);
$anlt = $anlt_arr[$usr['id']]['anlt'];
$ruserupd = $anlt_arr[$usr['id']]['ruserupd'];

$mskin = cot_tplfile(array('marketorders', 'analytics'), 'plug');

/* === Hook === */
foreach (cot_getextplugins('marketorders.analytics.main') as $pl)
{
	include $pl;
}
/* ===== */

$t = new XTemplate($mskin);

$t->assign(array(
  'ANLT_TYPE' => $type,
  'ANLT_DATA' => $anlt,
  'ANLT_LAST_UPDATE' => ($anlt['anlt_cache'] == 1 ? cot_build_timeago(($sys['now'] - ($sys['now'] - $usr['profile']['user_mk_cache_upd'])), 5) : 'Только что')
));

foreach($anlt['base-sales'] as $i => $sale) {
  $t->assign(array(
    'SALE_DATE' => $i,
    'SALE_CNT' => $sale['count'],
    'SALE_SUMM' => ($sale['sum'] > 0 ? $sale['sum'] : 0),
  ));
  $t->parse('MAIN.BASE_SALES_ROW');
}

foreach($anlt['extra-sales'] as $i => $sale) {
  $t->assign(array(
    'SALE_DATE' => $i,
    'SALE_CNT' => $sale['count'],
    'SALE_SUMM' => ($sale['sum'] > 0 ? $sale['sum'] : 0),
  ));
  $t->parse('MAIN.EXTRA_SALES_ROW');
}

if(count($ruserupd) > 0) $db->update($db_users, $ruserupd, 'user_id='.$usr['id']);