RefundOrderService.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. namespace bizGhs\order\services;
  3. use biz\hb\classes\HbClass;
  4. use biz\shop\classes\ShopCapitalClass;
  5. use biz\shop\classes\ShopClass;
  6. use biz\stat\classes\StatItemClass;
  7. use biz\stat\classes\StatKhCgClass;
  8. use biz\stat\classes\StatOutClass;
  9. use biz\stat\classes\StatRefundClass;
  10. use bizGhs\base\services\BaseService;
  11. use bizGhs\custom\classes\CustomClass;
  12. use bizGhs\clear\classes\OrderCgClearClass;
  13. use bizGhs\order\classes\OrderClass;
  14. use bizGhs\order\classes\OrderItemClass;
  15. use bizGhs\order\classes\RefundOrderClass;
  16. use bizGhs\order\classes\RefundOrderItemClass;
  17. use bizGhs\order\services\NextDayRefundService;
  18. use bizGhs\shop\classes\MainClass;
  19. use bizGhs\shop\classes\ShopMoneyClass;
  20. use bizGhs\stock\classes\StockRecordClass;
  21. use bizHd\cg\classes\CgRefundClass;
  22. use bizHd\purchase\classes\PurchaseClass;
  23. use bizHd\purchase\classes\PurchaseClearClass;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\orderSn;
  27. use common\components\util;
  28. use bizGhs\product\classes\ProductClass;
  29. use Yii;
  30. class RefundOrderService extends BaseService
  31. {
  32. public static $baseFile = '\bizGhs\order\classes\RefundOrderClass';
  33. //审核通过 ssh 20230811
  34. public static function passRefund($refund)
  35. {
  36. $relateOrderSn = $refund->relateOrderSn ?? '';
  37. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  38. if (empty($order)) {
  39. util::fail('没有找到订单信息');
  40. }
  41. if ($order->status == OrderClass::ORDER_STATUS_UN_PAY) {
  42. util::fail("待付款订单,不能申请售后");
  43. }
  44. if ($order->status == OrderClass::ORDER_STATUS_UN_SEND) {
  45. util::fail("待配送订单,不能申请售后");
  46. }
  47. if ($order->status == OrderClass::ORDER_STATUS_SENDING) {
  48. util::fail("配送中订单,不能申请售后");
  49. }
  50. if ($order->status == OrderClass::ORDER_STATUS_CANCEL) {
  51. util::fail("已取消订单,不能申请售后");
  52. }
  53. // 无支付时间不能售后;仅用 payTime 判是否转隔天
  54. NextDayRefundService::assertCanRefund($order);
  55. // 审核时若已非当天或已结清:自动转为隔天售后(不再要求驳回)
  56. $sameDay = intval($refund->sameDay ?? RefundOrderClass::SAME_DAY_YES);
  57. if ($sameDay === RefundOrderClass::SAME_DAY_YES && NextDayRefundService::mustUseNextDay($order)) {
  58. self::convertRefundToNextDay($refund, $order);
  59. $sameDay = RefundOrderClass::SAME_DAY_NO;
  60. }
  61. $currentOrderId = $order->id;
  62. $addTime = $order->addTime ?? '';
  63. $current = time();
  64. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  65. util::fail('历史订单,不能再发起退款');
  66. }
  67. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  68. util::fail('历史订单,不能发起退款哦');
  69. }
  70. // 隔天冲销不减 actPrice,上限用 actPrice - 已隔天累计;当天仍对比 realPrice
  71. if ($sameDay === RefundOrderClass::SAME_DAY_NO) {
  72. $remainNext = bcsub(
  73. (string)($order->actPrice ?? '0'),
  74. (string)($order->nextDayTkPrice ?? '0'),
  75. 2
  76. );
  77. if (bccomp((string)$refund->refundPrice, $remainNext, 2) === 1) {
  78. util::fail('冲销金额超过可冲销余额');
  79. }
  80. } elseif (bccomp($refund->refundPrice, $order->realPrice, 2) == 1) {
  81. util::fail("退款金额超过订单金额");
  82. }
  83. $refund->passTime = date("Y-m-d H:i:s");
  84. $refund->save();
  85. $customId = $order->customId ?? 0;
  86. $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true);
  87. if (!empty($clearList)) {
  88. $now = date("Y-m-d H:i:s");
  89. foreach ($clearList as $clear) {
  90. $deadline = $clear->deadline ?? '';
  91. if (!empty($deadline) && $deadline < $now) {
  92. $clear->status = 3;
  93. $clear->remark = "账单过期,已自动取消!!参数:{$deadline} {$now}";
  94. $clear->save();
  95. OrderCgClearClass::markExpireByClearId($clear->id);
  96. }
  97. }
  98. $currentClearId = OrderCgClearClass::getPendingClearIdByOrderId($currentOrderId, $customId);
  99. // 隔天冲销不要求先取消结账单
  100. if ($currentClearId > 0 && $sameDay === RefundOrderClass::SAME_DAY_YES) {
  101. //有结账单要取消才能售后
  102. util::success(['clearId' => $currentClearId, 'error' => 'hasUnClearOrder']);
  103. }
  104. }
  105. $relateOrderSn = $refund->relateOrderSn ?? '';
  106. if (empty($relateOrderSn)) {
  107. util::fail('没有订单信息');
  108. }
  109. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  110. if (empty($order)) {
  111. util::fail('没有订单信息哦');
  112. }
  113. RefundOrderClass::passRefund($refund, $order);
  114. $cgRefundId = $refund->cgRefundId ?? 0;
  115. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  116. if (empty($cgRefund)) {
  117. util::fail('信息缺失哦');
  118. }
  119. $cgId = $cgRefund->cgId ?? 0;
  120. $cg = PurchaseClass::getById($cgId, true);
  121. if (empty($cg)) {
  122. util::fail('采购信息缺失');
  123. }
  124. CgRefundClass::passRefund($cgRefund, $cg);
  125. }
  126. /**
  127. * 将当天售后单转为隔天冲销类型(审核跨日/已结清时调用)
  128. * 同步写销售售后单与采购售后单的 sameDay、fundType;
  129. * 花店申请无资金弹窗时:线上默认原路,其它默认返余额。
  130. */
  131. public static function convertRefundToNextDay($refund, $order)
  132. {
  133. $fundType = intval($refund->fundType ?? 0);
  134. if ($fundType === NextDayRefundService::FUND_UNUSED) {
  135. $fundType = NextDayRefundService::defaultNextDayFundType($order);
  136. }
  137. $fundType = NextDayRefundService::resolveFundType($fundType, $order, true);
  138. $refund->sameDay = RefundOrderClass::SAME_DAY_NO;
  139. $refund->fundType = $fundType;
  140. $refund->save(false, ['sameDay', 'fundType']);
  141. $cgRefundId = intval($refund->cgRefundId ?? 0);
  142. if ($cgRefundId > 0) {
  143. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  144. if (!empty($cgRefund)) {
  145. $cgRefund->sameDay = CgRefundClass::SAME_DAY_NO;
  146. $cgRefund->fundType = $fundType;
  147. $cgRefund->save(false, ['sameDay', 'fundType']);
  148. }
  149. }
  150. }
  151. //新增退款
  152. public static function addRefund($post, $order)
  153. {
  154. $refundPrice = $post['price'] ?? 0;
  155. if ($refundPrice <= 0) {
  156. util::fail('请填写退款金额');
  157. }
  158. $cause = $post['cause'] ?? 0;
  159. $data = [];
  160. $data['status'] = RefundOrderClass::STATUS_UN_COMPLETE;
  161. $orderSn = orderSn::getGhsRefundSn();
  162. $data['orderSn'] = $orderSn;
  163. $data['relateOrderSn'] = $order->orderSn ?? '';
  164. $sjId = $post['sjId'] ?? 0;
  165. $refundType = $post['refundType'] ?? 0;
  166. $shopId = $post['shopId'] ?? 0;
  167. $mainId = $post['mainId'] ?? 0;
  168. $data['sjId'] = $sjId;
  169. $data['shopId'] = $shopId;
  170. $data['mainId'] = $mainId;
  171. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  172. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  173. $data['cause'] = $cause;
  174. $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
  175. $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
  176. $data['refundSendCost'] = $refundSendCost;
  177. $data['refundPackCost'] = $refundPackCost;
  178. $order->refundSendCost = bcadd($refundSendCost, $order->refundSendCost, 2);
  179. $order->refundPackCost = bcadd($refundPackCost, $order->refundPackCost, 2);
  180. $order->refundLog = 1;
  181. $order->save();
  182. $postProduct = $post['product'] ?? '';
  183. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  184. //花材列表结构
  185. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  186. if (empty($postProduct)) {
  187. util::fail('没有退款花材');
  188. }
  189. $saleOrderSn = $order->orderSn ?? '';
  190. $orderItemData = OrderItemClass::getAllByCondition(['orderSn' => $saleOrderSn], null, '*', 'productId', true);
  191. if (empty($orderItemData)) {
  192. util::fail('没有找到花材');
  193. }
  194. $originPrice = 0;
  195. foreach ($postProduct as $currentProduct) {
  196. $id = $currentProduct['productId'] ?? 0;
  197. if (isset($orderItemData[$id]) == false) {
  198. util::fail('没有找到退款的花材');
  199. }
  200. $currentInfo = $orderItemData[$id];
  201. $name = $currentInfo->name;
  202. $ptItemId = $currentInfo->itemId;
  203. $ratio = $currentInfo->ratio;
  204. $itemCost = $currentInfo->cost;
  205. $orderItemId = $currentInfo->id;
  206. $num = $currentProduct['num'] ?? 0;
  207. $xhUnitName = $currentProduct['unitName'] ?? '';
  208. $xhUnitPrice = $currentProduct['unitPrice'] ?? '';
  209. $unitType = $currentProduct['unitType'] ?? 0;
  210. $xhPrice = bcmul($num, $xhUnitPrice, 2);
  211. $originPrice =bcadd($originPrice,$xhPrice,2);
  212. $itemNum = $num;
  213. if ($unitType == dict::getDict('unitType', 'small')) {
  214. $itemNum = bcdiv($num, $ratio, 2);
  215. }
  216. $thisItemData = [
  217. 'sjId' => $sjId,
  218. 'shopId' => $shopId,
  219. 'orderSn' => $orderSn,
  220. 'productId' => $id,
  221. 'name' => $name,
  222. 'itemId' => $ptItemId,
  223. 'itemCost' => $itemCost,
  224. 'itemNum' => $itemNum,
  225. 'xhNum' => $num,
  226. 'xhUnitName' => $xhUnitName,
  227. 'xhUnitType' => $unitType,
  228. 'xhUnitPrice' => $xhUnitPrice,
  229. 'xhPrice' => $xhPrice,
  230. 'mainId' => $mainId,
  231. 'orderItemId' => $orderItemId,
  232. 'cause' => $cause,
  233. ];
  234. RefundOrderItemClass::addData($thisItemData, $order);
  235. }
  236. if($refundPrice < $originPrice){
  237. //util::fail('退款金额要等于或大于花材金额总合');
  238. }
  239. }
  240. $data['customId'] = $order->customId ?? 0;
  241. $data['imgList'] = $post['imgList'] ?? '';
  242. $data['remark'] = $post['remark'] ?? '';
  243. $data['refundPrice'] = $refundPrice;
  244. $data['refundType'] = $post['refundType'] ?? 0;
  245. $data['status'] = 0;
  246. $data['customName'] = $order->customName??'';
  247. $data['customNamePy'] = $order->customNamePy??'';
  248. $data['customMobile'] = $order->customMobile??'';
  249. $data['orderTime'] = $order->payTime??'';
  250. // 无支付时间不能售后
  251. NextDayRefundService::assertCanRefund($order);
  252. // 隔天冲销:sameDay=0 + fundType;非当天/已结清强制冲销(即使创建时未传 sameDay)
  253. $sameDay = intval($post['sameDay'] ?? RefundOrderClass::SAME_DAY_YES);
  254. if (NextDayRefundService::mustUseNextDay($order)) {
  255. $sameDay = RefundOrderClass::SAME_DAY_NO;
  256. }
  257. if ($sameDay === RefundOrderClass::SAME_DAY_NO) {
  258. $check = NextDayRefundService::checkEligible($order);
  259. if (empty($check['ok'])) {
  260. util::fail($check['reason'] ?? '当前订单不可隔天冲销');
  261. }
  262. $fundTypeInput = intval($post['fundType'] ?? 0);
  263. // 未选手选资金时:线上默认原路,其它默认返余额(商家弹窗会覆盖)
  264. if ($fundTypeInput === NextDayRefundService::FUND_UNUSED) {
  265. $fundTypeInput = NextDayRefundService::defaultNextDayFundType($order);
  266. }
  267. $fundType = NextDayRefundService::resolveFundType($fundTypeInput, $order, true);
  268. $data['sameDay'] = RefundOrderClass::SAME_DAY_NO;
  269. $data['fundType'] = $fundType;
  270. } else {
  271. $data['sameDay'] = RefundOrderClass::SAME_DAY_YES;
  272. $data['fundType'] = NextDayRefundService::FUND_UNUSED;
  273. }
  274. $refund = RefundOrderClass::add($data, true);
  275. return $refund;
  276. }
  277. /**
  278. * 无原单退款(工作台「退款」):写 xhRefund.sameDay=0,relateOrderSn 为空;
  279. * 选花材后立即通过——退货加库存、返余额/仅记账,不写采购侧镜像单。
  280. * @param array $post customId/price/product/fundType/refundType/...
  281. * @return array 结果页/海报所需字段
  282. */
  283. public static function createFreeRefund($post)
  284. {
  285. $customId = intval($post['customId'] ?? 0);
  286. if ($customId <= 0) {
  287. util::fail('请选择客户');
  288. }
  289. $custom = CustomClass::getLockById($customId);
  290. if (empty($custom)) {
  291. util::fail('没有找到客户');
  292. }
  293. $mainId = intval($post['mainId'] ?? 0);
  294. if ($mainId > 0 && intval($custom->ownMainId ?? 0) !== $mainId) {
  295. util::fail('客户不属于当前门店');
  296. }
  297. $refundPrice = bcadd((string)($post['price'] ?? '0'), '0', 2);
  298. if (bccomp($refundPrice, '0', 2) <= 0) {
  299. util::fail('请填写退款金额');
  300. }
  301. // 前端 returnWay:0库存不退→仅退款;1库存退回→退货退款
  302. $returnWay = intval($post['returnWay'] ?? -1);
  303. if ($returnWay === 0) {
  304. $refundType = RefundOrderClass::REFUND_TYPE_MONEY;
  305. } elseif ($returnWay === 1) {
  306. $refundType = RefundOrderClass::REFUND_TYPE_MONEY_GOOD;
  307. } else {
  308. $refundType = intval($post['refundType'] ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
  309. }
  310. // 前端 toBalance:1返充余额→fundType2;0否→仅记账3
  311. $toBalance = intval($post['toBalance'] ?? -1);
  312. if ($toBalance === 1) {
  313. $post['fundType'] = NextDayRefundService::FUND_BALANCE;
  314. } elseif ($toBalance === 0) {
  315. $post['fundType'] = NextDayRefundService::FUND_NONE;
  316. }
  317. $fundType = NextDayRefundService::resolveFundType($post['fundType'] ?? 0, null, false);
  318. $sjId = intval($post['sjId'] ?? 0);
  319. $shopId = intval($post['shopId'] ?? 0);
  320. $orderSn = orderSn::getGhsRefundSn();
  321. // xhRefund 暂无 payWay 字段:退款方式写入备注便于追溯
  322. $payWay = intval($post['payWay'] ?? -1);
  323. $payWayNameMap = [
  324. 0 => '线下微信',
  325. 1 => '线下支付宝',
  326. 4 => '现金',
  327. 5 => '银行卡',
  328. ];
  329. $remark = trim((string)($post['remark'] ?? ''));
  330. if (isset($payWayNameMap[$payWay])) {
  331. $payTag = '退款方式:' . $payWayNameMap[$payWay];
  332. $remark = $remark === '' ? $payTag : ($remark . ';' . $payTag);
  333. }
  334. $data = [
  335. 'status' => RefundOrderClass::STATUS_UN_COMPLETE,
  336. 'orderSn' => $orderSn,
  337. 'relateOrderSn' => '',
  338. 'sjId' => $sjId,
  339. 'shopId' => $shopId,
  340. 'mainId' => $mainId,
  341. 'shopAdminId' => $post['shopAdminId'] ?? 0,
  342. 'shopAdminName' => $post['shopAdminName'] ?? '',
  343. 'cause' => $post['cause'] ?? 0,
  344. 'refundSendCost' => 0,
  345. 'refundPackCost' => 0,
  346. 'customId' => $customId,
  347. 'customName' => $custom->name ?? '',
  348. 'customNamePy' => $custom->namePy ?? '',
  349. 'customMobile' => $custom->mobile ?? '',
  350. 'orderTime' => date('Y-m-d H:i:s'),
  351. 'imgList' => $post['imgList'] ?? '',
  352. 'remark' => $remark,
  353. 'refundPrice' => $refundPrice,
  354. 'refundType' => $refundType,
  355. 'sameDay' => RefundOrderClass::SAME_DAY_NO,
  356. 'fundType' => $fundType,
  357. ];
  358. $postProduct = $post['product'] ?? [];
  359. // 库存退回必须有花材;库存不退可只退金额,有花材时仍落明细便于对账
  360. if ($refundType === RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  361. if (empty($postProduct) || !is_array($postProduct)) {
  362. util::fail('请选择花材');
  363. }
  364. }
  365. $refund = RefundOrderClass::add($data, true);
  366. // 有花材则写售后行(orderItemId=0);是否回库由 passFreeRefund 按 refundType 决定
  367. if (!empty($postProduct) && is_array($postProduct)) {
  368. foreach ($postProduct as $currentProduct) {
  369. $productId = intval($currentProduct['productId'] ?? 0);
  370. if ($productId <= 0) {
  371. util::fail('花材参数错误');
  372. }
  373. $product = ProductClass::getById($productId, true);
  374. if (empty($product) || intval($product->mainId ?? 0) !== $mainId) {
  375. util::fail('没有找到退款花材');
  376. }
  377. $num = bcadd((string)($currentProduct['num'] ?? '0'), '0', 2);
  378. if (bccomp($num, '0', 2) <= 0) {
  379. util::fail(($product->name ?? '花材') . '数量必须大于0');
  380. }
  381. $unitType = intval($currentProduct['unitType'] ?? 0);
  382. $xhUnitPrice = bcadd((string)($currentProduct['unitPrice'] ?? '0'), '0', 2);
  383. $xhUnitName = $currentProduct['unitName'] ?? '';
  384. if ($xhUnitName === '') {
  385. $xhUnitName = $unitType === 1
  386. ? ($product->smallUnit ?? '支')
  387. : ($product->bigUnit ?? '扎');
  388. }
  389. $ratio = bcadd((string)($product->ratio ?? '1'), '0', 2);
  390. if (bccomp($ratio, '0', 2) <= 0) {
  391. $ratio = '1';
  392. }
  393. $itemNum = $num;
  394. if ($unitType == dict::getDict('unitType', 'small')) {
  395. $itemNum = bcdiv($num, $ratio, 2);
  396. }
  397. $xhPrice = bcmul($num, $xhUnitPrice, 2);
  398. $thisItemData = [
  399. 'sjId' => $sjId,
  400. 'shopId' => $shopId,
  401. 'orderSn' => $orderSn,
  402. 'productId' => $productId,
  403. 'name' => $product->name ?? '',
  404. 'itemId' => $product->itemId ?? 0,
  405. 'itemCost' => $product->avCost ?? ($product->cost ?? 0),
  406. 'itemNum' => $itemNum,
  407. 'xhNum' => $num,
  408. 'xhUnitName' => $xhUnitName,
  409. 'xhUnitType' => $unitType,
  410. 'xhUnitPrice' => $xhUnitPrice,
  411. 'xhPrice' => $xhPrice,
  412. 'mainId' => $mainId,
  413. 'orderItemId' => 0,
  414. 'cause' => $post['cause'] ?? 0,
  415. ];
  416. RefundOrderItemClass::addData($thisItemData, null);
  417. }
  418. }
  419. NextDayRefundService::passFreeRefund($refund);
  420. $refund = RefundOrderClass::getById($refund->id, true);
  421. $custom = CustomClass::getById($customId, true);
  422. return [
  423. 'id' => intval($refund->id ?? 0),
  424. 'orderSn' => $refund->orderSn ?? '',
  425. 'sameDay' => RefundOrderClass::SAME_DAY_NO,
  426. 'fundType' => intval($refund->fundType ?? $fundType),
  427. 'refundPrice' => $refund->refundPrice ?? $refundPrice,
  428. 'customId' => $customId,
  429. 'customName' => $custom->name ?? ($refund->customName ?? ''),
  430. 'customBalance' => bcadd((string)($custom->balance ?? '0'), '0', 2),
  431. 'orderId' => 0,
  432. 'nextDayTkPrice' => '0.00',
  433. ];
  434. }
  435. }