<?php

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

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

$mkstat = array(
  'prodagi' => $db->query("SELECT COUNT(*) FROM $db_market_orders WHERE order_status!='new' AND order_pid=".$item['item_id'])->fetchColumn(),
  'dohod' => $db->query("SELECT SUM(order_cost-(order_cost*order_tax/100)) FROM $db_market_orders WHERE order_status='done' AND order_pid=".$item['item_id'])->fetchColumn(),
  'stat' => array(
    'total' => 0,
    'done' => 0,
    'cancel' => 0,
    'cancel_auto' => 0,
    'cancel_byseller' => 0,
    'done_orders_percent' => 0,
    'user_percent' => array(
      'good' => 0,
      'bad' => 100
    ),
    'level' => 'bad',
    'level_text' => 'Плохо'
  ),
  'conversoin' => 0,
  'position' => array(
    'totalsamecat' => ((!empty($item['item_cat']) && is_array($structure['market'][$item['item_cat']])) ? $structure['market'][$item['item_cat']]['count'] : 0),
    'otvetst' => 0,
    'convers' => 0,
    'otvetst_m' => 0,
    'convers_m' => 0,
  )
);

if(!$mkstat['prodagi']) $mkstat['prodagi'] = 0;
$mkstat['dohod_format'] = ($mkstat['dohod'] > 0 ? number_format($mkstat['dohod'], 0, '', ' ') : 0);

$lastorders = $db->query("SELECT * FROM $db_market_orders WHERE (order_status='cancel' OR order_status='done') AND order_pid=".$item['item_id'].' ORDER BY order_paid LIMIT 30')->fetchAll();

foreach($lastorders as $lord) {
  if($lord['order_status'] == 'done') {
    $mkstat['stat']['done']++;
  } else {
    if(in_array($lord['order_status_code'], array('afterpaid_24hour', 'afterqueue_24hour')))
		{
			$mkstat['stat']['cancel_auto']++;
    }
    elseif(in_array($lord['order_status_code'], array('seller_payer_cannot_execute_correct', 'seller_do_not_like_this_worker', 'seller_no_communication_with_worker', 'seller_no_time', 'seller_force_cancel')))
		{
			$mkstat['stat']['cancel_byseller']++;
    } else {
      $mkstat['stat']['cancel']++;
    }
  }
  $mkstat['stat']['total']++;
}

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

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

$item['item_mk_conversoin'] = ($mkstat['prodagi'] > 0 ? ((100 / $item['item_count']) * $mkstat['prodagi']) : 0);
$mkstat['stat']['conversoin'] = round($item['item_mk_conversoin'], 0);

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

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

//% от услуг
//$mkstat['position']['convers'] = $db->query("SELECT COUNT(*) FROM $db_market WHERE item_cat='".$item['item_cat']."' AND item_state=0 AND item_mk_conversoin>".$item['item_mk_conversoin']." AND item_id!=".$item['item_id'])->fetchColumn();
//$mkstat['position']['convers'] = round(($mkstat['position']['totalsamecat'] > 0 ? ((100 / $mkstat['position']['totalsamecat']) * $mkstat['position']['convers']) : 100), 0);
//$mkstat['position']['convers_m'] = 100 - $mkstat['position']['convers'];

//% от среднего
$mkstat['position']['convers'] = $db->query("SELECT avg(item_mk_conversoin) FROM $db_market WHERE item_cat='".$item['item_cat']."' AND item_state=0")->fetchColumn();
if($mkstat['position']['convers'] > 0) {
  $mkstat['position']['convers'] = round(((100 / $mkstat['position']['convers']) * $item['item_mk_conversoin']), 0) - 100;
  if($mkstat['position']['convers'] > 500) $mkstat['position']['convers'] = 500;
} else {
  $mkstat['position']['convers'] = 100;
}
$mkstat['position']['convers_m'] = -$mkstat['position']['convers'];

$t->assign('PRD_ROW_MKSTAT', $mkstat);
$t->parse("MAIN.PRD_MKSTAT_ROWS");

//item_mk_rating
