RefundController.php 8.8 KB

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