RefundController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use Yii;
  5. use bizGhs\order\classes\RefundOrderClass;
  6. use common\components\util;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. class RefundController extends BaseController
  11. {
  12. //列表
  13. public function actionList()
  14. {
  15. $get = Yii::$app->request->get();
  16. $where = [];
  17. $where['mainId'] = $this->mainId;
  18. $orderSn = $get['orderSn'] ?? '';
  19. if (!empty($orderSn)) {
  20. $where['relateOrderSn'] = $orderSn;
  21. }
  22. $list = RefundOrderClass::getOrderList($where);
  23. util::success($list);
  24. }
  25. public function actionDetail()
  26. {
  27. $orderSn = Yii::$app->request->get('orderSn', '');
  28. $orderData = RefundOrderClass::getOrderDetailByRelateOrder($orderSn, $this->shopId);
  29. util::success($orderData);
  30. }
  31. //退款 linqh 2021.6.25
  32. public function actionCreateOrder()
  33. {
  34. $shopAdmin = $this->shopAdmin;
  35. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  36. util::fail('超管才能操作退款');
  37. }
  38. $connection = Yii::$app->db;
  39. $transaction = $connection->beginTransaction();
  40. try {
  41. $post = Yii::$app->request->post();
  42. $id = isset($post['id']) ? $post['id'] : 0;
  43. $order = OrderClass::getById($id, true);
  44. OrderClass::valid($order, $this->shopId);
  45. if ($order->status != OrderClass::ORDER_STATUS_COMPLETE) {
  46. util::fail("订单完成了才能发起退款");
  47. }
  48. $addTime = $order->addTime ?? '';
  49. $current = time();
  50. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  51. util::fail('历史订单,不能再发起退款');
  52. }
  53. if (strtotime($addTime) <= strtotime('2022-04-01 00:00:00')) {
  54. util::fail('历史订单,不能发起退款哦');
  55. }
  56. //退款类型 1退货并退款 2 仅退款
  57. $refundType = $post['refundType'] ?? 1;
  58. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  59. //花材列表结构
  60. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  61. $productJson = $post['product'] ?? '';
  62. if (empty($productJson)) {
  63. util::fail('请选择花材');
  64. }
  65. $productList = json_decode($productJson, true);
  66. if (!is_array($productList)) {
  67. util::fail('请选择花材哦');
  68. }
  69. $post['product'] = $productList;
  70. } else {
  71. //仅退款花材直接设置为空
  72. $post['product'] = [];
  73. }
  74. $post['price'] = $post['price'] ?? 0;
  75. if ($post['price'] <= 0) {
  76. util::fail("退款金额不能小于0");
  77. }
  78. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  79. util::fail("退款金额超过订单金额");
  80. }
  81. $post['shopId'] = $this->shopId;
  82. $post['sjId'] = $this->sjId;
  83. $post['shopAdminId'] = $this->shopAdminId;
  84. $post['mainId'] = $this->mainId;
  85. $shopAdmin = $this->shopAdmin;
  86. $adminName = $shopAdmin['name'] ?? '';
  87. $post['shopAdminName'] = $adminName;
  88. OrderService::refund($id, $post);
  89. $transaction->commit();
  90. } catch (\Exception $exception) {
  91. $transaction->rollBack();
  92. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  93. util::fail('操作失败');
  94. }
  95. util::complete();
  96. }
  97. }