'主键', 'order_detail_id' => 'mall_id', 'give_score' => '赠送积分', 'give_balance' => '赠送余额', 'give_currency' => '赠送货币(json字符串保存)', 'deduct_score' => '减扣积分', 'deduct_currency' => '减扣货币(json字符串保存)', 'pay_money' => '商品实际付款金额,单位:元', 'full_reduce_money' => '满减金额,单位:元', 'coupon_discount_money' => '优惠券优惠金额,单位:元', 'user_coupon_id' => '用户优惠券id', 'member_price_deduct' => '会员价优惠金额', 'give_balance_settlement' => '赠送余额的结算方式:0:订单完成,1:支付完成', 'status' => '状态[-1:删除;0:禁用;1启用]', 'created_at' => '创建时间', 'updated_at' => '修改时间', ]; } public function getOrderDetail() { return $this->hasOne(OrderDetail::class, ['id' => 'order_detail_id']); } /** * 营销支付赠送 * @Author: lun * @DateTime: 2021/11/16 0016 19:51 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $mallId * @param $user_id * @param $source_table_id * @return bool * @throws \Exception */ public static function giveMarketing($mallId, $user_id, $source_table_id) { try { // 查询订单详情 $detail_ids = OrderDetail::find()->select('id')->where(['order_id' => $source_table_id])->asArray()->column(); if (empty($detail_ids)) throw new \Exception('订单详情id查询为空'); // 获取订单的营销数据 $order_marketing_list = self::find()->andWhere(['in', 'order_detail_id', $detail_ids])->all(); if (empty($order_marketing_list)) return true; $give_type = [UserWalletService::WALLET_TYPE_SCORE]; $give_money = []; // 循环计算赠送数量 foreach ($order_marketing_list as $marketing) { if (!isset($give_money[UserWalletService::WALLET_TYPE_SCORE])) $give_money[UserWalletService::WALLET_TYPE_SCORE] = 0; $give_money[UserWalletService::WALLET_TYPE_SCORE] += $marketing->give_score; } // 插入资金 foreach ($give_type as $type) { if ($give_money[$type] <= 0) continue; $insert_wallet[] = [ 'mall_id' => $mallId, 'user_id' => $user_id, 'is_frozen' => false, 'wallet_type' => $type, 'money' => $give_money[$type], 'desc' => '订单支付赠送', 'source_table' => Order::tableName(), 'source_table_id' => $source_table_id, 'from_type' => UserWalletEnum::CONSUME, 'capital_type' => UserWalletEnum::CONSUME_ORDER_GIVE, ]; $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new \Exception(UserWalletService::getStaticError()); } return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } /** * @desc:获取一个订单的marketing信息 * @Author: hua * @Date: 2021/11/18 * @Time: 9:41 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order * @return array */ public static function getMarketingInfo($order) { $member_price = $coupon_discount_money = $full_reduce_money = $score = $score_deduct_money = $agent_deduct = $user_card_deduct = 0; if (!empty($order['detail'])) { $currency = []; $order_detail_ids = array_column($order['detail'], 'id'); $order_marketing_list = self::lists(['where' => [['order_detail_id' => $order_detail_ids]]]); foreach ($order_marketing_list as $value) { $member_price += $value['member_price_deduct']; $coupon_discount_money += $value['coupon_discount_money']; $full_reduce_money += $value['full_reduce_money']; $score += $value['deduct_score']; $agent_deduct += $value['agent_deduct']; $deduct_currency = json_decode($value['deduct_currency'], true); if (!empty($deduct_currency)) { foreach ($deduct_currency as $currency_key => $item) { $name = $item['name'] ?? ''; $currency[$currency_key] = [ 'name' => $name . '抵扣', 'money' => -$item['money'], ]; } } $user_card_id = json_decode($value['user_card_id'],true) ?? []; if(!empty(($user_card_id))){ $user_card_deduct += $user_card_id['deduct_money']; } } } $member_price = bcmul($member_price,1,2); $full_reduce_money = bcmul($full_reduce_money,1,2); $coupon_discount_money = bcmul($coupon_discount_money,1,2); $agent_deduct = bcmul($agent_deduct,1,2); $user_card_deduct = bcmul($user_card_deduct,1,2); $marketing['deduct'] = [ 'member_price' => ['name' => '会员优惠', 'money' => -$member_price], 'full_reduce' => ['name' => '满减', 'money' => -$full_reduce_money], 'coupon' => ['name' => '优惠券', 'money' => -$coupon_discount_money], ]; if($user_card_deduct && $user_card_deduct > 0){ $marketing['deduct']['user_card_deduct'] = ['name' => '折扣价', 'money' => -$user_card_deduct]; } !empty($currency) && $marketing['deduct'] = array_merge($marketing['deduct'], $currency); if (MallAddons::isInstall($order['mall_id'], AddonsEnum::ADDONS_NAME_AGENT)){ $marketing['deduct']['agent_deduct'] = ['name' => '经销商内购商品抵扣', 'money' => -$agent_deduct]; } // 拼团免费开团优惠显示 if ( MallAddons::isInstall($order['mall_id'], AddonsEnum::ADDONS_NAME_GROUP_BUY) && ($order['marketing_type'] === AddonsEnum::ADDONS_NAME_GROUP_BUY || $order['order_source'] === AddonsEnum::ADDONS_NAME_GROUP_BUY) ){ // 查询是否是免费开团,是的话则显示优惠 $groupBuyMarketing = GroupBuyOrder::find() ->where([ 'order_id' => $order['id'], 'mall_id' => $order['mall_id'], 'status' => StatusEnum::ENABLED, 'is_free' => 1 ]) ->select('marketing') ->scalar(); $groupBuyMarketing = json_decode($groupBuyMarketing, true) ?: []; if (isset($groupBuyMarketing['is_free_open_group'])) { $marketing['deduct']['is_free_open_group'] = $groupBuyMarketing['is_free_open_group']; } } return $marketing; } /** * 订单扣减返还 * @Author: lun * @DateTime: 2021/11/20 0020 14:27 * @Copyright: copyright (c) 2021 广东七件事集团 * @param $order_id * @param $order_detail_id_all //该订单涉及的所有订单详情id * @param $order_detail_ids //要返还的订单详情id * @param $mall_id * @param $user_id * @param $desc * @param $from_type * @param $capital_type */ public static function deductBack($order_id, $order_detail_id_all, $order_detail_ids, $mall_id, $user_id, $desc, $from_type = UserWalletEnum::CONSUME, $capital_type = UserWalletEnum::CONSUME_ORDER_CLOSE) { try { $order_marketing_list = OrderMarketing::lists([ 'where' => [['order_detail_id' => $order_detail_ids], ['status' => StatusEnum::ENABLED]], 'select' => 'deduct_score,user_coupon_id,deduct_currency' ]); $back_score = 0; // $back_score = array_sum(array_column($order_marketing_list, 'deduct_score')); foreach($order_marketing_list as $val){ $deduct_currency = (array)json_decode($val['deduct_currency'], true); if (strpos($val['deduct_currency'], 'offset') !== false) { $deduct_currency = (array)json_decode($val['deduct_currency'], true)['offset']; } if(isset($deduct_currency['score'])) $back_score += $val['deduct_score']; } if (!empty($back_score) && $back_score > 0) { $insert_wallet[] = [ 'mall_id' => $mall_id, 'user_id' => $user_id, 'is_frozen' => false, 'not_add_total' => true, 'wallet_type' => UserWalletService::WALLET_TYPE_SCORE, 'money' => $back_score, 'desc' => $desc, 'source_table' => OrderDetail::tableName(), 'source_table_id' => join(',', $order_detail_ids), 'from_type' => $from_type, 'capital_type' => $capital_type, ]; } $refund_user_coupon_id_arr = []; if ($from_type == UserWalletEnum::REFUND) { //退款 $order_detail_id_diff = array_diff($order_detail_id_all, $order_detail_ids); if (empty($order_detail_id_diff)) $order_detail_id_diff = $order_detail_id_all; $order_marketing_list_diff = OrderMarketing::lists([ 'where' => [['order_detail_id' => $order_detail_id_diff], ['status' => StatusEnum::ENABLED]], 'select' => 'user_coupon_id' ]); $user_coupon_id_arr_diff = []; foreach ($order_marketing_list_diff as $value) { $user_coupon_id = json_decode($value['user_coupon_id'], true); if (!empty($user_coupon_id)) $user_coupon_id_arr_diff = array_merge($user_coupon_id_arr_diff, array_keys($user_coupon_id)); } foreach ($order_marketing_list as $item) { $user_coupon_id_arr = json_decode($item['user_coupon_id'], true); if (!empty($user_coupon_id_arr)) { foreach ($user_coupon_id_arr as $user_coupon_id => $money_) { if (in_array($user_coupon_id, $user_coupon_id_arr_diff)) $refund_user_coupon_id_arr[] = $user_coupon_id; } } } } else { //订单关闭 $refund_user_coupon_id_arr = []; foreach ($order_marketing_list as $item) { $user_coupon_id = json_decode($item['user_coupon_id'], true); if (!empty($user_coupon_id)) $refund_user_coupon_id_arr = array_merge($refund_user_coupon_id_arr, array_keys($user_coupon_id)); } } if (!empty($refund_user_coupon_id_arr)) { AddonsCouponUserRelate::updateAll(['is_use' => 0], ['id' => $refund_user_coupon_id_arr]); AddonsCouponUseLog::updateAll(['status' => StatusEnum::DELETE], ['order_id' => $order_id, 'user_coupon_id' => $refund_user_coupon_id_arr]); } // 返还货币,TODO 货币功能还未完成 // 返还对应的资产 if (empty($insert_wallet)) return true; $res = UserWalletService::batchHandleByQueue($insert_wallet); if (empty($res)) throw new Exception(UserWalletService::getStaticError()); return true; } catch (\Exception $e) { self::setStaticError($e->getMessage()); return false; } } public static function handleGiveScore($mall_id, $marketing, $profit, $num = 1, $mch_id = 0) { return $marketing; /* try{ if($profit <= 0) throw new \Exception('profit must be greater than 0'); $setting = \Yii::$app->services->setting->mall->get($mall_id, 'score'); if(empty(($setting['is_enable_score_giving'] ?? 0))) throw new \Exception('is_enable_score_giving is 0'); $score_giving_goods_type = (array)json_decode(($setting['score_giving_goods_type'] ?? ''), true); if($mch_id){ if(!in_array(1, $score_giving_goods_type)) throw new \Exception('1 is not in the scoregiving_goods_type'); }else{ if(!in_array(0, $score_giving_goods_type)) throw new \Exception('0 is not in the scoregiving_goods_type'); } $score_giving_num = (float)($setting['score_giving_num'] ?? 0); if($score_giving_num <= 0) throw new \Exception('score_giving_num must be greater than 0'); $score_name = $setting['score_name'] ?? '积分'; $score_giving_type = $setting['score_giving_type'] ?? 0; $score_giving_settlement_method = $setting['score_giving_settlement_method'] ?? 0; $give_num = 0; if($score_giving_type == 1){ $give_num = bcmul($score_giving_num, $num, 2); }else{ $give_num = bcmul($profit * $num, $score_giving_num / 100, 2); } if(!isset($marketing['give_currency'])) { $marketing['give_currency'] = []; }else{ $marketing['give_currency'] = json_decode($marketing['give_currency'], true); } if(!isset($marketing['give_currency']['score'])) { $marketing['give_currency']['score'] = ['name'=>$score_name, 'score'=>$give_num, 'score_giving_settlement_method' => $score_giving_settlement_method]; }else{ $marketing['give_currency']['score']['score'] += $give_num; } $marketing['give_currency'] = json_encode($marketing['give_currency']); }catch(\Exception $e){ }finally{ return $marketing; } */ } }