<?php

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\BillTransformer;
use App\Modules\Finance\Services\BillService;
use Illuminate\Http\Request;


class BillsController extends BaseController
{
    /**
     * @apiDefine Finance 结算提现模块
     */

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取结算列表
     * @api {GET} bills 获取结算列表
     * @apiGroup Finance
     * @apiName bills
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间2017-01-01.(可不传)
     * @apiParam   {String}  [end_time] 结束时间2017-02-01.(可不传)
     * @apiSuccess   {String}  channel_id 渠道ID.
     * @apiSuccess   {String}  channel_name 渠道名称.
     * @apiSuccess   {String}  date 日期.
     * @apiSuccess   {Number}  recharge_amount 充值金额.
     * @apiSuccess   {Number}  settlement_price 结算金额.
     * @apiSuccess   {Number}  tallage 扣税金额.
     * @apiSuccess   {String}  rate 比例.
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":
     *              {
     *                  "list":[
     *                             {
     *                               "channel_id": "1",
     *                               "channel_name": "张三渠道",
     *                               "date": "2017-11-18",
     *                               "recharge_amount": 1100,
     *                               "settlement_price": 440,
     *                               "tallage": 0,
     *                               "rate": "0.40%"
     *                               }
     *                          ]
     *                   "meta":{
     *                      "total": 4,
     *                       "per_page": 15,
     *                       "current_page": 1,
     *                       "last_page": 1,
     *                       "next_page_url": "",
     *                       "prev_page_url": ""
     *                          }
     *               }
     *     }
     */
	function get_list(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') : '';

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

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

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'search_name'=>$search_name,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
        ];
        $bills = BillService::getBillList($params);
        return response()->pagination(new BillTransformer(), $bills);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 获取结算导出
     * @api {GET} exportBills 获取结算导出
     * @apiGroup Finance
     * @apiName exportBills
     * @apiParam   {Number}  [distribution_channel_id] 渠道id.(可不传，获取所有渠道)
     * @apiParam   {String}  [distribution_channel_name] 渠道名称.(可不传，获取所有渠道)
     * @apiParam   {String}  [search_name] 搜索名称
     * @apiParam   {String}  [start_time] 开始时间2017-01-01.(可不传)
     * @apiParam   {String}  [end_time] 结束时间2017-02-01.(可不传)
     */
    function 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') : '';
        $start_time = $request->has('start_time') ? date('Ymd',strtotime($request->input('start_time'))) : '';
        $end_time = $request->has('end_time') ? date('Ymd',strtotime($request->input('end_time'))) : '';
        $end_time = self::getMaxDay($end_time);

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

        $params = [
            'channel_id'=>$distribution_channel_id,
            'channel_name'=>$distribution_channel_name,
            'search_name'=>$search_name,
            'start_date'=>$start_time,
            'end_date'=>$end_time,
        ];
        $bills = BillService::getBillList($params, true);

        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "结算" . date("YmdHis") . ".csv");
        echo iconv("UTF-8","GBK","\"结算日期\",\"充值金额\",\"佣金比例\",\"结算后金额\"\r\n");
        if($bills)
        {
            foreach($bills as $bill)
            {

                echo("\"" . iconv("UTF-8","GBK",date('Y-m-d',strtotime($bill->date))) .  "\",");
                echo("\"" . (float)$bill->recharge_amount .  "\",");
                echo("\"" . ($bill->rate).'%' .  "\",");
                echo("\"" . (float)$bill->settlement_price.  "\"\r\n");

            }
        }
        exit();
    }
}