199 lines
7.9 KiB
PHP
199 lines
7.9 KiB
PHP
<?php
|
|
|
|
namespace Tests\Feature\Business;
|
|
|
|
use App\Business\CourierPayableSummaryStore;
|
|
use App\Models\Auth\Permission;
|
|
use Illuminate\Database\Schema\Blueprint;
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Schema;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Mockery;
|
|
use Modules\EcommerceFinance\Repositories\InstallationDetailMirrorRepository;
|
|
use Modules\EcommerceFinance\Repositories\PromotionMarketingExpenseRepository;
|
|
use Modules\EcommerceFinance\View\BillingDetailsPageData;
|
|
use Tests\Feature\FeatureTestCase;
|
|
|
|
class FinancialAnalysisCourierFreightTest extends FeatureTestCase
|
|
{
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->user->attachPermission(
|
|
Permission::where('name', 'read-admin-panel')->firstOrFail()
|
|
);
|
|
}
|
|
|
|
public function test_financial_analysis_sums_courier_fees_by_month_and_store_without_deduplication(): void
|
|
{
|
|
Storage::fake('uploads');
|
|
Cache::flush();
|
|
|
|
config([
|
|
'database.connections.business_mirror' => [
|
|
'driver' => 'sqlite',
|
|
'database' => ':memory:',
|
|
'prefix' => '',
|
|
],
|
|
'upstream_mirrors.connection' => 'business_mirror',
|
|
]);
|
|
DB::purge('business_mirror');
|
|
|
|
Schema::connection('business_mirror')->create('store_receipt_order_match_rows', function (Blueprint $table): void {
|
|
$table->date('report_month');
|
|
$table->string('platform');
|
|
$table->string('store_name');
|
|
$table->decimal('income_amount', 20, 4)->default(0);
|
|
$table->decimal('discount_amount', 20, 4)->nullable();
|
|
$table->decimal('offset_amount', 20, 4)->nullable();
|
|
$table->decimal('default_compensation_amount', 20, 4)->nullable();
|
|
$table->decimal('refund_amount', 20, 4)->nullable();
|
|
});
|
|
Schema::connection('business_mirror')->create('store_receipt_product_cost_rows', function (Blueprint $table): void {
|
|
$table->date('report_month');
|
|
$table->string('platform');
|
|
$table->string('store_name');
|
|
$table->decimal('product_cost', 20, 4)->nullable();
|
|
});
|
|
|
|
foreach ([
|
|
['platform' => '淘系支付宝', 'income_amount' => 1000],
|
|
['platform' => '淘系微信', 'income_amount' => 100],
|
|
] as $income) {
|
|
DB::connection('business_mirror')->table('store_receipt_order_match_rows')->insert([
|
|
'report_month' => '2026-11-01',
|
|
'platform' => $income['platform'],
|
|
'store_name' => '测试店铺',
|
|
'income_amount' => $income['income_amount'],
|
|
'discount_amount' => 0,
|
|
'offset_amount' => 0,
|
|
'default_compensation_amount' => 0,
|
|
'refund_amount' => 0,
|
|
]);
|
|
}
|
|
DB::connection('business_mirror')->table('store_receipt_product_cost_rows')->insert([
|
|
'report_month' => '2026-11-01',
|
|
'platform' => '淘系支付宝',
|
|
'store_name' => '测试店铺',
|
|
'product_cost' => 300,
|
|
]);
|
|
|
|
foreach ([
|
|
['part' => '成品运费', 'freight_amount' => 10],
|
|
['part' => '配件运费', 'freight_amount' => 5],
|
|
] as $base) {
|
|
DB::table('courier_freight_rows')->insert([
|
|
'company_id' => $this->company->id,
|
|
'source' => '提供成本11月汇总',
|
|
'part' => $base['part'],
|
|
'transaction_no' => 'T-COURIER',
|
|
'shop_name' => '测试店铺',
|
|
'freight_amount' => $base['freight_amount'],
|
|
'created_at' => '2026-11-20 10:00:00',
|
|
'updated_at' => '2026-11-20 10:00:00',
|
|
]);
|
|
}
|
|
|
|
$store = app(CourierPayableSummaryStore::class);
|
|
$this->putSummarySnapshot($store, 'freight-difference', [
|
|
['日期', '交易单号', '差异(结算运费-提供成本运费)'],
|
|
['2026-11-02', 'T-COURIER', '0.5'],
|
|
['2026-11-03', 'T-COURIER', '0.25'],
|
|
['2026-10-15', 'T-COURIER', '1000'],
|
|
]);
|
|
$this->putSummarySnapshot($store, 'other-surcharge-summary', [
|
|
['日期', '交易单号', '金额'],
|
|
['2026-11-04', 'T-COURIER', '1.5'],
|
|
['2026-11-05', 'T-COURIER', '3'],
|
|
['2026-10-15', 'T-COURIER', '1000'],
|
|
]);
|
|
$this->putSummarySnapshot($store, 'return-fee', [
|
|
['日期', '交易订单', '结算金额'],
|
|
['2026-11-06', 'T-COURIER', '2.25'],
|
|
['2026-11-07', 'T-COURIER', '0.25'],
|
|
['2026-10-15', 'T-COURIER', '1000'],
|
|
]);
|
|
|
|
$this->app->instance(BillingDetailsPageData::class, new class {
|
|
public function expenseBucketsForMonth(string $reportMonth): array
|
|
{
|
|
return [];
|
|
}
|
|
});
|
|
$this->app->instance(PromotionMarketingExpenseRepository::class, new class {
|
|
public function promotionStoreExpenseBucketsForMonths(array $reportMonths): array
|
|
{
|
|
return [];
|
|
}
|
|
});
|
|
$installation = Mockery::mock(InstallationDetailMirrorRepository::class);
|
|
$installation->shouldReceive('settlementAmountBucketsForCustomerReviewMonths')
|
|
->andReturn([]);
|
|
$this->app->instance(InstallationDetailMirrorRepository::class, $installation);
|
|
|
|
$this->loginAs()->get(route('business.pages.show', [
|
|
'section' => 'financial-operations',
|
|
'page' => 'financial-analysis',
|
|
'month' => '2026-11',
|
|
]))
|
|
->assertOk()
|
|
->assertViewHas('content', function (array $content): bool {
|
|
$storeRows = collect($content['financialAnalysisRows']->items())
|
|
->where('store_name', '测试店铺');
|
|
$freight = $storeRows->sum(static fn (array $row): float => (float) $row['freight']);
|
|
|
|
return $storeRows->count() === 2
|
|
&& round($freight, 2) === 22.75
|
|
&& round(max($storeRows->pluck('freight')->map(static fn ($value): float => (float) $value)->all()), 2) === 22.75;
|
|
})
|
|
->assertViewHas('content', static function (array $content): bool {
|
|
return round((float) $content['financialAnalysisSummary']['courier_freight_expense_total'], 2) === 22.75;
|
|
})
|
|
->assertSeeText('快递运费合计')
|
|
->assertSeeText('22.75');
|
|
|
|
$csv = $this->loginAs()
|
|
->get(route('business.financial-operations.financial-analysis.export', [
|
|
'month' => '2026-11',
|
|
]))
|
|
->streamedContent();
|
|
|
|
$csvRows = array_map(
|
|
static fn (string $line): array => str_getcsv($line, ',', '"', '\\'),
|
|
preg_split('/\r\n|\n|\r/', trim($csv)) ?: [],
|
|
);
|
|
$storeRows = array_values(array_filter(
|
|
array_slice($csvRows, 1),
|
|
static fn (array $row): bool => ($row[5] ?? '') === '测试店铺',
|
|
));
|
|
|
|
$this->assertCount(2, $storeRows);
|
|
$this->assertSame(
|
|
22.75,
|
|
round(array_sum(array_map(
|
|
static fn (array $row): float => (float) ($row[11] ?? 0),
|
|
$storeRows,
|
|
)), 2),
|
|
);
|
|
}
|
|
|
|
/** @param list<list<string>> $rows */
|
|
private function putSummarySnapshot(
|
|
CourierPayableSummaryStore $store,
|
|
string $page,
|
|
array $rows,
|
|
): void {
|
|
$headers = array_shift($rows);
|
|
Storage::disk('uploads')->put($store->path((int) $this->company->id, $page), json_encode([
|
|
'version' => 1,
|
|
'headers' => $headers,
|
|
'rows' => $rows,
|
|
'source_name' => $page . '.csv',
|
|
'imported_at' => now()->toIso8601String(),
|
|
'imported_by' => $this->user->id,
|
|
], JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR));
|
|
}
|
|
}
|