RefundController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. //列表
  18. public function actionList()
  19. {
  20. $get = Yii::$app->request->get();
  21. $where = [];
  22. $where['mainId'] = $this->mainId;
  23. $orderSn = $get['orderSn'] ?? '';
  24. if (!empty($orderSn)) {
  25. $where['orderSn'] = $orderSn;
  26. }
  27. $list = HdRefundClass::getOrderList($where);
  28. util::success($list);
  29. }
  30. //退款单详情 ssh 20220427
  31. public function actionDetail()
  32. {
  33. $id = Yii::$app->request->get('id', 0);
  34. $refund = HdRefundClass::getById($id, true);
  35. HdRefundClass::valid($refund, $this->mainId);
  36. $refundSn = $refund->refundSn ?? '';
  37. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  38. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  39. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  40. }
  41. //退款 ssh 20220427
  42. public function actionCreateOrder()
  43. {
  44. $shopAdmin = $this->shopAdmin;
  45. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  46. util::fail('超管才能操作退款');
  47. }
  48. $connection = Yii::$app->db;
  49. $transaction = $connection->beginTransaction();
  50. try {
  51. $post = Yii::$app->request->post();
  52. $id = isset($post['id']) ? $post['id'] : 0;
  53. $order = OrderClass::getById($id, true);
  54. OrderClass::valid($order, $this->mainId);
  55. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  56. util::fail("待付款订单无法退款");
  57. }
  58. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  59. util::fail("取消订单不能发起退款");
  60. }
  61. if ($order->fromType == 4) {
  62. util::fail('请在美团APP发起退货退款');
  63. }
  64. $addTime = $order->addTime ?? '';
  65. $current = time();
  66. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  67. util::fail('历史订单,不能再发起退款');
  68. }
  69. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  70. util::fail('历史订单,不能发起退款哦');
  71. }
  72. //退款类型 1退货并退款 2 仅退款
  73. $refundType = $post['refundType'] ?? 1;
  74. if ($refundType == HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
  75. //商品花材结构 property 0商品 1花材
  76. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  77. $productJson = $post['product'] ?? '';
  78. if (empty($productJson)) {
  79. util::fail('请选择商品');
  80. }
  81. $productList = json_decode($productJson, true);
  82. if (!is_array($productList)) {
  83. util::fail('请选择商品哦');
  84. }
  85. $post['product'] = $productList;
  86. } else {
  87. //仅退款商品花材直接设置为空
  88. $post['product'] = [];
  89. }
  90. $post['price'] = $post['price'] ?? 0;
  91. if ($post['price'] <= 0) {
  92. util::fail("退款金额不能小于0");
  93. }
  94. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  95. util::fail("退款金额超过订单金额");
  96. }
  97. $post['shopId'] = $this->shopId;
  98. $post['sjId'] = $this->sjId;
  99. $post['shopAdminId'] = $this->shopAdminId;
  100. $post['mainId'] = $this->mainId;
  101. $shopAdmin = $this->shopAdmin;
  102. $adminName = $shopAdmin['name'] ?? '';
  103. $post['shopAdminName'] = $adminName;
  104. HdRefundService::addRefund($post, $order);
  105. $transaction->commit();
  106. //制作单取消通知
  107. $shopId = $this->shopId ?? 0;
  108. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  109. ShopExtClass::orderCancelRemind($shopExt, $order);
  110. } catch (\Exception $exception) {
  111. $transaction->rollBack();
  112. Yii::info("出错了:" . $exception->getMessage());
  113. util::fail('操作失败');
  114. }
  115. util::complete();
  116. }
  117. public function actionAllRefund()
  118. {
  119. $get = Yii::$app->request->get();
  120. $connection = Yii::$app->db;//事务处理
  121. $transaction = $connection->beginTransaction();
  122. try {
  123. $id = $get['id'] ?? 0;
  124. $order = OrderClass::getById($id, true);
  125. OrderClass::valid($order, $this->mainId);
  126. $shopId = $this->shopId ?? 0;
  127. $sjId = $this->sjId ?? 0;
  128. $mainId = $this->mainId ?? 0;
  129. $post = [];
  130. $post['price'] = $order->actPrice ?? 0;
  131. $post['refundType'] = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
  132. $post['id'] = $order->id ?? 0;
  133. $post['shopId'] = $shopId;
  134. $post['sjId'] = $sjId;
  135. $post['shopAdminId'] = 0;
  136. $post['mainId'] = $mainId;
  137. $staff = $this->shopAdmin;
  138. $post['shopAdminName'] = $staff->name ?? '';
  139. $orderSn = $order->orderSn ?? '';
  140. $orderGoods = OrderGoodsClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  141. $productData = [];
  142. if (!empty($orderGoods)) {
  143. foreach ($orderGoods as $goodsItem) {
  144. $productData[] = ['productId' => $goodsItem->goodsId, 'num' => $goodsItem->num, 'unitPrice' => $goodsItem->unitPrice, 'property' => 0,];
  145. }
  146. }
  147. $orderItem = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  148. if (!empty($orderItem)) {
  149. foreach ($orderItem as $singe) {
  150. $productData[] = ['productId' => $singe->itemId, 'num' => $singe->num, 'unitPrice' => $singe->unitPrice, 'property' => 1,];
  151. }
  152. }
  153. $post['product'] = $productData;
  154. HdRefundService::addRefund($post, $order);
  155. $transaction->commit();
  156. //制作单取消通知
  157. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  158. ShopExtClass::orderCancelRemind($shopExt, $order);
  159. } catch (\Exception $e) {
  160. $transaction->rollBack();
  161. $msg = $e->getMessage();
  162. noticeUtil::push("取消报错了,错误信息:{$msg}", '15280215347');
  163. util::fail('退款失败');
  164. }
  165. }
  166. }