refundPrice = $refundPrice; $currentTkPrice = bcadd($order->tkPrice, $refundPrice, 2); if ($currentTkPrice > $order->mainPay) { util::fail('可退款金额不足'); } $order->tkPrice = $currentTkPrice; $order->refund = OrderClass::REFUND_YES; $currentActPrice = bcsub($order->actPrice, $refundPrice, 2); if ($currentActPrice < 0) { util::fail('可退款金额不足!'); } $order->actPrice = $currentActPrice; $order->realPrice = $currentActPrice; $order->save(); $refundType = $post['refundType'] ?? 1; $post['orderId'] = $order->id ?? 0; $data = []; $data['status'] = HdRefundClass::STATUS_COMPLETE; $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'] ?? ''; $postProduct = $post['product'] ?? ''; if ($refundType == 1) { //商品花材结构 property 0商品 1花材 //[{productId:0,num:1,unitType:0,unitPrice:12,unitName:'扎',property: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 (floatval($refundPrice) < floatval($calcAmount)) { //util::fail('退款金额要大于商品金额总和'); } 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}超过可退数量"); } $currentOrderItem->refundNum = bcadd($hasRefundNum, $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); $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); } } } $data['customId'] = $order->customId ?? 0; $data['remark'] = $post['remark'] ?? ''; $data['refundPrice'] = $refundPrice; $data['refundType'] = $post['refundType'] ?? 0; //写入订单表 $refund = HdRefundClass::add($data, 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; $event = '销售退款'; $capitalData = [ 'capitalType' => $thisType, 'io' => 0, 'totalExpend' => $currentTotalExpend, 'payWay' => 0, 'amount' => $refundPrice, 'sjId' => $sjId, 'shopId' => $shopId, 'event' => $event, 'mainId' => $mainId, ]; ShopCapitalClass::addCapital($capitalData); //当天和当月支出统计 //StatOutClass::updateOrInsert($main, $shop, $refundPrice); //退款统计 //StatRefundClass::replace($main, $shop, $payWay, $refundPrice); if ($onlinePay == dict::getDict('onlinePay', 'yes')) { //退款流程 if ($payWay == dict::getDict('payWay', 'wxPay') || $payWay == dict::getDict('payWay', 'alipay')) { //门店余额减少 \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); $refundReason = ''; $refundParams = [ 'refundSn' => $refundSn, 'orderSn' => $myOrderSn, 'refundAmount' => $refundFee, 'refundReason' => $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(); } } 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); } if ($payWay == dict::getDict('payWay', 'cash')) { //如果现金支付 $moneyData = [ 'shopId' => $shopId, 'amount' => $refundPrice, 'orderSn' => $order->orderSn, 'mainId' => $mainId, ]; ShopMoneyClass::hdRefundSkOutAmount($moneyData, $main); } if ($payWay == dict::getDict('payWay', 'balancePay')) { //如果是余额支付,客户的余额要增加 CustomClass::skRefundDebtAmountReduce($custom, $refundPrice, $refund, $payWay); } } return $refund; } }