File size: 13,151 Bytes
70d47eb
 
 
 
 
 
 
 
 
 
 
 
 
5a2e857
70d47eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d83a43
70d47eb
 
 
 
5a2e857
 
70d47eb
 
5a2e857
 
 
 
 
70d47eb
5a2e857
70d47eb
5a2e857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d47eb
5a2e857
 
 
 
 
 
70d47eb
5a2e857
 
 
 
 
70d47eb
5a2e857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
70d47eb
 
5a2e857
 
 
 
 
 
 
70d47eb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a2e857
70d47eb
 
 
 
 
4d83a43
5a2e857
70d47eb
 
5a2e857
 
 
 
 
4d83a43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a2e857
4d83a43
 
 
 
 
5a2e857
 
 
4d83a43
5a2e857
4d83a43
5a2e857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4d83a43
5a2e857
 
 
70d47eb
4d83a43
 
 
70d47eb
4d83a43
 
70d47eb
4d83a43
 
 
5a2e857
 
 
 
 
 
 
 
 
 
4d83a43
 
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
from google.cloud import bigquery
import functions_framework
from query_seller_sale import queries
from google.oauth2 import service_account
import json
import requests
import streamlit as st
import pyperclip
from ap import send_message_via_webhook, Webhook_urls
import pandas as pd
import io
import time

# HTML for button styles and progress bar
html_subject = """
<html>
<head>
<style>
    .button {
        display: inline-block;
        padding: 10px 20px;
        border-radius: 12px;
        background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
        box-shadow: 
            0 6px 12px rgba(0, 0, 0, 0.3), 
            0 8px 16px rgba(0, 0, 0, 0.2), 
            inset 0 -2px 4px rgba(255, 255, 255, 0.6);
        text-align: center;
        position: relative;
        transform: translateY(4px);
        transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
        cursor: pointer;
        user-select: none;
    }
    .button:hover {
        box-shadow: 
            0 8px 16px rgba(0, 0, 0, 0.3), 
            0 12px 24px rgba(0, 0, 0, 0.2);
        transform: translateY(2px);
    }
    .button:active {
        box-shadow: 
            0 4px 8px rgba(0, 0, 0, 0.3), 
            0 6px 12px rgba(0, 0, 0, 0.2);
        transform: translateY(0);
    }
    .progress-bar {
        height: 20px; /* Adjusted thickness */
        width: 100%;
        background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
        border-radius: 12px;
        position: relative;
        overflow: hidden;
    }
    .progress-bar span {
        display: block;
        height: 100%;
        width: 0%;
        background: linear-gradient(to right, #ff0000, #ff4d4d); /* Red gradient */
        transition: width 0.3s ease;
    }
</style>
</head>
</html>
"""

st.markdown(html_subject, unsafe_allow_html=True)

def check_duplicates(credentials_file):
    """Check for duplicates using BigQuery with the provided credentials file."""
    results = {}
    credentials = service_account.Credentials.from_service_account_info(json.loads(credentials_file))
    scopes = ['https://www.googleapis.com/auth/cloud-platform',
              'https://www.googleapis.com/auth/drive']
    client = bigquery.Client(credentials=credentials, project=credentials.project_id)

    # Format and execute all queries
    for query_name, query_template in queries.items():
        formatted_query = query_template.format(start_date=start_date_str, end_date=end_date_str)

        # Execute the query
        try:
            query_job = client.query(formatted_query)
            df = query_job.result().to_dataframe()
            results[query_name] = df  # Store the DataFrame in the results dictionary

            # Display the DataFrame in the UI
            subheader_html = f"""
            <div style="background-image: linear-gradient(to right, #800000, #ff0000); 
                        -webkit-background-clip: text; 
                        -webkit-text-fill-color: transparent; 
                        margin: 10px 0;">
                <h4 style="margin: 0; font-size: 20px;">Results for {query_name}</h4> <!-- Change h2 to h4 -->
            </div>
            """

# Render the subheader
            st.markdown(subheader_html, unsafe_allow_html=True)
            st.dataframe(df)  # Display each DataFrame in the Streamlit app

        except Exception as e:
            st.error(f"An error occurred while querying {query_name}: {e}")

    # Check if the expected DataFrames are available for downloading
    if 'query_seller_net_data' in results and 'query_brand_accounting_entries' in results:
        df_net_data = results['query_seller_net_data']
        df_brand_entries = results['query_brand_accounting_entries']

        # Save both DataFrames to an Excel file with different sheets
        excel_buffer = io.BytesIO()
        with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
            df_net_data.to_excel(writer, sheet_name='ss_data', index=False)  # Changed to Net Data
            df_brand_entries.to_excel(writer, sheet_name='ss_entry', index=False)  # Changed to Brand Accounting

        excel_buffer.seek(0)

        # Create a CSV output with both DataFrames
        csv_buffer = io.StringIO()
        df_net_data.to_csv(csv_buffer, index=False, header=True)  # Write first DataFrame to CSV
        csv_buffer.write("\n")  # Add a new line to separate the sections
        df_net_data.to_csv(csv_buffer, index=False)
        df_brand_entries.to_csv(csv_buffer, index=False, header=True)  # Write second DataFrame to CSV
        csv_buffer.seek(0)

        # # Buttons to download the Excel and CSV files
        col1, col2 = st.columns(2)
        with col1:
            button_styles = """
                <style>
                div.stButton > button {
                    color: #ffffff; /* Text color */
                    font-size: 30px;
                    background-image: linear-gradient(to right, #800000, #ff0000); /* Maroon to light red gradient */
                    border: none;
                    padding: 10px 20px;
                    cursor: pointer;
                    border-radius: 15px;
                    display: inline-block;
                    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 8px 15px rgba(0, 0, 0, 0.1); /* Box shadow */
                    transition: all 0.3s ease; /* Smooth transition on hover */
                }
                div.stButton > button:hover {
                    background-color: #00ff00; /* Hover background color */
                    color: #ff0000; /* Hover text color */
                    box-shadow: 0 6px 10px rgba(0, 0, 0, 0.2), 0 12px 20px rgba(0, 0, 0, 0.2); /* Box shadow on hover */
                }
                </style>
                """
            # Inject styles into the app
            st.markdown(button_styles, unsafe_allow_html=True)

            # Display the download button
            st.download_button(
                label="Download Excel file",
                data=excel_buffer,  # Use the base64 data URL for the file
                file_name="query_results.xlsx",
                mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                key="download_excel"  # Optionally specify a key for the button
            )
    return results




# Streamlit UI for uploading credentials
st.markdown(html_subject, unsafe_allow_html=True)

# Upload credentials file
html_subject = """
    <html>
    <head>
    <style>
        .button {
            display: inline-block;
            padding: 10px 20px;
            border-radius: 12px;
            background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
            box-shadow: 
                0 6px 12px rgba(0, 0, 0, 0.3), 
                0 8px 16px rgba(0, 0, 0, 0.2), 
                inset 0 -2px 4px rgba(255, 255, 255, 0.6);
            text-align: center;
            position: relative;
            transform: translateY(4px);
            transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
            cursor: pointer;
            user-select: none;
        }
        .button:hover {
            box-shadow: 
                0 8px 16px rgba(0, 0, 0, 0.3), 
                0 12px 24px rgba(0, 0, 0, 0.2);
            transform: translateY(2px);
        }
        .button:active {
            box-shadow: 
                0 4px 8px rgba(0, 0, 0, 0.3), 
                0 6px 12px rgba(0, 0, 0, 0.2);
            transform: translateY(0);
        }
    </style>
    </head>
    <body>
        <div class="button">
            <h3 style="
                font-size: 20px;
                color: #ffffff;
                background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
                background-clip: text;
                -webkit-background-clip: text;
                text-fill-color: transparent;
                -webkit-text-fill-color: transparent;
                margin: 0;
                text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
            ">Upload the JSON file</h3>
        </div>
    </body>
    </html>
    """


st.markdown(html_subject, unsafe_allow_html=True)
credentials_file = st.file_uploader("", type="json")

st.write("")
col1, col2 = st.columns([0.118, 0.125])
# First column for Start Date
with col1:
    html_subject_start = """
    <html>
    <head>
    <style>
        .button {
            display: inline-block;
            padding: 10px 20px;
            border-radius: 12px;
            background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
            box-shadow: 
                0 6px 12px rgba(0, 0, 0, 0.3), 
                0 8px 16px rgba(0, 0, 0, 0.2), 
                inset 0 -2px 4px rgba(255, 255, 255, 0.6);
            text-align: center;
            position: relative;
            transform: translateY(4px);
            transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
            cursor: pointer;
            user-select: none;
        }
        .button:hover {
            box-shadow: 
                0 8px 16px rgba(0, 0, 0, 0.3), 
                0 12px 24px rgba(0, 0, 0, 0.2);
            transform: translateY(2px);
        }
        .button:active {
            box-shadow: 
                0 4px 8px rgba(0, 0, 0, 0.3), 
                0 6px 12px rgba(0, 0, 0, 0.2);
            transform: translateY(0);
        }
    </style>
    </head>
    <body>
        <div class="button">
            <h3 style="
                font-size: 20px;
                color: #ffffff;
                background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
                background-clip: text;
                -webkit-background-clip: text;
                text-fill-color: transparent;
                -webkit-text-fill-color: transparent;
                margin: 0;
                text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
            ">Start date</h3>
        </div>
    </body>
    </html>
    """

    st.markdown(html_subject_start, unsafe_allow_html=True)
    date_input_key_start = "start_date_input" 
    start_date = st.date_input("", value=None, key=date_input_key_start)

# Second column for End Date
with col2:
    html_subject_end = """
    <html>
    <head>
    <style>
        .button {
            display: inline-block;
            padding: 10px 20px;
            border-radius: 12px;
            background: linear-gradient(to bottom, #f8f9fa, #e0e0e0);
            box-shadow: 
                0 6px 12px rgba(0, 0, 0, 0.3), 
                0 8px 16px rgba(0, 0, 0, 0.2), 
                inset 0 -2px 4px rgba(255, 255, 255, 0.6);
            text-align: center;
            position: relative;
            transform: translateY(4px);
            transition: transform 0.2s ease-in-out, box-shadow 0.2s ease-in-out;
            cursor: pointer;
            user-select: none;
        }
        .button:hover {
            box-shadow: 
                0 8px 16px rgba(0, 0, 0, 0.3), 
                0 12px 24px rgba(0, 0, 0, 0.2);
            transform: translateY(2px);
        }
        .button:active {
            box-shadow: 
                0 4px 8px rgba(0, 0, 0, 0.3), 
                0 6px 12px rgba(0, 0, 0, 0.2);
            transform: translateY(0);
        }
    </style>
    </head>
    <body>
        <div class="button">
            <h3 style="
                font-size: 20px;
                color: #ffffff;
                background-image: linear-gradient(to right, #800000, #ff0000, #ffdab9);
                background-clip: text;
                -webkit-background-clip: text;
                text-fill-color: transparent;
                -webkit-text-fill-color: transparent;
                margin: 0;
                text-shadow: 0 2px 5px rgba(0, 0, 0, 0.4);
            ">End date</h3>
        </div>
    </body>
    </html>
    """

    st.markdown(html_subject_end, unsafe_allow_html=True)
    date_input_key_end = "end_date_input"  # Changed key to be unique
    end_date = st.date_input("", value=None, key=date_input_key_end)
if credentials_file is not None:
    if start_date and end_date:  # Ensure dates are selected
        start_date_str = start_date.strftime("%Y-%m-%d")
        end_date_str = end_date.strftime("%Y-%m-%d")
        
        # Read the credentials file
        credentials_data = credentials_file.read().decode("utf-8")
        
        # Check for duplicates
        results = check_duplicates(credentials_data)
        
        # Prepare to save results in an Excel file
        if results:
            excel_buffer = io.BytesIO()
            with pd.ExcelWriter(excel_buffer, engine='openpyxl') as writer:
                if 'query_seller_net_data' in results:
                    results['query_seller_net_data'].to_excel(writer, sheet_name='Net Data', index=False)
                if 'query_brand_accounting_entries' in results:
                    results['query_brand_accounting_entries'].to_excel(writer, sheet_name='Brand Accounting', index=False)
        else:
            st.error("No results found.")
    else:
        st.warning("Please select both the start and end dates to proceed.")