|
|
@@ -10,6 +10,10 @@ use bizGhs\custom\classes\CustomClass;
|
|
|
use bizGhs\custom\services\GhsRechargeSettleService;
|
|
|
use bizGhs\order\classes\OrderClass;
|
|
|
use bizGhs\order\classes\RefundOrderClass;
|
|
|
+use bizGhs\order\classes\RefundOrderItemClass;
|
|
|
+use bizGhs\product\classes\ProductClass;
|
|
|
+use bizGhs\shop\classes\MainClass;
|
|
|
+use bizGhs\stock\classes\StockRecordClass;
|
|
|
use bizHd\cg\classes\CgRefundClass;
|
|
|
use bizHd\purchase\classes\PurchaseClass;
|
|
|
use common\components\dict;
|
|
|
@@ -75,27 +79,28 @@ class NextDayRefundService
|
|
|
|
|
|
/**
|
|
|
* 解析隔天资金方式:挂账/余额强制返余额;在线可选原路/返余额/仅记账。
|
|
|
+ * $hasRelateOrder=false 表示无原单退款(工作台「退款」),不可原路退。
|
|
|
*/
|
|
|
public static function resolveFundType($fundType, $order, $hasRelateOrder = true)
|
|
|
{
|
|
|
$fundType = intval($fundType);
|
|
|
- $payWay = intval($order->payWay ?? 0);
|
|
|
- $onlinePay = intval($order->onlinePay ?? dict::getDict('onlinePay', 'not'));
|
|
|
- $debtPay = dict::getDict('payWay', 'debtPay');
|
|
|
- $balancePay = dict::getDict('payWay', 'balancePay');
|
|
|
- $wxPay = dict::getDict('payWay', 'wxPay');
|
|
|
- $aliPay = dict::getDict('payWay', 'alipay');
|
|
|
- $isOnline = $onlinePay == dict::getDict('onlinePay', 'yes') && in_array($payWay, [$wxPay, $aliPay], true);
|
|
|
-
|
|
|
- if (!$hasRelateOrder) {
|
|
|
+ // 无原单:只能返余额或仅记账
|
|
|
+ if (!$hasRelateOrder || empty($order)) {
|
|
|
if ($fundType === self::FUND_ORIGINAL) {
|
|
|
- util::fail('自由冲销不能原路退回');
|
|
|
+ util::fail('无原单退款不能原路退回');
|
|
|
}
|
|
|
if (!in_array($fundType, [self::FUND_BALANCE, self::FUND_NONE], true)) {
|
|
|
return self::FUND_BALANCE;
|
|
|
}
|
|
|
return $fundType;
|
|
|
}
|
|
|
+ $payWay = intval($order->payWay ?? 0);
|
|
|
+ $onlinePay = intval($order->onlinePay ?? dict::getDict('onlinePay', 'not'));
|
|
|
+ $debtPay = dict::getDict('payWay', 'debtPay');
|
|
|
+ $balancePay = dict::getDict('payWay', 'balancePay');
|
|
|
+ $wxPay = dict::getDict('payWay', 'wxPay');
|
|
|
+ $aliPay = dict::getDict('payWay', 'alipay');
|
|
|
+ $isOnline = $onlinePay == dict::getDict('onlinePay', 'yes') && in_array($payWay, [$wxPay, $aliPay], true);
|
|
|
if ($payWay === $debtPay || $payWay === $balancePay) {
|
|
|
return self::FUND_BALANCE;
|
|
|
}
|
|
|
@@ -175,6 +180,125 @@ class NextDayRefundService
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 无原单退款通过:加库存(退货退款)、累计门店支出、扣客户消费、按 fundType 返余额/仅记账。
|
|
|
+ * 不写订单 nextDayTkPrice(无关联销售单)。
|
|
|
+ */
|
|
|
+ public static function passFreeRefund($refund)
|
|
|
+ {
|
|
|
+ if (empty($refund)) {
|
|
|
+ util::fail('退款单不存在');
|
|
|
+ }
|
|
|
+ if (intval($refund->status) === RefundOrderClass::STATUS_COMPLETE) {
|
|
|
+ util::fail('已通过');
|
|
|
+ }
|
|
|
+ if (intval($refund->status) === RefundOrderClass::STATUS_REJECT) {
|
|
|
+ util::fail('已驳回');
|
|
|
+ }
|
|
|
+ if (intval($refund->status) === RefundOrderClass::STATUS_CANCEL) {
|
|
|
+ util::fail('已取消');
|
|
|
+ }
|
|
|
+
|
|
|
+ $refund->status = RefundOrderClass::STATUS_COMPLETE;
|
|
|
+ $refund->passTime = date('Y-m-d H:i:s');
|
|
|
+ $refund->save();
|
|
|
+
|
|
|
+ $refundSn = $refund->orderSn ?? '';
|
|
|
+ $refundPrice = bcadd((string)($refund->refundPrice ?? '0'), '0', 2);
|
|
|
+ $refundType = intval($refund->refundType ?? RefundOrderClass::REFUND_TYPE_MONEY_GOOD);
|
|
|
+ $shopId = intval($refund->shopId ?? 0);
|
|
|
+ $mainId = intval($refund->mainId ?? 0);
|
|
|
+ $sjId = intval($refund->sjId ?? 0);
|
|
|
+ $customName = $refund->customName ?? '';
|
|
|
+
|
|
|
+ // 退货退款:按明细回库(无原单行,不校验可退数量)
|
|
|
+ if ($refundType === RefundOrderClass::REFUND_TYPE_MONEY_GOOD) {
|
|
|
+ $refundItemList = RefundOrderItemClass::getAllByCondition(['orderSn' => $refundSn], null, '*', null, true);
|
|
|
+ if (empty($refundItemList)) {
|
|
|
+ util::fail('没有找到退款花材');
|
|
|
+ }
|
|
|
+ foreach ($refundItemList as $val) {
|
|
|
+ $val->status = 1;
|
|
|
+ $val->save();
|
|
|
+ $productId = intval($val->productId ?? 0);
|
|
|
+ $ptItemId = intval($val->itemId ?? 0);
|
|
|
+ $num = bcadd((string)($val->xhNum ?? '0'), '0', 2);
|
|
|
+ if (bccomp($num, '0', 2) <= 0 || $productId <= 0) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ if (intval($val->xhUnitType ?? 0) === 1) {
|
|
|
+ $smallNum = $num;
|
|
|
+ $bigNum = 0;
|
|
|
+ } else {
|
|
|
+ $smallNum = 0;
|
|
|
+ $bigNum = $num;
|
|
|
+ }
|
|
|
+ $stockInfo = ProductClass::addStock($productId, $bigNum, $smallNum);
|
|
|
+ ProductClass::tryRecoverLimitBuyWhenStockRollbackFromZero($productId, $stockInfo['oldStock'] ?? 0);
|
|
|
+ $oldStock = $stockInfo['oldStock'] ?? 0;
|
|
|
+ $newStock = $stockInfo['newStock'] ?? 0;
|
|
|
+ $recordData = [
|
|
|
+ 'relateName' => $customName,
|
|
|
+ 'itemNum' => $val->itemNum ?? 0,
|
|
|
+ 'sjId' => $sjId,
|
|
|
+ 'shopId' => $shopId,
|
|
|
+ 'mainId' => $mainId,
|
|
|
+ 'orderSn' => $refundSn,
|
|
|
+ 'itemId' => $ptItemId,
|
|
|
+ 'oldStock' => $oldStock,
|
|
|
+ 'productId' => $productId,
|
|
|
+ 'newStock' => $newStock,
|
|
|
+ 'io' => 1,
|
|
|
+ ];
|
|
|
+ StockRecordClass::ghsRefundAddRecord($recordData);
|
|
|
+ $val->itemStock = $oldStock;
|
|
|
+ $val->newStock = $newStock;
|
|
|
+ $val->save();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ $main = MainClass::getLockById($mainId);
|
|
|
+ if (empty($main)) {
|
|
|
+ util::fail('没有main信息');
|
|
|
+ }
|
|
|
+ $main->totalExpend = bcadd((string)($main->totalExpend ?? '0'), $refundPrice, 2);
|
|
|
+ $main->totalRefund = bcadd((string)($main->totalRefund ?? '0'), $refundPrice, 2);
|
|
|
+ $main->save();
|
|
|
+
|
|
|
+ return self::applyFreeFund($refund);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 无原单退款资金:扣减客户累计消费;返余额走成对账户,仅记账不动余额。
|
|
|
+ * @return string 处理后的客户余额
|
|
|
+ */
|
|
|
+ public static function applyFreeFund($refund)
|
|
|
+ {
|
|
|
+ $refundPrice = bcadd((string)($refund->refundPrice ?? '0'), '0', 2);
|
|
|
+ $customId = intval($refund->customId ?? 0);
|
|
|
+ $custom = CustomClass::getLockById($customId);
|
|
|
+ if (empty($custom)) {
|
|
|
+ util::fail('没有找到客户');
|
|
|
+ }
|
|
|
+ $buyAmount = bcsub((string)($custom->buyAmount ?? '0'), $refundPrice, 2);
|
|
|
+ if (bccomp($buyAmount, '0', 2) < 0) {
|
|
|
+ $buyAmount = '0.00';
|
|
|
+ }
|
|
|
+ $custom->buyAmount = $buyAmount;
|
|
|
+ $custom->save(false, ['buyAmount']);
|
|
|
+
|
|
|
+ $fundType = intval($refund->fundType ?? self::FUND_UNUSED);
|
|
|
+ $balance = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ if ($fundType === self::FUND_BALANCE) {
|
|
|
+ $pair = GhsRechargeSettleService::lockAccountPair($custom);
|
|
|
+ // 无原单:order 传 null,流水挂售后单号
|
|
|
+ CustomClass::nextDayRefundReturnBalance($pair['custom'], $pair['ghs'], $refund, null);
|
|
|
+ $custom = CustomClass::getById($customId, true);
|
|
|
+ $balance = bcadd((string)($custom->balance ?? '0'), '0', 2);
|
|
|
+ }
|
|
|
+ return $balance;
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 按时段汇总成功隔天售后金额(收入/销量统计扣减用)
|
|
|
*/
|