RefundController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. //根据退款类型:1退货并退款(需要减库存) 2:仅退款(不用减库存,不用传花材)
  40. $refundType = $post['refundType'] ?? 0;
  41. if ($refundType != RefundOrderClass::REFUND_TYPE_MONEY_GOOD && $refundType != RefundOrderClass::REFUND_TYPE_MONEY) {
  42. util::fail("请选择退款类型");
  43. }
  44. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  45. $infoProduct = $order['product'];
  46. $productIds = array_unique(array_filter(array_column($infoProduct, 'productId')));
  47. $productData = ProductClass::getProductByIds($productIds);
  48. $productData = array_column($productData, NULL, 'id');
  49. $orderProductMap = [];
  50. foreach ($infoProduct as $v) {
  51. $orderProductMap[$v['productId']] = $v;
  52. }
  53. //[{productId:0,bigNum:1,smallNum:0}]
  54. $productJson = $post['product'] ?? '';
  55. if (empty($productJson)) {
  56. util::fail('请选择花材');
  57. }
  58. $productList = json_decode($productJson, true);
  59. if (!is_array($productList)) {
  60. util::fail('花材格式不正确');
  61. }
  62. //合并
  63. $productList = ProductClass::mergeItemInfo($productList);
  64. if (empty($productList)) {
  65. util::fail('请选择花材');
  66. }
  67. //判定花材数不能大于原订单数
  68. foreach ($productList as $v) {
  69. $orderProductNum = $orderProductMap[$v['productId']]['num'] ?? 0;
  70. $productName = $productData[$v['productId']]['name'] ?? '';
  71. if (!$orderProductNum) {
  72. util::fail($productName . "花材数量错误");
  73. }
  74. //数量
  75. $bigNum = $v['bigNum'] ?? 0;
  76. $smallNum = $v['smallNum'] ?? 0;
  77. $ratio = $productData[$v['productId']]['ratio'] ?? 0;
  78. $num = ProductClass::mergeItemNum($bigNum, $smallNum, $ratio);
  79. if ($num > $orderProductNum) {
  80. util::fail($productName . "花材数量错误");
  81. }
  82. }
  83. $post['product'] = $productList;
  84. }
  85. $post['price'] = $post['price'] ?? 0;
  86. if ($post['price'] <= 0) {
  87. util::fail("退款金额不能小于0");
  88. }
  89. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  90. util::fail("退款金额不能大于订单金额");
  91. }
  92. $connection = Yii::$app->db;
  93. $transaction = $connection->beginTransaction();
  94. try {
  95. $post['shopId'] = $this->shopId;
  96. $post['sjId'] = $this->sjId;
  97. $post['shopAdminId'] = $this->shopAdminId;
  98. $shopAdmin = $this->shopAdmin;
  99. $adminName = $shopAdmin['name'] ?? '';
  100. $post['shopAdminName'] = $adminName;
  101. OrderService::refund($id, $post);
  102. $transaction->commit();
  103. } catch (\Exception $exception) {
  104. $transaction->rollBack();
  105. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  106. util::fail('操作失败');
  107. }
  108. util::complete();
  109. }
  110. }