0.01,], [['address_id'], 'integer'], [['remark'], 'string'], ]; } public function attributeLabels() { return [ 'refund_price' => '退款金额', 'orderType' => '订单类型', ]; } public function save() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } $query = OrderRefund::find()->with('goods'); $order_refund = $query->where([ 'id' => $this->order_refund_id, 'store_id' => $this->store_id, 'is_delete' => Order::IS_DELETE_FALSE, ])->one(); if (!$order_refund) { return [ 'code' => 1, 'msg' => '售后订单不存在,请刷新页面' ]; } if ($order_refund->status != 0) { return [ 'code' => 1, 'msg' => '售后订单已经处理过了,请刷新页面', 'id' => $order_refund->id, ]; } if ($this->type == 1) { return $this->submit1($order_refund); } if ($this->type == 2) { return $this->submit2($order_refund); } } /** * 平台订单退货退款 * @param $order_refund * @return \app\cyy\ValidationErrorResponse|array */ private function submit1($order_refund) { $order = Order::findOne($order_refund->order_id); if ($this->action == 1) {//同意 if ($this->refund != 1) { //仅同意,还未退款 if ($this->refund_price) { if ($this->refund_price > $order_refund->refund_price) { return [ 'code' => 1, 'msg' => '退款金额不能大于' . $order_refund->refund_price, ]; } $order_refund->refund_price = $this->refund_price; } if (!$this->address_id) { return [ 'code' => 1, 'msg' => '退货地址不能为空', ]; }; $order_refund->address_id = $this->address_id; $order_refund->is_agree = 1; $order_refund->save(); if ($order_refund->save()) { return [ 'code' => 0, 'msg' => '已同意退货。', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } else { //已同意,退款操作 $order_refund->status = 1; $order_refund->response_time = time(); if ($order_refund->refund_price > 0 && $order->pay_type == 1) { $res = Refund::refund($order, OrderNo::ORDER_MALL, $order_refund->order_refund_no, $order_refund->refund_price); if ($res !== true) { return $res; } } // 商城商品总库存恢复 $order_detail_list = OrderDetail::find()->where(['order_id' => $order->id, 'is_delete' => 0])->all(); foreach ($order_detail_list as $order_detail) { $goods = Goods::findOne($order_detail->goods_id); $attr_id_list = []; foreach (json_decode($order_detail->attr) as $item) { array_push($attr_id_list, $item->attr_id); } $goods->numAdd($attr_id_list, $order_detail->num); } $user = User::findOne(['id' => $order->user_id]); $integral = isset($order->integral) ? json_decode($order->integral)->forehead_integral : 0; if ($integral > 0) { $user->integral += $integral; $register = new Register(); $register->store_id = $this->store_id; $register->user_id = $user->id; $register->register_time = '..'; $register->created_at = time(); $register->continuation = 0; $register->integral = $integral; $register->order_id = $order->id; $register->type = 9; $register->save(); } if ($order_refund->refund_price > 0 && $order->pay_type == 3) { $before = $user->money; $user->money += floatval($order_refund->refund_price); $log = new AccountLog(); $log->store_id = get_store_id(); $log->user_id = $user->id; $log->type = AccountLog::TYPE_BALANCE; $log->amount = $order->pay_price; $log->log_type = AccountLog::LOG_TYPE_INCOME; $log->desc = "商城售后订单退款:退款订单号:{$order_refund->order_refund_no}"; $log->before = $before; $log->after = $user->money; $log->operator = ''; $log->operator_id = 0; $log->operator_type = AccountLog::TYPE_OPERATOR_NORMAL; $log->created_at = time(); $log->order_id = $order->id; $log->order_type = AccountLog::TYPE_PLATFORM_REFUND_ORDER; $log->save(); if (!$user->save()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } } if ($order_refund->save()) { // TODO: 退款成功消息 $goods = Goods::findOne(OrderDetail::findOne(['order_id' => $order->id])->goods_id); NoticeSend::OrderRefund($order->user_id, $order->mobile, $order->order_no, $order_refund->refund_price, $goods->name); return [ 'code' => 0, 'msg' => '处理成功,已完成退款退货。', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } } if ($this->action == 2) {//拒绝 $order_refund->status = 3; $order_refund->refuse_desc = $this->remark ? $this->remark : '卖家拒绝了您的退货请求'; $order_refund->response_time = time(); if ($order_refund->save()) { return [ 'code' => 0, 'msg' => '处理成功,已拒绝该退货退款订单。', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } } /** * 统一处理换货 * @param $order_refund * @return \app\cyy\ValidationErrorResponse|array */ private function submit2($order_refund) { if ($this->action == 1) {//同意 $order_refund->status = 2; if (!$this->address_id) { return [ 'code' => 1, 'msg' => '退货地址不能为空', ]; }; $order_refund->address_id = $this->address_id; $order_refund->response_time = time(); if ($order_refund->save()) { // TODO: 消息 return [ 'code' => 0, 'msg' => '处理成功,已同意换货。', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } if ($this->action == 2) {//拒绝 $order_refund->status = 3; $order_refund->refuse_desc = $this->remark ? $this->remark : '卖家已拒绝您的换货请求'; $order_refund->response_time = time(); if ($order_refund->save()) { return [ 'code' => 0, 'msg' => '处理成功,已拒绝换货请求。', ]; } return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } } }