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