import-pap.py
8.78 KB
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
#kpusat19_svr
#database dbdapja
#user samsatjb
#pass samsatjb onsoctcp 1526
#v_rtimepap
#tg_pros_bayar
#pap
#den_pajak
#jumlah
#v_rtime
#vsts yesterday
#tg_pros_bayar
#pkb_pok
#pkb_den
#bbnkb1_pok
#bbnkb2_pok
#jumlah
import sys
from models_eis import (ArPayment as EisArPayment,
Rekening as EisRekening,
EisBase, EisDBSession)
from conf import pap_url
from sqlalchemy import create_engine, literal_column, func
from datetime import datetime
from datetime import date
try:
from urllib import quote_plus, quote
except:
from urllib.parse import quote_plus, quote
from conf import pap_url as url
from DbTools import OtherEngine
data = url.split('@')
db_name = data[1]
data = data[0].split('//')
data = data[1].split(':')
db_user = data[0]
db_pass = data[1]
print(db_name, db_user, db_pass)
engine = OtherEngine(db_name, db_user, db_pass)
now = datetime.now()
tanggal = now.date()
tahun = now.strftime('%Y')
dep_kode = '3.03.02.'
dep_nama = 'BADAN PENDAPATAN DAERAH'
def calculate(tabel, all=None, kode=None):
query = EisDBSession.query(tabel).filter_by(tahun = str(tahun)).\
order_by(tabel.departemen_kd, tabel.tanggal, tabel.kode.desc())
if not all:
query = query.filter_by(tanggal=tanggal)
if kode:
query = query.filter_by(kode=kode)
old_level = 0
levels = {}
jumlahs = {}
key = ""
for row in query.all():
if row.level_id > old_level:
print('Lebih', jumlahs, key)
old_level = row.level_id
key = 'a'+str(row.level_id)
#JIKA level sama dengan sebelumnya jumlahkan
if row.level_id == old_level:
print('Sama')
if not key in jumlahs:
jumlahs[key] = row.jumlah
else:
jumlahs[key] += row.jumlah
#JIKA level < sebelumnya update current row
if row.level_id < old_level:
print('Kurang', jumlahs, key)
print('kode: ', row.kode, 'level: ', row.level_id, 'old: ', old_level, 'key: ', key, 'jml: ', row.jumlah, 'jmls: ', jumlahs)
row.jumlah = jumlahs[key]
EisDBSession.add(row)
EisDBSession.flush()
jumlahs[key] = 0 #key sebelumnya diset jadi 0
key = 'a'+str(row.level_id)
if not key in jumlahs:
jumlahs[key] = row.jumlah
else:
jumlahs[key] += row.jumlah
old_level = row.level_id
#print('kode: ', row.kode, 'level: ', row.level_id, 'old: ', old_level, 'key: ', key, 'jml: ', row.jumlah, 'jmls: ', jumlahs)
EisDBSession.commit()
def validate_parent(tabel, departemen_kd, departemen_nm, rekening, tanggal):
kode = rekening.split('.')
rekening = "\'" + rekening + "\'"
rows = EisDBSession.query(EisRekening).\
filter(literal_column(rekening).like(
func.concat(EisRekening.kode,'%'))).all()
for row in rows:
if not row.kode:
continue
induk = EisDBSession.query(tabel).\
filter_by(
tahun=str(row.tahun),
kode=row.kode.strip(),
departemen_kd = departemen_kd.strip(),
tanggal = tanggal,
).first()
if not induk:
induk = tabel()
induk.tahun = str(tahun)
induk.kode = row.kode.strip()
induk.departemen_kd = departemen_kd.strip()
induk.tanggal = tanggal
induk.departemen_nm = departemen_nm.strip()
induk.level_id = row.kode.count('.')
induk.nama = row.nama.strip()
EisDBSession.add(induk)
EisDBSession.flush()
def insert_data(row, kode, nama, denda_kode, denda_nama):
# TODO
eis = EisDBSession.query(EisArPayment).\
filter_by(tahun = str(tahun),
kode = kode,
departemen_kd = dep_kode,
tanggal = row['tanggal'],
).first()
if not eis:
eis = EisArPayment()
eis.tahun = str(tahun)
eis.kode = kode
eis.departemen_kd = dep_kode
eis.tanggal = row['tanggal']
eis.level_id = kode.count('.')
eis.nama = nama
eis.departemen_nm = dep_nama
eis.jumlah = row['pokok']
EisDBSession.add(eis)
EisDBSession.flush()
validate_parent(EisArPayment, eis.departemen_kd, eis.departemen_nm, eis.kode, eis.tanggal)
if row['denda']:
print('denda', row, denda_kode, denda_nama)
insert_denda(row, denda_kode, denda_nama)
def insert_denda(row, kode, nama):
eis = EisDBSession.query(EisArPayment).\
filter_by(tahun = str(tahun),
kode = kode,
departemen_kd = dep_kode,
tanggal = row['tanggal'],
).first()
if not eis:
eis = EisArPayment()
eis.tahun = str(tahun)
eis.kode = kode
eis.departemen_kd = dep_kode
eis.tanggal = row['tanggal']
eis.level_id = kode.count('.')
eis.nama = nama
eis.departemen_nm = dep_nama
eis.jumlah = row['denda']
EisDBSession.add(eis)
EisDBSession.flush()
validate_parent(EisArPayment, eis.departemen_kd, eis.departemen_nm, eis.kode, eis.tanggal)
def import_pap(all=False):
rek_kode = '4.1.1.06.01.'
rek_nama = 'PAJAK AIR PERMUKAAN'
denda_kode = '4.1.4.07.03.'
denda_nama = 'DENDA PAJAK AIR PERMUKAAN'
sql = "SELECT tg_pros_bayar as tanggal, pap as pokok, den_pajak as denda FROM v_rtimepap "\
"WHERE year(tg_pros_bayar) = '{tahun}' {where} "\
"ORDER BY tg_pros_bayar"
if all:
sql = sql.format(tahun = tahun,
where = '')
else:
sql = sql.format(tahun = tahun,
where = " AND tg_pros_bayar = '{tanggal}'".\
format(tanggal = tanggal.strftime('%d-%m-%Y')),)
print(sql)
rows = engine.fetchall(sql)
for row in rows:
insert_data(row, rek_kode, rek_nama, denda_kode, denda_nama)
EisDBSession.commit()
def import_pkb(all=False):
print ('IMPORT PKB')
rek_kode = '4.1.1.01.01.'
rek_nama = 'PAJAK KENDARAAN BERMOTOR'
denda_kode = '4.1.4.07.01.'
denda_nama = 'PENDAPATAN DENDA KENDARAAN BERMOTOR'
if all:
sql = "SELECT tg_pros_bayar as tanggal, pkb_pok as pokok, pkb_den as denda "\
"FROM v_sts "\
"WHERE year(tg_pros_bayar) = '{tahun}' {where} "\
"ORDER BY tg_pros_bayar "
sql = sql.format(tahun = tahun,
where = '')
print(sql)
rows = engine.fetchall(sql)
for row in rows:
insert_data(row, rek_kode, rek_nama, denda_kode, denda_nama)
sql = "SELECT tg_pros_bayar as tanggal, pkb_pok as pokok, pkb_den as denda "\
"FROM v_rtime "\
"WHERE year(tg_pros_bayar) = '{tahun}' {where} "\
"ORDER BY tg_pros_bayar"
sql = sql.format(tahun = tahun,
where = '')
print(sql)
rows = engine.fetchall(sql)
for row in rows:
insert_data(row, rek_kode, rek_nama, denda_kode, denda_nama)
EisDBSession.commit()
print ('IMPORT BBN')
rek_kode = '4.1.1.03.01.'
rek_nama = 'POKOK BEA BALIK NAMA KENDARAAN BERMOTOR'
denda_kode = '4.1.4.07.02.'
denda_nama = 'PENDAPATAN DENDA BBN-KB'
if all:
sql = "SELECT tg_pros_bayar as tanggal, (bbn1_pok+bbn2_pok) as pokok, "\
"(bbn1_den+bbn2_den) as denda "\
"FROM v_sts "\
"WHERE year(tg_pros_bayar) = '{tahun}' {where} "\
"ORDER BY tg_pros_bayar"
sql = sql.format(tahun = tahun,
where = '')
print(sql)
rows = engine.fetchall(sql)
for row in rows:
insert_data(row, rek_kode, rek_nama, denda_kode, denda_nama)
sql = "SELECT tg_pros_bayar as tanggal, (bbn1_pok+bbn2_pok) as pokok, "\
"(bbn1_den+bbn2_den) as denda "\
"FROM v_rtime "\
"WHERE year(tg_pros_bayar) = '{tahun}' {where} "\
"ORDER BY tg_pros_bayar"
sql = sql.format(tahun = tahun,
where = '')
sql = sql.format(tahun = tahun,
where = " AND tg_pros_bayar = '{tanggal}'".\
format(tanggal = tanggal.strftime('%d-%m-%Y')),)
print(sql)
rows = engine.fetchall(sql)
for row in rows:
insert_data(row, rek_kode, rek_nama, denda_kode, denda_nama)
EisDBSession.commit()
def main(argv):
all = argv and argv[1:] and argv[1]=='all' or False
import_pap(all)
import_pkb(all)
calculate(EisArPayment,all)
argv = sys.argv
print 'argv', argv
main(sys.argv)