Serdar commited on
Commit
d6a1d78
1 Parent(s): be48800

upload code

Browse files
Dockerfile ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9.7
2
+
3
+ COPY ./requirements.txt /api/requirements.txt
4
+ COPY ./sabiduria_tool_api /api/sabiduria_tool_api
5
+
6
+ WORKDIR /api
7
+
8
+ RUN python -m venv /api/venv
9
+ # Enable venv
10
+ ENV PATH="/api/venv/bin:$PATH"
11
+
12
+ RUN pip install --upgrade pip
13
+ RUN pip install -r requirements.txt
14
+
15
+ CMD ["uvicorn", "sabiduria_tool_api.main:app", "--host", "0.0.0.0", "--port", "7860"]
sabiduria_tool_api/api/__init__.py ADDED
File without changes
sabiduria_tool_api/api/routes/__init__.py ADDED
File without changes
sabiduria_tool_api/api/routes/router.py ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+
3
+ from sabiduria_tool_api.api.routes import router_sabiduria_tool
4
+ from sabiduria_tool_api.api.routes import router_health_check
5
+
6
+ api_router = APIRouter()
7
+ api_router.include_router(router_health_check.router_health_check)
8
+ api_router.include_router(router_sabiduria_tool.router_file)
sabiduria_tool_api/api/routes/router_health_check.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter
2
+
3
+ from sabiduria_tool_api.services.service_health_check import health_check_message
4
+
5
+ router_health_check = APIRouter()
6
+
7
+ @router_health_check.get("/health", name="Health Check")
8
+ async def health_check() -> str:
9
+ return await health_check_message()
sabiduria_tool_api/api/routes/router_sabiduria_tool.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from starlette.requests import Request
2
+ from fastapi import APIRouter, UploadFile, File
3
+ from fastapi.responses import FileResponse
4
+
5
+ from sabiduria_tool_api.services.service_sabiduria_tool import FileProcess
6
+
7
+ from pathlib import Path
8
+
9
+ router_file = APIRouter()
10
+ ROOT_DIR = str(Path(__file__).parent.parent.parent.parent)
11
+
12
+ @router_file.post("/file", name="concatane files")
13
+ async def upload_files(
14
+ request: Request,
15
+ files: list[UploadFile] = File(description="Multiple files as UploadFile"),
16
+ ):
17
+ processer = FileProcess(files=files, root_dir=ROOT_DIR)
18
+ await processer.calculate()
19
+
20
+ return FileResponse(path=f"{ROOT_DIR}/output.xlsx", filename="output.xlsx")
sabiduria_tool_api/core/__init__.py ADDED
File without changes
sabiduria_tool_api/core/config.py ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ APP_VERSION = "0.0.1"
2
+ APP_NAME = "FILE UPLOAD API"
3
+ API_PREFIX = "/api"
sabiduria_tool_api/core/event_handler.py ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from typing import Callable
3
+
4
+ from loguru import logger
5
+
6
+ logger.level("INFO", color="<green>")
7
+
8
+ def start_app_handler(app: FastAPI) -> Callable:
9
+ def startup() -> None:
10
+ logger.info("Running app start handler.")
11
+
12
+ return startup
13
+
14
+ def stop_app_handler(app: FastAPI) -> Callable:
15
+ def shutdown() -> None:
16
+ logger.info("Running app shutdown handler.")
17
+
18
+ return shutdown
sabiduria_tool_api/main.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+
3
+ from sabiduria_tool_api.core.config import API_PREFIX, APP_NAME, APP_VERSION
4
+ from sabiduria_tool_api.core.event_handler import start_app_handler, stop_app_handler
5
+ from sabiduria_tool_api.api.routes.router import api_router
6
+
7
+ def get_app() -> FastAPI:
8
+ fast_app = FastAPI(title=APP_NAME, version=APP_VERSION)
9
+ fast_app.include_router(api_router, prefix=API_PREFIX)
10
+
11
+ fast_app.add_event_handler("startup", start_app_handler(fast_app))
12
+ fast_app.add_event_handler("shutdown", stop_app_handler(fast_app))
13
+
14
+ return fast_app
15
+
16
+
17
+ app = get_app()
sabiduria_tool_api/services/service_health_check.py ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ async def health_check_message():
2
+ return "API is runnings"
sabiduria_tool_api/services/service_sabiduria_tool.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from shutil import copyfileobj
2
+ from pandas import read_excel, DataFrame
3
+ from os import listdir, remove, mkdir
4
+ from os.path import isdir
5
+ from loguru import logger
6
+
7
+ class FileProcess:
8
+
9
+ def __init__(self, files, root_dir) -> None:
10
+ self.files = files
11
+ self.data_store_path = f"{root_dir}/sabiduria_tool_api/data/"
12
+ self.root_dir = root_dir
13
+ self.final_data = {
14
+ "Ad": [],
15
+ "Soyad": [],
16
+ "TC": [],
17
+ "ToplamMatrah": [],
18
+ "Brut": [],
19
+ "EnAzMatrah": [],
20
+ "OdenecekTutar": [],
21
+ "DamgaVergisiKesintiToplami": [],
22
+ "GelirVergisiKesintiToplami": [],
23
+ "IstisnaEdilenGelirVergisiToplami": [],
24
+ "GorevSayisi": [],
25
+ "EnAzMatrahDosyasi": [],
26
+ "GorevYerleri": []
27
+ }
28
+
29
+ async def save_files(self) -> None:
30
+ if isdir(self.data_store_path):
31
+ old_files = self.get_files()
32
+ for o_file in old_files:
33
+ remove(f"{self.data_store_path}{o_file}")
34
+ else:
35
+ mkdir(self.data_store_path)
36
+ for file in self.files:
37
+ with open(f"{self.data_store_path}{file.filename}", "wb") as destination:
38
+ copyfileobj(file.file, destination)
39
+
40
+
41
+ def get_files(self) -> list:
42
+ all_files = listdir(f"{self.data_store_path}")
43
+ return all_files
44
+
45
+ def preprocess_files(self):
46
+ all_files = self.get_files()
47
+ for file in all_files:
48
+ values = []
49
+ selected_data = read_excel(f"{self.data_store_path}{file}", skiprows=9)
50
+ del selected_data[selected_data.columns[0]]
51
+ selected_data.rename(columns={'Unnamed: 17':'Kesintiler Toplamı'}, inplace=True)
52
+ selected_data.rename(columns={'Unnamed: 18':'Ödenecek Tutar'}, inplace=True)
53
+ for i, sira_no in enumerate(selected_data["Sıra No"]):
54
+ if type(sira_no) != int:
55
+ break
56
+ values.append(selected_data.iloc[[i]].values[0])
57
+ temp_df = DataFrame(values, columns=selected_data.columns.values)
58
+ logger.info(f"FILE SAVED TO: {self.data_store_path}{file}")
59
+ temp_df.to_excel(f"{self.data_store_path}{file}", sheet_name="page1")
60
+
61
+ def init_data(self):
62
+ all_file = self.get_files()
63
+ for file in all_file:
64
+ file_data = read_excel(f"{self.data_store_path}{file}")
65
+ file_name = file.split(".")[0]
66
+ for i, tc in enumerate(file_data['Tc. No']):
67
+ # init data
68
+ if tc not in self.final_data["TC"]:
69
+ self.final_data["TC"].append(tc)
70
+ self.final_data["Ad"].append(file_data["Adı"][i])
71
+ self.final_data["Soyad"].append(file_data["Soyadı"][i])
72
+ self.final_data["EnAzMatrah"].append(file_data["Aylık Vergi Matrahı (Kümülatif)**"][i])
73
+ self.final_data["Brut"].append(file_data["Brüt Ücret ****"][i])
74
+ self.final_data["ToplamMatrah"].append(file_data["Aylık Vergi Matrahı (Kümülatif)**"][i] + file_data["Brüt Ücret ****"][i])
75
+ self.final_data["OdenecekTutar"].append(file_data["Ödenecek Tutar"][i])
76
+ self.final_data["DamgaVergisiKesintiToplami"].append(file_data["Damga Vergisi Kesintisi"][i])
77
+ self.final_data["GelirVergisiKesintiToplami"].append(file_data["Gelir Vergisi Kesintisi"][i])
78
+ self.final_data["IstisnaEdilenGelirVergisiToplami"].append(file_data["İstisna Edilen Gelir Vergisi"][i])
79
+ self.final_data["GorevSayisi"].append(1)
80
+ self.final_data["GorevYerleri"].append(file_name + ", ")
81
+ self.final_data["EnAzMatrahDosyasi"].append(file_name)
82
+ # find lower basis
83
+ else:
84
+ id = self.final_data["TC"].index(tc)
85
+ if self.final_data["EnAzMatrah"][id] > file_data["Aylık Vergi Matrahı (Kümülatif)**"][i]:
86
+ self.final_data["EnAzMatrah"][id] = file_data["Aylık Vergi Matrahı (Kümülatif)**"][i]
87
+ self.final_data["EnAzMatrahDosyasi"][id] = file_name
88
+ self.final_data["Brut"][id] = file_data["Brüt Ücret ****"][i]
89
+ self.final_data["ToplamMatrah"][id] = file_data["Aylık Vergi Matrahı (Kümülatif)**"][i] + file_data["Brüt Ücret ****"][i]
90
+ self.final_data["OdenecekTutar"][id] = file_data["Ödenecek Tutar"][i]
91
+ self.final_data["DamgaVergisiKesintiToplami"][id] = file_data["Damga Vergisi Kesintisi"][i]
92
+ self.final_data["GelirVergisiKesintiToplami"][id] = file_data["Gelir Vergisi Kesintisi"][i]
93
+ self.final_data["IstisnaEdilenGelirVergisiToplami"][id] = file_data["İstisna Edilen Gelir Vergisi"][i]
94
+ self.final_data["GorevYerleri"][id] = file_name + ", "
95
+
96
+ async def calculate(self):
97
+ await self.save_files()
98
+ self.preprocess_files()
99
+ self.init_data()
100
+ all_files = self.get_files()
101
+ for file in all_files:
102
+ file_data = read_excel(f"{self.data_store_path}{file}")
103
+ file_name = file.split(".")[0]
104
+ for i, tc in enumerate(file_data['Tc. No']):
105
+ id = self.final_data["TC"].index(tc)
106
+ if file_name != self.final_data["EnAzMatrahDosyasi"][id]:
107
+ self.final_data["Brut"][id] += file_data["Brüt Ücret ****"][i]
108
+ self.final_data["ToplamMatrah"][id] += file_data["Brüt Ücret ****"][i]
109
+ self.final_data["OdenecekTutar"][id] += file_data["Ödenecek Tutar"][i]
110
+ self.final_data["DamgaVergisiKesintiToplami"][id] += file_data["Damga Vergisi Kesintisi"][i]
111
+ self.final_data["GelirVergisiKesintiToplami"][id] += file_data["Gelir Vergisi Kesintisi"][i]
112
+ self.final_data["IstisnaEdilenGelirVergisiToplami"][id] += file_data["İstisna Edilen Gelir Vergisi"][i]
113
+ self.final_data["GorevSayisi"][id] += 1
114
+ self.final_data["GorevYerleri"][id] += file_name + ", "
115
+
116
+ del all_files
117
+ clean_data = DataFrame(self.final_data)
118
+ del self.final_data
119
+ clean_data.to_excel(f"{self.root_dir}/output.xlsx", sheet_name="page1")