| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\models;
- use app\models\Level;
- use app\models\Goods;
- use app\models\MchSetting;
- use app\models\SupplierSetting;
- use yii\helpers\Json;
- class CommonGoods
- {
- /**
- * 获取当前规格的相应信息
- * @param array $goods 商品信息
- * @param array $currentAttrIds 当前选择的规格数据 例:[42,43]
- * @param array $otherData 特殊数据处理 阶梯团、秒杀
- * @return array
- */
- public static function currentGoodsAttr(array $goods, array $currentAttrIds, array $otherData = [], $number = 1,$goods_id=0)
- {
- $batch_price_tips = '';
- $current_batch_price_tips = '';
- $is_true = 0;
- $full_minus_attrs = [];
- /**
- * 判断商品是否开启会员折扣,且用户是否为会员,且会员中的折扣存在
- */
- //获取用户信息
- // $user = get_user();
- // //获取会员折扣信息
- // $level = Level::find()->where(['store_id' => get_store_id(), 'status' => 1, 'is_delete' => 0, 'level' => $user->level])->andWhere(['>', 'discount', 0])->select('id')->one();
- // $open = false;
- // //判断商品是否开启会员折扣,且用户是否为会员,且会员中的折扣存在
- // if ($goods['is_level'] == 1 && $user->level && !empty($level)) {
- // $open = false;
- // }
- // //如果以上条件成立,则不使用满减折扣
- // if (isset($goods['full_minus']) && $open){
- // foreach ($goods['full_minus'] as $k => $v){
- // $full_minus_list = json_decode($goods['full_minus'][$k]['attr'], true);
- // if (!$full_minus_list || count($full_minus_list) < 1) {
- // continue;
- // }
- // foreach ($full_minus_list as $key => $value) {
- // $attrIds = [];
- // foreach ($full_minus_list[$key]['attr_list'] as $item) {
- // $attrIds[] = $item['attr_id'];
- // }
- // sort($attrIds);
- // sort($currentAttrIds);
- // if (($number >= $goods['full_minus'][$k]['full_minus_num']) && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']) {
- // $full_minus_attrs = $full_minus_list[$key];
- // $current_batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
- // }
- // if (($number <= $goods['full_minus'][$k]['full_minus_num']) && $is_true == 0 && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']){
- // $is_true = 1;
- // $batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
- // }elseif (($number > $goods['full_minus'][$k]['full_minus_num']) && (implode($attrIds) === implode($currentAttrIds)) && $full_minus_list[$key]['price']){
- // $batch_price_tips ='满'.$goods['full_minus'][$k]['full_minus_num'].$goods['unit'].', '. $full_minus_list[$key]['price'] .'元';
- // }
- // }
- // }
- // }
- $attrs = Json::decode($goods['attr']);
- //$level = self::currentLevelDiscount();
-
- foreach ($attrs as $attr) {
- $attrIds = [];
- foreach ($attr['attr_list'] as $item) {
- $attrIds[] = $item['attr_id'];
- }
- sort($attrIds);
- sort($currentAttrIds);
- // 找出当前规格信息
- if (implode($attrIds) === implode($currentAttrIds)) {
- if ($full_minus_attrs && $full_minus_attrs['price']){
- $attr['price'] = $full_minus_attrs['price'];
- }
-
- // price 前端展示价格(该价格可以是 原价、会员价)
- $data['price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];
-
- $data['num'] = $attr['num'];
- $data['supplier_id'] = isset($goods['supplier_id']) ? $goods['supplier_id'] : 0;
- $data['supplier_goods_id'] = isset($goods['supplier_goods_id']) ? $goods['supplier_goods_id'] : 0;
- $data['supplier_price'] = isset($attr['supplier_price']) ? ($attr['supplier_price'] > 0 ? $attr['supplier_price'] : (isset($goods['supplier_price']) ? $goods['supplier_price'] : 0)) : 0;
- $data['goods_price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];//商品售价
- $data['attr_list'] = $attr['attr_list'];
-
- $data['pic'] = $attr['pic'];
- $data['no'] = $attr['no'];
- if(isset($attr['share_commission_first'])) $data['share_commission_first'] = $attr['share_commission_first'];
- if(isset($attr['share_commission_second'])) $data['share_commission_second'] = $attr['share_commission_second'];
- if(isset($attr['share_commission_third'])) $data['share_commission_third'] = $attr['share_commission_third'];
- $data['is_member_price'] = false;
- $data['is_level'] = false;
- $data['level_price'] = $attr['price'] > 0 ? $attr['price'] : $goods['price'];//$goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'] ;//会员折扣价
- $data['batch_price_tips'] = $batch_price_tips;
- $data['current_batch_price_tips'] = $current_batch_price_tips;
- // 用户等级 可以传入指定用户等级
- //$userLevel = $level['userLevel'] ? $level['userLevel'] : 0;
- // $keyName = 'member' . $userLevel;
- // // 为会员用户 会员价 > 0 并且商品开启了会员折扣 则显示会员价
- // if (isset($attr[$keyName]) && $attr[$keyName] > 0 && $goods['is_level'] && $level['userLevel'] >= 0) {
- // $data['price'] = $attr[$keyName];
- // $data['is_member_price'] = true;
- // $data['is_level'] = true;
- // }
- // \Yii::error([$attr, $keyName, $goods, $level]);
- // // 为会员用户 开启了规格会员价、但没设置或者规格价等于0 则使用全局会员折扣
- // if (isset($attr[$keyName]) && $attr[$keyName] <= 0 && $goods['is_level'] && $level['userLevel'] >= 0) {
- // // 如果算出的会员折扣价 < 0.01 那会员价就是0
- // $data['price'] = (($data['price'] * $level['discount']) / 10) >= 0.01 ? (($data['price'] * $level['discount']) / 10) : 0.00;
- // $data['is_member_price'] = true;
- // $data['is_level'] = true;
- // }
- // // 商城开启了会员折扣 且是会员用户
- // if ($level['userLevel'] >= 0 && $goods['is_level']) {
- // // 会员折扣价为 会员价
- // if ($data['is_member_price'] === true) {
- // $data['level_price'] = $data['price'];
- // // $data['is_level'] = false;
- // } else {
- // // 会员折扣价 根据会员折扣计算而来
- // $data['level_price'] = ($data['price'] * floatval($level['discount']) / 10) >= 0.01 ? ($data['price'] * floatval($level['discount']) / 10) : 0.00;
- // $data['is_level'] = true;
- // }
- // // TODO 特殊 拼团单买暂时不需要计算会员折扣
- // if (isset($otherData['order_type']) && $otherData['order_type'] === 'ONLY_BUY') {
- // $data['is_level'] = false;
- // // TODO 以下两个字段会被覆盖,没什么作用
- // $data['goods_price'] = $attr['single'] > 0 ? $attr['single'] : $otherData['single_price'];
- // $data['level_price'] = $data['single_price'];
- // }
- // }
- // // 商城开启了会员折扣 且是普通用户
- // if ($level['userLevel'] === -1 && $goods['is_level']) {
- // // 会员折扣价为 会员价
- // if ($data['is_member_price'] === true) {
- // $data['level_price'] = $data['goods_price'];
- // $data['is_level'] = false;
- // } else {
- // // 会员折扣价 根据会员折扣计算而来
- // $data['level_price'] = $data['goods_price'];
- // $data['is_level'] = false;
- // }
- // if (isset($otherData['order_type']) && $otherData['order_type'] === 'ONLY_BUY') {
- // $data['is_level'] = false;
- // $data['goods_price'] = $attr['single'] > 0 ? $attr['single'] : $otherData['single_price'];
- // $data['level_price'] = $data['single_price'];
- // }
- // }
- $data['price'] = sprintf("%.2f", $data['price']) > 0 ? sprintf("%.2f", $data['price']) : $goods['price'];
- $data['level_price'] = sprintf("%.2f", $data['level_price']) > 0 ? sprintf("%.2f", $data['level_price']) : sprintf("%.2f", $data['price']);
- return $data;
- }
- }
- }
- /**
- * 获取当前商品的最高分销价、及最低会员价(根据用户等级)
- * user_is_member 是否会员
- * is_level 是否开启会员折扣
- * is_share 是否开启分销
- * min_member_price 最低会员价
- * max_share_price 最高分销价
- * @param $goods
- * @return array
- */
- public static function getMMPrice(array $goods, $otherData = [])
- {
- $attrs = Json::decode($goods['attr']);
- $storeId = get_store_id();
- $level = self::currentLevelDiscount();
- $maxSharePriceArr = [];
- $minMemberPriceArr = [];
- foreach ($attrs as $attr) {
- $price = $goods['use_attr'] == 1 ? ($attr['price'] > 0 ? $attr['price'] : $goods['price']) : $goods['price'];
- // 商品开启单独分销设置 (按一级分销佣金计算)
- if ((int)$goods['individual_share'] === 1) {
- // 普通设置 (单商品全局)
- if ((int)$goods['attr_setting_type'] === 0 && $goods['share_commission_first'] > 0) {
- // 分销普通设置 按百分比
- if ((int)$goods['share_type'] === 0) {
- $maxSharePriceArr[] = ($goods['share_commission_first'] * $price) / 100;
- }
- // 分销普通设置 按固定金额
- if ((int)$goods['share_type'] === 1) {
- $maxSharePriceArr[] = $goods['share_commission_first'];
- }
- }
- // 详细设置 (多规格分销价)
- if ((int)$goods['attr_setting_type'] === 1 && $attr['share_commission_first'] > 0) {
- if ((int)$goods['share_type'] === 0) {
- $maxSharePriceArr[] = ($attr['share_commission_first'] * $price) / 100;
- }
- if ((int)$goods['share_type'] === 1) {
- $maxSharePriceArr[] = $attr['share_commission_first'];
- }
- }
- }
- // 开启商品会员折扣
- $data['user_is_member'] = false;
- if ((int)$goods['is_level'] === 1) {
- // 普通用户 (显示下一级会员价)
- if ($level['userLevel'] === -1) {
- $data['user_is_member'] = false;
- $keyName = 'member' . $level['list'][0]['level'];
- // 多规格会员价大于 > 0 则直接展示
- if (isset($attr[$keyName]) && $attr[$keyName] > 0) {
- $minMemberPriceArr[] = $attr[$keyName];
- }
- // 如果开启了会员折扣,但i设置的多规格会员价为0,则使用下一级全局会员折扣
- if (isset($attr[$keyName]) && $attr[$keyName] <= 0) {
- $minMemberPriceArr[] = ($price * $level['list'][0]['discount']) / 10;
- }
- }
- // 会员用户 (显示当前会员价)
- if ($level['userLevel'] >= 0) {
- $data['user_is_member'] = true;
- $keyName = 'member' . $level['userLevel'];
- // 多规格会员价大于 > 0 则直接展示
- if (isset($attr[$keyName]) && $attr[$keyName] > 0) {
- $minMemberPriceArr[] = $attr[$keyName];
- }
- // 如果开启了会员折扣,但设置的多规格会员价为0,则使用全局会员折扣
- if (isset($attr[$keyName]) && $attr[$keyName] <= 0) {
- $minMemberPriceArr[] = ($price * $level['discount']) / 10;
- }
- }
- }
- // 未开启商品会员折扣
- if ((int)$goods['is_level'] === 0) {
- // 显示最小规格价
- $minMemberPriceArr[] = $price;
- }
- }
- $data['max_share_price'] = !empty($maxSharePriceArr) ? max($maxSharePriceArr) : 0;
- $data['min_member_price'] = !empty($minMemberPriceArr) ? min($minMemberPriceArr) : 0;
- // 商品是否开启会员折扣
- $data['is_level'] = false;
- if ((int)$goods['is_level'] === 1) {
- $data['is_level'] = true;
- }
- // 总商城分销 是否开启
- $data['is_share'] = false;
- // 如果会员价 <= 0 则不显示
- if (sprintf('%.2f', $data['min_member_price']) <= 0) {
- $data['is_level'] = false;
- }
- // 如果会员中心一个会员都没有,则不显示会员价
- if (empty($level['list'])) {
- $data['is_level'] = false;
- }
- return $data;
- }
- /**
- * 获取当前会员折扣
- */
- private static function currentLevelDiscount()
- {
- $userLevel = isset(get_user()->level) ? get_user()->level : -1;
- $storeId = get_store_id();
- $levelList = Level::find()->where(['store_id' => $storeId, 'is_delete' => 0, 'status' => Level::STATUS_TRUE])
- ->select('id, level, name, discount')
- ->orderBy('level')
- ->asArray()->all();
- $currentLevelDiscount = 10;
- foreach ($levelList as $level) {
- if ((int)$level['level'] === $userLevel) {
- $currentLevelDiscount = $level['discount'];
- break;
- }
- }
- return [
- 'discount' => $currentLevelDiscount,
- 'userLevel' => $userLevel,
- 'list' => $levelList
- ];
- }
- public static function getMemberPrice(array $goods, $otherData = [])
- {
- $attrs = json_decode($goods['attr'], true);
- $storeId = get_store_id();
- $level = self::currentLevelDiscount();
- $minMemberPriceArr = [];
- foreach ($attrs as $attr) {
- $price = $attr['price'] > 0 ? $attr['price'] : $goods['price'];
- // 秒杀插件规格价字段有所不同
- if ($otherData && $otherData['type'] === 'MIAOSHA') {
- $price = $attr['miaosha_price'] > 0 ? $attr['miaosha_price'] : $goods['price'];
- }
- // 开启商品会员折扣
- $data['user_is_member'] = false;
- if ((int)$goods['is_level'] === 1) {
- // 普通用户 (显示下一级会员价)
- if ($level['userLevel'] === -1) {
-
- $data['user_is_member'] = false;
- $keyName = 'member' . $level['list'][0]['level'];
- // 多规格会员价大于 > 0 则直接展示
- if (isset($attr[$keyName])) {
- if ($attr[$keyName] > 0) {
- $minMemberPriceArr[] = $attr[$keyName];
- }
- // 如果开启了会员折扣,但i设置的多规格会员价为0,则使用下一级全局会员折扣
- if ($attr[$keyName] <= 0) {
- $minMemberPriceArr[] = ($price * $level['list'][0]['discount']) / 10;
- }
- }
- }
- // 会员用户 (显示当前会员价)
- if ($level['userLevel'] >= 0) {
- $data['user_is_member'] = true;
- $keyName = 'member' . $level['userLevel'];
- if (isset($attr[$keyName])) {
- // 多规格会员价大于 > 0 则直接展示
- if ($attr[$keyName] > 0) {
- $minMemberPriceArr[] = $attr[$keyName];
- }
- // 如果开启了会员折扣,但设置的多规格会员价为0,则使用全局会员折扣
- if ($attr[$keyName] <= 0) {
- $minMemberPriceArr[] = ($price * $level['discount']) / 10;
- }
- }
- }
- }
- // 未开启商品会员折扣
- if ((int)$goods['is_level'] === 0) {
- // 显示最小规格价
- $minMemberPriceArr[] = $price;
- }
- }
- $data['min_member_price'] = !empty($minMemberPriceArr) ? min($minMemberPriceArr) : 0;
- // 商品是否开启会员折扣
- $data['is_level'] = false;
- if ((int)$goods['is_level'] === 1) {
- $data['is_level'] = true;
- }
- // 如果会员价 <= 0 则不显示
- if (sprintf('%.2f', $data['min_member_price']) <= 0) {
- $data['is_level'] = false;
- }
- // 如果会员中心一个会员都没有,则不显示会员价
- if (empty($level['list'])) {
- $data['is_level'] = false;
- }
- return $data;
- }
- }
|