-
Notifications
You must be signed in to change notification settings - Fork 0
/
settle_change_new_cards_v2.py
140 lines (139 loc) · 7.38 KB
/
settle_change_new_cards_v2.py
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
# -*- coding: cp1251 -*-
# Äëÿ çàìåíû íîâûõ êàðò íà ñòàðóþ
# usage: settle_canhge_new_cards.py file2 file3 file4
# !!!!!!!!!!!!ãäå file1-ôàéë C:\Users\Sizykh\python_example\settle_change_new_cards.pyñ íîìåðàìè êàðò,êîòîðîûå åçäÿò, íî íå ïîïîëíÿëèñü - ïåðåâûïóùåííûå (îäèí íîìåð â ñòðîêå) ÍÅ ÈÑÏÎËÜÇÓÅÒÑß.
# file2 - ôàéë ñ -ñîîòâåòñòâèåì! ïåðåâûïóùåííàÿ êàðòà;ñòàðàÿ êàðòà (îäíà ïàðà â ñòðîêå, ðàçäåëèòåëü ";")
# file3 - ðååñòð ïåðå÷èñëåíèé, â êîòðîì íóæíî ñäåëàòü ïîäìåíó
# file4 - òåêóùèé ðååñòð ïîïîëíåíèé äëÿ ñâåðêè
# C:\Share_transport\LINK\AISVTK\perevipusk\perevip.csv
import subprocess as sp
import fnmatch
import os
import sys
#ïîëó÷èì ñïèñîê ïàðàìåòðîâ èç êîìàíäíîé ñòðîêè, åñëè îí ïåðåäàí
if len(sys.argv)==4:
#file1 = sys.argv[1]
file2 = sys.argv[1]
file3 = sys.argv[2]
file4 = sys.argv[3]
else:
print (len(sys.argv))
print ('wrong params number ',len(sys.argv), ' , usage: settle_canhge_new_cards.py file2 file3 file4')
sys.exit
#1. first buld list of used cards
used_cards_ls =[card.split(';')[4] for card in open(file3)]
UC = set(used_cards_ls)
UC.discard('')
print ('Set of used card ', len(UC), ' is builded from ', len(used_cards_ls))
charged_cards_ls =[card.split(';')[4] for card in open(file4)]
CHC = set(charged_cards_ls)
CHC.discard('')
print ('Set of charged card ', len(CHC), ' is builded from ', len(charged_cards_ls))
#charged_cards_ls = [charge_card.split(';')[4] for charge_card in open(file4)]
card_ls = list(UC-CHC)
#card_ls=[card.rstrip() for card in open(file1)]
print ('List of used uncharged cards is builded: ', len(card_ls))
#charged_not_used_card_ls = list(CHC-UC)
#card_ls=[card.rstrip() for card in open(file1)]
#print ('List of charged_not_used_card is builded: ', len(charged_not_used_card_ls))
#2. buld
log_file = open('log_change2.txt','w') #íîâûé ôàéë
exch_dct = {}
for card in card_ls:#öèêë ïî ñïèñêó êàðò, êîòîðûå åçäÿò, íî íå ïîïîëíåíû(ïåðåâûïóùåííûå)
print (card, end='' )
log_file.write(card)
for exch_str in open(file2):#öèêë ïî ôàéëó ñîîòâåòñòâèÿ ïåðåâûïóñêîâ äëÿ çàïîëíåíèÿ ñëîâàðÿ
#print (exch_str.rstrip())
if int(card) == int(exch_str.split(';')[0]) and int(exch_str.split(';')[0]) > int(exch_str.split(';')[1]):
exch_dct[exch_str.split(';')[0]]=exch_str.split(';')[1].rstrip()
print(' -added with ',exch_str.split(';')[1].rstrip())
log_file.write(' -added with '+ exch_str.split(';')[1].rstrip()+'\n')
break
elif int(card)== int(exch_str.split(';')[1]) and int(exch_str.split(';')[1]) > int(exch_str.split(';')[0]):
exch_dct[exch_str.split(';')[1]]=exch_str.split(';')[0].rstrip()
print(' -added with ',exch_str.split(';')[0].rstrip())
log_file.write(' -added with '+exch_str.split(';')[1].rstrip()+'\n')
break
else:
#êàðòà íå íàéäåíà ñðåäè ïåðåâûïóùåííûõ
print ('-no match')
log_file.write('-no match'+'\n')
#print(exch_str.split(';')[0])
print ('Âñåãî â ñëîâàðü äîáàâëåíî ', len(exch_dct))
log_file.write('Âñåãî â ñëîâàðü äîáàâëåíî '+ str(len(exch_dct))+'\n')
#òåïåðü ñîáñòâåíî ïîäìåíà íîâîé êàðòû íà ñòàðóþ äëÿ file3, íî ñíà÷àëà ïðîâåðèì, ÷òî "ñòàðûå êàðòû" ïîïîëíåíû íà òåêóùèé ìåñÿö
#print (exch_dct.values(), len(exch_dct.values()))
not_charged_ls =[]
for old_card in exch_dct.values():#öèêë ïî çíà÷åíèÿì ñëîâàðÿ
if old_card in CHC: #ïðîâåðêà íà âõîæäåíèå âî ìíîæåñòâî ïîïîëíåííûõ êàðò
print (old_card, ' is charged - OK')
log_file.write(old_card+ ' is charged - OK'+'\n')
else:
not_charged_ls.append(old_card)
j=0
if len(not_charged_ls)==0: #if all old cards are charged, do replace
print ('Replacing in ', file3,' ...')
res_file = open(file3+'_new2','w') #íîâûé ôàéë
#res_file.write('ORGANIZATIONID;INN;BIC;MIFAREUID;TRANSPORTCARDID;TICKETID;AMOUNT\n')
for f3_st in open(file3): #öèêë ïî ôàéëó èñõîäíîãî ðååñòðà ïåðå÷èñëåíèé
#åñëè â òåêóùåé ñòðîêå ôàéëà ïåðå÷èñëåíèé åñòü "íîâàÿ" êàðòà, òî å¸ íóæíî çàìåíèòü íà ñòàðóþ
if f3_st.split(';')[4] in exch_dct.keys():
res_file.write(f3_st.replace(f3_st.split(';')[4], exch_dct.get(f3_st.split(';')[4])))
j +=1
else:
res_file.write(f3_st)
print ('Card replacing is Done... ',str(j),' ',res_file.name)
#òåïåðü çàìåíèì ñóììó, åñëè ñòàðàÿ êàðòà åçäèëà
old_used_card_ls=[]
for old_card in exch_dct.values():#öèêë ïî çíà÷åíèÿì ñëîâàðÿ ñ ïåðåâûïóùåíûìè êàðòàìè
if old_card in UC: #ïðîâåðêà íà âõîæäåíèå âî ìíîæåñòâî åçäþùèõ êàðò
old_used_card_ls.append(old_card)
print (' List of old_used_card_ls - OK '+str(len(old_used_card_ls)))
log_file.write(' List of old_used_card_ls - OK '+str(len(old_used_card_ls))+'\n')
old_used_card_ls.sort()
#log_file.writelines(old_used_card_ls)
res_file.close()
res_file = open(file3+'_new2')
res_file3 = open(file3+'_new3','w') #íîâûé ôàéë
res_file3.write('ORGANIZATIONID;INN;BIC;MIFAREUID;TRANSPORTCARDID;TICKETID;AMOUNT'+'\n')
j=0
for f3_st in res_file: #öèêë ïî ôàéëó ðååñòðà ïåðå÷èñëåíèé c óæå ïîäìåíåííîé êàðòîé
#åñëè â òåêóùåé ñòðîêå ôàéëà åñòü êàðòà èç ôèëüðà, òî íóæíî ñóììó óìåíüøèòü â 2 ðàçà
if f3_st.split(';')[4] in old_used_card_ls:
f3_ls = f3_st.rstrip().split(';')
log_file.write(f3_ls[4]+';'+f3_ls[6]+';'+str(int(f3_ls[6])//2)+'\n')
f3_ls[6] = int(f3_ls[6])//2
res_file3.write(f3_ls[0]+';'+f3_ls[1]+';'+f3_ls[2]+';'+f3_ls[3]+';'+f3_ls[4]+';'+f3_ls[5]+';'+str(f3_ls[6])+'\n')
j +=1
else:
res_file3.write(f3_st)
print ('Done... ',str(j),' ',res_file3.name)
else:
print (not_charged_ls, ' list of not charged OLD cards in current month')
not_charged_ls = [sstr +'\n' for sstr in not_charged_ls]
not_charged_ls.sort()
log_file.writelines( not_charged_ls)
log_file.write('\n'+' list of not charged OLD cards in current month')
#òåïåðü èñêëþ÷èì èç èñõîäíîãî ðååñòðà ïîïîëíåíåíèé òå êàðòû, êîòîðûõ íåò â îáíîâëåíîì ðååñòðå ñïèñàíèé (res_file3)
res_file3.close()
res_file3 = open(file3+'_new3') #îòêðûâàåì çàíîâî íîâûé ôàéë ðååñòðà ñïèñàíèé â ðåæèìå ïðîñìîòðà
final_card_ls = [card.split(';')[4] for card in res_file3 if len(card.split(';')[0])<5]
ls = [int(card.split(';')[6]) for card in open(file3+'_new3') if len(card.split(';')[0])<5]
control_sum = sum(ls)
print (control_sum)
FC = set(final_card_ls)
FC.discard('')
log_file.write('Êàðò â íîâîì ôàéëå ïåðå÷èñëåíèé: '+str(len(FC))+'\n')
log_file.write('Êîíòðîëüíàÿ ñóììà èç íîâîãî ôàéëà ïåðå÷èñëåíèé :'+str(control_sum)+'\n')
#òåïåðü ìíîæåñòâî FC ñîäåðæèò ñïèñîê óíèêàëüíûõ êàðò, ïî êîòîðûì áóäåò ñïèñàíèå â ýòîì ìåñÿöå
control_sum2=0
k=0
res_file4 = open(file4+'_new4', 'w') #íîâûé ôàéë ðååñòðà ïîïîëíåíèé
res_file4.write ('ORGANIZATIONID;INN;BIC;MIFAREUID;TRANSPORTCARDID;TICKETID;AMOUNT'+'\n')
for str1 in open(file4): #öèêë ïî ñòàðîìó ôàéëó ðååñòðà ïîïîëíåíèé
if str1.split(';')[4] in FC: #åñëè êàðòà åñòü â íîâîì ðååñòðå ñïèñàíèé, òî ñòðîêó ïèøåì â íîâûé ôàéë
res_file4.write(str1)
k +=1
control_sum2 += int(str1.split(';')[6])
log_file.write('Ñòðîê â íîâîì ôàéëå ïîïîëíåíèé: '+str(k)+'\n')
log_file.write('Êîíòðîëüíàÿ ñóììà èç íîâîãî ôàéëà ïîïîëíåíèé :'+str(control_sum2) +'\n')