@hisaabo/cli 0.1.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/.turbo/turbo-build.log +13 -0
- package/.turbo/turbo-typecheck.log +4 -0
- package/CLI-UX-ARCHITECTURE.md +1269 -0
- package/dist/bin/hisaabo.js +3516 -0
- package/package.json +34 -0
- package/src/auth.ts +212 -0
- package/src/bin/hisaabo.ts +950 -0
- package/src/client.ts +1229 -0
- package/src/commands/bank/index.ts +244 -0
- package/src/commands/dashboard.ts +105 -0
- package/src/commands/expense/create.ts +51 -0
- package/src/commands/expense/delete.ts +45 -0
- package/src/commands/expense/list.ts +65 -0
- package/src/commands/gst/index.ts +116 -0
- package/src/commands/import/index.ts +100 -0
- package/src/commands/invoice/create.ts +262 -0
- package/src/commands/invoice/delete.ts +44 -0
- package/src/commands/invoice/get.ts +105 -0
- package/src/commands/invoice/list.ts +126 -0
- package/src/commands/invoice/pdf.ts +75 -0
- package/src/commands/invoice/status.ts +41 -0
- package/src/commands/item/create.ts +98 -0
- package/src/commands/item/delete.ts +44 -0
- package/src/commands/item/list.ts +81 -0
- package/src/commands/item/stock.ts +46 -0
- package/src/commands/party/create.ts +88 -0
- package/src/commands/party/delete.ts +44 -0
- package/src/commands/party/get.ts +42 -0
- package/src/commands/party/ledger.ts +63 -0
- package/src/commands/party/list.ts +69 -0
- package/src/commands/payment/create.ts +133 -0
- package/src/commands/payment/delete.ts +46 -0
- package/src/commands/payment/list.ts +66 -0
- package/src/commands/report/index.ts +218 -0
- package/src/commands/shipment/index.ts +170 -0
- package/src/commands/store/index.ts +77 -0
- package/src/commands/target/index.ts +118 -0
- package/src/components/ErrorDisplay.tsx +16 -0
- package/src/components/Header.tsx +26 -0
- package/src/components/MoneyCell.tsx +16 -0
- package/src/components/Spinner.tsx +12 -0
- package/src/components/StatusBadge.tsx +39 -0
- package/src/components/Table.tsx +63 -0
- package/src/config.ts +47 -0
- package/src/format.ts +180 -0
- package/src/output.ts +227 -0
- package/tsconfig.json +22 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import * as fs from "fs";
|
|
2
|
+
import * as path from "path";
|
|
3
|
+
import { HisaaboClient, HisaaboApiError } from "../../client.js";
|
|
4
|
+
import { requireAuth } from "../../config.js";
|
|
5
|
+
import { fatalError, EXIT, success } from "../../output.js";
|
|
6
|
+
|
|
7
|
+
interface PdfOpts {
|
|
8
|
+
output?: string;
|
|
9
|
+
open?: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export async function invoicePdfCommand(id: string, opts: PdfOpts): Promise<void> {
|
|
13
|
+
const cfg = requireAuth();
|
|
14
|
+
const client = new HisaaboClient(cfg);
|
|
15
|
+
|
|
16
|
+
// First fetch the invoice to get the invoice number
|
|
17
|
+
let invoiceNumber = id;
|
|
18
|
+
try {
|
|
19
|
+
const inv = await client.invoice.get(id);
|
|
20
|
+
invoiceNumber = inv.invoiceNumber;
|
|
21
|
+
} catch {
|
|
22
|
+
// Use id as-is if fetch fails
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Build PDF URL
|
|
26
|
+
const pdfUrl = `${cfg.apiUrl}/api/invoice/${id}/pdf`;
|
|
27
|
+
|
|
28
|
+
try {
|
|
29
|
+
const res = await fetch(pdfUrl, {
|
|
30
|
+
headers: {
|
|
31
|
+
"Authorization": `Bearer ${cfg.token}`,
|
|
32
|
+
"x-business-id": cfg.businessId,
|
|
33
|
+
"x-tenant-id": cfg.tenantId,
|
|
34
|
+
"x-client-type": "cli",
|
|
35
|
+
},
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
if (!res.ok) {
|
|
39
|
+
fatalError(`Failed to download PDF: HTTP ${res.status}`, EXIT.GENERAL);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const buffer = await res.arrayBuffer();
|
|
43
|
+
const bytes = Buffer.from(buffer);
|
|
44
|
+
|
|
45
|
+
// Determine output path
|
|
46
|
+
let outputPath: string;
|
|
47
|
+
if (opts.output) {
|
|
48
|
+
// If it's a directory, put the file inside it
|
|
49
|
+
if (fs.existsSync(opts.output) && fs.statSync(opts.output).isDirectory()) {
|
|
50
|
+
outputPath = path.join(opts.output, `${invoiceNumber}.pdf`);
|
|
51
|
+
} else {
|
|
52
|
+
outputPath = opts.output;
|
|
53
|
+
}
|
|
54
|
+
} else {
|
|
55
|
+
outputPath = `${invoiceNumber}.pdf`;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
fs.writeFileSync(outputPath, bytes);
|
|
59
|
+
success(`Saved: ${outputPath} (${Math.round(bytes.length / 1024)} KB)`);
|
|
60
|
+
|
|
61
|
+
if (opts.open) {
|
|
62
|
+
const openPkg = await import("open");
|
|
63
|
+
await openPkg.default(outputPath);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
} catch (e) {
|
|
67
|
+
if (e instanceof HisaaboApiError) {
|
|
68
|
+
const err = e.hisaaboError;
|
|
69
|
+
if (err.code === "not_found") fatalError(`Invoice not found: ${id}`, EXIT.NOT_FOUND);
|
|
70
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
71
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
72
|
+
}
|
|
73
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { HisaaboClient, HisaaboApiError, type InvoiceStatus } from "../../client.js";
|
|
2
|
+
import { requireAuth } from "../../config.js";
|
|
3
|
+
import { fatalError, outputJSON, EXIT } from "../../output.js";
|
|
4
|
+
import { formatStatus } from "../../format.js";
|
|
5
|
+
|
|
6
|
+
const VALID_STATUSES: InvoiceStatus[] = ["draft", "unfulfilled", "sent", "paid", "partial", "overdue", "cancelled"];
|
|
7
|
+
|
|
8
|
+
export async function invoiceStatusCommand(id: string, status: string, opts: { json?: boolean }): Promise<void> {
|
|
9
|
+
const cfg = requireAuth();
|
|
10
|
+
const client = new HisaaboClient(cfg);
|
|
11
|
+
|
|
12
|
+
if (!VALID_STATUSES.includes(status as InvoiceStatus)) {
|
|
13
|
+
fatalError(`Invalid status: ${status}. Valid: ${VALID_STATUSES.join(", ")}`, EXIT.USAGE);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
// Get current invoice to show before state
|
|
18
|
+
const before = await client.invoice.get(id);
|
|
19
|
+
const updated = await client.invoice.updateStatus(id, status as InvoiceStatus);
|
|
20
|
+
|
|
21
|
+
if (opts.json) {
|
|
22
|
+
outputJSON(updated);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const fromBadge = formatStatus(before.status);
|
|
27
|
+
const toBadge = formatStatus(status);
|
|
28
|
+
console.log(` ${id} status updated: ${fromBadge} -> ${toBadge}`);
|
|
29
|
+
|
|
30
|
+
} catch (e) {
|
|
31
|
+
if (e instanceof HisaaboApiError) {
|
|
32
|
+
const err = e.hisaaboError;
|
|
33
|
+
if (err.code === "not_found") fatalError(`Invoice not found: ${id}`, EXIT.NOT_FOUND);
|
|
34
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
35
|
+
if (err.code === "forbidden") fatalError(err.message, EXIT.FORBIDDEN);
|
|
36
|
+
if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
|
|
37
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
38
|
+
}
|
|
39
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
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 CreateOpts {
|
|
7
|
+
json?: boolean;
|
|
8
|
+
name?: string;
|
|
9
|
+
unit?: string;
|
|
10
|
+
salePrice?: string;
|
|
11
|
+
purchasePrice?: string;
|
|
12
|
+
taxPercent?: string;
|
|
13
|
+
stock?: string;
|
|
14
|
+
hsn?: string;
|
|
15
|
+
category?: string;
|
|
16
|
+
type?: string;
|
|
17
|
+
yes?: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function prompt(rl: readline.Interface, q: string): Promise<string> {
|
|
21
|
+
return new Promise((resolve) => rl.question(q, resolve));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export async function itemCreateCommand(opts: CreateOpts): Promise<void> {
|
|
25
|
+
const cfg = requireAuth();
|
|
26
|
+
const client = new HisaaboClient(cfg);
|
|
27
|
+
const isNonInteractive = !process.stdin.isTTY || opts.yes;
|
|
28
|
+
|
|
29
|
+
let name = opts.name;
|
|
30
|
+
let unit = opts.unit ?? "pcs";
|
|
31
|
+
let salePrice = opts.salePrice;
|
|
32
|
+
let purchasePrice = opts.purchasePrice;
|
|
33
|
+
let taxPercent = opts.taxPercent ?? "0";
|
|
34
|
+
let stockQuantity = opts.stock;
|
|
35
|
+
let hsn = opts.hsn;
|
|
36
|
+
let category = opts.category;
|
|
37
|
+
let itemType = (opts.type ?? "product") as "product" | "service";
|
|
38
|
+
|
|
39
|
+
if (!isNonInteractive) {
|
|
40
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
41
|
+
console.log("\n New Item\n " + "─".repeat(30) + "\n");
|
|
42
|
+
|
|
43
|
+
if (!name) name = (await prompt(rl, " Name: ")).trim();
|
|
44
|
+
const typeStr = await prompt(rl, " Type (product/service) [product]: ");
|
|
45
|
+
itemType = (typeStr.trim() || "product") as "product" | "service";
|
|
46
|
+
|
|
47
|
+
const unitStr = await prompt(rl, " Unit [pcs]: ");
|
|
48
|
+
unit = unitStr.trim() || "pcs";
|
|
49
|
+
|
|
50
|
+
const spStr = await prompt(rl, " Sale Price: ");
|
|
51
|
+
if (spStr.trim()) salePrice = spStr.trim();
|
|
52
|
+
|
|
53
|
+
const ppStr = await prompt(rl, " Purchase Price: ");
|
|
54
|
+
if (ppStr.trim()) purchasePrice = ppStr.trim();
|
|
55
|
+
|
|
56
|
+
const taxStr = await prompt(rl, " Tax % [0]: ");
|
|
57
|
+
taxPercent = taxStr.trim() || "0";
|
|
58
|
+
|
|
59
|
+
if (itemType === "product") {
|
|
60
|
+
const stockStr = await prompt(rl, " Opening Stock [0]: ");
|
|
61
|
+
stockQuantity = stockStr.trim() || "0";
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
const hsnStr = await prompt(rl, " HSN Code: ");
|
|
65
|
+
if (hsnStr.trim()) hsn = hsnStr.trim();
|
|
66
|
+
|
|
67
|
+
const catStr = await prompt(rl, " Category: ");
|
|
68
|
+
if (catStr.trim()) category = catStr.trim();
|
|
69
|
+
|
|
70
|
+
rl.close();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (!name) fatalError("--name is required", EXIT.USAGE);
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const item = await client.item.create({
|
|
77
|
+
name, unit, salePrice, purchasePrice, taxPercent,
|
|
78
|
+
stockQuantity, hsn, category, itemType,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
if (opts.json) {
|
|
82
|
+
outputJSON(item);
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
success(`Created: ${item.name} (${item.itemType})`);
|
|
87
|
+
console.log(` ID: ${item.id}\n`);
|
|
88
|
+
|
|
89
|
+
} catch (e) {
|
|
90
|
+
if (e instanceof HisaaboApiError) {
|
|
91
|
+
const err = e.hisaaboError;
|
|
92
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
93
|
+
if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
|
|
94
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
95
|
+
}
|
|
96
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
export async function itemDeleteCommand(id: string, opts: { yes?: boolean; json?: boolean }): Promise<void> {
|
|
7
|
+
const cfg = requireAuth();
|
|
8
|
+
const client = new HisaaboClient(cfg);
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const item = await client.item.get(id);
|
|
12
|
+
|
|
13
|
+
if (!opts.yes && process.stdin.isTTY) {
|
|
14
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
15
|
+
const answer = await new Promise<string>((resolve) => {
|
|
16
|
+
rl.question(` Delete ${item.name}? (y/N): `, resolve);
|
|
17
|
+
});
|
|
18
|
+
rl.close();
|
|
19
|
+
if (answer.trim().toLowerCase() !== "y") {
|
|
20
|
+
console.log(" Cancelled.");
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = await client.item.delete(id);
|
|
26
|
+
|
|
27
|
+
if (opts.json) {
|
|
28
|
+
outputJSON(result);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
success(`Deleted: ${item.name}`);
|
|
33
|
+
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e instanceof HisaaboApiError) {
|
|
36
|
+
const err = e.hisaaboError;
|
|
37
|
+
if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
|
|
38
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
39
|
+
if (err.code === "forbidden") fatalError(err.message, EXIT.FORBIDDEN);
|
|
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,81 @@
|
|
|
1
|
+
import { HisaaboClient, HisaaboApiError, type ItemSummary } from "../../client.js";
|
|
2
|
+
import { requireAuth } from "../../config.js";
|
|
3
|
+
import {
|
|
4
|
+
fatalError, outputJSON, outputTable, outputTSV, outputCSV, outputIds,
|
|
5
|
+
paginationFooter, EXIT, hasColor, type ColumnDef,
|
|
6
|
+
} from "../../output.js";
|
|
7
|
+
import { formatAmount } from "../../format.js";
|
|
8
|
+
import chalk from "chalk";
|
|
9
|
+
|
|
10
|
+
interface ListOpts {
|
|
11
|
+
json?: boolean;
|
|
12
|
+
format?: string;
|
|
13
|
+
search?: string;
|
|
14
|
+
category?: string;
|
|
15
|
+
type?: string;
|
|
16
|
+
lowStock?: boolean;
|
|
17
|
+
page?: number;
|
|
18
|
+
limit?: number;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export async function itemListCommand(opts: ListOpts): Promise<void> {
|
|
22
|
+
const cfg = requireAuth();
|
|
23
|
+
const client = new HisaaboClient(cfg);
|
|
24
|
+
const page = opts.page ?? 1;
|
|
25
|
+
const limit = opts.limit ?? 20;
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const result = await client.item.list({
|
|
29
|
+
search: opts.search ?? null,
|
|
30
|
+
category: opts.category ?? null,
|
|
31
|
+
itemType: opts.type as "product" | "service" | undefined ?? null,
|
|
32
|
+
lowStock: opts.lowStock ?? null,
|
|
33
|
+
page,
|
|
34
|
+
limit,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
if (opts.json) {
|
|
38
|
+
outputJSON({ data: result.data, pagination: { page: result.page, limit: result.limit, total: result.total } });
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
console.log(`\n Items` + " ".repeat(45) + `${result.total} total\n`);
|
|
43
|
+
|
|
44
|
+
const cols: ColumnDef<ItemSummary>[] = [
|
|
45
|
+
{ key: "name", header: "Name", width: 22 },
|
|
46
|
+
{ key: "unit", header: "Unit", width: 6 },
|
|
47
|
+
{ key: "salePrice", header: "Sale (₹)", align: "right", width: 12,
|
|
48
|
+
format: (v) => v ? formatAmount(String(v)) : "-" },
|
|
49
|
+
{ key: "stockQuantity", header: "Stock", width: 8,
|
|
50
|
+
format: (v, row) => {
|
|
51
|
+
const qty = String(v ?? "0");
|
|
52
|
+
const low = row.lowStockAlert;
|
|
53
|
+
const isLow = low && parseFloat(qty) <= parseFloat(low);
|
|
54
|
+
const str = row.itemType === "service" ? "-" : (isLow ? `${qty} !` : qty);
|
|
55
|
+
return isLow && hasColor() ? chalk.red(str) : str;
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{ key: "taxPercent", header: "Tax%", width: 6, format: (v) => `${v ?? 0}%` },
|
|
59
|
+
{ key: "category", header: "Category", width: 14, format: (v) => String(v ?? "-") },
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
if (opts.format === "tsv") outputTSV(result.data, cols);
|
|
63
|
+
else if (opts.format === "csv") outputCSV(result.data, cols);
|
|
64
|
+
else if (opts.format === "ids") outputIds(result.data.map((r) => r.id));
|
|
65
|
+
else {
|
|
66
|
+
outputTable(result.data, cols);
|
|
67
|
+
paginationFooter(result.page, result.limit, result.total);
|
|
68
|
+
if (result.data.some((i) => i.lowStockAlert && parseFloat(i.stockQuantity) <= parseFloat(i.lowStockAlert))) {
|
|
69
|
+
console.log("\n ! = below low stock alert threshold\n");
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
} catch (e) {
|
|
74
|
+
if (e instanceof HisaaboApiError) {
|
|
75
|
+
const err = e.hisaaboError;
|
|
76
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
77
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
78
|
+
}
|
|
79
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { HisaaboClient, HisaaboApiError } from "../../client.js";
|
|
2
|
+
import { requireAuth } from "../../config.js";
|
|
3
|
+
import { fatalError, outputJSON, EXIT, success } from "../../output.js";
|
|
4
|
+
import { formatAmount } from "../../format.js";
|
|
5
|
+
|
|
6
|
+
interface StockOpts {
|
|
7
|
+
json?: boolean;
|
|
8
|
+
reason?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export async function itemStockCommand(id: string, adjustment: string, opts: StockOpts): Promise<void> {
|
|
12
|
+
const cfg = requireAuth();
|
|
13
|
+
const client = new HisaaboClient(cfg);
|
|
14
|
+
|
|
15
|
+
// Validate adjustment format
|
|
16
|
+
if (!/^[+-]?\d+(\.\d+)?$/.test(adjustment)) {
|
|
17
|
+
fatalError(`Invalid adjustment: ${adjustment}. Use a number like +10, -5, or 100`, EXIT.USAGE);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const updated = await client.item.adjustStock({
|
|
22
|
+
itemId: id,
|
|
23
|
+
adjustment,
|
|
24
|
+
reason: opts.reason,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
if (opts.json) {
|
|
28
|
+
outputJSON(updated);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const adj = parseFloat(adjustment);
|
|
33
|
+
const dir = adj >= 0 ? "+" : "";
|
|
34
|
+
success(`Stock adjusted: ${updated.name} ${dir}${formatAmount(adjustment)} → ${formatAmount(updated.stockQuantity)}`);
|
|
35
|
+
|
|
36
|
+
} catch (e) {
|
|
37
|
+
if (e instanceof HisaaboApiError) {
|
|
38
|
+
const err = e.hisaaboError;
|
|
39
|
+
if (err.code === "not_found") fatalError(`Item not found: ${id}`, EXIT.NOT_FOUND);
|
|
40
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
41
|
+
if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
|
|
42
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
43
|
+
}
|
|
44
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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 CreateOpts {
|
|
7
|
+
json?: boolean;
|
|
8
|
+
type?: string;
|
|
9
|
+
name?: string;
|
|
10
|
+
phone?: string;
|
|
11
|
+
email?: string;
|
|
12
|
+
gstin?: string;
|
|
13
|
+
city?: string;
|
|
14
|
+
category?: string;
|
|
15
|
+
yes?: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async function prompt(rl: readline.Interface, q: string): Promise<string> {
|
|
19
|
+
return new Promise((resolve) => rl.question(q, resolve));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function partyCreateCommand(opts: CreateOpts): Promise<void> {
|
|
23
|
+
const cfg = requireAuth();
|
|
24
|
+
const client = new HisaaboClient(cfg);
|
|
25
|
+
const isNonInteractive = !process.stdin.isTTY || opts.yes;
|
|
26
|
+
|
|
27
|
+
let type = opts.type as "customer" | "supplier" | undefined;
|
|
28
|
+
let name = opts.name;
|
|
29
|
+
let phone = opts.phone;
|
|
30
|
+
let email = opts.email;
|
|
31
|
+
let gstin = opts.gstin;
|
|
32
|
+
let city = opts.city;
|
|
33
|
+
let category = opts.category;
|
|
34
|
+
|
|
35
|
+
if (!isNonInteractive) {
|
|
36
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
37
|
+
console.log("\n New Party\n " + "─".repeat(30) + "\n");
|
|
38
|
+
|
|
39
|
+
if (!type) {
|
|
40
|
+
const t = await prompt(rl, " Type (customer/supplier) [customer]: ");
|
|
41
|
+
type = (t.trim() || "customer") as "customer" | "supplier";
|
|
42
|
+
}
|
|
43
|
+
if (!name) {
|
|
44
|
+
name = (await prompt(rl, " Name: ")).trim();
|
|
45
|
+
}
|
|
46
|
+
if (!phone) {
|
|
47
|
+
const p = await prompt(rl, " Phone: ");
|
|
48
|
+
phone = p.trim() || undefined;
|
|
49
|
+
}
|
|
50
|
+
if (!email) {
|
|
51
|
+
const e = await prompt(rl, " Email: ");
|
|
52
|
+
email = e.trim() || undefined;
|
|
53
|
+
}
|
|
54
|
+
if (!gstin) {
|
|
55
|
+
const g = await prompt(rl, " GSTIN: ");
|
|
56
|
+
gstin = g.trim() || undefined;
|
|
57
|
+
}
|
|
58
|
+
if (!city) {
|
|
59
|
+
const c = await prompt(rl, " City: ");
|
|
60
|
+
city = c.trim() || undefined;
|
|
61
|
+
}
|
|
62
|
+
rl.close();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!name) fatalError("--name is required", EXIT.USAGE);
|
|
66
|
+
if (!type) type = "customer";
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
const party = await client.party.create({ type, name, phone, email, gstin, city, category });
|
|
70
|
+
|
|
71
|
+
if (opts.json) {
|
|
72
|
+
outputJSON(party);
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
success(`Created: ${party.name} (${party.type})`);
|
|
77
|
+
console.log(` ID: ${party.id}\n`);
|
|
78
|
+
|
|
79
|
+
} catch (e) {
|
|
80
|
+
if (e instanceof HisaaboApiError) {
|
|
81
|
+
const err = e.hisaaboError;
|
|
82
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
83
|
+
if (err.code === "validation_failed") fatalError(e.message, EXIT.VALIDATION);
|
|
84
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
85
|
+
}
|
|
86
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
87
|
+
}
|
|
88
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
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
|
+
export async function partyDeleteCommand(id: string, opts: { yes?: boolean; json?: boolean }): Promise<void> {
|
|
7
|
+
const cfg = requireAuth();
|
|
8
|
+
const client = new HisaaboClient(cfg);
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const party = await client.party.get(id);
|
|
12
|
+
|
|
13
|
+
if (!opts.yes && process.stdin.isTTY) {
|
|
14
|
+
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
|
|
15
|
+
const answer = await new Promise<string>((resolve) => {
|
|
16
|
+
rl.question(` Delete ${party.name}? (y/N): `, resolve);
|
|
17
|
+
});
|
|
18
|
+
rl.close();
|
|
19
|
+
if (answer.trim().toLowerCase() !== "y") {
|
|
20
|
+
console.log(" Cancelled.");
|
|
21
|
+
process.exit(0);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const result = await client.party.delete(id);
|
|
26
|
+
|
|
27
|
+
if (opts.json) {
|
|
28
|
+
outputJSON(result);
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
success(`Deleted: ${party.name}`);
|
|
33
|
+
|
|
34
|
+
} catch (e) {
|
|
35
|
+
if (e instanceof HisaaboApiError) {
|
|
36
|
+
const err = e.hisaaboError;
|
|
37
|
+
if (err.code === "not_found") fatalError(`Party not found: ${id}`, EXIT.NOT_FOUND);
|
|
38
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
39
|
+
if (err.code === "forbidden") fatalError(err.message, EXIT.FORBIDDEN);
|
|
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,42 @@
|
|
|
1
|
+
import { HisaaboClient, HisaaboApiError } from "../../client.js";
|
|
2
|
+
import { requireAuth } from "../../config.js";
|
|
3
|
+
import { fatalError, outputJSON, EXIT } from "../../output.js";
|
|
4
|
+
import { formatINR } from "../../format.js";
|
|
5
|
+
|
|
6
|
+
export async function partyGetCommand(id: string, opts: { json?: boolean }): Promise<void> {
|
|
7
|
+
const cfg = requireAuth();
|
|
8
|
+
const client = new HisaaboClient(cfg);
|
|
9
|
+
|
|
10
|
+
try {
|
|
11
|
+
const party = await client.party.get(id);
|
|
12
|
+
|
|
13
|
+
if (opts.json) {
|
|
14
|
+
outputJSON(party);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
console.log(`\n ${party.name} (${party.type.toUpperCase()})`);
|
|
19
|
+
console.log(" " + "─".repeat(50));
|
|
20
|
+
if (party.phone) console.log(` Phone: ${party.phone}`);
|
|
21
|
+
if (party.email) console.log(` Email: ${party.email}`);
|
|
22
|
+
if (party.gstin) console.log(` GSTIN: ${party.gstin}`);
|
|
23
|
+
if (party.pan) console.log(` PAN: ${party.pan}`);
|
|
24
|
+
if (party.billingAddress) console.log(` Address: ${party.billingAddress}`);
|
|
25
|
+
if (party.city) console.log(` City: ${party.city}`);
|
|
26
|
+
if (party.state) console.log(` State: ${party.state}`);
|
|
27
|
+
if (party.category) console.log(` Category: ${party.category}`);
|
|
28
|
+
if (party.creditPeriodDays) console.log(` Credit Period: ${party.creditPeriodDays} days`);
|
|
29
|
+
if (party.creditLimit && parseFloat(party.creditLimit) > 0) console.log(` Credit Limit: ${formatINR(party.creditLimit)}`);
|
|
30
|
+
console.log(` Balance: ${formatINR(party.balance)}`);
|
|
31
|
+
console.log();
|
|
32
|
+
|
|
33
|
+
} catch (e) {
|
|
34
|
+
if (e instanceof HisaaboApiError) {
|
|
35
|
+
const err = e.hisaaboError;
|
|
36
|
+
if (err.code === "not_found") fatalError(`Party not found: ${id}`, EXIT.NOT_FOUND);
|
|
37
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
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,63 @@
|
|
|
1
|
+
import { HisaaboClient, HisaaboApiError, type LedgerEntry } 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, formatINR } from "../../format.js";
|
|
8
|
+
|
|
9
|
+
interface LedgerOpts {
|
|
10
|
+
json?: boolean;
|
|
11
|
+
format?: string;
|
|
12
|
+
from?: string;
|
|
13
|
+
to?: string;
|
|
14
|
+
page?: number;
|
|
15
|
+
limit?: number;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export async function partyLedgerCommand(partyId: string, opts: LedgerOpts): Promise<void> {
|
|
19
|
+
const cfg = requireAuth();
|
|
20
|
+
const client = new HisaaboClient(cfg);
|
|
21
|
+
|
|
22
|
+
try {
|
|
23
|
+
const result = await client.party.ledger(partyId, {
|
|
24
|
+
fromDate: opts.from,
|
|
25
|
+
toDate: opts.to,
|
|
26
|
+
page: opts.page ?? 1,
|
|
27
|
+
limit: opts.limit ?? 50,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (opts.json) {
|
|
31
|
+
outputJSON(result);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
console.log(`\n Ledger: ${result.partyName} (${result.partyType})`);
|
|
36
|
+
console.log(" " + "═".repeat(60));
|
|
37
|
+
console.log(` Opening Balance: ${formatINR(result.openingBalance)}\n`);
|
|
38
|
+
|
|
39
|
+
const cols: ColumnDef<LedgerEntry>[] = [
|
|
40
|
+
{ key: "date", header: "Date", width: 13, format: (v) => formatDate(String(v ?? "")) },
|
|
41
|
+
{ key: "description", header: "Description", width: 25 },
|
|
42
|
+
{ key: "debit", header: "Debit (₹)", align: "right", width: 13, format: (v) => parseFloat(String(v ?? "0")) !== 0 ? formatAmount(String(v)) : "-" },
|
|
43
|
+
{ key: "credit", header: "Credit (₹)", align: "right", width: 13, format: (v) => parseFloat(String(v ?? "0")) !== 0 ? formatAmount(String(v)) : "-" },
|
|
44
|
+
{ key: "balance", header: "Balance (₹)", align: "right", width: 13, format: (v) => formatAmount(String(v ?? "0")) },
|
|
45
|
+
{ key: "referenceType", header: "Type", width: 10 },
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
if (opts.format === "tsv") outputTSV(result.entries, cols);
|
|
49
|
+
else if (opts.format === "csv") outputCSV(result.entries, cols);
|
|
50
|
+
else outputTable(result.entries, cols);
|
|
51
|
+
|
|
52
|
+
console.log(`\n Closing Balance: ${formatINR(result.closingBalance)}\n`);
|
|
53
|
+
|
|
54
|
+
} catch (e) {
|
|
55
|
+
if (e instanceof HisaaboApiError) {
|
|
56
|
+
const err = e.hisaaboError;
|
|
57
|
+
if (err.code === "not_found") fatalError(`Party not found: ${partyId}`, EXIT.NOT_FOUND);
|
|
58
|
+
if (err.code === "unauthorized") fatalError("Session expired. Run: hisaabo login", EXIT.AUTH);
|
|
59
|
+
if (err.code === "network_error") fatalError(err.message, EXIT.NETWORK);
|
|
60
|
+
}
|
|
61
|
+
fatalError(String(e instanceof Error ? e.message : e));
|
|
62
|
+
}
|
|
63
|
+
}
|