RefundController.php 5.1 KB

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