// +---------------------------------------------------------------------- declare (strict_types = 1); namespace app\store\controller; use app\store\model\RefundCompensate as RefundCompensateModel; use app\store\model\Order as OrderModel; use app\common\model\OrderGoods as OrderGoodsModel; use app\common\library\helper; use app\store\model\OrderRefund as OrderRefundModel; /** * 售后补偿单管理 * Class Refund * @package app\store\controller\order */ class RefundCompensate extends Controller { /** * 售后单补偿单列表 * @return array */ public function list() { $model = new RefundCompensateModel; $param = $this->request->param(); $list = $model->getList($param); return $this->renderSuccess(compact('list')); } /** * 售后单补偿单列表 * @return array */ public function financelist() { $model = new RefundCompensateModel; $param = $this->request->param(); $list = $model->getFinanceList($param); return $this->renderSuccess(compact('list')); } /** * 编辑 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function edit() { $model = new RefundCompensateModel; $data = $this->postForm(); $id = $data['id']; $order_goods_id = $data['order_goods_id']; if(empty($order_goods_id)){ return $this->renderError("请选择退款商品"); } $order_good = OrderGoodsModel::find($order_goods_id); if(empty($order_good)){ return $this->renderError("找不到订单商品"); } //判断价格 $refundMoney = helper::bcsub($order_good['total_pay_price'] + $order_good['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额 if($data['refund_money']>$refundMoney||$data['refund_money']<0){ //比较价格 return $this->renderError('请调整补偿金额后重新提交'); } $pass_refund_time = false; $refund_time = $order_good['refund_time']; if($refund_time>0&&time()>$refund_time){ $pass_refund_time = true; } if(!$pass_refund_time){ return $this->renderError('此商品未过售后期'); } $refund = OrderRefundModel::where(['order_goods_id'=>$data['order_goods_id']])->where('status','in',[0,20])->find(); if($refund){ return $this->renderError('已存在相同售后单'); } $one = $model->where("order_goods_id",$data['order_goods_id'])->where("order_id",$data['order_id'])->where('status','<>',40)->where('id',"<>",$id)->find(); if($one){ return $this->renderError("同一订单同一商品仅允许完成一笔售后小额打款"); } $model = $model->where('id',$id)->find(); if($model['audit_status_zg']==10){ return $this->renderError('主管同意不能修改了'); } if($model['finance_refund']==10){ return $this->renderError('财务已退款不能修改了'); } $data['audit_status_zg'] =0; if ($model->edit($data)) { return $this->renderSuccess('售后单修改成功,等待主管审核'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 添加 * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function add() { $model = new RefundCompensateModel; $data = $this->postForm(); $order_goods_id = $data['order_goods_id']; if(empty($order_goods_id)){ return $this->renderError("请选择退款商品"); } $order_good = OrderGoodsModel::find($order_goods_id); if(empty($order_good)){ return $this->renderError("找不到订单商品"); } //判断价格 $refundMoney = helper::bcsub($order_good['total_pay_price'] + $order_good['rice_card_money'], 0, 2); // 退款金额 = 实付金额 + 现金卡抵扣金额 if($data['refund_money']>$refundMoney||$data['refund_money']<0){ //比较价格 return $this->renderError('请调整补偿金额后重新提交'); } $pass_refund_time = false; $refund_time = $order_good['refund_time']; if($refund_time>0&&time()>$refund_time){ $pass_refund_time = true; } if(!$pass_refund_time){ return $this->renderError('此商品未过售后期'); } $refund = OrderRefundModel::where(['order_goods_id'=>$data['order_goods_id']])->where('status','in',[0,20])->find(); if($refund){ return $this->renderError('已存在相同售后单'); } $one = $model->where("order_goods_id",$data['order_goods_id'])->where("order_id",$data['order_id'])->where('status','<>',40)->find(); if($one){ return $this->renderError("同一订单同一商品仅允许完成一笔售后小额打款"); } if ($model->add($data)) { return $this->renderSuccess('售后单创建成功,等待主管审核'); } return $this->renderError($model->getError() ?: '添加失败'); } /** * 售后补偿单详情 * @param int $orderRefundId * @return array */ public function detail(int $id) { // 售后单详情 $model = new RefundCompensateModel; if (!$detail = $model->getDetail($id,['orderData'=>['goods']])) { return $this->renderError('未找到该售后单记录'); } $order_no = OrderModel::where('order_id',$detail['order_id'])->value("order_no")??''; if(empty($order_no)){ return $this->renderError('未找到订单号'); } $detail['order_no'] = $order_no; $newrow = []; foreach($detail['orderData']['goods'] as &$row){ if($row['goods_type']==20){ // continue; } $pass_refund_time = false; $refund_time = $row['refund_time']; if($refund_time>0&&time()>$refund_time){ $pass_refund_time = true; } $row['pass_refund_time'] = $pass_refund_time; $refund = OrderRefundModel::where(['order_goods_id'=>$row['order_goods_id']])->find(); $refund_state_text ='-'; $is_refund = false; $is_refund_close = false; if($refund){ $is_refund = true; $refund_state_text = $refund['state_text']; if($refund['status']==40||$refund['status']==30){ $is_refund_close = true; } } $row['is_refund_close'] = $is_refund_close; $row['is_refund'] = $is_refund; $row['refund_state_text'] = $refund_state_text; // $newrow[] = $row; } // $detail['orderData']['goods'] = $newrow; return $this->renderSuccess(compact('detail')); } /** * 主管审核 * @param int $orderRefundId * @return array|bool */ public function auditzg(int $id) { // 售后补偿单详情 $model = RefundCompensateModel::detail($id); if(empty($model)){ return $this->renderError("找不到啊"); } if($model['status'] == 20) { return $this->renderError("订单已完成,请不要操作"); } if($model['finance_refund'] == 10) { return $this->renderError("售后订单已经退款了,请不要操作"); } // 确认审核 $postData = $this->postForm(); unset($postData['id']); $audit_status = $postData['audit_status_zg']; if($audit_status == 10){ $res = '已审核通过'; } if($audit_status == 20){ $res = '已拒绝'; } if ($model->auditzg($postData)) { return $this->renderSuccess($postData,$res); } return $this->renderError($model->getError() ?: '操作失败'); } //关闭售后补偿单 public function close(int $id){ // 售后单详情 $model = RefundCompensateModel::detail($id); if(empty($model)){ return $this->renderError('找不到售后补偿单'); } $postData = $this->postForm(''); unset($postData['id']); // 关闭售后单 if ($model->close()) { return $this->renderSuccess(['status'=>40],'售后补偿单已关闭'); } return $this->renderError($model->getError() ?: '操作失败'); } /** * 财务退款 * @param int $orderRefundId * @return array|bool */ public function refund(int $id) { // 售后单详情 $model = RefundCompensateModel::detail($id); if(empty($model)){ return $this->renderError('找不到售后补偿单'); } if($model['audit_status_zg'] != 10) { return $this->renderError("主管审核通过才能退款"); } if ($model['finance_refund'] == 10 || $model['status'] == 20) { return $this->renderError("已完成退款,请勿重复操作"); } $ret = $model->refund(); // 退款 if ($ret&&empty($model->getError())) { return $this->renderSuccess('操作成功'); }else{ return $this->renderError($model->getError() ?: '操作失败'); } } }