RefundController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <?php
  2. //退款退货
  3. namespace hd\controllers;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizHd\custom\classes\CustomClass;
  6. use bizHd\custom\classes\HdClass;
  7. use bizHd\hb\classes\HbClass;
  8. use bizHd\member\classes\MemberLevelClass;
  9. use bizHd\order\classes\OrderClass;
  10. use bizHd\order\services\OrderService;
  11. use bizHd\refund\classes\HdRefundClass;
  12. use bizHd\refund\classes\HdRefundGoodsClass;
  13. use bizHd\refund\classes\HdRefundItemClass;
  14. use bizHd\refund\services\HdRefundService;
  15. use Yii;
  16. use common\components\util;
  17. class RefundController extends BaseController
  18. {
  19. //取消的订单退钱,特殊情况的操作:商家取消订单时,客户同时付款成功了,造成的流程冲突,需要退钱 ssh 20250429
  20. public function actionRefundAmount()
  21. {
  22. $get = Yii::$app->request->get();
  23. $id = $get['id'] ?? '';
  24. $order = OrderClass::getById($id, true);
  25. if (empty($order)) {
  26. util::fail('没有找到订单');
  27. }
  28. if ($order->mainId != $this->mainId) {
  29. util::fail('不是你的订单');
  30. }
  31. $staff = $this->shopAdmin;
  32. if ($staff->mobile != '15280215347') {
  33. util::fail('请联系管理员操作');
  34. }
  35. $shop = $this->shop;
  36. $respond = HdRefundClass::onlyRefundAmount($shop, $order);
  37. $status = $respond['status'];
  38. $msg = $respond['msg'];
  39. util::success(['status' => $status], $msg);
  40. }
  41. //列表
  42. public function actionList()
  43. {
  44. $get = Yii::$app->request->get();
  45. $where = [];
  46. $where['mainId'] = $this->mainId;
  47. $orderSn = $get['orderSn'] ?? '';
  48. if (!empty($orderSn)) {
  49. $where['orderSn'] = $orderSn;
  50. }
  51. $list = HdRefundClass::getOrderList($where);
  52. util::success($list);
  53. }
  54. //退款单详情 ssh 20220427
  55. public function actionDetail()
  56. {
  57. $id = Yii::$app->request->get('id', 0);
  58. $refund = HdRefundClass::getById($id, true);
  59. HdRefundClass::valid($refund, $this->mainId);
  60. $refundSn = $refund->refundSn ?? '';
  61. $itemList = HdRefundItemClass::getItemListBySn($refundSn);
  62. $goodsList = HdRefundGoodsClass::getGoodsListBySn($refundSn);
  63. util::success(['info' => $refund, 'itemList' => $itemList, 'goodsList' => $goodsList]);
  64. }
  65. //退款 ssh 20220427
  66. public function actionCreateOrder()
  67. {
  68. $shopAdmin = $this->shopAdmin;
  69. if (!isset($shopAdmin['super']) || $shopAdmin['super'] != 1) {
  70. util::fail('超管才能操作退款');
  71. }
  72. $post = Yii::$app->request->post();
  73. $id = isset($post['id']) ? $post['id'] : 0;
  74. $version = $post['version'] ?? 1;
  75. if ($version == 1) {
  76. util::fail('请升级小程序');
  77. }
  78. $refundType = $post['refundType'] ?? 1;//1退货并退款2只退款
  79. //避免重复提交
  80. util::checkRepeatCommit($id, 10);
  81. $connection = Yii::$app->db;
  82. $transaction = $connection->beginTransaction();
  83. try {
  84. $order = OrderClass::getById($id, true);
  85. OrderClass::valid($order, $this->mainId);
  86. if ($order->payStatus == 0) {
  87. util::fail('订单没有付款');
  88. }
  89. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  90. util::fail("待付款订单无法退款");
  91. }
  92. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  93. util::fail("取消订单不能发起退款");
  94. }
  95. if ($order->fromType == 4) {
  96. util::fail('请在美团APP发起退货退款');
  97. }
  98. if ($order->debt != 1 && $order->addTime < '2023-10-17 00:00:00') {
  99. util::fail('2023年10月17日前订单无法申请退款');
  100. }
  101. if ($order->status != 4) {
  102. //util::fail('订单完成了才能售后');
  103. }
  104. $addTime = $order->addTime ?? '';
  105. $current = time();
  106. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  107. util::fail('历史订单,不能再发起退款');
  108. }
  109. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  110. util::fail('历史订单,不能发起退款哦');
  111. }
  112. if ($order->forward == 1) {
  113. util::fail('负数订单不能发起售后');
  114. }
  115. if ($refundType == 1) {
  116. //商品花材结构 property 0花束 1花材
  117. //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property:1}]
  118. $productJson = $post['product'] ?? '';
  119. if (empty($productJson)) {
  120. util::fail('请选择商品');
  121. }
  122. $productList = json_decode($productJson, true);
  123. if (!is_array($productList)) {
  124. util::fail('请选择商品哦');
  125. }
  126. $post['product'] = $productList;
  127. }
  128. $post['price'] = $post['price'] ?? 0;
  129. if ($post['price'] <= 0) {
  130. util::fail("退款金额不能小于0");
  131. }
  132. if (bccomp($post['price'], $order->orderPrice, 2) == 1) {
  133. util::fail("退款金额超过订单金额");
  134. }
  135. $post['shopId'] = $this->shopId;
  136. $post['sjId'] = $this->sjId;
  137. $post['shopAdminId'] = $this->shopAdminId;
  138. $post['mainId'] = $this->mainId;
  139. $shopAdmin = $this->shopAdmin;
  140. $adminName = $shopAdmin['name'] ?? '';
  141. $post['shopAdminName'] = $adminName;
  142. $main = $this->main;
  143. //判断是否开负数订单
  144. $ret = OrderClass::ifShPay($order, $main, $post['price']);
  145. $shAddPay = $ret['shAddPay'] ?? 0;
  146. if ($shAddPay == 1) {
  147. $customId = $order->customId ?? 0;
  148. $custom = CustomClass::getById($customId, true);
  149. if (empty($custom)) {
  150. util::fail('没有客户哈!');
  151. }
  152. //线下付款
  153. $hasPay = 1;
  154. $shopId = $order->shopId;
  155. $staffId = $this->shopAdminId;
  156. $staffName = $this->shopAdminName;
  157. $forwardStock = $refundType == 1 ? 0 : 1;
  158. $refundPrice = $post['price'] ?? 0;
  159. $hasItem = false;
  160. $hasBouquet = false;
  161. // property 0花束 1花材
  162. $orderProductList = [];
  163. foreach ($productList as $thisInfo) {
  164. $num = $thisInfo['num'] ?? 0;
  165. $unitType = $thisInfo['unitType'] ?? 0;
  166. $property = $thisInfo['property'] ?? 0;
  167. $unitPrice = $thisInfo['unitPrice'] ?? 0;
  168. $productId = $thisInfo['productId'] ?? 0;
  169. $orderProductList[] = [
  170. 'productId' => $productId,
  171. 'unitPrice' => $unitPrice,
  172. 'remark' => '',
  173. 'property' => $property,
  174. 'unitType' => $unitType,
  175. 'num' => $num,
  176. ];
  177. if ($property == 0) {
  178. $hasBouquet = true;
  179. }
  180. if ($property == 1) {
  181. $hasItem = true;
  182. }
  183. }
  184. $orderType = 0;
  185. if ($hasBouquet) {
  186. $orderType = 1;
  187. }
  188. if ($hasItem) {
  189. $orderType = 2;
  190. }
  191. if ($hasItem && $hasBouquet) {
  192. $orderType = 3;
  193. }
  194. $sjId = $order->sjId;
  195. $mainId = $order->mainId;
  196. $deadline = time() + 8640000;
  197. $hdId = $order->hdId;
  198. $hdName = $order->hdName;
  199. $customNamePy = $order->customNamePy;
  200. $customName = $order->customName;
  201. $orderData = [
  202. 'hasPay' => 1,
  203. 'payWay' => 0,
  204. 'shopId' => $shopId,
  205. 'customId' => $customId,
  206. 'customName' => $customName,
  207. 'customNamePy' => $customNamePy,
  208. 'product' => $orderProductList,
  209. 'needPrint' => 2,
  210. 'getStaffId' => $staffId,
  211. 'getStaffName' => $staffName,
  212. 'shopAdminId' => $staffId,
  213. 'shopAdminName' => $staffName,
  214. 'dealPrice' => 1,
  215. 'forward' => 1,
  216. 'forwardStock' => $forwardStock,
  217. 'modifyPrice' => $refundPrice,
  218. 'orderType' => $orderType,
  219. 'sjId' => $sjId,
  220. 'mainId' => $mainId,
  221. 'deadline' => $deadline,
  222. 'staffId' => $staffId,
  223. 'staffName' => $staffName,
  224. 'flowerNum' => 0,
  225. 'fromType' => 1,
  226. 'hdId' => $hdId,
  227. 'hdName' => $hdName,
  228. 'sendType' => 1,
  229. 'sendSide' => 2,
  230. 'labourCost' => 0,
  231. ];
  232. //走负数订单,多处调用这个方法,注意同步修改,搜索关键词 createHdNewOrder
  233. $returnOrder = OrderService::createHdOrder($orderData, $custom, $hasPay);
  234. $shop = $this->shop;
  235. $staff = $this->shopAdmin;
  236. //售后开付款单,库存要变化,关系要建立,多处需要同步修改 sh_pay_change ssh
  237. OrderClass::shPayChange($returnOrder, $shop, $staff, $order);
  238. $transaction->commit();
  239. $orderId = $returnOrder->id;
  240. util::success(['forward' => 1, 'orderId' => $orderId]);
  241. } else {
  242. //走正常售后
  243. HdRefundService::addRefund($post, $order);
  244. }
  245. //售后之后,累计消费、积分、成长值回退 -- 表:xhCustom -> buyAmount、integral、growth,xhHd -> expendAmount
  246. $custom = CustomClass::getLockById($order->customId);
  247. if (!empty($custom)) {
  248. $custom->buyAmount = bcsub($custom->buyAmount, $post['price'], 2);
  249. $customName = $custom->name ?? $order->customName ?? '';
  250. $orderSnText = $order->orderSn ?? '';
  251. $shopAdminName = $this->shopAdminName;
  252. $eventPrefix = $customName;
  253. if ($eventPrefix === '' && $orderSnText !== '') {
  254. $eventPrefix = '客户';
  255. }
  256. $custom->eventRemark = $eventPrefix . '售后' . $orderSnText;
  257. $custom->relateId = $order->id;
  258. $custom->relateType = 1; //关联零售订单
  259. $custom->staffId = $this->shopAdminId;
  260. $custom->staffName = $shopAdminName;
  261. $custom->mainId = $this->mainId;
  262. $custom->save();
  263. $hd = HdClass::getLockById($custom->hdId);
  264. if (!empty($hd)) {
  265. $hd->expendAmount = bcsub($hd->expendAmount, $post['price'], 2);
  266. $hd->save();
  267. }
  268. }
  269. //红包退款
  270. $refundHb = $post['refundHb'] ?? 0;
  271. $hbId = $order->hbId;
  272. if ($refundHb == 1 && $hbId > 0) { // 不使用 $hbId =! 0,因为要用负数来表示红包已取消
  273. $updatedOrder = OrderClass::getById($id, true, 'id, actPrice, hbId, hbAmount');
  274. if ($updatedOrder->actPrice <= 0.00) {
  275. hbClass::hbBack($updatedOrder);
  276. }
  277. }
  278. $transaction->commit();
  279. //制作单取消通知
  280. $shopId = $this->shopId ?? 0;
  281. $shopExt = ShopExtClass::getByCondition(['shopId' => $shopId], true);
  282. ShopExtClass::orderCancelRemind($shopExt, $order);
  283. util::complete();
  284. } catch (\Exception $exception) {
  285. $transaction->rollBack();
  286. Yii::info("出错了:" . $exception->getMessage());
  287. util::fail('操作失败');
  288. }
  289. util::complete();
  290. }
  291. public function actionAllRefund()
  292. {
  293. util::fail('功能开发中');
  294. }
  295. }