rpc.py
8.95 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
import sys
import json
import requests
import hmac
import hashlib
import base64
from datetime import datetime, time, date
from sqlalchemy import not_, func, between, and_
from pyramid_rpc.jsonrpc import jsonrpc_method
from pyramid_rpc.jsonrpc import JsonRpcError
from ..tools import (date_from_str, dict_to_str, to_str, get_settings)
##MODEL
from ..models.isipkd import (DBSession, ARInvoice, ARSspd, User, Unit,
ObjekPajak, SubjekPajak, Rekening, Wilayah)
from .arinvoice import save as save_invoice
from ..models.rpc import DepartemenRoute, auth_from_rpc
import logging
log = logging.getLogger('RPC ESIPKD')
###########################################################################
def save_departemen_route(request, values, row=None):
if not row:
row = DepartemenRoute()
if not 'kode' in values or not values['kode']:
return 0
if not 'no_skrd' in values or not values['no_skrd']:
return 0
if not 'tgl_skrd' in values or not values['tgl_skrd']:
return 0
if not 'ar_invoice_id' in values or not values['ar_invoice_id']:
return 0
if not 'status' in values:
values['status'] = 0
row.from_dict(values)
DBSession.add(row)
DBSession.flush()
return row
@jsonrpc_method(method='set_invoice', endpoint='api-webr')
def set_invoice(request, data):
"""
Digunakan untuk menerima data tagihan
:param request:
:param data:
- op_kode
- rek_kode
- dasar
- tarif
- pokok
- denda
- bunga
- jumlah
- periode_1
- periode_2
- jatuh_tempo
- no_skrd
- tgl_skrd
:return:
- kd_bayar
"""
auth_from_rpc(request)
param = ['op_kode','rek_kode','dasar','tarif',
'pokok','denda','bunga','jumlah','periode_1',
'periode_2','jatuh_tempo','tgl_tetap','no_skrd']
get_mandatory(data, param)
## LOGGING SET INVOICE ##
log.error('==== SET INVOICE ====')
log.error(data)
values = clear_null_value(data)
if 'jatuh_tempo' in values and values['jatuh_tempo']:
values['jatuh_tempo'] = date_from_str(values['jatuh_tempo'])
if 'periode_1' in values and values['periode_1']:
values['periode_1'] = date_from_str(values['periode_1'])
if 'periode_2' in values and values['periode_2']:
values['periode_2'] = date_from_str(values['periode_2'])
values['status_bayar'] = 0
op = ObjekPajak.get_by_kode(values['op_kode'])
if not op:
return custom_error(-1,'Objek Pajak kode {} not found'.format(values['op_kode']))
values['objek_pajak_id'] = op.id
values['op_kode'] = op.kode
values['op_nama'] = op.nama
values['op_alamat_1'] = op.alamat_1
values['op_alamat_2'] = op.alamat_2
values['wilayah_id'] = op.wilayah_id
values['subjek_pajak_id'] = op.subjekpajak_id
values['unit_id'] = op.unit_id
subjek = SubjekPajak.get_by_id(values['subjek_pajak_id'])
if not subjek:
return custom_error(-1,'Subjek Pajak id {} not found, contact admin'.format(values['subjek_pajak_id']))
values['wp_kode'] = subjek.kode
values['wp_nama'] = subjek.nama
values['wp_alamat_1'] = subjek.alamat_1
values['wp_alamat_2'] = subjek.alamat_2
unit = Unit.get_by_id(values['unit_id'])
if not unit:
return custom_error(-1,'Unit id {} not found, contact admin'.format(values['unit_id']))
values['unit_kode'] = unit.id
values['unit_nama'] = unit.nama
rek = Rekening.get_by_kode(values['rek_kode'])
if not rek:
return custom_error(-1,'Rekening kode {} not found, contact admin'.format(values['rek_kode']))
values['rekening_id'] = rek.id
values['rek_nama'] = rek.nama
rdata = {}
for v in values:
v = v.strip()
row_invoice = DBSession.query(ARInvoice).\
filter(ARInvoice.no_skrd == values['no_skrd']).first()
if row_invoice:
if row_invoice.status_bayar == 1:
raise JsonRpcBillAllreadyPaidError
return custom_error(-1,'Bill already exist')
else:
invoice = save_invoice(request, values)
# Save Departemen_Route
values['kode'] = invoice.kode
values['no_skrd'] = invoice.no_skrd
values['tgl_skrd'] = invoice.tgl_tetap
values['ar_invoice_id'] = invoice.id
save_departemen_route(request, values)
if not save_departemen_route:
return dict(code=-32603,
message='Gagal menambahkan Invoice',
data=rdata)
rdata['kd_bayar'] = invoice.kode
return dict(code=0,
message='Sukses menambahkan Invoice',
data=rdata)
@jsonrpc_method(method='get_payment', endpoint='api-webr')
def get_payment(request, data):
"""
Digunakan untuk informasi pembayaran
:param request:
:param data:
- kd_bayar
:return:
- ntp
- ntb
- pembayaran_ke
- bunga
- bayar
- tgl_bayar
- jatuh_tempo
- bank_id
- channel_id
"""
auth_from_rpc(request)
get_mandatory(data, ['kd_bayar'])
## LOGGING GET PAYMENT ##
log.error('==== GET PAYMENT ====')
log.error(data)
row_invoice = DBSession.query(ARInvoice).filter_by(kode=data['kd_bayar']).first()
if not row_invoice:
raise JsonRpcBillNotFoundError
row_payment = DBSession.query(ARSspd.ntp, ARSspd.pembayaran_ke, ARSspd.bunga,
ARSspd.bayar, ARSspd.tgl_bayar, ARInvoice.jatuh_tempo,
ARSspd.ntb, ARSspd.bank_id, ARSspd.channel_id).\
join(ARInvoice, ARInvoice.id == ARSspd.arinvoice_id).\
filter(ARInvoice.kode==data['kd_bayar']).\
order_by(ARSspd.pembayaran_ke).all()
if not row_payment:
raise JsonRpcPaymentNotFoundError
rdata = []
for k in row_payment:
d = dict(
kd_bayar = data['kd_bayar'],
ntp = k.ntp.strip(),
pembayaran_ke = k.pembayaran_ke,
bunga = k.bunga,
bayar = k.bayar,
tgl_bayar = ymd(k.tgl_bayar),
jatuh_tempo = ymd(k.jatuh_tempo),
ntb = k.ntb.strip(),
bank_id = k.bank_id,
channel_id = k.channel_id,
)
rdata.append(d)
message = ""
return dict(code=0,
message=message,
data=rdata)
@jsonrpc_method(method='send_payment', endpoint='api-webr')
def send_payment(request, data):
"""
Digunakan untuk Scheduler pengiriman data pembayaran
:param request:
:param data:
- url
- token
:return:
-
"""
auth_from_rpc(request)
get_mandatory(data, ['url','token'])
ws_url = data['url']
token = data['token']
datar = []
row_payment = DBSession.query(
ARSspd.ntp, ARSspd.pembayaran_ke,
ARSspd.bunga, ARSspd.bayar, ARSspd.tgl_bayar,
ARSspd.ntb, ARSspd.bank_id, ARSspd.channel_id,
ARInvoice.kode.label("kd_bayar"), ARInvoice.jatuh_tempo,
DepartemenRoute.id).\
join(DepartemenRoute, DepartemenRoute.ar_invoice_id == ARSspd.arinvoice_id).\
join(ARInvoice, ARInvoice.id == ARSspd.arinvoice_id).\
filter(DepartemenRoute.status==0,
ARInvoice.status_bayar==1).\
order_by(ARSspd.pembayaran_ke).all()
if row_payment:
for k in row_payment:
responsetxt = None
d = dict(
kd_bayar = k.kd_bayar,
ntp = k.ntp,
pembayaran_ke = k.pembayaran_ke,
bunga = k.bunga,
bayar = k.bayar,
tgl_bayar = ymd(k.tgl_bayar),
jatuh_tempo = ymd(k.jatuh_tempo),
ntb = k.ntb,
bank_id = k.bank_id,
channel_id = k.channel_id,
token = token
)
# print('>>>>>>>>>>> DEBUG DATA PAYMENT',d)
## LOGGING SEND PAYMENT ##
log.error('==== SEND PAYMENT ====')
log.error(d)
response = requests.post(ws_url, data=json.dumps(d), verify=False)
if response.status_code != 200:
continue
# try:
# responsetxt = json.loads(response.text)
# if 'result' in responsetxt and responsetxt['result'] != 'success':
# continue
# except:
# continue
# Save Departemen_Route
values = dict(id=k.id,status=1)
row = DepartemenRoute.get_by_id(data['id'])
save_departemen_route(request, values, row)
del d['token']
datar.append(d)
return dict(code=0,
message="Send Payment Success",
data=datar,
)