-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathManual Processor.py
More file actions
207 lines (151 loc) · 7.01 KB
/
Manual Processor.py
File metadata and controls
207 lines (151 loc) · 7.01 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
from distutils import command
import os
from pickle import TRUE
import sys
import tkinter as tk
from pdf2image import convert_from_path
from PIL import ImageTk
import collections as ct
import csv
from tkPDFViewer import tkPDFViewer as pdf
import shutil
from openpyxl import load_workbook
from App import showpdf
os.chdir(r"M:\Contracts Folder")
applicationPath = r"M:\Contracts Folder\Utilities\Invoice Processor"
class ManualProcessor():
def __init__(self) -> None:
pass
def FindPDFs():
rejectDictionary = {}
rejectFolderPath = applicationPath+"/Rejected PDFs/"
rejectPathList = [rejectFolderPath+f for f in os.listdir(rejectFolderPath) if os.path.isfile(os.path.join(rejectFolderPath,f))]
for pdfFilePath in rejectPathList:
if pdfFilePath.endswith(".pdf") == True:
rejectDictionary[pdfFilePath.replace(".pdf",".csv")] = pdfFilePath
print(rejectDictionary)
return rejectDictionary
def FindContractPath(contractNumber):
filePath = "M:\Contracts Folder"
for dirNames in os.listdir(filePath):
if contractNumber[0:2] in dirNames[0:2]:
filePath += "/" + dirNames
for dirNames in os.listdir(filePath):
if contractNumber[0:5] in dirNames[0:5]:
filePath += "/" + dirNames
return filePath
def ShowPDF(PDF_PATH, key):
try:
ManualProcessor.previousEntry
except:
ManualProcessor.previousEntry = {}
root = tk.Tk()
root.title("Manual Entry")
root.geometry('2500x1000')
v1 = pdf.ShowPdf()
v1.img_object_li.clear()
v2 = v1.pdf_view(root,
pdf_location = PDF_PATH,
width = 80, height = 80)
v2.pack(side=tk.LEFT)
typeLabel = tk.Label(root, text='Enter '+key)
typeLabel.pack( side = tk.LEFT )
ManualProcessor.entryBox = tk.Entry (root)
ManualProcessor.entryBox.pack( side = tk.LEFT )
def EnterButtonPress():
ManualProcessor.entry = ManualProcessor.entryBox.get()
ManualProcessor.previousEntry[key] = ManualProcessor.entry
root.destroy()
return
enterButton = tk.Button(root, text="Enter", command=EnterButtonPress)
enterButton.pack( side = tk.LEFT )
def RejectButtonPress():
ManualProcessor.entry = "Reject"
root.destroy()
return
rejectButton = tk.Button(root, text="Reject", command=RejectButtonPress)
rejectButton.pack( side = tk.LEFT )
def PreviousButtonPress():
ManualProcessor.entry = ManualProcessor.previousEntry[key]
root.destroy()
return
try:
previousLabel = tk.Label(root, text= "Previous "+str(ManualProcessor.previousEntry[key]))
previousLabel.pack( side = tk.LEFT )
previousButton = tk.Button(root, text="Previous", command=PreviousButtonPress)
previousButton.pack( side = tk.LEFT )
except:
pass
root.mainloop()
def AcceptPDF(invoiceInfo, inputPDFPath):
#Checks and Rectifies Contracts Local Excel
filePath = ManualProcessor.FindContractPath(invoiceInfo["project_no"])
filePath += "/Commercial/CVR's/Invoice Consolidation"
os.makedirs(filePath, exist_ok=True)
localExcelPath = filePath+"/"+invoiceInfo["project_no"]+".xlsx"
if os.path.isfile(localExcelPath) == False:
print("Excel created at "+filePath)
shutil.copy(applicationPath+"/GIR Template.xlsx", localExcelPath)
pdfOutputPath = filePath+"/Invoice Archive/"+invoiceInfo['invoice_date']+"/"+invoiceInfo['invoice_id']+".pdf"
#Save to Contract Local Excel
wb = load_workbook(localExcelPath)
ws = wb.active
ws.append([invoiceInfo["excel_date"], invoiceInfo["supplier_name"] , '=HYPERLINK("{}", "{}")'.format(pdfOutputPath, invoiceInfo["invoice_id"]), invoiceInfo["IsHire"], invoiceInfo["line_item"] ,float(invoiceInfo["net_amount"])])
wb.save(localExcelPath)
#Save to Contract Global Excel
globalExcelPath = applicationPath+"/Global Invoice Reconcilliation.xlsx"
wb = load_workbook(globalExcelPath)
ws = wb.active
ws.append([invoiceInfo["excel_date"], invoiceInfo["project_no"] , invoiceInfo["supplier_name"] , '=HYPERLINK("{}", "{}")'.format(pdfOutputPath, invoiceInfo["invoice_id"]), invoiceInfo["IsHire"], invoiceInfo["line_item"] ,float(invoiceInfo["net_amount"])])
AttemptSave= True
wb.save(globalExcelPath)
#Send invoice to archive
os.makedirs(os.path.dirname(pdfOutputPath), exist_ok=True)
shutil.move(inputPDFPath, pdfOutputPath)
print(invoiceInfo['invoice_id']+" Processed")
pass
def RejectPDF():
print("PDF REJECTED")
def Process(pdfDictionary):
invoiceInfo = {}
for csvPath in pdfDictionary:
reject = False
invoiceInfo = {}
with open(csvPath) as file:
reader = csv.reader(file)
for row in reader:
if reject == False:
try:
type, value = row
invoiceInfo[type] = value
if value == "Error":
ManualProcessor.ShowPDF(pdfDictionary[csvPath], type)
print(ManualProcessor.entry)
if ManualProcessor.entry == "Reject":
reject = True
else:
invoiceInfo[type] = ManualProcessor.entry
except:
pass
testKeys = ["excel_date","project_no","supplier_name","net_amount"]
if reject == False:
for key in testKeys:
try:
invoiceInfo[key]
except:
ManualProcessor.ShowPDF(pdfDictionary[csvPath], key)
if ManualProcessor.entry == "Reject":
reject = True
else:
invoiceInfo[key] = ManualProcessor.entry
if reject == False:
ManualProcessor.AcceptPDF(invoiceInfo, pdfDictionary[csvPath])
os.remove(csvPath)
else:
os.remove(csvPath)
os.remove(pdfDictionary[csvPath])
def Main():
pdfDictionary = ManualProcessor.FindPDFs()
ManualProcessor.Process(pdfDictionary)
if __name__ == '__main__':
sys.exit(Main())