File size: 3,033 Bytes
c4ce710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import io

st.title("Sales vs SAP Reconciliation")

# File Uploaders
file1 = st.file_uploader("1. Upload Sales Data (File 1)", type=['csv', 'xlsx'])
file2 = st.file_uploader("2. Upload SAP Data (File 2)", type=['csv', 'xlsx'])

if file1 and file2:
    if st.button("Run Reconciliation"):
        with st.spinner("Processing... this may take a minute for large files."):
            # Load Data
            df1 = pd.read_excel(file1) if file1.name.endswith('.xlsx') else pd.read_csv(file1)
            df2 = pd.read_excel(file2) if file2.name.endswith('.xlsx') else pd.read_csv(file2)
            
            # Process File 1
            df1['Card_Acceptor_Name'] = df1['Card_Acceptor_Name'].astype(str)
            df1['Ticket_Number'] = df1['Ticket_Number'].astype(str).str.strip()
            df1['Sales_Details'] = df1['Sales_Details'].astype(str).str.strip()
            df1['Branch'] = df1['Card_Acceptor_Name'].str[:4]
            
            till_extract = df1['Sales_Details'].str.extract(r'(TILL\d+)', expand=False)
            standard_key = df1['Branch'] + df1['Ticket_Number']
            df1['Match_Key'] = till_extract.fillna(standard_key)
            
            df1['Value_Sales'] = pd.to_numeric(df1['Trans_Amount_HUC'], errors='coerce')
            df1.loc[df1['Ticket_Number'].str.startswith('K', na=False), 'Value_Sales'] *= -1
            
            # Process File 2
            df2['Match_Key'] = df2.iloc[:, 3].astype(str).str.strip()
            df2['Value_SAP'] = pd.to_numeric(df2.iloc[:, 8], errors='coerce')
            
            # Merge
            merged = pd.merge(df1, df2, on='Match_Key', how='outer', indicator=True)
            matches = merged[merged['_merge'] == 'both']
            missing_sap = merged[merged['_merge'] == 'left_only']
            missing_sales = merged[merged['_merge'] == 'right_only']
            
            # Display Summary
            st.subheader("Reconciliation Summary")
            st.write(f"**Total Matched:** {len(matches):,}")
            st.write(f"**Missing in SAP:** {len(missing_sap):,}")
            st.write(f"**Missing in Sales:** {len(missing_sales):,}")
            
            # Date Analysis
            if 'Transaction_Date' in missing_sap.columns:
                st.subheader("Top Problem Dates (Missing in SAP)")
                missing_sap['Date_Only'] = pd.to_datetime(missing_sap['Transaction_Date']).dt.date
                st.write(missing_sap['Date_Only'].value_counts().head(10))
            
            # Download Buttons function
            def convert_df(df):
                return df.to_csv(index=False).encode('utf-8')
                
            st.download_button("Download Matched", convert_df(matches), "1_Matched.csv", "text/csv")
            st.download_button("Download Missing in SAP", convert_df(missing_sap), "2_Missing_in_SAP.csv", "text/csv")
            st.download_button("Download Missing in Sales", convert_df(missing_sales), "3_Missing_in_Sales.csv", "text/csv")