quanwei
18 hours ago c441dea81bd86bdfb12dff35821fed51f4cc91c2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php
 
namespace app\operations\model\supplier;
 
use app\common\model\supplier\DepositRefund as DepositRefundModel;
use app\operations\model\supplier\Supplier as SupplierRegionModel;
use app\supplier\model\supplier\Supplier as SupplierModel;
use app\operations\model\supplier\Capital as CapitalModel;
 
/**
 * 退押金申请模型类
 */
class DepositRefund extends DepositRefundModel
{
    // 定义全局的查询范围
    protected $globalScope = ['status'];
    public function scopeStatus($query)
    {
        $shop_supplier_ids = SupplierRegionModel::getSupplierIdsByUser(session('jjjshop_operations')['user']);
        if (empty($shop_supplier_ids)){
            $query->where('shop_supplier_id', -1);
        }else{
            $query->where('shop_supplier_id', 'in', $shop_supplier_ids);
        }
    }
    /**
     * 获取列表数据
     */
    public function getList($params)
    {
        $model = $this;
        if ($params['status'] != '') {
            $model = $model->where('status', '=', $params['status']);
        }
        // 查询列表数据
        return $model->with(['supplier'])
            ->order(['create_time' => 'desc'])
            ->paginate($params);
    }
 
    /**
     * 退押金审核
     */
    public function submit($param)
    {
        $this->startTrans();
        try {
            $data = ['status' => $param['state']];
           $data['audit_time'] = time();
            // 更新申请记录
            $this->save($data);
            $supplier = SupplierModel::detail($this['shop_supplier_id']);
 
            if ($param['state'] == 2) {//拒绝退押金
                (new SupplierModel())->where(['shop_supplier_id' => $this['shop_supplier_id']])->update(['status' => 0]);
            } else if ($param['state'] == 1) {//同意退押金
                (new SupplierModel())->where(['shop_supplier_id' => $this['shop_supplier_id']])->update(['status' => 20, 'deposit_money' => 0, 'money' => $supplier['money'] + $this['deposit_money'], 'total_money' => $supplier['total_money'] + $this['deposit_money']]);
                $add = [
                    'shop_supplier_id' => $this['shop_supplier_id'],
                    'flow_type' => 10,
                    'money' => $this['deposit_money'],
                    'describe' => '退押金收入',
                ];
                //添加资金明细        
                CapitalModel::add($add);
            }
            $this->commit();
            return true;
        } catch (\Exception $e) {
            $this->error = $e->getMessage();
            $this->rollback();
            return false;
        }
    }
 
    /**
     * 获取退款数
     */
    public static function getRefundCount(){
        return (new static())->where('status', '=', 0)->count();
    }
 
    /**
     * 获取供应商申请统计数量
     */
    public function getRefundData($startDate = null, $endDate = null, $type)
    {
        $model = $this;
        $field = '';
        if($type == 'supplier_refund'){
            $field = 'audit_time';
            $model = $model->where('status', '=', 1);
        } else if($type == 'supplier_refund_apply'){
            $field = 'create_time';
        }
        if(!is_null($startDate)){
            $model = $model->where($field, '>=', strtotime($startDate));
        }
        if(is_null($endDate)){
            $model = $model->where($field, '<', strtotime($startDate) + 86400);
        }else{
            $model = $model->where($field, '<', strtotime($endDate) + 86400);
        }
 
        return $model->count();
    }
}