-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbillsService.js
More file actions
204 lines (176 loc) · 5.07 KB
/
Copy pathbillsService.js
File metadata and controls
204 lines (176 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Xero Bills service for managing bills in Xero
*/
const { getXeroClient, handleXeroError } = require('./xeroClient');
const logger = require('../../utils/logger');
/**
* Get all bills (purchase invoices) for a tenant
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Query options
* @returns {Array} List of bills
*/
const getBills = async (userId, tenantId, options = {}) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Set up query parameters
const where = options.where || 'Type=="ACCPAY"'; // Only get bills (accounts payable)
const order = options.order || 'Date DESC';
const page = options.page || 1;
const pageSize = options.pageSize || 50;
const statuses = options.statuses || undefined;
const invoiceNumbers = options.invoiceNumbers || undefined;
const contactIDs = options.contactIDs || undefined;
const createdByMyApp = options.createdByMyApp || undefined;
// Get bills from Xero
const response = await xero.accountingApi.getInvoices(
tenant.tenantId,
undefined,
undefined,
where,
order,
invoiceNumbers,
contactIDs,
statuses,
page,
pageSize,
undefined,
createdByMyApp
);
return response.body.invoices;
} catch (error) {
logger.error('Error in getBills:', error);
throw handleXeroError(error);
}
};
/**
* Get a single bill by ID
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {string} billId - Bill ID
* @returns {Object} Bill details
*/
const getBill = async (userId, tenantId, billId) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Get bill from Xero
const response = await xero.accountingApi.getInvoice(
tenant.tenantId,
billId
);
const bill = response.body.invoices[0];
// Verify it's a bill
if (bill.type !== 'ACCPAY') {
throw new Error('Invoice is not a bill (ACCPAY)');
}
return bill;
} catch (error) {
logger.error('Error in getBill:', error);
throw handleXeroError(error);
}
};
/**
* Create a new bill
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} billData - Bill data
* @returns {Object} Created bill
*/
const createBill = async (userId, tenantId, billData) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Ensure it's a bill
billData.type = 'ACCPAY';
// Create bill in Xero
const response = await xero.accountingApi.createInvoices(
tenant.tenantId,
{ invoices: [billData] }
);
return response.body.invoices[0];
} catch (error) {
logger.error('Error in createBill:', error);
throw handleXeroError(error);
}
};
/**
* Update an existing bill
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {string} billId - Bill ID
* @param {Object} billData - Bill data
* @returns {Object} Updated bill
*/
const updateBill = async (userId, tenantId, billId, billData) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Ensure billId is included in the data
billData.invoiceID = billId;
// Ensure it's a bill
billData.type = 'ACCPAY';
// Update bill in Xero
const response = await xero.accountingApi.updateInvoice(
tenant.tenantId,
billId,
{ invoices: [billData] }
);
return response.body.invoices[0];
} catch (error) {
logger.error('Error in updateBill:', error);
throw handleXeroError(error);
}
};
/**
* Get bill as PDF
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {string} billId - Bill ID
* @returns {Buffer} PDF file buffer
*/
const getBillAsPdf = async (userId, tenantId, billId) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Get bill as PDF from Xero
const response = await xero.accountingApi.getInvoiceAsPdf(
tenant.tenantId,
billId
);
return response.body;
} catch (error) {
logger.error('Error in getBillAsPdf:', error);
throw handleXeroError(error);
}
};
/**
* Void a bill
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {string} billId - Bill ID
* @returns {Object} Voided bill
*/
const voidBill = async (userId, tenantId, billId) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Get current bill
const bill = await getBill(userId, tenantId, billId);
// Update bill status to VOIDED
bill.status = 'VOIDED';
// Update bill in Xero
const response = await xero.accountingApi.updateInvoice(
tenant.tenantId,
billId,
{ invoices: [bill] }
);
return response.body.invoices[0];
} catch (error) {
logger.error('Error in voidBill:', error);
throw handleXeroError(error);
}
};
module.exports = {
getBills,
getBill,
createBill,
updateBill,
getBillAsPdf,
voidBill
};