<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request as Req;
use Request;

/**
 *
 */
class FundlogController extends Controller {
	/**
	 * @return 财务日志
	 */
	function index(Req $req) {
		$request=array();
		$request['startime']=isset($req->startime)?trim($req->startime):trans('status.default_time.seven_day');
		$request['endtime'] = isset($req->endtime) ? trim($req->endtime) : trans('status.default_time.etime');
		$request['account_name'] = isset($req->account_name) ? trim($req->account_name) : null;
		$request['loss_name'] = isset($req->loss_name) ? trim($req->loss_name) : null;
		$request['agent_name'] = isset($req->agent_name) ? trim($req->agent_name) : null;
		
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/money/fundLog');
		$dt->setLang('money');
		$dt->setPriKey('account_identity');
		$dt->addColsFields('account_name', array('sort' => false, 'templet' => '#username'));
		$dt->addColsFields('sendmoney');
		$dt->addColsFields('savemoney');
		$dt->addColsFields('getmoney');
		$dt->addColsFields('winmoney', array('templet' => '#usergetmoney'));
		$dt->addColsFields('adminadd');
		$dt->addColsFields('adminpull');
		$dt->addColsFields('recharge_send');
		$dt->addColsFields('back_return');

		return view('admin.Fundlog/index', $dt->render($request));
	}

	/**
	 * 单用户存/取/反水列表
	 * [detail description]
	 * @return [type] [description]
	 */
	function usermoneyRecord() {
		$account_name = Request::has('account_name') ? Request::get('account_name') : '';
		$dt = \App\Lib\DataTable\DataTable::init();
		$dt->setDataSource('/admin/money/moneyList?account_name=' . $account_name . '&sureblur=精确');
		$dt->setLang('money');
		$dt->setPriKey('trade_id');
		$dt->addColsFields('id', array('width' => 100));
		$dt->addColsFields('account_name', array('width' => 140));
		$dt->addColsFields('money_time', array('width' => 200));
		$dt->addColsFields('systemuser_ordertype', array('sort' => false, 'width' => 160));
		$dt->addColsFields('money', array('width' => 120));
		$dt->addColsFields('reason', array('sort' => false));
		$dt->setToolBar(array('view'), array('width' => 80));
		return view('admin.view', $dt->render());
	}

	//查看存取反水
	function View(Req $req) {
		$order_id = $req->trade_id;
		if (empty($order_id)) {
			abort(404);
		}
		$money_db = new \App\Models\Money_details;
		$bank_db = new \App\Models\Account_bank;
		$data = $money_db->getOrderDetails($order_id, 3);
		$bank_data = $bank_db->getInfoByUid($data['account_identity']);

		$data['bank_name'] = isset($bank_data['bank_name']) ? $bank_data['bank_name'] : '';
		$data['bank_address'] = isset($bank_data['bank_address']) ? $bank_data['bank_address'] : '';
		$data['bank_number'] = isset($bank_data['bank_number']) ? $bank_data['bank_number'] : '';
		$data['name'] = isset($bank_data['account_name']) ? $bank_data['account_name'] : '';
		return view('admin.MoneyManage/moneyView', ['data' => $data]);
	}
	//获取按照类型统计
	function getCount(Req $req) {
		$startTime = $req->startTime;
		$endTime = $req->endTime;
		$type = $req->type;
		$res = $this->getSumMoney($startTime, $endTime, $type);
		return responseToJson($res);
	}
	//按照类型统计总金额
	function getSumMoney($where, $type = '') {
		$db = new \App\Models\Money_details;
		if ($type) {
			$where[] = array('trade_type', '=', $type);
		} else {
			//获取总存取反水金额
			return $db->getTotal($where);
		}
		//规定的类型总数
		return $db->sumMoney($where);
	}
	//入款汇总
	function getMoneySum($where, $type = '') {
		$db = new \App\Models\Money_details;
		$where = array();
		if ($type) {
			$where[] = array('trade_type', '=', $type);
		} else {
			//获取总存取反水金额
			return $db->getIntoTotal($where);
		}
		//规定的类型总数
		return $db->sumIntoMoney($where);
	}
	function getActive($where, $type) {
		$db = new \App\Models\Money_details;
		return $db->getActiveMoney($where, $type);
	}
	//统计总数
	function allTotal(Req $req) {
		$startTime = date('Y-m-d 00:00:00');
		$endTime = date('Y-m-d 23:59:59');
		$data = array();
		$where = $req->where;
		if (count($where) == 0) {
			$where[] = array('money_time', '>=', $startTime);
			$where[] = array('money_time', '<=', $endTime);
		}
		$data['totalMoney'] = $this->getSumMoney($where);
		$data['onlineRecharge'] = $this->getSumMoney($where, 6); //在线充值
		$data['backstageRecharge'] = $this->getSumMoney($where, 11); //后台充值
		$data['remit'] = $this->getSumMoney($where, 14); //14汇款
		$data['rechargePrize'] = $this->getSumMoney($where, 13); //13充值赠送
		//活动金额
		$data['active_cut_money'] = $this->getActive($where, 2); //活动扣除
		$data['active_add_money'] = $this->getActive($where, 1); //活动加款
		$data['active_money'] = $this->getSumMoney($where, 12); //总活动

		$data['back_water'] = $this->getSumMoney($where, 7); //反水
		$data['return_water'] = $this->getSumMoney($where, 8); //回水
		$data['take_money'] = $this->getSumMoney($where, 5); //5前台提现
		$data['cut_money'] = $this->getSumMoney($where, 9); //管理员扣款
		$data['intoTotalMoney'] = $data['onlineRecharge'] + $data['backstageRecharge'] + $data['remit']; //总入款扣除活动充值赠送
		$data['cut_total_money'] = $data['cut_money'] + $data['take_money']; //扣款总额扣除活动充值赠送
		$data['profit'] = $data['intoTotalMoney'] - $data['cut_total_money'];
		$data['all_save_money'] = $data['intoTotalMoney'] + $data['active_add_money'] + $data['rechargePrize'] + $data['back_water'] + $data['return_water']; //总存款
		$data['all_take_money'] = $data['take_money'] + $data['cut_money'] + $data['active_cut_money']; //总扣款
		return responseToJson($data);
	}

}

?>