| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604 |
- <?php
- /**
- * HD 零售售后服务
- * 用途:有原单当天/跨天售后、无原单退款;隔天写 nextRefundNum/nextDayTkPrice,对齐资金三态。
- */
- namespace bizHd\refund\services;
- use biz\product\classes\ProductClass;
- use biz\shop\classes\MainClass;
- use biz\shop\classes\ShopCapitalClass;
- use biz\stat\classes\StatOutClass;
- use biz\stat\classes\StatRefundClass;
- use biz\stock\classes\GoodsStockRecordClass;
- use bizHd\base\services\BaseService;
- use bizHd\custom\classes\CustomClass;
- use bizHd\goods\classes\GoodsClass;
- use bizHd\merchant\classes\ShopClass;
- use bizHd\order\classes\OrderClass;
- use bizHd\order\classes\OrderGoodsClass;
- use bizHd\order\classes\OrderItemClass;
- use bizHd\refund\classes\HdNextDayRefundClass;
- use bizHd\refund\classes\HdRefundClass;
- use bizHd\refund\classes\HdRefundGoodsClass;
- use bizHd\refund\classes\HdRefundItemClass;
- use bizGhs\shop\classes\ShopMoneyClass;
- use bizHd\wx\classes\WxOpenClass;
- use common\components\dict;
- use common\components\noticeUtil;
- use common\components\orderSn;
- use common\components\util;
- use Yii;
- use common\components\lakala\Lakala;
- class HdRefundService extends BaseService
- {
- public static $baseFile = '\bizHd\refund\classes\HdRefundClass';
- /**
- * 有原单退款(当天或隔天)
- * 隔天:加 nextRefundNum/nextDayTkPrice,不减 actPrice;资金按快照三态落地。
- * @param array $post
- * @param object $order
- * @param object|null $existRefund 商城待审核行,非空则复用该记录而不是新插
- * @return object|\bizHd\refund\models\HdRefund
- */
- public static function addRefund($post, $order, $existRefund = null)
- {
- if (!empty($existRefund) && (int)$existRefund->status !== HdRefundClass::STATUS_PENDING) {
- util::fail('仅待审核申请可通过');
- }
- $refundPrice = bcadd((string)($post['price'] ?? '0'), '0', 2);
- if (bccomp($refundPrice, '0', 2) < 0) {
- util::fail('请填写退款金额');
- }
- // 红包全额抵扣的 0 元单允许 0 元售后(退货/退红包);其它 0 元仍拦截
- if (bccomp($refundPrice, '0', 2) === 0 && (int)($order->hbId ?? 0) <= 0) {
- util::fail('请填写退款金额');
- }
- HdNextDayRefundClass::assertCanRefund($order);
- $sameDay = intval($post['sameDay'] ?? HdRefundClass::SAME_DAY_YES);
- if (HdNextDayRefundClass::mustUseNextDay($order)) {
- $sameDay = HdRefundClass::SAME_DAY_NO;
- }
- // 可退上限与商城申请 remainRefundCash 对齐:用 orderPrice(实付合计),
- // 不要只卡 mainPay——组合付 / 会员折后 mainPay 与 orderPrice 不一致时,申请能过、审核会误报「可退款金额不足」
- $alreadyTk = bcadd((string)($order->tkPrice ?? '0'), '0', 2);
- $alreadyNext = bcadd((string)($order->nextDayTkPrice ?? '0'), '0', 2);
- $payCeiling = self::resolveRefundPayCeiling($order);
- $totalRefunded = bcadd(bcadd($alreadyTk, $alreadyNext, 2), $refundPrice, 2);
- if (bccomp($totalRefunded, $payCeiling, 2) > 0) {
- util::fail('可退款金额不足');
- }
- if ($sameDay === HdRefundClass::SAME_DAY_YES) {
- $order->tkPrice = bcadd($alreadyTk, $refundPrice, 2);
- $order->refund = OrderClass::REFUND_YES;
- // 当天售后扣减 actPrice;若历史数据 actPrice 略小于应退额,钳到 0(上限已用 orderPrice 校验过)
- $currentActPrice = bcsub((string)($order->actPrice ?? '0'), $refundPrice, 2);
- if (bccomp($currentActPrice, '0', 2) < 0) {
- $currentActPrice = '0.00';
- }
- $order->actPrice = $currentActPrice;
- $order->realPrice = $currentActPrice;
- $order->save();
- } else {
- // 隔天金额在通过资金段 applyHdAmountAndFund 写入 nextDayTkPrice
- $order->refund = OrderClass::REFUND_YES;
- $order->save(false, ['refund']);
- }
- $refundType = $post['refundType'] ?? 1;
- $post['orderId'] = $order->id ?? 0;
- $data = [];
- $data['status'] = HdRefundClass::STATUS_COMPLETE;
- // 审核通过复用申请时已生成的 refundSn,明细行才能挂到同一单
- if (!empty($existRefund) && !empty($existRefund->refundSn)) {
- $refundSn = $existRefund->refundSn;
- } else {
- $refundSn = orderSn::getHdRefundSn();
- }
- $data['refundSn'] = $refundSn;
- $data['orderSn'] = $order->orderSn ?? '';
- $data['orderId'] = $order->id ?? 0;
- $sjId = $post['sjId'] ?? 0;
- $shopId = $post['shopId'] ?? 0;
- $mainId = $post['mainId'] ?? 0;
- $myOrderSn = $order->orderSn ?? '';
- $thirdNo = $order->thirdNo ?? '';
- $data['sjId'] = $sjId;
- $data['shopId'] = $shopId;
- $data['mainId'] = $mainId;
- $data['shopAdminId'] = $post['shopAdminId'] ?? 0;
- $data['shopAdminName'] = $post['shopAdminName'] ?? '';
- $data['passTime'] = date('Y-m-d H:i:s');
- $postProduct = $post['product'] ?? '';
- if ($refundType == 1) {
- //商品花材结构 property 0商品 1花材
- if (empty($postProduct)) {
- util::fail('没有退款商品');
- }
- $goodsInfoList = [];
- $itemInfoList = [];
- $calcAmount = 0;
- foreach ($postProduct as $current) {
- $property = $current['property'] ?? 0;
- if ($property == 0) {
- $goodsInfoList[] = $current;
- } else {
- $itemInfoList[] = $current;
- }
- $calcNum = $current['num'] ?? 0;
- $calcUnitPrice = $current['unitPrice'] ?? 0;
- $calcPrice = bcmul($calcNum, $calcUnitPrice, 2);
- $calcAmount = bcadd($calcAmount, $calcPrice, 2);
- }
- if (!empty($itemInfoList)) {
- $ids = array_column($itemInfoList, 'productId');
- $productInfo = ProductClass::getByids($ids, null, 'id');
- $orderItemData = OrderItemClass::getAllByCondition(['orderSn' => $myOrderSn], null, '*', 'itemId', true);
- if (empty($orderItemData)) {
- util::fail('没有找到商品');
- }
- foreach ($itemInfoList as $currentProduct) {
- $id = $currentProduct['productId'] ?? 0;
- $unitPrice = $currentProduct['unitPrice'] ?? 0;
- $unitType = $currentProduct['unitType'] ?? 0;
- $unitName = $currentProduct['unitName'] ?? '';
- $currentRefundNum = $currentProduct['num'] ?? 0;
- $price = bcmul($currentRefundNum, $unitPrice, 2);
- $name = $productInfo[$id]['name'] ?? '';
- $ptItemId = $productInfo[$id]['itemId'] ?? 0;
- $cover = $productInfo[$id]['cover'] ?? '';
- $ratio = $productInfo[$id]['ratio'] ?? '';
- $bigUnit = $productInfo[$id]['bigUnit'] ?? '';
- $smallUnit = $productInfo[$id]['smallUnit'] ?? '';
- $currentOrderItem = $orderItemData[$id] ?? null;
- if (empty($currentOrderItem)) {
- util::fail('没有找到退款的花材');
- }
- $orderItemNum = $currentOrderItem->num ?? 0;
- $hasRefundNum = $currentOrderItem->refundNum ?? 0;
- $remainNum = bcsub($orderItemNum, $hasRefundNum);
- if ($currentRefundNum > $remainNum) {
- util::fail("{$name}超过可退数量");
- }
- // refundNum=总已退;隔天另记 nextRefundNum
- $currentOrderItem->refundNum = bcadd($hasRefundNum, $currentRefundNum);
- if ($sameDay === HdRefundClass::SAME_DAY_NO) {
- $currentOrderItem->nextRefundNum = bcadd($currentOrderItem->nextRefundNum ?? 0, $currentRefundNum);
- }
- $currentOrderItem->save();
- $thisItemData = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $name,
- 'itemId' => $id,
- 'ptItemId' => $ptItemId,
- 'num' => $currentRefundNum,
- 'unitName' => $unitName,
- 'unitType' => $unitType,
- 'cover' => $cover,
- 'ratio' => $ratio,
- 'bigUnit' => $bigUnit,
- 'smallUnit' => $smallUnit,
- 'unitPrice' => $unitPrice,
- 'price' => $price,
- ];
- HdRefundItemClass::addData($thisItemData, $refundSn, $post);
- }
- }
- if (!empty($goodsInfoList)) {
- $ids = array_column($goodsInfoList, 'productId');
- $goodsData = GoodsClass::getByIds($ids, null, 'id');
- $orderGoodsData = OrderGoodsClass::getAllByCondition(['orderSn' => $myOrderSn], null, '*', 'goodsId', true);
- foreach ($goodsInfoList as $currentGoods) {
- $goodsId = $currentGoods['productId'] ?? 0;
- $unitPrice = $currentGoods['unitPrice'] ?? 0;
- $name = $goodsData[$goodsId]['name'] ?? '';
- $currentRefundNum = $currentGoods['num'] ?? 0;
- $price = bcmul($currentRefundNum, $unitPrice, 2);
- $currentOrderGoods = $orderGoodsData[$goodsId] ?? [];
- if (empty($currentOrderGoods)) {
- util::fail('没有找到商品呢');
- }
- $totalNum = $currentOrderGoods->num ?? 0;
- $hasRefundNum = $currentOrderGoods->refundNum ?? 0;
- $remainNum = bcsub($totalNum, $hasRefundNum);
- if ($currentRefundNum > $remainNum) {
- util::fail("{$name}超过可退数量");
- }
- $currentOrderGoods->refundNum = bcadd($hasRefundNum, $currentRefundNum);
- if ($sameDay === HdRefundClass::SAME_DAY_NO) {
- $currentOrderGoods->nextRefundNum = bcadd($currentOrderGoods->nextRefundNum ?? 0, $currentRefundNum);
- }
- $currentOrderGoods->save();
- $cover = $goodsData[$goodsId]['cover'] ?? '';
- $num = $currentGoods['num'] ?? 0;
- $saveGoods = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $name,
- 'cover' => $cover,
- 'goodsId' => $goodsId,
- 'num' => $num,
- 'unitPrice' => $unitPrice,
- 'price' => $price,
- ];
- HdRefundGoodsClass::addData($saveGoods, $refundSn, $post);
- }
- }
- }
- $snap = HdNextDayRefundClass::buildRefundPaySnapshot(
- $order,
- true,
- ['payWay' => $post['payWay'] ?? null]
- );
- $data['customId'] = $order->customId ?? 0;
- $data['customName'] = $order->customName ?? '';
- $data['remark'] = $post['remark'] ?? '';
- $data['refundPrice'] = $refundPrice;
- $data['refundType'] = $post['refundType'] ?? 0;
- $data['sameDay'] = $sameDay;
- $data['onlinePay'] = $snap['onlinePay'];
- $data['payWay'] = $snap['payWay'];
- $data['hasReturn'] = $snap['hasReturn'];
- $data['couldReturn'] = $snap['couldReturn'];
- $data['returnBalance'] = $snap['returnBalance'];
- if (!empty($existRefund)) {
- foreach ($data as $key => $val) {
- $existRefund->$key = $val;
- }
- if (!$existRefund->save()) {
- throw new \Exception('更新退款单失败');
- }
- $refund = $existRefund;
- } else {
- $refund = HdRefundClass::add($data, true);
- }
- // 实付 0 仅退红包:不走拉卡拉/资金流水,避免 0 元通道失败;红包在 mall 审核通过后 hbBackIfNotExpired
- if (bccomp($refundPrice, '0', 2) <= 0) {
- HdNextDayRefundClass::syncRefundFundFlags($refund, ['hasReturn' => HdNextDayRefundClass::FLAG_YES]);
- return HdRefundClass::getById($refund->id, true);
- }
- $payWay = $order->payWay;
- $shopId = $order->shopId;
- $onlinePay = $order->onlinePay ?? 1;
- $main = MainClass::getLockById($mainId);
- $shop = ShopClass::getLockById($shopId);
- if (empty($main)) {
- util::fail('main信息缺失呢');
- }
- if (empty($shop)) {
- util::fail('没有找到门店,编号15860');
- }
- //总支出增加
- $currentTotalExpend = bcadd($main->totalExpend, $refundPrice, 2);
- $main->totalExpend = $currentTotalExpend;
- $totalRefund = bcadd($main->totalRefund, $refundPrice, 2);
- $main->totalRefund = $totalRefund;
- $main->save();
- $thisType = dict::getDict('capitalType', 'hdOrderRefund', 'id');
- $sjId = $order->sjId ?? 0;
- $capitalData = [
- 'capitalType' => $thisType,
- 'io' => 0,
- 'totalExpend' => $currentTotalExpend,
- 'payWay' => 0,
- 'amount' => $refundPrice,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'event' => '销售退款',
- 'mainId' => $mainId,
- ];
- ShopCapitalClass::addCapital($capitalData);
- // 隔天:金额与余额/挂账原路;线下现金等留给成功页
- if ($sameDay === HdRefundClass::SAME_DAY_NO) {
- HdNextDayRefundClass::applyHdAmountAndFund($refund, $order);
- // 线上隔天:仍尝试原路退回通道
- if (HdNextDayRefundClass::isOnlinePayOrder($refund) || HdNextDayRefundClass::isOnlinePayOrder($order)) {
- self::doOnlineLakalaRefund($shop, $refund, $refundSn, $myOrderSn, $thirdNo, $refundPrice, $main);
- HdNextDayRefundClass::syncRefundFundFlags($refund, ['hasReturn' => HdNextDayRefundClass::FLAG_YES]);
- }
- return HdRefundClass::getById($refund->id, true);
- }
- // —— 当天售后资金 ——
- if ($onlinePay == dict::getDict('onlinePay', 'yes')) {
- if ($payWay == dict::getDict('payWay', 'wxPay') || $payWay == dict::getDict('payWay', 'alipay')) {
- self::doOnlineLakalaRefund($shop, $refund, $refundSn, $myOrderSn, $thirdNo, $refundPrice, $main);
- HdNextDayRefundClass::syncRefundFundFlags($refund, ['hasReturn' => HdNextDayRefundClass::FLAG_YES]);
- }
- } else {
- $customId = $order->customId ?? 0;
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- if ($payWay == dict::getDict('payWay', 'debtPay')) {
- $currentRemain = bcsub($order->remainDebtPrice, $refundPrice, 2);
- if ($currentRemain < 0) {
- util::fail('剩余欠款不足售后金额');
- }
- $order->remainDebtPrice = $currentRemain;
- if ($currentRemain <= 0) {
- $order->debt = 0;
- }
- $order->save();
- CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund, $payWay);
- HdNextDayRefundClass::syncRefundFundFlags($refund, ['hasReturn' => HdNextDayRefundClass::FLAG_YES]);
- }
- if ($payWay == dict::getDict('payWay', 'cash')) {
- $moneyData = [
- 'shopId' => $shopId,
- 'amount' => $refundPrice,
- 'orderSn' => $order->orderSn,
- 'mainId' => $mainId,
- ];
- ShopMoneyClass::hdRefundSkOutAmount($moneyData, $main);
- // 现金:需人工确认原路(couldReturn=1),成功页 markHasReturn
- }
- if ($payWay == dict::getDict('payWay', 'balancePay')) {
- CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund, $payWay);
- HdNextDayRefundClass::syncRefundFundFlags($refund, ['hasReturn' => HdNextDayRefundClass::FLAG_YES]);
- }
- }
- return HdRefundClass::getById($refund->id, true);
- }
- /**
- * 售后可退金额上限(与商城申请 remainRefundCash 同一口径)
- * 优先 orderPrice;为空时回退 mainPay+cash,避免组合付/折后主付字段与实付合计不一致
- * @param object $order
- * @return string 两位小数字符串
- */
- protected static function resolveRefundPayCeiling($order)
- {
- $orderPrice = bcadd((string)($order->orderPrice ?? '0'), '0', 2);
- if (bccomp($orderPrice, '0', 2) > 0) {
- return $orderPrice;
- }
- $mainPay = bcadd((string)($order->mainPay ?? '0'), '0', 2);
- $cash = bcadd((string)($order->cash ?? '0'), '0', 2);
- $sum = bcadd($mainPay, $cash, 2);
- if (bccomp($sum, '0', 2) > 0) {
- return $sum;
- }
- // 再回退 actPrice + 已退(还原下单实付)
- $act = bcadd((string)($order->actPrice ?? '0'), '0', 2);
- $tk = bcadd((string)($order->tkPrice ?? '0'), '0', 2);
- $next = bcadd((string)($order->nextDayTkPrice ?? '0'), '0', 2);
- return bcadd(bcadd($act, $tk, 2), $next, 2);
- }
- /**
- * 拉卡拉线上原路退款
- */
- protected static function doOnlineLakalaRefund($shop, $refund, $refundSn, $myOrderSn, $thirdNo, $refundPrice, $main)
- {
- \biz\shop\classes\ShopClass::hdSaleRefundReduceBalance($main, $shop, $refund, $refundPrice);
- $refundFee = bcmul($refundPrice, 100);
- $merchantPrivateKeyPath = Yii::getAlias("@vendor/lakala") . '/production/api_private_key.pem';
- $lklCertificatePath = Yii::getAlias("@vendor/lakala") . '/production/lkl-apigw-v1.cer';
- $params = [
- 'appid' => 'OP00002119',
- 'serial_no' => '018b08cfddbd',
- 'merchant_no' => $shop->lklSjNo,
- 'term_no' => $shop->lklScanTermNo,
- 'merchantPrivateKeyPath' => $merchantPrivateKeyPath,
- 'lklCertificatePath' => $lklCertificatePath,
- ];
- $laResource = new Lakala($params);
- $refundParams = [
- 'refundSn' => $refundSn,
- 'orderSn' => $myOrderSn,
- 'refundAmount' => $refundFee,
- 'refundReason' => '',
- 'thirdNo' => $thirdNo,
- ];
- $response = $laResource->refund($refundParams);
- if (!isset($response['code']) || $response['code'] != 'BBS00000') {
- $errMsg = $response['msg'] ?? '';
- noticeUtil::push('散客订单,支付宝,退款失败,原因:' . $errMsg . ' refundSn:' . $refundSn . ' orderSn:' . $myOrderSn, '15280215347');
- util::fail('退款失败');
- }
- $wxRefundId = isset($response['resp_data']['trade_no']) ? $response['resp_data']['trade_no'] : '';
- $refund->thirdRefundNo = $wxRefundId;
- $refund->save();
- }
- /**
- * 无原单退款(工作台「退款」):以 orderId=0、空 orderSn 标识无原单;
- * sameDay 写 0 表示走跨天售后资金路径(字段本身不表示无原单),立即完成,资金留给成功页。
- * @param array $post
- * @return array
- */
- public static function createFreeRefund($post)
- {
- $customId = intval($post['customId'] ?? 0);
- if ($customId <= 0) {
- util::fail('请选择客户');
- }
- $custom = CustomClass::getLockById($customId);
- if (empty($custom)) {
- util::fail('没有找到客户');
- }
- $mainId = intval($post['mainId'] ?? 0);
- if ($mainId > 0 && intval($custom->mainId ?? 0) > 0 && intval($custom->mainId) !== $mainId) {
- // 部分客户用 mainId 归属;无字段则不拦
- }
- $refundPrice = bcadd((string)($post['price'] ?? '0'), '0', 2);
- if (bccomp($refundPrice, '0', 2) <= 0) {
- util::fail('请填写退款金额');
- }
- $returnWay = intval($post['returnWay'] ?? -1);
- if ($returnWay === 0) {
- $refundType = HdRefundClass::REFUND_TYPE_MONEY;
- } elseif ($returnWay === 1) {
- $refundType = HdRefundClass::REFUND_TYPE_MONEY_GOOD;
- } else {
- $refundType = intval($post['refundType'] ?? HdRefundClass::REFUND_TYPE_MONEY_GOOD);
- }
- $snap = HdNextDayRefundClass::buildRefundPaySnapshot(null, false, [
- 'payWay' => $post['payWay'] ?? null,
- ]);
- $sjId = intval($post['sjId'] ?? 0);
- $shopId = intval($post['shopId'] ?? 0);
- $refundSn = orderSn::getHdRefundSn();
- $remark = trim((string)($post['remark'] ?? ''));
- $data = [
- 'status' => HdRefundClass::STATUS_COMPLETE,
- 'refundSn' => $refundSn,
- 'orderSn' => '',
- 'orderId' => 0,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'mainId' => $mainId,
- 'shopAdminId' => $post['shopAdminId'] ?? 0,
- 'shopAdminName' => $post['shopAdminName'] ?? '',
- 'customId' => $customId,
- 'customName' => $custom->name ?? '',
- 'remark' => $remark,
- 'refundPrice' => $refundPrice,
- 'refundType' => $refundType,
- // 无原单复用跨天售后资金路径,故 sameDay=0;是否无原单以 orderId=0 为准
- 'sameDay' => HdRefundClass::SAME_DAY_NO,
- 'onlinePay' => $snap['onlinePay'],
- 'payWay' => $snap['payWay'],
- 'hasReturn' => $snap['hasReturn'],
- 'couldReturn' => $snap['couldReturn'],
- 'returnBalance' => $snap['returnBalance'],
- 'passTime' => date('Y-m-d H:i:s'),
- ];
- $postProduct = $post['product'] ?? [];
- if ($refundType === HdRefundClass::REFUND_TYPE_MONEY_GOOD) {
- if (empty($postProduct) || !is_array($postProduct)) {
- util::fail('请选择花材');
- }
- }
- $post['refundType'] = $refundType;
- $post['orderId'] = 0;
- $refund = HdRefundClass::add($data, true);
- // 有花材则写售后行;是否回库由 refundType 决定(HdRefundItemClass::addData)
- if (!empty($postProduct) && is_array($postProduct)) {
- $itemInfoList = [];
- $goodsInfoList = [];
- foreach ($postProduct as $current) {
- $property = intval($current['property'] ?? 1);
- if ($property === 0) {
- $goodsInfoList[] = $current;
- } else {
- $itemInfoList[] = $current;
- }
- }
- if (!empty($itemInfoList)) {
- $ids = array_column($itemInfoList, 'productId');
- $productInfo = ProductClass::getByids($ids, null, 'id');
- foreach ($itemInfoList as $currentProduct) {
- $id = intval($currentProduct['productId'] ?? 0);
- if ($id <= 0 || empty($productInfo[$id])) {
- util::fail('花材参数错误');
- }
- $num = bcadd((string)($currentProduct['num'] ?? '0'), '0', 2);
- if (bccomp($num, '0', 2) <= 0) {
- util::fail(($productInfo[$id]['name'] ?? '花材') . '数量必须大于0');
- }
- $unitPrice = bcadd((string)($currentProduct['unitPrice'] ?? '0'), '0', 2);
- $unitType = intval($currentProduct['unitType'] ?? 0);
- $unitName = $currentProduct['unitName'] ?? '';
- if ($unitName === '') {
- $unitName = $unitType === 1
- ? ($productInfo[$id]['smallUnit'] ?? '支')
- : ($productInfo[$id]['bigUnit'] ?? '扎');
- }
- $thisItemData = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $productInfo[$id]['name'] ?? '',
- 'itemId' => $id,
- 'ptItemId' => $productInfo[$id]['itemId'] ?? 0,
- 'num' => $num,
- 'unitName' => $unitName,
- 'unitType' => $unitType,
- 'cover' => $productInfo[$id]['cover'] ?? '',
- 'ratio' => $productInfo[$id]['ratio'] ?? '',
- 'bigUnit' => $productInfo[$id]['bigUnit'] ?? '',
- 'smallUnit' => $productInfo[$id]['smallUnit'] ?? '',
- 'unitPrice' => $unitPrice,
- 'price' => bcmul($num, $unitPrice, 2),
- ];
- HdRefundItemClass::addData($thisItemData, $refundSn, $post);
- }
- }
- if (!empty($goodsInfoList)) {
- $ids = array_column($goodsInfoList, 'productId');
- $goodsData = GoodsClass::getByIds($ids, null, 'id');
- foreach ($goodsInfoList as $currentGoods) {
- $goodsId = intval($currentGoods['productId'] ?? 0);
- if ($goodsId <= 0 || empty($goodsData[$goodsId])) {
- util::fail('商品参数错误');
- }
- $num = bcadd((string)($currentGoods['num'] ?? '0'), '0', 2);
- if (bccomp($num, '0', 2) <= 0) {
- util::fail(($goodsData[$goodsId]['name'] ?? '商品') . '数量必须大于0');
- }
- $unitPrice = bcadd((string)($currentGoods['unitPrice'] ?? '0'), '0', 2);
- $saveGoods = [
- 'refundSn' => $refundSn,
- 'mainId' => $mainId,
- 'sjId' => $sjId,
- 'shopId' => $shopId,
- 'name' => $goodsData[$goodsId]['name'] ?? '',
- 'cover' => $goodsData[$goodsId]['cover'] ?? '',
- 'goodsId' => $goodsId,
- 'num' => $num,
- 'unitPrice' => $unitPrice,
- 'price' => bcmul($num, $unitPrice, 2),
- ];
- HdRefundGoodsClass::addData($saveGoods, $refundSn, $post);
- }
- }
- }
- HdNextDayRefundClass::addMainExpendForRefund($mainId, $shopId, $sjId, $refundPrice, '无原单退款');
- HdNextDayRefundClass::applyFreeFund($refund);
- $refund = HdRefundClass::getById($refund->id, true);
- $custom = CustomClass::getById($customId, true);
- return HdNextDayRefundClass::buildFundResultPayload($refund, $custom, [
- 'nextDayTkPrice' => '0.00',
- 'needFundAction' => 1,
- ]);
- }
- }
|