<?php
/* ====================
[BEGIN_COT_EXT]
Hooks=moderation.page.mnotfind
[END_COT_EXT]
==================== */

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

if(in_array('payments', $moderauth)) {
  require_once cot_incfile('payments', 'module');

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

	$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 10")->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();

	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_'));
		}

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

	$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'")->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'")->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'")->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'")->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'")->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'")->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, '.', ' '),
	));

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