Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -15,7 +15,7 @@ warnings.filterwarnings('ignore', category=UserWarning, module='openpyxl')
|
|
| 15 |
PKT = pytz.timezone('Asia/Karachi')
|
| 16 |
|
| 17 |
# --- CONFIGURATION ---
|
| 18 |
-
#
|
| 19 |
SHAREPOINT_DOMAIN = "https://sphereconsultingpk.sharepoint.com"
|
| 20 |
|
| 21 |
def process_excel_streaming(file_obj):
|
|
@@ -71,38 +71,58 @@ def process_excel_streaming(file_obj):
|
|
| 71 |
for index, row in df_to_process.iterrows():
|
| 72 |
excel_row_num = index + 5
|
| 73 |
|
| 74 |
-
# Check JV No
|
| 75 |
if pd.isna(row.get('JV No.')) or str(row.get('JV No.')).strip() == '':
|
| 76 |
error_list.append(f"Row {excel_row_num}: Missing 'JV No.'")
|
| 77 |
|
| 78 |
-
# Check Date
|
| 79 |
if pd.isna(row.get('Date')):
|
| 80 |
error_list.append(f"Row {excel_row_num}: Missing 'Date'")
|
| 81 |
|
| 82 |
-
# Check Vendor
|
| 83 |
if pd.isna(row.get('Paid To / Vendor')) or str(row.get('Paid To / Vendor')).strip() == '':
|
| 84 |
error_list.append(f"Row {excel_row_num}: Missing 'Paid To / Vendor'")
|
| 85 |
|
| 86 |
-
# Check Account Code (COA)
|
| 87 |
if pd.isna(row.get('COA Code Odoo')) or str(row.get('COA Code Odoo')).strip() == '':
|
| 88 |
error_list.append(f"Row {excel_row_num}: Missing 'COA Code Odoo' (Account Code)")
|
| 89 |
|
| 90 |
-
# Check Amount
|
| 91 |
if pd.isna(row.get('Amount')):
|
| 92 |
error_list.append(f"Row {excel_row_num}: Missing 'Amount'")
|
| 93 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
if error_list:
|
| 95 |
yield log("β VALIDATION FAILED! Fix these errors in Excel and re-upload:"), None
|
| 96 |
yield log("-" * 40), None
|
| 97 |
-
|
|
|
|
| 98 |
yield log(f"β {err}"), None
|
| 99 |
-
if len(error_list) >
|
| 100 |
-
yield log(f"... and {len(error_list) -
|
|
|
|
| 101 |
yield log("-" * 40), None
|
| 102 |
yield log("π« Process Aborted. No file generated."), None
|
| 103 |
return
|
| 104 |
|
| 105 |
-
yield log("β
All Data Validated Successfully!"), None
|
| 106 |
|
| 107 |
# --- 4. FORMATTING DATES ---
|
| 108 |
df_to_process['Date'] = pd.to_datetime(df_to_process['Date'])
|
|
@@ -132,17 +152,18 @@ def process_excel_streaming(file_obj):
|
|
| 132 |
excel_row = index + 5
|
| 133 |
try:
|
| 134 |
cell = ws[f"{url_col_letter}{excel_row}"]
|
| 135 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
|
| 137 |
-
#
|
| 138 |
if raw_url:
|
| 139 |
-
# If it's a relative path (doesn't start with http), add the domain
|
| 140 |
if not raw_url.startswith(('http://', 'https://')):
|
| 141 |
-
#
|
| 142 |
-
clean_path = raw_url.lstrip('/')
|
| 143 |
-
|
| 144 |
-
full_url = f"{SHAREPOINT_DOMAIN}/{clean_path}"
|
| 145 |
-
urls.append(full_url)
|
| 146 |
else:
|
| 147 |
urls.append(raw_url)
|
| 148 |
else:
|
|
|
|
| 15 |
PKT = pytz.timezone('Asia/Karachi')
|
| 16 |
|
| 17 |
# --- CONFIGURATION ---
|
| 18 |
+
# If Excel gives a relative link (e.g. /sites/...), we add this prefix.
|
| 19 |
SHAREPOINT_DOMAIN = "https://sphereconsultingpk.sharepoint.com"
|
| 20 |
|
| 21 |
def process_excel_streaming(file_obj):
|
|
|
|
| 71 |
for index, row in df_to_process.iterrows():
|
| 72 |
excel_row_num = index + 5
|
| 73 |
|
| 74 |
+
# 1. Check JV No
|
| 75 |
if pd.isna(row.get('JV No.')) or str(row.get('JV No.')).strip() == '':
|
| 76 |
error_list.append(f"Row {excel_row_num}: Missing 'JV No.'")
|
| 77 |
|
| 78 |
+
# 2. Check Date
|
| 79 |
if pd.isna(row.get('Date')):
|
| 80 |
error_list.append(f"Row {excel_row_num}: Missing 'Date'")
|
| 81 |
|
| 82 |
+
# 3. Check Vendor
|
| 83 |
if pd.isna(row.get('Paid To / Vendor')) or str(row.get('Paid To / Vendor')).strip() == '':
|
| 84 |
error_list.append(f"Row {excel_row_num}: Missing 'Paid To / Vendor'")
|
| 85 |
|
| 86 |
+
# 4. Check Account Code (COA)
|
| 87 |
if pd.isna(row.get('COA Code Odoo')) or str(row.get('COA Code Odoo')).strip() == '':
|
| 88 |
error_list.append(f"Row {excel_row_num}: Missing 'COA Code Odoo' (Account Code)")
|
| 89 |
|
| 90 |
+
# 5. Check Amount
|
| 91 |
if pd.isna(row.get('Amount')):
|
| 92 |
error_list.append(f"Row {excel_row_num}: Missing 'Amount'")
|
| 93 |
|
| 94 |
+
# 6. --- NEW: CHECK PROJECT vs ANALYTIC MATCH ---
|
| 95 |
+
project_val = row.get('Project')
|
| 96 |
+
analytic_val = row.get('Analytical Account Odoo')
|
| 97 |
+
|
| 98 |
+
# Only check if Project column has a value
|
| 99 |
+
if pd.notna(project_val) and str(project_val).strip() != "":
|
| 100 |
+
p_str = str(project_val).strip().lower()
|
| 101 |
+
|
| 102 |
+
# Check if Analytic is empty when Project is present
|
| 103 |
+
if pd.isna(analytic_val) or str(analytic_val).strip() == "":
|
| 104 |
+
error_list.append(f"Row {excel_row_num}: Project '{project_val}' is set, but 'Analytical Account Odoo' is empty.")
|
| 105 |
+
else:
|
| 106 |
+
a_str = str(analytic_val).strip().lower()
|
| 107 |
+
# THE CHECK: Is project name inside the analytic string?
|
| 108 |
+
if p_str not in a_str:
|
| 109 |
+
error_list.append(f"Row {excel_row_num}: Mismatch! Project '{project_val}' not found in Analytic '{analytic_val}'")
|
| 110 |
+
|
| 111 |
+
# --- IF ERRORS FOUND, STOP IMMEDIATELY ---
|
| 112 |
if error_list:
|
| 113 |
yield log("β VALIDATION FAILED! Fix these errors in Excel and re-upload:"), None
|
| 114 |
yield log("-" * 40), None
|
| 115 |
+
# Print first 25 errors to log
|
| 116 |
+
for err in error_list[:25]:
|
| 117 |
yield log(f"β {err}"), None
|
| 118 |
+
if len(error_list) > 25:
|
| 119 |
+
yield log(f"... and {len(error_list) - 25} more errors."), None
|
| 120 |
+
|
| 121 |
yield log("-" * 40), None
|
| 122 |
yield log("π« Process Aborted. No file generated."), None
|
| 123 |
return
|
| 124 |
|
| 125 |
+
yield log("β
All Data & Project Matches Validated Successfully!"), None
|
| 126 |
|
| 127 |
# --- 4. FORMATTING DATES ---
|
| 128 |
df_to_process['Date'] = pd.to_datetime(df_to_process['Date'])
|
|
|
|
| 152 |
excel_row = index + 5
|
| 153 |
try:
|
| 154 |
cell = ws[f"{url_col_letter}{excel_row}"]
|
| 155 |
+
# Get raw URL
|
| 156 |
+
if cell.hyperlink:
|
| 157 |
+
raw_url = cell.hyperlink.target
|
| 158 |
+
else:
|
| 159 |
+
raw_url = None
|
| 160 |
|
| 161 |
+
# Fix Logic: Check for relative paths
|
| 162 |
if raw_url:
|
|
|
|
| 163 |
if not raw_url.startswith(('http://', 'https://')):
|
| 164 |
+
# Remove leading slash if present to avoid double slash
|
| 165 |
+
clean_path = raw_url.lstrip('/')
|
| 166 |
+
urls.append(f"{SHAREPOINT_DOMAIN}/{clean_path}")
|
|
|
|
|
|
|
| 167 |
else:
|
| 168 |
urls.append(raw_url)
|
| 169 |
else:
|