RefundController.php 9.9 KB

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