-
Notifications
You must be signed in to change notification settings - Fork 9
/
tax_script.py
217 lines (194 loc) · 7.74 KB
/
tax_script.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
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
208
209
210
211
212
213
214
215
216
217
import glob
import json
import os
import sys
import subprocess
import datetime
import numpy as np
import pandas as pd
import requests
def get_token(credentials_file):
with open(credentials_file) as f:
lines = f.readlines()
client_id = lines[0].split()[-1]
client_secret = lines[1].split()[-1]
url = "https://id.eta.gov.eg/connect/token"
payload = {
"grant_type": "client_credentials",
"client_id": client_id,
"client_secret": client_secret,
"scope": "InvoicingAPI",
}
headers = {
"Content-Type": "application/x-www-form-urlencoded",
}
response = requests.post(url, headers=headers, data=payload)
# Print the response if the script fails to connect
try:
token = response.json()["access_token"]
except:
print(response.text)
# Exit script
sys.exit()
return token
def upload_doc(invoice):
# read invoice
df_main = pd.read_excel(invoice, nrows=1)
df_main = df_main.loc[0]
# Always set invoice time to 65 minutes before submission to avoid late submission errors
# use 65 minutes to avoid daylight saving problems (invoice in future)
# remove next line if you want to use the invoice time in the excel sheet
df_main["date"] = datetime.datetime.utcnow() - datetime.timedelta(minutes=65)
df_main["date"] = df_main["date"].strftime(
"%Y-%m-%dT%H:%M:%SZ"
) # 2020-12-31T23:59:59Z
df_issuer_address = pd.read_excel(
invoice, header=None, index_col=0, usecols="A:B", skiprows=2, nrows=6
)
df_issuer_address = df_issuer_address.T
df_issuer_address.reset_index(drop=True, inplace=True)
df_issuer_address = df_issuer_address.loc[0]
df_receiver_address = pd.read_excel(
invoice, header=None, index_col=0, usecols="C:D", skiprows=2, nrows=6
)
df_receiver_address = df_receiver_address.T
df_receiver_address.reset_index(drop=True, inplace=True)
df_receiver_address = df_receiver_address.loc[0]
df_items = pd.read_excel(invoice, header=9)
df_totals = df_items.sum()
numerics = df_items.select_dtypes(include=np.number).columns
df_items[numerics] = df_items[numerics].round(8).astype(str)
totalSalesAmount = df_totals["salesTotal"].astype(float).round(8)
totalDiscountAmount = df_totals["discount"].astype(float).round(8)
netAmount = df_totals["netTotal"].astype(float).round(8)
taxTotals = df_totals["Tax"].astype(float).round(8)
totalAmount = df_totals["totalAmount"].astype(float).round(8)
invoice_lines = []
for i, item in df_items.iterrows():
line = {
"description": item["description"],
"itemType": item["itemType"],
"itemCode": item["itemCode"],
"unitType": "EA",
"quantity": float(item["quantity"]),
"internalCode": item["internalCode"],
"salesTotal": float(item["salesTotal"]),
"total": float(item["totalAmount"]),
"valueDifference": 0,
"totalTaxableFees": 0,
"netTotal": float(item["netTotal"]),
"itemsDiscount": 0,
"unitValue": {
"currencySold": "EGP",
"amountEGP": float(item["unitValue"]),
},
"discount": {
"rate": float(item["Discount Rate %"]),
"amount": float(item["discount"]),
},
"taxableItems": [
{
"taxType": "T1",
"amount": float(item["Tax"]),
"subType": "V009",
"rate": 14,
}
],
}
invoice_lines.append(line)
payload = {
"documents": [
{
"issuer": {
"address": {
"branchID": str(df_issuer_address["كود الفرع"]),
"country": df_issuer_address["الدولة"],
"governate": df_issuer_address["المحافظة"],
"regionCity": df_issuer_address["المدينة"],
"street": df_issuer_address["الشارع"],
"buildingNumber": str(df_issuer_address["رقم المبنى"]),
},
"type": "B",
"id": str(df_main["issuerId"]),
"name": df_main["issuerName"], # Max 36
},
"receiver": {
"address": {
"country": df_receiver_address["الدولة"],
"governate": df_receiver_address["المحافظة"],
"regionCity": df_receiver_address["المدينة"],
"street": df_receiver_address["الشارع"],
"buildingNumber": str(df_receiver_address["رقم المبنى"]),
},
"type": "B",
"id": str(df_main["receiverId"]),
"name": df_main["receiverName"],
},
"documentType": "I" if df_main["Inv type"] == 3 else "c",
"documentTypeVersion": "1.0",
"dateTimeIssued": df_main["date"], # 2020-12-31T23:59:59Z
"taxpayerActivityCode": str(df_main["Activity code"]),
"internalID": str(df_main["InternalID"]),
"purchaseOrderReference": str(df_main["PO number"]),
"invoiceLines": invoice_lines,
"totalDiscountAmount": totalDiscountAmount,
"totalSalesAmount": totalSalesAmount,
"netAmount": netAmount,
"taxTotals": [{"taxType": "T1", "amount": taxTotals}],
"totalAmount": totalAmount,
"extraDiscountAmount": 0,
"totalItemsDiscountAmount": 0,
}
]
}
# dump payload to json file. The c# signer expects a single documnent.
unsigned_file = "./c#_signer/SourceDocumentJson.json"
json.dump(
payload["documents"][0],
open(unsigned_file, "w", encoding="utf-8"),
indent=4,
ensure_ascii=False,
)
# run .bat file to sign the json file
subprocess.run(["c#_signer\SubmitInvoices.bat"])
# check cades signature. If it's invalid, send without signature
with open("./c#_signer/Cades.txt", "r") as f:
signature = f.read()
if len(signature) < 20:
print(f"Signature '{signature}' is invalid. Sending without signature.")
payload["documents"][0]["documentTypeVersion"] = "0.9"
else:
# load signed json file
signed_file = "./c#_signer/FullSignedDocument.json"
payload = json.load(open(signed_file, "r", encoding="utf-8"))
url = "https://api.invoicing.eta.gov.eg/api/v1/documentsubmissions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {token}",
}
# print(json.dumps(payload))
# print(json.dumps(payload).encode("utf-8"))
# print(json.dumps(payload, ensure_ascii=False).encode("utf-8"))
response = requests.request(
"POST",
url,
headers=headers,
data=json.dumps(payload, ensure_ascii=False).encode("utf-8"),
)
print(response.text)
if response.json()["rejectedDocuments"]:
raise Exception(f"File {file} is rejected.")
if __name__ == "__main__":
upload_dir = "../upload"
credentials_file = "tax_api_erp_credentials.txt"
token = get_token(credentials_file)
for file in glob.glob(f"{upload_dir}/*.xlsx"):
print(20 * "*")
print(f"Trying to upload {file} ...")
try:
upload_doc(file)
os.remove(file)
print(f"File {file} is uploaded successfully.")
except Exception as e:
print(e)
print(20 * "*")