RefundController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. if (!empty($clearList)) {
  56. $orderIds = [];
  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. print_r($orderIds);die;
  68. }
  69. $addTime = $order->addTime ?? '';
  70. $current = time();
  71. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  72. util::fail('历史订单,不能再发起退款');
  73. }
  74. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  75. util::fail('历史订单,不能发起退款哦');
  76. }
  77. //退款类型 1退货并退款 2 仅退款
  78. $refundType = $post['refundType'] ?? 1;
  79. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  80. //花材列表结构
  81. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  82. $productJson = $post['product'] ?? '';
  83. if (empty($productJson)) {
  84. util::fail('请选择花材');
  85. }
  86. $productList = json_decode($productJson, true);
  87. if (!is_array($productList)) {
  88. util::fail('请选择花材哦');
  89. }
  90. $post['product'] = $productList;
  91. } else {
  92. //仅退款花材直接设置为空
  93. $post['product'] = [];
  94. }
  95. $post['price'] = $post['price'] ?? 0;
  96. if ($post['price'] <= 0) {
  97. util::fail("退款金额不能小于0");
  98. }
  99. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  100. util::fail("退款金额超过订单金额");
  101. }
  102. $post['shopId'] = $this->shopId;
  103. $post['sjId'] = $this->sjId;
  104. $post['shopAdminId'] = $this->shopAdminId;
  105. $post['mainId'] = $this->mainId;
  106. $shopAdmin = $this->shopAdmin;
  107. $adminName = $shopAdmin['name'] ?? '';
  108. $post['shopAdminName'] = $adminName;
  109. OrderService::refund($id, $post);
  110. $transaction->commit();
  111. } catch (\Exception $exception) {
  112. $transaction->rollBack();
  113. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  114. util::fail('操作失败');
  115. }
  116. util::complete();
  117. }
  118. }