-
Notifications
You must be signed in to change notification settings - Fork 0
/
UPS_Delivery_Info.py
118 lines (96 loc) · 3.5 KB
/
UPS_Delivery_Info.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
import csv
import time
from dbConn import conn, cursor
from custInfo import getID, getInv
readdir = 'C:\\UPS_NCSV_EXPORT.csv'
writedir = 'C:\\Users\\taiwei\\Desktop\\test.csv'
with open(readdir,'rb') as ups_file:
with open(writedir, 'wb')as write_file:
writer = csv.writer(write_file, delimiter=',')
reader = csv.reader(ups_file)
# date in YYYYMMDD format
currentdate = int(time.strftime("%Y%m%d"))
#twoYearsAgoDate = currentdate - 20000
twoYearsAgoDate = currentdate - 300
# buffers for speeding up process and use less resources
soBuffer = 0
invNumberBuffer = None
for record in reader:
invNumberFromInv = ''
ccFromInv = ''
salesPersonFromInv = ''
if 'R' in record[0]:
continue
if int(record[16]) < twoYearsAgoDate:
continue
# if '/' is found, separate the string
elif '/' in record[0]:
tempList = record[0].split("/")
# loop through list to validate SO
pos = 0
while pos < len(tempList):
# check if SO only contains digits
if not tempList[pos].isdigit():
tempList.pop(pos)
pos-=1
pos+=1
for result in tempList:
#get invoice info
invNumber = getInv(result)
if invNumber != None:
invNumberFromInv = str(invNumber[0])
salesPersonFromInv = str(invNumber[2])
# check if no customerCode, then check database to retrieve one
if record[2] == '':
address = str(record[8])
date = str(record[16])
getCCID = getID(address, date)
if (getCCID == '') or (getCCID == None) or len(getCCID) < 2:
# check invoice # if customerCode can be found for the last time!!
if invNumber == None:
invNumberResult = "CC Not Found!"
else:
# add CC if found after search using invNumber
record[2] = str(invNumber[1])
else:
ccID = getID(address, date)[0]
record[2] = ccID
# don't write to file if Salesperson is 'RM'
if not "RM" in salesPersonFromInv and not len(salesPersonFromInv) > 2:
# get Invoice Number
finalResult = []
finalResult.extend([result, record[1],record[2],record[3],record[8],record[16], invNumberFromInv,salesPersonFromInv])
writer.writerows([finalResult])
print finalResult
# check if SO is valid
elif record[0].isdigit():
#get invoice info
invNumber = getInv(record[0])
if not invNumber == None:
print invNumber
invNumberFromInv = str(invNumber[0])
salesPersonFromInv = str(invNumber[2])
# check if no customerCode, then check database to retrieve one
if record[2] == '':
address = str(record[8])
date = str(record[16])
getCCID = getID(address, date)
if (getCCID == '') or (getCCID == None) or len(getCCID) < 2:
# check invoice # if customerCode can be found for the last time!!
if invNumber == None:
invNumberResult = "CC Not Found!"
else:
# add CC if found after search using invNumber
record[2] = str(invNumber[1])
else:
ccID = getID(address, date)[0]
record[2] = ccID
# don't write to file if Salesperson is 'RM'
if not "RM" in salesPersonFromInv and not len(salesPersonFromInv) > 2:
# get Invoice Number
finalResult = []
finalResult.extend([record[0], record[1],record[2],record[3],record[8],record[16], invNumberFromInv,salesPersonFromInv])
writer.writerows([finalResult])
print finalResult
input = raw_input('Finished!')
conn.close()