<?php
/**
 * Created by PhpStorm.
 * User: tandunzhao
 * Date: 2017/12/7
 * Time: 上午9:49
 */

namespace App\Modules\Finance\Models;


use Illuminate\Database\Eloquent\Model;

/**
 * 清算明细表
 * Class Liquidation
 * @package App\Modules\Finance\Models
 */
class Liquidation extends Model
{
    protected $table = 'liquidations';
    protected $fillable = ['distribution_channel_id', 'pay_merchant_source','amount', 'date', 'payment_id', 'remark'];

    /**
     * 保存清算明细
     * @param $channel_id 渠道ID
     * @param $pay_merchant_source 渠道支付通道
     * @param $amount 清算金额
     * @param $date 清算时间
     * @param $payment_id 打款ID
     * @param string $remark 备注
     * @return mixed
     */
    public static function addLiquidation($channel_id, $pay_merchant_source, $amount, $date, $payment_id, $remark = '') {
        $data['distribution_channel_id'] = $channel_id;
        $data['pay_merchant_source'] = $pay_merchant_source;
        $data['amount'] = (float)$amount;
        $data['date'] = $date;
        $data['payment_id'] = $payment_id;
        $data['remark'] = $remark;

        return Liquidation::create($data);
    }

    /**
     * 清算明细表列表
     * @param $params[]
     *                  channel_id:渠道ID 可选
     *                  channel_name:渠道名称 可选
     *                  pay_merchant_source 支付通道渠道(可不传，获取所有通道) 可选
     *                  start_date:开始时间 可选
     *                  end_date:结束时间 可选
     *                  search_name: 搜索名称
     * @param $isAll
     * @return mixed
     */
    public static function getLiquidationList($params=[], $isAll = false) {
        $search_object = self::select([
            'liquidations.id',
            'liquidations.distribution_channel_id',
            'liquidations.pay_merchant_source',
            'liquidations.amount',
            'liquidations.payment_id',
            'liquidations.date',
            'liquidations.remark',
            'liquidations.created_at',
            'distribution_channels.name as distribution_channel_name',
            'distribution_channels.phone',
            'distribution_channels.pay_merchant_id',
            'distribution_channels.nickname',
            'distribution_channels.person_in_charge_name'
        ])
            ->leftjoin('distribution_channels','distribution_channels.id','=','liquidations.distribution_channel_id')
            ->where('liquidations.amount', '<>', 0)
            ->orderBy('liquidations.id','desc');

        if(isset($params['channel_id']) && $params['channel_id']) $search_object->where('liquidations.distribution_channel_id', $params['channel_id']);
        if(isset($params['channel_name']) && $params['channel_name']) $search_object->where('distribution_channels.name','like', "%".$params['channel_name']."%");
        if(isset($params['pay_merchant_source']) && $params['pay_merchant_source']) $search_object->where('liquidations.pay_merchant_source', $params['pay_merchant_source']);

        if(isset($params['search_name']) && $params['search_name']) {
            $search_object->where(function ($query) use($params) {
                $query->where('distribution_channels.name','like', "%".$params['search_name']."%")
                    ->orWhere('distribution_channels.nickname', 'like', "%".$params['search_name']."%")
                    ->orWhere('distribution_channels.person_in_charge_name', 'like', "%".$params['search_name']."%");
            });
        }

        if(isset($params['start_date']) && $params['start_date']) $search_object->where('liquidations.created_at','>=', $params['start_date']);
        if(isset($params['end_date']) && $params['end_date']) $search_object->where('liquidations.created_at','<=', $params['end_date']);

        if($isAll) {

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

            return $search_object->paginate();
        }
    }
}