RefundController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use Yii;
  5. use bizGhs\order\classes\RefundOrderClass;
  6. use common\components\util;
  7. use bizGhs\product\classes\ProductClass;
  8. use bizGhs\order\classes\OrderClass;
  9. use bizGhs\order\services\OrderService;
  10. class RefundController extends BaseController
  11. {
  12. //列表
  13. public function actionList()
  14. {
  15. $where = [];
  16. $where['shopId'] = $this->shopId;
  17. $list = RefundOrderClass::getOrderList($where);
  18. util::success($list);
  19. }
  20. public function actionDetail()
  21. {
  22. $orderSn = Yii::$app->request->get('orderSn', '');
  23. $orderData = RefundOrderClass::getOrderDetailByRelateOrder($orderSn, $this->shopId);
  24. util::success($orderData);
  25. }
  26. //退款 linqh 2021.6.25
  27. public function actionCreateOrder()
  28. {
  29. $post = Yii::$app->request->post();
  30. $id = isset($post['id']) ? $post['id'] : 0;
  31. $order = OrderService::getOrderInfo($id, false, false, false, false);
  32. OrderClass::valid($order, $this->shopId);
  33. if ($order['status'] != OrderClass::ORDER_STATUS_COMPLETE) {
  34. util::fail("订单完成了才能发起退款");
  35. }
  36. if ($order['refund'] == 2) {
  37. util::fail("已经退过了");
  38. }
  39. $addTime = $order['addTime'] ?? '';
  40. $current = time();
  41. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  42. util::fail('订单已经很久远了,不能再发起退款');
  43. }
  44. //根据退款类型:1退货并退款(需要减库存) 2:仅退款(不用减库存,不用传花材)
  45. $refundType = $post['refundType'] ?? 0;
  46. if ($refundType != RefundOrderClass::REFUND_TYPE_MONEY_GOOD && $refundType != RefundOrderClass::REFUND_TYPE_MONEY) {
  47. util::fail("请选择退款类型");
  48. }
  49. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  50. $infoProduct = $order['product'];
  51. $productIds = array_unique(array_filter(array_column($infoProduct, 'productId')));
  52. $productData = ProductClass::getProductByIds($productIds);
  53. $productData = array_column($productData, NULL, 'id');
  54. $orderProductMap = [];
  55. foreach ($infoProduct as $v) {
  56. $orderProductMap[$v['productId']] = $v;
  57. }
  58. //[{productId:0,bigNum:1,smallNum:0}]
  59. $productJson = $post['product'] ?? '';
  60. if (empty($productJson)) {
  61. util::fail('请选择花材');
  62. }
  63. $productList = json_decode($productJson, true);
  64. if (!is_array($productList)) {
  65. util::fail('花材格式不正确');
  66. }
  67. //合并
  68. $productList = ProductClass::mergeItemInfo($productList);
  69. if (empty($productList)) {
  70. util::fail('请选择花材');
  71. }
  72. //判定花材数不能大于原订单数
  73. foreach ($productList as $v) {
  74. $orderProductNum = $orderProductMap[$v['productId']]['num'] ?? 0;
  75. $productName = $productData[$v['productId']]['name'] ?? '';
  76. if (!$orderProductNum) {
  77. util::fail($productName . "花材数量错误");
  78. }
  79. //数量
  80. $bigNum = $v['bigNum'] ?? 0;
  81. $smallNum = $v['smallNum'] ?? 0;
  82. $ratio = $productData[$v['productId']]['ratio'] ?? 0;
  83. $num = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  84. if ($num > $orderProductNum) {
  85. util::fail($productName . "花材数量错误");
  86. }
  87. }
  88. $post['product'] = $productList;
  89. }
  90. $post['price'] = $post['price'] ?? 0;
  91. if ($post['price'] <= 0) {
  92. util::fail("退款金额不能小于0");
  93. }
  94. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  95. util::fail("退款金额不能大于订单金额");
  96. }
  97. $connection = Yii::$app->db;
  98. $transaction = $connection->beginTransaction();
  99. try {
  100. $post['shopId'] = $this->shopId;
  101. $post['sjId'] = $this->sjId;
  102. $post['shopAdminId'] = $this->shopAdminId;
  103. $shopAdmin = $this->shopAdmin;
  104. $adminName = $shopAdmin['name'] ?? '';
  105. $post['shopAdminName'] = $adminName;
  106. OrderService::refund($id, $post);
  107. $transaction->commit();
  108. } catch (\Exception $exception) {
  109. $transaction->rollBack();
  110. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  111. util::fail('操作失败');
  112. }
  113. util::complete();
  114. }
  115. }