CommonGoods.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  8. use app\models\Level;
  9. use app\models\Goods;
  10. use app\models\MchSetting;
  11. use app\models\SupplierSetting;
  12. use yii\helpers\Json;
  13. class CommonGoods
  14. {
  15. /**
  16. * 获取当前规格的相应信息
  17. * @param array $goods 商品信息
  18. * @param array $currentAttrIds 当前选择的规格数据 例:[42,43]
  19. * @param array $otherData 特殊数据处理 阶梯团、秒杀
  20. * @return array
  21. */
  22. public static function currentGoodsAttr(array $goods, array $currentAttrIds, array $otherData = [], $number = 1,$goods_id=0)
  23. {
  24. $batch_price_tips = '';
  25. $current_batch_price_tips = '';
  26. $is_true = 0;
  27. $full_minus_attrs = [];
  28. /**
  29. * 判断商品是否开启会员折扣,且用户是否为会员,且会员中的折扣存在
  30. */
  31. //获取用户信息
  32. // $user = get_user();
  33. // //获取会员折扣信息
  34. // $level = Level::find()->where(['store_id' => get_store_id(), 'status' => 1, 'is_delete' => 0, 'level' => $user->level])->andWhere(['>', 'discount', 0])->select('id')->one();
  35. // $open = false;
  36. // //判断商品是否开启会员折扣,且用户是否为会员,且会员中的折扣存在
  37. // if ($goods['is_level'] == 1 && $user->level && !empty($level)) {
  38. // $open = false;
  39. // }
  40. // //如果以上条件成立,则不使用满减折扣
  41. // if (isset($goods['full_minus']) && $open){
  42. // foreach ($goods['full_minus'] as $k => $v){
  43. // $full_minus_list = json_decode($goods['full_minus'][$k]['attr'], true);
  44. // if (!$full_minus_list || count($full_minus_list) < 1) {
  45. // continue;
  46. // }
  47. // foreach ($full_minus_list as $key => $value) {
  48. // $attrIds = [];
  49. // foreach ($full_minus_list[$key]['attr_list'] as $item) {
  50. // $attrIds[] = $item['attr_id'];
  51. // }
  52. // sort($attrIds);
  53. // sort($currentAttrIds);
  54. // if (($number >= $goods['full_minus'][$k]['full_minus_num']) && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']) {
  55. // $full_minus_attrs = $full_minus_list[$key];
  56. // $current_batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
  57. // }
  58. // if (($number <= $goods['full_minus'][$k]['full_minus_num']) && $is_true == 0 && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']){
  59. // $is_true = 1;
  60. // $batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
  61. // }elseif (($number > $goods['full_minus'][$k]['full_minus_num']) && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']){
  62. // $batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
  63. // }
  64. // }
  65. // }
  66. // }
  67. $attrs = Json::decode($goods['attr']);
  68. //$level = self::currentLevelDiscount();
  69. foreach ($attrs as $attr) {
  70. $attrIds = [];
  71. foreach ($attr['attr_list'] as $item) {
  72. $attrIds[] = $item['attr_id'];
  73. }
  74. sort($attrIds);
  75. sort($currentAttrIds);
  76. // 找出当前规格信息
  77. if (implode($attrIds) === implode($currentAttrIds)) {
  78. if ($full_minus_attrs && $full_minus_attrs['price']){
  79. $attr['price'] = $full_minus_attrs['price'];
  80. }
  81. // price 前端展示价格(该价格可以是 原价、会员价)
  82. $data['price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];
  83. $data['num'] = $attr['num'];
  84. $data['supplier_id'] = isset($goods['supplier_id']) ? $goods['supplier_id'] : 0;
  85. $data['supplier_goods_id'] = isset($goods['supplier_goods_id']) ? $goods['supplier_goods_id'] : 0;
  86. $data['supplier_price'] = isset($attr['supplier_price']) ? ($attr['supplier_price'] > 0 ? $attr['supplier_price'] : (isset($goods['supplier_price']) ? $goods['supplier_price'] : 0)) : 0;
  87. $data['goods_price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];//商品售价
  88. $data['attr_list'] = $attr['attr_list'];
  89. $data['pic'] = $attr['pic'];
  90. $data['no'] = $attr['no'];
  91. if(isset($attr['share_commission_first'])) $data['share_commission_first'] = $attr['share_commission_first'];
  92. if(isset($attr['share_commission_second'])) $data['share_commission_second'] = $attr['share_commission_second'];
  93. if(isset($attr['share_commission_third'])) $data['share_commission_third'] = $attr['share_commission_third'];
  94. $data['is_member_price'] = false;
  95. $data['is_level'] = false;
  96. $data['level_price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'] ;//会员折扣价
  97. $data['batch_price_tips'] = $batch_price_tips;
  98. $data['current_batch_price_tips'] = $current_batch_price_tips;
  99. // 用户等级 可以传入指定用户等级
  100. //$userLevel = $level['userLevel'] ? $level['userLevel'] : 0;
  101. // $keyName = 'member' . $userLevel;
  102. // // 为会员用户 会员价 > 0 并且商品开启了会员折扣 则显示会员价
  103. // if (isset($attr[$keyName]) && $attr[$keyName] > 0 && $goods['is_level'] && $level['userLevel'] >= 0) {
  104. // $data['price'] = $attr[$keyName];
  105. // $data['is_member_price'] = true;
  106. // $data['is_level'] = true;
  107. // }
  108. // \Yii::error([$attr, $keyName, $goods, $level]);
  109. // // 为会员用户 开启了规格会员价、但没设置或者规格价等于0 则使用全局会员折扣
  110. // if (isset($attr[$keyName]) && $attr[$keyName] <= 0 && $goods['is_level'] && $level['userLevel'] >= 0) {
  111. // // 如果算出的会员折扣价 < 0.01 那会员价就是0
  112. // $data['price'] = (($data['price'] * $level['discount']) / 10) >= 0.01 ? (($data['price'] * $level['discount']) / 10) : 0.00;
  113. // $data['is_member_price'] = true;
  114. // $data['is_level'] = true;
  115. // }
  116. // // 商城开启了会员折扣 且是会员用户
  117. // if ($level['userLevel'] >= 0 && $goods['is_level']) {
  118. // // 会员折扣价为 会员价
  119. // if ($data['is_member_price'] === true) {
  120. // $data['level_price'] = $data['price'];
  121. // // $data['is_level'] = false;
  122. // } else {
  123. // // 会员折扣价 根据会员折扣计算而来
  124. // $data['level_price'] = ($data['price'] * floatval($level['discount']) / 10) >= 0.01 ? ($data['price'] * floatval($level['discount']) / 10) : 0.00;
  125. // $data['is_level'] = true;
  126. // }
  127. // // TODO 特殊 拼团单买暂时不需要计算会员折扣
  128. // if (isset($otherData['order_type']) && $otherData['order_type'] === 'ONLY_BUY') {
  129. // $data['is_level'] = false;
  130. // // TODO 以下两个字段会被覆盖,没什么作用
  131. // $data['goods_price'] = $attr['single'] > 0 ? $attr['single'] : $otherData['single_price'];
  132. // $data['level_price'] = $data['single_price'];
  133. // }
  134. // }
  135. // // 商城开启了会员折扣 且是普通用户
  136. // if ($level['userLevel'] === -1 && $goods['is_level']) {
  137. // // 会员折扣价为 会员价
  138. // if ($data['is_member_price'] === true) {
  139. // $data['level_price'] = $data['goods_price'];
  140. // $data['is_level'] = false;
  141. // } else {
  142. // // 会员折扣价 根据会员折扣计算而来
  143. // $data['level_price'] = $data['goods_price'];
  144. // $data['is_level'] = false;
  145. // }
  146. // if (isset($otherData['order_type']) && $otherData['order_type'] === 'ONLY_BUY') {
  147. // $data['is_level'] = false;
  148. // $data['goods_price'] = $attr['single'] > 0 ? $attr['single'] : $otherData['single_price'];
  149. // $data['level_price'] = $data['single_price'];
  150. // }
  151. // }
  152. $data['price'] = sprintf("%.2f", $data['price']) > 0 ? sprintf("%.2f", $data['price']) : $goods['price'];
  153. $data['level_price'] = sprintf("%.2f", $data['level_price']) > 0 ? sprintf("%.2f", $data['level_price']) : sprintf("%.2f", $data['price']);
  154. return $data;
  155. }
  156. }
  157. }
  158. /**
  159. * 获取当前商品的最高分销价、及最低会员价(根据用户等级)
  160. * user_is_member 是否会员
  161. * is_level 是否开启会员折扣
  162. * is_share 是否开启分销
  163. * min_member_price 最低会员价
  164. * max_share_price 最高分销价
  165. * @param $goods
  166. * @return array
  167. */
  168. public static function getMMPrice(array $goods, $otherData = [])
  169. {
  170. $attrs = Json::decode($goods['attr']);
  171. $storeId = get_store_id();
  172. $level = self::currentLevelDiscount();
  173. $maxSharePriceArr = [];
  174. $minMemberPriceArr = [];
  175. foreach ($attrs as $attr) {
  176. $price = $goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];
  177. // 商品开启单独分销设置 (按一级分销佣金计算)
  178. if ((int)$goods['individual_share'] === 1) {
  179. // 普通设置 (单商品全局)
  180. if ((int)$goods['attr_setting_type'] === 0 && $goods['share_commission_first'] > 0) {
  181. // 分销普通设置 按百分比
  182. if ((int)$goods['share_type'] === 0) {
  183. $maxSharePriceArr[] = ($goods['share_commission_first'] * $price) / 100;
  184. }
  185. // 分销普通设置 按固定金额
  186. if ((int)$goods['share_type'] === 1) {
  187. $maxSharePriceArr[] = $goods['share_commission_first'];
  188. }
  189. }
  190. // 详细设置 (多规格分销价)
  191. if ((int)$goods['attr_setting_type'] === 1 && $attr['share_commission_first'] > 0) {
  192. if ((int)$goods['share_type'] === 0) {
  193. $maxSharePriceArr[] = ($attr['share_commission_first'] * $price) / 100;
  194. }
  195. if ((int)$goods['share_type'] === 1) {
  196. $maxSharePriceArr[] = $attr['share_commission_first'];
  197. }
  198. }
  199. }
  200. // 开启商品会员折扣
  201. $data['user_is_member'] = false;
  202. if ((int)$goods['is_level'] === 1) {
  203. // 普通用户 (显示下一级会员价)
  204. if ($level['userLevel'] === -1) {
  205. $data['user_is_member'] = false;
  206. $keyName = 'member' . $level['list'][0]['level'];
  207. // 多规格会员价大于 > 0 则直接展示
  208. if (isset($attr[$keyName]) && $attr[$keyName] > 0) {
  209. $minMemberPriceArr[] = $attr[$keyName];
  210. }
  211. // 如果开启了会员折扣,但i设置的多规格会员价为0,则使用下一级全局会员折扣
  212. if (isset($attr[$keyName]) && $attr[$keyName] <= 0) {
  213. $minMemberPriceArr[] = ($price * $level['list'][0]['discount']) / 10;
  214. }
  215. }
  216. // 会员用户 (显示当前会员价)
  217. if ($level['userLevel'] >= 0) {
  218. $data['user_is_member'] = true;
  219. $keyName = 'member' . $level['userLevel'];
  220. // 多规格会员价大于 > 0 则直接展示
  221. if (isset($attr[$keyName]) && $attr[$keyName] > 0) {
  222. $minMemberPriceArr[] = $attr[$keyName];
  223. }
  224. // 如果开启了会员折扣,但设置的多规格会员价为0,则使用全局会员折扣
  225. if (isset($attr[$keyName]) && $attr[$keyName] <= 0) {
  226. $minMemberPriceArr[] = ($price * $level['discount']) / 10;
  227. }
  228. }
  229. }
  230. // 未开启商品会员折扣
  231. if ((int)$goods['is_level'] === 0) {
  232. // 显示最小规格价
  233. $minMemberPriceArr[] = $price;
  234. }
  235. }
  236. $data['max_share_price'] = !empty($maxSharePriceArr) ? max($maxSharePriceArr) : 0;
  237. $data['min_member_price'] = !empty($minMemberPriceArr) ? min($minMemberPriceArr) : 0;
  238. // 商品是否开启会员折扣
  239. $data['is_level'] = false;
  240. if ((int)$goods['is_level'] === 1) {
  241. $data['is_level'] = true;
  242. }
  243. // 总商城分销 是否开启
  244. $data['is_share'] = false;
  245. // 如果会员价 <= 0 则不显示
  246. if (sprintf('%.2f', $data['min_member_price']) <= 0) {
  247. $data['is_level'] = false;
  248. }
  249. // 如果会员中心一个会员都没有,则不显示会员价
  250. if (empty($level['list'])) {
  251. $data['is_level'] = false;
  252. }
  253. return $data;
  254. }
  255. /**
  256. * 获取当前会员折扣
  257. */
  258. private static function currentLevelDiscount()
  259. {
  260. $userLevel = isset(get_user()->level) ? get_user()->level : -1;
  261. $storeId = get_store_id();
  262. $levelList = Level::find()->where(['store_id' => $storeId, 'is_delete' => 0, 'status' => Level::STATUS_TRUE])
  263. ->select('id, level, name, discount')
  264. ->orderBy('level')
  265. ->asArray()->all();
  266. $currentLevelDiscount = 10;
  267. foreach ($levelList as $level) {
  268. if ((int)$level['level'] === $userLevel) {
  269. $currentLevelDiscount = $level['discount'];
  270. break;
  271. }
  272. }
  273. return [
  274. 'discount' => $currentLevelDiscount,
  275. 'userLevel' => $userLevel,
  276. 'list' => $levelList
  277. ];
  278. }
  279. public static function getMemberPrice(array $goods, $otherData = [])
  280. {
  281. $attrs = json_decode($goods['attr'], true);
  282. $storeId = get_store_id();
  283. $level = self::currentLevelDiscount();
  284. $minMemberPriceArr = [];
  285. foreach ($attrs as $attr) {
  286. $price = $attr['price'] > 0 ? $attr['price'] : $goods['price'];
  287. // 秒杀插件规格价字段有所不同
  288. if ($otherData && $otherData['type'] === 'MIAOSHA') {
  289. $price = $attr['miaosha_price'] > 0 ? $attr['miaosha_price'] : $goods['price'];
  290. }
  291. // 开启商品会员折扣
  292. $data['user_is_member'] = false;
  293. if ((int)$goods['is_level'] === 1) {
  294. // 普通用户 (显示下一级会员价)
  295. if ($level['userLevel'] === -1) {
  296. $data['user_is_member'] = false;
  297. $keyName = 'member' . $level['list'][0]['level'];
  298. // 多规格会员价大于 > 0 则直接展示
  299. if (isset($attr[$keyName])) {
  300. if ($attr[$keyName] > 0) {
  301. $minMemberPriceArr[] = $attr[$keyName];
  302. }
  303. // 如果开启了会员折扣,但i设置的多规格会员价为0,则使用下一级全局会员折扣
  304. if ($attr[$keyName] <= 0) {
  305. $minMemberPriceArr[] = ($price * $level['list'][0]['discount']) / 10;
  306. }
  307. }
  308. }
  309. // 会员用户 (显示当前会员价)
  310. if ($level['userLevel'] >= 0) {
  311. $data['user_is_member'] = true;
  312. $keyName = 'member' . $level['userLevel'];
  313. if (isset($attr[$keyName])) {
  314. // 多规格会员价大于 > 0 则直接展示
  315. if ($attr[$keyName] > 0) {
  316. $minMemberPriceArr[] = $attr[$keyName];
  317. }
  318. // 如果开启了会员折扣,但设置的多规格会员价为0,则使用全局会员折扣
  319. if ($attr[$keyName] <= 0) {
  320. $minMemberPriceArr[] = ($price * $level['discount']) / 10;
  321. }
  322. }
  323. }
  324. }
  325. // 未开启商品会员折扣
  326. if ((int)$goods['is_level'] === 0) {
  327. // 显示最小规格价
  328. $minMemberPriceArr[] = $price;
  329. }
  330. }
  331. $data['min_member_price'] = !empty($minMemberPriceArr) ? min($minMemberPriceArr) : 0;
  332. // 商品是否开启会员折扣
  333. $data['is_level'] = false;
  334. if ((int)$goods['is_level'] === 1) {
  335. $data['is_level'] = true;
  336. }
  337. // 如果会员价 <= 0 则不显示
  338. if (sprintf('%.2f', $data['min_member_price']) <= 0) {
  339. $data['is_level'] = false;
  340. }
  341. // 如果会员中心一个会员都没有,则不显示会员价
  342. if (empty($level['list'])) {
  343. $data['is_level'] = false;
  344. }
  345. return $data;
  346. }
  347. }