SupplierPool.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.com
  5. * Copyright (c) 2023 爆块云 All rights reserved.
  6. */
  7. namespace app\models;
  8. use app\constants\OptionSetting;
  9. use app\modules\admin\models\GolangForm;
  10. use app\plugins\scanCodePay\models\Order as ScanOrder;
  11. use app\models\SupplierPoolDetail;
  12. use Exception;
  13. use yii\db\ActiveRecord;
  14. use yii\behaviors\TimestampBehavior;
  15. use Yii;
  16. use yii\helpers\Json;
  17. /**
  18. * This is the model class for table "{{%UnitFounder_pool}}".
  19. *
  20. * @property integer $id
  21. * @property integer $config_id
  22. * @property integer $store_id
  23. * @property integer $start_time
  24. * @property integer $end_time
  25. * @property float $money
  26. * @property float $rl_rate
  27. * @property float $mx_rate
  28. * @property integer $is_send
  29. * @property integer $is_scan
  30. * @property integer $send_time
  31. * @property integer $created_at
  32. * @property integer $updated_at
  33. */
  34. class SupplierPool extends \yii\db\ActiveRecord
  35. {
  36. /**
  37. * @inheritdoc
  38. */
  39. public static function tableName()
  40. {
  41. return '{{%supplier_pool}}';
  42. }
  43. public function behaviors()
  44. {
  45. return [
  46. [
  47. 'class' => TimestampBehavior::class,
  48. 'attributes' => [
  49. ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
  50. ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at']
  51. ]
  52. ]
  53. ];
  54. }
  55. /**
  56. * @inheritdoc
  57. */
  58. public function rules()
  59. {
  60. return [
  61. [['is_send', 'send_time', 'start_time', 'config_id', 'end_time', 'store_id', 'is_scan'], 'integer'],
  62. [['money', 'rl_rate', 'mx_rate'], 'number'],
  63. [['created_at', 'updated_at'], 'safe']
  64. ];
  65. }
  66. /**
  67. * 增加合伙人分红池
  68. * @param $order
  69. * @param $money
  70. * @param string $desc
  71. */
  72. public static function poolPush($supplier_id, $order_id, $order_type, $money, $desc = '', $rl_rate = 0, $mx_rate = 0, $model = 0, $add_money = 0)//: void
  73. {
  74. try {
  75. switch ($order_type) {
  76. case 1:
  77. $order = Order::find()->where(['id'=>$order_id])->asArray()->one();
  78. break;
  79. case 2:
  80. $order = ScanOrder::find()->where(['id'=>$order_id])->asArray()->one();
  81. break;
  82. case 3:
  83. $go_res = GolangForm::inventoryOrderExist(['id'=>$order_id]);
  84. if (!$go_res || !isset($go_res['code']) || $go_res['code'] != 0) throw new Exception("订单不存在1");
  85. if (!$go_res['data'] || !$go_res['data']['exist'] || !$go_res['data']['info']) throw new Exception("订单不存在2");
  86. $order = $go_res['data']['info'];
  87. break;
  88. default:
  89. throw new Exception("订单类型错误");
  90. }
  91. if (empty($order)) throw new Exception("订单不存在");
  92. $query = self::find()->where(['and',
  93. [
  94. '<', 'start_time', time()
  95. ],
  96. [
  97. '>=', 'end_time', time()
  98. ],
  99. [
  100. '=', 'is_send', 0
  101. ]
  102. ]);
  103. $supplier = Supplier::findOne($supplier_id);
  104. if (!$supplier || $supplier->pool_rate <= 0) throw new Exception("没有设置合伙人分红比例".$supplier_id);
  105. $money *= ($supplier->pool_rate/100);
  106. $setting = Option::getDecode(OptionSetting::SUPPLIER_POOL_SETTING, -1, OptionSetting::SUPPLIER_POOL_GROUP_NAME);
  107. if (!isset($setting['pools'])) throw new Exception("没有设置合伙人奖金池");
  108. foreach ($setting['pools'] as $pool) {
  109. $item = self::checkpush($pool);
  110. if (!$item) throw new Exception("设置错误".Json::encode($pool));
  111. $p_query = clone $query;
  112. $pool = $p_query->andWhere(['config_id' => $item['config_id']])->orderBy(['id' => SORT_DESC])->one();
  113. $div_money = ($money * $item['rate'])/100;
  114. $div_money = truncateDecimal($div_money);
  115. // 如果没有奖金池,重新创建奖金池
  116. if (!$pool) {
  117. $pool = new self();
  118. [$start_time, $end_time] = self::getTime($item['time_value'], $item['time_type'], $item['day']);
  119. $pool->start_time = $start_time;
  120. $pool->end_time = $end_time;
  121. $pool->is_send = 0;
  122. $pool->send_time = 0;
  123. $pool->is_scan = 0;
  124. $pool->store_id = 0;
  125. $pool->mx_rate = $mx_rate;
  126. $pool->rl_rate = $rl_rate;
  127. $pool->config_id = $item['config_id'];
  128. }
  129. $before_money = $pool->money ? $pool->money : 0;
  130. $pool->money += $div_money;
  131. if (!$pool->save()) {
  132. throw new Exception("---------------- 合伙人奖金池更新失败 -------------------" . $pool->getErrorSummary(false)[0]);
  133. }
  134. $pool_log = new SupplierPoolDetail();
  135. $pool_log->order_id = $order_id;
  136. $pool_log->config_id = $item['config_id'];
  137. $pool_log->order_no = $order['order_no'];
  138. $pool_log->pay_price = $order['total_price'];
  139. $pool_log->store_id = 0;
  140. $pool_log->before_money = $before_money;
  141. $pool_log->after_money = $pool->money;
  142. $pool_log->total_pv = $add_money;
  143. $pool_log->rl_rate = $rl_rate;
  144. $pool_log->mx_rate = $mx_rate;
  145. $pool_log->money = $div_money;
  146. $pool_log->total_price = $order['total_price'];
  147. $pool_log->is_scan = 0;
  148. $pool_log->pool_id = $pool->id;
  149. $pool_log->model_type = $model;
  150. $pool_log->desc = $desc ?: '订单号:' . $order['order_no'] . '追加';
  151. if (!$pool_log->save()) {
  152. throw new Exception("---------------- 奖金池记录保存失败 -------------------" . $pool_log->getErrorSummary(false)[0]);
  153. }
  154. }
  155. return [
  156. 'code' => 0,
  157. 'msg' => "发放成功",
  158. 'data'=>$setting
  159. ];
  160. } catch (Exception $e) {
  161. return [
  162. 'code' => 1,
  163. 'msg' => $e->getMessage()
  164. ];
  165. }
  166. }
  167. private static function checkpush($item)
  168. {
  169. if (!isset($item['config_id']) || !$item['config_id']) return false;
  170. if (!isset($item['rate']) || $item['rate'] <= 0) return false;
  171. if (!isset($item['time_value']) || $item['time_value'] <= 0) return false;
  172. if (!isset($item['time_type']) || !$item['time_type']) return false;
  173. if ($item['time_type'] == 'month') {
  174. $item['day'] = $item['day'] ?? 1;
  175. }
  176. return $item;
  177. }
  178. private static function getTime($time_value, $time_type, $day = 1)
  179. {
  180. // 计算 $start_time
  181. switch ($time_type) {
  182. case 'minute':
  183. $start_time = strtotime(date('Y-m-d H:i') . ':00');
  184. break;
  185. case 'hour':
  186. $start_time = strtotime(date('Y-m-d H') . ':00:00');
  187. break;
  188. case 'week':
  189. $start_time = strtotime(date('Y-m-d'), strtotime('monday this week'));
  190. break;
  191. case 'month':
  192. $day = $day < 10 ? '0' . $day : $day;
  193. $start_time = strtotime(date('Y-m') . '-' . $day);
  194. break;
  195. default:
  196. $start_time = strtotime(date('Y-m-d'));
  197. break;
  198. }
  199. // 计算 $end_time
  200. $end_time = strtotime('+' . $time_value . ' ' . $time_type, $start_time) - 1;
  201. return [$start_time, $end_time];
  202. }
  203. public static function poolSend($id)
  204. {
  205. $t = \Yii::$app->db->beginTransaction();
  206. try {
  207. $pool = self::findOne($id);
  208. if (!$pool) throw new Exception("分红池不存在");
  209. if (bccomp($pool->money, 0, 2) <= 0) throw new Exception("分红池金额为空");
  210. if ($pool->is_send) throw new Exception('分红池已发送');
  211. $config = CloudInventoryLevel::findOne($pool->config_id);
  212. if (!$config) throw new Exception("配置不存在");
  213. self::poolSendReward($pool, $config);
  214. $pool->is_send = 1;
  215. $pool->send_time = time();
  216. if (!$pool->save()) {
  217. throw new Exception("分红池状态保存失败" . json_encode($pool->getErrors()));
  218. }
  219. $t->commit();
  220. return [
  221. 'code' => 0,
  222. 'msg' => "发放成功"
  223. ];
  224. } catch (\Exception $e) {
  225. $t->rollBack();
  226. //debug_log([__METHOD__, __LINE__, "合伙人分红发放 异常" . $e->getMessage()], "app_debug_partner.log");
  227. return [
  228. 'code' => 1,
  229. 'msg' => $e->getMessage(),
  230. ];
  231. }
  232. }
  233. private static function poolSendReward($pool, $config)
  234. {
  235. $end_time = $pool->end_time;
  236. $supplier = CloudInventoryLevel::findOne($pool->config_id);
  237. if (empty($supplier)) throw new Exception('云库存等级不存在');
  238. $month = date('m',strtotime($end_time));
  239. $start_time = strtotime(date('Y-') .$month. '-' . 1);
  240. ActionLog::addLog(1,'monthAndStartTime',$month.'--'.$start_time);
  241. $list = get_performance_bonus_users_amount_award($start_time, $end_time, $supplier->level);
  242. $totalMoney = array_sum(array_column($list, 'total_amount'));
  243. if (!$totalMoney) throw new \Exception("权重为". $totalMoney);
  244. $dividend_amount = 0;
  245. $log_desc = "(" . $config->name . ") 发放红包";
  246. $money = bcdiv($pool->money, $totalMoney, 8);
  247. foreach ($list as $u) {
  248. $div_money = bcmul($money, $u['total_amount'], 8);
  249. if (bccomp($div_money, 0, 8) > 0) {
  250. SaasUser::addPurchaseLog($u['saas_user_id'], $pool->store_id, $div_money, $log_desc, 1, AccountLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY);
  251. $index = SupplierPoolAwardRecord::createRecord($pool->config_id, $pool->store_id, $u['saas_user_id'], $pool->id, 0, $u['total_amount'], $log_desc, $div_money);
  252. $dividend_amount = bcadd($dividend_amount, $div_money, 4);
  253. } else {
  254. //debug_log([__METHOD__, __LINE__, "合伙人分红发放 等级:【{$u['level_id']}】,发放金额:{$div_money} 异常"], "app_debug_partner.log");
  255. }
  256. }
  257. /*$left_money = bcsub($pool->money, $dividend_amount, 2);
  258. if ($left_money > 0) {
  259. $setting = Option::getDecode(OptionSetting::PLUGIN_CONFIG_POOL_SETTING, $pool->store_id, OptionSetting::SHARE_GROUP_NAME);
  260. SaasUser::addPurchaseLog($setting['string_code_store_push_id'], $pool->store_id, $left_money, '指定发放:' . $config->name, 1, AccountLog::TYPE_PURCHASE_CLOUD_INVENTORY_PURCHASE_MONEY);
  261. $index = PluginPoolAwardRecord::createRecord($pool->config_id, $pool->store_id, $setting['string_code_store_push_id'], $pool->id, 0, $u['total_amount'], '指定发放', $left_money);
  262. }*/
  263. }
  264. }