<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/5
 * Time: 下午4:18
 */

namespace App\Modules\Finance\Models;


use Illuminate\Database\Eloquent\Model;
use DB;

/**
 * 打款统计数据
 * Class PaymentStatistic
 * @package App\Modules\Finance\Models
 */
class PaymentStatistic extends Model
{
    protected $table = 'payment_statistics';
    protected $fillable = ['distribution_channel_id', 'distribution_channel_name', 'amount', 'amount_num', 'amount_person', 'amount_person_num', 'amount_company', 'amount_company_num', 'amount_audit_fail', 'amount_audit_fail_num', 'amount_fail', 'amount_fail_num', 'tallage', 'date'];


    /**
     * 获取每日统计报表
     * @param string $start_time
     * @param string $end_time
     * @param bool $isAll
     * @return mixed
     */
    public static function getGroupByDayDataStatistic($start_time = '', $end_time = '', $isAll = false) {
        $search_object = self::select(
            DB::raw('sum(amount) amount'),
            DB::raw('sum(amount_num) amount_num'),
            DB::raw('sum(amount_person) amount_person'),
            DB::raw('sum(amount_person_num) amount_person_num'),
            DB::raw('sum(amount_company) amount_company'),
            DB::raw('sum(amount_company_num) amount_company_num'),
            DB::raw('sum(amount_audit_fail) amount_audit_fail'),
            DB::raw('sum(amount_audit_fail_num) amount_audit_fail_num'),
            DB::raw('sum(amount_fail) amount_fail'),
            DB::raw('sum(amount_fail_num) amount_fail_num'),
            DB::raw('sum(tallage) tallage'),
            'date')
         ->groupBy('date');

        if($start_time) $search_object->where('date','>=',$start_time);
        if($end_time) $search_object->where('date','<=',$end_time);

        if($isAll) {
            return $search_object->get();
        } else {
            return $search_object->paginate();
        }
    }

}