| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- <?php
- /**
- * 红包抵扣计算
- */
- namespace addons\Redpack\common\logic\order;
- use addons\Redpack\common\enums\RedpackEnum;
- use addons\Redpack\common\models\RedpackGoodsSetting;
- use addons\Redpack\common\models\RedpackWallet;
- use common\enums\AppEnum;
- use common\enums\StatusEnum;
- use common\traits\ErrorTrait;
- use yii\helpers\Json;
- class RedpackMarketing
- {
- use ErrorTrait;
- /**
- * 红包抵扣处理
- * @Author: lun
- * @DateTime: 2021/10/6 0006 11:02
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $item
- * @param $form
- * @param array $extra
- * @return mixed
- */
- public static function computeOld($item, &$form, array &$extra = [])
- {
- try {
- $mall_id = $item['mch_order_info']['mall_id'];
- $user_id = $item['mch_order_info']['user_id'];
- $redpack_text_arr = RedpackEnum::getRedpackText('', $mall_id);// 红包中文名称
- $deduct_money = 0;// 抵扣总额
- $deduct_arr = [];// 抵扣冻结跟金额数组
- // 商品处理
- foreach ($item['goods'] as $goods_id => &$goods) {
- // 查询该商品是否设置了红包抵扣
- $redpack = RedpackGoodsSetting::find()->where(['goods_id' => $goods_id, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
- if (empty($redpack)) continue;
- // 单件抵扣:比如单件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额10元
- // 多件抵扣:比如多件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额30元
- $num = $goods['num'];// 商品数量
- foreach ($redpack as $value) {
- $money = $value['deduct_type'] == 1 ? $value['deduct_money'] : $value['deduct_money'] * $num;// 计算抵扣金额
- if ($money <= 0) continue;
- $redpack_text = $redpack_text_arr[$value['redpack_wallet_type']];// 红包中文名称
- $redpack_en = $value['redpack_wallet_type'];// 红包英文标识
- // 检查是否有被选中
- $is_use = !empty($form->data['is_use_' . $redpack_en]) ? true : false;
- // 前端抵扣数据展示组合
- if ($form->action == AppEnum::ACTION_PREVIEW) {
- $form->marketing['deduct'][$redpack_en] = [
- 'name' => $redpack_text,
- 'deduct_val' => empty($form->marketing['deduct'][$redpack_en]['deduct_val']) ? $money : $form->marketing['deduct'][$redpack_en]['deduct_val'] + $money,
- 'money' => empty($form->marketing['deduct'][$redpack_en]['money']) ? $money : $form->marketing['deduct'][$redpack_en]['money'] + $money,
- 'is_show_radio' => true,// 是否显示单选按钮
- 'is_use' => $is_use,// 是否被选中单选按钮
- ];
- }
- // 组合订单优惠表数据
- if (!empty($form->data['is_use_' . $redpack_en])) {
- $deduct_currency = $goods['goods_details'][0]['marketing']['deduct_currency'];
- if($deduct_currency){
- $deduct_currency = (array_merge($deduct_currency, [$redpack_en => ['name' => RedpackEnum::getRedpackText($redpack_en, $mall_id),'money' => $money]]));// 将合并的数组转为json字符串
- }else{
- $deduct_currency = [$redpack_en => ['name' => RedpackEnum::getRedpackText($redpack_en, $mall_id),'money' => $money]];
- }
- $goods['goods_details'][0]['marketing']['deduct_currency'] = $deduct_currency;// 插入order_marketing表数据
- $goods_price = $item['goods'][$goods_id]['goods_price'];
- $item['goods'][$goods_id]['goods_price'] -= $money;// 此商品减扣后的金额计算
- if($item['goods'][$goods_id]['goods_price']<0){
- $item['goods'][$goods_id]['goods_price'] = 0;
- $money = $goods_price;
- }
- $deduct_money += $money;
- $deduct_arr[$redpack_en] = empty($deduct_arr[$redpack_en]) ? $money : $deduct_arr[$redpack_en] + $money;
- }
- }
- $goods['goods_details'][0]['marketing']['deduct_currency'] = json_encode($goods['goods_details'][0]['marketing']['deduct_currency']);
- }
- // 计算订单减扣后的金额
- if ($deduct_money > 0) {
- $item['mch_order_info']['pay_money'] -= $deduct_money;
- $item['mch_order_info']['goods_price'] -= $deduct_money;
- // 扣减红包金额
- if ($form->action == AppEnum::ACTION_CREATE) {
- foreach ($deduct_arr as $key => $deduct_redpack_money) {
- $res = RedpackWallet::setData([
- 'mall_id' => $mall_id,
- 'user_id' => $user_id,
- 'is_frozen' => $key == RedpackEnum::WALLET_REDPACK ? false : true,
- 'money' => -$deduct_redpack_money,
- 'from_type' => RedpackEnum::FROM_DEDUTION,
- ], false);
- if ($res == false) throw new \Exception(RedpackWallet::getStaticError());
- }
- }
- }
- return $item;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- public static function compute($item, &$form, array &$extra = [])
- {
- try {
- $mall_id = $item['mch_order_info']['mall_id'];
- $user_id = $item['mch_order_info']['user_id'];
- $redpack_text_arr = RedpackEnum::getRedpackText('', $mall_id);// 红包中文名称
- $deduct_money = 0;// 抵扣总额
- $deduct_arr = [];// 抵扣冻结跟金额数组
- // 商品处理
- $user_red_pack_wallet = RedpackWallet::findOne(['user_id'=>$user_id, 'status' => StatusEnum::ENABLED]);
- foreach ($item['goods'] as $goods_id => &$goods) {
- // 查询该商品是否设置了红包抵扣
- $redpack = RedpackGoodsSetting::find()->where(['goods_id' => $goods_id, 'is_enable' => StatusEnum::ENABLED, 'status' => StatusEnum::ENABLED])->asArray()->all();
- if (empty($redpack)) continue;
- // 单件抵扣:比如单件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额30元
- // 多件抵扣:比如多件抵扣10元,购买A商品共3件,每件50元,总价=150元,可抵扣的金额10元
- foreach ($redpack as $value) {
- $total_goods_price = array_sum(array_column($goods['goods_details'],'goods_price'));//同一种商品不同规格总价格
- $total_num = array_sum(array_column($goods['goods_details'],'num'));// 同一种商品不同规格总数量
- if (!empty($value['is_deduct_percent'])) {
- // 按照百分比抵扣 = 商品金额 * 设置比例 / 100;百分比抵扣跟商品数量无关
- $money = $item['goods'][$goods_id]['goods_price'] * $value['deduct_money'] / 100;
- } else {
- // 固定金额抵扣
- $money = $value['deduct_type'] == 2 ? $value['deduct_money'] : $value['deduct_money'] * $total_num;// 计算抵扣金额
- }
- if($user_red_pack_wallet[$value['redpack_wallet_type']]<$money){
- $money = $user_red_pack_wallet[$value['redpack_wallet_type']];
- }
- $user_red_pack_wallet[$value['redpack_wallet_type']] -= $money;// 余额逐步减少
- if($money<=0) continue;
- $redpack_text = $redpack_text_arr[$value['redpack_wallet_type']];// 红包中文名称
- $redpack_en = $value['redpack_wallet_type'];// 红包英文标识
- // 检查是否有被选中
- $temp_total_deduct_money = 0;
- $is_use = false;
- if($total_goods_price<=0) continue;
- if (!empty($form->data['is_use_' . $redpack_en])) $is_use = true;
- foreach ($goods['goods_details'] as $k=>&$detail) {
- $rate = $detail['goods_price']/$total_goods_price;
- if(!isset($goods['goods_details'][$k+1])){
- //最后一个
- $temp_deduct_money = bcsub($money, $temp_total_deduct_money, 2);
- } else {
- $temp_deduct_money = bcmul($rate, $money, 2);
- }
- if( $detail['goods_price']>$temp_deduct_money){
- $is_use && $detail['goods_price'] -= $temp_deduct_money;
- }else{
- // $temp_deduct_money = $detail['goods_price'];
- $is_use && $detail['goods_price'] =0;
- }
- $temp_total_deduct_money += $temp_deduct_money;
- if ($is_use) {
- $deduct_currency = $detail['marketing']['deduct_currency'];
- if($deduct_currency){
- $deduct_currency = json_decode($deduct_currency,true);
- $deduct_currency = (array_merge($deduct_currency, [$redpack_en => ['name' => RedpackEnum::getRedpackText($redpack_en, $mall_id),'money' => $temp_deduct_money]]));// 将合并的数组转为json字符串
- }else{
- $deduct_currency = [$redpack_en => ['name' => RedpackEnum::getRedpackText($redpack_en, $mall_id),'money' => $temp_deduct_money]];
- }
- $detail['marketing']['deduct_currency'] = json_encode($deduct_currency);// 插入order_marketing表数据
- }
- }
- if ($is_use) {
- $item['goods'][$goods_id]['goods_price'] -= $temp_total_deduct_money;// 此商品减扣后的金额计算
- if($item['goods'][$goods_id]['goods_price']<0) $item['goods'][$goods_id]['goods_price'] = 0;
- $deduct_arr[$redpack_en] = empty($deduct_arr[$redpack_en]) ? $temp_total_deduct_money : $deduct_arr[$redpack_en] + $temp_total_deduct_money;
- }
- // 前端抵扣数据展示组合
- if ($form->action == AppEnum::ACTION_PREVIEW) {
- $form->marketing['deduct'][$redpack_en] = [
- 'name' => $redpack_text,
- 'deduct_val' => empty($form->marketing['deduct'][$redpack_en]['deduct_val']) ? round($temp_total_deduct_money, 2) : bcadd($form->marketing['deduct'][$redpack_en]['deduct_val'], $temp_total_deduct_money, 2),
- 'money' => empty($form->marketing['deduct'][$redpack_en]['money']) ? round($temp_total_deduct_money, 2) : bcadd($form->marketing['deduct'][$redpack_en]['money'], $temp_total_deduct_money, 2),
- 'is_show_radio' => true,// 是否显示单选按钮
- 'is_use' => $is_use,// 是否被选中单选按钮
- ];
- }
- $is_use && $deduct_money += $temp_total_deduct_money;
- }
- }
- // 计算订单减扣后的金额
- if ($deduct_money > 0) {
- $item['mch_order_info']['pay_money'] -= $deduct_money;
- $item['mch_order_info']['goods_price'] = bcsub(floatval($item['mch_order_info']['goods_price']), $deduct_money, 2);
- // 扣减红包金额
- if ($form->action == AppEnum::ACTION_CREATE && $deduct_arr) {
- foreach ($deduct_arr as $key => $deduct_redpack_money) {
- self::checkRedpack($mall_id, $user_id, $deduct_redpack_money, $key);
- }
- }
- }
- return $item;
- } catch (\Exception $e) {
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- /**
- * 检测用户余额是否足够
- * @param $mall_id
- * @param $user_id
- * @param $money
- * @param $redpack_type
- * @return void
- * @throws \Exception
- */
- public static function checkRedpack($mall_id, $user_id, $money, $redpack_type)
- {
- $wallet = RedpackWallet::findOne(['user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (empty($wallet)) throw new \Exception(RedpackEnum::getRedpackText($redpack_type, $mall_id) . "余额不足");
- if ($wallet[$redpack_type] < $money) throw new \Exception(RedpackEnum::getRedpackText($redpack_type, $mall_id) . "余额不足");
- }
- }
|