ShareMoneyForm.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\common\CommonGoods;
  9. use app\models\Goods;
  10. use app\models\GoodsShare;
  11. use app\models\MchSetting;
  12. use app\models\Option;
  13. use app\models\Order;
  14. use app\models\OrderDetail;
  15. use app\models\OrderGoodsCancel;
  16. use app\models\OrderShare;
  17. use app\models\Share;
  18. use app\models\ShareHolder;
  19. use app\models\ShareHolderLevel;
  20. use app\models\ShareLevel;
  21. use app\models\User;
  22. use app\models\UserBindLog;
  23. use app\models\UserShareLog;
  24. use yii\base\BaseObject;
  25. use yii\base\Model;
  26. use yii\helpers\Json;
  27. use app\models\ActivityRebateOrderN;
  28. use app\models\OrderPospalDetail;
  29. class ShareMoneyForm extends Model
  30. {
  31. /**
  32. * @var $order Order
  33. */
  34. public $order;
  35. public $order_type;
  36. public $share_send_type = 1;
  37. public $order_share_rate = 0;
  38. public $integral = 0;
  39. public $store_id;
  40. public $yinbao = 0;
  41. public function setData()
  42. {
  43. $store_id = $this->store_id ? $this->store_id : get_store_id();
  44. \Yii::warning('=== 分销计算:订单id:' . $this->order->id . ',计算分销开始 ===');
  45. $setting = Option::get('share_money_setting', $store_id);
  46. $setting = $setting ? Json::decode($setting['value']) : [];
  47. if (!$setting) {
  48. \Yii::warning('未开启分销设置1');
  49. return false;
  50. }
  51. // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) {
  52. // \Yii::warning('未开启分销设置2');
  53. // return false;
  54. // }
  55. // 获取到当前到订单佣金发放类型 1: 佣金 2:积分, 3:余额
  56. $this->share_send_type = $setting['giveType'] ?? 1;
  57. // 获取积分和佣金的兑换比例
  58. $integral = Option::get('integral', $this->order->store_id, 'store')['value'];
  59. if (!$integral && $this->share_send_type == 2) {
  60. \Yii::warning('发放积分佣金类型比例未设置');
  61. return false;
  62. }
  63. $this->integral = $integral;
  64. // 订单分销 2
  65. $this->order_share_rate = $setting['scale'] ?: 0;
  66. $this->setOrderProfit();
  67. $this->setLimitProfit($setting);
  68. $order = $this->order;
  69. /* @var $orderShare Order */
  70. $orderShare = $this->order;
  71. $orderShare->share_send_type = $this->share_send_type;
  72. $orderDetail = $this->getOrderDetail();
  73. if (!$orderDetail) {
  74. \Yii::warning('订单详情不存在');
  75. return false;
  76. }
  77. $user = User::findOne($this->order->user_id);
  78. if (!$user) {
  79. \Yii::warning('订单用户不存在');
  80. return false;
  81. }
  82. $cParent1 = $user->parent_id;
  83. $orderShare->parent_id = $cParent1;
  84. if ($user->parent_id) {
  85. $parent = User::findOne($user->parent_id);//上级
  86. $cParent2 = $parent->parent_id;
  87. $orderShare->parent_id_1 = $cParent2;
  88. if ($parent->parent_id) {
  89. $parent_1 = User::findOne($parent->parent_id);//上上级
  90. $cParent3 = $parent_1->parent_id;
  91. $orderShare->parent_id_2 = $cParent3;
  92. } else {
  93. $orderShare->parent_id_2 = -1;
  94. }
  95. } else {
  96. $orderShare->parent_id_1 = -1;
  97. $orderShare->parent_id_2 = -1;
  98. }
  99. // 旧关系记录
  100. if ($user->old_parent_id > 0) {
  101. $old_parent = User::findOne($user->old_parent_id);//上级
  102. $orderShare->old_parent_id = $old_parent->id;
  103. if ($old_parent->old_parent_id > 0) {
  104. $old_parent_1 = User::findOne($old_parent->old_parent_id);//上上级
  105. $orderShare->old_parent_id_1 = $old_parent_1->id;
  106. if ($old_parent_1->old_parent_id > 0) {
  107. $old_parent_2 = User::findOne($old_parent_1->old_parent_id);//上上级
  108. $orderShare->old_parent_id_2 = $old_parent_2->id;
  109. }
  110. }
  111. }
  112. // $share_commission_money_first = 0;//一级分销总佣金
  113. // $share_commission_money_second = 0;//二级分销总佣金
  114. // $share_commission_money_third = 0;//三级分销总佣金
  115. // foreach ($orderDetail as $item) {
  116. // // TODO: 价格
  117. // $item_price = doubleval($item['price']);
  118. // // $item_price = doubleval($orderShare->pay_price);
  119. //
  120. // if ($item['individual_share'] == 1) {
  121. // $rate_first = doubleval($item['share_commission_first']);
  122. // $rate_second = doubleval($item['share_commission_second']);
  123. // $rate_third = doubleval($item['share_commission_third']);
  124. // $shareType = $item['share_type'];
  125. // } else {
  126. // $setting_shara = Option::get('share_money_setting', $order->store_id, 'share');
  127. // $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : [];
  128. // $rate_first = doubleval($setting_shara['level_one']['value']);
  129. // $rate_second = doubleval($setting_shara['level_two']['value']);
  130. // $rate_third = doubleval($setting_shara['level_three']['value']);
  131. // $shareType = isset($setting_shara['price_type']) ? $setting_shara['price_type'] : 0;
  132. // }
  133. // if ($shareType == 1) { // 金钱
  134. // if ($this->share_send_type == 2) {
  135. // // 兑换比例
  136. // $share_commission_money_first += round($rate_first * $integral) * $item['num'];
  137. // $share_commission_money_second += round($rate_second * $integral) * $item['num'];
  138. // $share_commission_money_third += round($rate_third * $integral) * $item['num'];
  139. // } else {
  140. // $share_commission_money_first += $rate_first * $item['num'];
  141. // $share_commission_money_second += $rate_second * $item['num'];
  142. // $share_commission_money_third += $rate_third * $item['num'];
  143. // }
  144. // } else { // 比例
  145. // if ($this->share_send_type == 2) {
  146. // $share_commission_money_first += round($item_price * ($rate_first / 100) * $integral);
  147. // $share_commission_money_second += round($item_price * ($rate_second / 100) * $integral);
  148. // $share_commission_money_third += round($item_price * ($rate_third / 100 ) * $integral);
  149. // } else {
  150. // $share_commission_money_first += $item_price * $rate_first / 100;
  151. // $share_commission_money_second += $item_price * $rate_second / 100;
  152. // $share_commission_money_third += $item_price * $rate_third / 100;
  153. // }
  154. // }
  155. // }
  156. $share_setting = Option::get('share_basic_setting', $store_id);
  157. $share_setting = $share_setting ? Json::decode($share_setting['value']) : [];
  158. // 如果开启自购返利 一级是自己
  159. if ($share_setting['is_rebate']['value'] == 1 && $user->is_distributor == 1) {
  160. if ($share_setting['level']['value'] >= 1) {
  161. $orderShare->rebate = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->user_id, 1);
  162. }
  163. if ($share_setting['level']['value'] >= 2) {
  164. $orderShare->first_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id, 2);
  165. }
  166. if (intval($share_setting['level']['value']) === 3) {
  167. $orderShare->second_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_1, 3);
  168. }
  169. $orderShare->third_price = 0;
  170. } else {
  171. $orderShare->rebate = 0;
  172. if ($share_setting['level']['value'] >= 1) {
  173. $orderShare->first_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id, 1);
  174. }
  175. if ($share_setting['level']['value'] >= 2) {
  176. $orderShare->second_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_1, 2);
  177. }
  178. if (intval($share_setting['level']['value']) === 3) {
  179. $orderShare->third_price = $this->getShareCommissionMoney($store_id, $orderDetail, $integral, $orderShare->old_parent_id_2, 3);
  180. }
  181. }
  182. // 计算股东分销
  183. // 一级分销
  184. // if ($orderShare->parent_id > 0) {
  185. // $share_holder_1 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id, 'is_delete' => 0, 'status' => 1]);
  186. // if ($share_holder_1) {
  187. // $level_id_1 = $share_holder_1->level_id;
  188. // $one_rate = ShareHolderLevel::findOne($level_id_1)->share_rate_one;
  189. // if ($one_rate > 0) {
  190. // $orderShare->holder_first_price = $orderShare->profit * doubleval($one_rate) / 100;
  191. // }
  192. // }
  193. // }
  194. // 二级分销
  195. // if ($orderShare->parent_id_1 > 0) {
  196. // $share_holder_2 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id_1, 'is_delete' => 0, 'status' => 1]);
  197. // if ($share_holder_2) {
  198. // $level_id_2 = $share_holder_2->level_id;
  199. // $two_rate = ShareHolderLevel::findOne($level_id_2)->share_rate_two;
  200. // if ($two_rate > 0) {
  201. // $orderShare->holder_second_price = $orderShare->profit * doubleval($two_rate) / 100;
  202. // }
  203. // }
  204. // }
  205. // 三级分销
  206. // if ($orderShare->parent_id_2 > 0) {
  207. // $share_holder_3 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->parent_id_2, 'is_delete' => 0, 'status' => 1]);
  208. // if ($share_holder_3) {
  209. // $level_id_3 = $share_holder_3->level_id;
  210. // $three_rate = ShareHolderLevel::findOne($level_id_3)->share_rate_three;
  211. // if ($two_rate > 0) {
  212. // $orderShare->holder_third_price = $orderShare->profit * doubleval($three_rate) / 100;
  213. // }
  214. // }
  215. // }
  216. // 计算股东分销(旧关系)
  217. // 一级分销
  218. if ($orderShare->old_parent_id > 0) {
  219. $share_holder_1 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id, 'is_delete' => 0, 'status' => 1]);
  220. if ($share_holder_1) {
  221. $level_id_1 = $share_holder_1->level_id;
  222. $one_rate = ShareHolderLevel::findOne($level_id_1)->share_rate_one;
  223. if ($one_rate > 0) {
  224. $orderShare->old_holder_first_price = $orderShare->profit * doubleval($one_rate) / 100;
  225. }
  226. }
  227. }
  228. // 二级分销
  229. if ($orderShare->old_parent_id_1 > 0) {
  230. $share_holder_2 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id_1, 'is_delete' => 0, 'status' => 1]);
  231. if ($share_holder_2) {
  232. $level_id_2 = $share_holder_2->level_id;
  233. $two_rate = ShareHolderLevel::findOne($level_id_2)->share_rate_two;
  234. if ($two_rate > 0) {
  235. $orderShare->old_holder_second_price = $orderShare->profit * doubleval($two_rate) / 100;
  236. }
  237. }
  238. }
  239. // 三级分销
  240. if ($orderShare->old_parent_id_2 > 0) {
  241. $share_holder_3 = ShareHolder::findOne(['store_id' => $orderShare->store_id, 'user_id' => $orderShare->old_parent_id_2, 'is_delete' => 0, 'status' => 1]);
  242. if ($share_holder_3) {
  243. $level_id_3 = $share_holder_3->level_id;
  244. $three_rate = ShareHolderLevel::findOne($level_id_3)->share_rate_three;
  245. if ($three_rate > 0) {
  246. $orderShare->old_holder_third_price = $orderShare->profit * doubleval($three_rate) / 100;
  247. }
  248. }
  249. }
  250. $res = $orderShare->save();
  251. if (!$res) {
  252. \Yii::warning('=== 分销计算:订单id:' . $this->order->id . '分销订单生成失败 ===');
  253. }
  254. \Yii::warning('=== 分销计算:订单id:' . $this->order->id . '分销订单生成成功 ===');
  255. return $res;
  256. }
  257. public function getShareCommissionMoney($store_id, $orderDetail, $integral, $user_id, $type = 1) {
  258. $share = Share::findOne(['user_id' => $user_id, 'status' => Share::SHARE_AUDIT_PASS, 'is_delete' => Share::SHARE_NOT_DELETE]);
  259. $share_commission_money = 0;
  260. if ($share) {
  261. $share_level_id = $share->level;
  262. $share_level = ShareLevel::findOne(['level' => $share_level_id, 'store_id' => $store_id, 'is_delete' => ShareLevel::SHARE_NOT_DELETE,
  263. 'status' => ShareLevel::STATUS_ON]);
  264. foreach ($orderDetail as $item) {
  265. if(ActivityRebateOrderN::passBaseRebate($store_id, $item['goods_id'], $this->order->parent_id)){
  266. continue;
  267. }
  268. // TODO: 价格
  269. $item_price = doubleval($item['price']);
  270. // $item_price = doubleval($orderShare->pay_price);
  271. $rate_first = 0;
  272. if ($item['individual_share'] == 1) {
  273. if ($type === 1) {
  274. $rate_first = $item['share_commission_new_first']['share_commission_level_' . $share_level_id];
  275. } elseif ($type === 2) {
  276. $rate_first = $item['share_commission_new_second']['share_commission_level_' . $share_level_id];
  277. } elseif ($type === 3) {
  278. $rate_first = $item['share_commission_new_third']['share_commission_level_' . $share_level_id];
  279. }
  280. $shareType = $item['share_type'];
  281. } else {
  282. $setting_shara = Option::get('share_money_setting', $store_id, 'share');
  283. $setting_shara = $setting_shara ? Json::decode($setting_shara['value']) : [];
  284. if ($type === 1) {
  285. $rate_first = $share_level->first_profit ?? ($setting_shara['level_one']['value'] ?? 0);
  286. } elseif ($type === 2) {
  287. $rate_first = $share_level->second_profit ?? ($setting_shara['level_two']['value'] ?? 0);
  288. } elseif ($type === 3) {
  289. $rate_first = $share_level->third_profit ?? ($setting_shara['level_three']['value'] ?? 0);
  290. }
  291. if ($share_level) {
  292. $shareType = intval($share_level->profit_type);
  293. } else {
  294. $shareType = 0;
  295. }
  296. }
  297. if ($shareType == 1) { // 金钱
  298. if ($this->share_send_type == 2) {
  299. // 兑换比例
  300. $share_commission_money += round($rate_first * $integral) * $item['num'];
  301. } else {
  302. $share_commission_money += $rate_first * $item['num'];
  303. }
  304. } else { // 比例
  305. if ($this->share_send_type == 2) {
  306. $share_commission_money += round($item_price * ($rate_first / 100) * $integral);
  307. } else {
  308. $share_commission_money += $item_price * $rate_first / 100;
  309. }
  310. }
  311. }
  312. return sprintf("%.2f", $share_commission_money < 0.01 ? 0 : $share_commission_money);
  313. }
  314. return 0;
  315. }
  316. public function setScanCodePayData()
  317. {
  318. \Yii::error('===========================================');
  319. $sotre_id = $this->store_id ? $this->store_id : get_store_id();
  320. $setting = Option::get('share_money_setting', $sotre_id);
  321. $setting = $setting ? Json::decode($setting['value']) : [];
  322. if (!$setting) {
  323. \Yii::warning('未开启分销设置1');
  324. return false;
  325. }
  326. // if (!is_array($setting['commission_type']) || empty($setting['commission_type'])) {
  327. // \Yii::warning('未开启分销设置2');
  328. // return false;
  329. // }
  330. // 获取到当前到订单佣金发放类型 1: 佣金 2:积分, 3:余额
  331. if (!empty($setting['giveType'])) {
  332. $this->share_send_type = $setting['giveType'];
  333. }
  334. // 获取积分和佣金的兑换比例
  335. $integral = Option::get('integral', $this->order->store_id, 'store')['value'];
  336. if (!$integral && $this->share_send_type == 2) {
  337. \Yii::warning('发放积分佣金类型比例未设置');
  338. return false;
  339. }
  340. // $commission_type = $setting['commission_type'];
  341. // 订单分销 2
  342. // if (in_array(2, $commission_type) && $setting['scale'] > 0) {
  343. $this->order_share_rate = $setting['scale'];
  344. $this->setOrderProfit();
  345. // } else {
  346. // \Yii::warning('未开启订单佣金分销');
  347. // }
  348. // if (!in_array(1, $commission_type)) {
  349. // \Yii::warning('未开启会员佣金分销');
  350. // return false;
  351. // }
  352. $user = User::findOne($this->order->user_id);
  353. if (!$user) {
  354. \Yii::warning('订单用户不存在');
  355. return false;
  356. }
  357. /* @var $orderShare \app\plugins\scanCodePay\models\Order */
  358. $orderShare = $this->order;
  359. $cParent1 = $user->parent_id;
  360. $orderShare->parent_id = $cParent1;
  361. if ($user->parent_id) {
  362. $parent = User::findOne($user->parent_id);//上级
  363. $cParent2 = $parent->parent_id;
  364. $orderShare->parent_id_1 = $cParent2;
  365. if ($parent->parent_id) {
  366. $parent_1 = User::findOne($parent->parent_id);//上上级
  367. $cParent3 = $parent_1->parent_id;
  368. $orderShare->parent_id_2 = $cParent3;
  369. } else {
  370. $orderShare->parent_id_2 = -1;
  371. }
  372. } else {
  373. $orderShare->parent_id_1 = -1;
  374. $orderShare->parent_id_2 = -1;
  375. }
  376. $res = $orderShare->save();
  377. if (!$res) {
  378. \Yii::error('分销订单生成失败');
  379. }
  380. \Yii::error('分销订单生成成功');
  381. return $res;
  382. }
  383. /**
  384. * 普通订单
  385. * @return array
  386. */
  387. private function getOrderDetail()
  388. {
  389. /* @var $order Order */
  390. $order = $this->order;
  391. /* @var $list OrderDetail */
  392. if($this->yinbao){
  393. $list = OrderPospalDetail::find()->where(['is_delete' => 0, 'order_id' => $order->id])->all();
  394. }else{
  395. $list = OrderDetail::find()->where(['is_delete' => 0, 'order_id' => $order->id])->all();
  396. }
  397. $newList = [];
  398. foreach ($list as $value) {
  399. $goods = $value->goods;
  400. // 入住商商品未开启独立分销的时候 不能走平台的分销 因为分销佣金是入住商支付的
  401. if($goods->mch_id > 0 && $goods->individual_share == 0){
  402. continue;
  403. }
  404. $buyAttrList = Json::decode($value['attr']);
  405. $orderGoodsCancel = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'order_detail_id' => $value->id, 'status' => [
  406. OrderGoodsCancel::STATUS_PASS,
  407. OrderGoodsCancel::STATUS_PAY,
  408. OrderGoodsCancel::STATUS_PAY_FAIL
  409. ]])->groupBy('order_detail_id')->select('SUM(num) as num, SUM(refund_price) as refund_price')->asArray()->one();
  410. $value['total_price'] = bcsub($value['total_price'], $orderGoodsCancel['refund_price'], 2);
  411. $value['num'] = bcsub($value['num'], $orderGoodsCancel['num']);
  412. if ($value['num'] <= 0) {
  413. continue;
  414. }
  415. if (intval($goods['attr_setting_type']) === 1) {
  416. $attrIdArr2 = [];
  417. foreach ($buyAttrList as $attrListItem2) {
  418. $attrIdArr2[] = $attrListItem2['attr_id'];
  419. }
  420. $goodsData = [
  421. 'attr' => $goods['attr'],
  422. 'price' => $goods['price'],
  423. 'is_level' => $goods['is_level'],
  424. ];
  425. $res = CommonGoods::currentGoodsAttr($goodsData, $attrIdArr2, [], 1, $order->user_id, $order->store_id);
  426. $newItem = [
  427. 'individual_share' => $goods['individual_share'],
  428. 'share_commission_new_first' => $res['share_commission_new_first'],
  429. 'share_commission_new_second' => $res['share_commission_new_second'],
  430. 'share_commission_new_third' => $res['share_commission_new_third'],
  431. 'share_type' => $goods['share_type'],
  432. 'num' => $value['num'],
  433. 'price' => $value['total_price'],
  434. 'mch_id' => $goods['mch_id']
  435. ];
  436. } else {
  437. $newItem = [
  438. 'individual_share' => $goods['individual_share'],
  439. 'share_commission_new_first' => $goods['share_commission_new_first'] ? json_decode($goods['share_commission_new_first'], true) : [],
  440. 'share_commission_new_second' => $goods['share_commission_new_second'] ? json_decode($goods['share_commission_new_second'], true) : [],
  441. 'share_commission_new_third' => $goods['share_commission_new_third'] ? json_decode($goods['share_commission_new_third'], true) : [],
  442. 'share_type' => $goods['share_type'],
  443. 'num' => $value['num'],
  444. 'price' => $value['total_price'],
  445. 'mch_id' => $goods['mch_id']
  446. ];
  447. }
  448. array_push($newList, array_merge($newItem, [
  449. 'goods_id' => $value['goods_id'],
  450. ]));
  451. }
  452. return $newList;
  453. }
  454. /**
  455. * 订单佣金计算
  456. */
  457. private function setOrderProfit() {
  458. $price = $this->order->pay_price;
  459. $refund_price = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'status' => [
  460. OrderGoodsCancel::STATUS_PASS,
  461. OrderGoodsCancel::STATUS_PAY,
  462. OrderGoodsCancel::STATUS_PAY_FAIL
  463. ]])->sum('refund_price');
  464. $price = bcsub($price, $refund_price, 2);
  465. if ($price <= 0) {
  466. \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成-可退款金额为0');
  467. $this->order->share_order_profit = 0;
  468. if ($this->order->save()) {
  469. \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成');
  470. return true;
  471. }
  472. return false;
  473. }
  474. $profit = 0;
  475. // 积分
  476. if ($this->share_send_type == 2) {
  477. $profit = round($price * ($this->order_share_rate / 100) * $this->integral);
  478. } else {
  479. $profit = round($price * ($this->order_share_rate / 100), 2);
  480. }
  481. if ($profit < 0.01) {
  482. \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算为0');
  483. return false;
  484. }
  485. $this->order->share_order_profit = $profit;
  486. $this->order->share_send_type = $this->share_send_type;
  487. if ($this->order->save()) {
  488. \Yii::warning('订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算完成');
  489. return true;
  490. }
  491. \Yii::warning(['订单佣金分销计算:订单id:' . $this->order->id . '订单分销佣金计算失败', $this->order->errors]);
  492. return false;
  493. }
  494. /**
  495. * 临时绑定关系佣金计算
  496. */
  497. private function setLimitProfit($setting){
  498. $price = $this->order->pay_price;
  499. $refund_price = OrderGoodsCancel::find()->where(['order_id' => $this->order->id, 'status' => [
  500. OrderGoodsCancel::STATUS_PASS,
  501. OrderGoodsCancel::STATUS_PAY,
  502. OrderGoodsCancel::STATUS_PAY_FAIL
  503. ]])->sum('refund_price');
  504. $price = bcsub($price, $refund_price, 2);
  505. if ($price <= 0) {
  506. return false;
  507. }
  508. $profit = $price * ($setting['scale']/100);
  509. //临时绑定关系发放佣金
  510. $bind_log = UserBindLog::find()->where(['user_id' => $this->order->user_id])->select('id, bind_time, parent_id')->orderBy('id desc')->asArray()->one();
  511. if ($bind_log) {
  512. if ($bind_log['parent_id']) {
  513. $share = Share::findOne(['user_id' => $bind_log['parent_id'], 'status' => 1, 'is_delete' => 0]);
  514. if (!$share) {
  515. return false;
  516. }
  517. }
  518. if ($bind_log['bind_time'] + ($setting['expire_day'] * 24 * 60 * 60) >= time()) {
  519. $this->order->limit_price = $profit;
  520. $this->order->limit_id = $bind_log['id'];
  521. if ($this->order->save()) {
  522. return true;
  523. }
  524. }
  525. }
  526. return false;
  527. }
  528. }