quanwei
2025-11-21 2d9362ae6f528f57e6133d5d80f0b633c24e8eb6
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
 
 
namespace app\common\model\store;
 
use app\common\model\BaseModel;
 
/**
 * 商家门店核销优惠券记录模型
 */
class Coupon extends BaseModel
{
    protected $pk = 'id';
    protected $name = 'store_coupon';
    protected $updateTime = false;
 
    /**
     * 关联门店表
     */
    public function store()
    {
        return $this->BelongsTo("app\\common\\model\\store\\Store", 'store_id', 'store_id');
    }
 
    /**
     * 关联店员表
     */
    public function clerk()
    {
        return $this->BelongsTo("app\\common\\model\\store\\Clerk", 'clerk_id', 'clerk_id');
    }
 
    /**
     * 关联用户优惠券表
     */
    public function usercoupon()
    {
        return $this->BelongsTo("app\\common\\model\\plus\\coupon\\UserCoupon", 'user_coupon_id', 'user_coupon_id');
    }
 
    /**
     * 关联供应商表
     */
    public function supplier()
    {
        return $this->belongsTo('app\\common\\model\\supplier\\Supplier', 'shop_supplier_id', 'shop_supplier_id')->field(['shop_supplier_id', 'name']);
    }
 
    /**
     * 新增核销记录
     */
    public static function add(
        $user_coupon_id,
        $project_id,
        $cost_price,
        $store_id,
        $clerk_id,
        $shop_supplier_id
    )
    {
        return (new static)->save([
            'user_coupon_id' => $user_coupon_id,
            'project_id' => $project_id,
            'cost_price' => $cost_price,
            'store_id' => $store_id,
            'clerk_id' => $clerk_id,
            'shop_supplier_id' => $shop_supplier_id,
            'app_id' => self::$app_id
        ]);
    }
    /**
     * 核销优惠券详情
     */
    public static function detail($where)
    {
        $filter = is_array($where) ? $where : ['user_coupon_id' => $where];
        return (new static())->where($filter)->find();
    }
    
    /**
     * 统计优惠券当天核销数量
     */
    public static function getVerifyNum($user_coupon_id, $project_id) {
        $startTime = strtotime(date('Y-m-d'));
        return (new static())->where('create_time', '>=', $startTime)
            ->where('create_time', '<', $startTime + 86400)
            ->where('user_coupon_id', '=', $user_coupon_id)
            ->where('project_id', '=', $project_id)
            ->count();
    }
 
    /**
     * 统计商户核销的优惠券金额 by yj 2024.3.16
     */
    public static function getCouponMoneyBySupplier($shop_supplier_id,$create_time='')
    {
        $model = new static();
        //搜索时间段
        if (!empty($create_time)) {
            $sta_time = array_shift($create_time);
            $end_time = array_pop($create_time);
            $model = $model->whereBetweenTime('store_coupon.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
        }
        $money = $model->alias("store_coupon")
            ->join('store', 'store_coupon.store_id = store.store_id', 'left')
            ->where('store.shop_supplier_id', '=', $shop_supplier_id)
            ->sum("cost_price");
 
        return $money;
    }
 
    /**
     * 统计商户核销的优惠券数量 by yj 2024.3.16
     */
    public static function getCouponNumBySupplier($shop_supplier_id,$create_time='')
    {
        $model = new static();
        //搜索时间段
        if (!empty($create_time)) {
            $sta_time = array_shift($create_time);
            $end_time = array_pop($create_time);
            $model = $model->whereBetweenTime('store_coupon.create_time', $sta_time, date('Y-m-d 23:59:59', strtotime($end_time)));
        }
        $num= $model->alias("store_coupon")
            ->join('store', 'store_coupon.store_id = store.store_id', 'left')
            ->where('store.shop_supplier_id', '=', $shop_supplier_id)
            ->count();
 
        return $num;
    }
}