-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
247 lines (223 loc) · 8.18 KB
/
index.html
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>CSV Generator</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
#output {
white-space: pre-wrap;
background-color: #f9f9f9;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
}
</style>
</head>
<body>
<div id="output">Generating CSV...</div>
<script>
/**
* Constants
*/
const ORIGIN = "https://ibm-demo.rossum.app";
let annotationData = null;
let lineCount = 1;
/**
* Utility function to display output in the designated div.
* @param {string} output - The content to display.
*/
const displayOutput = (output) => {
const outputDiv = document.getElementById("output");
outputDiv.innerHTML = output;
};
/**
* Recursively searches for data points by schema ID within the annotation data.
* @param {Array} content - The annotation data array.
* @param {string} schemaId - The schema ID to search for.
* @returns {Array} - An array of matching data points.
*/
const findBySchemaId = (content, schemaId) => {
return content.reduce((results, dp) => {
if (dp.schemaId === schemaId) {
return [...results, dp];
} else if (dp.children) {
return [...results, ...findBySchemaId(dp.children, schemaId)];
}
return results;
}, []);
};
/**
* Retrieves field values based on provided field IDs.
* @param {Array|null} fieldIds - An array of field schema IDs.
* @returns {Array} - An array of field values.
*/
const getFieldValue = (fieldIds) => {
if (!fieldIds) return [""];
for (const id of fieldIds) {
const nodes = findBySchemaId(annotationData, id);
const filtered_nodes = nodes
.filter((node) => node.content)
.map((node) => node.content.value.replace(/\n/g, " ") || "");
console.log("filtered nodes", filtered_nodes);
const hasNonEmptyValue = filtered_nodes.some((value) => value !== "");
if (hasNonEmptyValue) {
return filtered_nodes; // Return the first valid list
}
}
return [""]; // Return [""] if no valid lists found
};
/**
* Generates CSV content from the provided dictionary and initiates download.
* @param {Object} csvDict - A dictionary where keys are headers and values are arrays of column data.
*/
const generateCsvContent = (csvDict) => {
const headers = Object.keys(csvDict);
let csv = headers.join(",") + "\n";
for (let i = 0; i < lineCount; i++) {
const row = headers
.map((header) => {
let value =
csvDict[header].length === 1
? csvDict[header][0]
: csvDict[header][i] || "";
// Escape quotes and commas
if (value.includes(",") || value.includes('"')) {
value = `"${value.replace(/"/g, '""')}"`;
}
return value;
})
.join(",");
csv += `${row}\n`;
}
// Optionally display the CSV in the output div
// Trigger CSV download
const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);
const downloadLink = document.createElement("a");
const documentId =
findBySchemaId(annotationData, "document_id")[0]?.content?.value ||
"output";
downloadLink.href = url;
downloadLink.download = `${documentId}.csv`;
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
URL.revokeObjectURL(url);
displayOutput("Successfully generated!");
setTimeout(() => {
window.close();
}, 500);
};
/**
* Creates a CSV dictionary based on predefined field mappings and generates the CSV.
* @param {Array} annotationData - The annotation data array.
*/
const createCsv = (annotationData) => {
const fieldMappings = {
"Client Name": ["client_name"],
"Customer Name": ["customer_name"],
"Invoice Date": ["date_issue"],
"Invoice Reference": ["document_id"],
"Contract Reference": ["order_id"],
"Item Total Amount": ["item_amount_total"],
"Claimed Amount": ["amount_due"],
UPC: ["item_upc", "item_upc_dist"],
"UPC Unit": null,
"Item Description": ["item_description", "item_description_dist"],
Quantity: ["item_quantity"],
"Allowance Rate": ["item_amount"],
"Lump Sum": ["item_lump_sum"],
"Allowance Type": ["allowance_type"],
"Deal Description": ["deal_description"],
"Performance Start Date": [
"item_perf_start_date",
"date_start_performance",
],
"Performance End Date": [
"item_perf_end_date",
"date_end_performance",
],
"Purchase Start Date": [
"item_purchase_s_date",
"purchase_start_date",
],
"Purchase End Date": ["item_purchase_e_date", "purchase_end_date"],
"Sell Start Date": ["sell_start_date"],
"Sell End Date": ["sell_end_date"],
"Ship Start Date": ["ship_start_date"],
"Ship End Date": ["ship_end_date"],
"Billing Start Date": ["item_billing_start_date", "billing_s_date"],
"Billing End Date": ["item_billing_end_date", "billing_e_date"],
Comment: ["comment"],
"Promo division": ["final_promo_division"],
"Contract#": ["contract_number", "order_id"],
"Vendor#": ["vendor_id"],
"Coupon#": ["coupon_number"],
"Claim Code": ["claim_code"],
"Claim Description": ["claim_description"],
"PO#": ["item_po_number", "po_number"],
"AllowanceRate/Case": ["item_amount_case"],
"AllowanceRate/Unit": ["item_amount_unit"],
"Contract Status": null,
"PO Date": ["item_po_date", "po_date"],
"Ad Date": ["ad_date"],
Rate: null,
"Check No.": ["check_number"],
"Other Allowances": ["item_other_allowances"],
"Debit Memo#": ["debit_memo"],
Location: ["location"],
SRP: null,
Banner: ["banner"],
"Audit Period": ["audit_period"],
Facilities: ["final_promo_division"],
"Item code": ["item_code", "item_code_dist"],
Identifiers: ["item_identifiers"],
"Ref date": ["item_ref_date"],
"REF ID": ["order_id"],
};
const csvDict = {};
Object.entries(fieldMappings).forEach(([header, fieldIds]) => {
const values = getFieldValue(fieldIds);
if (values.length > lineCount) {
lineCount = values.length;
}
csvDict[header] = values;
});
generateCsvContent(csvDict);
};
/**
* Initializes the CSV creation process by requesting annotation data.
*/
const initializeCsvGeneration = () => {
// Request annotation data from the opener window
window.opener.postMessage({ type: "GET_DATAPOINTS" }, ORIGIN);
};
/**
* Handles incoming messages from other windows.
* @param {MessageEvent} event - The message event.
*/
const handleMessage = (event) => {
if (event.origin !== ORIGIN) return;
const { type, result } = event.data;
if (type === "GET_DATAPOINTS") {
annotationData = result;
console.log("Received annotationData:", annotationData);
createCsv(annotationData);
}
};
/**
* Sets up event listeners and initializes the process.
*/
const setup = () => {
window.addEventListener("message", handleMessage, false);
initializeCsvGeneration();
};
// Execute setup on window load
window.onload = setup;
</script>
</body>
</html>