log2iso.py
2.94 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
import sys
from argparse import ArgumentParser
from time import time
from ISO8583.ISOErrors import BitNotSet
from sqlalchemy import func
from zope.sqlalchemy import register
from opensipkd.waktu import create_datetime
from opensipkd.string import FixLength
from .models import Common
from .log_models import LogFile
from .common import (
BaseAppById,
my_registry,
create_session,
InvalidSource,
)
class App(BaseAppById):
def __init__(self, conf):
super().__init__(conf)
class Log(self.models.Log, Common):
pass
self.report_orm = Log
if self.conf['models'].find('bphtb') > -1:
self.conf_name = 'bphtb log file last id'
self.pajak = 'bphtb'
else:
self.conf_name = 'pbb log file last id'
self.pajak = 'pbb'
register(self.prod_session)
def get_session_for_save(self): # Override
return self.prod_session
def get_filter_query(self, q):
return q.filter(LogFile.id > self.last_id)
def get_count(self): # Override
q = self.prod_session.query(func.count())
q = self.get_filter_query(q)
return q.scalar()
def get_payment_query(self): # Override
q = self.prod_session.query(LogFile)
q = self.get_filter_query(q)
return q.order_by(LogFile.id)
def get_prefix_log(self): # Override
return f'ID {self.log_id}'
def create_data(self, log_file): # Override
self.log_id = log_file.id
raw = log_file.line[15:]
iso = self.service.Doc()
try:
iso.setIsoContent(raw.encode('utf-8'))
except ValueError as e:
raise InvalidSource(f'ID {log_file.id} tidak dipahami: {str(e)}')
try:
if iso.getBit(3) != self.service.PAYMENT_CODE:
raise InvalidSource(f'ID {log_file.id} bukan payment')
except BitNotSet:
raise InvalidSource(f'ID {log_file.id} bit 3 tidak ada')
# filename = 'sent_220103_log.txt'
tahun = '20' + log_file.filename[5:7] # 2022
tahun = int(tahun)
raw = iso.getBit(13) + iso.getBit(12)
iso.transaction_time.set_raw(raw)
waktu = iso.transaction_time.get_value(tahun)
d = dict(
id=log_file.id, mti=iso.getMTI(), created=waktu,
bits=iso.get_values())
if self.pajak == 'bphtb':
try:
profile = FixLength(self.service.INVOICE_PROFILE)
profile.set_raw(iso.getBit(47))
d['bit_047_data'] = profile.to_dict()
profile = FixLength(self.service.INVOICE_PROFILE2)
profile.set_raw(iso.getBit(48))
d['bit_048_data'] = profile.to_dict()
except BitNotSet:
pass
else:
profile = FixLength(self.service.INVOICE_PROFILE)
profile.set_raw(iso.getBit(62))
d['bits_data'] = {62: profile.to_dict()}
return d