-
Notifications
You must be signed in to change notification settings - Fork 0
/
actions.refresh.data.js
51 lines (45 loc) · 1.76 KB
/
actions.refresh.data.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
function refreshData() {
// Update sales and expenses from kiosks
try {
const properties = { ...DEFAULT_PROPERTIES, ..._getUserProperties() };
loadSaleRangeFromSema(properties);
loadExpenseRangeFromSema(properties);
} catch (err) {
_log(err.message);
_toast(err.message);
}
}
// Update expenses
function loadExpenseRangeFromSema(properties) {
const sheet = SpreadsheetApp.getActive().getSheetByName('AllExpenses');
let expenses = _fetch('GET', API_GET_EXPENSE_ENDPOINT, properties);
// normalize expenses to expense line
expenses = expenses.map(function (expense, ind, arr) {
return [
expense.kiosk.name, new Date(expense.created_at), " ", parseFloat(expense.total, 10), expense.notes || "", expense.expense_account.category, expense.expense_account.name
];
});
expenses.unshift(
["Kiosk", "Date", "Account #", "Total", "Description", "Category", "Sub Category"]
);
// TODO: Make use of `jsonToSpreadsheet` instead
var range = sheet.clear().getRange(1, 1, expenses.length, (expenses[0]).length);
// update sale range
range.setValues(expenses);
// toast success
_toast("Updated expenses data.");
}
// Update sales
function loadSaleRangeFromSema(properties) {
const sheet = SpreadsheetApp.getActive().getSheetByName('AllSales');
const receipts = _fetch('get', API_GET_SALE_ENDPOINT, properties);
// normalize from receipts to sale lines
const sales = getSalesFromReceipts(receipts);
// Clear and get range from sheet
// getRange(LINE, COLUMN)
var range = sheet.clear().getRange(1, 1, sales.length, (sales[0]).length);
// update sale range
range.setValues(sales);
// toast success
_toast("Updated sales data.");
}