-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontracts.html
336 lines (295 loc) · 11.5 KB
/
contracts.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<!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;
// Initialize the sbdDict to store values with IDs containing "_sbd"
let sbdDict = {};
/**
* 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.
* If a field ID contains "_sbd", its values are added to sbdDict.
* @param {Array|null} fieldIds - An array of field schema IDs.
* @returns {Array} - An array of field values.
*/
const getFieldValue = (header, fieldIds) => {
if (!fieldIds) return [""];
for (const id of fieldIds) {
const nodes = findBySchemaId(annotationData, id);
const nodeValues = nodes
.filter((node) => node.content && node.content.value !== undefined)
.map((node) => node.content.value.replace(/\n/g, " ") || "");
// Check if the ID contains "_sbd" and add to sbdDict if it does
if (id.includes("_sbd")) {
if (!sbdDict[header]) {
sbdDict[header] = [];
}
sbdDict[header].push(...nodeValues);
}
const hasNonEmptyValue = nodeValues.some((value) => value !== "");
if (hasNonEmptyValue) {
return nodeValues; // Return the first valid list
}
}
return [""]; // Return [""] if no valid lists found
};
/**
* Generates CSV content from the provided dictionaries and initiates download.
* @param {Object} csvDict - A dictionary where keys are headers and values are arrays of column data.
* @param {Object} sbdDict - A dictionary where keys are sbd headers and values are arrays of sbd data.
*/
const generateCsvContent = (csvDict, sbdDict) => {
// Separate sbd columns and main columns
const sbdHeaders = Object.keys(sbdDict);
const mainHeaders = Object.keys(csvDict).filter(
(header) => !sbdHeaders.includes(header)
);
console.log(mainHeaders);
console.log(sbdHeaders);
// Combine headers
console.log(mainHeaders, sbdHeaders);
console.log("0000");
const combinedHeaders = [...mainHeaders, ...sbdHeaders];
let csv = combinedHeaders.join(",") + "\n";
console.log("-------------");
console.log(combinedHeaders);
console.log(Object.keys(csvDict));
// Determine the number of rows in main data and sbd data
// const mainRowCount = Math.max(
// ...Object.values(csvDict).map((arr) => arr.length)
// );
const mainRowCount = Math.max(
...Object.keys(csvDict)
.filter((header) => mainHeaders.includes(header)) // Filter keys that are in mainHeaders
.map((header) => csvDict[header].length) // Map the filtered headers to their lengths
);
const sbdRowCount = Math.max(
...Object.values(sbdDict).map((arr) => arr.length)
);
// console.log("saaaaa");
// console.log(.length);
// console.log(combinedHeaders);
// Outer loop: Iterate over main rows
// Determine the number of iterations for the outer loop
// Determine if there are any sbd rows to process
// Determine if there are any sbd rows to process
const hasSbdRows = sbdRowCount > 0;
// Determine the number of iterations for the outer loop
// If there are no sbd rows, set outerIterations to 1 to ensure the inner loop runs once
const outerIterations = hasSbdRows ? sbdRowCount : 1;
// Outer loop: Iterate over sbd rows if present, otherwise run once
for (let i = 0; i < outerIterations; i++) {
// Inner loop: Iterate over main rows
for (let j = 0; j < mainRowCount; j++) {
const row = combinedHeaders.map((header) => {
let value = "";
console.log("debug");
console.log(csvDict[header]);
console.log(csvDict[header].length);
console.log("main value should be here");
if (hasSbdRows) {
if (sbdHeaders.includes(header)) {
if (hasSbdRows) {
// When sbdRowCount > 0, use the current index 'i' to get sbd values
value =
sbdDict[header].length === 1
? sbdDict[header][0]
: sbdDict[header][i] || "";
} else {
// When sbdRowCount is 0, set sbd headers to empty or a default value
value = ""; // You can set a default value if needed
}
} else if (Object.keys(csvDict).includes(header)) {
// Process main headers as usual
value =
csvDict[header].length === 1
? csvDict[header][0]
: csvDict[header][j] || "";
console.log("main value: " + value);
}
} else {
value =
csvDict[header].length == 1
? csvDict[header][0]
: csvDict[header][j] || "";
}
// Escape quotes and commas in the value
if (value.includes(",") || value.includes('"')) {
value = `"${value.replace(/"/g, '""')}"`;
}
return value;
});
console.log(row);
csv += `${row.join(",")}\n`;
}
}
// Optionally display the CSV in the output div
// displayOutput(csv);
// Log the sbdDict for debugging or further processing
console.log("sbdDict:", sbdDict);
// 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": null,
"Invoice Reference": null,
"Contract Reference": ["item_sbd_order_id", "order_id"],
"Item Total Amount": ["item_amount_total"],
"Claimed Amount": null,
UPC: ["item_upc"],
"UPC Unit": ["item_upc_unit"],
"Item Description": ["item_description"],
Quantity: ["item_quantity"],
"Allowance Rate": ["item_amount"],
"Lump Sum": null,
"Allowance Type": ["item_sbd_allowance_type", "allowance_type"],
"Deal Description": ["deal_description"],
"Peformance Start Date": ["date_start_performance"],
"Peformance End Date": ["date_end_performance"],
"Purchase Start Date": [
"item_purchase_s_date_sbd",
"purchase_start_date",
],
"Purchase End Date": [
"item_purchase_e_date_sbd",
"purchase_end_date",
],
"Sell Start Date": null,
"Sell End Date": null,
"Ship Start Date": null,
"Ship End Date": null,
"Billing Start Date": ["billing_s_date"],
"Billing End Date": ["billing_e_date"],
Comment: ["comment"],
"Promo division": ["item_sbd_promo_division", "promo_division"],
"Contract#": ["contract_number"],
"Vendor#": ["vendor_id"],
"Coupon#": null,
"Claim Code": null,
"Claim Description": null,
"PO#": null,
"AllowanceRate/Case": ["item_amount_case"],
"AllowanceRate/Unit": ["item_amount_unit"],
"Contract Status": ["item_sbd_contract_status", "contract_status"],
"PO Date": null,
"Ad Date": ["item_sbd_ad_date", "ad_date"],
Rate: null,
"Check No.": null,
"Other Allowances": ["item_other_allowances"],
"Debit Memo#": null,
Location: ["location"],
SRP: null,
Banner: null,
"Audit Period": null,
Facilities: null,
"Item code": ["item_code"],
Identifiers: null,
"Ref date": null,
"REF ID": null,
};
const csvDict = {};
Object.entries(fieldMappings).forEach(([header, fieldIds]) => {
const values = getFieldValue(header, fieldIds);
csvDict[header] = values;
});
// After collecting all field values, generate the CSV with both csvDict and sbdDict
generateCsvContent(csvDict, sbdDict);
};
/**
* 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;
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>