RefundController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. //退款退货
  3. namespace hd\controllers;
  4. use bizHd\order\classes\OrderClass;
  5. use bizHd\refund\classes\HdRefundClass;
  6. use bizHd\refund\classes\HdRefundGoodsClass;
  7. use bizHd\refund\classes\HdRefundItemClass;
  8. use bizHd\refund\services\HdRefundService;
  9. use Yii;
  10. use common\components\util;
  11. class RefundController extends BaseController
  12. {
  13. //列表
  14. public function actionList()
  15. {
  16. $get = Yii::$app->request->get();
  17. $where = [];
  18. $where['mainId'] = $this->mainId;
  19. $orderSn = $get['orderSn'] ?? '';
  20. if (!empty($orderSn)) {
  21. $where['orderSn'] = $orderSn;
  22. }
  23. $list = HdRefundClass::getOrderList($where);
  24. util::success($list);
  25. }
  26. //退款单详情 ssh 20220427
  27. public function actionDetail()
  28. {
  29. $id = Yii::$app->request->get('id', 0);
  30. $refund = HdRefundClass::getById($id, true);
  31. HdRefundClass::valid($refund, $this->mainId);
  32. $refundSn = $refund->refundSn ?? '';
  33. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  34. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  35. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  36. }
  37. //退款 ssh 20220427
  38. public function actionCreateOrder()
  39. {
  40. $shopAdmin = $this->shopAdmin;
  41. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  42. util::fail('超管才能操作退款');
  43. }
  44. $connection = Yii::$app->db;
  45. $transaction = $connection->beginTransaction();
  46. try {
  47. $post = Yii::$app->request->post();
  48. $id = isset($post['id']) ? $post['id'] : 0;
  49. $order = OrderClass::getById($id, true);
  50. OrderClass::valid($order, $this->mainId);
  51. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  52. util::fail("待付款订单无法退款");
  53. }
  54. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  55. util::fail("取消订单不能发起退款");
  56. }
  57. $addTime = $order->addTime ?? '';
  58. $current = time();
  59. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  60. util::fail('历史订单,不能再发起退款');
  61. }
  62. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  63. util::fail('历史订单,不能发起退款哦');
  64. }
  65. //退款类型 1退货并退款 2 仅退款
  66. $refundType = $post['refundType'] ?? 1;
  67. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  68. //商品花材结构 property 0商品 1花材
  69. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  70. $productJson = $post['product'] ?? '';
  71. if (empty($productJson)) {
  72. util::fail('请选择商品');
  73. }
  74. $productList = json_decode($productJson, true);
  75. if (!is_array($productList)) {
  76. util::fail('请选择商品哦');
  77. }
  78. $post['product'] = $productList;
  79. } else {
  80. //仅退款商品花材直接设置为空
  81. $post['product'] = [];
  82. }
  83. $post['price'] = $post['price'] ?? 0;
  84. if ($post['price'] <= 0) {
  85. util::fail("退款金额不能小于0");
  86. }
  87. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  88. util::fail("退款金额超过订单金额");
  89. }
  90. $post['orderId'] = $id;
  91. $post['shopId'] = $this->shopId;
  92. $post['sjId'] = $this->sjId;
  93. $post['shopAdminId'] = $this->shopAdminId;
  94. $post['mainId'] = $this->mainId;
  95. $shopAdmin = $this->shopAdmin;
  96. $adminName = $shopAdmin['name'] ?? '';
  97. $post['shopAdminName'] = $adminName;
  98. HdRefundService::addRefund($post, $order);
  99. $transaction->commit();
  100. } catch (\Exception $exception) {
  101. $transaction->rollBack();
  102. Yii::info("出错了:" . $exception->getMessage());
  103. util::fail('操作失败');
  104. }
  105. util::complete();
  106. }
  107. }