quotingSystem / embroidery.py
keyinnovations's picture
Upload 8 files
4ee93fa verified
import streamlit as st
import pandas as pd
from utils import *
from data import *
from io import BytesIO
def embroidery():
df = pd.DataFrame(emb_data)
if "embroidery_entries" not in st.session_state:
st.session_state.embroidery_entries = []
if "editing_index" not in st.session_state:
st.session_state.editing_index = None
if "temp_edit_data" in st.session_state:
temp = st.session_state.temp_edit_data
st.session_state.brand_name = temp["Brand Name"]
st.session_state.quote_date = pd.to_datetime(temp["Selected Date"])
st.session_state.quantity = temp["Quantity"]
st.session_state.stitch_count = temp["Stitch Count"]
st.session_state.item_price = float(temp["Net Cost (per unit)"].replace("$", "")) - float(temp["Stitch Price"].replace("$", ""))
del st.session_state.temp_edit_data
col1, col2 = st.columns(2)
with col1:
brand_name = st.text_input("Brand Name", key="brand_name")
with col2:
cur_date = st.date_input("Date", value="today", key="quote_date")
company_choice = st.selectbox("Select Company", list(emb_data.keys()), key="emb_company")
if company_choice:
df = pd.DataFrame(emb_data[company_choice])
col1, col2, col3 = st.columns(3)
with col1:
quantity = st.number_input("Enter Quantity", min_value=1, step=1, key="quantity")
with col2:
stitch_count = st.multiselect(
"Select Stitch Count",
options=list(df.columns[1:]),
key="stitch_count"
)
with col3:
item_price = st.number_input('Item Cost (Fixed)', min_value=0.0, step=0.01, key="item_price")
margin_percentage = st.number_input("Enter Margin (%)", min_value=1.0, max_value=99.0, step=0.1, value=40.0)
margin = margin_percentage / 100 # Manual margin stored as decimal
margin_display = f"{margin_percentage:.2f}%" # Display the exact selected percentage
quantity_labels = df["Quantity"].tolist()
selected_range = find_quantity_range(quantity, quantity_labels)
if stitch_count:
stitch_prices = df[df["Quantity"] == selected_range][stitch_count].values[0]
stitch_price = float(sum(stitch_prices))
else:
stitch_price = 0.0
net_value = quantity * (item_price + stitch_price)
unit_net_cost = item_price + stitch_price
net_value = unit_net_cost * quantity
if st.session_state.editing_index is not None:
if st.button("βœ… Update Entry"):
total_selling_price = net_value / (1 - margin)
unit_selling_price = total_selling_price / quantity
entry = {
"Brand Name": brand_name,
"Selected Date": cur_date,
"Quantity": quantity,
"Stitch Count": stitch_count,
"Stitch Price": f"${stitch_price:.2f}",
"Net Cost (per unit)": f"${unit_net_cost:.2f}",
"Total Net Cost": f"${net_value:.2f}",
"Margin": margin_display,
"Unit Selling Price": f"${unit_selling_price:.2f}",
"Total Selling Price": f"${total_selling_price:.2f}"
}
st.session_state.embroidery_entries[st.session_state.editing_index] = entry
st.session_state.editing_index = None
st.rerun()
else:
if st.button("βž• Add Entry"):
if quantity and stitch_count and item_price:
total_selling_price = net_value / (1 - margin)
unit_selling_price = total_selling_price / quantity
entry = {
"Brand Name": brand_name,
"Selected Date": cur_date,
"Quantity": quantity,
"Stitch Count": stitch_count,
"Stitch Price": f"${stitch_price:.2f}",
"Net Cost (per unit)": f"${unit_net_cost:.2f}",
"Total Net Cost": f"${net_value:.2f}",
"Margin": margin_display,
"Unit Selling Price": f"${unit_selling_price:.2f}",
"Total Selling Price": f"${total_selling_price:.2f}"
}
st.session_state.embroidery_entries.append(entry)
if st.session_state.embroidery_entries:
#st.subheader(brand_name)
st.markdown(f"<h4 style='color: #f1c40f';>{brand_name}</h4>", unsafe_allow_html=True)
cols = st.columns(len(st.session_state.embroidery_entries))
for i, (col, entry) in enumerate(zip(cols, st.session_state.embroidery_entries)):
with col:
#st.write(f"### Entry {i+1}")
st.markdown(f"<h4 style='color: #d31145';>Entry {i+1}</h4><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸ“Œ Quantity: <br><b>{entry['Quantity']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸͺ‘ Stitch Price: <br><b>{entry['Stitch Price']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸͺ™ Net Cost (per unit): <br><b>{entry['Net Cost (per unit)']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸ’° Total Net Cost: <br><b>{entry['Total Net Cost']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸ“ˆ Margin: <br><b>{entry['Margin']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸ“Š Unit Price: <br><b>{entry['Unit Selling Price']}</b></div><br>", unsafe_allow_html=True)
st.markdown(f"<div style='font-size: 16px;'>πŸ’° Selling Price: <br><b>{entry['Total Selling Price']}</b></div><br>", unsafe_allow_html=True)
edit_col, delete_col,_ = st.columns([2, 2, 2])
with edit_col:
if st.button(f"✏️", key=f"edit_{i}"):
st.session_state.editing_index = i
st.session_state.temp_edit_data = entry
st.rerun()
with delete_col:
if st.button(f"πŸ—‘οΈ", key=f"delete_{i}"):
del st.session_state.embroidery_entries[i]
st.rerun()
if st.button("πŸ”„ Reset Entries"):
st.session_state.embroidery_entries = []
st.rerun()
entries = st.session_state.get("embroidery_entries", [])
if entries:
st.markdown("""
<style>
.excel-button {
background-color: #d31145;
color: white;
padding: 12px 24px;
border: none;
border-radius: 8px;
font-size: 14px;
font-weight: bold;
cursor: pointer;
transition: background-color 0.3s ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}
.excel-button:hover {
background-color: #b80e3a;
}
</style>
""", unsafe_allow_html=True)
df = pd.DataFrame(entries)
df = df[[
'Quantity', 'Stitch Price', 'Net Cost (per unit)', 'Total Net Cost',
'Margin', 'Unit Selling Price', 'Total Selling Price', 'Selected Date'
]]
output = BytesIO()
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
df.to_excel(writer, index=False, startrow=3, sheet_name='Embroidery Breakdown')
workbook = writer.book
worksheet = writer.sheets['Embroidery Breakdown']
header_format = workbook.add_format({'bold': True, 'font_color': '#d31145', 'font_size': 14})
worksheet.write('A1', brand_name, header_format)
#worksheet.write('A2', cur_date)
processed_data = output.getvalue()
output.seek(0)
st.download_button(
label="πŸ“₯ Download Results as Excel",
data=output,
file_name='embroidery.xlsx',
mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
key="excel-download",
help="Download embroidery pricing breakdown as an Excel file"
)
# def embroidery():
# df = pd.DataFrame(emb_data)
# if "embroidery_entries" not in st.session_state:
# st.session_state.embroidery_entries = []
# col1, col2 = st.columns(2)
# with col1:
# brand_name = st.text_input("Brand Name", key="brand_name")
# with col2:
# cur_date = st.date_input("Date", value="today", key="quote_date")
# company_choice = st.selectbox("Select Company", list(emb_data.keys()), key="emb_company")
# if company_choice:
# df = pd.DataFrame(emb_data[company_choice])
# col1, col2, col3 = st.columns(3)
# with col1:
# quantity = st.number_input("Enter Quantity", min_value=1, step=1, key="quantity")
# with col2:
# stitch_count = st.multiselect(
# "Select Stitch Count",
# options=list(df.columns[1:]),
# key="stitch_count"
# )
# with col3:
# item_price = st.number_input('Item Cost (Fixed)', min_value=0.0, step=0.01, key="item_price")
# margin_percentage = st.number_input("Enter Margin (%)", min_value=1.0, max_value=99.0, step=0.1, value=40.0)
# margin = margin_percentage / 100 # Manual margin stored as decimal
# margin_display = f"{margin_percentage:.2f}%" # Display the exact selected percentage
# quantity_labels = df["Quantity"].tolist()
# selected_range = find_quantity_range(quantity, quantity_labels)
# if stitch_count:
# stitch_prices = df[df["Quantity"] == selected_range][stitch_count].values[0]
# stitch_price = float(sum(stitch_prices))
# else:
# stitch_price = 0.0
# net_value = quantity * (item_price + stitch_price)
# unit_net_cost = item_price + stitch_price
# net_value = unit_net_cost * quantity
# if st.button("βž• Add Entry"):
# if quantity and stitch_count and item_price:
# total_selling_price = net_value / (1 - margin)
# unit_selling_price = total_selling_price / quantity
# entry = {
# "Brand Name": brand_name,
# "Selected Date": cur_date,
# "Quantity": quantity,
# "Stitch Count": stitch_count,
# "Stitch Price": f"${stitch_price:.2f}",
# "Net Cost (per unit)": f"${unit_net_cost:.2f}",
# "Total Net Cost": f"${net_value:.2f}",
# "Margin": margin_display,
# "Unit Selling Price": f"${unit_selling_price:.2f}",
# "Total Selling Price": f"${total_selling_price:.2f}"
# }
# st.session_state.embroidery_entries.append(entry)
# if st.session_state.embroidery_entries:
# #st.subheader(brand_name)
# st.markdown(f"<h4 style='color: #f1c40f';>{brand_name}</h4>", unsafe_allow_html=True)
# cols = st.columns(len(st.session_state.embroidery_entries))
# for i, (col, entry) in enumerate(zip(cols, st.session_state.embroidery_entries)):
# with col:
# #st.write(f"### Entry {i+1}")
# st.markdown(f"<h4 style='color: #d31145';>Entry {i+1}</h4><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸ“Œ Quantity: <br><b>{entry['Quantity']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸͺ‘ Stitch Price: <br><b>{entry['Stitch Price']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸͺ™ Net Cost (per unit): <br><b>{entry['Net Cost (per unit)']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸ’° Total Net Cost: <br><b>{entry['Total Net Cost']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸ“ˆ Margin: <br><b>{entry['Margin']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸ“Š Unit Price: <br><b>{entry['Unit Selling Price']}</b></div><br>", unsafe_allow_html=True)
# st.markdown(f"<div style='font-size: 16px;'>πŸ’° Selling Price: <br><b>{entry['Total Selling Price']}</b></div><br>", unsafe_allow_html=True)
# if st.button(f"❌ Delete {i+1}", key=f"delete_{i}"):
# del st.session_state.embroidery_entries[i]
# st.rerun()
# if st.button("πŸ”„ Reset Entries"):
# st.session_state.entries = []
# st.rerun()
# entries = st.session_state.get("embroidery_entries", [])
# if entries:
# st.subheader("")
# st.markdown("""
# <style>
# .excel-button {
# background-color: #d31145;
# color: white;
# padding: 12px 24px;
# border: none;
# border-radius: 8px;
# font-size: 14px;
# font-weight: bold;
# cursor: pointer;
# transition: background-color 0.3s ease;
# box-shadow: 0 2px 5px rgba(0,0,0,0.2);
# }
# .excel-button:hover {
# background-color: #b80e3a;
# }
# </style>
# """, unsafe_allow_html=True)
# df = pd.DataFrame(entries)
# df = df[[
# 'Quantity', 'Stitch Price', 'Net Cost (per unit)', 'Total Net Cost',
# 'Margin', 'Unit Selling Price', 'Total Selling Price', 'Selected Date'
# ]]
# output = BytesIO()
# with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
# df.to_excel(writer, index=False, startrow=3, sheet_name='Embroidery Breakdown')
# workbook = writer.book
# worksheet = writer.sheets['Embroidery Breakdown']
# header_format = workbook.add_format({'bold': True, 'font_color': '#d31145', 'font_size': 14})
# worksheet.write('A1', brand_name, header_format)
# #worksheet.write('A2', cur_date)
# processed_data = output.getvalue()
# output.seek(0)
# st.download_button(
# label="πŸ“₯ Download Results as Excel",
# data=output,
# file_name='embroidery.xlsx',
# mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
# key="excel-download",
# help="Download embroidery pricing breakdown as an Excel file"
# )
# html = """
# <html>
# <head>
# <script src="https://cdnjs.cloudflare.com/ajax/libs/html2pdf.js/0.10.1/html2pdf.bundle.min.js"></script>
# <style>
# .pdf-button {{
# background-color: #d31145;
# color: white;
# padding: 12px 24px;
# border: none;
# border-radius: 8px;
# font-size: 14px;
# font-weight: bold;
# cursor: pointer;
# transition: background-color 0.3s ease;
# box-shadow: 0 2px 5px rgba(0,0,0,0.2);
# }}
# .pdf-button:hover {{
# background-color: #b80e3a;
# }}
# </style>
# </head>
# <body>
# <div id="pdf-content" style="display: none;">
# <h4 style='color:#d31145;'>{brand_name}</h4>
# <p>{cur_date}</p>
# """.format(brand_name=brand_name, cur_date=cur_date)
# for i, entry in enumerate(entries):
# html += f"""
# <div style='margin-bottom: 20px; border:1px solid #ccc; padding: 10px; border-radius: 8px;'>
# <h4 style='color:#d31145;'>Entry {i+1}</h4>
# <p>πŸ“Œ Quantity: {entry['Quantity']}</p>
# <p>πŸͺ‘ Stitch Price: {entry['Stitch Price']}</p>
# <p>πŸ’° Net Cost: {entry['Total Net Cost']}</p>
# <p>πŸ“ˆ Margin: {entry['Margin']}</p>
# <p>πŸ“Š Unit Price: {entry['Unit Selling Price']}</p>
# <p>πŸ’΅ Selling Price: {entry['Total Selling Price']}</p>
# </div>
# """
# html += """
# </div>
# <button class="pdf-button" onclick="downloadPDF()">πŸ“„ Download Results as PDF</button>
# <script>
# function downloadPDF() {
# const element = document.getElementById("pdf-content");
# element.style.display = 'block';
# html2pdf().set({
# margin: 0.5,
# filename: 'embroidery_breakdown.pdf',
# image: { type: 'jpeg', quality: 0.98 },
# html2canvas: { scale: 2 },
# jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
# }).from(element).save().then(() => {
# element.style.display = 'none';
# });
# }
# </script>
# </body>
# </html>
# """
# st.components.v1.html(html, height=100)