|
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_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) |
|
|
|
|
|
for query_name, query_template in queries.items(): |
|
formatted_query = query_template.format(start_date=start_date_str, end_date=end_date_str) |
|
|
|
|
|
try: |
|
query_job = client.query(formatted_query) |
|
df = query_job.result().to_dataframe() |
|
results[query_name] = df |
|
|
|
|
|
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> |
|
""" |
|
|
|
|
|
st.markdown(subheader_html, unsafe_allow_html=True) |
|
st.dataframe(df) |
|
|
|
except Exception as e: |
|
st.error(f"An error occurred while querying {query_name}: {e}") |
|
|
|
|
|
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'] |
|
|
|
|
|
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) |
|
df_brand_entries.to_excel(writer, sheet_name='ss_entry', index=False) |
|
|
|
excel_buffer.seek(0) |
|
|
|
|
|
csv_buffer = io.StringIO() |
|
df_net_data.to_csv(csv_buffer, index=False, header=True) |
|
csv_buffer.write("\n") |
|
df_net_data.to_csv(csv_buffer, index=False) |
|
df_brand_entries.to_csv(csv_buffer, index=False, header=True) |
|
csv_buffer.seek(0) |
|
|
|
|
|
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> |
|
""" |
|
|
|
st.markdown(button_styles, unsafe_allow_html=True) |
|
|
|
|
|
st.download_button( |
|
label="Download Excel file", |
|
data=excel_buffer, |
|
file_name="query_results.xlsx", |
|
mime="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", |
|
key="download_excel" |
|
) |
|
return results |
|
|
|
|
|
|
|
|
|
|
|
st.markdown(html_subject, unsafe_allow_html=True) |
|
|
|
|
|
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]) |
|
|
|
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) |
|
|
|
|
|
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" |
|
end_date = st.date_input("", value=None, key=date_input_key_end) |
|
if credentials_file is not None: |
|
if start_date and end_date: |
|
start_date_str = start_date.strftime("%Y-%m-%d") |
|
end_date_str = end_date.strftime("%Y-%m-%d") |
|
|
|
|
|
credentials_data = credentials_file.read().decode("utf-8") |
|
|
|
|
|
results = check_duplicates(credentials_data) |
|
|
|
|
|
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.") |
|
|