<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=market.myitems.loop
 * [END_COT_EXT]
 */

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

$rtstat = array(
  'stat' => array(
    'total' => 0,
    'positive' => 0,
    'good' => 0,
    'bad' => 0,
    'empty' => 0,
    'repeat' => 0,
    'user_percent' => array(
      'good' => 0,
      'bad' => 100
    ),
    'level' => 'bad',
    'level_text' => 'Плохо'
  ),
  'position' => array(
    'totalsamecat' => ((!empty($item['item_cat']) && is_array($structure['market'][$item['item_cat']])) ? $structure['market'][$item['item_cat']]['count'] : 0),
    'otzivy' => 0,
    'otzivy_m' => 0,
  )
);

$lastorders = $db->query("SELECT * FROM $db_market_orders as o
    LEFT JOIN $db_reviews as r ON r.item_code=o.order_id
    WHERE (item_id IS NULL OR (item_area='market' AND item_type='review'))
          AND (order_status='cancel' OR order_status='done') AND order_pid=".$item['item_id'].' ORDER BY order_paid LIMIT 80')->fetchAll();

foreach($lastorders as $lord) {
  if($lord['item_id'] > 0) {
    if($lord['item_score'] == 0) {
      $rtstat['stat']['bad']++;
    } else {
      $rtstat['stat']['good']++;
    }
  } elseif($db->query("SELECT * FROM $db_market_orders WHERE order_userid=".$lord['order_userid']." AND order_pid=".$lord['order_pid']." AND order_id!=".$lord['order_id']." AND order_status!='new'")->fetchColumn() > 0) {
    $rtstat['stat']['repeat']++;
  } else {
    $rtstat['stat']['empty']++;
  }
  $rtstat['stat']['total']++;
}

$rtstat['stat']['positive'] = $rtstat['stat']['good'] + $rtstat['stat']['repeat'];

$item['item_rt_rating'] = ($rtstat['stat']['total'] > 0 ? ((100 / $rtstat['stat']['total']) * $rtstat['stat']['positive']) : 0);
$rtstat['stat']['user_percent']['good'] = round($item['item_rt_rating'], 0);
$rtstat['stat']['user_percent']['bad'] = 100 - $rtstat['stat']['user_percent']['good'];

if($rtstat['stat']['user_percent']['good'] >= 75) {
  $rtstat['stat']['level'] = 'excellent';
  $rtstat['stat']['level_text'] = 'Отлично';
} elseif ($rtstat['stat']['user_percent']['good'] >= 50) {
  $rtstat['stat']['level'] = 'good';
  $rtstat['stat']['level_text'] = 'Хорошо';
} elseif ($rtstat['stat']['user_percent']['good'] >= 25) {
  $rtstat['stat']['level'] = 'satisfactorily';
  $rtstat['stat']['level_text'] = 'Средние';
}

// Add field if missing
if (!$db->fieldExists($db_market, "item_rt_rating"))
{
	$dbres = $db->query("ALTER TABLE `$db_market` ADD COLUMN `item_rt_rating` float(16,2) DEFAULT '0'");
}

$db->update($db_market, array('item_rt_rating' => round($item['item_rt_rating'], 0)), 'item_id='.$item['item_id']);

$rtstat['position']['otzivy'] = $db->query("SELECT COUNT(*) FROM $db_market WHERE item_cat='".$item['item_cat']."' AND item_state=0 AND item_rt_rating>".$item['item_rt_rating']." AND item_id!=".$item['item_id'])->fetchColumn();
$rtstat['position']['otzivy'] = round((($rtstat['position']['otzivy'] > 0  && $rtstat['position']['totalsamecat'] > 0) ? ((100 / $rtstat['position']['totalsamecat']) * $rtstat['position']['otzivy']) : 100), 0);
$rtstat['position']['otzivy_m'] = 100 - $rtstat['position']['otzivy'];

$t->assign('PRD_ROW_RTSTAT', $rtstat);
$t->parse("MAIN.PRD_RTSTAT_ROWS");
