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(), ]; } } }