quanwei
2 days ago 73b874c72ad55eb9eef21c36160ac0de58f0189e
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
<?php
 
namespace app\common\model\plus\operations;
use app\common\enum\order\OrderTypeEnum;
use app\common\model\BaseModel;
use app\common\model\settings\Setting as SettingModel;
use app\common\model\plus\operations\Operations as OperationsModel;
/**
 * 运营中心订单管理
 */
class Order extends BaseModel
{
    protected $name="operations_order";
 
    /**
     * 订单所属用户
     * @return \think\model\relation\BelongsTo
     */
    public function user()
    {
        return $this->belongsTo('app\common\model\user\User');
    }
 
    /**
     * 一级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function agentFirst()
    {
        return $this->belongsTo('app\common\model\user\User', 'first_user_id');
    }
 
    /**
     * 二级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function agentSecond()
    {
        return $this->belongsTo('app\common\model\user\User', 'second_user_id');
    }
 
    /**
     * 三级分销商用户
     * @return \think\model\relation\BelongsTo
     */
    public function agentThird()
    {
        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];
    }
 
    /**
     * 订单详情
     */
    public static function getDetailByOrderId($orderId, $orderType)
    {
        return (new static())->where('order_id', '=', $orderId)
            ->where('order_type', '=', $orderType)
            ->find();
    }
    public static function grantMoney($order, $orderType = OrderTypeEnum::MASTER)
    {
        // 订单是否已完成
        if ($order['order_status']['value'] != 30) {
            return false;
        }
        // 分销订单详情
        $model = self::getDetailByOrderId($order['order_id'], $orderType);
        if (!$model || $model['is_settled'] == 1) {
            return false;
        }
        // 佣金结算天数
        $settleDays = Setting::getItem('settlement', $order['app_id'])['settle_days'];
        // 写入结算时间
        $deadlineTime = $model['settle_end_time'];
        if($deadlineTime == 0){
            $deadlineTime = $order['receipt_time'] + $settleDays  * 86400;
            $model->save([
                'settle_end_time' => $deadlineTime
            ]);
        }
        if ($deadlineTime > time()) {
            return false;
        }
 
        // 重新计算分销佣金
        $capital = $model->getCapitalByOrder($order);
        // 发放一级分销商佣金
        $model['first_user_id'] > 0 && OperationsModel::grantMoney($model['first_user_id'], $capital['first_money']);
        // 发放二级分销商佣金
        $model['second_user_id'] > 0 && OperationsModel::grantMoney($model['second_user_id'], $capital['second_money']);
        // 发放三级分销商佣金
        $model['third_user_id'] > 0 && OperationsModel::grantMoney($model['third_user_id'], $capital['third_money']);
        // 更新分销订单记录
        $model->save([
            'order_price' => $capital['orderPrice'],
            'first_money' => $model['first_user_id'] > 0? $capital['first_money']:0,
            'second_money' => $model['second_user_id'] > 0? $capital['second_money']:0,
            'third_money' => $model['third_user_id'] > 0? $capital['third_money']:0,
            'is_settled' => 1,
            'settle_time' => time()
        ]);
        event('AgentUserGrade', $model['first_user_id']);
        event('AgentUserGrade', $model['second_user_id']);
        event('AgentUserGrade', $model['third_user_id']);
        // 更新队长等级 by lyzflash
        event('TeamUserGrade', $model['first_user_id']);
        event('TeamUserGrade', $model['second_user_id']);
        event('TeamUserGrade', $model['third_user_id']);
        // 更新股东等级 by lyzflash
        event('ShareholderUserGrade', $model['first_user_id']);
        event('ShareholderUserGrade', $model['second_user_id']);
        event('ShareholderUserGrade', $model['third_user_id']);
        return true;
    }
    /**
     * 获取订单分销佣金数据
     * @param $order
     * @param string $source
     * @param array $operations
     * @return array
     */
    protected function getCapitalByOrder($order, $source = 'create',$operations=[])
    {
        $setting = Setting::getItem('bonus', $order['app_id']);
        // 分销订单佣金数据
        $capital = [
            // 订单总金额(不含运费)
            'orderPrice' => bcsub($order['pay_price'], $order['express_price'], 2),
            // 一级分销佣金
            'first_money' => 0.00,
            // 二级分销佣金
            'second_money' => 0.00,
            // 三级分销佣金
            'third_money' => 0.00,
            // 是否记录
            'is_record' => true
        ];
        $total_count = count($order['product']);
        // 计算分销佣金
        foreach ($order['product'] as $product) {
            // 商品实付款金额
            $productPrice = min($capital['orderPrice'], $product['total_pay_price']);
            // 计算商品实际佣金
            $productCapital = $this->calculateProductCapital($setting, $productPrice,$product);
            // 累积分销佣金
            $capital['first_money'] += $productCapital['first_money'];
            $capital['second_money'] += $productCapital['second_money'];
            $capital['third_money'] += $productCapital['third_money'];
        }
        return $capital;
    }
    /**
     * 计算商品实际佣金 by yj
     * @param $setting
     * @param $productPrice
     * @return float[]|int[]
     */
    private function calculateProductCapital($setting, $productPrice,$product)
    {
        $storeSetting=SettingModel::getItem('store', $product['app_id']);
        $productMoney=bcmul($productPrice, bcdiv($storeSetting['commission_rate'],100,2), 2);
        $capital['first_money'] = bcmul($productMoney, bcdiv($setting['province_ratio'],100,2), 2);
        $capital['second_money'] = bcmul($productMoney, bcdiv($setting['city_ratio'],100,2), 2);
        $capital['third_money'] = bcmul($productMoney, bcdiv($setting['area_ratio'],100,2), 2);
        return $capital;
    }
}