| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\plugins\scanCodePay\models;
- use app\constants\OptionSetting;
- use app\models\common\events\OrderEvent;
- use app\models\AccountLog;
- use app\models\CardSend;
- use app\models\common\Bind;
- use app\models\common\ShareMoney;
- use app\models\CouponPaySend;
- use app\models\Goods;
- use app\models\Option;
- use app\models\OrderMessage;
- use app\models\Share;
- use app\models\Shop;
- use app\models\ShopShare;
- use app\models\User;
- use app\models\VerifyCard;
- use app\models\VerifyCardLog;
- use app\models\VerifyCardSale;
- use app\modules\client\models\v1\ShareMoneyForm;
- use app\utils\Notice\NoticeSend;
- use app\utils\PrintOrder;
- use yii\base\BaseObject;
- use yii\base\Model;
- use yii\helpers\Json;
- class OrderComplete extends Model
- {
- public $order;
- public $store_id = 1;
- public $order_id;
- public $order_type;
- public $form_id;
- public $order_refund_no;
- const EVENT_ORDER_FOLLOW = 'order_follow_up';
- const EVENT_ORDER_REFUND = 'order_refund';
- // 支付完成之后,相关的操作
- public function notify(OrderEvent $event = null)
- {
- $this->store_id = $event ? $event->store_id : $this->store_id;
- $this->order_id = $event ? $event->order_id : $this->order_id;
- $this->order_type = $event ? $event->order_type : $this->order_type;
- try {
- $this->OrderNotify($event);
- $is_print = 1;
- $order = $this->order;
- $this->shareShop($order, $event);
- //发放核销卡
- $this->sendVerifyCard($order->id, $order->user_id, $this->order_type);
- // 后台订单提醒
- OrderMessage::set($order->id, $order->store_id, $this->order_type, 0);
- // 订单打印
- if ($is_print == 1) {
- // 订单打印
- if ((int)$order->md_id === -1 || (int)$order->md_id === 0 || !isset($order->md_id)) {
- $order->md_id = 0;
- }
- $printer_order = new PrintOrder($order->store_id, $order->id, 'pay', $this->order_type,
- $order->md_id, 0, $order['mch_id']);
- $res = $printer_order->print_order();
- }
- } catch (\Exception $e) {
- \Yii::error('line->>>' . $e->getLine());
- \Yii::error($e->getMessage());
- \Yii::error('订单支付完成后操作异常');
- }
- }
- /**
- * 发放核销卡
- */
- private function sendVerifyCard($order_id, $user_id, $order_type) {
- $goods_id = OrderDetail::find()->select('goods_id')->where(['order_id' => $order_id,'is_delete' => 0])->asArray()->all();
- foreach ($goods_id as $k => $v) {
- $verify_card_info = Goods::find()->select('id, verify_card_id')->where(['id' => $v['goods_id'], 'is_delete' => 0])->asArray()->one();
- $this->startSendVerifyCard($verify_card_info['verify_card_id'],$verify_card_info['id'],$user_id,$order_type);
- }
- }
- /**
- * 核销卡发放
- */
- private function startSendVerifyCard($verify_card_id, $goods_id, $user_id, $order_type){
- if(!empty($verify_card_id)){
- $verify_card_id_arr = explode(',', $verify_card_id);
- foreach($verify_card_id_arr as $value){
- $verify_form = new VerifyCardSale();
- $verify_form->store_id = $this->store_id;
- $verify_form->user_id = $user_id;
- $verify_form->verify_card_id = $value;
- //查询核销卡使用总次数
- $left_info = VerifyCard::find()->select('total_num,expire_day, type')->where([
- 'id' => $value,
- 'is_delete' => 0
- ])->asArray()->one();
- $verify_form->left_num = $left_info['total_num'];
- $verify_form->sale_time = time();
- $verify_form->end_time = time() + $left_info['expire_day'] * (3600*24);
- $verify_form->goods_id = $goods_id;
- $verify_form->order_type = $order_type;
- //视频卡改为使用状态,新增领取记录
- if ($left_info->type == 5) {
- $verify_form->status = 1;
- }
- $verify_form->save();
- //视频卡改为使用状态,新增领取记录
- if ($left_info->type == 5) {
- $logModel = new VerifyCardLog();
- $logModel->sale_id = $verify_form->id;
- $logModel->store_id = $this->store_id;
- $logModel->use_time = time();
- $logModel->user_id = $user_id;
- $logModel->type = VerifyCardLog::WRITE_TYPE_EXCHANGE;
- $logModel->describe = '视频卡兑换';
- if (!$logModel->save()) {
- \Yii::error("视频卡兑换失败");
- }
- }
- //核销卡核销数量增加
- $verify_card_form = VerifyCard::findOne([
- 'id' => $value,
- 'is_delete' => 0
- ]);
- $verify_card_form ->use_num += 1;
- $verify_card_form ->save();
- }
- }
- }
- // 自提点分销
- public function shareShop($order) {
- if ($order->is_offline == 1) {
- $shop = Shop::findOne($order->shop_id);
- if ($shop) {
- $user = User::findOne($order->user_id);
- $info = addslashes("会员 ".$user->nickname." 下单,获得奖励");
- $orderDetail = OrderDetail::find()->select(['goods_id','total_price','num'])->where(['order_id' => $order->id])->all();
- $shop_bili = Option::get(OptionSetting::SHOP_RATIO, $order->store_id)['value'];
- $shop_bili_type = Option::get(OptionSetting::SHOP_RATIO_TYPE, $order->store_id)['value'];
- $money = 0;
- foreach ($orderDetail as $v){
- $goods_money = $v['total_price'];
- if($shop_bili_type == 2){
- $money += $v['num']*$shop_bili;
- }else{
- $money += $goods_money*$shop_bili * 0.01;
- }
- }
- if ($money > 0.01) {
- $share_form = new ShopShare();
- $share_form->store_id = $order->store_id;
- $share_form->order_id = $order->id;
- $share_form->shop_id = $shop->id;
- $share_form->amount = $money;
- $share_form->user_id = $order->user_id;
- $share_form->the_desc = $info;
- $share_form->created_at = time();
- $share_form->save();
- }
- }
- }
- }
- // 商城
- private function OrderNotify($event = null)
- {
- $order = $this->order = Order::findOne(['id' => $this->order_id]);
- $user = User::findOne($order->user_id);
- // 首次付款,绑定上下级
- if ($user->parent_id == 0) {
- $form = new Bind();
- $form->user_id = $order->user_id;
- $form->store_id = $this->store_id;
- $form->parent_id = $user->parent_user_id;
- $form->condition = 2;
- $bindForm = $form->save();
- if ($bindForm['code'] == 0) {
- $form = new ShareMoneyForm();
- $form->order = $this->order;
- $form->order_type = $this->order_type;
- $form->setData();
- }
- }
- // 消费满指定金额自动成为分销商
- $this->autoBecomeShare($order->user_id, $order->store_id, 'STORE');
- // 购买指定或任意商品自动成为分销商
- $this->autoBuyGood($order->user_id, $order->store_id, $order->id);
- if (in_array($this->order->pay_type, [1, 3])) {
- // 支付成功赠送优惠券
- $this->paySendCoupon($order->store_id, $order->user_id);
- // 支付成功赠送卡券
- $this->paySendCard($order->store_id, $order->user_id, $order->id);
- }
- // 记录购买记录
- $this->setBuyData($order);
- // 消息通知
- // $goods = Goods::findOne(OrderDetail::findOne(['order_id' => $this->order_id])->goods_id);
- NoticeSend::OrderPay($order->user_id, $order->mobile, $order->order_no, $order->pay_price, '当面付');
- NoticeSend::PlaceOrder($order->order_no, $order->mch_id, $order->store_id, $order);
- return true;
- }
- /**
- * 支付成功送优惠券
- */
- private function paySendCoupon($store_id, $user_id)
- {
- $form = new CouponPaySend();
- $form->store_id = $store_id;
- $form->user_id = $user_id;
- $form->save();
- }
- /**
- * 消费满指定金额自动成为分销商
- * @param $user_id integer 用户id
- */
- private function autoBecomeShare($user_id, $store_id, $type = null)
- {
- $setting = Option::get('share_basic_setting', $store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
- $auto_share_val = floatval($setting['auto_share_val']['value']);
- if ($auto_share_val == 0) {
- return;
- }
- $share = Share::findOne(['user_id' => $user_id, 'is_delete' => 0, 'store_id' => $store_id]);
- if ($share && $share->status == 1) {
- return;
- }
- $consumption_sum = Order::find()->where(['user_id' => $user_id, 'is_delete' => 0, 'is_pay' => 1])->sum('pay_price');
- $consumption_sum = floatval(($consumption_sum ? $consumption_sum : 0));
- if ($consumption_sum < $auto_share_val) {
- return;
- }
- if (!$share || $share->status == 2) {
- $share = new Share();
- $share->user_id = $user_id;
- $share->mobile = '';
- $share->name = '';
- $share->is_delete = 0;
- $share->store_id = $store_id;
- }
- $share->status = 1;
- $share->created_at = time();
- $share->save();
- $user = User::findOne($user_id);
- $user->time = time();
- $user->is_distributor = 1;
- $user->save();
- }
- /**
- * 购买指定商品成为分销商
- */
- public function autoBuyGood($user_id, $store_id, $order_id, $type = 0)
- {
- $setting = Option::get('share_basic_setting', $store_id);
- $setting = $setting ? Json::decode($setting['value']) : [];
- //如果开启无需审核才能开始走下面逻辑
- if (intval($setting['share_condition']['value']) !== 2) {
- return;
- }
- // 购买商城成分销商关闭状态不执行
- if ($setting['share_goods_status']['value'] == 0) {
- return;
- }
- $share = Share::findOne(['user_id' => $user_id, 'is_delete' => 0, 'store_id' => $store_id]);
- if ($share && $share->status == 1) {
- return;
- }
- $goodIds = OrderDetail::find()->where(['order_id' => $order_id])->select('goods_id')->all();
- $sign = false;
- // 购买任意商品
- if ($setting['share_goods_status']['value'] == 1) {
- $sign = true;
- }
- // 购买指定商品自动成为分销商
- if ($setting['share_goods_status']['value'] == 2) {
- foreach ($goodIds as $item) {
- $goods_ids = \explode(',', $setting['share_goods_id']['value']);
- if (in_array($item->goods_id, $goods_ids)) {
- $sign = true;
- break;
- }
- }
- }
- if ($sign) {
- if (!$share || $share->status == 2) {
- $share = new Share();
- $share->user_id = $user_id;
- $share->mobile = '';
- $share->name = '';
- $share->is_delete = 0;
- $share->store_id = $store_id;
- }
- $share->status = 1;
- $share->created_at = time();
- $share->save();
- $user = User::findOne($user_id);
- $user->time = time();
- $user->is_distributor = 1;
- $user->save();
- }
- }
- /**
- * 支付成功送卡券
- */
- private function paySendCard($store_id, $user_id, $order_id)
- {
- $form = new CardSend();
- $form->store_id = $store_id;
- $form->user_id = $user_id;
- $form->order_id = $order_id;
- $form->save();
- }
- // 售后订单申请成功,相关操作
- public function refund(OrderEvent $event = null)
- {
- $this->store_id = $event ? $event->store_id : $this->store_id;
- $this->order_id = $event ? $event->order_id : $this->order_id;
- $this->order_type = $event ? $event->order_type : $this->order_type;
- $this->order_refund_no = $event ? $event->order_refund_no : $this->order_refund_no;
- $this->form_id = $event ? $event->form_id : $this->form_id;
- try {
- $order = Order::findOne(['id' => $this->order_id]);
- NoticeSend::MchCancel($order->order_no, $order->mch_id);
- } catch (\Exception $e) {
- }
- }
- public function setBuyData($order)
- {
- $key = "buy_data";
- // 用户
- $user = User::findOne($order->user_id);
- $goods = OrderDetail::findOne(['order_id' => $order->id]);
- $data = (object)null;
- $data->type = 0;
- $data->store_id = $order->store_id;
- $data->order_no = $order->order_no;
- $data->user = $user->nickname;
- $data->goods = $goods->goods_id;
- $data->address = $goods->goods_name;
- $data->avatar_url = $user->avatar_url;
- $data->time = time();
- $new = json_encode($data);
- $cache = cache();
- $cache->set($key, $new, 300);
- }
- }
|