<?php

/**
 * [BEGIN_COT_EXT]
 * Hooks=payments.balance.history.query
 * [END_COT_EXT]
 */

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

$market_order = cot_import('market-order', 'G', 'INT');

$market_orders = ($market_order > 0 ? $db->query("SELECT order_id FROM $db_market_orders WHERE order_seller=".$usr['id']." AND order_pid=".$market_order)->fetchAll(PDO::FETCH_COLUMN) : array());

if(count($market_orders) > 0) {
  if(in_array('refund', $operation)) {  //Возврат оплаты заказа
     $where['pay_area'][] = "(pay_code='market_reftocust_emptyitem:".implode("' OR pay_code='market_reftocust_emptyitem:", $market_orders)."'
                           OR pay_code='market_code:".implode("' OR pay_code='market_code:", $market_orders)."')
                           OR pay_code='market_code_arbitrage_payer:".implode("' OR pay_code='market_code:", $market_orders)."')
                           OR pay_code='market_code_arbt:".implode("' OR pay_code='market_code:", $market_orders)."')
                           ";
  }
  if(in_array('order_in', $operation)) {  //Начисление за сделанный заказ
     $where['pay_area'][] = "(pay_code='market_code_done:".implode("' OR pay_code='market_code_done:", $market_orders)."'
                          OR pay_code='market_code_autodone:".implode("' OR pay_code='market_code_autodone:", $market_orders)."'
                          OR pay_code='market_code_arbitrage_seller:".implode("' OR pay_code='market_code_autodone:", $market_orders)."'
                          OR pay_code='market_code_arbtdone:".implode("' OR pay_code='market_code_autodone:", $market_orders)."'
                          )";
  }
  if(in_array('order_out', $operation)) {  //Оплата заказа
     $where['pay_area'][] = "((pay_area='market_payorder' OR pay_area='market_payaddextras' OR pay_area='market_accept_seller_payaddextras' OR pay_area='market_arbitrage') AND (pay_code='".implode("' OR pay_code='", $market_orders)."'))";
  }
} else {
  if(in_array('refund', $operation)) {  //Возврат оплаты заказа
     $where['pay_area'][] = "(pay_code='market_reftocust_emptycart' OR pay_code LIKE 'market_reftocust_emptyitem:%' OR pay_code LIKE 'market_code:%' OR pay_code='market_code_arbitrage_payer' OR pay_code LIKE 'market_code_arbt:%')";
  }
  if(in_array('order_in', $operation)) {  //Начисление за сделанный заказ
     $where['pay_area'][] = "(pay_code LIKE 'market_code_done:%' OR pay_code LIKE 'market_code_autodone:%' OR pay_code LIKE 'market_code_arbitrage_seller:%' OR pay_code LIKE 'market_code_arbtdone:%')";
  }
  if(in_array('order_out', $operation)) {  //Оплата заказа
     $where['pay_area'][] = "(pay_area='market_payorder' OR pay_area='market_payaddextras' OR pay_area='market_accept_seller_payaddextras'  OR pay_area='market_arbitrage')";
  }
}

$user_marketitems = $db->query("SELECT item_id, item_title FROM $db_market WHERE item_userid=".$usr['id'])->fetchAll(PDO::FETCH_KEY_PAIR);