RefundController.php 10 KB

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