license_statistics.py
8.53 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
import re
import os
import ast
import datetime
from openpyxl import Workbook
from django.core.management import BaseCommand
from settings import conf
from common.mixins import LoggerMixin
from apps.doc.models import HILDoc, AFCDoc
from apps.doc import consts
class Command(BaseCommand, LoggerMixin):
def __init__(self):
super().__init__()
self.log_base = '[license statistics]'
self.header_map = {
consts.MVI_CLASSIFY: [('申请ID', '发票代码', '发票号码', '开票日期', '不含税价', '发票联', '购买方名称',
'购买方证件号码', '纳税人识别号', '车架号', '价税合计小写', '销货单位名称', '增值税税额',
'增值税税率', '发票章有无', '价税合计大写', '发动机号', '车辆类型', '厂牌型号', '产地',
'合格证号', '进口证明书号', '商检单号', '电话', '销货方纳税人识别号', '账号', '地址',
'开户银行', '主管税务机关及代码', '吨位', '限乘人数')],
consts.IC_CLASSIFY: [('申请ID', '姓名', '公民身份号码', '出生年月', '住址', '性别', '民族'),
('申请ID', '有效期限', '签发机关')],
consts.RP_CLASSIFY: [('申请ID', '姓名', '公民身份号码', '出生年月', '住址', '性别'),
('申请ID', '有效期限', '签发机关')],
consts.BC_CLASSIFY: [('申请ID', '发卡行名称', '银行卡号', '银行卡类型', '持卡人姓名')],
consts.BL_CLASSIFY: [('申请ID', '统一社会信用代码', '名称', '类型', '法定代表人', '成立日期', '营业期限', '注册资本',
'住所', '经营范围')],
consts.UCI_CLASSIFY: [('申请ID', '发票代码', '发票号码', '开票日期', '车价合计小写', '发票联', '买方单位/个人',
'买方单位代码/身份证号码', '车架号', '车价合计大写', '二手车市场', '发票章有无', '车牌照号',
'登记证号', '买方单位/住址', '车辆类型', '厂牌型号', '转入地车辆管理所名称', '卖方单位/个人',
'卖方单位代码/身份证号码', '卖方单位/个人住址')],
consts.EEP_CLASSIFY: [('申请ID', '姓名', '证件号码', '换证次数(签发次数)', '有效期限', '出生日期', '性别',
'签发机关', '签发地点')],
consts.DL_CLASSIFY: [('申请ID', '1 号牌号码', '3 所有人', '5 使用性质', '7 车辆识别代号', '9 注册日期', '10 发证日期',
'2 车辆类型', '4 住址', '6 品牌型号', '8 发动机号码'),
('申请ID', '1 号牌号码', '11 档案编号', '12 核定载人数', '13 总质量', '14 整备质量',
'15 核对载质量', '16 外廓尺寸', '17 准牵引总质量')],
consts.PP_CLASSIFY: [('申请ID', '类型/Type', '姓名/Name', '护照号码/Passport No', '有效期至/Date of expiry',
'签发日期/Date of issue', '国家码/Country Code', '性别/Sex', '国籍/Nationality',
'出生日期/Date of birth', '出生地点/Place of birth', '签发地点/Place of issue')],
consts.MVC_CLASSIFY: [('申请ID', '机动车所有人/身份证明名称/号码', '登记日期', '车辆识别代号/车架号', '车辆出厂日期',
'发证日期', '使用性质', '车辆获得方式', '机动车登记编号', '车辆类型', '车辆品牌', '车辆型号',
'车身颜色', '国产/进口', '发动机号', '发动机型号', '制造厂名称', '登记机关', '机动车登记证书编号'),
('姓名/名称', '身份证明名称/号码', '转移登记日期')],
consts.VAT_CLASSIFY: [('申请ID', '发票代码', '发票代码(开具)', '发票号码', '发票号码(开具)', '开票日期', '校验码',
'货物或应税劳务、服务名称', '开具金额合计(不含税)', '税率', '税额合计', '价税合计(小写)',
'价税合计(大写)', '购买方名称', '购买方纳税人识别号', '购买方地址、电话', '购买方开户行及账号',
'销售方名称', '销售方纳税人识别号', '销售方地址、电话', '销售方开户行及账号', '销售方:(章)',
'备注')],
}
def add_arguments(self, parser):
parser.add_argument(
'--date',
default=datetime.date.today() - datetime.timedelta(days=1),
dest='date',
help='将要计算的日期,格式: 2018-01-01'
)
def handle(self, *args, **kwargs):
date = kwargs.get('date')
if isinstance(date, str):
if not re.match(r'\d{4}-\d{2}-\d{2}', date):
print('date format error')
return
date_str = date
else:
date_str = date.strftime('%Y-%m-%d')
excel_dir = os.path.join(conf.DATA_DIR, 'AFC', 'Logs')
if not os.path.exists(excel_dir):
print('excel dir not exists')
return
excel_path = os.path.join(excel_dir, 'license_{0}.xlsx'.format(date_str))
log_path = os.path.join(conf.LOG_DIR, 'license.log.{0}'.format(date_str))
if not os.path.exists(log_path):
print('log_path not exists')
return
wb = Workbook()
for classify, (_, name, _, _, _, _) in consts.LICENSE_ORDER:
if classify == consts.DL_CLASSIFY or classify == consts.VAT_CLASSIFY:
continue
ws = wb.create_sheet(name)
headers = self.header_map.get(classify, [])
for header in headers:
ws.append(header)
wb.remove(wb.get_sheet_by_name('Sheet'))
with open(log_path, 'r', encoding='utf-8') as fp:
for line in fp:
search_obj = re.search(r'task=(.*) license_summary=(.*)', line)
task_str = search_obj.group(1)
license_summary = ast.literal_eval(search_obj.group(2))
business_type, doc_id_str = task_str.split(consts.SPLIT_STR)
doc_id = int(doc_id_str)
doc_class = HILDoc if business_type == consts.HIL_PREFIX else AFCDoc
doc_queryset = doc_class.objects.filter(id=doc_id).values_list('application_id', 'document_scheme')
if len(doc_queryset) == 0:
continue
application_id, document_scheme = doc_queryset[0]
if document_scheme != consts.DOC_SCHEME_LIST[1]:
continue
for classify, (_, name, field_order, side_diff, _, _) in consts.LICENSE_ORDER:
if classify == consts.DL_CLASSIFY or classify == consts.VAT_CLASSIFY:
continue
license_list = license_summary.get(classify)
if not license_list:
continue
ws = wb.get_sheet_by_name(name)
for license_dict in license_list:
if classify == consts.IC_CLASSIFY and license_dict.get('类别') == '1': # 居住证处理
license_summary.setdefault(consts.RP_CLASSIFY, []).append(license_dict)
continue
if side_diff:
key, field_order_yes, field_order_no = consts.FIELD_ORDER_MAP.get(classify)
field_order = field_order_yes if key in license_dict else field_order_no
all_value = []
for search_field, write_field in field_order:
if write_field is None:
continue
field_value = license_dict.get(search_field, '')
if isinstance(field_value, list):
all_value.append('\n'.join(field_value))
else:
all_value.append(field_value)
ws.append((application_id, *all_value))
wb.save(excel_path)