RefundController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. $shopAdmin = $this->shopAdmin;
  19. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  20. util::fail('超管才能报损');
  21. }
  22. $post = Yii::$app->request->post();
  23. $post['shopId'] = $this->shopId;
  24. $post['sjId'] = $this->sjId;
  25. $post['mainId'] = $this->mainId;
  26. $post['shopAdminId'] = $this->shopAdminId;
  27. $shopAdmin = $this->shopAdmin;
  28. $adminName = $shopAdmin['name'] ?? '';
  29. $post['shopAdminName'] = $adminName;
  30. $productJson = $post['product'] ?? '';
  31. if (empty($productJson)) {
  32. util::fail('请选择花材');
  33. }
  34. $productList = json_decode($productJson, true);
  35. if (empty($productList)) {
  36. util::fail('请选择花材');
  37. }
  38. foreach ($productList as $v) {
  39. $bigNum = $v['bigNum'] ?? 0;
  40. $smallNum = $v['smallNum'] ?? 0;
  41. if ($bigNum <= 0) {
  42. util::fail('花材数量不能为0');
  43. }
  44. if ($smallNum > 0) {
  45. util::fail('小单位不能报损');
  46. }
  47. }
  48. $connection = Yii::$app->db;//事务处理
  49. $transaction = $connection->beginTransaction();
  50. try {
  51. //判断花材有效性
  52. ProductClass::valid($productList, $this->mainId);
  53. $post['product'] = $productList;
  54. $return = StockWastageOrderClass::addOrder($post);
  55. $transaction->commit();
  56. util::success($return);
  57. } catch (\Exception $e) {
  58. $transaction->rollBack();
  59. Yii::info("报损失败原因:" . $e->getMessage());
  60. util::fail('报损失败');
  61. }
  62. }
  63. //列表
  64. public function actionList()
  65. {
  66. $get = Yii::$app->request->get();
  67. $where = [];
  68. $where['mainId'] = $this->mainId;
  69. $orderSn = $get['orderSn'] ?? '';
  70. if (!empty($orderSn)) {
  71. $where['relateOrderSn'] = $orderSn;
  72. }
  73. $list = RefundOrderClass::getOrderList($where);
  74. util::success($list);
  75. }
  76. public function actionDetail()
  77. {
  78. $id = Yii::$app->request->get('id', 0);
  79. $refund = RefundOrderClass::getById($id, true);
  80. RefundOrderClass::valid($refund, $this->mainId);
  81. $orderSn = $refund->orderSn ?? '';
  82. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  83. util::success(['info' => $refund, 'itemList' => $itemList]);
  84. }
  85. //退款 lqh 2021.6.25
  86. public function actionCreateOrder()
  87. {
  88. $shopAdmin = $this->shopAdmin;
  89. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  90. util::fail('超管才能操作退款');
  91. }
  92. $connection = Yii::$app->db;
  93. $transaction = $connection->beginTransaction();
  94. try {
  95. $post = Yii::$app->request->post();
  96. $id = isset($post['id']) ? $post['id'] : 0;
  97. $order = OrderClass::getById($id, true);
  98. OrderClass::valid($order, $this->shopId);
  99. if ($order->status != OrderClass::ORDER_STATUS_COMPLETE) {
  100. util::fail("订单完成了才能发起退款");
  101. }
  102. $customId = $order->customId ?? 0;
  103. $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*');
  104. $orderIds = [];
  105. if (!empty($clearList)) {
  106. foreach ($clearList as $clear) {
  107. $saleStr = $clear['saleIds'] ?? '';
  108. if (!empty($saleStr)) {
  109. $current = explode(',', $saleStr);
  110. $orderIds = array_merge($orderIds, $current);
  111. }
  112. }
  113. if (!empty($orderIds) && in_array($id, $orderIds)) {
  114. util::fail('此客户有结账单未完成');
  115. }
  116. }
  117. $addTime = $order->addTime ?? '';
  118. $current = time();
  119. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  120. util::fail('历史订单,不能再发起退款');
  121. }
  122. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  123. util::fail('历史订单,不能发起退款哦');
  124. }
  125. //退款类型 1退货并退款 2 仅退款
  126. $refundType = $post['refundType'] ?? 1;
  127. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  128. //花材列表结构
  129. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  130. $productJson = $post['product'] ?? '';
  131. if (empty($productJson)) {
  132. util::fail('请选择花材');
  133. }
  134. $productList = json_decode($productJson, true);
  135. if (!is_array($productList)) {
  136. util::fail('请选择花材哦');
  137. }
  138. $post['product'] = $productList;
  139. } else {
  140. //仅退款花材直接设置为空
  141. $post['product'] = [];
  142. }
  143. $post['price'] = $post['price'] ?? 0;
  144. if ($post['price'] <= 0) {
  145. util::fail("退款金额不能小于0");
  146. }
  147. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  148. util::fail("退款金额超过订单金额");
  149. }
  150. $post['shopId'] = $this->shopId;
  151. $post['sjId'] = $this->sjId;
  152. $post['shopAdminId'] = $this->shopAdminId;
  153. $post['mainId'] = $this->mainId;
  154. $shopAdmin = $this->shopAdmin;
  155. $adminName = $shopAdmin['name'] ?? '';
  156. $post['shopAdminName'] = $adminName;
  157. $respond = OrderService::refund($id, $post);
  158. $transaction->commit();
  159. util::success($respond);
  160. } catch (\Exception $exception) {
  161. $transaction->rollBack();
  162. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  163. util::fail('操作失败');
  164. }
  165. }
  166. }