-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathekos_parsing.js
109 lines (91 loc) · 3.18 KB
/
ekos_parsing.js
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
_FIELD_MAP = {
"batch.batch_id": "Batch Number",
"batch.product": "Product",
"batch.recipe": "Recipe",
"batch.status": "Status"};
var getEkosFieldId = function(fieldId, domRoot) {
return $('div.inputfieldlabel:has(> label:contains("' + _FIELD_MAP[fieldId] + '"))', domRoot).attr('class').match(/(\d+)Input/)[1];
}
var getEkosFieldValue = function(id, domRoot) {
return $('.' + id + 'InputValue', domRoot).text().trim();
}
var setEkosInputValue = function(fieldId, value, domRoot) {
id = getEkosFieldId(fieldId);
return $('input#' + id, domRoot)[0].value = value;
}
var deployInventoryScan = function(domRoot) {
tpl = `<button type="button" mobilefriendly="false" title="">
<div class="CreateButton"></div></button>`;
domRoot = document;
var scanEkosIngredients = function(event) {
var items = {};
$('tr.datarow', domRoot).each(function(k,v) {
var item = {};
var h =$($('td.button_cell button', v)).attr('onclick');
var re = /(\d+)/g;
item.ekos_id = h.match(re)[1];
var t =$('td#Item', v);
item.ekos_label = t.text();
item.ekos_hash = $(v).attr('rowguid');
items[item.ekos_id] = item;
});
chrome.runtime.sendMessage({
action: MSG_ACTIONS.WRITE_EKOS_MAP,
section: "inventory_items",
map: items
},
function(count) {
alert(count + ' inventory items saved to Firebase');
});
};
var btn = domRoot.createElement('div');
btn.classList.add('button');
btn.innerHTML = tpl;
$("div.CreateButton", $(btn)).text("Scan Inventory Items");
$(btn).click(scanEkosIngredients);
$(btn).appendTo('div.button_section_centered');
}
var buttonHtml = `
<style>
@media print { .printhide{ display:none; } }
</style>
<div class="printhide">
<button id="post">Post Invoice to K.N.O.T.S.</button>
</div>
`;
var invoiceAddons = function(domRoot) {
console.log("parsing invoice");
var invoicetext = $("div#right_side_top")[0].innerText;
var customertext = $("div#left_side_bottom")[0].innerText;
var invtable = $("table:has(td:contains('SUBTOTAL')) td");
var invoice = {
"id": invoicetext.match(/Invoice\: E-(.*)/)[1],
"date": invoicetext.match(/Order Date\: (.*)/)[1],
"licensee": customertext.match(/License Number\: (.*)/)[1],
items: [],
subtotal: parseFloat(invtable[2].innerText.replace('$','')),
tax: parseFloat(invtable[5].innerText.replace('$','')),
total: parseFloat(invtable[8].innerText.replace('$',''))
};
var ordertable = $("table:has(th:contains('Item Number'))")[0];
$("tr:has(td)", ordertable).each(function(i,o) {
var td = $("td", o);
invoice.items.push({
sku: td[1].innerText,
quantity: parseInt(td[3].innerText),
unit_price: parseFloat(td[4].innerText.replace('$','').replace(',','')),
total: parseFloat(td[5].innerText.replace('$','').replace(',',''))
});
});
console.log("invoice", invoice);
$("div#invoice_title").after(buttonHtml);
document.title = "e-" + invoice.id;
$("button#post").click(function() {
console.log("POSTING invoice to knotted systems.");
chrome.runtime.sendMessage({
action: MSG_ACTIONS.WRITE,
section: "invoice/" + invoice.id,
map: invoice
});
});
}