quanwei
2025-12-13 30563323a53b0d0260c97d08a9e8bd4cc8227a95
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
 
namespace app\common\model\plus\shareholder;
 
use app\common\model\BaseModel;
use app\common\enum\order\OrderTypeEnum;
 
/**
 * 股东订单模型
 */
class Order extends BaseModel
{
    protected $name = 'shareholder_order';
    protected $pk = 'id';
 
    /**
     * 订单所属用户
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\user\User');
    }
 
    /**
     * 股东用户
     * @return \think\model\relation\BelongsTo
     */
    public function shareholderHeads()
    {
        return $this->belongsTo('app\common\model\user\User', 'heads_id');
    }
 
    /**
     * 一级股东用户
     * @return \think\model\relation\BelongsTo
     */
    public function shareholderFirst()
    {
        return $this->belongsTo('app\common\model\user\User', 'first_user_id');
    }
 
    /**
     * 二级股东用户
     * @return \think\model\relation\BelongsTo
     */
    public function shareholderSecond()
    {
        return $this->belongsTo('app\common\model\user\User', 'second_user_id');
    }
 
    /**
     * 三级股东用户
     * @return \think\model\relation\BelongsTo
     */
    public function shareholderThird()
    {
        return $this->belongsTo('app\common\model\user\User', 'third_user_id');
    }
 
    /**
     * 订单类型
     * @param $value
     * @return array
     */
    public function getOrderTypeAttr($value)
    {
        $types = OrderTypeEnum::getTypeName();
        return ['text' => $types[$value], 'value' => $value];
    }
 
    /**
     * 分红类型
     * @param $value
     * @return array
     */
    public function getBonusTypeAttr($value)
    {
        $bonus_type = ['10' => '分红', '20' => '级差奖', '30' => '平级奖'];
        return ['text' => $bonus_type[$value], 'value' => $value];
    }
 
    /**
     * 订单详情
     */
    public static function orderDetail($id)
    {
        return (new static())->where('id', '=', $id)->find();
    }
 
    /**
     * 起始可结算订单
     */
    public static function orderFirst()
    {
        return (new static())->where('is_settled', '=', 0)->order(['create_time' => 'asc'])->find();
    }
 
    /**
     * 订单详情
     */
    public static function getDetailByOrderId($orderId, $orderType)
    {
        return (new static())->where('order_id', '=', $orderId)
            ->where('order_type', '=', $orderType)
            ->select();
    }
 
    // 统计股东团队业绩
    public static function getOrderAllPrice($userId, $bonusType = 10)
    {
        return (new static())->where('heads_id', '=', $userId)
            ->where('bonus_type', '=', $bonusType)
            ->sum('order_price');
    }
 
    /**
     * 创建分红订单记录
     */
    public static function createOrder($order, $order_type = OrderTypeEnum::MASTER)
    {
        // 订单是否已完成
        if ($order['order_status']['value'] != 30) {
            return false;
        }
        // 分红订单模型
        $model = new self;
        // 股东基本设置
        $setting = Setting::getItem('basic', $order['app_id']);
        // 是否开启分红功能
        if (!$setting['is_open']) {
            return false;
        }
        // 如果没有股东,不记录订单
        if(User::shareholderCount() <= 0) {
            return false;
        }
        // 重新整理订单
        $capital = $model->getCapitalByOrder($order);
        return $model->save([
            'user_id' => $order['user_id'],
            'order_id' => $order['order_id'],
            'order_type' => $order_type,
            'total_price' => $capital['total_price'],
            'order_price' => $capital['orderPrice'],
            'is_settled' => 0,
            'shop_supplier_id' => $order['shop_supplier_id'],
            'app_id' => $order['app_id']
        ]);
    }
 
    /**
     * 计算订单金额
     * @param $order
     * @return array
     */
    protected function getCapitalByOrder($order)
    {
        // 分红设置
        $setting = Setting::getItem('basic', $order['app_id']);
        $capital = [
            // 订单总金额(不含运费)
            'total_price' => bcsub($order['pay_price'], $order['express_price'], 2),
            // 订单总金额(不含运费) 减去 结算价 = 订单利润
            'orderPrice' => bcsub(bcsub($order['pay_price'], $order['express_price'], 2), $order['settlement_price'], 2),
            // 是否记录
            'is_record' => true
        ];
        $total_count = count($order['product']);
        $productPriceTotal = 0;
        // 计算分红
        foreach ($order['product'] as $product) {
            // 判断商品存在售后退款则不计算佣金
            if ($this->checkProductRefund($product)) {
                continue;
            }
            // 商品实付款金额
            $productPriceTotal += min($capital['orderPrice'], $product['total_pay_price']);
        }
        if($total_count == 0){
            $capital['is_record'] = false;
        }
        return $capital;
    }
 
    /**
     * 验证商品是否存在售后
     * @param $product
     * @return bool
     */
    private function checkProductRefund($product)
    {
        return !empty($product['refund'])
            && $product['refund']['type']['value'] != 20
            && $product['refund']['is_agree']['value'] != 20;
    }
 
}