业绩明细:冲减金额后面新增「种菜金额」列

口径:订单号同时出现在「种菜明细表」和「聚水潭订单标签明细(是否种菜订单=是,即标签命中种菜映射)」里时,该订单的收入金额就是种菜金额,其余留空。两边的订单号都按账单侧同一套「核心单号」清洗(去日期前缀/空格/横杠)后比较;金额取该行已有的收入金额,只是展示,不参与差额计算(它本身就是收入金额的一部分)。

- 新增迁移:store_receipt_order_match_rows 增加 planting_amount 列(在 offset_amount 后面)。
- 同步命令:正式同步时写入该列;--amounts-only 一并回填;新增 --planting-only 只刷这一列(不读账单,适合给已同步好的账期补数);新增 --company 选项。
- 页面:列顺序 收入金额 / 优惠金额 / 冲减金额 / 种菜金额 / 商品金额…,顶部统计加「种菜金额合计」,导出同步带上该列。
- 补 NavigationTest 的列断言。
This commit is contained in:
qiuxiang 2026-10-08 10:58:39 +08:00
parent dcdea11f0f
commit 7b09d0de05
6 changed files with 197 additions and 7 deletions

View File

@ -6,6 +6,7 @@ use Illuminate\Console\Command;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;
use InvalidArgumentException;
use App\Business\JstOrderLabelPlantingRules;
use Modules\EcommerceFinance\Services\AccountLedgerRemarkMap;
use Modules\EcommerceFinance\Services\AccountMappingStore;
use Modules\EcommerceFinance\Services\LedgerRemarkFilter;
@ -31,9 +32,11 @@ class SyncStoreReceiptOrderMatches extends Command
protected $signature = 'store-receipt:order-match:sync
{month : 账单月份,格式 YYYY-MM}
{--platform=taobao-alipay : 收款账户,目前支持 taobao-alipay}
{--company=1 : 公司 ID(种菜订单号取自该公司的种菜明细表与聚水潭订单标签明细)}
{--chunk=1200 : 单次向上游查询的订单号数量}
{--limit=0 : 仅处理前 N 笔账单(调试用,0 表示全量)}
{--amounts-only : 只回填「优惠金额 / 冲减金额」两列,不重跑订单侧匹配}';
{--amounts-only : 只回填「优惠金额 / 冲减金额 / 种菜金额」三列,不重跑订单侧匹配}
{--planting-only : 只回填「种菜金额」一列(不读账单,直接按已有明细行的订单号刷)}';
protected $description = '按店铺收款账户账单匹配订单侧商品金额,生成「业绩明细」核对镜像';
@ -167,6 +170,10 @@ class SyncStoreReceiptOrderMatches extends Command
$billTable,
));
if ((bool) $this->option('planting-only')) {
return $this->backfillPlantingAmounts($reportMonth, (string) $source['label']);
}
// 「总账备注」→ 按平台分组的账单项目集合(淘系页签同页混有淘宝/天猫/阿里巴巴店铺,
// 必须连店铺名前缀一起过滤,才能与页面按行取到的总账备注一致)。
// 收入金额只认 LEDGER_REMARKS;优惠/冲减金额各自单独成列,不参与收入与差额。
@ -255,6 +262,10 @@ class SyncStoreReceiptOrderMatches extends Command
$incomeTotal = 0.0;
$discountTotal = 0.0;
$offsetTotal = 0.0;
// 种菜订单号:种菜明细表的订单号 ∩ 聚水潭订单标签明细里「是否种菜订单=是」的订单号。
$plantingCores = $this->plantingOrderCores();
$plantingTotal = 0.0;
$plantingRows = 0;
$remarkMap = app(AccountLedgerRemarkMap::class);
$tabPlatform = $remarkMap->defaultPlatform($platform);
$ledgerMap = $remarkMap->map();
@ -323,6 +334,12 @@ class SyncStoreReceiptOrderMatches extends Command
$groups[$key]['offset_amount'] += $amount;
$offsetTotal += $amount;
}
// 种菜金额只标记「这个订单是不是种菜单」,金额仍取该行的收入金额,
// 所以在这里按核心单号打个标记,金额等聚合完再取。
if (isset($plantingCores[$core])) {
$groups[$key]['planting_seen'] = true;
}
}
// 行口径保持不变:仍然是「有收款账单(收入白名单)的店铺 + 订单号」,
@ -331,6 +348,18 @@ class SyncStoreReceiptOrderMatches extends Command
$this->info('账单收款笔数:' . $paymentTotal . ',按「店铺 + 订单号」聚合后:' . count($groups) . ' 行');
$this->line(' 优惠金额合计:' . number_format($discountTotal, 2) . ',冲减金额合计:' . number_format($offsetTotal, 2));
foreach ($groups as $group) {
if (! empty($group['planting_seen'])) {
$plantingRows++;
$plantingTotal += (float) $group['income_amount'];
}
}
$this->line(sprintf(
' 种菜金额:%d 行(识别到 %d 个种菜订单号),合计 %s',
$plantingRows,
count($plantingCores),
number_format($plantingTotal, 2),
));
if ($incomeOverlapRows > 0) {
$this->line(sprintf(
' 提示:%d 笔(%s 元)按旧口径已计入收入金额,未重复计入优惠/冲减列。',
@ -523,6 +552,7 @@ class SyncStoreReceiptOrderMatches extends Command
'income_amount' => $income,
'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' => ! empty($group['planting_seen']) ? $income : null,
'product_amount' => $productAmount,
'difference' => $difference,
'match_status' => $status,
@ -557,8 +587,55 @@ class SyncStoreReceiptOrderMatches extends Command
}
/**
* 只回填「优惠金额 / 冲减金额」两列:不重建业绩明细、不查订单侧,
* 先清空该账期该平台的两列,再按 row_key 更新镜像里已存在的行。
* 只回填「种菜金额」:不读账单,直接按业绩明细里已有的「订单号」判断是不是种菜订单,
* 是就把该行的收入金额写进去,不是就清空。适合给已经同步好的账期补这一列。
*/
private function backfillPlantingAmounts(string $reportMonth, string $platform): int
{
$connection = (string) config('upstream_mirrors.connection', 'business_mirror');
$cores = $this->plantingOrderCores();
if ($cores === []) {
$this->warn('没有识别到任何种菜订单号(种菜明细表与聚水潭订单标签明细的交集为空),本列将全部为空。');
}
$target = static fn () => DB::connection($connection)->table(self::TARGET_TABLE)
->where('report_month', $reportMonth)
->where('platform', $platform);
$rows = $target()->get(['row_key', 'order_core_no', 'income_amount']);
$target()->update(['planting_amount' => null]);
$values = [];
$total = 0.0;
foreach ($rows as $row) {
$core = self::orderCoreNo((string) $row->order_core_no);
if ($core === '' || ! isset($cores[$core])) {
continue;
}
$amount = round((float) $row->income_amount, 2);
$values[(string) $row->row_key] = $amount;
$total += $amount;
}
$this->updateAmountColumn($connection, $reportMonth, 'planting_amount', $values);
$this->info(sprintf(
'已回填 %s %s 种菜金额:%d / %d 行,合计 %s(共识别 %d 个种菜订单号)。',
$reportMonth,
$platform,
count($values),
count($rows),
number_format($total, 2),
count($cores),
));
return self::SUCCESS;
}
/**
* 只回填「优惠金额 / 冲减金额 / 种菜金额」三列:不重建业绩明细、不查订单侧,
* 先清空该账期该平台的三列,再按 row_key 更新镜像里已存在的行。
*
* @param array<string,mixed> $source
* @param array<string,array<string,mixed>> $groups
@ -579,12 +656,13 @@ class SyncStoreReceiptOrderMatches extends Command
$target()
->where('report_month', $reportMonth)
->where('platform', $platform)
->update(['discount_amount' => null, 'offset_amount' => null]);
->update(['discount_amount' => null, 'offset_amount' => null, 'planting_amount' => null]);
$discounts = [];
$offsets = [];
$plantings = [];
foreach ($groups as $group) {
if (empty($group['discount_seen']) && empty($group['offset_seen'])) {
if (empty($group['discount_seen']) && empty($group['offset_seen']) && empty($group['planting_seen'])) {
continue;
}
@ -599,19 +677,25 @@ class SyncStoreReceiptOrderMatches extends Command
if (! empty($group['offset_seen'])) {
$offsets[$rowKey] = round((float) $group['offset_amount'], 2);
}
if (! empty($group['planting_seen'])) {
$plantings[$rowKey] = round((float) $group['income_amount'], 2);
}
}
$this->updateAmountColumn($connection, $reportMonth, 'discount_amount', $discounts);
$this->updateAmountColumn($connection, $reportMonth, 'offset_amount', $offsets);
$this->updateAmountColumn($connection, $reportMonth, 'planting_amount', $plantings);
$this->info(sprintf(
'已回填 %s %s:优惠 %d 行(%s)/ 冲减 %d 行(%s),镜像原有 %d 行,未重建业绩明细。',
'已回填 %s %s:优惠 %d 行(%s)/ 冲减 %d 行(%s)/ 种菜 %d 行(%s),镜像原有 %d 行,未重建业绩明细。',
$month,
$platform,
count($discounts),
number_format(array_sum($discounts), 2),
count($offsets),
number_format(array_sum($offsets), 2),
count($plantings),
number_format(array_sum($plantings), 2),
count($existing),
));
@ -657,6 +741,58 @@ class SyncStoreReceiptOrderMatches extends Command
/** 单号清洗:去掉日期前缀(如 260630-)、空格、横杠,得到核心号。 */
public static function cleanOrderNo(string $value): string
{
return self::orderCoreNo($value);
}
/**
* 种菜订单号集合:种菜明细表的订单号 ∩ 聚水潭订单标签明细里「是否种菜订单=是」的订单号。
*
* 两边都按键清洗后的「核心单号」比较(去掉日期前缀、空格、横杠),与账单侧同一套口径。
*
* @return array<string,true>
*/
private function plantingOrderCores(): array
{
$companyId = (int) $this->option('company');
$positiveLabels = [];
foreach (app(JstOrderLabelPlantingRules::class)->map($companyId) as $label => $flag) {
if ($flag === '是') {
$positiveLabels[] = $label;
}
}
if ($positiveLabels === []) {
return [];
}
// 聚水潭订单标签明细:命中种菜映射的订单号
$labelled = DB::table('jst_order_labels')
->where('company_id', $companyId)
->whereIn('label', $positiveLabels)
->distinct()
->pluck('order_no');
$cores = [];
foreach ($labelled as $orderNo) {
$core = self::orderCoreNo((string) $orderNo);
if ($core !== '') {
$cores[$core] = true;
}
}
// 种菜明细表:只有两边都出现才算种菜订单
$matched = [];
foreach (DB::table('planting_details')->where('company_id', $companyId)->distinct()->pluck('order_no') as $orderNo) {
$core = self::orderCoreNo((string) $orderNo);
if ($core !== '' && isset($cores[$core])) {
$matched[$core] = true;
}
}
return $matched;
}
private static function orderCoreNo(string $value): string
{
$value = str_replace("\u{3000}", ' ', trim($value));
if ($value === '') {

View File

@ -654,6 +654,7 @@ class Pages extends Controller
SUM(income_amount) as income_total,
SUM(discount_amount) as discount_total,
SUM(offset_amount) as offset_total,
SUM(planting_amount) as planting_total,
SUM(product_amount) as goods_total,
SUM(difference) as difference_total,
SUM(CASE WHEN payment_count > 1 THEN 1 ELSE 0 END) as multi_pay,
@ -675,6 +676,7 @@ class Pages extends Controller
'income_total' => round((float) ($agg->income_total ?? 0), 2),
'discount_total' => round((float) ($agg->discount_total ?? 0), 2),
'offset_total' => round((float) ($agg->offset_total ?? 0), 2),
'planting_total' => round((float) ($agg->planting_total ?? 0), 2),
'goods_total' => round((float) ($agg->goods_total ?? 0), 2),
'difference_total' => round((float) ($agg->difference_total ?? 0), 2),
'per_page' => $limit,
@ -763,6 +765,7 @@ class Pages extends Controller
'income_amount' => ['income_amount', '收入金额', 'money'],
'discount_amount' => ['discount_amount', '优惠金额', 'money'],
'offset_amount' => ['offset_amount', '冲减金额', 'money'],
'planting_amount' => ['planting_amount', '种菜金额', 'money'],
'product_amount' => ['product_amount', '商品金额', 'money'],
'difference' => ['difference', '差额', 'money'],
'match_status' => ['match_status', '匹配状态', 'text'],

View File

@ -787,7 +787,7 @@ $navigation = [
'directory' => [
'icon' => 'receipt_long',
'status' => '共享镜像',
'description' => '覆盖店铺收款账户账单各平台页签(总账备注=订单收入),按「店铺 + 订单号」匹配订单侧商品金额;一行 = 一个店铺的一个订单,商品金额只挂一次。「优惠金额」「冲减金额」另取总账备注=优惠收入 / 冲减收入的账单金额,不计入收入金额;差额=(收入金额+优惠金额+冲减金额)−商品金额,只反映真正对不上的部分。',
'description' => '覆盖店铺收款账户账单各平台页签(总账备注=订单收入),按「店铺 + 订单号」匹配订单侧商品金额;一行 = 一个店铺的一个订单,商品金额只挂一次。「优惠金额」「冲减金额」另取总账备注=优惠收入 / 冲减收入的账单金额,不计入收入金额;「种菜金额」是订单号同时出现在种菜明细表和聚水潭订单标签明细(是否种菜订单=是)里时该订单的收入金额,只是收入金额里的一部分,不参与差额;差额=(收入金额+优惠金额+冲减金额)−商品金额,只反映真正对不上的部分。',
'empty' => '店铺收款匹配订单明细尚未同步,请先执行 store-receipt:order-match:sync。',
'fields' => [
['name' => 'platform', 'label' => '平台'],
@ -797,6 +797,7 @@ $navigation = [
['name' => 'income_amount', 'label' => '收入金额', 'kind' => 'number'],
['name' => 'discount_amount', 'label' => '优惠金额', 'kind' => 'number'],
['name' => 'offset_amount', 'label' => '冲减金额', 'kind' => 'number'],
['name' => 'planting_amount', 'label' => '种菜金额', 'kind' => 'number'],
['name' => 'product_amount', 'label' => '商品金额', 'kind' => 'number'],
['name' => 'difference', 'label' => '差额', 'kind' => 'number'],
['name' => 'match_status', 'label' => '匹配状态'],

View File

@ -0,0 +1,46 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* 业绩明细新增「种菜金额」列(放在冲减金额后面):
* 订单号同时出现在「种菜明细表」和「聚水潭订单标签明细(是否种菜订单=是)」里时,
* 该订单的收入金额就是种菜金额;只展示,不参与差额计算(差额口径保持
* (收入金额+优惠金额+冲减金额)−商品金额,种菜金额本身就是收入金额的一部分)。
*/
return new class extends Migration
{
private function connection(): string
{
return (string) config('upstream_mirrors.connection', 'business_mirror');
}
public function up(): void
{
$schema = Schema::connection($this->connection());
if (! $schema->hasTable('store_receipt_order_match_rows')) {
return;
}
if (! $schema->hasColumn('store_receipt_order_match_rows', 'planting_amount')) {
$schema->table('store_receipt_order_match_rows', function (Blueprint $table): void {
$table->decimal('planting_amount', 20, 4)->nullable()->after('offset_amount');
});
}
}
public function down(): void
{
$schema = Schema::connection($this->connection());
if (! $schema->hasTable('store_receipt_order_match_rows')
|| ! $schema->hasColumn('store_receipt_order_match_rows', 'planting_amount')) {
return;
}
$schema->table('store_receipt_order_match_rows', function (Blueprint $table): void {
$table->dropColumn('planting_amount');
});
}
};

View File

@ -1041,6 +1041,7 @@
<span class="receipt-match-summary__item"><em>账单收入合计</em><strong>{{ number_format((float) $receiptStats['income_total'], 2) }}</strong></span>
<span class="receipt-match-summary__item"><em>优惠金额合计</em><strong>{{ number_format((float) ($receiptStats['discount_total'] ?? 0), 2) }}</strong></span>
<span class="receipt-match-summary__item"><em>冲减金额合计</em><strong>{{ number_format((float) ($receiptStats['offset_total'] ?? 0), 2) }}</strong></span>
<span class="receipt-match-summary__item"><em>种菜金额合计</em><strong>{{ number_format((float) ($receiptStats['planting_total'] ?? 0), 2) }}</strong></span>
<span class="receipt-match-summary__item"><em>订单商品金额合计</em><strong>{{ number_format((float) $receiptStats['goods_total'], 2) }}</strong></span>
<span class="receipt-match-summary__item"><em>差额合计(已匹配)</em><strong>{{ number_format((float) $receiptStats['difference_total'], 2) }}</strong></span>
</div>

View File

@ -96,6 +96,9 @@ class NavigationTest extends FeatureTestCase
->assertSee('data-column-key="store_name"', false)
->assertSee('data-column-key="online_order_no"', false)
->assertSee('data-column-key="income_amount"', false)
->assertSee('data-column-key="discount_amount"', false)
->assertSee('data-column-key="offset_amount"', false)
->assertSee('data-column-key="planting_amount"', false)
->assertSee('data-column-key="product_amount"', false)
->assertSee('data-column-key="difference"', false)
->assertSee('data-column-key="match_status"', false)