@hisaabo/cli 0.1.0 → 0.3.2

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.
Files changed (146) hide show
  1. package/README.md +332 -0
  2. package/dist/bin/audit-trail-2YGXGUXW.js +78 -0
  3. package/dist/bin/categories-P5VFWD5F.js +47 -0
  4. package/dist/bin/check-slug-4TMOTWUJ.js +65 -0
  5. package/dist/bin/chunk-2P35S2C3.js +46 -0
  6. package/dist/bin/chunk-EYNDY7RB.js +175 -0
  7. package/dist/bin/chunk-FFBG6RU4.js +156 -0
  8. package/dist/bin/chunk-FTOBDKMO.js +804 -0
  9. package/dist/bin/client-QNJ2H5CW.js +11 -0
  10. package/dist/bin/collection-efficiency-WMKEGS6U.js +165 -0
  11. package/dist/bin/config-UKVQYPPR.js +18 -0
  12. package/dist/bin/create-Y22FAVOW.js +83 -0
  13. package/dist/bin/default-account-SA56M66A.js +58 -0
  14. package/dist/bin/expenses-VZAOLYUW.js +89 -0
  15. package/dist/bin/export-QZ6WT6PJ.js +59 -0
  16. package/dist/bin/factory-2DCU3B5T.js +483 -0
  17. package/dist/bin/get-6UYP6UVD.js +71 -0
  18. package/dist/bin/get-EXL44I2X.js +66 -0
  19. package/dist/bin/hisaabo.js +2029 -1450
  20. package/dist/bin/invite-O26SAL72.js +53 -0
  21. package/dist/bin/invoice-breakdown-XEATXVMR.js +97 -0
  22. package/dist/bin/items-3U5USBVG.js +62 -0
  23. package/dist/bin/items-toggle-YEKMI2CM.js +46 -0
  24. package/dist/bin/list-FX2C3NCL.js +46 -0
  25. package/dist/bin/list-VBAHY72W.js +79 -0
  26. package/dist/bin/members-5FPRUAB6.js +55 -0
  27. package/dist/bin/monthly-comparison-JEJ3TTY2.js +176 -0
  28. package/dist/bin/order-cancel-XI2LKYXV.js +55 -0
  29. package/dist/bin/order-confirm-HUFCU6MO.js +53 -0
  30. package/dist/bin/order-get-NSDMPUZR.js +88 -0
  31. package/dist/bin/order-update-C7QYSD5R.js +60 -0
  32. package/dist/bin/output-QFLEKG5W.js +35 -0
  33. package/dist/bin/payment-modes-WI3AWSES.js +126 -0
  34. package/dist/bin/profit-loss-5RIEOBKP.js +135 -0
  35. package/dist/bin/receivables-aging-L6DIQCNE.js +161 -0
  36. package/dist/bin/remove-M2B4TZ37.js +52 -0
  37. package/dist/bin/revoke-H3V2GNCD.js +52 -0
  38. package/dist/bin/sales-trend-TICB3KGF.js +71 -0
  39. package/dist/bin/sequence-XUD6FAKY.js +66 -0
  40. package/dist/bin/store-UQ2DDQ5I.js +82 -0
  41. package/dist/bin/summary-BPNFC7I2.js +113 -0
  42. package/dist/bin/top-customers-WGGJWKCN.js +81 -0
  43. package/dist/bin/top-items-C7ZDDJMR.js +84 -0
  44. package/dist/bin/top-outstanding-MOGWJIQS.js +64 -0
  45. package/dist/bin/unpaid-invoices-ZUPNMQJP.js +65 -0
  46. package/dist/bin/untracked-BCJEVDW4.js +67 -0
  47. package/dist/bin/update-HIU4PANX.js +54 -0
  48. package/dist/bin/update-IG565YIN.js +61 -0
  49. package/dist/bin/update-PVLAX7T3.js +57 -0
  50. package/dist/bin/update-role-Q4ZYJRYT.js +45 -0
  51. package/dist/bin/update-settings-XPC6L5WM.js +56 -0
  52. package/package.json +6 -1
  53. package/src/bin/hisaabo.ts +42 -933
  54. package/src/bin/registrars/api-key.ts +44 -0
  55. package/src/bin/registrars/auth.ts +153 -0
  56. package/src/bin/registrars/automated-invoice.ts +167 -0
  57. package/src/bin/registrars/bank.ts +90 -0
  58. package/src/bin/registrars/business.ts +120 -0
  59. package/src/bin/registrars/dashboard.ts +225 -0
  60. package/src/bin/registrars/document.ts +184 -0
  61. package/src/bin/registrars/expense.ts +87 -0
  62. package/src/bin/registrars/gst.ts +41 -0
  63. package/src/bin/registrars/import.ts +49 -0
  64. package/src/bin/registrars/invoice.ts +117 -0
  65. package/src/bin/registrars/item.ts +112 -0
  66. package/src/bin/registrars/party.ts +81 -0
  67. package/src/bin/registrars/payment.ts +128 -0
  68. package/src/bin/registrars/report.ts +142 -0
  69. package/src/bin/registrars/shipment.ts +74 -0
  70. package/src/bin/registrars/store.ts +128 -0
  71. package/src/bin/registrars/target.ts +46 -0
  72. package/src/bin/registrars/tenant.ts +55 -0
  73. package/src/client.ts +495 -4
  74. package/src/commands/api-key/create.ts +72 -0
  75. package/src/commands/api-key/list.ts +71 -0
  76. package/src/commands/api-key/revoke.ts +46 -0
  77. package/src/commands/automated-invoice/create.ts +85 -0
  78. package/src/commands/automated-invoice/delete.ts +42 -0
  79. package/src/commands/automated-invoice/get.ts +113 -0
  80. package/src/commands/automated-invoice/history.ts +65 -0
  81. package/src/commands/automated-invoice/index.ts +11 -0
  82. package/src/commands/automated-invoice/list.ts +91 -0
  83. package/src/commands/automated-invoice/pause.ts +29 -0
  84. package/src/commands/automated-invoice/resume.ts +29 -0
  85. package/src/commands/automated-invoice/run-now.ts +29 -0
  86. package/src/commands/automated-invoice/suggestions.ts +68 -0
  87. package/src/commands/automated-invoice/update.ts +54 -0
  88. package/src/commands/automated-invoice/usage.ts +68 -0
  89. package/src/commands/business/audit-trail.ts +79 -0
  90. package/src/commands/business/export.ts +52 -0
  91. package/src/commands/business/get.ts +55 -0
  92. package/src/commands/business/sequence.ts +65 -0
  93. package/src/commands/business/update.ts +64 -0
  94. package/src/commands/dashboard/collection-efficiency.ts +164 -0
  95. package/src/commands/dashboard/expenses.ts +76 -0
  96. package/src/commands/dashboard/invoice-breakdown.ts +89 -0
  97. package/src/commands/dashboard/monthly-comparison.ts +193 -0
  98. package/src/commands/dashboard/payment-modes.ts +121 -0
  99. package/src/commands/dashboard/profit-loss.ts +146 -0
  100. package/src/commands/dashboard/receivables-aging.ts +169 -0
  101. package/src/commands/dashboard/sales-trend.ts +68 -0
  102. package/src/commands/dashboard/summary.ts +105 -0
  103. package/src/commands/dashboard/top-customers.ts +70 -0
  104. package/src/commands/dashboard/top-items.ts +74 -0
  105. package/src/commands/dashboard/top-outstanding.ts +61 -0
  106. package/src/commands/document/factory.ts +590 -0
  107. package/src/commands/expense/categories.ts +40 -0
  108. package/src/commands/expense/update.ts +58 -0
  109. package/src/commands/import/index.ts +68 -0
  110. package/src/commands/item/categories.ts +40 -0
  111. package/src/commands/item/low-stock-count.ts +32 -0
  112. package/src/commands/item/merge.ts +53 -0
  113. package/src/commands/item/price-history.ts +59 -0
  114. package/src/commands/item/rename-unit.ts +39 -0
  115. package/src/commands/item/sales-stats.ts +44 -0
  116. package/src/commands/item/stock-history.ts +70 -0
  117. package/src/commands/item/switch-base-unit.ts +42 -0
  118. package/src/commands/item/update.ts +59 -0
  119. package/src/commands/item/variants-create.ts +58 -0
  120. package/src/commands/item/variants-delete.ts +47 -0
  121. package/src/commands/item/variants-list.ts +63 -0
  122. package/src/commands/item/variants-update.ts +61 -0
  123. package/src/commands/party/ledger-report.ts +81 -0
  124. package/src/commands/party/merge.ts +50 -0
  125. package/src/commands/party/stats.ts +40 -0
  126. package/src/commands/party/top-items.ts +57 -0
  127. package/src/commands/party/update.ts +65 -0
  128. package/src/commands/payment/default-account.ts +56 -0
  129. package/src/commands/payment/get.ts +59 -0
  130. package/src/commands/payment/unpaid-invoices.ts +69 -0
  131. package/src/commands/payment/untracked.ts +70 -0
  132. package/src/commands/payment/update.ts +54 -0
  133. package/src/commands/report/index.ts +348 -1
  134. package/src/commands/store/check-slug.ts +48 -0
  135. package/src/commands/store/items-toggle.ts +41 -0
  136. package/src/commands/store/items.ts +61 -0
  137. package/src/commands/store/order-cancel.ts +48 -0
  138. package/src/commands/store/order-confirm.ts +45 -0
  139. package/src/commands/store/order-get.ts +66 -0
  140. package/src/commands/store/order-update.ts +52 -0
  141. package/src/commands/store/update-settings.ts +52 -0
  142. package/src/commands/tenant/invite.ts +57 -0
  143. package/src/commands/tenant/list.ts +46 -0
  144. package/src/commands/tenant/members.ts +55 -0
  145. package/src/commands/tenant/remove.ts +47 -0
  146. package/src/commands/tenant/update-role.ts +41 -0
@@ -98,3 +98,71 @@ export async function importItemsCommand(filePath: string, opts: { json?: boolea
98
98
  fatalError(String(e instanceof Error ? e.message : e));
99
99
  }
100
100
  }
101
+
102
+ export async function importInvoicesCommand(filePath: string, opts: { json?: boolean; format?: string }): Promise<void> {
103
+ const cfg = requireAuth();
104
+ const client = new HisaaboClient(cfg);
105
+
106
+ const fmt = opts.format ?? (filePath.endsWith(".csv") ? "csv" : "json");
107
+ const data = fmt === "csv"
108
+ ? readCsvFile(filePath)
109
+ : readJsonFile(filePath) as Array<Record<string, unknown>>;
110
+
111
+ console.log(` Importing ${data.length} invoices...`);
112
+
113
+ try {
114
+ const result = await client.import.importInvoices({ invoices: data as Array<Record<string, unknown>> });
115
+
116
+ if (opts.json) {
117
+ outputJSON(result);
118
+ return;
119
+ }
120
+
121
+ success(`Imported: ${result.imported} invoices`);
122
+ if (result.skipped > 0) warn(`Skipped: ${result.skipped}`);
123
+ result.errors.forEach((e) => console.error(` Row ${e.row}: ${e.message}`));
124
+
125
+ } catch (e) {
126
+ if (e instanceof HisaaboApiError) {
127
+ const err = e.hisaaboError;
128
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
129
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
130
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
131
+ }
132
+ fatalError(String(e instanceof Error ? e.message : e));
133
+ }
134
+ }
135
+
136
+ export async function importPaymentsCommand(filePath: string, opts: { json?: boolean; format?: string }): Promise<void> {
137
+ const cfg = requireAuth();
138
+ const client = new HisaaboClient(cfg);
139
+
140
+ const fmt = opts.format ?? (filePath.endsWith(".csv") ? "csv" : "json");
141
+ const data = fmt === "csv"
142
+ ? readCsvFile(filePath)
143
+ : readJsonFile(filePath) as Array<Record<string, unknown>>;
144
+
145
+ console.log(` Importing ${data.length} payments...`);
146
+
147
+ try {
148
+ const result = await client.import.importPayments({ payments: data as Array<Record<string, unknown>> });
149
+
150
+ if (opts.json) {
151
+ outputJSON(result);
152
+ return;
153
+ }
154
+
155
+ success(`Imported: ${result.imported} payments`);
156
+ if (result.skipped > 0) warn(`Skipped: ${result.skipped}`);
157
+ result.errors.forEach((e) => console.error(` Row ${e.row}: ${e.message}`));
158
+
159
+ } catch (e) {
160
+ if (e instanceof HisaaboApiError) {
161
+ const err = e.hisaaboError;
162
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
163
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
164
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
165
+ }
166
+ fatalError(String(e instanceof Error ? e.message : e));
167
+ }
168
+ }
@@ -0,0 +1,40 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT } from "../../output.js";
4
+
5
+ interface CategoriesOpts {
6
+ json?: boolean;
7
+ }
8
+
9
+ export async function itemCategoriesCommand(opts: CategoriesOpts): Promise<void> {
10
+ const cfg = requireAuth();
11
+ const client = new HisaaboClient(cfg);
12
+
13
+ try {
14
+ const categories = await client.item.categories();
15
+
16
+ if (opts.json) {
17
+ outputJSON(categories);
18
+ return;
19
+ }
20
+
21
+ if (!Array.isArray(categories) || categories.length === 0) {
22
+ console.log(" (no categories)\n");
23
+ return;
24
+ }
25
+
26
+ console.log();
27
+ for (const cat of categories) {
28
+ process.stdout.write(` ${cat}\n`);
29
+ }
30
+ console.log();
31
+
32
+ } catch (e) {
33
+ if (e instanceof HisaaboApiError) {
34
+ const err = e.hisaaboError;
35
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
36
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
37
+ }
38
+ fatalError(String(e instanceof Error ? e.message : e));
39
+ }
40
+ }
@@ -0,0 +1,32 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT } from "../../output.js";
4
+
5
+ interface LowStockCountOpts {
6
+ json?: boolean;
7
+ }
8
+
9
+ export async function itemLowStockCountCommand(opts: LowStockCountOpts): Promise<void> {
10
+ const cfg = requireAuth();
11
+ const client = new HisaaboClient(cfg);
12
+
13
+ try {
14
+ const result = await client.item.lowStockCount();
15
+
16
+ if (opts.json) {
17
+ outputJSON(result);
18
+ return;
19
+ }
20
+
21
+ const count = typeof result === "number" ? result : (result?.count ?? 0);
22
+ process.stdout.write(`\n ${count} item${count === 1 ? "" : "s"} below low-stock threshold\n\n`);
23
+
24
+ } catch (e) {
25
+ if (e instanceof HisaaboApiError) {
26
+ const err = e.hisaaboError;
27
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
28
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
29
+ }
30
+ fatalError(String(e instanceof Error ? e.message : e));
31
+ }
32
+ }
@@ -0,0 +1,53 @@
1
+ import * as readline from "readline";
2
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
3
+ import { requireAuth } from "../../config.js";
4
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
5
+
6
+ interface MergeOpts {
7
+ conversionFactor?: string;
8
+ yes?: boolean;
9
+ json?: boolean;
10
+ }
11
+
12
+ export async function itemMergeCommand(sourceId: string, targetId: string, opts: MergeOpts): Promise<void> {
13
+ const cfg = requireAuth();
14
+ const client = new HisaaboClient(cfg);
15
+
16
+ if (!opts.yes && process.stdin.isTTY) {
17
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
18
+ const answer = await new Promise<string>((resolve) => {
19
+ rl.question(
20
+ ` Merge ${sourceId} into ${targetId}? Invoice history will be moved. (y/n): `,
21
+ resolve,
22
+ );
23
+ });
24
+ rl.close();
25
+ if (answer.trim().toLowerCase() !== "y") {
26
+ console.log(" Cancelled.");
27
+ process.exit(0);
28
+ }
29
+ }
30
+
31
+ const conversionFactor = opts.conversionFactor ? parseFloat(opts.conversionFactor) : undefined;
32
+
33
+ try {
34
+ const result = await client.item.merge({ sourceId, targetId, conversionFactor });
35
+
36
+ if (opts.json) {
37
+ outputJSON(result);
38
+ return;
39
+ }
40
+
41
+ success(`Merged ${sourceId} into ${targetId}`);
42
+
43
+ } catch (e) {
44
+ if (e instanceof HisaaboApiError) {
45
+ const err = e.hisaaboError;
46
+ if (err.code === "not_found") fatalError(`Item not found`, EXIT.NOT_FOUND);
47
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
48
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
49
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
50
+ }
51
+ fatalError(String(e instanceof Error ? e.message : e));
52
+ }
53
+ }
@@ -0,0 +1,59 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import {
4
+ fatalError, outputJSON, outputTable, outputTSV, outputCSV,
5
+ EXIT, type ColumnDef,
6
+ } from "../../output.js";
7
+ import { formatAmount, formatDate } from "../../format.js";
8
+
9
+ interface PriceHistoryOpts {
10
+ json?: boolean;
11
+ format?: string;
12
+ }
13
+
14
+ interface PriceHistoryRow {
15
+ date: string;
16
+ salePrice: string;
17
+ purchasePrice: string;
18
+ changedBy: string;
19
+ }
20
+
21
+ export async function itemPriceHistoryCommand(id: string, opts: PriceHistoryOpts): Promise<void> {
22
+ const cfg = requireAuth();
23
+ const client = new HisaaboClient(cfg);
24
+
25
+ try {
26
+ const result = await client.item.priceHistory({ itemId: id });
27
+
28
+ if (opts.json) {
29
+ outputJSON(result);
30
+ return;
31
+ }
32
+
33
+ const entries: PriceHistoryRow[] = Array.isArray(result?.history) ? result.history : (Array.isArray(result) ? result : []);
34
+
35
+ console.log(`\n Price History: ${result?.itemName ?? id}\n`);
36
+
37
+ const cols: ColumnDef<PriceHistoryRow>[] = [
38
+ { key: "date", header: "Date", width: 13, format: (v) => formatDate(String(v ?? "")) },
39
+ { key: "salePrice", header: "Sale Price (₹)", align: "right", width: 16, format: (v) => v ? formatAmount(String(v)) : "-" },
40
+ { key: "purchasePrice", header: "Purchase (₹)", align: "right", width: 14, format: (v) => v ? formatAmount(String(v)) : "-" },
41
+ { key: "changedBy", header: "Changed By", width: 18 },
42
+ ];
43
+
44
+ if (opts.format === "tsv") outputTSV(entries, cols);
45
+ else if (opts.format === "csv") outputCSV(entries, cols);
46
+ else outputTable(entries, cols);
47
+
48
+ console.log();
49
+
50
+ } catch (e) {
51
+ if (e instanceof HisaaboApiError) {
52
+ const err = e.hisaaboError;
53
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
54
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
55
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
56
+ }
57
+ fatalError(String(e instanceof Error ? e.message : e));
58
+ }
59
+ }
@@ -0,0 +1,39 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
4
+
5
+ interface RenameUnitOpts {
6
+ old?: string;
7
+ new?: string;
8
+ json?: boolean;
9
+ }
10
+
11
+ export async function itemRenameUnitCommand(id: string, opts: RenameUnitOpts): Promise<void> {
12
+ const cfg = requireAuth();
13
+ const client = new HisaaboClient(cfg);
14
+
15
+ if (!opts.old) fatalError("--old is required", EXIT.USAGE);
16
+ if (!opts.new) fatalError("--new is required", EXIT.USAGE);
17
+
18
+ try {
19
+ const result = await client.item.renameUnit({ itemId: id, oldUnit: opts.old, newUnit: opts.new });
20
+
21
+ if (opts.json) {
22
+ outputJSON(result);
23
+ return;
24
+ }
25
+
26
+ success(`Renamed unit '${opts.old}' to '${opts.new}' for item ${id}`);
27
+ console.log();
28
+
29
+ } catch (e) {
30
+ if (e instanceof HisaaboApiError) {
31
+ const err = e.hisaaboError;
32
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
33
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
34
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
35
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
36
+ }
37
+ fatalError(String(e instanceof Error ? e.message : e));
38
+ }
39
+ }
@@ -0,0 +1,44 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT } from "../../output.js";
4
+ import { formatINR, formatAmount } from "../../format.js";
5
+
6
+ interface SalesStatsOpts {
7
+ json?: boolean;
8
+ }
9
+
10
+ export async function itemSalesStatsCommand(id: string, opts: SalesStatsOpts): Promise<void> {
11
+ const cfg = requireAuth();
12
+ const client = new HisaaboClient(cfg);
13
+
14
+ try {
15
+ const result = await client.item.salesStats({ itemId: id });
16
+
17
+ if (opts.json) {
18
+ outputJSON(result);
19
+ return;
20
+ }
21
+
22
+ console.log(`\n Sales Stats: ${result?.itemName ?? id}`);
23
+ console.log(" " + "─".repeat(40));
24
+ console.log(` Total Sold: ${formatAmount(String(result?.totalQuantitySold ?? "0"))} units`);
25
+ console.log(` Total Revenue: ${formatINR(String(result?.totalRevenue ?? "0"))}`);
26
+ console.log(` Avg Price: ${formatINR(String(result?.avgSalePrice ?? "0"))}`);
27
+ if (result?.invoiceCount !== undefined) {
28
+ console.log(` Invoices: ${result.invoiceCount}`);
29
+ }
30
+ if (result?.lastSoldDate) {
31
+ console.log(` Last Sold: ${result.lastSoldDate}`);
32
+ }
33
+ console.log();
34
+
35
+ } catch (e) {
36
+ if (e instanceof HisaaboApiError) {
37
+ const err = e.hisaaboError;
38
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
39
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
40
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
41
+ }
42
+ fatalError(String(e instanceof Error ? e.message : e));
43
+ }
44
+ }
@@ -0,0 +1,70 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import {
4
+ fatalError, outputJSON, outputTable, outputTSV, outputCSV,
5
+ paginationFooter, EXIT, type ColumnDef,
6
+ } from "../../output.js";
7
+ import { formatAmount, formatDate } from "../../format.js";
8
+
9
+ interface StockHistoryOpts {
10
+ page?: number;
11
+ limit?: number;
12
+ json?: boolean;
13
+ format?: string;
14
+ }
15
+
16
+ interface StockAdjustmentRow {
17
+ date: string;
18
+ adjustment: string;
19
+ reason: string;
20
+ balanceAfter: string;
21
+ createdBy: string;
22
+ }
23
+
24
+ export async function itemStockHistoryCommand(id: string, opts: StockHistoryOpts): Promise<void> {
25
+ const cfg = requireAuth();
26
+ const client = new HisaaboClient(cfg);
27
+
28
+ const page = opts.page ?? 1;
29
+ const limit = opts.limit ?? 20;
30
+
31
+ try {
32
+ const result = await client.item.stockAdjustmentHistory({ itemId: id, page, limit });
33
+
34
+ if (opts.json) {
35
+ outputJSON(result);
36
+ return;
37
+ }
38
+
39
+ const entries: StockAdjustmentRow[] = Array.isArray(result?.data) ? result.data : (Array.isArray(result) ? result : []);
40
+ const total: number = result?.total ?? entries.length;
41
+
42
+ console.log(`\n Stock History: ${result?.itemName ?? id}\n`);
43
+
44
+ const cols: ColumnDef<StockAdjustmentRow>[] = [
45
+ { key: "date", header: "Date", width: 13, format: (v) => formatDate(String(v ?? "")) },
46
+ { key: "adjustment", header: "Adjustment", align: "right", width: 12, format: (v) => formatAmount(String(v ?? "0")) },
47
+ { key: "balanceAfter", header: "Balance After", align: "right", width: 14, format: (v) => formatAmount(String(v ?? "0")) },
48
+ { key: "reason", header: "Reason", width: 25 },
49
+ { key: "createdBy", header: "By", width: 16 },
50
+ ];
51
+
52
+ if (opts.format === "tsv") outputTSV(entries, cols);
53
+ else if (opts.format === "csv") outputCSV(entries, cols);
54
+ else {
55
+ outputTable(entries, cols);
56
+ paginationFooter(page, limit, total);
57
+ }
58
+
59
+ console.log();
60
+
61
+ } catch (e) {
62
+ if (e instanceof HisaaboApiError) {
63
+ const err = e.hisaaboError;
64
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
65
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
66
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
67
+ }
68
+ fatalError(String(e instanceof Error ? e.message : e));
69
+ }
70
+ }
@@ -0,0 +1,42 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
4
+
5
+ interface SwitchBaseUnitOpts {
6
+ unit?: string;
7
+ conversionFactor?: string;
8
+ json?: boolean;
9
+ }
10
+
11
+ export async function itemSwitchBaseUnitCommand(id: string, opts: SwitchBaseUnitOpts): Promise<void> {
12
+ const cfg = requireAuth();
13
+ const client = new HisaaboClient(cfg);
14
+
15
+ if (!opts.unit) {
16
+ fatalError("--unit is required", EXIT.USAGE);
17
+ }
18
+
19
+ const conversionFactor = opts.conversionFactor ? parseFloat(opts.conversionFactor) : undefined;
20
+
21
+ try {
22
+ const result = await client.item.switchBaseUnit({ itemId: id, unit: opts.unit, conversionFactor });
23
+
24
+ if (opts.json) {
25
+ outputJSON(result);
26
+ return;
27
+ }
28
+
29
+ success(`Switched base unit for item ${id} to: ${opts.unit}`);
30
+ console.log();
31
+
32
+ } catch (e) {
33
+ if (e instanceof HisaaboApiError) {
34
+ const err = e.hisaaboError;
35
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
36
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
37
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
38
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
39
+ }
40
+ fatalError(String(e instanceof Error ? e.message : e));
41
+ }
42
+ }
@@ -0,0 +1,59 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
4
+
5
+ interface UpdateOpts {
6
+ name?: string;
7
+ salePrice?: string;
8
+ purchasePrice?: string;
9
+ taxPercent?: string;
10
+ hsn?: string;
11
+ unit?: string;
12
+ category?: string;
13
+ sku?: string;
14
+ lowStockAlert?: string;
15
+ json?: boolean;
16
+ }
17
+
18
+ export async function itemUpdateCommand(id: string, opts: UpdateOpts): Promise<void> {
19
+ const cfg = requireAuth();
20
+ const client = new HisaaboClient(cfg);
21
+
22
+ // Only send explicitly provided fields
23
+ const data: Record<string, unknown> = {};
24
+ if (opts.name !== undefined) data["name"] = opts.name;
25
+ if (opts.salePrice !== undefined) data["salePrice"] = opts.salePrice;
26
+ if (opts.purchasePrice !== undefined) data["purchasePrice"] = opts.purchasePrice;
27
+ if (opts.taxPercent !== undefined) data["taxPercent"] = opts.taxPercent;
28
+ if (opts.hsn !== undefined) data["hsn"] = opts.hsn;
29
+ if (opts.unit !== undefined) data["unit"] = opts.unit;
30
+ if (opts.category !== undefined) data["category"] = opts.category;
31
+ if (opts.sku !== undefined) data["sku"] = opts.sku;
32
+ if (opts.lowStockAlert !== undefined) data["lowStockAlert"] = opts.lowStockAlert;
33
+
34
+ if (Object.keys(data).length === 0) {
35
+ fatalError("No fields to update. Provide at least one option.", EXIT.USAGE);
36
+ }
37
+
38
+ try {
39
+ const result = await client.item.update(id, data as Parameters<typeof client.item.update>[1]);
40
+
41
+ if (opts.json) {
42
+ outputJSON(result);
43
+ return;
44
+ }
45
+
46
+ success(`Updated item: ${result.name}`);
47
+ console.log(` ID: ${result.id}\n`);
48
+
49
+ } catch (e) {
50
+ if (e instanceof HisaaboApiError) {
51
+ const err = e.hisaaboError;
52
+ if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
53
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
54
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
55
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
56
+ }
57
+ fatalError(String(e instanceof Error ? e.message : e));
58
+ }
59
+ }
@@ -0,0 +1,58 @@
1
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
2
+ import { requireAuth } from "../../config.js";
3
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
4
+
5
+ interface VariantsCreateOpts {
6
+ sku?: string;
7
+ salePrice?: string;
8
+ purchasePrice?: string;
9
+ stock?: string;
10
+ lowStockAlert?: string;
11
+ attributes?: string;
12
+ json?: boolean;
13
+ }
14
+
15
+ export async function itemVariantsCreateCommand(itemId: string, opts: VariantsCreateOpts): Promise<void> {
16
+ const cfg = requireAuth();
17
+ const client = new HisaaboClient(cfg);
18
+
19
+ let parsedAttributes: Record<string, unknown> | undefined;
20
+ if (opts.attributes) {
21
+ try {
22
+ parsedAttributes = JSON.parse(opts.attributes) as Record<string, unknown>;
23
+ } catch {
24
+ fatalError("--attributes must be valid JSON", EXIT.USAGE);
25
+ }
26
+ }
27
+
28
+ const data: Record<string, unknown> = { itemId };
29
+ if (opts.sku !== undefined) data["sku"] = opts.sku;
30
+ if (opts.salePrice !== undefined) data["salePrice"] = opts.salePrice;
31
+ if (opts.purchasePrice !== undefined) data["purchasePrice"] = opts.purchasePrice;
32
+ if (opts.stock !== undefined) data["stockQuantity"] = opts.stock;
33
+ if (opts.lowStockAlert !== undefined) data["lowStockAlert"] = opts.lowStockAlert;
34
+ if (parsedAttributes !== undefined) data["attributes"] = parsedAttributes;
35
+
36
+ try {
37
+ const result = await client.item.createVariant(data);
38
+
39
+ if (opts.json) {
40
+ outputJSON(result);
41
+ return;
42
+ }
43
+
44
+ success(`Created variant: ${result?.id ?? "OK"}`);
45
+ if (result?.sku) console.log(` SKU: ${result.sku}`);
46
+ console.log();
47
+
48
+ } catch (e) {
49
+ if (e instanceof HisaaboApiError) {
50
+ const err = e.hisaaboError;
51
+ if (err.code === "not_found") fatalError(`Item not found: ${itemId}`, EXIT.NOT_FOUND);
52
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
53
+ if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
54
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
55
+ }
56
+ fatalError(String(e instanceof Error ? e.message : e));
57
+ }
58
+ }
@@ -0,0 +1,47 @@
1
+ import * as readline from "readline";
2
+ import { HisaaboClient, HisaaboApiError } from "../../client.js";
3
+ import { requireAuth } from "../../config.js";
4
+ import { fatalError, outputJSON, EXIT, success } from "../../output.js";
5
+
6
+ interface VariantsDeleteOpts {
7
+ yes?: boolean;
8
+ json?: boolean;
9
+ }
10
+
11
+ export async function itemVariantsDeleteCommand(variantId: string, opts: VariantsDeleteOpts): Promise<void> {
12
+ const cfg = requireAuth();
13
+ const client = new HisaaboClient(cfg);
14
+
15
+ if (!opts.yes && process.stdin.isTTY) {
16
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
17
+ const answer = await new Promise<string>((resolve) => {
18
+ rl.question(` Delete variant ${variantId}? (y/N): `, resolve);
19
+ });
20
+ rl.close();
21
+ if (answer.trim().toLowerCase() !== "y") {
22
+ console.log(" Cancelled.");
23
+ process.exit(0);
24
+ }
25
+ }
26
+
27
+ try {
28
+ const result = await client.item.deleteVariant({ variantId });
29
+
30
+ if (opts.json) {
31
+ outputJSON(result);
32
+ return;
33
+ }
34
+
35
+ success(`Deleted variant: ${variantId}`);
36
+
37
+ } catch (e) {
38
+ if (e instanceof HisaaboApiError) {
39
+ const err = e.hisaaboError;
40
+ if (err.code === "not_found") fatalError(`Variant not found: ${variantId}`, EXIT.NOT_FOUND);
41
+ if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
42
+ if (err.code === "forbidden") fatalError(err.message, EXIT.FORBIDDEN);
43
+ if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
44
+ }
45
+ fatalError(String(e instanceof Error ? e.message : e));
46
+ }
47
+ }