| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models\pt;
- use app\constants\OptionSetting;
- use app\models\AccountLog;
- use app\models\Cat;
- use app\models\DeliveryRules;
- use app\models\Goods;
- use app\models\GoodsCat;
- use app\models\IntegralAppreciationUser;
- use app\models\IntegralAppreciationUserIntegralLog;
- use app\models\Option;
- use app\models\Order;
- use app\models\PtActivity;
- use app\models\PtActivityGoods;
- use app\models\PtActivityGoodsCat;
- use app\models\PtActivityOrder;
- use app\models\PtActivityOrderDetail;
- use app\models\SaasUser;
- use app\models\SeckillActivity;
- use app\models\SeckillActivityGoods;
- use app\models\SeckillActivityOrderLog;
- use app\models\Store;
- use app\models\User;
- use app\modules\client\models\v1\pt\order\OrderForm;
- use app\utils\OrderNo;
- use app\utils\Refund;
- use app\utils\Wechat\WechatNewPay;
- use app\utils\Wechat\WechatPay;
- use yii\base\Model;
- class PtActivityOrderForm extends Model
- {
- public $order_id;
- //拼团id
- public $pt_number;
- //下单用户
- public $name;
- //拼团状态
- public $status;
- public $order_no;
- public $start_time;
- public $end_time;
- public $is_platform;
- public function rules()
- {
- return [
- [['order_id', 'pt_number', 'status', 'is_platform'], 'integer'],
- [['name', 'order_no', 'start_time', 'end_time'], 'string'],
- ];
- }
- public function search ()
- {
- try {
- $query = PtActivityOrder::find()->alias('o')->where(['o.is_pay' => 1, 'o.pt_number' => 0])
- ->leftJoin(['od' => PtActivityOrderDetail::tableName()], 'od.order_id = o.id')
- ->leftJoin(['a' => PtActivity::tableName()], 'od.activity_id = a.id')
- ->leftJoin(['s' => Store::tableName()], 'o.store_id = s.id');
- if ($this->pt_number) {
- $query->andWhere(['o.id' => $this->pt_number]);
- }
- if ($this->name) {
- $query->andWhere(['LIKE', 'od.goods_name', $this->name]);
- }
- if ($this->is_platform) {
- $query->andWhere(['a.is_platform' => 1]);
- } else {
- $query->andWhere(['o.store_id' => get_store_id(), 'a.is_platform' => 0]);
- }
- switch ((int)$this->status) {
- case 1:
- $query->andWhere(['o.is_pt_finish' => 0]);
- break;
- case 2:
- $query->andWhere(['o.is_pt_finish' => 1]);
- break;
- case 3:
- $query->andWhere(['o.is_pt_finish' => 2]);
- break;
- }
- $query->select('od.goods_name, od.pic, o.pay_time, o.id, o.pt_number, o.is_pt_finish, od.activity_id, o.store_id');
- $query->orderBy('o.pay_time desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$list) {
- $store = Store::findOne($list['store_id']);
- $list['store_name'] = $store->name;
- $list['logo'] = $store->logo ?: Option::get(OptionSetting::STORE_LOGO, $list['store_id'], 'store', '');
- $list['store_id'] = $store->id;
- $pt_activity = PtActivity::findOne($list['activity_id']);
- $list['party_size'] = $pt_activity->party_size;
- $list['join_num'] = PtActivityOrder::find()->where(['is_pay' => 1])
- ->andWhere(['OR', ['pt_number' => $list['id']], ['id' => $list['id']]])->select('id')->count();
- $list['error_order_num'] = PtActivityOrder::find()->where(['is_pay' => 1, 'is_winner' => 0, 'is_pay_loser' => 0])
- ->andWhere(['<>', 'is_pt_finish', 0])
- ->andWhere(['OR', ['pt_number' => $list['id']], ['id' => $list['id']]])->select('id')->count();
- $list['status'] = ((int)$list['is_pt_finish'] + 1);
- $list['pt_number'] = $list['id'];
- $list['pay_time'] = date("Y-m-d H:i:s", $list['pay_time']);
- $list['activity_info'] = $pt_activity;
- if($pt_activity->party_type == 1){
- $list['party_size'] = $pt_activity->party_goods_count;
- $pt_number = $list['pt_number'];
- $goodsCountQuery = PtActivityOrderDetail::find()->alias('pod')
- ->leftJoin(['po' => PtActivityOrder::tableName()], 'pod.order_id = po.id')
- ->where(['po.is_pay' => 1, 'pod.is_delete' => 0])
- ->andWhere(['OR' , ['po.id' => $pt_number], ['po.pt_number' => $pt_number]]);
- $goodsCount = $goodsCountQuery->sum('pod.num');
- $list['join_num'] = $goodsCount;
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //拼团信息
- public function getPtInfo() {
- try {
- $pt_number = $this->pt_number;
- $query = PtActivityOrder::find()->alias('o')->where(['o.is_pay' => 1])
- ->andWhere(['OR', ['o.pt_number' => $pt_number], ['o.id' => $pt_number]])
- ->leftJoin(['u' => User::tableName()], 'u.id = o.user_id')
- ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding');
- if ($this->order_no) {
- $query->andWhere(['LIKE', 'o.order_no', $this->order_no]);
- }
- if ($this->name) {
- $query->andWhere(['LIKE', 'su.name', $this->name]);
- }
- if ($this->start_time) {
- $query->andWhere(['>=', 'o.pay_time', $this->start_time]);
- }
- if ($this->end_time) {
- $query->andWhere(['<=', 'o.pay_time', $this->end_time]);
- }
- $query->select('o.id, o.order_no, su.name, su.avatar, o.is_pt_finish, o.pay_time, o.is_winner, o.is_pay_loser');
- $query->orderBy('o.pay_time desc');
- $pagination = pagination_make($query);
- foreach ($pagination['list'] as &$list) {
- $list['error_order'] = 0;
- if ((int)$list['is_pt_finish'] !== 0 && (int)$list['is_winner'] === 0 && (int)$list['is_pay_loser'] !== 1) {
- $list['error_order'] = 1;
- }
- $order_detail = PtActivityOrderDetail::findOne(['order_id' => $list['id']]);
- $list['goods_name'] = $order_detail->goods_name;
- $list['total_price'] = $order_detail->total_price;
- $list['num'] = $order_detail->num;
- $list['pic'] = $order_detail->pic;
- $list['attr'] = json_decode($order_detail->attr, true);
- $list['pay_time'] = date('Y-m-d H:i:s', $list['pay_time']);
- $list['status'] = 0;
- $list['is_pay_loser'] *= 1;
- if ((int)$list['is_pt_finish'] === 0) { //进行中
- $list['status'] = 1;
- }
- if ((int)$list['is_pt_finish'] === 1 && (int)$list['is_winner'] === 1) { //已成功
- $list['status'] = 2;
- }
- if (((int)$list['is_pt_finish'] === 1 && (int)$list['is_winner'] === 0) || (int)$list['is_pt_finish'] === 2) { //已失败
- $list['status'] = 3;
- }
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $pagination['list'],
- 'pageNo' => $pagination['pageNo'],
- 'totalCount' => $pagination['totalCount'],
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //拼团失败成员退款
- public function loserHandle($order_id, $head_is_free = 0) {
- $t = \Yii::$app->db->beginTransaction();
- try {
- // $order = PtActivityOrder::findOne($order_id);
- // TODO 这块注释掉 说是让所有拼团完成的订单过完售后期后才开始给团长退款 设置了团长免单后这里就不再进行退款了 过完售后才开始传递head_is_free = 1
- // $head_is_free = 0;
- // $order_detail = PtActivityOrderDetail::find()->where(['order_id' => $order_id])->select('goods_id, activity_id')->asArray()->one();
- // if (!empty($order_detail)) {
- // $pt_activity_goods = PtActivityGoods::findOne([
- // 'activity_id' => $order_detail['activity_id'],
- // 'goods_id' => $order_detail['goods_id'],
- // 'is_delete' => 0
- // ]);
- // if ($pt_activity_goods) {
- // $pt_activity = PtActivity::findOne(['id' => $pt_activity_goods->activity_id, 'is_delete' => 0]);
- // if ($pt_activity) {
- // $head_is_free = (int)$pt_activity->head_is_free;
- // }
- // }
- //
- // }
- $pt_order = PtActivityOrder::find()->where(['trade_status' => 0, 'is_pay' => 1, 'is_winner' => 0, 'is_pay_loser' => 0])
- ->andWhere(['OR', ['id' => $order_id], ['pt_number' => $order_id]])
- ->andWhere(['<>', 'is_pt_finish', 0])
- ->asArray()->all();
- if ($head_is_free) {
- $pt_order_id = [];
- if (!empty($pt_order)) {
- $pt_order_id = array_column($pt_order, 'id');
- }
- //如果团长已成功,则开始免单
- if (!in_array($order_id, $pt_order_id)) {
- $head_pt_order = PtActivityOrder::find()->where(['id' => $order_id])
- ->andWhere(['<>', 'is_pt_finish', 0])->asArray()->one();
- $pt_order = array_merge($pt_order, [$head_pt_order]);
- }
- }
- if (!empty($pt_order)) {
- foreach ($pt_order as $item) {
- $order = PtActivityOrder::findOne($item['id']);
- //余额支付
- if ((int)$order->pay_type === 3) {
- $user = User::findOne(['id' => $item['user_id']]);
- if ($user) {
- // $user->money = ((float)$user->money + (float)$item['pay_price']);
- // $user->save();
- $str = "拼团未成功退款,订单号为:{$item['order_no']}。";
- if ($order->is_winner) {
- $str = "拼团,团长免单退款,订单号为:{$item['order_no']}。";
- }
- $res = AccountLog::saveLog($user->id, floatval($item['pay_price']), AccountLog::TYPE_BALANCE, AccountLog::LOG_TYPE_INCOME, 3, $item['id'], $str);
- if (!$res) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => '支付失败'
- ];
- }
- }
- }
- //微信支付
- if (in_array((int)$order->pay_type, [1, 4])) {
- $order_refund_no = 'RP' . substr($order->order_no, 2);
- $order->order_refund_no = $order_refund_no;
- $res = Refund::refund($order, OrderNo::ORDER_MALL, $order_refund_no, $order->pay_price);
- if ($res !== true) {
- return $res;
- }
- }
- /*if (!intval($order->is_winner)) {
- //增值积分订单取消
- $order_id = $order->id;
- //增值积分有订单取消
- $integralAppreciationUserIntegralLog = IntegralAppreciationUserIntegralLog::findOne([
- 'order_id' => $order_id,
- 'order_type' => IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT,
- 'type' => IntegralAppreciationUserIntegralLog::TYPE_EXPEND,
- 'source_type' => IntegralAppreciationUserIntegralLog::SOURCE_TYPE_BUY_GOODS
- ]);
- if ($integralAppreciationUserIntegralLog) {
- $integralAppreciationUser = IntegralAppreciationUser::findOne($integralAppreciationUserIntegralLog->integral_user_id);
- $integralAppreciationUser->integral = bcadd($integralAppreciationUser->integral, $integralAppreciationUserIntegralLog->amount, 2);
- $integralAppreciationUser->save();
- IntegralAppreciationUserIntegralLog::saveIntegralLog(
- $integralAppreciationUser->id,
- $integralAppreciationUserIntegralLog->amount,
- IntegralAppreciationUserIntegralLog::TYPE_INCOME,
- IntegralAppreciationUserIntegralLog::SOURCE_TYPE_ORDER_CANCEL,
- "订单取消 订单号{$this->order_no}",
- $order_id,
- IntegralAppreciationUserIntegralLog::ORDER_TYPE_PT);
- }
- }*/
- $order->is_pay_loser = 1;
- $order->save();
- }
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '支付成功'
- ];
- } catch (\Exception $e) {
- $t->rollBack();
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|