-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtaxes.js
215 lines (197 loc) · 6.84 KB
/
taxes.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
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
'use strict'
const Promise = require('bluebird')
const moment = require('moment')
const numeral = require('numeral')
const roundTo = require('round-to')
const sqlite3 = require('sqlite3').verbose()
const table = require('text-table')
const config = require('./config.json')
const db = new sqlite3.Database(config.dbFilePath)
console.log('node taxes.js fi m 2019 10')
console.log('node taxes.js vat q 2019 1')
console.log('node taxes.js vat p 2019')
const type = process.argv[2]
const periodType = process.argv[3]
console.log('type, periodType', {type, periodType})
const roundAmount = (amount) => roundTo(amount, 2)
let date
let startDate
let endDate
switch (periodType) {
case 'y':
date = moment([process.argv[4]])
startDate = date.startOf('year').format('YYYY-MM-DD 00:00:00')
endDate = date.endOf('year').format('YYYY-MM-DD 23:59:59')
break
case 'q':
date = moment(`${process.argv[4]}-${process.argv[5]}`, 'YYYY-Q')
startDate = date.startOf('quarter').format('YYYY-MM-DD 00:00:00')
endDate = date.endOf('quarter').format('YYYY-MM-DD 23:59:59')
break
case 'm':
date = moment(`${process.argv[4]}-${process.argv[5]}`, 'YYYY-M')//moment([process.argv[4], process.argv[5]])
startDate = date.startOf('month').format('YYYY-MM-DD 00:00:00')
endDate = date.endOf('month').format('YYYY-MM-DD 23:59:59')
break
case 'p':
// date = moment(`${process.argv[4]}-${process.argv[5]}`, 'YYYY-Q')
startDate = '2019-01-01 00:00:00'
endDate = '2019-06-30 23:59:59'
break
default:
throw new Error('Wrong period Type')
}
const taxes = {
vat: () => {
return new Promise((resolve, reject) => {
console.log(`\n\n---=== VAT for Upwork period from ${startDate} till ${endDate} ===---\n`)
const sql = 'SELECT id, amount, rate, date FROM statements WHERE skip = 0 AND type = "service fee" AND date BETWEEN ? AND ? ORDER BY id'
return db.all(sql, [startDate, endDate], (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
.then(rows => {
const data = [
[
'Transaction',
'Date',
'Amount',
'Rate NB',
'Amount BYN',
'VAT BYN'
]
]
const total = rows.reduce((total, row) => {
const sum = roundAmount(row.amount * row.rate)
const vat = roundAmount(sum * config.rates.vat / 100)
const date = moment(row.date, 'YYYY-MM-DD hh:mm:ss').format('YYYY-MM-DD')
data.push([
row.id,
date,
numeral(row.amount).format('$0,0.00'),
numeral(row.rate).format('0,0.0000'),
// numeral(sum).format('0000[.]00'),
numeral(sum).format('0,0.00'),
numeral(vat).format('0,0.00')
])
total.amount += sum
total.vat += vat
return total
}, { amount: 0, fi: 0, vat: 0 })
var t = table(data, { align: [ 'l', 'c', 'r', 'r', 'r', 'r'] });
console.log(t);
console.log(`Total Upwork income: ${numeral(total.amount).format('0,0.00')} p`)
console.log(`\tTotal VAT: ${numeral(total.vat).format('0,0.00')} p`)
return rows
})
},
fi: () => {
return new Promise((resolve, reject) => {
console.log(`\n\n---=== Foreight Income Taxes for Upwork period from ${startDate} till ${endDate} ===---\n`)
const sql = 'SELECT id, amount, rate, date FROM statements WHERE skip = 0 AND type = "service fee" AND date BETWEEN ? AND ? ORDER BY id'
return db.all(sql, [startDate, endDate], (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
.then(rows => {
const data = [
[
'Transaction',
'Date',
'Amount',
'Rate NB',
'Amount BYN',
'FI BYN'
]
]
const total = rows.reduce((total, row) => {
const sum = roundAmount(row.amount * row.rate)
const fi = roundAmount(sum * config.rates.income / 100)
const date = moment(row.date, 'YYYY-MM-DD hh:mm:ss').format('YYYY-MM-DD')
data.push([
row.id,
date,
numeral(row.amount).format('$0,0.00'),
numeral(row.rate).format('0,0.0000'),
numeral(sum).format('0,0.00'),
numeral(fi).format('0,0.00')
])
total.amount += sum
total.fi += fi
return total
}, { amount: 0, fi: 0, vat: 0 })
var t = table(data, { align: [ 'l', 'c', 'r', 'r', 'r', 'r'] });
console.log(t);
console.log(`Total Upwork income: ${numeral(total.amount).format('0,0.00')} p`)
console.log(`\tTotal FI Taxes: ${numeral(total.fi).format('0,0.00')} p`)
return rows
})
},
usn: () => {
return new Promise((resolve, reject) => {
console.log(`\n\n---=== USN Taxes for Upwork period from ${startDate} till ${endDate} ===---\n`)
const sql = 'SELECT id, type, amount, rate, date FROM statements WHERE skip = 0 AND NOT (type = "vat" OR type = "service fee" OR type = "withdrawal" OR type = "withdrawal fee") AND date BETWEEN ? AND ? ORDER BY id'
// const sql = 'SELECT * FROM statements WHERE skip = 0 AND (type = "service fee" OR type = "withdrawal") AND date BETWEEN ? AND ? ORDER BY id'
// const sql = 'SELECT * FROM statements WHERE skip = 1 and date BETWEEN ? AND ? ORDER BY id'
return db.all(sql, [startDate, endDate], (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
.then(rows => {
const data = [
[
'Transaction',
'Type',
'Date',
'Amount',
'Rate NB',
'Amount BYN',
'USN BYN'
]
]
const total = rows.reduce((total, row) => {
const sum = roundAmount(row.amount * row.rate)
const usn = roundAmount(sum * config.rates.usn / 100)
const date = moment(row.date, 'YYYY-MM-DD hh:mm:ss').format('YYYY-MM-DD')
data.push([
row.id,
row.type,
date,
numeral(row.amount).format('$0,0.00'),
numeral(row.rate).format('0,0.0000'),
numeral(sum).format('0,0.00'),
numeral(usn).format('0,0.00')
])
total.amount += sum
total.usn += usn
return total
}, { amount: 0, usn: 0, vat: 0 })
var t = table(data, { align: [ 'l', 'c', 'c', 'r', 'r', 'r', 'r'] });
console.log(t);
console.log(`Total Upwork income: ${numeral(total.amount).format('0,0.00')} p`)
console.log(`\tTotal USN Taxes: ${numeral(total.usn).format('0,0.00')} p`)
return rows
})
},
test: () => {
return new Promise((resolve, reject) => {
console.log(`\n\n---=== USN Taxes for Upwork period from ${startDate} till ${endDate} ===---\n`)
const sql = 'SELECT id, type, amount, rate, date FROM statements WHERE date BETWEEN ? AND ? ORDER BY id'
// const sql = 'SELECT * FROM statements WHERE skip = 0 AND (type = "service fee" OR type = "withdrawal") AND date BETWEEN ? AND ? ORDER BY id'
// const sql = 'SELECT * FROM statements WHERE skip = 1 and date BETWEEN ? AND ? ORDER BY id'
return db.all(sql, [startDate, endDate], (err, res) => {
if (err) return reject(err)
return resolve(res)
})
})
.then(rows => {
console.log('rows', rows)
return rows
})
}
}
return taxes[type]()
.catch(err => console.log(err.message, err))