RefundController.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. $connection = Yii::$app->db;
  105. $transaction = $connection->beginTransaction();
  106. try {
  107. $post = Yii::$app->request->post();
  108. $id = isset($post['id']) ? $post['id'] : 0;
  109. $order = OrderClass::getById($id, true);
  110. OrderClass::valid($order, $this->shopId);
  111. if ($order->status != OrderClass::ORDER_STATUS_COMPLETE) {
  112. util::fail("订单完成了才能发起退款");
  113. }
  114. $customId = $order->customId ?? 0;
  115. $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true);
  116. $orderIds = [];
  117. if (!empty($clearList)) {
  118. foreach ($clearList as $clear) {
  119. $saleStr = $clear->saleIds ?? '';
  120. $deadline = $clear->deadline ?? '';
  121. if ($deadline < date("Y-m-d H:i:s")) {
  122. $clear->status = 3;
  123. $clear->save();
  124. }else{
  125. if (!empty($saleStr)) {
  126. $current = explode(',', $saleStr);
  127. $orderIds = array_merge($orderIds, $current);
  128. }
  129. }
  130. }
  131. if (!empty($orderIds) && in_array($id, $orderIds)) {
  132. util::fail('此客户有结账单未完成');
  133. }
  134. }
  135. $addTime = $order->addTime ?? '';
  136. $current = time();
  137. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  138. util::fail('历史订单,不能再发起退款');
  139. }
  140. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  141. util::fail('历史订单,不能发起退款哦');
  142. }
  143. //退款类型 1退货并退款 2 仅退款
  144. $refundType = $post['refundType'] ?? 1;
  145. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  146. //花材列表结构
  147. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  148. $productJson = $post['product'] ?? '';
  149. if (empty($productJson)) {
  150. util::fail('请选择花材');
  151. }
  152. $productList = json_decode($productJson, true);
  153. if (!is_array($productList)) {
  154. util::fail('请选择花材哦');
  155. }
  156. $post['product'] = $productList;
  157. } else {
  158. //仅退款花材直接设置为空
  159. $post['product'] = [];
  160. }
  161. $post['price'] = $post['price'] ?? 0;
  162. if ($post['price'] <= 0) {
  163. util::fail("退款金额不能小于0");
  164. }
  165. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  166. util::fail("退款金额超过订单金额");
  167. }
  168. $post['shopId'] = $this->shopId;
  169. $post['sjId'] = $this->sjId;
  170. $post['shopAdminId'] = $this->shopAdminId;
  171. $post['mainId'] = $this->mainId;
  172. $shopAdmin = $this->shopAdmin;
  173. $adminName = $shopAdmin['name'] ?? '';
  174. $post['shopAdminName'] = $adminName;
  175. $respond = OrderService::refund($id, $post);
  176. $transaction->commit();
  177. util::success($respond);
  178. } catch (\Exception $exception) {
  179. $transaction->rollBack();
  180. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  181. util::fail('操作失败');
  182. }
  183. }
  184. }