ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 买家申请退款退货 文件路径: shop/controllers/Buyer/Service/ReturnCtl.php 方法: addReturn() 参数: order\_id:“退款/退货”的订单号 goods\_id:“退款/退货”的订单商品id return\_message:“退款/退货”说明 return\_reason\_id:“退款/退货”原因id nums:“退款/退货”商品数量 # 1.根据goods\_id查找订单信息,将查找出的订单id与传递参数订单id进行比较。两者不一致,报错。 ``` <pre class="calibre14">``` <span class="token2">if</span><span class="token1">(</span>$goods<span class="token1">[</span><span class="token4">'order_id'</span><span class="token1">]</span> <span class="token">!==</span> $order_id<span class="token1">)</span> <span class="token1">{</span> $flag2 <span class="token">=</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` # 2.判断订单状态,只有已付款订单才可进去“退款/退货” ``` <pre class="calibre14">``` <span class="token2">if</span> <span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span> <span class="token"><</span> Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED<span class="token1">)</span> <span class="token1">{</span> $flag2 <span class="token">=</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` # 3.确定return\_type的值,判断此次的“退款/退货”究竟是退款,还是退货。 ``` <pre class="calibre14">``` <span class="token2">if</span> <span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'order_is_virtual'</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> $field<span class="token1">[</span><span class="token4">'return_type'</span><span class="token1">]</span> <span class="token">=</span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_TYPE_VIRTUAL<span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> switch <span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> case Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED<span class="token1">:</span>$field<span class="token1">[</span><span class="token4">'return_type'</span><span class="token1">]</span> <span class="token">=</span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_TYPE_ORDER <span class="token1">;</span> <span class="token5">//退款</span> <span class="token2">break</span><span class="token1">;</span> case Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_FINISH<span class="token1">:</span>$field<span class="token1">[</span><span class="token4">'return_type'</span><span class="token1">]</span> <span class="token">=</span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_TYPE_GOODS <span class="token1">;</span> <span class="token5">//退货</span> <span class="token2">break</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` # 4.如果是货到付款,确认收货(付款)后才能退款 ``` <pre class="calibre14">``` <span class="token2">if</span><span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'payment_id'</span><span class="token1">]</span> <span class="token">==</span> PaymentChannlModel<span class="token1">:</span><span class="token1">:</span>PAY_CONFIRM<span class="token1">)</span><span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span> <span class="token"><</span> Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_RECEIVED<span class="token1">)</span> <span class="token1">{</span> $flag2 <span class="token">=</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> <span class="token1">}</span> ``` ``` # 5.根据订单状态,确定return\_goods\_return值。 退款(货到付款只支持退货,不支持退款) ``` <pre class="calibre14">``` <span class="token2">if</span><span class="token1">(</span>$goods<span class="token1">[</span><span class="token4">'order_goods_status'</span><span class="token1">]</span> <span class="token">==</span> Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED <span class="token">&&</span> $order<span class="token1">[</span><span class="token4">'payment_id'</span><span class="token1">]</span> <span class="token">!==</span> PaymentChannlModel<span class="token1">:</span><span class="token1">:</span>PAY_CONFIRM<span class="token1">)</span> <span class="token1">{</span> <span class="token5">//白条支付不支持退款和退货</span> <span class="token2">if</span><span class="token1">(</span><span class="token3">strstr</span><span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'payment_name'</span><span class="token1">]</span><span class="token1">,</span><span class="token4">'白条支付'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> $flag2 <span class="token">=</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> $field<span class="token1">[</span><span class="token4">'return_goods_return'</span><span class="token1">]</span> <span class="token">=</span> <span class="token6">0</span><span class="token1">;</span> <span class="token5">//是否需要退货 0-不需要 1-需要</span> $<span class="token2">return</span> <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderReturnModel<span class="token">-</span><span class="token">></span><span class="token3">getByWhere</span><span class="token1">(</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'order_goods_id'</span> <span class="token">=</span><span class="token">></span> $goods_id<span class="token1">,</span> <span class="token4">'return_type'</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_TYPE_ORDER<span class="token1">,</span> <span class="token4">'return_state:!='</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_SELLER_UNPASS <span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` 退货 ``` <pre class="calibre14">``` <span class="token2">if</span> <span class="token1">(</span>$goods<span class="token1">[</span><span class="token4">'order_goods_status'</span><span class="token1">]</span> <span class="token">==</span> Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_FINISH<span class="token1">)</span> <span class="token1">{</span> <span class="token2">if</span><span class="token1">(</span><span class="token3">strstr</span><span class="token1">(</span>$order<span class="token1">[</span><span class="token4">'payment_name'</span><span class="token1">]</span><span class="token1">,</span><span class="token4">'白条支付'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">{</span> $flag2 <span class="token">=</span> <span class="token6">false</span><span class="token1">;</span> <span class="token1">}</span> $field<span class="token1">[</span><span class="token4">'return_goods_return'</span><span class="token1">]</span> <span class="token">=</span> <span class="token6">1</span><span class="token1">;</span> <span class="token5">//需要退货</span> <span class="token5">//查询是否存在该订单商品的退货申请信息,且该申请未被卖家拒绝,以此判断是否重新提交退货申请</span> <span class="token5">//只有以前没有提交过该商品的退货申请,且未被卖家拒绝的情况下,才可以提交退货申请</span> $<span class="token2">return</span> <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderReturnModel<span class="token">-</span><span class="token">></span><span class="token3">getByWhere</span><span class="token1">(</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'order_goods_id'</span> <span class="token">=</span><span class="token">></span> $goods_id<span class="token1">,</span> <span class="token4">'return_type'</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_TYPE_GOODS<span class="token1">,</span> <span class="token4">'return_state:!='</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_SELLER_UNPASS <span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ``` # 6.计算生成“退款/退货”订单所需要的各种金额 //判断这件“退款/退货”商品是否还有可退数量(退款,退货都会退还商品数量) ``` <pre class="calibre14">``` $this_goods_return <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderReturnModel<span class="token">-</span><span class="token">></span><span class="token3">getByWhere</span><span class="token1">(</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'order_goods_id'</span> <span class="token">=</span><span class="token">></span> $goods_id<span class="token1">,</span> <span class="token4">'return_state:!='</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_SELLER_UNPASS <span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> ``` ``` - $this\_goods\_return\_num :“退款/退货”商品总的已退还件数 - $this\_goods\_return\_cash :“退款/退货”商品总的已退金额(包含正在审核中的金额) //查找该笔订单已经进行过或正进行中的的退款,退货 ``` <pre class="calibre14">``` $order_return <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderReturnModel<span class="token">-</span><span class="token">></span><span class="token3">getByWhere</span><span class="token1">(</span><span class="token3">array</span><span class="token1">(</span> <span class="token4">'order_number'</span> <span class="token">=</span><span class="token">></span> $order<span class="token1">[</span><span class="token4">'order_id'</span><span class="token1">]</span><span class="token1">,</span> <span class="token4">'return_state:!='</span> <span class="token">=</span><span class="token">></span> Order_ReturnModel<span class="token1">:</span><span class="token1">:</span>RETURN_SELLER_UNPASS <span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> ``` ``` //订单已经退款退货的金额(包括与同意的退款和正在审核中的退款) ``` <pre class="calibre14">``` $order_return_cash <span class="token">=</span> <span class="token3">array_sum</span><span class="token1">(</span><span class="token3">array_column</span><span class="token1">(</span>$order_return<span class="token1">,</span> <span class="token4">'return_cash'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> ``` ``` //订单已经退还的商品数量 ``` <pre class="calibre14">``` $order_return_num <span class="token">=</span> <span class="token3">array_sum</span><span class="token1">(</span><span class="token3">array_column</span><span class="token1">(</span>$order_return<span class="token1">,</span> <span class="token4">'order_goods_num'</span><span class="token1">)</span><span class="token1">)</span><span class="token1">;</span> ``` ``` //订单还可退还的金额 = 订单实付金额 - 订单已退金额 //如果没有发货,可以退运费 ``` <pre class="calibre14">``` <span class="token2">if</span> <span class="token1">(</span>Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED <span class="token">==</span> $order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> $order_can_return_cash <span class="token">=</span> $order<span class="token1">[</span><span class="token4">'order_payment_amount'</span><span class="token1">]</span> <span class="token">-</span> $order_return_cash<span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> $order_can_return_cash <span class="token">=</span> $order<span class="token1">[</span><span class="token4">'order_payment_amount'</span><span class="token1">]</span> <span class="token">-</span> $order_return_cash <span class="token">-</span> $order<span class="token1">[</span><span class="token4">'order_shipping_fee'</span><span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">/* * $data['order']['order_refund_amount'] 与 $order_return_cash 的区别 * $data['order']['order_refund_amount']:表示商家已经同意的退款金额 * $order_return_cash:表示买家已经申请的退款,除被商家拒绝的退款外,正在审核的退款也包含在内 */</span> <span class="token5">//订单已退还的金额</span> $return_limit <span class="token">=</span> $order<span class="token1">[</span><span class="token4">'order_refund_amount'</span><span class="token1">]</span><span class="token1">;</span> <span class="token5">//订单可退金额</span> $cash_limit <span class="token">=</span> $order_can_return_cash<span class="token1">;</span> <span class="token5">//订单可退商品数量</span> $goods_can_return_nums <span class="token">=</span> $order_all_goods_num <span class="token">-</span> $order_return_num<span class="token1">;</span> <span class="token5">//该件商品可退的总金额</span> $return_goods_cash <span class="token">=</span> $goods<span class="token1">[</span><span class="token4">'order_goods_amount'</span><span class="token1">]</span> <span class="token">-</span> $this_goods_return_cash<span class="token1">;</span> <span class="token5">//该件商品还可退还商品数量</span> $return_goods_nums <span class="token">=</span> $goods<span class="token1">[</span><span class="token4">'order_goods_num'</span><span class="token1">]</span> <span class="token">-</span> $this_goods_return_num<span class="token1">;</span> ``` ``` //实际该件商品可退还的金额(有时可能包含运费) //该件商品全部“退款/退货” //return\_goods\_nums ``` <pre class="calibre14">``` <span class="token2">if</span><span class="token1">(</span>$goods_can_return_nums <span class="token">==</span> $nums <span class="token">&&</span> Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED <span class="token">==</span> $order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span><span class="token1">)</span> <span class="token1">{</span> <span class="token5">//加上运费(未发货)</span> $return_cash <span class="token">=</span> $return_goods_cash <span class="token">+</span> $order<span class="token1">[</span><span class="token4">'order_shipping_fee'</span><span class="token1">]</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> $return_cash <span class="token">=</span> <span class="token3">floor</span><span class="token1">(</span>$nums <span class="token">*</span> $goods<span class="token1">[</span><span class="token4">'order_goods_payment_amount'</span><span class="token1">]</span> <span class="token">*</span> <span class="token6">100</span><span class="token1">)</span> <span class="token">/</span> <span class="token6">100</span><span class="token1">;</span> <span class="token1">}</span> <span class="token5">//如果订单为已付款状态,并且所有商品都退款,则将运费退还</span> <span class="token2">if</span><span class="token1">(</span>Order_StateModel<span class="token1">:</span><span class="token1">:</span>ORDER_PAYED <span class="token">==</span> $order<span class="token1">[</span><span class="token4">'order_status'</span><span class="token1">]</span> <span class="token">&&</span> $nums <span class="token">==</span> $goods_can_return_nums<span class="token1">)</span> <span class="token1">{</span> $return_cash <span class="token">=</span> $cash_limit<span class="token1">;</span> <span class="token1">}</span> <span class="token5">/*退款退货走同样的流程。区别是:退款时可能会退还运费,退货不可能退还运费。*/</span> ``` ``` # 7.订单商品表中插入订单商品的“退款/退货”状态 ``` <pre class="calibre15">``` <span class="token2">if</span><span class="token1">(</span>$field<span class="token1">[</span><span class="token4">'return_goods_return'</span><span class="token1">]</span> <span class="token">==</span> <span class="token6">0</span><span class="token1">)</span> <span class="token1">{</span> <span class="token5">//退款</span> $goods_field<span class="token1">[</span><span class="token4">'goods_return_status'</span><span class="token1">]</span> <span class="token">=</span> Order_GoodsModel<span class="token1">:</span><span class="token1">:</span>REFUND_IN<span class="token1">;</span> $edit_flag <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderGoodsModel<span class="token">-</span><span class="token">></span><span class="token3">editGoods</span><span class="token1">(</span>$goods_id<span class="token1">,</span> $goods_field<span class="token1">)</span><span class="token1">;</span> <span class="token3">check_rs</span><span class="token1">(</span>$edit_flag<span class="token1">,</span> $rs_row<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> <span class="token2">else</span> <span class="token1">{</span> <span class="token5">//退货</span> $goods_field<span class="token1">[</span><span class="token4">'goods_refund_status'</span><span class="token1">]</span> <span class="token">=</span> Order_GoodsModel<span class="token1">:</span><span class="token1">:</span>REFUND_IN<span class="token1">;</span> $edit_flag <span class="token">=</span> $this<span class="token">-</span><span class="token">></span>orderGoodsModel<span class="token">-</span><span class="token">></span><span class="token3">editGoods</span><span class="token1">(</span>$goods_id<span class="token1">,</span> $goods_field<span class="token1">)</span><span class="token1">;</span> <span class="token3">check_rs</span><span class="token1">(</span>$edit_flag<span class="token1">,</span> $rs_row<span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span> ``` ```