shrudhit commited on
Commit
25b1716
Β·
verified Β·
1 Parent(s): 9942f4b

Create APP.py

Browse files
Files changed (1) hide show
  1. APP.py +319 -0
APP.py ADDED
@@ -0,0 +1,319 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+ import openpyxl
4
+ import os
5
+
6
+ # -------------------------
7
+ # GLOBAL VARIABLES
8
+ # -------------------------
9
+ merged_data = pd.DataFrame()
10
+ total_ads_cost = 0.0
11
+
12
+
13
+ # ============================================================
14
+ # STEP 1 β€” USER UPLOADS EXCEL FILES β†’ GENERATE SHEET2
15
+ # ============================================================
16
+ def step1_generate_sheet2(files):
17
+ global merged_data, total_ads_cost
18
+
19
+ if not files:
20
+ return None, "❌ Please upload at least one Excel file."
21
+
22
+ merged_data = pd.DataFrame()
23
+ total_ads_cost = 0.0
24
+
25
+ for f in files:
26
+ try:
27
+ # Gradio 6.x gives file path directly
28
+ filepath = str(f)
29
+
30
+ df = pd.read_excel(
31
+ filepath,
32
+ sheet_name="Order Payments",
33
+ header=1,
34
+ skiprows=[2]
35
+ )
36
+
37
+ df["Source_File"] = os.path.basename(filepath)
38
+
39
+ merged_data = pd.concat(
40
+ [merged_data, df],
41
+ ignore_index=True
42
+ )
43
+
44
+ # -------------------------
45
+ # Ads Cost (3rd Sheet)
46
+ # -------------------------
47
+ ads_df = pd.read_excel(
48
+ filepath,
49
+ sheet_name=2,
50
+ skiprows=2
51
+ )
52
+
53
+ ads_total = pd.to_numeric(
54
+ ads_df.iloc[:, 7],
55
+ errors="coerce"
56
+ ).sum()
57
+
58
+ total_ads_cost += ads_total
59
+
60
+ except Exception as e:
61
+ print(f"Skipping {filepath}: {e}")
62
+
63
+ # -------------------------
64
+ # Filter Required Statuses
65
+ # -------------------------
66
+ status_keep = [
67
+ "Delivered",
68
+ "Return",
69
+ "Exchange",
70
+ "Shipped"
71
+ ]
72
+
73
+ merged_data = merged_data[
74
+ merged_data["Live Order Status"].isin(status_keep)
75
+ ]
76
+
77
+ merged_data = merged_data.sort_values(
78
+ by="Supplier SKU"
79
+ )
80
+
81
+ # -------------------------
82
+ # Create Sheet2
83
+ # -------------------------
84
+ sheet2 = pd.DataFrame()
85
+
86
+ sheet2["Supplier SKU"] = (
87
+ merged_data["Supplier SKU"]
88
+ .dropna()
89
+ .unique()
90
+ )
91
+
92
+ sheet2["Cost"] = ""
93
+ sheet2["Final Settlement Amount"] = ""
94
+ sheet2["Final Purchase"] = ""
95
+ sheet2["Total Sale"] = ""
96
+
97
+ filename = "Sheet2_Fill_Cost.xlsx"
98
+
99
+ sheet2.to_excel(
100
+ filename,
101
+ index=False
102
+ )
103
+
104
+ return (
105
+ filename,
106
+ "βœ… Sheet2 created! Download and fill COST column."
107
+ )
108
+
109
+
110
+ # ============================================================
111
+ # STEP 2 β€” USER UPLOADS FILLED SHEET2 β†’ FINAL REPORT
112
+ # ============================================================
113
+ def step2_generate_final(sheet2_file):
114
+ global merged_data, total_ads_cost
115
+
116
+ if merged_data.empty:
117
+ return None, "❌ You must complete Step 1 first."
118
+
119
+ if sheet2_file is None:
120
+ return None, "❌ Upload the filled Sheet2 Excel file."
121
+
122
+ filepath = str(sheet2_file)
123
+
124
+ sheet2_filled = pd.read_excel(filepath)
125
+
126
+ # -------------------------
127
+ # Merge Cost
128
+ # -------------------------
129
+ final_sheet1 = merged_data.merge(
130
+ sheet2_filled[
131
+ ["Supplier SKU", "Cost"]
132
+ ],
133
+ on="Supplier SKU",
134
+ how="left"
135
+ )
136
+
137
+ final_sheet1["Cost"] = pd.to_numeric(
138
+ final_sheet1["Cost"],
139
+ errors="coerce"
140
+ ).fillna(0)
141
+
142
+ final_sheet1["Quantity"] = pd.to_numeric(
143
+ final_sheet1["Quantity"],
144
+ errors="coerce"
145
+ ).fillna(0)
146
+
147
+ # Purchase
148
+ final_sheet1["Purchase"] = (
149
+ final_sheet1["Cost"]
150
+ * final_sheet1["Quantity"]
151
+ )
152
+
153
+ # -------------------------
154
+ # Summary Sheet
155
+ # -------------------------
156
+ sheet2_final = (
157
+ final_sheet1.groupby("Supplier SKU")
158
+ .agg({
159
+ "Final Settlement Amount": "sum",
160
+ "Purchase": "sum"
161
+ })
162
+ .reset_index()
163
+ )
164
+
165
+ sheet2_final["Total Sale"] = (
166
+ sheet2_final["Final Settlement Amount"]
167
+ - sheet2_final["Purchase"]
168
+ )
169
+
170
+ sheet2_final = sheet2_final.sort_values(
171
+ by="Total Sale"
172
+ )
173
+
174
+ # -------------------------
175
+ # Ads Cost
176
+ # -------------------------
177
+ sheet2_final["Ads Cost"] = 0.0
178
+
179
+ if not sheet2_final.empty:
180
+ sheet2_final.loc[
181
+ sheet2_final.index[0],
182
+ "Ads Cost"
183
+ ] = round(total_ads_cost, 2)
184
+
185
+ # Rename Columns
186
+ sheet2_final.columns = [
187
+ "Supplier SKU",
188
+ "Final Settlement Amount",
189
+ "Final Purchase",
190
+ "Total Sale",
191
+ "Ads Cost"
192
+ ]
193
+
194
+ sheet2_final.insert(
195
+ 1,
196
+ "Cost",
197
+ sheet2_filled["Cost"]
198
+ )
199
+
200
+ # -------------------------
201
+ # Save Output
202
+ # -------------------------
203
+ output_file = "Final_Meesho_Report.xlsx"
204
+
205
+ with pd.ExcelWriter(output_file) as writer:
206
+ final_sheet1.to_excel(
207
+ writer,
208
+ sheet_name="Sheet1",
209
+ index=False
210
+ )
211
+
212
+ sheet2_final.to_excel(
213
+ writer,
214
+ sheet_name="Sheet2",
215
+ index=False
216
+ )
217
+
218
+ # -------------------------
219
+ # Excel Formula
220
+ # -------------------------
221
+ wb = openpyxl.load_workbook(output_file)
222
+
223
+ ws = wb["Sheet1"]
224
+
225
+ ws["M1"] = "Purchase"
226
+
227
+ last_row = ws.max_row
228
+
229
+ for row in range(2, last_row + 1):
230
+ ws[f"M{row}"] = (
231
+ f'=VLOOKUP(E{row},Sheet2!$A:$B,2,FALSE)*I{row}'
232
+ )
233
+
234
+ wb.save(output_file)
235
+
236
+ return (
237
+ output_file,
238
+ "πŸŽ‰ Final Profit Report generated successfully!"
239
+ )
240
+
241
+
242
+ # ============================================================
243
+ # UI
244
+ # ============================================================
245
+ with gr.Blocks(title="MeeProfit Analyzer") as app:
246
+
247
+ gr.Markdown(
248
+ "# πŸ“Š MeeProfit Analyzer\n"
249
+ "Smart Excel Profit Automation for Meesho Sellers"
250
+ )
251
+
252
+ # STEP 1
253
+ with gr.Tab("✨ Step 1 β€” Create Cost Sheet"):
254
+
255
+ gr.Markdown(
256
+ """
257
+ Upload Meesho Order Payments files.
258
+
259
+ Download generated Sheet2 and fill Cost column.
260
+ """
261
+ )
262
+
263
+ files_step1 = gr.File(
264
+ file_count="multiple",
265
+ label="πŸ“‚ Upload Excel Files"
266
+ )
267
+
268
+ btn1 = gr.Button(
269
+ "πŸš€ Generate Sheet2"
270
+ )
271
+
272
+ sheet2_download = gr.File(
273
+ label="⬇️ Download Sheet2"
274
+ )
275
+
276
+ msg1 = gr.Markdown()
277
+
278
+ btn1.click(
279
+ step1_generate_sheet2,
280
+ inputs=files_step1,
281
+ outputs=[
282
+ sheet2_download,
283
+ msg1
284
+ ]
285
+ )
286
+
287
+ # STEP 2
288
+ with gr.Tab("πŸ“˜ Step 2 β€” Generate Final Report"):
289
+
290
+ gr.Markdown(
291
+ """
292
+ Upload filled Sheet2 file.
293
+ """
294
+ )
295
+
296
+ file_step2 = gr.File(
297
+ label="πŸ“‚ Upload Filled Sheet2"
298
+ )
299
+
300
+ btn2 = gr.Button(
301
+ "πŸ“Š Generate Final Report"
302
+ )
303
+
304
+ final_download = gr.File(
305
+ label="⬇️ Download Final Report"
306
+ )
307
+
308
+ msg2 = gr.Markdown()
309
+
310
+ btn2.click(
311
+ step2_generate_final,
312
+ inputs=file_step2,
313
+ outputs=[
314
+ final_download,
315
+ msg2
316
+ ]
317
+ )
318
+
319
+ app.launch()