<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/11/22
 * Time: 上午10:14
 */

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\FinancialStatDetailTransformer;
use App\Http\Controllers\Manage\Finance\Transformers\FinancialStatTransformer;
use App\Modules\Finance\Services\FinancialStatService;
use Illuminate\Http\Request;

class FinancialStatController extends BaseController
{

    /**
     * @apiDefine Finance 结算提现模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取提现汇总信息
     * @api {GET} getFinancialStat 获取提现汇总信息
     * @apiGroup Finance
     * @apiName getFinancialStat
     * @apiParam   {Number}  distribution_channel_id 渠道id.
     * @apiSuccess   {String}  channel_id 渠道id.
     * @apiSuccess   {Number}  accumulative_amount 累计提现金额.
     * @apiSuccess   {Number}  enable_amount 可提现金额.
     * @apiSuccess   {Number}  latest_amount 最近提现金额.
     * @apiSuccess   {Number}  pending_amount 提现中金额.
     * @apiSuccess   {String}  latest_time 最后提现时间.
     * @apiSuccess   {String}  rate 当前佣金比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                   "channel_id": 1,
     *                   "accumulative_amount": "0.00",
     *                   "enable_amount": "2170.00",
     *                   "latest_amount": "0.00",
     *                   "latest_time": "",
     *                   "pending_amount": "0.00",
     *                   "rate": "0.60%"
     *              }
     *     }
     */
    function get_FinancialStatInfo(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        if(empty($distribution_channel_id)) {
            return response()->error("PARAM_EMPTY");
        }

        $result = FinancialStatService::getFinancialStatSingle($distribution_channel_id);

        return response()->item(new FinancialStatTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取账号信息列表
     * @api {GET} financialStats 获取账号信息列表
     * @apiGroup Finance
     * @apiName financialStats
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.
     * @apiParam   {String}  [person_in_charge_name] 渠道负责人名称.
     * @apiParam   {String}  [search_name] 搜索名称.
     * @apiParam   {Number}  [is_frozen] 0:解冻； -1:冻结状态.
     * @apiParam   {String}  [ac_start_time] 账号创建开始时间2017-01-01
     * @apiParam   {String}  [ac_end_time] 账号创建结束时间2017-02-01
     * @apiParam   {String}  [frozen_start_time] 账号冻结开始时间2017-01-01
     * @apiParam   {String}  [frozen_end_time] 账号冻结结束时间2017-02-01
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     * @apiSuccess   {String}  channel_id 渠道id.
     * @apiSuccess   {Number}  accumulative_amount 累计提现金额.
     * @apiSuccess   {Number}  enable_amount 可提现金额.
     * @apiSuccess   {Number}  latest_amount 最近提现金额.
     * @apiSuccess   {Number}  pending_amount 提现中金额.
     * @apiSuccess   {String}  latest_time 最后提现时间.
     * @apiSuccess   {String}  rate 当前佣金比例.
     * @apiSuccess   {String}  phone 电话号码.
     * @apiSuccess   {String}  name 渠道名称.
     * @apiSuccess   {String}  pay_merchant 支付方式.
     * @apiSuccess   {String}  nickname 渠道昵称.
     * @apiSuccess   {String}  channel_remark 渠道备注.
     * @apiSuccess   {String}  channel_created_time 渠道创建时间.
     * @apiSuccess   {String}  fc_remark 冻结备注.
     * @apiSuccess   {String}  person_in_charge_name 渠道负责人
     * @apiSuccess   {String}  frozen_time 渠道冻结解冻时间
     * @apiSuccess   {Number}  is_company 0:对私, 1:对公司
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                   "list": [
     *                       {
     *                           "channel_id": 1,
     *                           "accumulative_amount": "1000.00",
     *                           "enable_amount": "11170.00",
     *                           "latest_amount": "0.00",
     *                           "latest_time": "2017-11-28 15:43:39",
     *                           "pending_amount": "1600.00",
     *                           "rate": "0.60%",
     *                           "phone": "",
     *                           "name": "sfsd",
     *                           "pay_merchant": "34",
     *                           "nickname": "",
     *                           "channel_remark": ""
     *                           "channel_created_time": "2017-11-28 15:43:39",
     *                           "frozen_time": "2017-11-28 15:43:39",
     *                           "person_in_charge_name": "",
     *                           "fc_remark": ""
     *                           "is_company": 0
     *                       }
     *                      "meta": {
     *                       ],
     *                           "total": 2,
     *                           "per_page": 15,
     *                           "current_page": 1,
     *                           "last_page": 1,
     *                           "next_page_url": "",
     *                           "prev_page_url": ""
     *                       }
     *              }
     *     }
     */
    function get_FinancialStatListDetail(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $person_in_charge_name = $request->has('person_in_charge_name') ? $request->input('person_in_charge_name') : '';
        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $is_frozen = '';
        if(is_numeric($isFrozen) && ($isFrozen == -1 || $isFrozen == 0)) {
            $is_frozen = $isFrozen;
        }
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        $ac_start_time = $request->has('ac_start_time') && !empty($request->input('ac_start_time')) ? date('Ymd',strtotime($request->input('ac_start_time'))) : '';
        $ac_end_time = $request->has('ac_end_time') && !empty($request->input('ac_end_time')) ? date('Ymd',strtotime($request->input('ac_end_time'))) : '';
        $ac_end_time = self::getMaxDay($ac_end_time);

        $frozen_start_time = $request->has('frozen_start_time') && !empty($request->input('frozen_start_time')) ? date('Ymd',strtotime($request->input('frozen_start_time'))) : '';
        $frozen_end_time = $request->has('frozen_end_time') && !empty($request->input('frozen_end_time')) ? date('Ymd',strtotime($request->input('frozen_end_time'))) : '';
        $frozen_end_time = self::getMaxDay($frozen_end_time);

        $is_company = $request->has('is_company') ? $request->input('is_company') : '';

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'person_in_charge_name'=>$person_in_charge_name,
            'search_name'=>$search_name,
            'ac_start_time'=>$ac_start_time,
            'ac_end_time'=>$ac_end_time,
            'is_frozen'=>$is_frozen,
            'frozen_start_time'=>$frozen_start_time,
            'frozen_end_time'=>$frozen_end_time,
            'is_company'=>$is_company,
        ];
        $result = FinancialStatService::getFrozenFinancialStatListParam($params);

        return response()->pagination(new FinancialStatDetailTransformer(), $result);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 结算明细
     * @api {GET} financialStatSettlementPriceDetail 结算明细
     * @apiGroup Finance
     * @apiName financialStatSettlementPriceDetail
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.
     * @apiParam   {String}  [person_in_charge_name] 渠道负责人名称.
     * @apiParam   {String}  [search_name] 搜索名称.
     * @apiParam   {Number}  [is_frozen] 0:解冻； -1:冻结状态.
     * @apiParam   {String}  [ac_start_time] 账号创建开始时间2017-01-01
     * @apiParam   {String}  [ac_end_time] 账号创建结束时间2017-02-01
     * @apiParam   {String}  [frozen_start_time] 账号冻结开始时间2017-01-01
     * @apiParam   {String}  [frozen_end_time] 账号冻结结束时间2017-02-01
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     * @apiSuccess   {String}  channel_id 渠道id.
     * @apiSuccess   {Number}  accumulative_amount 累计提现金额.
     * @apiSuccess   {Number}  enable_amount 可提现金额.
     * @apiSuccess   {Number}  latest_amount 最近提现金额.
     * @apiSuccess   {Number}  pending_amount 提现中金额.
     * @apiSuccess   {String}  latest_time 最后提现时间.
     * @apiSuccess   {String}  rate 当前佣金比例.
     * @apiSuccess   {String}  phone 电话号码.
     * @apiSuccess   {String}  name 渠道名称.
     * @apiSuccess   {String}  pay_merchant 支付方式.
     * @apiSuccess   {String}  nickname 渠道昵称.
     * @apiSuccess   {String}  channel_remark 渠道备注.
     * @apiSuccess   {String}  channel_created_time 渠道创建时间
     * @apiSuccess   {String}  person_in_charge_name 渠道负责人
     * @apiSuccess   {String}  frozen_time 渠道冻结解冻时间
     * @apiSuccess   {String}  fc_remark 冻结备注.
     * @apiSuccess   {Number}  recharge_amount_count 总充值金额.
     * @apiSuccess   {Number}  settlement_price_count 总结算金额.
     * @apiSuccess   {Number}  tallage 手续费
     * @apiSuccess   {Number}  is_company 0:对私, 1:对公司
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *                   "list": [
     *                       {
     *                           "channel_id": 1,
     *                           "accumulative_amount": "1000.00",
     *                           "enable_amount": "11170.00",
     *                           "latest_amount": "0.00",
     *                           "latest_time": "2017-11-28 15:43:39",
     *                           "pending_amount": "1600.00",
     *                           "rate": "0.60%",
     *                           "phone": "",
     *                           "name": "sfsd",
     *                           "pay_merchant": "34",
     *                           "nickname": "",
     *                           "channel_remark": "",
     *                           "channel_created_time": "2017-11-28 15:43:39",
     *                           "frozen_time": "2017-11-28 15:43:39",
     *                           "person_in_charge_name": "",
     *                           "fc_remark": "",
     *                           "recharge_amount_count": "6300.00",
     *                           "settlement_price_count": "6300.00",
     *                           "tallage": 0
     *                           "is_company": 0
     *                       }
     *                      "meta": {
     *                       ],
     *                           "total": 2,
     *                           "per_page": 15,
     *                           "current_page": 1,
     *                           "last_page": 1,
     *                           "next_page_url": "",
     *                           "prev_page_url": ""
     *                       }
     *              }
     *     }
     */
    function settlement_price_detail(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $person_in_charge_name = $request->has('person_in_charge_name') ? $request->input('person_in_charge_name') : '';
        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $is_frozen = '';
        if(is_numeric($isFrozen) && ($isFrozen == -1 || $isFrozen == 0)) {
            $is_frozen = $isFrozen;
        }
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        $ac_start_time = $request->has('ac_start_time') && !empty($request->input('ac_start_time')) ? date('Ymd',strtotime($request->input('ac_start_time'))) : '';
        $ac_end_time = $request->has('ac_end_time') && !empty($request->input('ac_end_time')) ? date('Ymd',strtotime($request->input('ac_end_time'))) : '';
        $ac_end_time = self::getMaxDay($ac_end_time);

        $frozen_start_time = $request->has('frozen_start_time') && !empty($request->input('frozen_start_time')) ? date('Ymd',strtotime($request->input('frozen_start_time'))) : '';
        $frozen_end_time = $request->has('frozen_end_time') && !empty($request->input('frozen_end_time')) ? date('Ymd',strtotime($request->input('frozen_end_time'))) : '';
        $frozen_end_time = self::getMaxDay($frozen_end_time);

        $is_company = $request->has('is_company') ? $request->input('is_company') : '';

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'person_in_charge_name'=>$person_in_charge_name,
            'search_name'=>$search_name,
            'ac_start_time'=>$ac_start_time,
            'ac_end_time'=>$ac_end_time,
            'is_frozen'=>$is_frozen,
            'frozen_start_time'=>$frozen_start_time,
            'frozen_end_time'=>$frozen_end_time,
            'is_company'=>$is_company,
        ];

        $result = FinancialStatService::getFrozenFinancialStatPriceListDetail($params);
        return response()->pagination(new FinancialStatDetailTransformer(), $result);
    }



    /**
     * @apiVersion 1.0.0
     * @apiDescription 结算明细导出
     * @api {GET} financialStatSettlementPriceDetailExport 结算明细导出
     * @apiGroup Finance
     * @apiName financialStatSettlementPriceDetailExport
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.
     * @apiParam   {String}  [person_in_charge_name] 渠道负责人名称.
     * @apiParam   {String}  [search_name] 搜索名称.
     * @apiParam   {Number}  [is_frozen] 0:解冻； -1:冻结状态.
     * @apiParam   {String}  [ac_start_time] 账号创建开始时间2017-01-01
     * @apiParam   {String}  [ac_end_time] 账号创建结束时间2017-02-01
     * @apiParam   {String}  [frozen_start_time] 账号冻结开始时间2017-01-01
     * @apiParam   {String}  [frozen_end_time] 账号冻结结束时间2017-02-01
     * @apiParam   {Number}  [is_company] 0:对私, 1:对公司.(可不传，获取所有渠道)
     */
    function settlement_price_detail_export(Request $request) {
        $distribution_channel_id = $request->has('distribution_channel_id') ? $request->input('distribution_channel_id') : '';
        $distribution_channel_name = $request->has('distribution_channel_name') ? $request->input('distribution_channel_name') : '';
        $person_in_charge_name = $request->has('person_in_charge_name') ? $request->input('person_in_charge_name') : '';
        $isFrozen = $request->has('is_frozen') ? $request->input('is_frozen') : '';
        $is_frozen = '';
        if(is_numeric($isFrozen) && ($isFrozen == -1 || $isFrozen == 0)) {
            $is_frozen = $isFrozen;
        }
        $search_name = $request->has('search_name') ? $request->input('search_name') : '';

        $ac_start_time = $request->has('ac_start_time') && !empty($request->input('ac_start_time')) ? date('Ymd',strtotime($request->input('ac_start_time'))) : '';
        $ac_end_time = $request->has('ac_end_time') && !empty($request->input('ac_end_time')) ? date('Ymd',strtotime($request->input('ac_end_time'))) : '';
        $ac_end_time = self::getMaxDay($ac_end_time);

        $frozen_start_time = $request->has('frozen_start_time') && !empty($request->input('frozen_start_time')) ? date('Ymd',strtotime($request->input('frozen_start_time'))) : '';
        $frozen_end_time = $request->has('frozen_end_time') && !empty($request->input('frozen_end_time')) ? date('Ymd',strtotime($request->input('frozen_end_time'))) : '';
        $frozen_end_time = self::getMaxDay($frozen_end_time);

        $is_company = $request->has('is_company') ? $request->input('is_company') : '';

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'person_in_charge_name'=>$person_in_charge_name,
            'search_name'=>$search_name,
            'ac_start_time'=>$ac_start_time,
            'ac_end_time'=>$ac_end_time,
            'is_frozen'=>$is_frozen,
            'frozen_start_time'=>$frozen_start_time,
            'frozen_end_time'=>$frozen_end_time,
            'is_company'=>$is_company,
        ];

        $result = FinancialStatService::getFrozenFinancialStatPriceListDetail($params);


        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "结算明细" . date("YmdHis") . ".csv");
        echo iconv("UTF-8","GBK","\"渠道ID\",\"渠道名称\",\"对私、对公\",\"充值金额\",\"佣金比例\",\"结算金额(元)\",\"扣税金额(元)\",\"提现金额(元)\",\"账户最新余额(元)\"\r\n");
        if($result)
        {
            foreach($result as $item)
            {
                echo("\"" . $item->id .  "\",");
                echo("\"" . iconv("UTF-8","GBK",$item->name) .  "\",");
                echo("\"" . $item->is_company .  "\",");
                echo("\"" . $item->recharge_amount_count .  "\",");
                echo("\"" . ($item->commission_rate * 100)."%" .  "\",");
                echo("\"" . $item->settlement_price_count .  "\",");
                echo("\"" . $item->tallage .  "\",");
                echo("\"" . $item->accumulative_withdrawal_amount .  "\",");
                echo("\"" . $item->enable_withdrawal_amount .  "\"\r\n");
            }
        }
        exit();
    }
}