CgRefundClass.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  1. <?php
  2. namespace bizHd\cg\classes;
  3. use biz\shop\classes\ShopCapitalClass;
  4. use biz\shop\classes\ShopExtClass;
  5. use bizHd\ghs\classes\GhsClass;
  6. use bizHd\purchase\classes\PurchaseClass;
  7. use bizHd\purchase\classes\PurchaseItemClass;
  8. use bizHd\shop\classes\MainClass;
  9. use bizGhs\stock\classes\StockRecordClass;
  10. use bizHd\shop\classes\ShopClass;
  11. use bizHd\wx\classes\WxOpenClass;
  12. use common\components\dict;
  13. use common\components\noticeUtil;
  14. use common\components\orderSn;
  15. use common\components\util;
  16. use Yii;
  17. use bizGhs\base\classes\BaseClass;
  18. use bizGhs\order\traits\OrderTrait;
  19. use bizGhs\product\classes\ProductClass;
  20. use common\components\lakala\Lakala;
  21. class CgRefundClass extends BaseClass
  22. {
  23. use OrderTrait;
  24. public static $baseFile = '\bizHd\cg\models\CgRefund';
  25. const STATUS_UN_COMPLETE = 0;//待审核
  26. const STATUS_COMPLETE = 1;//已通过
  27. const STATUS_REJECT = 2;//已驳回
  28. const STATUS_CANCEL = 3;//已取消
  29. const REFUND_TYPE_MONEY_GOOD = 1; //退款退货
  30. const REFUND_TYPE_MONEY = 2; // 仅退款
  31. const SAME_DAY_YES = 1;
  32. const SAME_DAY_NO = 0;
  33. /**
  34. * 跨天售后原路退:走拉卡拉退款,不改采购单 actPrice
  35. * 优先读售后单资金快照 onlinePay/payWay,缺省回落采购原单
  36. */
  37. public static function nextDayOriginalOnlineRefund($cgRefund, $cg)
  38. {
  39. $onlinePay = $cgRefund->onlinePay ?? ($cg->onlinePay ?? 1);
  40. $payWay = $cgRefund->payWay ?? ($cg->payWay ?? 0);
  41. if ($onlinePay != dict::getDict('onlinePay', 'yes')) {
  42. util::fail('非在线支付无法原路退回');
  43. }
  44. if ($payWay != dict::getDict('payWay', 'wxPay') && $payWay != dict::getDict('payWay', 'alipay')) {
  45. util::fail('支付方式不支持原路退回');
  46. }
  47. $ghsId = $cg->ghsId ?? 0;
  48. $ghs = GhsClass::getById($ghsId, true);
  49. if (empty($ghs)) {
  50. util::fail('没有找到供货商');
  51. }
  52. $ghsShopId = $ghs->shopId ?? 0;
  53. $ghsShop = ShopClass::getById($ghsShopId, true);
  54. if (empty($ghsShop)) {
  55. util::fail('没有找到供货商的门店');
  56. }
  57. $refundPrice = bcadd((string)($cgRefund->refundPrice ?? '0'), '0', 2);
  58. $refundFee = bcmul($refundPrice, 100);
  59. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  60. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  61. $params = [
  62. 'appid' => 'OP00002119',
  63. 'serial_no' => '018b08cfddbd',
  64. 'merchant_no' => $ghsShop->lklSjNo,
  65. 'term_no' => $ghsShop->lklScanTermNo,
  66. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  67. 'lklCertificatePath' => $lklCertificatePath,
  68. ];
  69. $laResource = new Lakala($params);
  70. // 流水号策略暂维持售后单号;唯一性撞号问题另行处理
  71. $refundOrderSn = $cgRefund->orderSn ?? orderSn::getCgRefundSn();
  72. $aliParams = [
  73. 'refundSn' => $refundOrderSn,
  74. 'orderSn' => $cg->orderSn ?? '',
  75. 'refundAmount' => $refundFee,
  76. 'refundReason' => '订单售后,原路退回',
  77. 'thirdNo' => $cg->thirdNo ?? '',
  78. ];
  79. $response = $laResource->refund($aliParams);
  80. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  81. $errMsg = $response['msg'] ?? '';
  82. util::fail('退款失败:' . $errMsg);
  83. }
  84. $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
  85. $cgRefund->thirdRefundNo = $refundTradeNo;
  86. $cgRefund->save(false, ['thirdRefundNo']);
  87. }
  88. public static function onlyRefundMoney($ghsShop, $cg)
  89. {
  90. $refundOrderSn = orderSn::getCgRefundSn();
  91. $refundPrice = $cg->actPrice ?? 0;
  92. $orderSn = $cg->orderSn ?? '';
  93. $refundFee = bcmul($refundPrice, 100);
  94. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  95. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  96. $params = [
  97. 'appid' => 'OP00002119',
  98. 'serial_no' => '018b08cfddbd',
  99. 'merchant_no' => $ghsShop->lklSjNo,
  100. 'term_no' => $ghsShop->lklScanTermNo,
  101. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  102. 'lklCertificatePath' => $lklCertificatePath,
  103. ];
  104. $laResource = new Lakala($params);
  105. $refundReason = '';
  106. //退款用拉卡拉原单号比较稳定,用自己内部的单号,好像会出现找不到订单号的情况
  107. $thirdNo = $cg->thirdNo ?? '';
  108. $thisOrderSn = '';
  109. //竟然会出现thirdNo为空的情况
  110. if (!empty($thirdNo)) {
  111. $thisOrderSn = $orderSn;
  112. }
  113. $backParams = [
  114. 'refundSn' => $refundOrderSn,
  115. 'thirdNo' => $thirdNo,
  116. 'orderSn' => $thisOrderSn,
  117. 'refundAmount' => $refundFee,
  118. 'refundReason' => $refundReason,
  119. ];
  120. $response = $laResource->refund($backParams);
  121. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  122. $errMsg = $response['msg'] ?? '';
  123. noticeUtil::push("钱退回失败 {$orderSn} {$errMsg}", '15280215347');
  124. return ['status' => 0, 'msg' => $errMsg];
  125. } else {
  126. $ghsShopId = $ghsShop->id;
  127. ShopExtClass::orderCancelBackMoneyReport($ghsShopId);
  128. $sjName = $ghsShop->merchantName;
  129. $shopName = $ghsShop->shopName;
  130. $name = $shopName == '首店' ? $sjName : $sjName . '-' . $shopName;
  131. noticeUtil::push("钱已经原路退回 success {$orderSn} {$name}", '15280215347');
  132. return ['status' => 1, 'msg' => '退款成功'];
  133. }
  134. }
  135. //审核通过 ssh 20230811
  136. public static function passRefund($cgRefund, $cg)
  137. {
  138. if ($cgRefund->status == 1) {
  139. util::fail('已通过');
  140. }
  141. if ($cgRefund->status == 2) {
  142. util::fail('已驳回');
  143. }
  144. if ($cgRefund->status == 3) {
  145. util::fail('已取消');
  146. }
  147. $refundType = $cgRefund->refundType ?? 1;
  148. $refundSn = $cgRefund->orderSn ?? '';
  149. // 提前读取:退货累加 nextRefundNum 时要用
  150. $sameDay = intval($cgRefund->sameDay ?? self::SAME_DAY_YES);
  151. $cgRefund->status = 1;
  152. $cgRefund->save();
  153. $orderSn = $cg->orderSn ?? '';
  154. $mainId = $cg->mainId ?? 0;
  155. $shopId = $cg->shopId ?? 0;
  156. $sjId = $cg->sjId ?? 0;
  157. $ghsName = $cg->ghsName ?? '';
  158. $payWay = $cg->payWay;
  159. $onlinePay = $cg->onlinePay ?? 1;
  160. $cg->refund = PurchaseClass::REFUND_YES;
  161. $cg->save();
  162. $transaction_id = $cg->thirdNo ?? '';
  163. $refundOrderSn = orderSn::getCgRefundSn();
  164. $refundPrice = $cgRefund->refundPrice ?? 0;
  165. if (is_numeric($refundPrice) == false || $refundPrice < 0) {
  166. util::fail('退款金额有问题');
  167. }
  168. $shop = ShopClass::getLockById($shopId);
  169. if (empty($shop)) {
  170. util::fail('没有找到门店14');
  171. }
  172. $main = MainClass::getLockById($mainId);
  173. if (empty($main)) {
  174. util::fail('没有main信息7');
  175. }
  176. // 处理退货退款前,把库存先补回。(本次哪些花材要退多少,就补回多少)
  177. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  178. $field = 'id, cgItemId, itemNum, xhNum';
  179. $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, $field, null, true);
  180. if (empty($refundItemList)) {
  181. util::fail('没有找到售后订单的花材哦');
  182. }
  183. $refundList = [];
  184. foreach ($refundItemList as $val) {
  185. $cgItemId = $val->cgItemId;
  186. if (empty($cgItemId)) {
  187. util::fail('旧订单无法售后,请联系管理员哦');
  188. }
  189. $refundList[$cgItemId] = $val;
  190. }
  191. $field = 'id, xhNum, productId';
  192. $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $cg->orderSn], null, $field, null, true);
  193. if (empty($cgItemList)) {
  194. util::fail('采购单花材错误');
  195. }
  196. $itemDatas = [];
  197. foreach ($cgItemList as $cgItem) {
  198. if (isset($refundList[$cgItem->id])) {
  199. $refund = $refundList[$cgItem->id];
  200. $product = ProductClass::getById($cgItem->productId, true);
  201. $itemDatas[$cgItem->productId] = ['product' => $product, 'addNum' => $refund->itemNum]; // 使用 itemNum 合适,还是 xhNum ?
  202. }
  203. }
  204. $cgShop = $shop;//采购门店
  205. $adminId = $cgShop->adminId;
  206. // 补回本次退货的花材数量
  207. PurchaseClass::increaseProductStock($itemDatas, $cgShop, $adminId, $cg->mainId, '系统');
  208. }
  209. if ($refundType == CgRefundClass::REFUND_TYPE_MONEY_GOOD) {
  210. $refundItemList = CgRefundItemClass::getAllByCondition(['orderSn' => $refundSn], null, '*', null, true);
  211. if (empty($refundItemList)) {
  212. util::fail('没有找到售后订单的花材哦');
  213. }
  214. $refundData = [];
  215. foreach ($refundItemList as $key => $val) {
  216. $val->status = 1;
  217. $val->save();
  218. $cgItemId = $val->cgItemId ?? 0;
  219. if (empty($cgItemId)) {
  220. util::fail('旧订单无法售后,请联系管理员哦');
  221. }
  222. $refundData[$cgItemId] = $val;
  223. }
  224. $cgItemList = PurchaseItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  225. if (empty($cgItemList)) {
  226. util::fail('采购单花材错误');
  227. }
  228. foreach ($cgItemList as $oneKey => $oneItem) {
  229. $oneItemId = $oneItem->id;
  230. $productId = $oneItem->productId ?? 0;
  231. $ptItemId = $oneItem->itemId ?? 0;
  232. $itemName = $oneItem->name ?? '';
  233. $cgItemNum = $oneItem->xhNum ?? 0;
  234. $refundNum = $oneItem->refundNum ?? 0;
  235. $remainNum = bcsub($cgItemNum, $refundNum);
  236. if (isset($refundData[$oneItemId])) {
  237. $refundSingle = $refundData[$oneItemId];
  238. $num = $refundSingle->xhNum ?? 0;
  239. if ($num > $remainNum) {
  240. util::fail("{$itemName} 超过可退数量");
  241. }
  242. // refundNum=总已退;隔天部分另记 nextRefundNum
  243. $oneItem->refundNum = bcadd($refundNum, $num);
  244. if ($sameDay === self::SAME_DAY_NO) {
  245. $oneItem->nextRefundNum = bcadd($oneItem->nextRefundNum ?? 0, $num);
  246. }
  247. $oneItem->save();
  248. if ($refundSingle->xhUnitType == 1) {
  249. $smallNum = $num;
  250. $bigNum = 0;
  251. } else {
  252. $smallNum = 0;
  253. $bigNum = $num;
  254. }
  255. $itemNum = $refundSingle->itemNum;
  256. if ($bigNum > 0 || $smallNum > 0) {
  257. $stockInfo = ProductClass::decreaseStock($productId, $bigNum, $smallNum);
  258. $oldStock = $stockInfo['oldStock'] ?? 0;
  259. $newStock = $stockInfo['newStock'] ?? 0;
  260. $recordData = [];
  261. $recordData['itemNum'] = $itemNum;
  262. $recordData['sjId'] = $sjId;
  263. $recordData['shopId'] = $shopId;
  264. $recordData['mainId'] = $mainId;
  265. $recordData['orderSn'] = $orderSn;
  266. $recordData['itemId'] = $ptItemId;
  267. $recordData['oldStock'] = $oldStock; //当时库存
  268. $recordData['productId'] = $productId;
  269. $recordData['newStock'] = $newStock;// 最新库存
  270. $recordData['relateName'] = $ghsName;
  271. $recordData['io'] = 0;
  272. StockRecordClass::hdCgApplyRefundAddRecord($recordData);
  273. $refundSingle->itemStock = $oldStock;
  274. $refundSingle->newStock = $newStock;
  275. $refundSingle->save();
  276. }
  277. }
  278. }
  279. }
  280. // 跨天售后:累计 nextDayTkPrice,不改 actPrice;原路退在此发起
  281. if ($sameDay === self::SAME_DAY_NO) {
  282. \bizGhs\order\classes\NextDayRefundClass::applyCgAmountAndFund($cgRefund, $cg);
  283. return;
  284. }
  285. //记录当天累计退款金额(影响 actPrice)
  286. $currentTkPrice = bcadd($cg->tkPrice, $refundPrice, 2);
  287. $cg->tkPrice = $currentTkPrice;
  288. $cg->refund = PurchaseClass::REFUND_YES;
  289. $cg->save();
  290. $ghsId = $cg->ghsId;
  291. $ghs = GhsClass::getLockById($ghsId);
  292. if (empty($ghs)) {
  293. util::fail('没有找到供货商');
  294. }
  295. // 根据 $refundPrice 更新消费金额 expendAmount
  296. $expendAmount = bcsub($ghs->expendAmount, $refundPrice, 2);
  297. $ghs->expendAmount = $expendAmount;
  298. $ghs->save();
  299. if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
  300. if ($payWay == dict::getDict('payWay', 'wxPay') || $payWay == dict::getDict('payWay', 'alipay')) {
  301. $ghsShopId = $ghs->shopId ?? 0;
  302. $ghsShop = ShopClass::getById($ghsShopId, true);
  303. if (empty($ghsShop)) {
  304. util::fail('没有找到供货商的门店');
  305. }
  306. $refundFee = bcmul($refundPrice, 100);
  307. $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
  308. $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
  309. $params = [
  310. 'appid' => 'OP00002119',
  311. 'serial_no' => '018b08cfddbd',
  312. 'merchant_no' => $ghsShop->lklSjNo,
  313. 'term_no' => $ghsShop->lklScanTermNo,
  314. 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
  315. 'lklCertificatePath' => $lklCertificatePath,
  316. ];
  317. $laResource = new Lakala($params);
  318. $refundReason = '';
  319. $aliParams = [
  320. 'refundSn' => $refundOrderSn,
  321. 'orderSn' => $orderSn,
  322. 'refundAmount' => $refundFee,
  323. 'refundReason' => $refundReason,
  324. 'thirdNo' => $transaction_id,//这个参数必传,不然聚合收银合支付宝付款的退款不会成功
  325. ];
  326. $response = $laResource->refund($aliParams);
  327. if (!isset($response['code']) || $response['code'] != 'BBS00000') {
  328. $errMsg = $response['msg'] ?? '';
  329. //$lklSjNo = $ghsShop->lklSjNo ?? '';
  330. //noticeUtil::push('商户号:' . $lklSjNo . '的退款失败了哦,请注意:' . $errMsg . ' 金额:' . $refundFee, '15280215347');
  331. util::fail('退款失败:' . $errMsg);
  332. }
  333. $refundTradeNo = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
  334. $cgRefund->thirdRefundNo = $refundTradeNo;
  335. $cgRefund->save();
  336. $cg->refundPrice = $refundPrice;
  337. $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
  338. if ($currentActPrice < 0) {
  339. util::fail('可退款金额不足!!');
  340. }
  341. $cg->actPrice = $currentActPrice;
  342. $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
  343. if ($currentRealPrice < 0) {
  344. util::fail('可退款金额不足,请排查。');
  345. }
  346. $cg->realPrice = $currentRealPrice;
  347. $cg->save();
  348. } else {
  349. util::fail('暂不支持退款');
  350. }
  351. } else {
  352. if ($payWay == dict::getDict('payWay', 'debtPay')) {
  353. $clearId = $cg->clearId ?? 0;
  354. if (!empty($clearId)) {
  355. util::fail('订单已经结帐,无法再发起退款');
  356. }
  357. $cg->refundPrice = $refundPrice;
  358. $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
  359. if ($currentActPrice < 0) {
  360. util::fail('可退款金额不足');
  361. }
  362. $cg->actPrice = $currentActPrice;
  363. $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
  364. if ($currentRealPrice < 0) {
  365. util::fail('可退款金额不足,请排查');
  366. }
  367. $cg->realPrice = $currentRealPrice;
  368. $currentRemainDebt = bcsub($cg->remainDebtPrice, $refundPrice, 2);
  369. if ($currentRemainDebt < 0) {
  370. util::fail('可退款金额不足哦');
  371. }
  372. if ($currentRemainDebt == 0) {
  373. $cg->debt = PurchaseClass::DEBT_NO;
  374. }
  375. $cg->remainDebtPrice = $currentRemainDebt;
  376. $cg->save();
  377. //门店应付总款减少
  378. $currentMayPay = bcsub($main->mayPay, $refundPrice, 2);
  379. if ($currentMayPay < 0) {
  380. //noticeUtil::push("应付款有问题! {$main->mayPay} {$refundPrice}", '15280215347');
  381. //util::fail('应付款有问题!');
  382. }
  383. $main->mayPay = $currentMayPay;
  384. $main->save();
  385. //应付供货商款减少
  386. GhsClass::refundDebtAmountReduce($ghs, $cgRefund, $cg);
  387. } elseif ($payWay == dict::getDict('payWay', 'balancePay')) {
  388. // 余额支付:资金在销售单 payAfter 成对账户扣减,退款由 RefundOrderClass 原路退回;采购单仅更新金额
  389. $cg->refundPrice = $refundPrice;
  390. $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
  391. if ($currentActPrice < 0) {
  392. util::fail('可退款金额不足');
  393. }
  394. $cg->actPrice = $currentActPrice;
  395. $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
  396. if ($currentRealPrice < 0) {
  397. util::fail('可退款金额不足,请排查');
  398. }
  399. $cg->realPrice = $currentRealPrice;
  400. $cg->save();
  401. } elseif ($payWay == dict::getDict('payWay', 'cash')) {
  402. $cg->refundPrice = $refundPrice;
  403. $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
  404. if ($currentActPrice < 0) {
  405. util::fail('可退款金额不足...');
  406. }
  407. $cg->actPrice = $currentActPrice;
  408. $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
  409. if ($currentRealPrice < 0) {
  410. util::fail('可退款金额不足,请确认');
  411. }
  412. $cg->realPrice = $currentRealPrice;
  413. $cg->save();
  414. } else {
  415. //其它线下退款直接完成
  416. $cg->refundPrice = $refundPrice;
  417. $currentActPrice = bcsub($cg->actPrice, $refundPrice, 2);
  418. if ($currentActPrice < 0) {
  419. util::fail('可退款金额不足...');
  420. }
  421. $cg->actPrice = $currentActPrice;
  422. $currentRealPrice = bcsub($cg->realPrice, $refundPrice, 2);
  423. if ($currentRealPrice < 0) {
  424. util::fail('可退款金额不足,请确认');
  425. }
  426. $cg->realPrice = $currentRealPrice;
  427. $cg->save();
  428. }
  429. }
  430. return $cgRefund;
  431. }
  432. //驳回 ssh 20230811
  433. public static function reject($cgRefund, $rejectReason)
  434. {
  435. if ($cgRefund->status == self::STATUS_COMPLETE) {
  436. util::fail('申请已通过');
  437. }
  438. if ($cgRefund->status == self::STATUS_REJECT) {
  439. util::fail('申请已驳回');
  440. }
  441. if ($cgRefund->status == self::STATUS_CANCEL) {
  442. util::fail('申请已取消');
  443. }
  444. $cgRefund->status = self::STATUS_REJECT;
  445. $cgRefund->rejectReason = $rejectReason;
  446. $cgRefund->save();
  447. }
  448. //取消退款 ssh 20230808
  449. public static function cancelRefund($refund)
  450. {
  451. if ($refund->status == self::STATUS_COMPLETE) {
  452. util::fail('申请已通过');
  453. }
  454. if ($refund->status == self::STATUS_REJECT) {
  455. util::fail('申请已驳回');
  456. }
  457. if ($refund->status == self::STATUS_CANCEL) {
  458. util::fail('申请已取消');
  459. }
  460. $refund->status = self::STATUS_CANCEL;
  461. $refund->save();
  462. }
  463. public static function valid($refund, $mainId)
  464. {
  465. if (isset($refund->mainId) == false || $refund->mainId != $mainId) {
  466. util::fail('无法访问,编号5');
  467. }
  468. return true;
  469. }
  470. public static function getRefundList($where)
  471. {
  472. $data = self::getList('*', $where, 'addTime DESC');
  473. $list = isset($data['list']) && !empty($data['list']) ? $data['list'] : [];
  474. if (empty($list)) {
  475. return $data;
  476. }
  477. $data['list'] = $list;
  478. return $data;
  479. }
  480. //2021.1.23 lqh 订单详情
  481. public static function getOrderDetail($orderSn, $shopId)
  482. {
  483. $orderData = self::orderExist(['orderSn' => $orderSn, 'shopId' => $shopId]);
  484. $orderInfo = CgRefundItemClass::getOrderItemDetail($orderSn);
  485. $itemData = [];
  486. if ($orderInfo) {
  487. foreach ($orderInfo as $v) {
  488. $info = json_decode($v['itemInfo'], true);
  489. $tmp = $info;
  490. $tmp['itemCover'] = self::groupImg($info['itemCover']);
  491. unset($v['itemInfo']);
  492. //兼容 旧数据,没有 rank (花材等级 默认给B)
  493. if (!isset($tmp['rank'])) {
  494. $tmp['rank'] = 'B';
  495. }
  496. $tmp = array_merge($tmp, $v);
  497. $itemData[] = $tmp;
  498. }
  499. }
  500. //下面注释放开前,员工id要用shopAdminId
  501. //$orderData = self::groupAdmin($orderData);
  502. $orderData['itemInfo'] = $itemData;
  503. return $orderData;
  504. }
  505. //2021.1.23 lqh 判断定时是否存在
  506. public static function orderExist($where)
  507. {
  508. $orderData = self::getByCondition($where);
  509. if (!$orderData) {
  510. util::fail('订单不存在');
  511. }
  512. return $orderData;
  513. }
  514. // 组装 orderItem lqh 2021.1.25
  515. public static function groupOrderItem($ghsItemInfo, $orderSn, $cgItemList)
  516. {
  517. $productData = self::getProductMapData($ghsItemInfo);
  518. //写入详情表
  519. $batchData = [];
  520. foreach ($ghsItemInfo as $v) {
  521. $tmp = [];
  522. $productId = $v['productId'];
  523. $itemId = $productData[$productId]['itemId'];
  524. $rank = $productData[$productId]['rank'] ?? 'B';// 花材级别
  525. $tmp['orderSn'] = $orderSn;
  526. $tmp['itemId'] = $itemId;
  527. $tmp['productId'] = $v['productId'];
  528. $unitNum = $productData[$productId]['ratio'] ?? 0;
  529. $tmp['itemStock'] = $v['oldStock'];//当时库存
  530. $tmp['newStock'] = $v['newStock'];//当时库存
  531. $tmp['itemNum'] = ProductClass::mergeItemNum($v['bigNum'], $v['smallNum'], $unitNum);// 数量
  532. $tmp['name'] = $productData[$productId]['name'] ?? '';
  533. $tmp['cover'] = $productData[$productId]['cover'] ?? '';
  534. $tmp['itemPrice'] = $cgItemList[$productId]['price'] ?? 0; //当时售卖的价格
  535. $tmp['totalPrice'] = bcmul($tmp['itemPrice'], $tmp['itemNum'], 2);
  536. $tmp['itemCost'] = $productData[$productId]['cost'] ?? 0; //当时成本价
  537. $stockFormat = ProductClass::formatStock($tmp['itemStock'], $unitNum); // 库存总单位数量转大小单位的数量
  538. $itemInfo = [
  539. 'itemName' => $productData[$productId]['name'] ?? '',//花材名称
  540. 'itemCover' => $productData[$productId]['cover'] ?? '',//花材图片
  541. 'bigNum' => $v['bigNum'] ?? 0,//数量(大单位)
  542. 'smallNum' => $v['smallNum'] ?? 0,//数量(小单位)
  543. 'bigNumStock' => $stockFormat['bigNum'],// 当时库存大单位数
  544. 'smallNumStock' => $stockFormat['smallNum'],//当时库存 小单位数
  545. 'itemUnit' => $unitNum ? 2 : 1,//单位数量 2表示有两个单位(e.g 扎和支) 1表是只有一个单位(e.g 扎)
  546. 'ratio' => $unitNum,// 比例
  547. 'rank' => $rank,// 花材级别
  548. 'bigUnit' => $productData[$productId]['bigUnit'],//大单位名称 扎
  549. 'smallUnit' => $productData[$productId]['smallUnit'],//小单位名称 支
  550. ];
  551. $tmp['itemInfo'] = json_encode($itemInfo);
  552. $batchData[] = $tmp;
  553. }
  554. return $batchData;
  555. }
  556. }