RefundController.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. //退款退货
  3. namespace ghs\controllers;
  4. use bizGhs\order\classes\RefundOrderItemClass;
  5. use bizHd\purchase\classes\PurchaseClearClass;
  6. use common\components\imgUtil;
  7. use Yii;
  8. use bizGhs\order\classes\RefundOrderClass;
  9. use common\components\util;
  10. use bizGhs\product\classes\ProductClass;
  11. use bizGhs\order\classes\OrderClass;
  12. use bizGhs\order\services\OrderService;
  13. use bizGhs\order\classes\StockWastageOrderClass;
  14. class RefundController extends BaseController
  15. {
  16. //审核通过 ssh 20230811
  17. public function actionPass()
  18. {
  19. $get = Yii::$app->request->get();
  20. $id = $get['id'] ?? 0;
  21. $refund = RefundOrderClass::getById($id, true);
  22. RefundOrderClass::valid($refund, $this->mainId);
  23. RefundOrderClass::passRefund($refund);
  24. util::complete('操作成功');
  25. }
  26. public function actionReject()
  27. {
  28. $get = Yii::$app->request->get();
  29. $id = $get['id'] ?? 0;
  30. $rejectReason = $get['rejectReason'] ?? '';
  31. $refund = RefundOrderClass::getById($id, true);
  32. RefundOrderClass::valid($refund, $this->mainId);
  33. RefundOrderClass::rejectRefund($refund, $rejectReason);
  34. util::complete('操作成功');
  35. }
  36. //退款之后报损 ssh 20230409
  37. public function actionRefundWaste()
  38. {
  39. $shopAdmin = $this->shopAdmin;
  40. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  41. util::fail('超管才能报损');
  42. }
  43. $post = Yii::$app->request->post();
  44. $refundItemId = $post['refundItemId'] ?? 0;
  45. $wasteProductId = $post['wasteProductId'] ?? 0;
  46. $wasteNum = $post['wasteNum'] ?? 0;
  47. unset($post['refundId']);
  48. unset($post['wasteNum']);
  49. unset($post['wasteProductId']);
  50. $productInfo = ProductClass::getById($wasteProductId, true);
  51. if (empty($productInfo)) {
  52. util::fail('没有找到花材');
  53. }
  54. $ptItemId = $productInfo->itemId ?? 0;
  55. $ratio = $productInfo->ratio ?? 20;
  56. $refundItem = RefundOrderItemClass::getById($refundItemId, true);
  57. if (empty($refundItem)) {
  58. util::fail('没有找到退货信息');
  59. }
  60. if ($refundItem->mainId != $this->mainId) {
  61. util::fail('不是您的退款信息');
  62. }
  63. $unitType = $refundItem->xhUnitType ?? 0;
  64. if ($unitType == 1) {
  65. util::fail('无法报损');
  66. }
  67. $refundNum = $refundItem->xhNum ?? 0;
  68. $hasWasteNum = $refundItem->xhWasteNum ?? 0;
  69. $remainNum = bcsub($refundNum, $hasWasteNum);
  70. if ($wasteNum > $remainNum) {
  71. util::fail('剩余可报损数量 ' . $remainNum);
  72. }
  73. $productList = [['productId' => $wasteProductId, 'bigNum' => $wasteNum, 'smallNum' => 0, 'classId' => 0, 'itemId' => $ptItemId, 'ratio' => $ratio]];
  74. $post['shopId'] = $this->shopId;
  75. $post['sjId'] = $this->sjId;
  76. $post['mainId'] = $this->mainId;
  77. $post['shopAdminId'] = $this->shopAdminId;
  78. $shopAdmin = $this->shopAdmin;
  79. $adminName = $shopAdmin['name'] ?? '';
  80. $post['shopAdminName'] = $adminName;
  81. $connection = Yii::$app->db;//事务处理
  82. $transaction = $connection->beginTransaction();
  83. try {
  84. $refundItem->xhWasteNum = bcadd($refundItem->xhWasteNum, $wasteNum);
  85. $refundItem->save();
  86. $post['product'] = $productList;
  87. $return = StockWastageOrderClass::addOrder($post);
  88. $transaction->commit();
  89. util::success($return);
  90. } catch (\Exception $e) {
  91. $transaction->rollBack();
  92. Yii::info("报损失败原因:" . $e->getMessage());
  93. util::fail('报损失败');
  94. }
  95. }
  96. //列表
  97. public function actionList()
  98. {
  99. $get = Yii::$app->request->get();
  100. $where = [];
  101. $where['mainId'] = $this->mainId;
  102. $orderSn = $get['orderSn'] ?? '';
  103. if (!empty($orderSn)) {
  104. $where['relateOrderSn'] = $orderSn;
  105. }
  106. $list = RefundOrderClass::getOrderList($where);
  107. util::success($list);
  108. }
  109. public function actionDetail()
  110. {
  111. $id = Yii::$app->request->get('id', 0);
  112. $refund = RefundOrderClass::getById($id);
  113. if (empty($refund)) {
  114. util::fail('没有找到退款信息');
  115. }
  116. if (isset($refund['mainId']) == false || $refund['mainId'] != $this->mainId) {
  117. util::fail('没有权限');
  118. }
  119. $imgString = $refund['imgList'] ?? '';
  120. if (!empty($imgString)) {
  121. $imgArr = json_decode($imgString, true);
  122. $smallImgList = [];
  123. $bigImgList = [];
  124. if (!empty($imgArr)) {
  125. foreach ($imgArr as $image) {
  126. $smallImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  127. $bigImg = imgUtil::groupImg($image) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  128. $smallImgList[] = $smallImg;
  129. $bigImgList[] = $bigImg;
  130. }
  131. }
  132. $refund['imgList'] = $imgArr;
  133. $refund['smallImgList'] = $smallImgList;
  134. $refund['bigImgList'] = $bigImgList;
  135. }
  136. $orderSn = $refund['orderSn'] ?? '';
  137. $itemList = RefundOrderItemClass::getOrderItemDetail($orderSn);
  138. util::success(['info' => $refund, 'itemList' => $itemList]);
  139. }
  140. //退款 lqh 2021.6.25
  141. public function actionCreateOrder()
  142. {
  143. $shopAdmin = $this->shopAdmin;
  144. if (isset($shopAdmin['super']) == false || $shopAdmin['super'] != 1) {
  145. util::fail('超管才能操作退款');
  146. }
  147. //国恋只有指定人有退款权限
  148. if ($this->mainId == 7704) {
  149. if (in_array($this->shopAdminId, [133727, 133545]) == false) {
  150. util::fail('没有权限操作');
  151. }
  152. }
  153. $connection = Yii::$app->db;
  154. $transaction = $connection->beginTransaction();
  155. try {
  156. $post = Yii::$app->request->post();
  157. $id = isset($post['id']) ? $post['id'] : 0;
  158. $order = OrderClass::getById($id, true);
  159. OrderClass::valid($order, $this->shopId);
  160. if ($order->status != OrderClass::ORDER_STATUS_COMPLETE) {
  161. util::fail("订单完成了才能发起退款");
  162. }
  163. $customId = $order->customId ?? 0;
  164. $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true);
  165. $orderIds = [];
  166. if (!empty($clearList)) {
  167. foreach ($clearList as $clear) {
  168. $saleStr = $clear->saleIds ?? '';
  169. $deadline = $clear->deadline ?? '';
  170. if ($deadline < date("Y-m-d H:i:s")) {
  171. $clear->status = 3;
  172. $clear->save();
  173. } else {
  174. if (!empty($saleStr)) {
  175. $current = explode(',', $saleStr);
  176. $orderIds = array_merge($orderIds, $current);
  177. }
  178. }
  179. }
  180. if (!empty($orderIds) && in_array($id, $orderIds)) {
  181. util::fail('此客户有结账单未完成');
  182. }
  183. }
  184. $addTime = $order->addTime ?? '';
  185. $current = time();
  186. if ((strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  187. util::fail('历史订单,不能再发起退款');
  188. }
  189. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  190. util::fail('历史订单,不能发起退款哦');
  191. }
  192. //退款类型 1退货并退款 2 仅退款
  193. $refundType = $post['refundType'] ?? 1;
  194. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  195. //花材列表结构
  196. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}] productId 花材id num 退货数 unitType 大小单位0大1小 unitPrice 售价 unitName单位名称
  197. $productJson = $post['product'] ?? '';
  198. if (empty($productJson)) {
  199. util::fail('请选择花材');
  200. }
  201. $productList = json_decode($productJson, true);
  202. if (!is_array($productList)) {
  203. util::fail('请选择花材哦');
  204. }
  205. $post['product'] = $productList;
  206. } else {
  207. //仅退款花材直接设置为空
  208. $post['product'] = [];
  209. }
  210. $post['price'] = $post['price'] ?? 0;
  211. if ($post['price'] <= 0) {
  212. util::fail("退款金额不能小于0");
  213. }
  214. if (bccomp($post['price'], $order['realPrice'], 2) == 1) {
  215. util::fail("退款金额超过订单金额");
  216. }
  217. $post['shopId'] = $this->shopId;
  218. $post['sjId'] = $this->sjId;
  219. $post['shopAdminId'] = $this->shopAdminId;
  220. $post['mainId'] = $this->mainId;
  221. $shopAdmin = $this->shopAdmin;
  222. $adminName = $shopAdmin['name'] ?? '';
  223. $post['shopAdminName'] = $adminName;
  224. $respond = OrderService::refund($id, $post);
  225. $transaction->commit();
  226. util::success($respond);
  227. } catch (\Exception $exception) {
  228. $transaction->rollBack();
  229. Yii::info("退款出错了,报错信息:" . $exception->getMessage());
  230. util::fail('操作失败');
  231. }
  232. }
  233. }