<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/8
 * Time: 下午3:25
 */

namespace App\Http\Controllers\Manage\Finance;


use App\Http\Controllers\Manage\Finance\Transformers\PaymentStatisticTransformer;
use App\Modules\Finance\Services\PaymentStatisticService;
use Illuminate\Http\Request;

class PaymentStatisticController extends BaseController
{

    /**
     * @apiVersion 1.0.0
     * @apiDescription 每天对账统计
     * @api {GET} paymentStaticDay 每天对账统计
     * @apiGroup Finance
     * @apiName paymentStaticDay
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     * @apiSuccess {Number}  amount 打款成功金额.
     * @apiSuccess {Number}  amount_num 打款成功笔.
     * @apiSuccess {Number}  amount_person 对私打款成功金额.
     * @apiSuccess {Number}  amount_person_num 对私打款成功笔.
     * @apiSuccess {Number}  amount_company 对公打款成功金额.
     * @apiSuccess {Number}  amount_company_num 对公打款成功笔.
     * @apiSuccess {Number}  amount_audit_fail 审核不通过金额.
     * @apiSuccess {Number}  amount_audit_fail_num 审核不通过笔数.
     * @apiSuccess {Number}  amount_fail 打款失败金额.
     * @apiSuccess {Number}  amount_fail_num 打款失败笔数.
     * @apiSuccess {Number}  tallage 扣税金额.
     * @apiSuccess {String}  created_time 每天对账统计时间
     * @apiSuccessExample {json} Success-Response:
     *
     *     {
     *         "code": 0,
     *         "msg": "",
     *         "data":{
     *              "list": [
     *                      {
     *                          "amount": "2664.0000",
     *                          "amount_num": "9",
     *                          "amount_person": "2664.0000",
     *                          "amount_person_num": "9",
     *                          "amount_company": "2664.0000",
     *                          "amount_company_num": "9",
     *                          "amount_audit_fail": "6660.0000",
     *                          "amount_audit_fail_num": "12",
     *                          "amount_fail": "3663.0000",
     *                          "amount_fail_num": "9",
     *                          "tallage": "264.0000",
     *                          "created_time": "2017-12-04 00:00:00"
     *                      }
     *                   ],
     *                     "meta": {
     *                           "total": 1,
     *                           "per_page": 15,
     *                           "current_page": 1,
     *                          "last_page": 1,
     *                          "next_page_url": "",
     *                          "prev_page_url": ""
     *                   }
     *              }
     *     }
     */
    function get_list_day(Request $request) {
        $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);

        $payments = PaymentStatisticService::getGroupByDayDataStatistic($start_time, $end_time);

        return response()->pagination(new PaymentStatisticTransformer(), $payments);
    }

    /**
     * @apiVersion 1.0.0
     * @apiDescription 每天对账统计导出
     * @api {GET} paymentStaticDayExport 每天对账统计导出
     * @apiGroup Finance
     * @apiName paymentStaticDayExport
     * @apiParam   {String}  [start_time] 开始时间(可不传)
     * @apiParam   {String}  [end_time] 结束时间(可不传)
     */
    function get_list_day_export(Request $request) {
        $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);

        $payments = PaymentStatisticService::getGroupByDayDataStatistic($start_time, $end_time, true);

        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=" . "对账统计" . date("YmdHis") . ".csv");
        echo iconv("UTF-8","GBK","\"操作日期\",\"打款成功金额\",\"打款成功笔数\",\"对私成功金额\",\"对私成功笔数\",\"对公成功金额\",\"对公成功笔数\",\"审核不通过金额\",\"审核不通过笔数\",\"打款失败金额\",\"打款失败笔数\",\"扣税金额\"\r\n");
        if($payments)
        {
            foreach($payments as $item)
            {
                echo("\"" . iconv("UTF-8","GBK",date('Y-m-d H:i:s',strtotime($item->created_at))) .  "\",");
                echo("\"" . $item->amount .  "\",");
                echo("\"" . $item->amount_num .  "\",");
                echo("\"" . $item->amount_person .  "\",");
                echo("\"" . $item->amount_person_num .  "\",");
                echo("\"" . $item->amount_company .  "\",");
                echo("\"" . $item->amount_company_num .  "\",");
                echo("\"" . $item->amount_audit_fail .  "\",");
                echo("\"" . ($item->amount_audit_fail_num) .  "\",");
                echo("\"" . $item->amount_fail .  "\",");
                echo("\"" . $item->amount_fail_num .  "\",");
                echo("\"" . $item->tallage .  "\"\r\n");
            }
        }
        exit();
    }
}