@company-six/qbosync 0.3.1 → 0.6.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/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,25 @@ export declare class QbosyncClient {
136
137
  ok: true;
137
138
  documents: ReportEntry[];
138
139
  } | Failure>;
140
+ /**
141
+ * Lists the codes the app uses for one company, each with a label, so that company sees them in QBO Sync's
142
+ * Setup before any document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
143
+ * codes. `company_key` names the company as the app's documents do; an app with a single company may leave it
144
+ * out, one with several must send it (0.6.0), and no company sees what was listed for another. Each kind sent is
145
+ * that company's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
146
+ * Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
147
+ * Safe to call again with the same list, for example each time the app starts.
148
+ */
149
+ declareCodes(input: CodesDeclaration): Promise<{
150
+ ok: true;
151
+ codes: DeclaredCodes;
152
+ } | Failure>;
153
+ /** The codes QBO Sync holds for the app and one company, as last listed with `declareCodes`. */
154
+ declaredCodes(companyKey?: string): Promise<{
155
+ ok: true;
156
+ codes: DeclaredCodes;
157
+ } | Failure>;
158
+ private codesAnswer;
139
159
  /** Which of the app's companies are connected to QBO Sync, with their QuickBooks and syncing. */
140
160
  listCompanies(): Promise<{
141
161
  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,44 @@ export class QbosyncClient {
219
219
  }
220
220
  return { ok: true, documents };
221
221
  }
222
+ // ---- codes
223
+ /**
224
+ * Lists the codes the app uses for one company, each with a label, so that company sees them in QBO Sync's
225
+ * Setup before any document carries them: item codes under Setup -> Products, cost codes under Setup -> Cost
226
+ * codes. `company_key` names the company as the app's documents do; an app with a single company may leave it
227
+ * out, one with several must send it (0.6.0), and no company sees what was listed for another. Each kind sent is
228
+ * that company's whole list of that kind (a code left out is removed); a kind not sent is left as it is.
229
+ * Changes nothing about posting. Checked here first: a list that fails is not sent (`rejected`, `status` absent).
230
+ * Safe to call again with the same list, for example each time the app starts.
231
+ */
232
+ async declareCodes(input) {
233
+ const checked = codesDeclarationSchema.safeParse(input);
234
+ if (!checked.success) {
235
+ const reasons = checked.error.issues.map(formatIssue).join("; ");
236
+ return { ok: false, reason: "rejected", error: `The codes were not sent: ${reasons}`, detail: reasons };
237
+ }
238
+ const called = await this.call("/api/v1/codes", {
239
+ method: "PUT",
240
+ headers: { "Content-Type": "application/json" },
241
+ body: JSON.stringify(checked.data),
242
+ });
243
+ return this.codesAnswer(called);
244
+ }
245
+ /** The codes QBO Sync holds for the app and one company, as last listed with `declareCodes`. */
246
+ async declaredCodes(companyKey) {
247
+ const query = companyKey ? `?company_key=${encodeURIComponent(companyKey)}` : "";
248
+ return this.codesAnswer(await this.call(`/api/v1/codes${query}`));
249
+ }
250
+ async codesAnswer(called) {
251
+ if (!called.ok)
252
+ return called;
253
+ const body = (await called.res.json().catch(() => null));
254
+ if (!called.res.ok)
255
+ return failureOf(called.res, body);
256
+ if (!Array.isArray(body?.item_codes) || !Array.isArray(body?.cost_codes))
257
+ return unexpected(called.res.status, "QBO Sync's answer has no codes lists");
258
+ return { ok: true, codes: { item_codes: body.item_codes, cost_codes: body.cost_codes } };
259
+ }
222
260
  // ---- companies and people
223
261
  /** Which of the app's companies are connected to QBO Sync, with their QuickBooks and syncing. */
224
262
  async listCompanies() {
@@ -47,7 +47,10 @@ export interface PostedLine {
47
47
  quantity: number;
48
48
  amount_cents: number;
49
49
  }
50
- /** What was posted, in the app's own terms. A credit memo has no discount. */
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,64 @@
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
+ * A list is for ONE company (0.6.0): the `company_key` its documents carry. A company sees only the codes listed for
13
+ * it, never those an app listed for its other companies. An app with a single company may leave `company_key` out.
14
+ */
15
+ /** Longest item code QBO Sync keeps (Setup -> Products). */
16
+ export declare const MAX_ITEM_CODE_LENGTH = 200;
17
+ /** Most codes of one kind an app may list. */
18
+ export declare const MAX_DECLARED_CODES = 200;
19
+ /** An item code as invoice, credit memo and bill item lines send it: kept exactly, with nothing around it. */
20
+ export declare const declaredItemCode: z.ZodString;
21
+ export declare const declaredItemCodeSchema: z.ZodObject<{
22
+ /** Exactly as the app puts it on a line's `item_code`. */
23
+ code: z.ZodString;
24
+ /** What the code stands for, in the words the company's bookkeeper reads: "Shipping and freight". */
25
+ label: z.ZodString;
26
+ /** A longer note, when the label is not enough: "Every line added with + Shipping". */
27
+ description: z.ZodOptional<z.ZodString>;
28
+ }, z.core.$strict>;
29
+ export declare const declaredCostCodeSchema: z.ZodObject<{
30
+ /** Exactly as the app puts it on a bill line's `cost_code`. */
31
+ code: z.ZodString;
32
+ label: z.ZodString;
33
+ description: z.ZodOptional<z.ZodString>;
34
+ }, z.core.$strict>;
35
+ export type DeclaredItemCode = z.infer<typeof declaredItemCodeSchema>;
36
+ export type DeclaredCostCode = z.infer<typeof declaredCostCodeSchema>;
37
+ /** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces that company's list of that kind. */
38
+ export declare const codesDeclarationSchema: z.ZodObject<{
39
+ /**
40
+ * The company the lists are for, as the app's documents name it. Needed when the app sends for several
41
+ * companies; with one, it may be left out (0.6.0).
42
+ */
43
+ company_key: z.ZodOptional<z.ZodString>;
44
+ item_codes: z.ZodOptional<z.ZodArray<z.ZodObject<{
45
+ /** Exactly as the app puts it on a line's `item_code`. */
46
+ code: z.ZodString;
47
+ /** What the code stands for, in the words the company's bookkeeper reads: "Shipping and freight". */
48
+ label: z.ZodString;
49
+ /** A longer note, when the label is not enough: "Every line added with + Shipping". */
50
+ description: z.ZodOptional<z.ZodString>;
51
+ }, z.core.$strict>>>;
52
+ cost_codes: z.ZodOptional<z.ZodArray<z.ZodObject<{
53
+ /** Exactly as the app puts it on a bill line's `cost_code`. */
54
+ code: z.ZodString;
55
+ label: z.ZodString;
56
+ description: z.ZodOptional<z.ZodString>;
57
+ }, z.core.$strict>>>;
58
+ }, z.core.$strict>;
59
+ export type CodesDeclaration = z.infer<typeof codesDeclarationSchema>;
60
+ /** What QBO Sync holds for the app and one company: `GET /api/v1/codes`, and the answer to a `PUT`. */
61
+ export interface DeclaredCodes {
62
+ item_codes: DeclaredItemCode[];
63
+ cost_codes: DeclaredCostCode[];
64
+ }
@@ -0,0 +1,74 @@
1
+ import { z } from "zod";
2
+ import { costCode } from "./bill.js";
3
+ import { text } from "./common.js";
4
+ /**
5
+ * The codes an app uses, listed ahead of any document (0.5.0). Each company maps an app's codes in QBO Sync's
6
+ * Setup: item codes to QuickBooks products (Setup -> Products), cost codes to QuickBooks accounts (Setup -> Cost
7
+ * codes). Those steps learn a code when a document first carries it; a code listed here shows from day one, named
8
+ * by its label, so a company can finish its setup before the first document arrives.
9
+ *
10
+ * Listing changes nothing about posting, and is optional: an app with real catalogue SKUs lists nothing, and an app
11
+ * 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
12
+ * removed, a kind not sent is left as it is.
13
+ *
14
+ * A list is for ONE company (0.6.0): the `company_key` its documents carry. A company sees only the codes listed for
15
+ * it, never those an app listed for its other companies. An app with a single company may leave `company_key` out.
16
+ */
17
+ /** Longest item code QBO Sync keeps (Setup -> Products). */
18
+ export const MAX_ITEM_CODE_LENGTH = 200;
19
+ /** Most codes of one kind an app may list. */
20
+ export const MAX_DECLARED_CODES = 200;
21
+ const label = z
22
+ .string()
23
+ .trim()
24
+ .min(1, "must not be empty")
25
+ .max(80, "must be at most 80 characters");
26
+ const description = z.string().trim().min(1, "must not be empty when given").max(200, "must be at most 200 characters");
27
+ /** An item code as invoice, credit memo and bill item lines send it: kept exactly, with nothing around it. */
28
+ export const declaredItemCode = z
29
+ .string()
30
+ .min(1, "must not be empty")
31
+ .max(MAX_ITEM_CODE_LENGTH, `must be at most ${MAX_ITEM_CODE_LENGTH} characters`)
32
+ .refine((c) => c === c.trim(), "must have no spaces around it");
33
+ export const declaredItemCodeSchema = z.strictObject({
34
+ /** Exactly as the app puts it on a line's `item_code`. */
35
+ code: declaredItemCode,
36
+ /** What the code stands for, in the words the company's bookkeeper reads: "Shipping and freight". */
37
+ label,
38
+ /** A longer note, when the label is not enough: "Every line added with + Shipping". */
39
+ description: description.optional(),
40
+ });
41
+ export const declaredCostCodeSchema = z.strictObject({
42
+ /** Exactly as the app puts it on a bill line's `cost_code`. */
43
+ code: costCode,
44
+ label,
45
+ description: description.optional(),
46
+ });
47
+ function noRepeats(list, ctx, field) {
48
+ const seen = new Set();
49
+ list.forEach((entry, i) => {
50
+ if (seen.has(entry.code))
51
+ ctx.addIssue({ code: "custom", path: [field, i, "code"], message: `"${entry.code}" is listed twice` });
52
+ seen.add(entry.code);
53
+ });
54
+ }
55
+ /** What `PUT /api/v1/codes` takes. At least one kind; each kind sent replaces that company's list of that kind. */
56
+ export const codesDeclarationSchema = z
57
+ .strictObject({
58
+ /**
59
+ * The company the lists are for, as the app's documents name it. Needed when the app sends for several
60
+ * companies; with one, it may be left out (0.6.0).
61
+ */
62
+ company_key: text.optional(),
63
+ item_codes: z.array(declaredItemCodeSchema).max(MAX_DECLARED_CODES, `at most ${MAX_DECLARED_CODES} codes`).optional(),
64
+ cost_codes: z.array(declaredCostCodeSchema).max(MAX_DECLARED_CODES, `at most ${MAX_DECLARED_CODES} codes`).optional(),
65
+ })
66
+ .superRefine((d, ctx) => {
67
+ if (d.item_codes === undefined && d.cost_codes === undefined) {
68
+ ctx.addIssue({ code: "custom", path: [], message: "send item_codes, cost_codes or both" });
69
+ }
70
+ if (d.item_codes)
71
+ noRepeats(d.item_codes, ctx, "item_codes");
72
+ if (d.cost_codes)
73
+ noRepeats(d.cost_codes, ctx, "cost_codes");
74
+ });