RefundController.php 4.8 KB

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