RefundController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. //退款退货
  3. namespace hd\controllers;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizHd\order\classes\OrderClass;
  6. use bizHd\order\classes\OrderGoodsClass;
  7. use bizHd\order\classes\OrderItemClass;
  8. use bizHd\refund\classes\HdRefundClass;
  9. use bizHd\refund\classes\HdRefundGoodsClass;
  10. use bizHd\refund\classes\HdRefundItemClass;
  11. use bizHd\refund\services\HdRefundService;
  12. use common\components\noticeUtil;
  13. use Yii;
  14. use common\components\util;
  15. class RefundController extends BaseController
  16. {
  17. //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
  18. public function actionRefundAmount()
  19. {
  20. $get = Yii::$app->request->get();
  21. $id = $get['id'] ?? '';
  22. $order = OrderClass::getById($id, true);
  23. if (empty($order)) {
  24. util::fail('没有找到订单');
  25. }
  26. if ($order->mainId != $this->mainId) {
  27. util::fail('不是你的订单');
  28. }
  29. $staff = $this->shopAdmin;
  30. if ($staff->mobile != '15280215347') {
  31. util::fail('请联系管理员操作');
  32. }
  33. $shop = $this->shop;
  34. $respond = HdRefundClass::onlyRefundAmount($shop, $order);
  35. $status = $respond['status'];
  36. $msg = $respond['msg'];
  37. util::success(['status' => $status], $msg);
  38. }
  39. //列表
  40. public function actionList()
  41. {
  42. $get = Yii::$app->request->get();
  43. $where = [];
  44. $where['mainId'] = $this->mainId;
  45. $orderSn = $get['orderSn'] ?? '';
  46. if (!empty($orderSn)) {
  47. $where['orderSn'] = $orderSn;
  48. }
  49. $list = HdRefundClass::getOrderList($where);
  50. util::success($list);
  51. }
  52. //退款单详情 ssh 20220427
  53. public function actionDetail()
  54. {
  55. $id = Yii::$app->request->get('id', 0);
  56. $refund = HdRefundClass::getById($id, true);
  57. HdRefundClass::valid($refund, $this->mainId);
  58. $refundSn = $refund->refundSn ?? '';
  59. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  60. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  61. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  62. }
  63. //退款 ssh 20220427
  64. public function actionCreateOrder()
  65. {
  66. $shopAdmin = $this->shopAdmin;
  67. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  68. util::fail('超管才能操作退款');
  69. }
  70. $post = Yii::$app->request->post();
  71. $id = isset($post['id']) ? $post['id'] : 0;
  72. $version = $post['version'] ?? 1;
  73. if ($version == 1) {
  74. util::fail('请升级小程序');
  75. }
  76. //避免重复提交
  77. util::checkRepeatCommit($id, 10);
  78. $connection = Yii::$app->db;
  79. $transaction = $connection->beginTransaction();
  80. try {
  81. $order = OrderClass::getById($id, true);
  82. OrderClass::valid($order, $this->mainId);
  83. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  84. util::fail("待付款订单无法退款");
  85. }
  86. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  87. util::fail("取消订单不能发起退款");
  88. }
  89. if ($order->fromType == 4) {
  90. util::fail('请在美团APP发起退货退款');
  91. }
  92. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  93. util::fail('10月17日前订单无法申请退款');
  94. }
  95. if ($order->payWay == 2) {
  96. util::fail('余额支付订单暂不支持售后');
  97. }
  98. $addTime = $order->addTime ?? '';
  99. $current = time();
  100. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  101. util::fail('历史订单,不能再发起退款');
  102. }
  103. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  104. util::fail('历史订单,不能发起退款哦');
  105. }
  106. //商品花材结构 property 0商品 1花材
  107. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  108. $productJson = $post['product'] ?? '';
  109. if (empty($productJson)) {
  110. util::fail('请选择商品');
  111. }
  112. $productList = json_decode($productJson, true);
  113. if (!is_array($productList)) {
  114. util::fail('请选择商品哦');
  115. }
  116. $post['product'] = $productList;
  117. $post['price'] = $post['price'] ?? 0;
  118. if ($post['price'] <= 0) {
  119. util::fail("退款金额不能小于0");
  120. }
  121. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  122. util::fail("退款金额超过订单金额");
  123. }
  124. $post['shopId'] = $this->shopId;
  125. $post['sjId'] = $this->sjId;
  126. $post['shopAdminId'] = $this->shopAdminId;
  127. $post['mainId'] = $this->mainId;
  128. $shopAdmin = $this->shopAdmin;
  129. $adminName = $shopAdmin['name'] ?? '';
  130. $post['shopAdminName'] = $adminName;
  131. HdRefundService::addRefund($post, $order);
  132. $transaction->commit();
  133. //制作单取消通知
  134. $shopId = $this->shopId ?? 0;
  135. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  136. ShopExtClass::orderCancelRemind($shopExt, $order);
  137. util::complete();
  138. } catch (\Exception $exception) {
  139. $transaction->rollBack();
  140. Yii::info("出错了:" . $exception->getMessage());
  141. util::fail('操作失败');
  142. }
  143. util::complete();
  144. }
  145. public function actionAllRefund()
  146. {
  147. util::fail('功能开发中');
  148. }
  149. }