-
Notifications
You must be signed in to change notification settings - Fork 66
/
Kode.gs
217 lines (181 loc) · 7.45 KB
/
Kode.gs
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
//CONFIG
var BOT_TOKEN = "6642440655:AADRAWN3nXcuJf5xJkSQLhyJgQXGag08cLc" //BOT TOKEN ANDA
var SS_URL = "https://docs.google.com/spreadsheets/d/1LArhvsBXZ8lJ3tzvx8DyYrFWwtRxlQsWQEFzdsyignw/edit#gid=0" //URL SPREADSHEET
var CREDIT_SHEET_NAME = "Pemasukan" //NAMA SHEET PEMASUKAN
var DEBIT_SHEET_NAME = "Pengeluaran" //NAMA SHEET PENGELUARAN
//BEGIN
const ss = SpreadsheetApp.openByUrl(SS_URL);
const creditSheet = ss.getSheetByName(CREDIT_SHEET_NAME);
const debitSheet = ss.getSheetByName(DEBIT_SHEET_NAME);
const Credit = new Collection.Collect(creditSheet)
const Debit = new Collection.Collect(debitSheet)
function doGet(e) {
return HtmlService.createHtmlOutput('<h1>OK</h1>')
}
function doPost(e) {
const queryParameters = e.parameter;
const usersQuery = queryParameters.users
const validUsers = usersQuery.split(',')
try {
if (e.postData.type == "application/json") {
let update = JSON.parse(e.postData.contents);
if (update) {
commands(update, validUsers)
return true
}
}
} catch (e) {
Logger.log(e)
}
}
function commands(update, validUsers) {
const chatId = update.message.chat.id;
const first_name = update.message.chat.first_name;
const text = update.message.text || '';
const tanggal = new Date().toLocaleString();
const _date = new Date().toJSON()
if (validUsers.includes(String(chatId))) {
if (text.startsWith("/start")) {
sendMessage({
chat_id: chatId,
text: "Mulai laporan keuangan.\nPemasukan:\n/masuk [nominal] [#kategori] [item1, item2 dst]\nPengeluaran:\n/keluar [nominal] [#kategori] [item1, item2 dst]\n\nRekapitulasi: /rekap [tanggal/bulan] [tanggal/bulan (opsional)]\nTanggal dan bulan berformat YYYY-MM-DD dan YYYY-MM\nContoh: \n/rekap 2024-01-01\n/rekap 2024-01-01 2024-01-10\n/rekap 2024-01\n/rekap 2024-01 2024-06"
})
} else if (text.startsWith("/masuk")) {
const stext = text.split(' ')
const nominal = Number(stext[1]);
const kategori = stext[2].startsWith('#') ? stext[2].replace('#', '') : '';
stext.splice(0, 3);
const item = stext.join(' ')
if (nominal && kategori && item) {
Credit.insert(
{
_date,
Tanggal: tanggal,
Kategori: kategori,
Nominal: nominal,
Item: item,
ReporterID: chatId,
ReporterName: first_name
}
)
sendMessage({
chat_id: chatId,
text: 'Laporan pemasukan sukses.'
})
} else {
sendMessage({
chat_id: chatId,
text: 'Gagal. Pastikan sesuai format. \n/masuk [harga] [#kategori] [item1, item2 dst]'
})
}
} else if (text.startsWith("/keluar")) {
const stext = text.split(' ')
const nominal = Number(stext[1]);
const kategori = stext[2].startsWith('#') ? stext[2].replace('#', '') : '';
stext.splice(0, 3);
const item = stext.join(' ')
if (nominal && kategori && item) {
Debit.insert(
{
_date,
Tanggal: tanggal,
Kategori: kategori,
Nominal: nominal,
Item: item,
ReporterID: chatId,
ReporterName: first_name
}
)
sendMessage({
chat_id: chatId,
text: 'Laporan pengeluaran sukses.'
})
} else {
sendMessage({
chat_id: chatId,
text: 'Gagal. Pastikan sesuai format. \n/keluar [harga] [#kategori] [item1, item2 dst]'
})
}
} else if (text.startsWith("/rekap")) {
const stext = text.split(' ')
stext.splice(0, 1);
const oDateRange = Collection.generateDateRange(stext.join(' '))
const dataRange = oDateRange.dateRange
const sumType = oDateRange.sumType
const monthNames = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember']
if (dataRange.length > 0) {
let textToSend = "Rekapitulasi: \n"
for (let _date of dataRange) {
const pengeluaran = Debit.find(
{
_date: d => new Date(d) >= _date.from && new Date(d) < _date.to
}
)
const pemasukan = Credit.find(
{
_date: d => new Date(d) >= _date.from && new Date(d) < _date.to
}
)
let rekapMasuk = pemasukan.reduce((acc, item) => {
if (!acc[item.Kategori]) {
acc[item.Kategori] = 0;
}
acc[item.Kategori] += item.Nominal;
return acc;
}, {});
let rekapKeluar = pengeluaran.reduce((acc, item) => {
if (!acc[item.Kategori]) {
acc[item.Kategori] = 0;
}
acc[item.Kategori] += item.Nominal;
return acc;
}, {});
if (sumType == 'daily') {
let dateTo = new Date(_date.to)
dateTo.setDate(dateTo.getDate() - 1)
textToSend += "Pemasukan " + _date.from.toLocaleDateString() + ' s.d ' + dateTo.toLocaleDateString() + "\n"
textToSend += Object.keys(rekapMasuk).map((i) => `${i}: ${Number(rekapMasuk[i]).toLocaleString('id-ID')}`).join('\n') || '---'
textToSend += '\nTotal: ' + Object.values(rekapMasuk).reduce((acc, value) => acc + value, 0).toLocaleString('id-ID');
textToSend += "\n"
textToSend += "\n"
textToSend += "Pengeluaran " + _date.from.toLocaleDateString() + ' s.d ' + dateTo.toLocaleDateString() + "\n"
textToSend += Object.keys(rekapKeluar).map((i) => `${i}: ${Number(rekapKeluar[i]).toLocaleString('id-ID')}`).join('\n') || '---'
textToSend += '\nTotal: ' + Object.values(rekapKeluar).reduce((acc, value) => acc + value, 0).toLocaleString('id-ID');
textToSend += "\n"
textToSend += "\n"
}
else {
textToSend += "Pemasukan bulan " + monthNames[_date.from.getMonth()] + ' ' + _date.from.getFullYear() + "\n"
textToSend += Object.keys(rekapMasuk).map((i) => `${i}: ${Number(rekapMasuk[i]).toLocaleString('id-ID')}`).join('\n') || '---'
textToSend += '\nTotal: ' + Object.values(rekapMasuk).reduce((acc, value) => acc + value, 0).toLocaleString('id-ID');
textToSend += "\n"
textToSend += "\n"
textToSend += "Pengeluaran bulan " + monthNames[_date.from.getMonth()] + ' ' + _date.from.getFullYear() + "\n"
textToSend += Object.keys(rekapKeluar).map((i) => `${i}: ${Number(rekapKeluar[i]).toLocaleString('id-ID')}`).join('\n') || '---'
textToSend += '\nTotal: ' + Object.values(rekapKeluar).reduce((acc, value) => acc + value, 0).toLocaleString('id-ID');
textToSend += "\n"
textToSend += "\n"
}
}
sendMessage({
chat_id: chatId,
text: textToSend
})
} else {
sendMessage({
chat_id: chatId,
text: 'Gagal. Pastikan sesuai format. \n/rekap [tanggal/bulan] [tanggal/bulan (opsional)]\nTanggal dan bulan berformat YYYY-MM-DD dan YYYY-MM\nContoh: \n/rekap 2024-01-01\n/rekap 2024-01-01 2024-01-10\n/rekap 2024-01\n/rekap 2024-01 2024-06'
})
}
}
}
}
function sendMessage(postdata) {
var options = {
'method': 'post',
'contentType': 'application/json',
'payload': JSON.stringify(postdata),
'muteHttpExceptions': true
};
UrlFetchApp.fetch('https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage', options);
}