RefundOrderService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. // 隔天冲销允许已结账单;当天售后仍拦截结账
  54. $sameDay = intval($refund->sameDay ?? RefundOrderClass::SAME_DAY_YES);
  55. if ($sameDay === RefundOrderClass::SAME_DAY_YES && isset($order->clearId) && !empty($order->clearId)) {
  56. util::fail('订单已结账,请驳回,并在线下完成售后');
  57. }
  58. $currentOrderId = $order->id;
  59. $addTime = $order->addTime ?? '';
  60. $current = time();
  61. if ($order->debt != 1 && (strtotime($addTime) + 3 * 30 * 24 * 60 * 60) < $current) {
  62. util::fail('历史订单,不能再发起退款');
  63. }
  64. if (strtotime($addTime) <= strtotime('2022-03-30 00:00:00')) {
  65. util::fail('历史订单,不能发起退款哦');
  66. }
  67. // 隔天冲销不减 actPrice,上限用 actPrice - 已隔天累计;当天仍对比 realPrice
  68. if ($sameDay === RefundOrderClass::SAME_DAY_NO) {
  69. $remainNext = bcsub(
  70. (string)($order->actPrice ?? '0'),
  71. (string)($order->nextDayTkPrice ?? '0'),
  72. 2
  73. );
  74. if (bccomp((string)$refund->refundPrice, $remainNext, 2) === 1) {
  75. util::fail('冲销金额超过可冲销余额');
  76. }
  77. } elseif (bccomp($refund->refundPrice, $order->realPrice, 2) == 1) {
  78. util::fail("退款金额超过订单金额");
  79. }
  80. $refund->passTime = date("Y-m-d H:i:s");
  81. $refund->save();
  82. $customId = $order->customId ?? 0;
  83. $clearList = PurchaseClearClass::getAllByCondition(['customId' => $customId, 'status' => 1], null, '*', null, true);
  84. if (!empty($clearList)) {
  85. $now = date("Y-m-d H:i:s");
  86. foreach ($clearList as $clear) {
  87. $deadline = $clear->deadline ?? '';
  88. if (!empty($deadline) && $deadline < $now) {
  89. $clear->status = 3;
  90. $clear->remark = "账单过期,已自动取消!!参数:{$deadline} {$now}";
  91. $clear->save();
  92. OrderCgClearClass::markExpireByClearId($clear->id);
  93. }
  94. }
  95. $currentClearId = OrderCgClearClass::getPendingClearIdByOrderId($currentOrderId, $customId);
  96. // 隔天冲销不要求先取消结账单
  97. if ($currentClearId > 0 && $sameDay === RefundOrderClass::SAME_DAY_YES) {
  98. //有结账单要取消才能售后
  99. util::success(['clearId' => $currentClearId, 'error' => 'hasUnClearOrder']);
  100. }
  101. }
  102. $relateOrderSn = $refund->relateOrderSn ?? '';
  103. if (empty($relateOrderSn)) {
  104. util::fail('没有订单信息');
  105. }
  106. $order = OrderClass::getByCondition(['orderSn' => $relateOrderSn], true);
  107. if (empty($order)) {
  108. util::fail('没有订单信息哦');
  109. }
  110. RefundOrderClass::passRefund($refund, $order);
  111. $cgRefundId = $refund->cgRefundId ?? 0;
  112. $cgRefund = CgRefundClass::getById($cgRefundId, true);
  113. if (empty($cgRefund)) {
  114. util::fail('信息缺失哦');
  115. }
  116. $cgId = $cgRefund->cgId ?? 0;
  117. $cg = PurchaseClass::getById($cgId, true);
  118. if (empty($cg)) {
  119. util::fail('采购信息缺失');
  120. }
  121. CgRefundClass::passRefund($cgRefund, $cg);
  122. }
  123. //新增退款
  124. public static function addRefund($post, $order)
  125. {
  126. $refundPrice = $post['price'] ?? 0;
  127. if ($refundPrice <= 0) {
  128. util::fail('请填写退款金额');
  129. }
  130. $cause = $post['cause'] ?? 0;
  131. $data = [];
  132. $data['status'] = RefundOrderClass::STATUS_UN_COMPLETE;
  133. $orderSn = orderSn::getGhsRefundSn();
  134. $data['orderSn'] = $orderSn;
  135. $data['relateOrderSn'] = $order->orderSn ?? '';
  136. $sjId = $post['sjId'] ?? 0;
  137. $refundType = $post['refundType'] ?? 0;
  138. $shopId = $post['shopId'] ?? 0;
  139. $mainId = $post['mainId'] ?? 0;
  140. $data['sjId'] = $sjId;
  141. $data['shopId'] = $shopId;
  142. $data['mainId'] = $mainId;
  143. $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
  144. $data['shopAdminName'] = $post['shopAdminName'] ?? '';
  145. $data['cause'] = $cause;
  146. $refundSendCost = isset($post['refundSendCost']) && is_numeric($post['refundSendCost']) ? $post['refundSendCost'] : 0;
  147. $refundPackCost = isset($post['refundPackCost']) && is_numeric($post['refundPackCost']) ? $post['refundPackCost'] : 0;
  148. $data['refundSendCost'] = $refundSendCost;
  149. $data['refundPackCost'] = $refundPackCost;
  150. $order->refundSendCost = bcadd($refundSendCost, $order->refundSendCost, 2);
  151. $order->refundPackCost = bcadd($refundPackCost, $order->refundPackCost, 2);
  152. $order->refundLog = 1;
  153. $order->save();
  154. $postProduct = $post['product'] ?? '';
  155. if ($refundType == RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  156. //花材列表结构
  157. // [{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎'}]
  158. if (empty($postProduct)) {
  159. util::fail('没有退款花材');
  160. }
  161. $saleOrderSn = $order->orderSn ?? '';
  162. $orderItemData = OrderItemClass::getAllByCondition(['orderSn' => $saleOrderSn], null, '*', 'productId', true);
  163. if (empty($orderItemData)) {
  164. util::fail('没有找到花材');
  165. }
  166. $originPrice = 0;
  167. foreach ($postProduct as $currentProduct) {
  168. $id = $currentProduct['productId'] ?? 0;
  169. if (isset($orderItemData[$id]) == false) {
  170. util::fail('没有找到退款的花材');
  171. }
  172. $currentInfo = $orderItemData[$id];
  173. $name = $currentInfo->name;
  174. $ptItemId = $currentInfo->itemId;
  175. $ratio = $currentInfo->ratio;
  176. $itemCost = $currentInfo->cost;
  177. $orderItemId = $currentInfo->id;
  178. $num = $currentProduct['num'] ?? 0;
  179. $xhUnitName = $currentProduct['unitName'] ?? '';
  180. $xhUnitPrice = $currentProduct['unitPrice'] ?? '';
  181. $unitType = $currentProduct['unitType'] ?? 0;
  182. $xhPrice = bcmul($num, $xhUnitPrice, 2);
  183. $originPrice =bcadd($originPrice,$xhPrice,2);
  184. $itemNum = $num;
  185. if ($unitType == dict::getDict('unitType', 'small')) {
  186. $itemNum = bcdiv($num, $ratio, 2);
  187. }
  188. $thisItemData = [
  189. 'sjId' => $sjId,
  190. 'shopId' => $shopId,
  191. 'orderSn' => $orderSn,
  192. 'productId' => $id,
  193. 'name' => $name,
  194. 'itemId' => $ptItemId,
  195. 'itemCost' => $itemCost,
  196. 'itemNum' => $itemNum,
  197. 'xhNum' => $num,
  198. 'xhUnitName' => $xhUnitName,
  199. 'xhUnitType' => $unitType,
  200. 'xhUnitPrice' => $xhUnitPrice,
  201. 'xhPrice' => $xhPrice,
  202. 'mainId' => $mainId,
  203. 'orderItemId' => $orderItemId,
  204. 'cause' => $cause,
  205. ];
  206. RefundOrderItemClass::addData($thisItemData, $order);
  207. }
  208. if($refundPrice < $originPrice){
  209. //util::fail('退款金额要等于或大于花材金额总合');
  210. }
  211. }
  212. $data['customId'] = $order->customId ?? 0;
  213. $data['imgList'] = $post['imgList'] ?? '';
  214. $data['remark'] = $post['remark'] ?? '';
  215. $data['refundPrice'] = $refundPrice;
  216. $data['refundType'] = $post['refundType'] ?? 0;
  217. $data['status'] = 0;
  218. $data['customName'] = $order->customName??'';
  219. $data['customNamePy'] = $order->customNamePy??'';
  220. $data['customMobile'] = $order->customMobile??'';
  221. $data['orderTime'] = $order->payTime??'';
  222. // 隔天冲销:sameDay=0 + fundType;已结账单强制冲销路径(即使当天)
  223. $sameDay = intval($post['sameDay'] ?? RefundOrderClass::SAME_DAY_YES);
  224. if (NextDayRefundService::mustUseNextDay($order)) {
  225. $sameDay = RefundOrderClass::SAME_DAY_NO;
  226. }
  227. if ($sameDay === RefundOrderClass::SAME_DAY_NO) {
  228. $check = NextDayRefundService::checkEligible($order);
  229. if (empty($check['ok'])) {
  230. util::fail($check['reason'] ?? '当前订单不可隔天冲销');
  231. }
  232. $fundType = NextDayRefundService::resolveFundType($post['fundType'] ?? 0, $order, true);
  233. $data['sameDay'] = RefundOrderClass::SAME_DAY_NO;
  234. $data['fundType'] = $fundType;
  235. } else {
  236. $data['sameDay'] = RefundOrderClass::SAME_DAY_YES;
  237. $data['fundType'] = NextDayRefundService::FUND_UNUSED;
  238. }
  239. $refund = RefundOrderClass::add($data, true);
  240. return $refund;
  241. }
  242. /**
  243. * 无原单退款(工作台「退款」):写 xhRefund.sameDay=0,relateOrderSn 为空;
  244. * 选花材后立即通过——退货加库存、返余额/仅记账,不写采购侧镜像单。
  245. * @param array $post customId/price/product/fundType/refundType/...
  246. * @return array 结果页/海报所需字段
  247. */
  248. public static function createFreeRefund($post)
  249. {
  250. $customId = intval($post['customId'] ?? 0);
  251. if ($customId <= 0) {
  252. util::fail('请选择客户');
  253. }
  254. $custom = CustomClass::getLockById($customId);
  255. if (empty($custom)) {
  256. util::fail('没有找到客户');
  257. }
  258. $mainId = intval($post['mainId'] ?? 0);
  259. if ($mainId > 0 && intval($custom->mainId ?? 0) !== $mainId) {
  260. util::fail('客户不属于当前门店');
  261. }
  262. $refundPrice = bcadd((string)($post['price'] ?? '0'), '0', 2);
  263. if (bccomp($refundPrice, '0', 2) <= 0) {
  264. util::fail('请填写退款金额');
  265. }
  266. $refundType = intval($post['refundType'] ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
  267. $fundType = NextDayRefundService::resolveFundType($post['fundType'] ?? 0, null, false);
  268. $sjId = intval($post['sjId'] ?? 0);
  269. $shopId = intval($post['shopId'] ?? 0);
  270. $orderSn = orderSn::getGhsRefundSn();
  271. $data = [
  272. 'status' => RefundOrderClass::STATUS_UN_COMPLETE,
  273. 'orderSn' => $orderSn,
  274. 'relateOrderSn' => '',
  275. 'sjId' => $sjId,
  276. 'shopId' => $shopId,
  277. 'mainId' => $mainId,
  278. 'shopAdminId' => $post['shopAdminId'] ?? 0,
  279. 'shopAdminName' => $post['shopAdminName'] ?? '',
  280. 'cause' => $post['cause'] ?? 0,
  281. 'refundSendCost' => 0,
  282. 'refundPackCost' => 0,
  283. 'customId' => $customId,
  284. 'customName' => $custom->name ?? '',
  285. 'customNamePy' => $custom->namePy ?? '',
  286. 'customMobile' => $custom->mobile ?? '',
  287. 'orderTime' => date('Y-m-d H:i:s'),
  288. 'imgList' => $post['imgList'] ?? '',
  289. 'remark' => $post['remark'] ?? '',
  290. 'refundPrice' => $refundPrice,
  291. 'refundType' => $refundType,
  292. 'sameDay' => RefundOrderClass::SAME_DAY_NO,
  293. 'fundType' => $fundType,
  294. ];
  295. $postProduct = $post['product'] ?? [];
  296. if ($refundType === RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  297. if (empty($postProduct) || !is_array($postProduct)) {
  298. util::fail('请选择花材');
  299. }
  300. }
  301. $refund = RefundOrderClass::add($data, true);
  302. // 无原单明细:按商品主档写售后行,orderItemId=0
  303. if ($refundType === RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
  304. foreach ($postProduct as $currentProduct) {
  305. $productId = intval($currentProduct['productId'] ?? 0);
  306. if ($productId <= 0) {
  307. util::fail('花材参数错误');
  308. }
  309. $product = ProductClass::getById($productId, true);
  310. if (empty($product) || intval($product->mainId ?? 0) !== $mainId) {
  311. util::fail('没有找到退款花材');
  312. }
  313. $num = bcadd((string)($currentProduct['num'] ?? '0'), '0', 2);
  314. if (bccomp($num, '0', 2) <= 0) {
  315. util::fail(($product->name ?? '花材') . '数量必须大于0');
  316. }
  317. $unitType = intval($currentProduct['unitType'] ?? 0);
  318. $xhUnitPrice = bcadd((string)($currentProduct['unitPrice'] ?? '0'), '0', 2);
  319. $xhUnitName = $currentProduct['unitName'] ?? '';
  320. if ($xhUnitName === '') {
  321. $xhUnitName = $unitType === 1
  322. ? ($product->smallUnit ?? '支')
  323. : ($product->bigUnit ?? '扎');
  324. }
  325. $ratio = bcadd((string)($product->ratio ?? '1'), '0', 2);
  326. if (bccomp($ratio, '0', 2) <= 0) {
  327. $ratio = '1';
  328. }
  329. $itemNum = $num;
  330. if ($unitType == dict::getDict('unitType', 'small')) {
  331. $itemNum = bcdiv($num, $ratio, 2);
  332. }
  333. $xhPrice = bcmul($num, $xhUnitPrice, 2);
  334. $thisItemData = [
  335. 'sjId' => $sjId,
  336. 'shopId' => $shopId,
  337. 'orderSn' => $orderSn,
  338. 'productId' => $productId,
  339. 'name' => $product->name ?? '',
  340. 'itemId' => $product->itemId ?? 0,
  341. 'itemCost' => $product->avCost ?? ($product->cost ?? 0),
  342. 'itemNum' => $itemNum,
  343. 'xhNum' => $num,
  344. 'xhUnitName' => $xhUnitName,
  345. 'xhUnitType' => $unitType,
  346. 'xhUnitPrice' => $xhUnitPrice,
  347. 'xhPrice' => $xhPrice,
  348. 'mainId' => $mainId,
  349. 'orderItemId' => 0,
  350. 'cause' => $post['cause'] ?? 0,
  351. ];
  352. RefundOrderItemClass::addData($thisItemData, null);
  353. }
  354. }
  355. NextDayRefundService::passFreeRefund($refund);
  356. $refund = RefundOrderClass::getById($refund->id, true);
  357. $custom = CustomClass::getById($customId, true);
  358. return [
  359. 'id' => intval($refund->id ?? 0),
  360. 'orderSn' => $refund->orderSn ?? '',
  361. 'sameDay' => RefundOrderClass::SAME_DAY_NO,
  362. 'fundType' => intval($refund->fundType ?? $fundType),
  363. 'refundPrice' => $refund->refundPrice ?? $refundPrice,
  364. 'customId' => $customId,
  365. 'customName' => $custom->name ?? ($refund->customName ?? ''),
  366. 'customBalance' => bcadd((string)($custom->balance ?? '0'), '0', 2),
  367. 'orderId' => 0,
  368. 'nextDayTkPrice' => '0.00',
  369. ];
  370. }
  371. }