<?php
/**
 * Created by PhpStorm.
 * User: hp
 * Date: 2017/11/22
 * Time: 14:15
 */

namespace App\Modules\Finance\Models;

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

class TransfromAccountRecord extends Model
{
    protected $table = 'transfer_account_record';
    protected $fillable = ['day', 'company_name', 'account', 'fee', 'status', 'remark'];

    /**
     * 添加记录
     */
    static function addTransformAccountRecord($data)
    {
        return self::create($data);
    }

    /**
     * 修改记录
     */
    static function updateTransformAccountRecord($id, $fee, $day, $status, $company_name, $account, $remark)
    {
        $object = self::where('id', $id);
        $params = [];
        if ($fee) {
            $params['fee'] = $fee;
        }

        if ($day) {
            $params['day'] = $day;
        }

        if ($status) {
            $params['status'] = $status;
        }

        if ($company_name) {
            $params['company_name'] = $company_name;
        }

        if ($account) {
            $params['account'] = $account;
        }

        if ($remark) {
            $params['remark'] = $remark;
        }

        return $object->update($params);
    }


    /**
     * 获取到账记录
     * @param array $params
     * @param bool $is_all
     * @return mixed
     */
    static function getTransformAccountRecord($params = [], $is_all = false)
    {
        $search_object = self::orderBy('transfer_account_record.created_at', 'desc');

        if (isset($params['status']) && $params['status']) $search_object->where('status', $params['status']);
        if (isset($params['account']) && $params['account']) $search_object->where('account', $params['account']);
        if (isset($params['end_time']) && $params['end_time']) $search_object->where('day', '<=', $params['end_time']);
        if (isset($params['start_time']) && $params['start_time']) $search_object->where('day', '>=', $params['start_time']);
        if (isset($params['company_name']) && $params['company_name']) $search_object->where('company_name', 'like', '%' . $params['company_name'] . '%');

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