@company-six/qbosync 0.3.1 → 0.5.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 +50 -3
- package/dist/client.d.ts +18 -0
- package/dist/client.js +36 -1
- package/dist/contract/answers.d.ts +5 -2
- package/dist/contract/bill.d.ts +271 -0
- package/dist/contract/bill.js +130 -0
- package/dist/contract/codes.d.ts +56 -0
- package/dist/contract/codes.js +65 -0
- package/dist/contract/index.d.ts +182 -2
- package/dist/contract/index.js +5 -2
- package/dist/contract/protocol.d.ts +2 -2
- package/dist/contract/protocol.js +2 -2
- package/dist/contract/statusReport.d.ts +2 -0
- package/package.json +2 -2
- package/sql/002_daily_report.sql +260 -260
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @company-six/qbosync
|
|
2
2
|
|
|
3
|
-
Connect your app to QBO Sync, which posts your sales to each company's QuickBooks. Your app sends invoices and credit memos; QBO Sync checks them, posts them and tells your app what happened to each one.
|
|
3
|
+
Connect your app to QBO Sync, which posts your sales and costs to each company's QuickBooks. Your app sends invoices and credit memos, and bills and vendor credits if it records what the company owes its vendors; QBO Sync checks them, posts them and tells your app what happened to each one.
|
|
4
4
|
|
|
5
5
|
Server-side only (Node 20 or later): it holds your app's tokens. TypeScript types are included (TypeScript 5 or later).
|
|
6
6
|
|
|
@@ -105,7 +105,54 @@ Each document is checked against the contract first; one that fails is not sent
|
|
|
105
105
|
|
|
106
106
|
**A document is sent once.** QBO Sync keeps it exactly as first received, so `version` is 1 and a later version is refused. To correct an invoice, send a credit memo, or have a person roll it back in QBO Sync. Sending again after a failure is always safe: QBO Sync answers `unchanged`.
|
|
107
107
|
|
|
108
|
-
|
|
108
|
+
**Item codes and products.** QuickBooks puts every invoice and credit memo line on a product, and QBO Sync picks it by the line's `item_code`: the product the company chose for your code in QBO Sync's Setup → Products, else the QuickBooks product with that SKU, else (by default) a new product named after the line. Send real SKUs if you have a catalogue, or a few fixed category codes (`SALES`, `SHIPPING`) if you don't, the same code for the same thing every time. A company can set your app to never create products; then a line whose code has no product waits in QBO Sync until the company chooses one, and its status report says so. Nothing changes in what you send.
|
|
109
|
+
|
|
110
|
+
**Listing your codes (0.5.0).** If you send a few fixed codes, list them once, each with a label, so every company sees them by name in Setup → Products (item codes) and Setup → Cost codes (bill cost codes) before your first document arrives:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
const listed = await qbosync.declareCodes({
|
|
114
|
+
item_codes: [
|
|
115
|
+
{ code: "SALES", label: "Products, decoration and other charges" },
|
|
116
|
+
{ code: "SHIPPING", label: "Shipping and freight", description: "Every shipping line" },
|
|
117
|
+
],
|
|
118
|
+
cost_codes: [{ code: "freight", label: "Inbound freight" }],
|
|
119
|
+
});
|
|
120
|
+
if (!listed.ok) console.error(listed.error);
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
Each list you send replaces your whole list of that kind (a code left out is removed); a kind you leave out is kept. It is checked against the contract before anything is sent, changes nothing about posting, and is safe to call again with the same lists, for example each time your app starts. `declaredCodes()` reads back what QBO Sync holds. An app with real catalogue SKUs lists nothing.
|
|
124
|
+
|
|
125
|
+
Apps send invoices, credit memos, bills and vendor credits. Payments, refunds and payouts reach QBO Sync through its own connectors, matched to your invoices by `order_ref`, so give every invoice the `order_ref` of its order. Paying a bill is a money movement too: apps never send one.
|
|
126
|
+
|
|
127
|
+
### Bills and vendor credits (0.5.0)
|
|
128
|
+
|
|
129
|
+
A bill is what the company owes a vendor; a vendor credit is what a vendor gave back. QBO Sync posts them as QuickBooks Bills and Vendor Credits.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import type { BillDTO } from "@company-six/qbosync/contract";
|
|
133
|
+
|
|
134
|
+
const bill: BillDTO = {
|
|
135
|
+
schema_version: "1", source_ref: "po-2207-bill", company_key: "Acme", version: 1, status: "active",
|
|
136
|
+
type: "bill",
|
|
137
|
+
number: "BT-55120", // the vendor's bill number
|
|
138
|
+
issue_date: "2026-10-18",
|
|
139
|
+
terms_code: "net_30", // or due_date; QuickBooks works the date out from terms
|
|
140
|
+
currency: "USD",
|
|
141
|
+
vendor: { ref: "vendor-77", name: "Blank Tee Supply" }, // one QuickBooks vendor per ref
|
|
142
|
+
customer: { ref: "customer-3141", name: "Corner Market" }, // optional: whose sale this cost belongs to
|
|
143
|
+
lines: [
|
|
144
|
+
{ description: "Freight", amount_cents: 1500, cost_code: "freight" }, // an account line
|
|
145
|
+
{ description: "Black tee, M", amount_cents: 19800, item_code: "TEE-BLK-M", quantity: 48, unit_cost_cents: 412.5 }, // an item line
|
|
146
|
+
],
|
|
147
|
+
total_cents: 21300,
|
|
148
|
+
};
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
- A line is an **account line** (`cost_code`: your word for what was paid for) or an **item line** (`item_code`, `quantity`, `unit_cost_cents`: a product bought, by SKU). Each company maps your cost codes to its accounts once, in QBO Sync's Setup; a bill whose code is not mapped waits, and its status report says so. An item line posts on the product the company chose for its `item_code` in Setup → Products (the same choice your invoices use), else the QuickBooks product with that SKU; a bill never creates a product, so with neither it fails, or waits until the company chooses one if it has set your app to never create products.
|
|
152
|
+
- `customer` (on the bill, or on one line) puts the cost on that customer for job costing; it is not billed to them again.
|
|
153
|
+
- A vendor credit has the same fields, no terms, and may name its bill with `applies_to_bill_ref`. QuickBooks offers the credit when the company next pays that vendor.
|
|
154
|
+
- `number` is unique per vendor among your bills. When QuickBooks already holds a bill from that vendor with that number, the bill waits for a person in QBO Sync to link it or post it anyway.
|
|
155
|
+
- Like an invoice, a bill is kept as first received: correct it with a vendor credit.
|
|
109
156
|
|
|
110
157
|
## 6. Receive status reports
|
|
111
158
|
|
|
@@ -202,7 +249,7 @@ async function reportDay(day: string) { // "2026-11-08"
|
|
|
202
249
|
|
|
203
250
|
const rows = docs.map((doc, i) => ({
|
|
204
251
|
run_id: run.id,
|
|
205
|
-
document_type: doc.document_type, // "invoice" | "credit_memo"
|
|
252
|
+
document_type: doc.document_type, // "invoice" | "credit_memo" | "bill" | "vendor_credit"
|
|
206
253
|
source_ref: doc.source_ref,
|
|
207
254
|
company_key: doc.company_key,
|
|
208
255
|
document_id: doc.id,
|
package/dist/client.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { type CollectResult, type ConnectRequestStore, type OpenResult } from "./connectRequest.ts";
|
|
2
2
|
import { type ConnectionStore, type Log } from "./connection.ts";
|
|
3
|
+
import { type CodesDeclaration, type DeclaredCodes } from "./contract/index.ts";
|
|
3
4
|
import { type SignInRole } from "./contract/protocol.ts";
|
|
4
5
|
import { type AppCompanyStatus, type ReceiveResult, type ReportEntry, type SendOutcome } from "./contract/answers.ts";
|
|
5
6
|
import { type CheckedStatusReports } from "./statusReports.ts";
|
|
@@ -136,6 +137,23 @@ export declare class QbosyncClient {
|
|
|
136
137
|
ok: true;
|
|
137
138
|
documents: ReportEntry[];
|
|
138
139
|
} | Failure>;
|
|
140
|
+
/**
|
|
141
|
+
* Lists the codes the app uses, each with a label, so a company sees them in QBO Sync's Setup before any
|
|
142
|
+
* document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost codes. Each kind
|
|
143
|
+
* sent is the app's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
|
|
144
|
+
* Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
|
|
145
|
+
* Safe to call again with the same list, for example each time the app starts.
|
|
146
|
+
*/
|
|
147
|
+
declareCodes(input: CodesDeclaration): Promise<{
|
|
148
|
+
ok: true;
|
|
149
|
+
codes: DeclaredCodes;
|
|
150
|
+
} | Failure>;
|
|
151
|
+
/** The codes QBO Sync holds for the app, as last listed with `declareCodes`. */
|
|
152
|
+
declaredCodes(): Promise<{
|
|
153
|
+
ok: true;
|
|
154
|
+
codes: DeclaredCodes;
|
|
155
|
+
} | Failure>;
|
|
156
|
+
private codesAnswer;
|
|
139
157
|
/** Which of the app's companies are connected to QBO Sync, with their QuickBooks and syncing. */
|
|
140
158
|
listCompanies(): Promise<{
|
|
141
159
|
ok: true;
|
package/dist/client.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { collectConnectRequest, openConnectRequest } from "./connectRequest.js";
|
|
2
2
|
import { currentCredential, sendRenewingOn401 } from "./connection.js";
|
|
3
|
-
import { checkDocument } from "./contract/index.js";
|
|
3
|
+
import { checkDocument, codesDeclarationSchema, formatIssue } from "./contract/index.js";
|
|
4
4
|
import { MAX_DOCUMENTS_PER_REQUEST, MAX_REPORT_REFS } from "./contract/protocol.js";
|
|
5
5
|
import { endedReasonInWords, outcomeOf } from "./contract/answers.js";
|
|
6
6
|
import { checkStatusReports, SigningKeys } from "./statusReports.js";
|
|
@@ -219,6 +219,41 @@ export class QbosyncClient {
|
|
|
219
219
|
}
|
|
220
220
|
return { ok: true, documents };
|
|
221
221
|
}
|
|
222
|
+
// ---- codes
|
|
223
|
+
/**
|
|
224
|
+
* Lists the codes the app uses, each with a label, so a company sees them in QBO Sync's Setup before any
|
|
225
|
+
* document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost codes. Each kind
|
|
226
|
+
* sent is the app's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
|
|
227
|
+
* Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
|
|
228
|
+
* Safe to call again with the same list, for example each time the app starts.
|
|
229
|
+
*/
|
|
230
|
+
async declareCodes(input) {
|
|
231
|
+
const checked = codesDeclarationSchema.safeParse(input);
|
|
232
|
+
if (!checked.success) {
|
|
233
|
+
const reasons = checked.error.issues.map(formatIssue).join("; ");
|
|
234
|
+
return { ok: false, reason: "rejected", error: `The codes were not sent: ${reasons}`, detail: reasons };
|
|
235
|
+
}
|
|
236
|
+
const called = await this.call("/api/v1/codes", {
|
|
237
|
+
method: "PUT",
|
|
238
|
+
headers: { "Content-Type": "application/json" },
|
|
239
|
+
body: JSON.stringify(checked.data),
|
|
240
|
+
});
|
|
241
|
+
return this.codesAnswer(called);
|
|
242
|
+
}
|
|
243
|
+
/** The codes QBO Sync holds for the app, as last listed with `declareCodes`. */
|
|
244
|
+
async declaredCodes() {
|
|
245
|
+
return this.codesAnswer(await this.call("/api/v1/codes"));
|
|
246
|
+
}
|
|
247
|
+
async codesAnswer(called) {
|
|
248
|
+
if (!called.ok)
|
|
249
|
+
return called;
|
|
250
|
+
const body = (await called.res.json().catch(() => null));
|
|
251
|
+
if (!called.res.ok)
|
|
252
|
+
return failureOf(called.res, body);
|
|
253
|
+
if (!Array.isArray(body?.item_codes) || !Array.isArray(body?.cost_codes))
|
|
254
|
+
return unexpected(called.res.status, "QBO Sync's answer has no codes lists");
|
|
255
|
+
return { ok: true, codes: { item_codes: body.item_codes, cost_codes: body.cost_codes } };
|
|
256
|
+
}
|
|
222
257
|
// ---- companies and people
|
|
223
258
|
/** Which of the app's companies are connected to QBO Sync, with their QuickBooks and syncing. */
|
|
224
259
|
async listCompanies() {
|
|
@@ -47,7 +47,10 @@ export interface PostedLine {
|
|
|
47
47
|
quantity: number;
|
|
48
48
|
amount_cents: number;
|
|
49
49
|
}
|
|
50
|
-
/**
|
|
50
|
+
/**
|
|
51
|
+
* What was posted, in the app's own terms. A credit memo has no discount. A bill or vendor credit has no shipping or
|
|
52
|
+
* tax (0), and an account line reports quantity 1.
|
|
53
|
+
*/
|
|
51
54
|
export interface PostedAmounts {
|
|
52
55
|
total_cents: number;
|
|
53
56
|
shipping_cents: number;
|
|
@@ -68,7 +71,7 @@ export interface UnknownEntry {
|
|
|
68
71
|
}
|
|
69
72
|
export interface KnownEntry {
|
|
70
73
|
source_ref: string;
|
|
71
|
-
document_type: "invoice" | "credit_memo";
|
|
74
|
+
document_type: "invoice" | "credit_memo" | "bill" | "vendor_credit";
|
|
72
75
|
company_key: string | null;
|
|
73
76
|
company: string;
|
|
74
77
|
latest_version: number | null;
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* What a company owes its vendors: bills, and the vendor credits that reduce them. QBO Sync posts them as
|
|
4
|
+
* QuickBooks Bills and Vendor Credits. Paying a bill is a money movement, and apps send none.
|
|
5
|
+
*
|
|
6
|
+
* Built on QuickBooks' own bill, not on any one app's: a line is an ACCOUNT line (a cost on the account its
|
|
7
|
+
* `cost_code` maps to) or an ITEM line (a product bought, by `item_code`, with quantity and unit cost).
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A cost code: the app's own word for what a line paid for (`freight`, `decoration`). Each company maps each
|
|
11
|
+
* app's codes once, in QBO Sync's Setup, to one of its QuickBooks accounts; a document with a code that is not
|
|
12
|
+
* mapped WAITS in QBO Sync until it is.
|
|
13
|
+
*/
|
|
14
|
+
export declare const costCode: z.ZodString;
|
|
15
|
+
/** One QuickBooks vendor per `ref` per company. Its name in QuickBooks is `company`, else `name`. */
|
|
16
|
+
export declare const vendor: z.ZodObject<{
|
|
17
|
+
ref: z.ZodString;
|
|
18
|
+
name: z.ZodString;
|
|
19
|
+
company: z.ZodOptional<z.ZodString>;
|
|
20
|
+
email: z.ZodOptional<z.ZodString>;
|
|
21
|
+
address: z.ZodOptional<z.ZodObject<{
|
|
22
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
23
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
24
|
+
city: z.ZodOptional<z.ZodString>;
|
|
25
|
+
region: z.ZodOptional<z.ZodString>;
|
|
26
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
27
|
+
country: z.ZodOptional<z.ZodString>;
|
|
28
|
+
}, z.core.$strict>>;
|
|
29
|
+
}, z.core.$strict>;
|
|
30
|
+
/**
|
|
31
|
+
* One line of a bill or vendor credit: EITHER an account line (`cost_code`) OR an item line (`item_code`,
|
|
32
|
+
* `quantity`, `unit_cost_cents`). An item line's amount is quantity × unit cost rounded to whole cents; the unit
|
|
33
|
+
* cost may carry fractions of a cent (bought at $0.125 each). `customer` and `class_key` on a line take the place
|
|
34
|
+
* of the document's own for that line.
|
|
35
|
+
*/
|
|
36
|
+
export declare const billLine: z.ZodObject<{
|
|
37
|
+
description: z.ZodString;
|
|
38
|
+
amount_cents: z.ZodInt;
|
|
39
|
+
cost_code: z.ZodOptional<z.ZodString>;
|
|
40
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
41
|
+
quantity: z.ZodOptional<z.ZodNumber>;
|
|
42
|
+
unit_cost_cents: z.ZodOptional<z.ZodNumber>;
|
|
43
|
+
customer: z.ZodOptional<z.ZodObject<{
|
|
44
|
+
ref: z.ZodString;
|
|
45
|
+
name: z.ZodString;
|
|
46
|
+
company: z.ZodOptional<z.ZodString>;
|
|
47
|
+
email: z.ZodOptional<z.ZodString>;
|
|
48
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
49
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
50
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
51
|
+
city: z.ZodOptional<z.ZodString>;
|
|
52
|
+
region: z.ZodOptional<z.ZodString>;
|
|
53
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
54
|
+
country: z.ZodOptional<z.ZodString>;
|
|
55
|
+
}, z.core.$strict>>;
|
|
56
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
57
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
58
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
59
|
+
city: z.ZodOptional<z.ZodString>;
|
|
60
|
+
region: z.ZodOptional<z.ZodString>;
|
|
61
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
62
|
+
country: z.ZodOptional<z.ZodString>;
|
|
63
|
+
}, z.core.$strict>>;
|
|
64
|
+
}, z.core.$strict>>;
|
|
65
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
66
|
+
}, z.core.$strict>;
|
|
67
|
+
/** Quantity × unit cost, rounded to whole cents, as the line's amount must be. */
|
|
68
|
+
export declare function itemLineAmount(line: {
|
|
69
|
+
quantity: number;
|
|
70
|
+
unit_cost_cents: number;
|
|
71
|
+
}): number;
|
|
72
|
+
/**
|
|
73
|
+
* What the company owes a vendor: posted as a Bill on Accounts Payable. `number` is the vendor's own bill
|
|
74
|
+
* number. `due_date` or `terms_code` (the same `net_<days>` as invoices) say when it is due; with terms,
|
|
75
|
+
* QuickBooks works the date out. `customer` (or a line's own) says whose sale the cost belongs to.
|
|
76
|
+
*
|
|
77
|
+
* A total of $0 is allowed: QuickBooks takes a $0 bill.
|
|
78
|
+
*
|
|
79
|
+
* Refused on its own: a line with both or neither of `cost_code` and `item_code`; an item line without its
|
|
80
|
+
* quantity or unit cost, or whose amount is not quantity × unit cost; a total that is not the sum of the lines.
|
|
81
|
+
* Whether the number is already used is checked when the document arrives.
|
|
82
|
+
*/
|
|
83
|
+
export declare const billSchema: z.ZodObject<{
|
|
84
|
+
type: z.ZodLiteral<"bill">;
|
|
85
|
+
due_date: z.ZodOptional<z.ZodString>;
|
|
86
|
+
terms_code: z.ZodOptional<z.ZodString>;
|
|
87
|
+
number: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodString>>;
|
|
88
|
+
issue_date: z.ZodString;
|
|
89
|
+
currency: z.ZodString;
|
|
90
|
+
vendor: z.ZodObject<{
|
|
91
|
+
ref: z.ZodString;
|
|
92
|
+
name: z.ZodString;
|
|
93
|
+
company: z.ZodOptional<z.ZodString>;
|
|
94
|
+
email: z.ZodOptional<z.ZodString>;
|
|
95
|
+
address: z.ZodOptional<z.ZodObject<{
|
|
96
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
97
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
98
|
+
city: z.ZodOptional<z.ZodString>;
|
|
99
|
+
region: z.ZodOptional<z.ZodString>;
|
|
100
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
101
|
+
country: z.ZodOptional<z.ZodString>;
|
|
102
|
+
}, z.core.$strict>>;
|
|
103
|
+
}, z.core.$strict>;
|
|
104
|
+
customer: z.ZodOptional<z.ZodObject<{
|
|
105
|
+
ref: z.ZodString;
|
|
106
|
+
name: z.ZodString;
|
|
107
|
+
company: z.ZodOptional<z.ZodString>;
|
|
108
|
+
email: z.ZodOptional<z.ZodString>;
|
|
109
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
110
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
111
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
112
|
+
city: z.ZodOptional<z.ZodString>;
|
|
113
|
+
region: z.ZodOptional<z.ZodString>;
|
|
114
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
115
|
+
country: z.ZodOptional<z.ZodString>;
|
|
116
|
+
}, z.core.$strict>>;
|
|
117
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
118
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
119
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
120
|
+
city: z.ZodOptional<z.ZodString>;
|
|
121
|
+
region: z.ZodOptional<z.ZodString>;
|
|
122
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
123
|
+
country: z.ZodOptional<z.ZodString>;
|
|
124
|
+
}, z.core.$strict>>;
|
|
125
|
+
}, z.core.$strict>>;
|
|
126
|
+
order_ref: z.ZodOptional<z.ZodString>;
|
|
127
|
+
references: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
128
|
+
label: z.ZodString;
|
|
129
|
+
value: z.ZodString;
|
|
130
|
+
}, z.core.$strict>>>;
|
|
131
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
132
|
+
lines: z.ZodArray<z.ZodObject<{
|
|
133
|
+
description: z.ZodString;
|
|
134
|
+
amount_cents: z.ZodInt;
|
|
135
|
+
cost_code: z.ZodOptional<z.ZodString>;
|
|
136
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
137
|
+
quantity: z.ZodOptional<z.ZodNumber>;
|
|
138
|
+
unit_cost_cents: z.ZodOptional<z.ZodNumber>;
|
|
139
|
+
customer: z.ZodOptional<z.ZodObject<{
|
|
140
|
+
ref: z.ZodString;
|
|
141
|
+
name: z.ZodString;
|
|
142
|
+
company: z.ZodOptional<z.ZodString>;
|
|
143
|
+
email: z.ZodOptional<z.ZodString>;
|
|
144
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
145
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
146
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
147
|
+
city: z.ZodOptional<z.ZodString>;
|
|
148
|
+
region: z.ZodOptional<z.ZodString>;
|
|
149
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
150
|
+
country: z.ZodOptional<z.ZodString>;
|
|
151
|
+
}, z.core.$strict>>;
|
|
152
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
153
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
154
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
155
|
+
city: z.ZodOptional<z.ZodString>;
|
|
156
|
+
region: z.ZodOptional<z.ZodString>;
|
|
157
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
158
|
+
country: z.ZodOptional<z.ZodString>;
|
|
159
|
+
}, z.core.$strict>>;
|
|
160
|
+
}, z.core.$strict>>;
|
|
161
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
162
|
+
}, z.core.$strict>>;
|
|
163
|
+
total_cents: z.ZodInt;
|
|
164
|
+
schema_version: z.ZodLiteral<"1">;
|
|
165
|
+
source_ref: z.ZodString;
|
|
166
|
+
company_key: z.ZodString;
|
|
167
|
+
version: z.ZodInt;
|
|
168
|
+
status: z.ZodEnum<{
|
|
169
|
+
active: "active";
|
|
170
|
+
voided: "voided";
|
|
171
|
+
}>;
|
|
172
|
+
}, z.core.$strict>;
|
|
173
|
+
/**
|
|
174
|
+
* What a vendor gave back: posted as a Vendor Credit, which lowers what the company owes that vendor.
|
|
175
|
+
* `applies_to_bill_ref` names the bill it is against, when it is against one; the bill must be one this app sent,
|
|
176
|
+
* from the same vendor `ref`, and the credit posts after it. QuickBooks offers the credit when the company next
|
|
177
|
+
* pays that vendor; QBO Sync does not apply it.
|
|
178
|
+
*/
|
|
179
|
+
export declare const vendorCreditSchema: z.ZodObject<{
|
|
180
|
+
type: z.ZodLiteral<"vendor_credit">;
|
|
181
|
+
applies_to_bill_ref: z.ZodOptional<z.ZodString>;
|
|
182
|
+
number: z.ZodOptional<z.ZodPipe<z.ZodString, z.ZodString>>;
|
|
183
|
+
issue_date: z.ZodString;
|
|
184
|
+
currency: z.ZodString;
|
|
185
|
+
vendor: z.ZodObject<{
|
|
186
|
+
ref: z.ZodString;
|
|
187
|
+
name: z.ZodString;
|
|
188
|
+
company: z.ZodOptional<z.ZodString>;
|
|
189
|
+
email: z.ZodOptional<z.ZodString>;
|
|
190
|
+
address: z.ZodOptional<z.ZodObject<{
|
|
191
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
192
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
193
|
+
city: z.ZodOptional<z.ZodString>;
|
|
194
|
+
region: z.ZodOptional<z.ZodString>;
|
|
195
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
196
|
+
country: z.ZodOptional<z.ZodString>;
|
|
197
|
+
}, z.core.$strict>>;
|
|
198
|
+
}, z.core.$strict>;
|
|
199
|
+
customer: z.ZodOptional<z.ZodObject<{
|
|
200
|
+
ref: z.ZodString;
|
|
201
|
+
name: z.ZodString;
|
|
202
|
+
company: z.ZodOptional<z.ZodString>;
|
|
203
|
+
email: z.ZodOptional<z.ZodString>;
|
|
204
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
205
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
206
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
207
|
+
city: z.ZodOptional<z.ZodString>;
|
|
208
|
+
region: z.ZodOptional<z.ZodString>;
|
|
209
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
210
|
+
country: z.ZodOptional<z.ZodString>;
|
|
211
|
+
}, z.core.$strict>>;
|
|
212
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
213
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
214
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
215
|
+
city: z.ZodOptional<z.ZodString>;
|
|
216
|
+
region: z.ZodOptional<z.ZodString>;
|
|
217
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
218
|
+
country: z.ZodOptional<z.ZodString>;
|
|
219
|
+
}, z.core.$strict>>;
|
|
220
|
+
}, z.core.$strict>>;
|
|
221
|
+
order_ref: z.ZodOptional<z.ZodString>;
|
|
222
|
+
references: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
223
|
+
label: z.ZodString;
|
|
224
|
+
value: z.ZodString;
|
|
225
|
+
}, z.core.$strict>>>;
|
|
226
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
227
|
+
lines: z.ZodArray<z.ZodObject<{
|
|
228
|
+
description: z.ZodString;
|
|
229
|
+
amount_cents: z.ZodInt;
|
|
230
|
+
cost_code: z.ZodOptional<z.ZodString>;
|
|
231
|
+
item_code: z.ZodOptional<z.ZodString>;
|
|
232
|
+
quantity: z.ZodOptional<z.ZodNumber>;
|
|
233
|
+
unit_cost_cents: z.ZodOptional<z.ZodNumber>;
|
|
234
|
+
customer: z.ZodOptional<z.ZodObject<{
|
|
235
|
+
ref: z.ZodString;
|
|
236
|
+
name: z.ZodString;
|
|
237
|
+
company: z.ZodOptional<z.ZodString>;
|
|
238
|
+
email: z.ZodOptional<z.ZodString>;
|
|
239
|
+
bill_to: z.ZodOptional<z.ZodObject<{
|
|
240
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
241
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
242
|
+
city: z.ZodOptional<z.ZodString>;
|
|
243
|
+
region: z.ZodOptional<z.ZodString>;
|
|
244
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
245
|
+
country: z.ZodOptional<z.ZodString>;
|
|
246
|
+
}, z.core.$strict>>;
|
|
247
|
+
ship_to: z.ZodOptional<z.ZodObject<{
|
|
248
|
+
line1: z.ZodOptional<z.ZodString>;
|
|
249
|
+
line2: z.ZodOptional<z.ZodString>;
|
|
250
|
+
city: z.ZodOptional<z.ZodString>;
|
|
251
|
+
region: z.ZodOptional<z.ZodString>;
|
|
252
|
+
postal_code: z.ZodOptional<z.ZodString>;
|
|
253
|
+
country: z.ZodOptional<z.ZodString>;
|
|
254
|
+
}, z.core.$strict>>;
|
|
255
|
+
}, z.core.$strict>>;
|
|
256
|
+
class_key: z.ZodOptional<z.ZodString>;
|
|
257
|
+
}, z.core.$strict>>;
|
|
258
|
+
total_cents: z.ZodInt;
|
|
259
|
+
schema_version: z.ZodLiteral<"1">;
|
|
260
|
+
source_ref: z.ZodString;
|
|
261
|
+
company_key: z.ZodString;
|
|
262
|
+
version: z.ZodInt;
|
|
263
|
+
status: z.ZodEnum<{
|
|
264
|
+
active: "active";
|
|
265
|
+
voided: "voided";
|
|
266
|
+
}>;
|
|
267
|
+
}, z.core.$strict>;
|
|
268
|
+
export type BillLine = z.infer<typeof billLine>;
|
|
269
|
+
export type Vendor = z.infer<typeof vendor>;
|
|
270
|
+
export type BillDTO = z.infer<typeof billSchema>;
|
|
271
|
+
export type VendorCreditDTO = z.infer<typeof vendorCreditSchema>;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { termsCode } from "./invoice.js";
|
|
3
|
+
import { address, cents, currency, customer, docNumber, envelope, isoDate, orderRef, references, sum, text } from "./common.js";
|
|
4
|
+
/**
|
|
5
|
+
* What a company owes its vendors: bills, and the vendor credits that reduce them. QBO Sync posts them as
|
|
6
|
+
* QuickBooks Bills and Vendor Credits. Paying a bill is a money movement, and apps send none.
|
|
7
|
+
*
|
|
8
|
+
* Built on QuickBooks' own bill, not on any one app's: a line is an ACCOUNT line (a cost on the account its
|
|
9
|
+
* `cost_code` maps to) or an ITEM line (a product bought, by `item_code`, with quantity and unit cost).
|
|
10
|
+
*/
|
|
11
|
+
/**
|
|
12
|
+
* A cost code: the app's own word for what a line paid for (`freight`, `decoration`). Each company maps each
|
|
13
|
+
* app's codes once, in QBO Sync's Setup, to one of its QuickBooks accounts; a document with a code that is not
|
|
14
|
+
* mapped WAITS in QBO Sync until it is.
|
|
15
|
+
*/
|
|
16
|
+
export const costCode = z
|
|
17
|
+
.string()
|
|
18
|
+
.regex(/^[a-z][a-z0-9_]{0,39}$/, "must be lower-case letters, digits and _, starting with a letter, at most 40 characters (for example freight)");
|
|
19
|
+
/** One QuickBooks vendor per `ref` per company. Its name in QuickBooks is `company`, else `name`. */
|
|
20
|
+
export const vendor = z.strictObject({
|
|
21
|
+
ref: text,
|
|
22
|
+
name: text,
|
|
23
|
+
company: z.string().optional(),
|
|
24
|
+
email: z.string().optional(),
|
|
25
|
+
address: address.optional(),
|
|
26
|
+
});
|
|
27
|
+
/**
|
|
28
|
+
* One line of a bill or vendor credit: EITHER an account line (`cost_code`) OR an item line (`item_code`,
|
|
29
|
+
* `quantity`, `unit_cost_cents`). An item line's amount is quantity × unit cost rounded to whole cents; the unit
|
|
30
|
+
* cost may carry fractions of a cent (bought at $0.125 each). `customer` and `class_key` on a line take the place
|
|
31
|
+
* of the document's own for that line.
|
|
32
|
+
*/
|
|
33
|
+
export const billLine = z.strictObject({
|
|
34
|
+
description: text,
|
|
35
|
+
amount_cents: cents,
|
|
36
|
+
cost_code: costCode.optional(),
|
|
37
|
+
item_code: z.string().optional(),
|
|
38
|
+
quantity: z.number().positive("must be more than zero").optional(),
|
|
39
|
+
unit_cost_cents: z.number().min(0, "must not be negative").optional(),
|
|
40
|
+
customer: customer.optional(),
|
|
41
|
+
class_key: z.string().optional(),
|
|
42
|
+
});
|
|
43
|
+
/** The fields a bill and a vendor credit share. */
|
|
44
|
+
const vendorDocument = {
|
|
45
|
+
...envelope,
|
|
46
|
+
number: docNumber.optional(),
|
|
47
|
+
issue_date: isoDate,
|
|
48
|
+
currency,
|
|
49
|
+
vendor,
|
|
50
|
+
customer: customer.optional(),
|
|
51
|
+
order_ref: orderRef.optional(),
|
|
52
|
+
references: references.optional(),
|
|
53
|
+
class_key: z.string().optional(),
|
|
54
|
+
lines: z.array(billLine).min(1, "needs at least one line"),
|
|
55
|
+
total_cents: cents,
|
|
56
|
+
};
|
|
57
|
+
/** Quantity × unit cost, rounded to whole cents, as the line's amount must be. */
|
|
58
|
+
export function itemLineAmount(line) {
|
|
59
|
+
// The small nudge keeps 0.125 × 3 (0.37499999999999994) from rounding the wrong way.
|
|
60
|
+
return Math.round(line.quantity * line.unit_cost_cents + 1e-9);
|
|
61
|
+
}
|
|
62
|
+
/** The checks a bill and a vendor credit share: each line's kind and amount, and the total. */
|
|
63
|
+
function checkLines(doc, ctx) {
|
|
64
|
+
doc.lines.forEach((line, i) => {
|
|
65
|
+
const at = (field) => ["lines", i, field];
|
|
66
|
+
const hasCost = line.cost_code !== undefined;
|
|
67
|
+
const hasItem = line.item_code !== undefined && line.item_code.trim() !== "";
|
|
68
|
+
if (hasCost === hasItem) {
|
|
69
|
+
ctx.addIssue({ code: "custom", path: at("cost_code"), message: "a line needs either cost_code (an account line) or item_code (an item line), not both" });
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
if (hasCost) {
|
|
73
|
+
for (const field of ["quantity", "unit_cost_cents"]) {
|
|
74
|
+
if (line[field] !== undefined)
|
|
75
|
+
ctx.addIssue({ code: "custom", path: at(field), message: "is only for an item line (item_code)" });
|
|
76
|
+
}
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
if (line.quantity === undefined)
|
|
80
|
+
ctx.addIssue({ code: "custom", path: at("quantity"), message: "is required on an item line" });
|
|
81
|
+
if (line.unit_cost_cents === undefined)
|
|
82
|
+
ctx.addIssue({ code: "custom", path: at("unit_cost_cents"), message: "is required on an item line" });
|
|
83
|
+
if (line.quantity !== undefined && line.unit_cost_cents !== undefined) {
|
|
84
|
+
const expected = itemLineAmount({ quantity: line.quantity, unit_cost_cents: line.unit_cost_cents });
|
|
85
|
+
if (expected !== line.amount_cents) {
|
|
86
|
+
ctx.addIssue({
|
|
87
|
+
code: "custom",
|
|
88
|
+
path: at("amount_cents"),
|
|
89
|
+
message: `must equal quantity × unit_cost_cents rounded to whole cents (${line.quantity} × ${line.unit_cost_cents} = ${expected})`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
const expected = sum(doc.lines.map((l) => l.amount_cents));
|
|
95
|
+
if (doc.total_cents !== expected) {
|
|
96
|
+
ctx.addIssue({ code: "custom", path: ["total_cents"], message: `must equal the sum of the lines (${expected})` });
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* What the company owes a vendor: posted as a Bill on Accounts Payable. `number` is the vendor's own bill
|
|
101
|
+
* number. `due_date` or `terms_code` (the same `net_<days>` as invoices) say when it is due; with terms,
|
|
102
|
+
* QuickBooks works the date out. `customer` (or a line's own) says whose sale the cost belongs to.
|
|
103
|
+
*
|
|
104
|
+
* A total of $0 is allowed: QuickBooks takes a $0 bill.
|
|
105
|
+
*
|
|
106
|
+
* Refused on its own: a line with both or neither of `cost_code` and `item_code`; an item line without its
|
|
107
|
+
* quantity or unit cost, or whose amount is not quantity × unit cost; a total that is not the sum of the lines.
|
|
108
|
+
* Whether the number is already used is checked when the document arrives.
|
|
109
|
+
*/
|
|
110
|
+
export const billSchema = z
|
|
111
|
+
.strictObject({
|
|
112
|
+
...vendorDocument,
|
|
113
|
+
type: z.literal("bill"),
|
|
114
|
+
due_date: isoDate.optional(),
|
|
115
|
+
terms_code: termsCode.optional(),
|
|
116
|
+
})
|
|
117
|
+
.superRefine(checkLines);
|
|
118
|
+
/**
|
|
119
|
+
* What a vendor gave back: posted as a Vendor Credit, which lowers what the company owes that vendor.
|
|
120
|
+
* `applies_to_bill_ref` names the bill it is against, when it is against one; the bill must be one this app sent,
|
|
121
|
+
* from the same vendor `ref`, and the credit posts after it. QuickBooks offers the credit when the company next
|
|
122
|
+
* pays that vendor; QBO Sync does not apply it.
|
|
123
|
+
*/
|
|
124
|
+
export const vendorCreditSchema = z
|
|
125
|
+
.strictObject({
|
|
126
|
+
...vendorDocument,
|
|
127
|
+
type: z.literal("vendor_credit"),
|
|
128
|
+
applies_to_bill_ref: text.optional(),
|
|
129
|
+
})
|
|
130
|
+
.superRefine(checkLines);
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
/**
|
|
3
|
+
* The codes an app uses, listed ahead of any document (0.5.0). Each company maps an app's codes in QBO Sync's
|
|
4
|
+
* Setup: item codes to QuickBooks products (Setup -> Products), cost codes to QuickBooks accounts (Setup -> Cost
|
|
5
|
+
* codes). Those steps learn a code when a document first carries it; a code listed here shows from day one, named
|
|
6
|
+
* by its label, so a company can finish its setup before the first document arrives.
|
|
7
|
+
*
|
|
8
|
+
* Listing changes nothing about posting, and is optional: an app with real catalogue SKUs lists nothing, and an app
|
|
9
|
+
* may still send a code it never listed. Each kind sent is the app's whole list of that kind: a code left out is
|
|
10
|
+
* removed, a kind not sent is left as it is.
|
|
11
|
+
*/
|
|
12
|
+
/** Longest item code QBO Sync keeps (Setup -> Products). */
|
|
13
|
+
export declare const MAX_ITEM_CODE_LENGTH = 200;
|
|
14
|
+
/** Most codes of one kind an app may list. */
|
|
15
|
+
export declare const MAX_DECLARED_CODES = 200;
|
|
16
|
+
/** An item code as invoice, credit memo and bill item lines send it: kept exactly, with nothing around it. */
|
|
17
|
+
export declare const declaredItemCode: z.ZodString;
|
|
18
|
+
export declare const declaredItemCodeSchema: z.ZodObject<{
|
|
19
|
+
/** Exactly as the app puts it on a line's `item_code`. */
|
|
20
|
+
code: z.ZodString;
|
|
21
|
+
/** What the code stands for, in the words the company's bookkeeper reads: "Shipping and freight". */
|
|
22
|
+
label: z.ZodString;
|
|
23
|
+
/** A longer note, when the label is not enough: "Every line added with + Shipping". */
|
|
24
|
+
description: z.ZodOptional<z.ZodString>;
|
|
25
|
+
}, z.core.$strict>;
|
|
26
|
+
export declare const declaredCostCodeSchema: z.ZodObject<{
|
|
27
|
+
/** Exactly as the app puts it on a bill line's `cost_code`. */
|
|
28
|
+
code: z.ZodString;
|
|
29
|
+
label: z.ZodString;
|
|
30
|
+
description: z.ZodOptional<z.ZodString>;
|
|
31
|
+
}, z.core.$strict>;
|
|
32
|
+
export type DeclaredItemCode = z.infer<typeof declaredItemCodeSchema>;
|
|
33
|
+
export type DeclaredCostCode = z.infer<typeof declaredCostCodeSchema>;
|
|
34
|
+
/** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces the app's list of that kind. */
|
|
35
|
+
export declare const codesDeclarationSchema: z.ZodObject<{
|
|
36
|
+
item_codes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
37
|
+
/** Exactly as the app puts it on a line's `item_code`. */
|
|
38
|
+
code: z.ZodString;
|
|
39
|
+
/** What the code stands for, in the words the company's bookkeeper reads: "Shipping and freight". */
|
|
40
|
+
label: z.ZodString;
|
|
41
|
+
/** A longer note, when the label is not enough: "Every line added with + Shipping". */
|
|
42
|
+
description: z.ZodOptional<z.ZodString>;
|
|
43
|
+
}, z.core.$strict>>>;
|
|
44
|
+
cost_codes: z.ZodOptional<z.ZodArray<z.ZodObject<{
|
|
45
|
+
/** Exactly as the app puts it on a bill line's `cost_code`. */
|
|
46
|
+
code: z.ZodString;
|
|
47
|
+
label: z.ZodString;
|
|
48
|
+
description: z.ZodOptional<z.ZodString>;
|
|
49
|
+
}, z.core.$strict>>>;
|
|
50
|
+
}, z.core.$strict>;
|
|
51
|
+
export type CodesDeclaration = z.infer<typeof codesDeclarationSchema>;
|
|
52
|
+
/** What QBO Sync holds for the app: `GET /api/v1/codes`, and the answer to a `PUT`. */
|
|
53
|
+
export interface DeclaredCodes {
|
|
54
|
+
item_codes: DeclaredItemCode[];
|
|
55
|
+
cost_codes: DeclaredCostCode[];
|
|
56
|
+
}
|