财务经营分析表接入推广费
从聚水潭平台费用(新)→ 推广费(店铺)按账期和店铺汇总推广费支出,写入财务经营分析表推广费用-有票及汇总、CSV导出。
This commit is contained in:
parent
97b5824aee
commit
23fc36c74c
@ -76,6 +76,7 @@ use Modules\EcommerceFinance\Repositories\JdPromotionAccountBalanceRepository;
|
||||
use Modules\EcommerceFinance\Repositories\JdWalletBillRepository;
|
||||
use Modules\EcommerceFinance\Repositories\NonJingxiRefundMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\PinduoduoSpreadBillRepository;
|
||||
use Modules\EcommerceFinance\Repositories\PromotionMarketingExpenseRepository;
|
||||
use Modules\EcommerceFinance\Repositories\ProductCatalogMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\ProductPriceMirrorRepository;
|
||||
use Modules\EcommerceFinance\Repositories\RawBoardPayablesRepository;
|
||||
@ -3555,6 +3556,8 @@ class Pages extends Controller
|
||||
$purchaseCostTotal = 0.0;
|
||||
$billInvoiceExpenseTotal = 0.0;
|
||||
$billNonInvoiceExpenseTotal = 0.0;
|
||||
$promotionInvoiceExpenseComplete = $rows !== [];
|
||||
$promotionInvoiceExpenseTotal = 0.0;
|
||||
$netProfitComplete = $rows !== [];
|
||||
$netProfitTotal = 0.0;
|
||||
/** @var array<string,true> $platformOptions */
|
||||
@ -3567,6 +3570,11 @@ class Pages extends Controller
|
||||
$purchaseCostTotal += (float) ($row['purchase_cost'] ?? 0);
|
||||
$billInvoiceExpenseTotal += (float) ($row['bill_invoice_expense'] ?? 0);
|
||||
$billNonInvoiceExpenseTotal += (float) ($row['bill_non_invoice_expense'] ?? 0);
|
||||
if ($row['promotion_invoice_expense'] === null) {
|
||||
$promotionInvoiceExpenseComplete = false;
|
||||
} else {
|
||||
$promotionInvoiceExpenseTotal += (float) $row['promotion_invoice_expense'];
|
||||
}
|
||||
if ($row['net_profit'] === null) {
|
||||
$netProfitComplete = false;
|
||||
} else {
|
||||
@ -3597,6 +3605,9 @@ class Pages extends Controller
|
||||
'purchase_cost_total' => round($purchaseCostTotal, 2),
|
||||
'bill_invoice_expense_total' => round($billInvoiceExpenseTotal, 2),
|
||||
'bill_non_invoice_expense_total' => round($billNonInvoiceExpenseTotal, 2),
|
||||
'promotion_invoice_expense_total' => $promotionInvoiceExpenseComplete
|
||||
? round($promotionInvoiceExpenseTotal, 2)
|
||||
: null,
|
||||
'net_profit_total' => $netProfitComplete ? round($netProfitTotal, 2) : null,
|
||||
],
|
||||
'financialAnalysisMonths' => $months,
|
||||
@ -3621,9 +3632,26 @@ class Pages extends Controller
|
||||
? array_map(static fn (string $value): string => $value . '-01', $months)
|
||||
: ($filters['month'] !== '' ? [$filters['month'] . '-01'] : []);
|
||||
|
||||
$promotionExpenseAvailable = true;
|
||||
try {
|
||||
$promotionExpenseBucketsByMonth = app(PromotionMarketingExpenseRepository::class)
|
||||
->promotionStoreExpenseBucketsForMonths(array_map(
|
||||
static fn (string $value): string => substr($value, 0, 7),
|
||||
$sourceMonths,
|
||||
));
|
||||
} catch (Throwable $exception) {
|
||||
report($exception);
|
||||
$promotionExpenseBucketsByMonth = [];
|
||||
$promotionExpenseAvailable = false;
|
||||
}
|
||||
|
||||
$rows = [];
|
||||
foreach ($sourceMonths as $sourceMonth) {
|
||||
foreach ($this->financialAnalysisAggregates($sourceMonth) as $values) {
|
||||
$reportMonth = substr($sourceMonth, 0, 7);
|
||||
foreach ($this->financialAnalysisAggregates(
|
||||
$sourceMonth,
|
||||
$promotionExpenseAvailable ? ($promotionExpenseBucketsByMonth[$reportMonth] ?? []) : null,
|
||||
) as $values) {
|
||||
if ($filters['platform'] !== '' && $values['platform'] !== $filters['platform']) {
|
||||
continue;
|
||||
}
|
||||
@ -3631,7 +3659,7 @@ class Pages extends Controller
|
||||
&& ! str_contains((string) $values['store_name'], $filters['store_name'])) {
|
||||
continue;
|
||||
}
|
||||
$rows[] = array_merge(['month' => substr($sourceMonth, 0, 7)], $values);
|
||||
$rows[] = array_merge(['month' => $reportMonth], $values);
|
||||
}
|
||||
}
|
||||
|
||||
@ -3667,16 +3695,20 @@ class Pages extends Controller
|
||||
*
|
||||
* 两张表都是几十万行,聚合结果按月份缓存 5 分钟;筛选在内存里做,避免每次开页重跑分组。
|
||||
*
|
||||
* @param array<string,float>|null $promotionExpenseBuckets
|
||||
* @return array<string,array<string,mixed>>
|
||||
*/
|
||||
private function financialAnalysisAggregates(string $reportMonth): array
|
||||
private function financialAnalysisAggregates(
|
||||
string $reportMonth,
|
||||
?array $promotionExpenseBuckets = null,
|
||||
): array
|
||||
{
|
||||
$connection = (string) config('upstream_mirrors.connection', 'business_mirror');
|
||||
|
||||
return cache()->remember(
|
||||
'financial-analysis-aggregates-v4:' . $reportMonth . ':' . $this->financialAnalysisMappingSignature(),
|
||||
'financial-analysis-aggregates-v5:' . $reportMonth . ':' . $this->financialAnalysisMappingSignature(),
|
||||
now()->addMinutes(5),
|
||||
function () use ($connection, $reportMonth): array {
|
||||
function () use ($connection, $reportMonth, $promotionExpenseBuckets): array {
|
||||
$rows = [];
|
||||
$incomeRows = DB::connection($connection)->table('store_receipt_order_match_rows')
|
||||
->where('report_month', $reportMonth)
|
||||
@ -3757,6 +3789,12 @@ class Pages extends Controller
|
||||
$row['company_name'] = $subject->company_name;
|
||||
$row['company_type'] = $subject->company_type;
|
||||
$row['taxpayer_type'] = $subject->taxpayer_type;
|
||||
if ($promotionExpenseBuckets !== null) {
|
||||
$row['promotion_invoice_expense'] = round(
|
||||
$promotionExpenseBuckets[$row['store_name']] ?? 0.0,
|
||||
2,
|
||||
);
|
||||
}
|
||||
|
||||
$costsAvailable = true;
|
||||
$totalCost = 0.0;
|
||||
|
||||
@ -183,6 +183,68 @@ class PromotionMarketingExpenseRepository
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚水潭「平台费用(新)→ 推广费(店铺)」按账期汇总的店铺推广费支出。
|
||||
*
|
||||
* @return array<string,float>
|
||||
*/
|
||||
public function promotionStoreExpenseBucketsForMonth(string $reportMonth): array
|
||||
{
|
||||
return $this->promotionStoreExpenseBucketsForMonths([$reportMonth])[$reportMonth] ?? [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param list<string> $reportMonths
|
||||
* @return array<string,array<string,float>>
|
||||
*/
|
||||
public function promotionStoreExpenseBucketsForMonths(array $reportMonths): array
|
||||
{
|
||||
$months = array_values(array_unique(array_filter(
|
||||
$reportMonths,
|
||||
static fn (mixed $month): bool => is_string($month)
|
||||
&& preg_match('/^\d{4}-\d{2}$/', $month) === 1,
|
||||
)));
|
||||
sort($months, SORT_STRING);
|
||||
if ($months === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$start = $months[0] . '-01';
|
||||
$endExclusive = CarbonImmutable::parse(end($months) . '-01', 'Asia/Shanghai')
|
||||
->addMonth()
|
||||
->toDateString();
|
||||
$cacheKey = 'promotion-marketing-expense:monthly-stores:'
|
||||
. hash('sha256', $start . '|' . $endExclusive);
|
||||
|
||||
return Cache::remember($cacheKey, self::CACHE_SECONDS, function () use ($start, $endExclusive): array {
|
||||
$buckets = [];
|
||||
foreach ($this->sources() as $source) {
|
||||
[$where, $bindings] = $this->where($source, $start, $endExclusive, '', true);
|
||||
$month = 'CONVERT(varchar(7), ' . $source['date'] . ', 23)';
|
||||
$sql = 'SELECT ' . $month . ' AS report_month, '
|
||||
. $source['store'] . ' AS store_name, '
|
||||
. 'COALESCE(SUM(' . $source['expense'] . '), 0) AS amount'
|
||||
. ' FROM ' . $source['table'] . ' c ' . $where
|
||||
. ' AND ' . $source['store'] . " IS NOT NULL AND LTRIM(RTRIM(" . $source['store'] . ")) <> N''"
|
||||
. ' GROUP BY ' . $month . ', ' . $source['store']
|
||||
. ' HAVING ABS(SUM(' . $source['expense'] . ')) > 0.004';
|
||||
foreach ($this->select($source['connection'], $sql, $bindings) as $row) {
|
||||
$reportMonth = trim((string) ($row['report_month'] ?? ''));
|
||||
$store = $source['store_label'] ?? trim((string) ($row['store_name'] ?? ''));
|
||||
if (preg_match('/^\d{4}-\d{2}$/', $reportMonth) !== 1 || $store === '') {
|
||||
continue;
|
||||
}
|
||||
$buckets[$reportMonth][$store] = round(
|
||||
($buckets[$reportMonth][$store] ?? 0.0) + (float) ($row['amount'] ?? 0),
|
||||
2,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $buckets;
|
||||
});
|
||||
}
|
||||
|
||||
/** @return array<int,string> */
|
||||
public function storeOptions(array $filters = []): array
|
||||
{
|
||||
|
||||
@ -84,7 +84,8 @@
|
||||
<div class="financial-analysis-note">
|
||||
口径:一行 = 一个月的一个平台的一个店铺。申报收入 = 账单收入 + 退款 + 收入冲减项,金额按源字段记账符号展示。
|
||||
采购成本取商品成本表汇总;账单费用-有票 / 无票按店铺收款账户账单和科目映射票据口径汇总。
|
||||
快递运费、安装费、推广费用、售后费用-小额打款、管理费用和人工费用尚未建立统一的店铺月汇总,显示为“—”;
|
||||
推广费用-有票取聚水潭「平台费用(新)→ 推广费(店铺)」按店铺月度支出汇总。
|
||||
快递运费、安装费、售后费用-小额打款、管理费用和人工费用尚未建立统一的店铺月汇总,显示为“—”;
|
||||
上述费用口径齐备后自动计算净利润。
|
||||
</div>
|
||||
|
||||
@ -97,6 +98,7 @@
|
||||
<div class="item"><span>采购成本合计</span><b>{{ $money($summary['purchase_cost_total'] ?? null) }}</b></div>
|
||||
<div class="item"><span>账单费用-有票合计</span><b>{{ $money($summary['bill_invoice_expense_total'] ?? null) }}</b></div>
|
||||
<div class="item"><span>账单费用-无票合计</span><b>{{ $money($summary['bill_non_invoice_expense_total'] ?? null) }}</b></div>
|
||||
<div class="item"><span>推广费用-有票合计</span><b>{{ $money($summary['promotion_invoice_expense_total'] ?? null) }}</b></div>
|
||||
<div class="item"><span>净利润合计</span><b>{{ $money($summary['net_profit_total'] ?? null) }}</b></div>
|
||||
</div>
|
||||
|
||||
|
||||
@ -10,6 +10,7 @@ use App\Utilities\SidebarPermissions;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Modules\EcommerceFinance\Repositories\PromotionMarketingExpenseRepository;
|
||||
use Modules\EcommerceFinance\View\BillingDetailsPageData;
|
||||
use Tests\Feature\FeatureTestCase;
|
||||
|
||||
@ -315,6 +316,12 @@ class NavigationTest extends FeatureTestCase
|
||||
]];
|
||||
}
|
||||
});
|
||||
$this->app->instance(PromotionMarketingExpenseRepository::class, new class {
|
||||
public function promotionStoreExpenseBucketsForMonths(array $reportMonths): array
|
||||
{
|
||||
return ['2026-11' => ['测试店铺' => 30]];
|
||||
}
|
||||
});
|
||||
|
||||
$this->loginAs()
|
||||
->get(route('business.pages.show', [
|
||||
@ -328,13 +335,15 @@ class NavigationTest extends FeatureTestCase
|
||||
->assertSeeText('880.00')
|
||||
->assertSeeText('300.00')
|
||||
->assertSeeText('10.00')
|
||||
->assertSeeText('20.00');
|
||||
->assertSeeText('20.00')
|
||||
->assertSeeText('30.00')
|
||||
->assertSeeText('推广费用-有票合计');
|
||||
|
||||
$csv = $this->loginAs()
|
||||
->get(route('business.financial-operations.financial-analysis.export'))
|
||||
->streamedContent();
|
||||
$this->assertStringContainsString(
|
||||
'2026-11,,,,淘系支付宝,测试店铺,1000.00,-40.00,-80.00,880.00,300.00,,,10.00,20.00',
|
||||
'2026-11,,,,淘系支付宝,测试店铺,1000.00,-40.00,-80.00,880.00,300.00,,,10.00,20.00,30.00',
|
||||
$csv,
|
||||
);
|
||||
}
|
||||
|
||||
@ -136,6 +136,29 @@ class PromotionMarketingExpenseRepositoryTest extends TestCase
|
||||
]));
|
||||
}
|
||||
|
||||
public function test_monthly_store_expense_buckets_merge_platform_sources(): void
|
||||
{
|
||||
$repository = $this->repository();
|
||||
$repository->monthlyStoreRows = [
|
||||
'tmall_spread_bills' => [
|
||||
['report_month' => '2026-10', 'store_name' => '天猫-甲店', 'amount' => 10],
|
||||
],
|
||||
'alibaba_bill' => [
|
||||
['report_month' => '2026-10', 'store_name' => '天猫-甲店', 'amount' => 20],
|
||||
],
|
||||
'pdd_spread_bills' => [
|
||||
['report_month' => '2026-10', 'store_name' => '拼多多-乙店', 'amount' => 30],
|
||||
],
|
||||
];
|
||||
|
||||
$this->assertEquals([
|
||||
'2026-10' => [
|
||||
'天猫-甲店' => 30.0,
|
||||
'拼多多-乙店' => 30.0,
|
||||
],
|
||||
], $repository->promotionStoreExpenseBucketsForMonths(['2026-10']));
|
||||
}
|
||||
|
||||
public function test_platform_and_store_summary_uses_the_installation_platform_categories(): void
|
||||
{
|
||||
$repository = $this->repository();
|
||||
@ -198,15 +221,26 @@ class PromotionMarketingExpenseRepositoryTest extends TestCase
|
||||
|
||||
public array $storeSummaryRows = [];
|
||||
|
||||
public array $monthlyStoreRows = [];
|
||||
|
||||
public array $queries = [];
|
||||
|
||||
protected function select(string $connection, string $sql, array $bindings): array
|
||||
{
|
||||
$this->queries[] = compact('connection', 'sql', 'bindings');
|
||||
foreach (array_merge(array_keys($this->histogramRows), array_keys($this->detailRows), array_keys($this->storeRows), array_keys($this->storeSummaryRows)) as $table) {
|
||||
foreach (array_merge(
|
||||
array_keys($this->histogramRows),
|
||||
array_keys($this->detailRows),
|
||||
array_keys($this->storeRows),
|
||||
array_keys($this->storeSummaryRows),
|
||||
array_keys($this->monthlyStoreRows),
|
||||
) as $table) {
|
||||
if (! str_contains($sql, $table)) {
|
||||
continue;
|
||||
}
|
||||
if (str_contains($sql, 'AS report_month')) {
|
||||
return $this->monthlyStoreRows[$table] ?? [];
|
||||
}
|
||||
if (str_contains($sql, 'GROUP BY') && str_contains($sql, 'AS store_name')) {
|
||||
return $this->storeSummaryRows[$table] ?? [];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user