From 68f0e83d53fdf054826dae2f7bfc6da6b129c244 Mon Sep 17 00:00:00 2001 From: chenxi Date: Wed, 2 Dec 2026 11:02:20 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(=E5=BA=97=E9=93=BA=E6=94=B6=E6=AC=BE):?= =?UTF-8?q?=20=E5=BC=80=E6=94=BE=E8=B4=A6=E6=88=B7=E8=B4=A6=E5=8D=95?= =?UTF-8?q?=E5=8F=AA=E8=AF=BB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 为店铺收款账户账单全部 12 个页签开放只读外部 API。 --- docs/店铺收款账户账单外部接口.md | 146 ++++++++++++ .../Controllers/Api/StoreReceivingBills.php | 135 ++++++++++++ routes/api.php | 9 + .../Business/StoreReceivingBillsApiTest.php | 208 ++++++++++++++++++ 4 files changed, 498 insertions(+) create mode 100644 docs/店铺收款账户账单外部接口.md create mode 100644 modules/EcommerceFinance/Http/Controllers/Api/StoreReceivingBills.php create mode 100644 tests/Feature/Business/StoreReceivingBillsApiTest.php diff --git a/docs/店铺收款账户账单外部接口.md b/docs/店铺收款账户账单外部接口.md new file mode 100644 index 000000000..449b213b8 --- /dev/null +++ b/docs/店铺收款账户账单外部接口.md @@ -0,0 +1,146 @@ +# 店铺收款账户账单外部接口 + +用于向外部系统只读开放「账单流水数据 › 店铺收款账户账单」各账户页签的数据。接口复用页面现有的数据源、筛选、汇总、余额和总账备注口径。 + +## 鉴权与公司范围 + +接口使用系统现有 API 鉴权: + +```http +Authorization: Basic +``` + +如果系统启用了 OAuth,也可以使用: + +```http +Authorization: Bearer +``` + +调用账号需要具备: + +- API 基础权限 `read-api`; +- 「账单流水数据 › 店铺收款账户账单」页面权限。 + +公司范围使用 `X-Company: ` 请求头或 `company_id` 查询参数。未指定时使用调用账号的首个公司。 + +## 账户目录 + +```http +GET /api/ecommerce-finance/store-receiving-bills +``` + +返回 12 个可用页签及各自的数据地址: + +| `account` | 页面名称 | +| --- | --- | +| `taobao-alipay` | 全部支付宝账户 | +| `taobao-wechat` | 淘系微信账户 | +| `pinduoduo` | 拼多多账户 | +| `jd-wallet` | 京东网银钱包账户 | +| `jd-transactions` | 京东账单 | +| `jingxi` | 京喜账单 | +| `douyin` | 抖音账户 | +| `dewu` | 得物账户 | +| `dewu-alipay` | 得物支付宝账户 | +| `vipshop` | 唯品会MP账户 | +| `vipshop-jx` | 唯品会JX账户 | +| `xiaohongshu` | 小红书账户 | + +## 分页查询 + +```http +GET /api/ecommerce-finance/store-receiving-bills/{account} +``` + +常用参数: + +| 参数 | 说明 | +| --- | --- | +| `start` | 开始日期,`YYYY-MM-DD` | +| `end` | 结束日期,`YYYY-MM-DD` | +| `store_name` | 原始店铺/店铺名称 | +| `ledger_remark` | 总账备注 | +| `bill_item` | 账单项目 | +| `keyword` | 关键词;各页签沿用页面实际匹配字段 | +| `sort` | `occurred_at`、`income`、`expense`、`balance` 或 `record_code` | +| `dir` | `asc` 或 `desc` | +| `page` | 页码,从 1 开始 | +| `limit` | 每页条数,1–100,默认 10 | + +日期规则: + +- `start` 和 `end` 需要同时提供; +- 两者都不提供时,默认查询当前自然月,与页面默认口径一致; +- 不提供日期不会自动扫描全部历史数据。 + +每个 `account` 支持的筛选字段与页面一致。例如京东账单支持 `keyword_match`,唯品会按账单期间筛选。 + +示例: + +```bash +curl --get 'https://caiwu.example.com/api/ecommerce-finance/store-receiving-bills/taobao-alipay' \ + --user 'api-user@example.com:password' \ + -H 'Accept: application/json' \ + -H 'X-Company: 1' \ + --data-urlencode 'start=2026-11-01' \ + --data-urlencode 'end=2026-11-30' \ + --data-urlencode 'store_name=天猫-亿家达旗舰店' \ + --data-urlencode 'page=1' \ + --data-urlencode 'limit=100' +``` + +响应沿用页面的分页结构,并增加 `account` 标识: + +```json +{ + "data": [], + "total": 0, + "current_page": 1, + "last_page": 1, + "per_page": 100, + "summary": { + "income": 0, + "expense": 0, + "balance": 0, + "count": 0 + }, + "columns": { + "title": "全部支付宝账户", + "groups": [], + "columns": [] + }, + "account": { + "slug": "taobao-alipay", + "title": "全部支付宝账户" + } +} +``` + +不同账户的 `summary` 和 `columns.columns` 字段会按页面实际列配置返回。 + +## 筛选选项 + +```http +GET /api/ecommerce-finance/store-receiving-bills/{account}/filter-options +``` + +返回该页签可用的 `shops`、`bill_items`、`ledger_remarks`,供外部系统生成筛选条件。 + +## 分页与同步建议 + +1. 先调用账户目录,按目标页签选择 `account`。 +2. 按月份或明确日期范围查询,`limit` 使用 100。 +3. 依次读取到 `last_page`,不要只取第一页。 +4. 外部同步记录最后成功账期与 `account`,下次从下一个账期继续读取。 +5. 接口为只读 GET 接口,不暴露新增、修改、删除或 CSV 导出能力。 + +## 状态码 + +| 状态码 | 说明 | +| --- | --- | +| `200` | 成功 | +| `401` | 未提供有效凭据 | +| `403` | 调用账号没有 API 或账单页面权限 | +| `404` | 账户类型或公司不存在 | +| `422` | 日期、筛选参数或分页参数不合法 | +| `503` | 对应账户的共享业务镜像尚未完成首次同步,稍后重试 | diff --git a/modules/EcommerceFinance/Http/Controllers/Api/StoreReceivingBills.php b/modules/EcommerceFinance/Http/Controllers/Api/StoreReceivingBills.php new file mode 100644 index 000000000..657d52f27 --- /dev/null +++ b/modules/EcommerceFinance/Http/Controllers/Api/StoreReceivingBills.php @@ -0,0 +1,135 @@ + */ + private const ACCOUNTS = [ + 'taobao-alipay' => ['title' => '全部支付宝账户', 'controller' => AlipayBills::class], + 'taobao-wechat' => ['title' => '淘系微信账户', 'controller' => TaobaoWechatAccountBills::class], + 'pinduoduo' => ['title' => '拼多多账户', 'controller' => PinduoduoAccountBills::class], + 'jd-wallet' => ['title' => '京东网银钱包账户', 'controller' => AccountBills::class], + 'jd-transactions' => ['title' => '京东账单', 'controller' => AccountBills::class], + 'jingxi' => ['title' => '京喜账单', 'controller' => AccountBills::class], + 'douyin' => ['title' => '抖音账户', 'controller' => AccountBills::class], + 'dewu' => ['title' => '得物账户', 'controller' => DewuAccountBills::class], + 'dewu-alipay' => ['title' => '得物支付宝账户', 'controller' => AccountBills::class], + 'vipshop' => ['title' => '唯品会MP账户', 'controller' => AccountBills::class], + 'vipshop-jx' => ['title' => '唯品会JX账户', 'controller' => AccountBills::class], + 'xiaohongshu' => ['title' => '小红书账户', 'controller' => AccountBills::class], + ]; + + public function accounts(): JsonResponse + { + $this->authorizeStoreReceivingBills(); + + $accounts = collect(self::ACCOUNTS) + ->map(fn (array $account, string $slug): array => [ + 'slug' => $slug, + 'title' => $account['title'], + 'url' => route('api.ecommerce-finance.store-receiving-bills.index', ['account' => $slug]), + ]) + ->values() + ->all(); + + return response()->json(['data' => $accounts]); + } + + public function index(Request $request, string $account): Response + { + $this->authorizeStoreReceivingBills(); + $this->prepareDataRequest($request, $account); + + $controller = app(self::ACCOUNTS[$account]['controller']); + $parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : []; + $response = app()->call([$controller, 'index'], $parameters); + + return $this->decorateResponse($response, $account); + } + + public function filterOptions(string $account): Response + { + $this->authorizeStoreReceivingBills(); + $this->ensureAccountExists($account); + + $controller = app(self::ACCOUNTS[$account]['controller']); + $parameters = $controller instanceof AccountBills ? ['bill_type' => $account] : []; + $response = app()->call([$controller, 'filterOptions'], $parameters); + + return $this->decorateResponse($response, $account); + } + + private function prepareDataRequest(Request $request, string $account): void + { + $this->ensureAccountExists($account); + + // 外部接口固定返回 JSON,不允许通过 export=1 触发 CSV 下载。 + $request->query->remove('export'); + + $start = trim((string) $request->query->get('start', '')); + $end = trim((string) $request->query->get('end', '')); + + if (($start === '') !== ($end === '')) { + throw ValidationException::withMessages([ + 'start' => ['开始日期和结束日期需要同时提供。'], + 'end' => ['开始日期和结束日期需要同时提供。'], + ]); + } + + // 与页面默认口径一致:未指定日期时只读当前自然月,避免无界扫描全表。 + // 指定日期时完全遵循调用方参数,不做账期改写。 + if ($start === '') { + $request->query->set('start', now()->startOfMonth()->toDateString()); + $request->query->set('end', now()->endOfMonth()->toDateString()); + } + } + + private function ensureAccountExists(string $account): void + { + abort_unless(isset(self::ACCOUNTS[$account]), 404, '未知的店铺收款账户类型。'); + } + + private function authorizeStoreReceivingBills(): void + { + $section = collect(SidebarPermissions::navigationFor(user())) + ->firstWhere('slug', 'billing-transactions'); + $page = collect($section['pages'] ?? []) + ->firstWhere('slug', 'store-receiving-bills'); + + abort_if(empty($page), 403, '当前账号没有店铺收款账户账单权限。'); + } + + private function decorateResponse(Response $response, string $account): Response + { + if (! $response instanceof JsonResponse || ! is_array($response->getData(true))) { + return $response; + } + + $payload = $response->getData(true); + $payload['account'] = [ + 'slug' => $account, + 'title' => self::ACCOUNTS[$account]['title'], + ]; + $response->setData($payload); + + return $response; + } +} diff --git a/routes/api.php b/routes/api.php index 9bfb72338..d987c6337 100644 --- a/routes/api.php +++ b/routes/api.php @@ -1,6 +1,7 @@ 'api.'], function () { + // 店铺收款账户账单:只读分页数据、账户目录和筛选项。 + Route::get('ecommerce-finance/store-receiving-bills', [StoreReceivingBills::class, 'accounts']) + ->name('ecommerce-finance.store-receiving-bills.accounts'); + Route::get('ecommerce-finance/store-receiving-bills/{account}/filter-options', [StoreReceivingBills::class, 'filterOptions']) + ->name('ecommerce-finance.store-receiving-bills.filter-options'); + Route::get('ecommerce-finance/store-receiving-bills/{account}', [StoreReceivingBills::class, 'index']) + ->name('ecommerce-finance.store-receiving-bills.index'); + // Ping Route::get('ping', 'Common\Ping@pong')->name('ping'); diff --git a/tests/Feature/Business/StoreReceivingBillsApiTest.php b/tests/Feature/Business/StoreReceivingBillsApiTest.php new file mode 100644 index 000000000..422e7c591 --- /dev/null +++ b/tests/Feature/Business/StoreReceivingBillsApiTest.php @@ -0,0 +1,208 @@ +loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.accounts')) + ->assertOk() + ->assertJsonCount(12, 'data') + ->assertJsonFragment(['slug' => 'taobao-alipay', 'title' => '全部支付宝账户']) + ->assertJsonFragment(['slug' => 'xiaohongshu', 'title' => '小红书账户']); + } + + public function test_taobao_alipay_api_uses_existing_query_and_returns_account_metadata(): void + { + $repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class); + $repository->shouldReceive('paginate') + ->once() + ->with( + Mockery::on(fn (array $filters): bool => $filters['store_name'] === '天猫-亿家达旗舰店' + && $filters['start'] === '2026-11-01' + && $filters['end'] === '2026-11-30'), + 'occurred_at', + 'desc', + 2, + 20, + ) + ->andReturn($this->emptyAlipayResult(2, 20)); + $this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([ + 'store_name' => '天猫-亿家达旗舰店', + 'start' => '2026-11-01', + 'end' => '2026-11-30', + 'page' => 2, + 'limit' => 20, + ])) + ->assertOk() + ->assertJsonPath('account.slug', 'taobao-alipay') + ->assertJsonPath('account.title', '全部支付宝账户') + ->assertJsonPath('current_page', 2) + ->assertJsonPath('per_page', 20); + } + + public function test_api_defaults_to_current_month_when_dates_are_omitted(): void + { + $this->travelTo('2026-12-02 10:00:00'); + + $repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class); + $repository->shouldReceive('paginate') + ->once() + ->with( + Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-12-01' + && $filters['end'] === '2026-12-31'), + 'occurred_at', + 'desc', + 1, + 10, + ) + ->andReturn($this->emptyAlipayResult()); + $this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay'])) + ->assertOk(); + } + + public function test_generic_account_tab_uses_the_shared_account_bills_query(): void + { + $repository = Mockery::mock(StoreReceivingDewuAlipayBillMirrorRepository::class); + $repository->shouldReceive('paginate') + ->once() + ->with( + Mockery::on(fn (array $filters): bool => $filters['start'] === '2026-11-01' + && $filters['end'] === '2026-11-30'), + 'occurred_at', + 'desc', + 1, + 5, + ) + ->andReturn($this->emptyAlipayResult(1, 5)); + $this->app->instance(StoreReceivingDewuAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'dewu-alipay']) . '?' . http_build_query([ + 'start' => '2026-11-01', + 'end' => '2026-11-30', + 'limit' => 5, + ])) + ->assertOk() + ->assertJsonPath('account.slug', 'dewu-alipay') + ->assertJsonPath('account.title', '得物支付宝账户') + ->assertJsonPath('columns.title', '得物支付宝账户'); + } + + public function test_api_rejects_an_incomplete_date_range(): void + { + $repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class); + $repository->shouldNotReceive('paginate'); + $this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withExceptionHandling() + ->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?start=2026-11-01') + ->assertStatus(422) + ->assertJsonValidationErrors(['start', 'end']); + } + + public function test_filter_options_endpoint_uses_the_same_page_options(): void + { + $repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class); + $repository->shouldReceive('filterOptionsForChannel') + ->once() + ->with('alipayCSV', StoreReceivingAlipayBillMirrorRepository::DATASET, true) + ->andReturn([ + 'shops' => ['天猫-亿家达旗舰店'], + 'bill_items' => ['订单收入'], + ]); + $this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.filter-options', ['account' => 'taobao-alipay'])) + ->assertOk() + ->assertJsonPath('account.slug', 'taobao-alipay') + ->assertJsonPath('shops.0', '天猫-亿家达旗舰店') + ->assertJsonPath('bill_items.0', '订单收入'); + } + + public function test_export_parameter_still_returns_json_data(): void + { + $repository = Mockery::mock(StoreReceivingAlipayBillMirrorRepository::class); + $repository->shouldReceive('paginate') + ->once() + ->andReturn($this->emptyAlipayResult()); + $this->app->instance(StoreReceivingAlipayBillMirrorRepository::class, $repository); + + $this->loginAs(); + + $this->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'taobao-alipay']) . '?' . http_build_query([ + 'start' => '2026-11-01', + 'end' => '2026-11-30', + 'export' => 1, + ])) + ->assertOk() + ->assertHeader('content-type', 'application/json') + ->assertJsonPath('account.slug', 'taobao-alipay'); + } + + public function test_unknown_account_returns_not_found(): void + { + $this->loginAs(); + + $this->withExceptionHandling() + ->withHeaders($this->apiHeaders()) + ->getJson(route('api.ecommerce-finance.store-receiving-bills.index', ['account' => 'unknown-account'])) + ->assertNotFound(); + } + + /** @return array */ + private function emptyAlipayResult(int $page = 1, int $perPage = 10): array + { + return [ + 'data' => [], + 'total' => 0, + 'current_page' => $page, + 'last_page' => 1, + 'per_page' => $perPage, + 'summary' => [ + 'income' => 0, + 'expense' => 0, + 'opening_balance' => 0, + 'opening_balance_date' => null, + 'opening_balance_source' => null, + 'balance' => 0, + 'count' => 0, + ], + ]; + } + + private function apiHeaders(): array + { + return [ + 'Authorization' => 'Basic ' . base64_encode('test@company.com:123456'), + ]; + } +} -- 2.47.2 From d8cc58d4f74b238b2db70eaefac78c8872295a6f Mon Sep 17 00:00:00 2001 From: limeng <1131177064@qq.com> Date: Wed, 2 Dec 2026 11:06:30 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20=E7=A7=8D=E8=8F=9C=E6=8D=9F=E8=80=97?= =?UTF-8?q?=E8=AF=86=E5=88=AB=E6=8B=8DA+B=E5=B9=B6=E5=85=8D=E6=89=A3?= =?UTF-8?q?=E6=8C=87=E5=AE=9A=E6=9D=A1=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/PlantingLossCalculator.php | 33 +++++++++-- .../planting-loss.blade.php | 6 +- .../Business/PlantingWorkOrderPagesTest.php | 4 +- .../PlantingLossCalculatorTest.php | 55 +++++++++++++++++++ 4 files changed, 87 insertions(+), 11 deletions(-) diff --git a/modules/EcommerceFinance/Services/PlantingLossCalculator.php b/modules/EcommerceFinance/Services/PlantingLossCalculator.php index 4eb94ad16..fbc6967cd 100644 --- a/modules/EcommerceFinance/Services/PlantingLossCalculator.php +++ b/modules/EcommerceFinance/Services/PlantingLossCalculator.php @@ -16,6 +16,7 @@ use Modules\EcommerceFinance\Repositories\ShippingOrderRepository; * 2. 用线上订单号去匹配发货数据(jst_oms_shipping)中 M-1、M 两个月发货的交易单号,匹配到的保留; * 3. 任务备注或卖家备注写了“拍A发B”或“拍A发A”的不扣款,不论是否匹配到发货都单独列出发给运营; * 4. 实际只发了「信誉订单虚拟条码」「透明挂钩」这类商品的不扣款; + * XF001-JX、DF- 开头条码仅免对应商品,混合发货的普通商品仍计算扣款; * 5. 其余匹配到的作为扣款项,扣款金额 = Σ 发货商品成本价 × 数量 + 该订单在发货数据中的运费(2026-10-19 更正), * 成本价按发货条形码(=商品编码)取普通商品资料。未匹配到发货的不计入种菜损耗。 */ @@ -86,13 +87,13 @@ class PlantingLossCalculator return substr($window['from'], 0, 7) . '~' . $month; } - /** 命中的免扣字样(拍A发B / 拍A发A),未命中返回 null。全角、大小写、空格均兼容。 */ + /** 命中的免扣字样(拍A发B / 拍A发A / 拍A+B),未命中返回 null。全角、大小写、空格均兼容。 */ public static function exemptionKeyword(string ...$remarks): ?string { foreach ($remarks as $remark) { $text = mb_strtoupper((string) preg_replace('/\s+/u', '', mb_convert_kana($remark, 'as'))); - if (preg_match('/拍A发([AB])/u', $text, $match) === 1) { - return '拍A发' . $match[1]; + if (preg_match('/拍A发[AB]|拍A\+B(?![A-Z0-9])/u', $text, $match) === 1) { + return $match[0]; } } @@ -101,6 +102,9 @@ class PlantingLossCalculator public static function isFreeGoods(string $barcode, string $productName): bool { + if (self::isExemptBarcode($barcode)) { + return true; + } foreach (self::FREE_GOODS_KEYWORDS as $keyword) { if (str_contains($productName, $keyword) || str_contains($barcode, $keyword)) { return true; @@ -110,6 +114,14 @@ class PlantingLossCalculator return false; } + /** 只免该商品,不触发虚拟条码的整单免扣规则。 */ + public static function isExemptBarcode(string $barcode): bool + { + $code = strtoupper(trim($barcode)); + + return $code === 'XF001-JX' || str_starts_with($code, 'DF-'); + } + public static function isVirtualOrderItem(string $barcode, string $productName): bool { $code = strtoupper(trim($barcode)); @@ -139,7 +151,7 @@ class PlantingLossCalculator */ public function rows(string $month): array { - $key = 'planting-loss:rows:v3:' . $month; + $key = 'planting-loss:rows:v4:' . $month; $cached = Cache::get($key); if (is_array($cached)) { return $cached; @@ -178,6 +190,7 @@ class PlantingLossCalculator $deductLines = []; $missingCost = []; $cents = 0; + $exemptBarcodes = []; $virtualOrder = false; foreach ($shipment['lines'] ?? [] as $line) { if (self::isVirtualOrderItem($line['barcode'], $line['product_name'])) { @@ -186,6 +199,9 @@ class PlantingLossCalculator } foreach ($virtualOrder ? [] : ($shipment['lines'] ?? []) as $line) { if (self::isFreeGoods($line['barcode'], $line['product_name'])) { + if (self::isExemptBarcode($line['barcode'])) { + $exemptBarcodes[$line['barcode']] = true; + } continue; } $cost = $costs[$line['barcode']] ?? null; @@ -215,14 +231,19 @@ class PlantingLossCalculator // 拍A发B/拍A发A 不论是否匹配到发货都单独列出发给运营(2026-10-20 财务确认);匹配发货只用于挑出真实发货的单。 $keyword = self::exemptionKeyword((string) $row['task_remark'], (string) $row['seller_remark']); + $exemptionBasis = '备注含“' . $keyword . '”' . ($keyword === '拍A+B' ? '(按拍A发B处理)' : '') + . ($shipment === null ? '(未匹配到发货)' : ''); [$result, $basis] = match (true) { - $keyword !== null => [self::RESULT_EXEMPT, '备注含“' . $keyword . '”' . ($shipment === null ? '(未匹配到发货)' : '')], + $keyword !== null => [self::RESULT_EXEMPT, $exemptionBasis], $shipment === null => [self::RESULT_UNMATCHED, self::shippingLabel($month) . ' 发货数据中无此交易单号'], $virtualOrder => [self::RESULT_FREE_GOODS, '发货含信誉订单虚拟条码/透明挂钩,整单不扣款'], - $deductLines === [] => [self::RESULT_FREE_GOODS, '只发了虚拟条码/透明挂钩/无实物商品'], + $deductLines === [] => [self::RESULT_FREE_GOODS, $exemptBarcodes !== [] ? '只发了免扣条码/无实物商品' : '只发了虚拟条码/透明挂钩/无实物商品'], $missingText !== '' => [self::RESULT_DEDUCT, $missingText . ',扣款金额未含缺失部分'], default => [self::RESULT_DEDUCT, '已匹配发货,按成本价×数量+运费扣款'], }; + if ($exemptBarcodes !== [] && $keyword === null && ! $virtualOrder) { + $basis .= ';免扣条码:' . implode('、', array_keys($exemptBarcodes)); + } $row['loss_result'] = $result; $row['loss_basis'] = $basis; diff --git a/resources/views/business/pages/billing-transactions/planting-loss.blade.php b/resources/views/business/pages/billing-transactions/planting-loss.blade.php index 5eaaf4ac1..398eb1eda 100644 --- a/resources/views/business/pages/billing-transactions/planting-loss.blade.php +++ b/resources/views/business/pages/billing-transactions/planting-loss.blade.php @@ -13,7 +13,7 @@ @endphp
-

{{ $plantingFilters['month'] }} 月种菜报销明细(完成时间 {{ substr($plantingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingWindow['to'])->subDay()->format('Y-m-d') }})的线上订单号,匹配 {{ $shippingLabel }} 发货数据(发货时间 {{ substr($plantingShippingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingShippingWindow['to'])->subDay()->format('Y-m-d') }})中的交易单号。任务备注或卖家备注写了“拍A发B / 拍A发A”的(不论是否匹配到发货)、以及匹配到的单中只发了信誉订单虚拟条码/透明挂钩的不扣款,其余按发货商品成本价×数量+该单运费扣款。

+

{{ $plantingFilters['month'] }} 月种菜报销明细(完成时间 {{ substr($plantingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingWindow['to'])->subDay()->format('Y-m-d') }})的线上订单号,匹配 {{ $shippingLabel }} 发货数据(发货时间 {{ substr($plantingShippingWindow['from'], 0, 10) }} 至 {{ \Illuminate\Support\Carbon::parse($plantingShippingWindow['to'])->subDay()->format('Y-m-d') }})中的交易单号。任务备注或卖家备注写了“拍A发B / 拍A发A / 拍A+B”的(不论是否匹配到发货)、以及匹配到的单中只发了信誉订单虚拟条码/透明挂钩的不扣款,XF001-JX、DF- 开头条码仅免对应商品,混合发货的普通商品继续扣款;其余按发货商品成本价×数量+该单运费扣款。

{{ $syncLabels[$plantingSync['code']] ?? $plantingSync['code'] }}@if($plantingSync['last_succeeded_at']) · {{ \Illuminate\Support\Str::of($plantingSync['last_succeeded_at'])->substr(0, 16) }}@endif {{ $shippingLabels[$plantingShippingSync['code'] ?? 'unavailable'] ?? $plantingShippingSync['code'] }}@if($plantingShippingSync['last_succeeded_at'] ?? null) · {{ \Illuminate\Support\Str::of($plantingShippingSync['last_succeeded_at'])->substr(0, 16) }}@endif @@ -39,10 +39,10 @@
当月种菜报销{{ number_format($plantingSummary['total']) }}匹配到发货 {{ number_format($plantingSummary['matched']) }} 单
扣款金额(元){{ $money($deduct['deduction_amount']) }}扣款 {{ number_format($deduct['count']) }} 单@if($deduct['cost_missing'] > 0) · {{ number_format($deduct['cost_missing']) }} 单成本价/运费缺失@endif 不扣款(拍A发B/拍A发A){{ number_format($exempt['count']) }}需发运营 · 最终报销 {{ $money($exempt['reimbursement_amount']) }} - 不扣款(虚拟条码/透明挂钩){{ number_format($free['count']) }}最终报销 {{ $money($free['reimbursement_amount']) }} + 不扣款(虚拟条码/透明挂钩等){{ number_format($free['count']) }}最终报销 {{ $money($free['reimbursement_amount']) }} 未匹配发货(不计入){{ number_format($unmatched['count']) }}最终报销 {{ $money($unmatched['reimbursement_amount']) }}
-
计算口径

① 取该月种菜报销明细:工单类型“种菜报销”、状态“已完成”,完成时间 M 月 2 日 00:00 至次月 1 日 23:59:59(与「种菜报销」页同一口径)。② 用线上订单号(交易单号)精确匹配「快递应付 → 发货数据」中发货时间在上月 1 日至本月末的交易单号,匹配到的保留,同一单多个包裹/商品合并为一行。③ 任务备注或卖家备注写了“拍A发B”或“拍A发A”(不区分全半角、大小写和空格)的不做扣款,不论是否匹配到发货都单独列出,「导出拍A发B(发运营)」按种菜报销字段导出。④ 发货数据里该交易单号下出现“信誉订单虚拟条码”(X00006/X00002)或透明挂钩(条形码 GG 开头)的整单不扣款(同单号下的其它商品视为合并发货的别的订单);只发了“无实物”商品的也不扣款。⑤ 其余匹配到的为扣款项:扣款金额 = Σ 发货条形码(商品编码)在「普通商品资料」中的成本价 × 发货数量 + 该订单在「发货数据」中的运费合计;普通商品资料里没有成本价(空或 0)的商品、发货数据里运费为空的订单分别标为“成本价缺失”“运费缺失”,缺失部分不计入扣款金额,补齐后自动重算。未匹配到发货的单不计入种菜损耗,仅供核对。

+
计算口径

① 取该月种菜报销明细:工单类型“种菜报销”、状态“已完成”,完成时间 M 月 2 日 00:00 至次月 1 日 23:59:59(与「种菜报销」页同一口径)。② 用线上订单号(交易单号)精确匹配「快递应付 → 发货数据」中发货时间在上月 1 日至本月末的交易单号,匹配到的保留,同一单多个包裹/商品合并为一行。③ 任务备注或卖家备注写了“拍A发B”“拍A发A”或“拍A+B”(按拍A发B处理,不区分全半角、大小写和空格)的不做扣款,不论是否匹配到发货都单独列出,「导出拍A发B(发运营)」按种菜报销字段导出。④ 发货数据里该交易单号下出现“信誉订单虚拟条码”(X00006/X00002)或透明挂钩(条形码 GG 开头)的整单不扣款(同单号下的其它商品视为合并发货的别的订单);只发了“无实物”商品的也不扣款。⑤ 发货条码 XF001-JX 和 DF- 开头条码免对应商品成本(不区分大小写),混合发货的普通商品继续按成本价和既有订单运费计算;仅有免扣商品的整单不扣款。⑥ 其余匹配到的为扣款项:扣款金额 = Σ 发货条形码(商品编码)在「普通商品资料」中的成本价 × 发货数量 + 该订单在「发货数据」中的运费合计;普通商品资料里没有成本价(空或 0)的商品、发货数据里运费为空的订单分别标为“成本价缺失”“运费缺失”,缺失部分不计入扣款金额,补齐后自动重算。未匹配到发货的单不计入种菜损耗,仅供核对。

@if(($plantingShippingSync['code'] ?? '') !== 'succeeded' && ! $plantingError)
发货数据当前状态为“{{ $shippingLabels[$plantingShippingSync['code'] ?? 'unavailable'] ?? $plantingShippingSync['code'] }}”{{ ($plantingShippingSync['warning'] ?? '') !== '' ? ':' . $plantingShippingSync['warning'] : '' }},匹配结果可能不完整。
@endif @if($plantingError)@endif
diff --git a/tests/Feature/Business/PlantingWorkOrderPagesTest.php b/tests/Feature/Business/PlantingWorkOrderPagesTest.php index 2353c5d1f..aff4b8453 100644 --- a/tests/Feature/Business/PlantingWorkOrderPagesTest.php +++ b/tests/Feature/Business/PlantingWorkOrderPagesTest.php @@ -103,7 +103,7 @@ class PlantingWorkOrderPagesTest extends FeatureTestCase { $this->mockData([ $this->row(), - $this->row(['work_order_no' => 'GD-EXEMPT', 'task_remark' => '拍A发B', 'loss_result' => Calculator::RESULT_EXEMPT]), + $this->row(['work_order_no' => 'GD-EXEMPT', 'task_remark' => '拍A+B+DF-A3ZP001501', 'loss_result' => Calculator::RESULT_EXEMPT]), ]); $response = $this->loginAs()->get(route('business.planting-data.planting-loss.export', [ @@ -114,7 +114,7 @@ class PlantingWorkOrderPagesTest extends FeatureTestCase $response->assertOk(); $csv = $response->streamedContent(); $this->assertStringContainsString('工单编号,线上订单号,任务备注,损耗判定', $csv); - $this->assertStringContainsString("GD-EXEMPT,'3316392626048007769,拍A发B", $csv); + $this->assertStringContainsString("GD-EXEMPT,'3316392626048007769,拍A+B+DF-A3ZP001501", $csv); $this->assertStringNotContainsString('GD565967381', $csv); // 发运营:按种菜报销字段导出,忽略损耗页的列设置。 diff --git a/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php b/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php index 3025bd1d5..cdba295ba 100644 --- a/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php +++ b/tests/Unit/EcommerceFinance/PlantingLossCalculatorTest.php @@ -143,11 +143,21 @@ class PlantingLossCalculatorTest extends TestCase $this->assertSame('拍A发B', Calculator::exemptionKeyword('拍A发B+GG003-SZ')); $this->assertSame('拍A发A', Calculator::exemptionKeyword('', '客户要求 拍a 发A')); $this->assertSame('拍A发B', Calculator::exemptionKeyword('CCDD', '拍a发b,供货价一致')); + $this->assertSame('拍A+B', Calculator::exemptionKeyword('拍A+B+DF-A3ZP001501')); + $this->assertSame('拍A+B', Calculator::exemptionKeyword('', '客户要求 拍a + b')); + $this->assertNull(Calculator::exemptionKeyword('拍A+BB', '拍A+B123')); $this->assertNull(Calculator::exemptionKeyword('AABB', '拍B发A')); $this->assertNull(Calculator::exemptionKeyword('转挂钩发货', '')); $this->assertTrue(Calculator::isFreeGoods('XN001', '信誉订单虚拟条码')); $this->assertTrue(Calculator::isFreeGoods('GG003-SZ', '透明挂钩3个')); $this->assertFalse(Calculator::isFreeGoods('DNZ-01', '儿童学习椅')); + $this->assertTrue(Calculator::isFreeGoods('xf001-jx', '普通名称')); + $this->assertTrue(Calculator::isFreeGoods(' df-A3ZP001501 ', '普通名称')); + $this->assertFalse(Calculator::isExemptBarcode('XF001-JX-OTHER')); + $this->assertFalse(Calculator::isExemptBarcode('ADF-123')); + $this->assertFalse(Calculator::isExemptBarcode('DF123')); + $this->assertFalse(Calculator::isVirtualOrderItem('XF001-JX', '普通名称')); + $this->assertFalse(Calculator::isVirtualOrderItem('DF-123', '普通名称')); $this->assertTrue(Calculator::isFreeGoods('tcxj', '书柜 晒图有奖励(无实物)')); $this->assertTrue(Calculator::isVirtualOrderItem('X00006', '')); $this->assertTrue(Calculator::isVirtualOrderItem('GG003-SZ', '透明挂钩3个')); @@ -236,4 +246,49 @@ class PlantingLossCalculatorTest extends TestCase $this->assertSame('GG003-SZ', $row['product_code']); $this->assertSame('2026-09-02 00:00:00', $row['completed_at']); } + + public function test_ab_shorthand_exempts_real_goods_even_when_freight_is_missing(): void + { + $this->workOrder(1, 'GD635802033', '3316534251275002174', '2026-11-05 11:22:44', [], '拍A+B+DF-A3ZP001501'); + $this->workOrder(2, 'GD-SELLER-AB', 'NO-SHIPMENT', '2026-11-06 10:00:00', [], '', '拍 a + b'); + $this->ship('3316534251275002174', '2026-10-28 10:00:00', 'YT001', 5.01, 'DF-A3ZP001501', '实物商品'); + DB::connection('business_mirror')->table('fixture_sku')->insert([ + 'autoid' => 5, 'sku_id' => 'DF-A3ZP001501', 'sku_type' => 'normal', 'cost_price' => 5.01, + ]); + DB::connection('planting_shipping')->table('jst_oms_shipping')->update(['freight' => null]); + + $rows = collect(app(Calculator::class)->rows('2026-11'))->keyBy('work_order_no'); + foreach ($rows as $row) { + $this->assertSame(Calculator::RESULT_EXEMPT, $row['loss_result']); + $this->assertNull($row['deduction_amount']); + $this->assertFalse($row['cost_missing']); + } + $this->assertCount(2, $rows); + $this->assertSame('备注含“拍A+B”(按拍A发B处理)', $rows['GD635802033']['loss_basis']); + $this->assertSame('备注含“拍A+B”(按拍A发B处理)(未匹配到发货)', $rows['GD-SELLER-AB']['loss_basis']); + } + + public function test_exempt_barcodes_skip_only_their_goods_in_mixed_shipments(): void + { + $this->workOrder(1, 'GD-ONLY-EXEMPT', 'ONLY-EXEMPT', '2026-11-05 11:22:44'); + $this->workOrder(2, 'GD-MIXED-EXEMPT', 'MIXED-EXEMPT', '2026-11-05 11:22:44'); + foreach (['ONLY-EXEMPT', 'MIXED-EXEMPT'] as $order) { + $this->ship($order, '2026-11-01 10:00:00', 'YT001', 20, 'xf001-jx', '普通商品'); + $this->ship($order, '2026-11-01 10:00:00', 'YT001', 30, 'df-123', '另一商品'); + } + // 免扣商品没有成本价也不应报缺失;混合订单保留普通商品成本与既有订单运费。 + $this->ship('MIXED-EXEMPT', '2026-11-01 10:00:00', 'YT001', 40.6, 'DNZ-01', '儿童学习椅', 2); + DB::connection('planting_shipping')->table('jst_oms_shipping')->update(['freight' => 0]); + DB::connection('planting_shipping')->table('jst_oms_shipping')->where('barcode', 'DNZ-01')->update(['freight' => 3.2]); + $rows = collect(app(Calculator::class)->rows('2026-11'))->keyBy('work_order_no'); + + $this->assertSame(Calculator::RESULT_FREE_GOODS, $rows['GD-ONLY-EXEMPT']['loss_result']); + $this->assertNull($rows['GD-ONLY-EXEMPT']['deduction_amount']); + $this->assertFalse($rows['GD-ONLY-EXEMPT']['cost_missing']); + $this->assertSame(Calculator::RESULT_DEDUCT, $rows['GD-MIXED-EXEMPT']['loss_result']); + $this->assertSame('204.20', $rows['GD-MIXED-EXEMPT']['deduction_amount']); + $this->assertSame("DNZ-01 × 2 × 100.5 = 201.00\n运费 3.20", $rows['GD-MIXED-EXEMPT']['deduction_detail']); + $this->assertFalse($rows['GD-MIXED-EXEMPT']['cost_missing']); + $this->assertStringContainsString('免扣条码:xf001-jx、df-123', $rows['GD-MIXED-EXEMPT']['loss_basis']); + } } -- 2.47.2