RefundController.php 7.8 KB

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