| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285 |
- <?php
- /**
- * 厦门云联储网络科技有限公司
- * https://www.baokuaiyun.com
- * Copyright (c) 2023 爆块云 All rights reserved.
- */
- namespace app\models;
- use app\constants\OptionSetting;
- use app\modules\admin\models\GolangForm;
- use app\plugins\scanCodePay\models\Order as ScanOrder;
- use app\models\SupplierPoolDetail;
- use Exception;
- use yii\db\ActiveRecord;
- use yii\behaviors\TimestampBehavior;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%UnitFounder_pool}}".
- *
- * @property integer $id
- * @property integer $config_id
- * @property integer $store_id
- * @property integer $start_time
- * @property integer $end_time
- * @property float $money
- * @property float $rl_rate
- * @property float $mx_rate
- * @property integer $is_send
- * @property integer $is_scan
- * @property integer $send_time
- * @property integer $created_at
- * @property integer $updated_at
- */
- class SupplierPool extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return '{{%supplier_pool}}';
- }
- public function behaviors()
- {
- return [
- [
- 'class' => TimestampBehavior::class,
- 'attributes' => [
- ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
- ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
- ]
- ]
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['is_send', 'send_time', 'start_time', 'config_id', 'end_time', 'store_id', 'is_scan'], 'integer'],
- [['money', 'rl_rate', 'mx_rate'], 'number'],
- [['created_at', 'updated_at'], 'safe']
- ];
- }
- /**
- * 增加合伙人分红池
- * @param $order
- * @param $money
- * @param string $desc
- */
- public static function poolPush($supplier_id, $order_id, $order_type, $money, $desc = '', $rl_rate = 0, $mx_rate = 0, $model = 0, $add_money = 0)//: void
- {
- try {
- switch ($order_type) {
- case 1:
- $order = Order::find()->where(['id'=>$order_id])->asArray()->one();
- break;
- case 2:
- $order = ScanOrder::find()->where(['id'=>$order_id])->asArray()->one();
- break;
- case 3:
- $go_res = GolangForm::inventoryOrderExist(['id'=>$order_id]);
- if (!$go_res || !isset($go_res['code']) || $go_res['code'] != 0) throw new Exception("订单不存在1");
- if (!$go_res['data'] || !$go_res['data']['exist'] || !$go_res['data']['info']) throw new Exception("订单不存在2");
- $order = $go_res['data']['info'];
- break;
- default:
- throw new Exception("订单类型错误");
- }
- if (empty($order)) throw new Exception("订单不存在");
- $query = self::find()->where(['and',
- [
- '<', 'start_time', time()
- ],
- [
- '>=', 'end_time', time()
- ],
- [
- '=', 'is_send', 0
- ]
- ]);
- $supplier = Supplier::findOne($supplier_id);
- if (!$supplier || $supplier->pool_rate <= 0) throw new Exception("没有设置合伙人分红比例".$supplier_id);
- $money *= ($supplier->pool_rate/100);
- $setting = Option::getDecode(OptionSetting::SUPPLIER_POOL_SETTING, -1, OptionSetting::SUPPLIER_POOL_GROUP_NAME);
- if (!isset($setting['pools'])) throw new Exception("没有设置合伙人奖金池");
- foreach ($setting['pools'] as $pool) {
- $item = self::checkpush($pool);
- if (!$item) throw new Exception("设置错误".Json::encode($pool));
- $p_query = clone $query;
- $pool = $p_query->andWhere(['config_id' => $item['config_id']])->orderBy(['id' => SORT_DESC])->one();
- $div_money = ($money * $item['rate'])/100;
- $div_money = truncateDecimal($div_money);
- // 如果没有奖金池,重新创建奖金池
- if (!$pool) {
- $pool = new self();
- [$start_time, $end_time] = self::getTime($item['time_value'], $item['time_type'], $item['day']);
- $pool->start_time = $start_time;
- $pool->end_time = $end_time;
- $pool->is_send = 0;
- $pool->send_time = 0;
- $pool->is_scan = 0;
- $pool->store_id = 0;
- $pool->mx_rate = $mx_rate;
- $pool->rl_rate = $rl_rate;
- $pool->config_id = $item['config_id'];
- }
- $before_money = $pool->money ? $pool->money : 0;
- $pool->money += $div_money;
- if (!$pool->save()) {
- throw new Exception("---------------- 合伙人奖金池更新失败 -------------------" . $pool->getErrorSummary(false)[0]);
- }
- $pool_log = new SupplierPoolDetail();
- $pool_log->order_id = $order_id;
- $pool_log->config_id = $item['config_id'];
- $pool_log->order_no = $order['order_no'];
- $pool_log->pay_price = $order['total_price'];
- $pool_log->store_id = 0;
- $pool_log->before_money = $before_money;
- $pool_log->after_money = $pool->money;
- $pool_log->total_pv = $add_money;
- $pool_log->rl_rate = $rl_rate;
- $pool_log->mx_rate = $mx_rate;
- $pool_log->money = $div_money;
- $pool_log->total_price = $order['total_price'];
- $pool_log->is_scan = 0;
- $pool_log->pool_id = $pool->id;
- $pool_log->model_type = $model;
- $pool_log->desc = $desc ?: '订单号:' . $order['order_no'] . '追加';
- if (!$pool_log->save()) {
- throw new Exception("---------------- 奖金池记录保存失败 -------------------" . $pool_log->getErrorSummary(false)[0]);
- }
- }
- return [
- 'code' => 0,
- 'msg' => "发放成功",
- 'data'=>$setting
- ];
- } catch (Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- private static function checkpush($item)
- {
- if (!isset($item['config_id']) || !$item['config_id']) return false;
- if (!isset($item['rate']) || $item['rate'] <= 0) return false;
- if (!isset($item['time_value']) || $item['time_value'] <= 0) return false;
- if (!isset($item['time_type']) || !$item['time_type']) return false;
- if ($item['time_type'] == 'month') {
- $item['day'] = $item['day'] ?? 1;
- }
- return $item;
- }
- private static function getTime($time_value, $time_type, $day = 1)
- {
- // 计算 $start_time
- switch ($time_type) {
- case 'minute':
- $start_time = strtotime(date('Y-m-d H:i') . ':00');
- break;
- case 'hour':
- $start_time = strtotime(date('Y-m-d H') . ':00:00');
- break;
- case 'week':
- $start_time = strtotime(date('Y-m-d'), strtotime('monday this week'));
- break;
- case 'month':
- $day = $day < 10 ? '0' . $day : $day;
- $start_time = strtotime(date('Y-m') . '-' . $day);
- break;
- default:
- $start_time = strtotime(date('Y-m-d'));
- break;
- }
- // 计算 $end_time
- $end_time = strtotime('+' . $time_value . ' ' . $time_type, $start_time) - 1;
- return [$start_time, $end_time];
- }
- public static function poolSend($id)
- {
- $t = \Yii::$app->db->beginTransaction();
- try {
- $pool = self::findOne($id);
- if (!$pool) throw new Exception("分红池不存在");
- if (bccomp($pool->money, 0, 2) <= 0) throw new Exception("分红池金额为空");
- if ($pool->is_send) throw new Exception('分红池已发送');
- $config = CloudInventoryLevel::findOne($pool->config_id);
- if (!$config) throw new Exception("配置不存在");
- self::poolSendReward($pool, $config);
- $pool->is_send = 1;
- $pool->send_time = time();
- if (!$pool->save()) {
- throw new Exception("分红池状态保存失败" . json_encode($pool->getErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => "发放成功"
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- //debug_log([__METHOD__, __LINE__, "合伙人分红发放 异常" . $e->getMessage()], "app_debug_partner.log");
- return [
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ];
- }
- }
- private static function poolSendReward($pool, $config)
- {
- $end_time = $pool->end_time;
- $supplier = CloudInventoryLevel::findOne($pool->config_id);
- if (empty($supplier)) throw new Exception('云库存等级不存在');
- $month = date('m',strtotime($end_time));
- $start_time = strtotime(date('Y-') .$month. '-' . 1);
- ActionLog::addLog(1,'monthAndStartTime',$month.'--'.$start_time);
- $list = get_performance_bonus_users_amount_award($start_time, $end_time, $supplier->level);
- $totalMoney = array_sum(array_column($list, 'total_amount'));
- if (!$totalMoney) throw new \Exception("权重为". $totalMoney);
- $dividend_amount = 0;
- $log_desc = "(" . $config->name . ") 发放红包";
- $money = bcdiv($pool->money, $totalMoney, 8);
- foreach ($list as $u) {
- $div_money = bcmul($money, $u['total_amount'], 8);
- if (bccomp($div_money, 0, 8) > 0) {
- SaasUser::addPurchaseLog($u['saas_user_id'], $pool->store_id, $div_money, $log_desc, 1, AccountLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY);
- $index = SupplierPoolAwardRecord::createRecord($pool->config_id, $pool->store_id, $u['saas_user_id'], $pool->id, 0, $u['total_amount'], $log_desc, $div_money);
- $dividend_amount = bcadd($dividend_amount, $div_money, 4);
- } else {
- //debug_log([__METHOD__, __LINE__, "合伙人分红发放 等级:【{$u['level_id']}】,发放金额:{$div_money} 异常"], "app_debug_partner.log");
- }
- }
- /*$left_money = bcsub($pool->money, $dividend_amount, 2);
- if ($left_money > 0) {
- $setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $pool->store_id, OptionSetting::SHARE_GROUP_NAME);
- SaasUser::addPurchaseLog($setting['string_code_store_push_id'], $pool->store_id, $left_money, '指定发放:' . $config->name, 1, AccountLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY);
- $index = PluginPoolAwardRecord::createRecord($pool->config_id, $pool->store_id, $setting['string_code_store_push_id'], $pool->id, 0, $u['total_amount'], '指定发放', $left_money);
- }*/
- }
- }
|