@perrylink/dsh-einvoice 0.1.0-alpha.2 → 0.2.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -3
- package/lib/adapters/ofd-zip-xml.js +4 -4
- package/lib/core/invoice.d.ts +2 -0
- package/lib/core/invoice.js +14 -0
- package/lib/plugin/tools.js +9 -5
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,7 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
File-level Chinese e-invoice ingest, parse and pool for DeepSeek Harness (SKU-04 S1).
|
|
4
4
|
|
|
5
|
-
> **Status:
|
|
5
|
+
> **Status: file-level public demo (0.2.0-alpha.0) — PARTIAL, NOT COUNTED, NOT GA.**
|
|
6
|
+
> This package does **not** implement OCR/scanning, tax-bureau verification/receipt, tax filing, 30-year archive compliance, or real-invoice accuracy claims. `invoice.review_queue` / `invoice.record_receipt` are local-only; the legacy names are deprecated aliases. The customer evidence gate (2 enterprises + 1 bookkeeping firm + 100 real invoices) is still PARKED. S1 uses public/open-source and synthetic fixtures; its acceptance rows are `not counted` toward GA. Do not describe this package as GA-ready.
|
|
6
7
|
|
|
7
8
|
## Install
|
|
8
9
|
|
|
@@ -21,13 +22,15 @@ The bundle inserts one Host row. It requires the base profile's `tools` and `sto
|
|
|
21
22
|
| `invoice.parse` | Parse old OFD `Attachs/original_invoice.xml` or 数电票 `Tags/CustomTag.xml` + ObjectRef; return eight fields, lines, evidence and unknown fields | R0 |
|
|
22
23
|
| `invoice.query` | Query the invoice pool by entity, period, lifecycle or text | R0 |
|
|
23
24
|
| `invoice.dedup` | Group duplicates by invoice number or seller+date+total | R0 |
|
|
24
|
-
| `invoice.
|
|
25
|
-
| `invoice.
|
|
25
|
+
| `invoice.review_queue` | Queue parsed invoices for manual local review; optional buyer-tax-id mismatch flags. Old `invoice.verify` is a deprecated alias. | R1 local write |
|
|
26
|
+
| `invoice.record_receipt` | Hash a local receipt, append it, move lifecycle to receipt-recorded/quarantined; `verification_source=local_receipt`, `tax_verified=false`; old `invoice.receipt` is a deprecated alias. | R1 local write |
|
|
26
27
|
| `invoice.redlink` | Bind a negative red invoice to its blue invoice; store deltas | R1 local write |
|
|
27
28
|
| `invoice.archive` | Build a manifest + originals + receipts ZIP under the local archive directory | R1 local write |
|
|
28
29
|
| `invoice.check` | Verify manifest/member hashes, missing files and traversal | R0 |
|
|
29
30
|
| `invoice.restore` | Restore a verified archive into the local restore root | R1 local write |
|
|
30
31
|
|
|
32
|
+
Client: deferred; see `CLIENT-DECISION.md`. GA: blocked by the customer evidence gate; see `GA-CHECKLIST.md`.
|
|
33
|
+
|
|
31
34
|
S3 adds red/blue matching, deterministic archive packages, check and restore. S2 verification is manual/local (no tax-bureau network call).
|
|
32
35
|
|
|
33
36
|
## Configuration
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/** OFD (ZIP + XML) and standalone XML reader for SKU-04 S1. */
|
|
2
2
|
import { unzipSync } from 'fflate';
|
|
3
3
|
import { XMLParser } from 'fast-xml-parser';
|
|
4
|
-
import { InvoiceError, amountToCents, normalizeDate, normalizeTaxId, } from '../core/invoice.js';
|
|
4
|
+
import { InvoiceError, amountToCents, priceToCents, normalizeDate, normalizeTaxId, } from '../core/invoice.js';
|
|
5
5
|
export const PARSER_ID = 'ofd-zip-xml';
|
|
6
6
|
export const PARSER_VERSION = 'ofd-zip-xml@1';
|
|
7
7
|
const xmlParser = new XMLParser({
|
|
@@ -96,7 +96,7 @@ function parseOldInvoice(root) {
|
|
|
96
96
|
line_no: index + 1,
|
|
97
97
|
description: textAt(line, 'Item'),
|
|
98
98
|
quantity: textAt(line, 'Quantity') || undefined,
|
|
99
|
-
unit_price_cents: textAt(line, 'Price') ?
|
|
99
|
+
unit_price_cents: textAt(line, 'Price') ? priceToCents(textAt(line, 'Price')) : undefined,
|
|
100
100
|
amount_cents: amount ? amountToCents(amount) : 0,
|
|
101
101
|
tax_rate: textAt(line, 'TaxScheme') || undefined,
|
|
102
102
|
tax_cents: tax ? amountToCents(tax) : undefined,
|
|
@@ -240,7 +240,7 @@ function parseDigitalInvoice(root, textMap) {
|
|
|
240
240
|
line_no: index + 1,
|
|
241
241
|
description,
|
|
242
242
|
quantity: textMap.get(quantityRefs[index] ?? '') || undefined,
|
|
243
|
-
unit_price_cents: textMap.get(priceRefs[index] ?? '') ?
|
|
243
|
+
unit_price_cents: textMap.get(priceRefs[index] ?? '') ? priceToCents(textMap.get(priceRefs[index] ?? '') ?? '') : undefined,
|
|
244
244
|
amount_cents: amountText ? amountToCents(amountText) : 0,
|
|
245
245
|
tax_rate: textMap.get(taxSchemeRefs[index] ?? '') || undefined,
|
|
246
246
|
tax_cents: taxText ? amountToCents(taxText) : undefined,
|
|
@@ -299,7 +299,7 @@ function parseGenericInvoice(root) {
|
|
|
299
299
|
line_no: index + 1,
|
|
300
300
|
description: textAt(line, 'Description') || textAt(line, 'Item'),
|
|
301
301
|
quantity: textAt(line, 'Quantity') || undefined,
|
|
302
|
-
unit_price_cents: textAt(line, 'UnitPrice') ?
|
|
302
|
+
unit_price_cents: textAt(line, 'UnitPrice') ? priceToCents(textAt(line, 'UnitPrice')) : undefined,
|
|
303
303
|
amount_cents: textAt(line, 'Amount') ? amountToCents(textAt(line, 'Amount')) : 0,
|
|
304
304
|
tax_rate: textAt(line, 'TaxRate') || undefined,
|
|
305
305
|
tax_cents: textAt(line, 'TaxAmount') ? amountToCents(textAt(line, 'TaxAmount')) : undefined,
|
package/lib/core/invoice.d.ts
CHANGED
|
@@ -48,6 +48,8 @@ export declare class InvoiceError extends Error {
|
|
|
48
48
|
/** Parse a decimal amount (yuan) into integer cents without floating point. */
|
|
49
49
|
export declare function amountToCents(raw: unknown): number;
|
|
50
50
|
/** Normalize the common Chinese date spellings to YYYY-MM-DD. */
|
|
51
|
+
/** Unit prices may carry tax-calculation precision; round half-up to integer cents. */
|
|
52
|
+
export declare function priceToCents(raw: unknown): number;
|
|
51
53
|
export declare function normalizeDate(raw: unknown): string;
|
|
52
54
|
/** Validate the shape of a PRC taxpayer id without claiming checksum validity. */
|
|
53
55
|
export declare function normalizeTaxId(raw: unknown): string;
|
package/lib/core/invoice.js
CHANGED
|
@@ -37,6 +37,20 @@ export function amountToCents(raw) {
|
|
|
37
37
|
return Number(cents);
|
|
38
38
|
}
|
|
39
39
|
/** Normalize the common Chinese date spellings to YYYY-MM-DD. */
|
|
40
|
+
/** Unit prices may carry tax-calculation precision; round half-up to integer cents. */
|
|
41
|
+
export function priceToCents(raw) {
|
|
42
|
+
if (typeof raw !== 'string' && typeof raw !== 'number')
|
|
43
|
+
throw new InvoiceError('E_INV_FIELD_MISSING', 'price is not a value');
|
|
44
|
+
const normalized = String(raw).trim().replace(/[¥¥,,\s]/g, '');
|
|
45
|
+
const match = /^(-?)(\d+)(?:\.(\d{1,12}))?$/.exec(normalized);
|
|
46
|
+
if (!match)
|
|
47
|
+
throw new InvoiceError('E_INV_FIELD_MISSING', `price is not a decimal: ${String(raw).slice(0, 80)}`);
|
|
48
|
+
const sign = match[1] === '-' ? -1n : 1n;
|
|
49
|
+
const whole = BigInt(match[2]);
|
|
50
|
+
const fraction = BigInt((match[3] ?? '').padEnd(12, '0'));
|
|
51
|
+
const cents = (whole * 100n + (fraction + 5000000000n) / 10000000000n);
|
|
52
|
+
return Number(sign * cents);
|
|
53
|
+
}
|
|
40
54
|
export function normalizeDate(raw) {
|
|
41
55
|
if (typeof raw !== 'string' || raw.trim() === '') {
|
|
42
56
|
throw new InvoiceError('E_INV_FIELD_MISSING', 'issue date is empty');
|
package/lib/plugin/tools.js
CHANGED
|
@@ -449,7 +449,7 @@ export function createTools(deps) {
|
|
|
449
449
|
},
|
|
450
450
|
});
|
|
451
451
|
const verify = defineTool({
|
|
452
|
-
name: 'invoice.
|
|
452
|
+
name: 'invoice.review_queue',
|
|
453
453
|
description: 'Queue parsed invoices for manual/semi-automatic verification. S2 has no tax-bureau network call: it records a pending local queue and optional entity-tax-id mismatch flags.',
|
|
454
454
|
parameters: {
|
|
455
455
|
invoice_ids: { type: 'array', items: { type: 'string' }, description: 'Optional subset; defaults to parsed invoices' },
|
|
@@ -533,7 +533,7 @@ export function createTools(deps) {
|
|
|
533
533
|
},
|
|
534
534
|
});
|
|
535
535
|
const receipt = defineTool({
|
|
536
|
-
name: 'invoice.
|
|
536
|
+
name: 'invoice.record_receipt',
|
|
537
537
|
description: 'Record a local verification receipt for one parsed invoice: hash the receipt file, append an immutable receipt record, and move the invoice to verified/quarantined. Local R1 write; no tax-bureau write.',
|
|
538
538
|
parameters: {
|
|
539
539
|
invoice_id: { type: 'string', required: true },
|
|
@@ -552,6 +552,8 @@ export function createTools(deps) {
|
|
|
552
552
|
receipt_sha256: { type: 'string', required: true },
|
|
553
553
|
duplicate: { type: 'boolean', required: true },
|
|
554
554
|
lifecycle: { type: 'string', required: true },
|
|
555
|
+
verification_source: { type: 'string', required: true },
|
|
556
|
+
tax_verified: { type: 'boolean', required: true },
|
|
555
557
|
},
|
|
556
558
|
},
|
|
557
559
|
render: (_args, value) => text(`receipt ${value.receipt_id} (${value.duplicate ? 'duplicate' : 'recorded'}), invoice=${value.lifecycle}`),
|
|
@@ -571,7 +573,7 @@ export function createTools(deps) {
|
|
|
571
573
|
const receiptSha = sha256Buffer(buffer);
|
|
572
574
|
const existing = (record.receipts ?? []).find((item) => item.receipt_sha256 === receiptSha);
|
|
573
575
|
if (existing) {
|
|
574
|
-
return { receipt_id: existing.receipt_id, receipt_sha256: receiptSha, duplicate: true, lifecycle: record.lifecycle };
|
|
576
|
+
return { receipt_id: existing.receipt_id, receipt_sha256: receiptSha, duplicate: true, lifecycle: record.lifecycle, verification_source: 'local_receipt', tax_verified: false };
|
|
575
577
|
}
|
|
576
578
|
const now = new Date().toISOString();
|
|
577
579
|
const receiptRecord = {
|
|
@@ -600,7 +602,7 @@ export function createTools(deps) {
|
|
|
600
602
|
updated_at: now,
|
|
601
603
|
};
|
|
602
604
|
await deps.table.put(record.doc_id, updated);
|
|
603
|
-
return { receipt_id: receiptRecord.receipt_id, receipt_sha256: receiptSha, duplicate: false, lifecycle };
|
|
605
|
+
return { receipt_id: receiptRecord.receipt_id, receipt_sha256: receiptSha, duplicate: false, lifecycle, verification_source: 'local_receipt', tax_verified: false };
|
|
604
606
|
},
|
|
605
607
|
});
|
|
606
608
|
const archiveDir = path.join(deps.baseDir, 'archives');
|
|
@@ -891,5 +893,7 @@ export function createTools(deps) {
|
|
|
891
893
|
};
|
|
892
894
|
},
|
|
893
895
|
});
|
|
894
|
-
|
|
896
|
+
const legacyVerify = { ...verify, name: 'invoice.verify', description: 'Deprecated alias for invoice.review_queue; local review queue only, no tax-bureau verification.' };
|
|
897
|
+
const legacyReceipt = { ...receipt, name: 'invoice.receipt', description: 'Deprecated alias for invoice.record_receipt; local receipt only, no tax-bureau verification.' };
|
|
898
|
+
return [ingest, parse, query, dedup, verify, legacyVerify, receipt, legacyReceipt, redlink, archive, check, restore];
|
|
895
899
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@perrylink/dsh-einvoice",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-alpha.0",
|
|
4
4
|
"description": "File-level Chinese e-invoice (OFD/XML) ingest, parse and pool for DeepSeek Harness (SKU-04 S1).",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./lib/plugin/index.js",
|