diff --git a/app/Console/Commands/SyncStoreReceiptOrderMatches.php b/app/Console/Commands/SyncStoreReceiptOrderMatches.php index 334a487b..15c5a9cc 100644 --- a/app/Console/Commands/SyncStoreReceiptOrderMatches.php +++ b/app/Console/Commands/SyncStoreReceiptOrderMatches.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Schema; use InvalidArgumentException; use App\Business\JstOrderLabelPlantingRules; use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap; @@ -211,6 +212,10 @@ class SyncStoreReceiptOrderMatches extends Command } $select = ['store_name', 'bill_item', $orderColumn, $timeColumn, $amountColumn]; + // 「冲减金额」按行金额(收入侧 − 支出侧)取数,账单表有支出列时一并取出。 + if (Schema::connection($connection)->hasColumn($billTable, 'expense_amount')) { + $select[] = 'expense_amount'; + } if ($realStoreColumn !== '') { $select[] = $realStoreColumn; } @@ -333,6 +338,9 @@ class SyncStoreReceiptOrderMatches extends Command } $amount = (float) $bill->{$amountColumn}; + // 「冲减金额」按行金额取数=收入侧 − 支出侧(负数表示净扣减)。 + // 得物/天猫这类平台把「卖家承担优惠 / 扣回」记在支出侧,只取收入侧会漏掉扣减、并把退款冲回当成冲减。 + $offsetAmount = $amount - (float) ($bill->expense_amount ?? 0); if ($bucket === 'income') { $groups[$key]['payment_count']++; $groups[$key]['income_amount'] += $amount; @@ -345,8 +353,8 @@ class SyncStoreReceiptOrderMatches extends Command $promoRowsByKey[$key] = ($promoRowsByKey[$key] ?? 0) + 1; } else { $groups[$key]['offset_seen'] = true; - $groups[$key]['offset_amount'] += $amount; - $offsetTotal += $amount; + $groups[$key]['offset_amount'] += $offsetAmount; + $offsetTotal += $offsetAmount; $promoRowsByKey[$key] = ($promoRowsByKey[$key] ?? 0) + 1; } @@ -884,6 +892,8 @@ class SyncStoreReceiptOrderMatches extends Command 'income_amount' => 0, 'discount_amount' => ! empty($group['discount_seen']) ? round((float) $group['discount_amount'], 2) : null, 'offset_amount' => ! empty($group['offset_seen']) ? round((float) $group['offset_amount'], 2) : null, + // 补行没有订单侧商品金额,种菜金额同样留空(列必须与其它行一致,否则批量插入会因列数不匹配失败)。 + 'planting_amount' => null, 'product_amount' => null, 'difference' => null, 'match_status' => self::STATUS_UNMATCHED,