RefundController.php 4.0 KB

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