RefundController.php 11 KB

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