| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- namespace common\models\order;
- use addons\Boss\backend\controllers\SettingController;
- use addons\Coupon\common\models\AddonsCouponUseLog;
- use addons\Coupon\common\models\AddonsCouponUserRelate;
- use addons\GroupBuy\common\models\GroupBuyOrder;
- use common\enums\AddonsEnum;
- use common\enums\StatusEnum;
- use common\enums\UserWalletEnum;
- use common\models\mall\MallAddons;
- use services\common\UserWalletService;
- use Yii;
- use yii\db\Exception;
- /**
- * This is the model class for table "{{%order_marketing}}".
- *
- *
- * @property int $id 主键
- * @property int $order_detail_id mall_id
- * @property float $give_score 赠送积分
- * @property float $give_balance 赠送余额
- * @property string|null $give_currency 赠送货币(json字符串保存)
- * @property float $deduct_score 减扣积分
- * @property string|null $deduct_currency 减扣货币(json字符串保存)
- * @property float $pay_money 商品实际付款金额,单位:元
- * @property float $full_reduce_money 满减金额,单位:元
- * @property float $coupon_discount_money 优惠券优惠金额,单位:元
- * @property float $agent_deduct 经销商内购商品抵扣
- * @property string $agent_extra 经销商内购商品抵扣额外信息
- * @property int $user_coupon_id 用户优惠券id
- * @property string $user_card_id 亲友卡抵扣额外信息
- * @property int $member_price_deduct 会员价优惠金额
- * @property int $give_balance_settlement 赠送余额的结算方式:0:订单完成,1:支付完成
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at 创建时间
- * @property int $updated_at 修改时间
- * @property int $give_score_settlement 赠送积分的结算方式:0订单完成;1支付完成
- * @property float $give_digital 赠送数字币
- * @property int $give_digital_settlement 赠送数字币的结算方式:0订单完成;1支付完成
- */
- class OrderMarketing extends \common\models\base\BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%order_marketing}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['order_detail_id', 'status', 'created_at', 'updated_at','give_score_settlement','give_balance_settlement'], 'integer'],
- [['give_score', 'give_balance', 'deduct_score', 'pay_money', 'full_reduce_money', 'member_price_deduct', 'coupon_discount_money', 'agent_deduct'], 'number'],
- [['give_currency', 'deduct_currency', 'user_coupon_id', 'agent_extra','user_card_id'], 'string'],
- [['give_digital'],'number'],
- [['give_digital_settlement'],'integer']
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => '主键',
- '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;
- }
- */
- }
- }
|