| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- use app\models\ReOrder;
- use app\models\ReOrderRefundMoney;
- use yii\base\Model;
- use app\models\User;
- use app\models\AccountLog;
- use app\utils\OrderNo;
- use app\utils\Refund;
- use app\utils\Notice\NoticeSend;
- use app\models\Option;
- use app\constants\OptionSetting;
- class ReOrderForm extends Model
- {
- public $export;
- public $user_id;
- public $order_id;
- public $store_id;
- public $name;
- public $phone;
- public $status;
- public $audit_status;
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['store_id'], 'integer'],
- [['name', 'phone', 'status', 'audit_status', 'user_id', 'order_id'], 'safe'],
- [['export'], 'safe'],
- ];
- }
- public function search() {
- $query = ReOrder::find()->alias('ro')->where([
- 'ro.is_pay' => 1,
- 'ro.is_delete' => 0,
- ])->leftJoin(['u' => User::tableName()], 'ro.user_id = u.id')->orderBy('ro.id DESC');
- if($this->store_id > 0){
- $query->andWhere(['ro.store_id' => $this->store_id]);
- }
- if($this->user_id > 0){
- $query->andWhere(['ro.user_id' => $this->user_id]);
- }
- if($this->name){
- $query->andWhere(['like', 'u.nickname', $this->name]);
- }
- if($this->phone){
- $query->andWhere(['like', 'u.binding', $this->phone]);
- }
- $query->select('ro.*, u.avatar_url, u.binding, u.nickname');
- $data = pagination_make($query);
- $data['data'] = $data['list'];
- unset($data['list']);
- foreach ($data['data'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- if($this->export){
- return $this->export($data['data']);
- }
- return [
- 'code' => 0,
- 'data' => $data,
- 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- private function export($list) {
- $rows = [[
- 'ID',
- '会员信息',
- '支付金额(元)',
- '赠送金额(元)',
- '订单号',
- '时间',
- ]];
- foreach($list as $item){
- $r = [
- $item['id'],
- $item['nickname'],
- $item['pay_price'],
- $item['send_price'],
- $item['order_no'],
- $item['created_at'],
- ];
- $rows[] = $r;
- }
- $writer = \Spatie\SimpleExcel\SimpleExcelWriter::streamDownload(time() . '.xlsx')->noHeaderRow()
- ->addRows($rows)->toBrowser();
- }
-
- private function getReOrderRefundNo()
- {
- $order_refund_no = null;
- while (true) {
- $order_refund_no = 'RM'.date('YmdHis') . mt_rand(100000, 999999);
- $exist_order_refund_no = ReOrderRefundMoney::find()->where(['order_refund_no' => $order_refund_no])->exists();
- if (!$exist_order_refund_no) {
- break;
- }
- }
- return $order_refund_no;
- }
- //充值订单退款列表
- public function refundMoneyList() {
- $query = ReOrderRefundMoney::find()->alias('rm')->where([
- 'rm.is_delete' => 0,
- ]);
- $query->leftJoin(['ro' => ReOrder::tableName()], 'ro.id = rm.order_id');
- $query->leftJoin(['u' => User::tableName()], 'rm.user_id = u.id')->orderBy('rm.id DESC');
- if($this->user_id > 0){
- $query->andWhere(['rm.user_id' => $this->user_id]);
- }
- if($this->order_id > 0){
- $query->andWhere(['rm.order_id' => $this->order_id]);
- }
- if($this->store_id > 0){
- $query->andWhere(['rm.store_id' => $this->store_id]);
- }
- if($this->name){
- $query->andWhere(['like', 'u.nickname', $this->name]);
- }
- if($this->phone){
- $query->andWhere(['like', 'u.binding', $this->phone]);
- }
- if($this->status > -1){
- $query->andWhere(['status' => $this->status]);
- }
- if($this->audit_status > -1){
- $query->andWhere(['audit_status' => $this->audit_status]);
- }
- $query->select('rm.*, ro.pay_price, ro.send_price, ro.send_integral, ro.pay_time, ro.order_no, u.avatar_url, u.binding, u.nickname');
- $data = pagination_make($query);
- $data['data'] = $data['list'];
- unset($data['list']);
- return [
- 'code' => 0,
- 'data' => $data,
- // 'sql' => $query->createCommand()->getRawSql(),
- ];
- }
- //充值订单申请退款
- public function refundMoneyApply($user_id = 0, $order_id = 0, $desc_user = '') {
- try{
- $key = 'refundMoneyApply_' . $order_id;
- $cache = cache()->get($key);
- if($cache){
- throw new \Exception('操作失败,操作太频繁,请稍后重试。');
- }
- cache()->set($key, 1, 30);
- $where = [
- 'user_id' => $user_id,
- 'order_id' => $order_id,
- 'is_delete' => 0,
- 'store_id' => $this->store_id,
- ];
- $has = ReOrderRefundMoney::findOne($where);
- if ($has) {
- throw new \Exception('操作失败,该订单已存在申请退款记录。');
- }
- $order = ReOrder::findOne($order_id);
- if (!$order) {
- throw new \Exception('操作失败,订单不存在。');
- }
- if($order->is_pay != 1){
- throw new \Exception('操作失败,订单还未支付。');
- }
- $time = time();
- $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, $this->store_id, 0)['value'];
- $sale_time = $time - ($after_sale_time * 86400);
- if($order->pay_time < $sale_time){
- throw new \Exception('操作失败,订单已过售后期。');
- }
- $refund = new ReOrderRefundMoney();
- $refund->store_id = $order->store_id;
- $refund->user_id = $order->user_id;
- $refund->order_id = $order->id;
- $refund->refund_price = $order->pay_price;
- $refund->sub_balance = $order->pay_price + $order->send_price;
- $refund->sub_integral = intval($order->send_integral);
- $refund->order_refund_no = $this->getReOrderRefundNo();
- $refund->desc_user = $desc_user;
- if (!$refund->save()) {
- throw new \Exception('操作失败,' . array_shift($refund->getFirstErrors()));
- }
- return [
- 'code' => 0,
- 'msg' => '操作成功,请等待审核'
- ];
- } catch (\Exception $ex) {
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
-
- //充值订单退款审核
- public function refundMoneyAudit($audit_status = 0, $refund_id = 0, $desc_admin = '', $refund_price = 0.01, $sub_balance = 0.01, $sub_integral = 0) {
- $t = \Yii::$app->db->beginTransaction();
- try{
- $refund = ReOrderRefundMoney::findOne($refund_id);
- if($refund->audit_status != 0){
- throw new \Exception('操作失败,记录状态错误。');
- }
- $refund->desc_admin = $desc_admin;
- if($audit_status == ReOrderRefundMoney::AUDIT_STATUS_REJECT){
- $refund->audit_status = ReOrderRefundMoney::AUDIT_STATUS_REJECT;
- if (!$refund->save()) {
- throw new \Exception('操作失败,审核错误。' . array_shift($refund->getFirstErrors()));
- }
- $t->commit();
- return [
- 'code' => 0,
- 'msg' => '拒绝成功',
- ];
- }
- $refund->audit_status = ReOrderRefundMoney::AUDIT_STATUS_OK;
- if($refund_price <= 0){
- throw new \Exception('操作失败,退款金额不能为0。');
- }
- if($sub_balance <= 0){
- throw new \Exception('操作失败,扣减余额不能为0。');
- }
- $where = [
- 'id' => $refund->order_id,
- 'is_delete' => 0,
- 'store_id' => $this->store_id,
- ];
- $order = ReOrder::findOne($where);
- if($refund_price > $order->pay_price){
- throw new \Exception('操作失败,退款金额超出支付金额。');
- }
- //扣余额
- $user = User::findOne(['id' => $order->user_id]);
- if($sub_balance > $user->money){
- throw new \Exception('操作失败,用户余额不足。');
- }
- if($sub_integral > $user->integral){
- throw new \Exception('操作失败,用户积分不足。');
- }
- if($sub_balance > 0){
- $type = AccountLog::TYPE_BALANCE;
- $log_type = AccountLog::LOG_TYPE_EXPEND;
- $order_type = AccountLog::TYPE_RECHARGE_REFUND_ORDER;
- $desc = '充值订单退款,充值订单号:' . $order->order_no;
- $saveLog = AccountLog::saveLog($user->id, floatval($sub_balance), $type, $log_type, $order_type, $order->id, $desc);
- if(!$saveLog){
- throw new \Exception('操作失败,余额修改失败。');
- }
- }
- //扣积分
- if($sub_integral > 0){
- $type = AccountLog::TYPE_INTEGRAL;
- $log_type = AccountLog::LOG_TYPE_EXPEND;
- $order_type = AccountLog::TYPE_RECHARGE_REFUND_ORDER;
- $desc = '充值订单退款,充值订单号:' . $order->order_no;
- $saveLog = AccountLog::saveLog($user->id, $sub_integral, $type, $log_type, $order_type, $order->id, $desc);
- if(!$saveLog){
- throw new \Exception('操作失败,积分修改失败。');
- }
- }
- $refund->refund_price = $refund_price;
- $refund->sub_balance = $sub_balance;
- $refund->sub_integral = $sub_integral;
- $refund->status = 1;
- if (!$refund->save()) {
- throw new \Exception('操作失败,审核错误。' . array_shift($refund->getFirstErrors()));
- }
- if (in_array($order->pay_type, [1, 4])) {
- $refund_res = Refund::refund($order, OrderNo::ORDER_MALL, $refund->order_refund_no, $refund->refund_price);
- if ($refund_res !== true) {
- \Yii::error([__METHOD__, $refund_res]);
- $refund->status = 0;
- $refund->status_desc = json_encode($refund_res, JSON_UNESCAPED_UNICODE);
- if (!$refund->save()) {
- throw new \Exception('操作失败,' . array_shift($refund->getFirstErrors()));
- }
- }
- }
- $t->commit();
- // $order->mobile = $user->binding;
- // NoticeSend::OrderRefund($order->user_id, $order->mobile, $order->order_no, $refund->refund_price, '充值订单退款');
- return [
- 'code' => 0,
- 'msg' => '处理成功。',
- ];
- } catch (\Exception $ex) {
- $t->rollback();
- return [
- 'code' => 1,
- 'msg' => $ex->getMessage(),
- ];
- }
- }
- }
|