RefundController.php 4.5 KB

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