RefundController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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_COMPLETE) {
  52. util::fail("完成的订单才能发起退款");
  53. }
  54. $addTime = $order->addTime ?? '';
  55. $current = time();
  56. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  57. util::fail('历史订单,不能再发起退款');
  58. }
  59. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  60. util::fail('历史订单,不能发起退款哦');
  61. }
  62. //退款类型 1退货并退款 2 仅退款
  63. $refundType = $post['refundType'] ?? 1;
  64. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  65. //商品花材结构 property 0商品 1花材
  66. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  67. $productJson = $post['product'] ?? '';
  68. if (empty($productJson)) {
  69. util::fail('请选择商品');
  70. }
  71. $productList = json_decode($productJson, true);
  72. if (!is_array($productList)) {
  73. util::fail('请选择商品哦');
  74. }
  75. $post['product'] = $productList;
  76. } else {
  77. //仅退款商品花材直接设置为空
  78. $post['product'] = [];
  79. }
  80. $post['price'] = $post['price'] ?? 0;
  81. if ($post['price'] <= 0) {
  82. util::fail("退款金额不能小于0");
  83. }
  84. if (bccomp($post['price'], $order['orderPrice'], 2) == 1) {
  85. util::fail("退款金额超过订单金额");
  86. }
  87. $post['shopId'] = $this->shopId;
  88. $post['sjId'] = $this->sjId;
  89. $post['shopAdminId'] = $this->shopAdminId;
  90. $post['mainId'] = $this->mainId;
  91. $shopAdmin = $this->shopAdmin;
  92. $adminName = $shopAdmin['name'] ?? '';
  93. $post['shopAdminName'] = $adminName;
  94. HdRefundService::addRefund($post,$order);
  95. $transaction->commit();
  96. } catch (\Exception $exception) {
  97. $transaction->rollBack();
  98. Yii::info("出错了:" . $exception->getMessage());
  99. util::fail('操作失败');
  100. }
  101. util::complete();
  102. }
  103. }