-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreportsService.js
More file actions
231 lines (209 loc) · 6.38 KB
/
reportsService.js
File metadata and controls
231 lines (209 loc) · 6.38 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/**
* Xero Reports service for generating and retrieving reports from Xero
*/
const { getXeroClient, handleXeroError } = require('./xeroClient');
const logger = require('../../utils/logger');
/**
* Get a list of available report types
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @returns {Array} List of report types
*/
const getReportTypes = async (userId, tenantId) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Get report types from Xero
const response = await xero.accountingApi.getReportTypes(tenant.tenantId);
return response.body.reports;
} catch (error) {
logger.error('Error in getReportTypes:', error);
throw handleXeroError(error);
}
};
/**
* Get a specific report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {string} reportId - Report ID/name
* @param {Object} options - Report options
* @returns {Object} Report data
*/
const getReport = async (userId, tenantId, reportId, options = {}) => {
try {
const { xero, tenant } = await getXeroClient(userId, tenantId);
// Get report from Xero
const response = await xero.accountingApi.getReportWithParams(
tenant.tenantId,
reportId,
options
);
return response.body.reports[0];
} catch (error) {
logger.error('Error in getReport:', error);
throw handleXeroError(error);
}
};
/**
* Get profit and loss report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Profit and loss report
*/
const getProfitAndLoss = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
fromDate: options.fromDate,
toDate: options.toDate,
periods: options.periods,
timeframe: options.timeframe,
trackingCategoryID: options.trackingCategoryID,
trackingOptionID: options.trackingOptionID,
trackingCategoryID2: options.trackingCategoryID2,
trackingOptionID2: options.trackingOptionID2,
standardLayout: options.standardLayout !== undefined ? options.standardLayout : true,
paymentsOnly: options.paymentsOnly
};
return await getReport(userId, tenantId, 'ProfitAndLoss', reportOptions);
} catch (error) {
logger.error('Error in getProfitAndLoss:', error);
throw error;
}
};
/**
* Get balance sheet report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Balance sheet report
*/
const getBalanceSheet = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
date: options.date,
periods: options.periods,
timeframe: options.timeframe,
trackingCategoryID: options.trackingCategoryID,
trackingOptionID: options.trackingOptionID,
trackingCategoryID2: options.trackingCategoryID2,
trackingOptionID2: options.trackingOptionID2,
standardLayout: options.standardLayout !== undefined ? options.standardLayout : true,
paymentsOnly: options.paymentsOnly
};
return await getReport(userId, tenantId, 'BalanceSheet', reportOptions);
} catch (error) {
logger.error('Error in getBalanceSheet:', error);
throw error;
}
};
/**
* Get cash summary report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Cash summary report
*/
const getCashSummary = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
fromDate: options.fromDate,
toDate: options.toDate
};
return await getReport(userId, tenantId, 'BankSummary', reportOptions);
} catch (error) {
logger.error('Error in getCashSummary:', error);
throw error;
}
};
/**
* Get budget summary report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Budget summary report
*/
const getBudgetSummary = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
date: options.date,
periods: options.periods,
timeframe: options.timeframe
};
return await getReport(userId, tenantId, 'BudgetSummary', reportOptions);
} catch (error) {
logger.error('Error in getBudgetSummary:', error);
throw error;
}
};
/**
* Get aged receivables report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Aged receivables report
*/
const getAgedReceivables = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
date: options.date,
fromDate: options.fromDate,
toDate: options.toDate,
contactID: options.contactID
};
return await getReport(userId, tenantId, 'AgedReceivablesByContact', reportOptions);
} catch (error) {
logger.error('Error in getAgedReceivables:', error);
throw error;
}
};
/**
* Get aged payables report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Aged payables report
*/
const getAgedPayables = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
date: options.date,
fromDate: options.fromDate,
toDate: options.toDate,
contactID: options.contactID
};
return await getReport(userId, tenantId, 'AgedPayablesByContact', reportOptions);
} catch (error) {
logger.error('Error in getAgedPayables:', error);
throw error;
}
};
/**
* Get trial balance report
* @param {string} userId - User ID
* @param {string} tenantId - Xero tenant ID
* @param {Object} options - Report options
* @returns {Object} Trial balance report
*/
const getTrialBalance = async (userId, tenantId, options = {}) => {
try {
const reportOptions = {
date: options.date,
paymentsOnly: options.paymentsOnly
};
return await getReport(userId, tenantId, 'TrialBalance', reportOptions);
} catch (error) {
logger.error('Error in getTrialBalance:', error);
throw error;
}
};
module.exports = {
getReportTypes,
getReport,
getProfitAndLoss,
getBalanceSheet,
getCashSummary,
getBudgetSummary,
getAgedReceivables,
getAgedPayables,
getTrialBalance
};