<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=moderation.page.mfind
Order=1
[END_COT_EXT]
==================== */

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

if($m == 'payments' && in_array('payments', $moderauth)) {
  $mfind = true;

  $id = cot_import('id', 'G', 'INT');

  if($id > 0 && $a == 'addpayment')
  {
  	$summ = cot_import('summ', 'P', 'INT');
    $summ_dodji = cot_import('summ_dodji', 'P', 'INT');
  	$desc = cot_import('desc', 'P', 'TXT');

    if(is_numeric($summ) && $summ != 0) {
      cot_payments_updateuserbalance_hand($id, $summ, $desc, false);
      cot_message('Платеж на сумму '.$summ.' '.$cfg['payments']['valuta'].' успешно создан');
    }
    if(is_numeric($summ_dodji) && $summ_dodji != 0) {
      cot_payments_updateuserbalance_hand($id, $summ_dodji, $desc, true);
      cot_message('Платеж на сумму '.$summ_dodji.' '.$cfg['payments']['valutadodji'].' успешно создан');
    }
  	cot_redirect(cot_url('moderation', 'm=payments&id='.$id));
  }

  $t = new XTemplate(cot_tplfile('moderation.payments', 'module'));
  require_once cot_incfile('payments', 'module');

	list($pg, $d, $durl) = cot_import_pagenav('d', $cfg['maxrowsperpage']);

	$where['status'] = "pay_status='done'";
	$where['summ'] = '(pay_summ>0 OR pay_dodji>0)';

	if(isset($id))
	{
		$where['userid'] = 'pay_userid=' . $id;
		$urr = $db->query("SELECT * FROM $db_users WHERE user_id=" . (int)$id)->fetch();
		$t->assign(cot_generate_usertags($urr, 'USER_'));
	}

	$where = array_filter($where);
	$where = ($where) ? 'WHERE ' . implode(' AND ', $where) : '';

	$pays = $db->query("SELECT * FROM $db_payments AS p
		LEFT JOIN $db_users AS u ON u.user_id=p.pay_userid
		$where
		ORDER BY pay_pdate DESC, pay_id DESC LIMIT $d, " . $cfg['maxrowsperpage'])->fetchAll();

	$totalitems = $db->query("SELECT COUNT(*) FROM $db_payments AS p
		LEFT JOIN $db_users AS u ON u.user_id=p.pay_userid
		$where")->fetchColumn();

	$pagenav = cot_pagenav('moderation', 'm=payments&id='.$id, $d, $totalitems, $cfg['maxrowsperpage']);

	$t->assign(array(
    'PAGENAV_COUNT' => $totalitems,
		'PAGENAV_PAGES' => $pagenav['main'],
		'PAGENAV_PREV' => $pagenav['prev'],
		'PAGENAV_NEXT' => $pagenav['next']
	));

	foreach($pays as $pay)
	{
		$t->assign(cot_generate_paytags($pay, 'PAY_ROW_'));

		if($pay['pay_userid'] > 0)
		{
			$t->assign(cot_generate_usertags($pay, 'PAY_ROW_USER_'));
		}
		else
		{
			$t->assign(array(
				'PAY_ROW_USER_ID' => 0,
				'PAY_ROW_USER_NICKNAME' => $L['Guest'],
			));
		}

		$t->parse('MAIN.PAY_ROW');
	}

	if(!empty($id))
	{
		$where_string = 'AND pay_userid='.$id;
	}

	$inbalance = $db->query("SELECT SUM(pay_summ) as summ FROM $db_payments AS p
		WHERE pay_area='balance' AND pay_summ>0 AND pay_paydodji=0 AND pay_status='done' $where_string")->fetchColumn();

	$outbalance = $db->query("SELECT SUM(pay_summ) as summ FROM $db_payments AS p
		WHERE pay_area='balance' AND pay_summ<0 AND pay_paydodji=0 AND pay_status='done' $where_string")->fetchColumn();

	$credit = $db->query("SELECT SUM(pay_summ) as summ FROM $db_payments AS p
		WHERE pay_area!='balance' AND pay_paydodji=0 AND pay_status='done' $where_string")->fetchColumn();

	$inbalance_dodji = $db->query("SELECT SUM(pay_dodji) as summ FROM $db_payments AS p
		WHERE pay_area='balance' AND pay_dodji>0 AND pay_paydodji=1 AND pay_status='done' $where_string")->fetchColumn();

	$outbalance_dodji = $db->query("SELECT SUM(pay_dodji) as summ FROM $db_payments AS p
		WHERE pay_area='balance' AND pay_dodji<0 AND pay_paydodji=1 AND pay_status='done' $where_string")->fetchColumn();

	$credit_dodji = $db->query("SELECT SUM(pay_dodji) as summ FROM $db_payments AS p
		WHERE pay_area!='balance' AND pay_paydodji=1 AND pay_status='done' $where_string")->fetchColumn();

	$t->assign(array(
    'ADMIN_PAYMENTS_CNT' => $totalitems,
		'ADMIN_PAYMENTS_INBALANCE' => number_format($inbalance, 2, '.', ' '),
		'ADMIN_PAYMENTS_OUTBALANCE' => number_format(abs($outbalance), 2, '.', ' '),
		'ADMIN_PAYMENTS_BALANCE' => number_format($inbalance - abs($outbalance), 2, '.', ' '),
		'ADMIN_PAYMENTS_CREDIT' => number_format($credit, 2, '.', ' '),
		'ADMIN_PAYMENTS_INBALANCE_DODJI' => number_format($inbalance_dodji, 2, '.', ' '),
		'ADMIN_PAYMENTS_OUTBALANCE_DODJI' => number_format(abs($outbalance_dodji), 2, '.', ' '),
		'ADMIN_PAYMENTS_BALANCE_DODJI' => number_format($inbalance_dodji - abs($outbalance_dodji), 2, '.', ' '),
		'ADMIN_PAYMENTS_CREDIT_DODJI' => number_format($credit_dodji, 2, '.', ' '),
	));

  cot_display_messages($t);

  $t->parse('MAIN');

  echo $t->text('MAIN');
}