Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| from utils import * | |
| from data import * | |
| from io import BytesIO | |
| def embScreen(): | |
| if "combined_emb_screen_entries" not in st.session_state: | |
| st.session_state.combined_emb_screen_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") | |
| # Select Embroidery and Screen Print Companies | |
| emb_company = st.selectbox("Select Embroidery Company", list(emb_data.keys()), key="combo_emb_screen_emb_company") | |
| sp_company = st.selectbox("Select Screen Print Company", list(screen_data.keys()), key="combo_emb_screen_sp_company") | |
| df_emb = pd.DataFrame(emb_data[emb_company]) | |
| sp_data = screen_data[sp_company] | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| quantity = st.number_input("Enter Quantity", min_value=1, step=1, key="combo_emb_screen_quantity") | |
| with col2: | |
| stitch_count = st.multiselect("Embroidery Stitch Count", list(df_emb.columns[1:]), key="combo_emb_screen_stitch_count") | |
| with col3: | |
| item_price = st.number_input("Item Cost (Fixed)", min_value=0.0, step=0.01, key="combo_emb_screen_item_price") | |
| # Screen Print Selection | |
| col4, col5, col6 = st.columns(3) | |
| with col4: | |
| selected_type = st.selectbox("Print Type", list(sp_data.keys()), key="combo_emb_screen_print_type") | |
| with col5: | |
| selected_size = st.selectbox("Print Size", list(sp_data[selected_type].columns[1:4]), key="combo_emb_screen_print_size") | |
| with col6: | |
| selected_options = st.multiselect("Options", ["Darks", "Fleece", "90% Poly+", "Sleeves & Legs"], key="combo_emb_screen_options") | |
| # --- Embroidery Pricing --- | |
| quantity_labels_emb = df_emb["Quantity"].tolist() | |
| emb_range = find_quantity_range(quantity, quantity_labels_emb) | |
| emb_stitch_price = float(sum(df_emb[df_emb["Quantity"] == emb_range][stitch_count].values[0])) if stitch_count else 0.0 | |
| # --- Screen Print Pricing --- | |
| sp_df = sp_data[selected_type] | |
| sp_range = next((row["Qty"] for _, row in sp_df.iterrows() if quantity <= int(row["Qty"].split()[-1])), sp_df.iloc[-1]["Qty"]) | |
| sp_row = sp_df[sp_df["Qty"] == sp_range] | |
| sp_base_price = sp_row[selected_size].values[0] if not sp_row.empty else 0.0 | |
| sp_option_cost = sum(sp_row[opt].values[0] for opt in selected_options if opt in sp_row) if not sp_row.empty else 0.0 | |
| sp_total_price = sp_base_price + sp_option_cost | |
| # Total Decoration and Net Cost | |
| total_deco_price = emb_stitch_price + sp_total_price | |
| net_value = quantity * (item_price + total_deco_price) | |
| unit_net_cost = item_price + total_deco_price | |
| net_value = unit_net_cost * quantity | |
| margin_percentage = st.number_input("Enter Margin (%)", min_value=1.0, max_value=99.0, step=0.1, value=40.0, key="combo_emb_screen_margin_val") | |
| margin = margin_percentage / 100 | |
| margin_display = f"{margin_percentage:.2f}%" | |
| total_selling_price = net_value / (1 - margin) | |
| unit_selling_price = total_selling_price / quantity | |
| # Add Entry Button | |
| if st.button("β Add Entry", key="add_emb_screen_entry"): | |
| entry = { | |
| "Brand Name": brand_name, | |
| "Selected Date": cur_date, | |
| "Quantity": quantity, | |
| "Embroidery Stitch Count": stitch_count, | |
| "Emb Price": f"${emb_stitch_price:.2f}", | |
| "Print Type": selected_type, | |
| "Print Size": selected_size, | |
| "SP Base Price": f"${sp_base_price:.2f}", | |
| "SP Options": ", ".join(selected_options) if selected_options else "None", | |
| "SP Option Price": f"${sp_option_cost:.2f}", | |
| "SP Total Price": f"${sp_total_price:.2f}", | |
| "Total Decoration Cost": f"${total_deco_price:.2f}", | |
| "Item Cost": f"${item_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.combined_emb_screen_entries.append(entry) | |
| # Pricing Breakdown Display | |
| if st.session_state.combined_emb_screen_entries: | |
| #st.subheader("Pricing Breakdown") | |
| st.markdown(f"<h4 style='color: #f1c40f';>{brand_name}</h4>", unsafe_allow_html=True) | |
| entries = st.session_state.combined_emb_screen_entries | |
| cols = st.columns(len(entries)) | |
| for i, (col, entry) in enumerate(zip(cols, entries)): | |
| with col: | |
| st.markdown(f"<h4 style='color: #d31145;'>Entry {i+1}</h4>", 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;'>πͺ‘ Embroidery Price:<br><b>{entry['Emb Price']}</b></div><br>", unsafe_allow_html=True) | |
| st.markdown(f"<div style='font-size: 16px;'>π¨οΈ Screen Print Total:<br><b>{entry['SP Total 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;'>π΅ Total 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_emb_screen_{i}"): | |
| del st.session_state.combined_emb_screen_entries[i] | |
| st.rerun() | |
| if st.button("π Reset Entries", key="reset_emb_screen"): | |
| st.session_state.combined_emb_screen_entries = [] | |
| st.rerun() | |
| entries = st.session_state.get("combined_emb_screen_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', 'SP Total Price', 'Emb 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='Emb Screenprint Breakdown') | |
| workbook = writer.book | |
| worksheet = writer.sheets['Emb Screenprint 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='embroideryScreenprint.xlsx', | |
| mime='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', | |
| key="excel-download", | |
| help="Download screenprint fullcolor 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>πͺ‘ Embroidery Price: {entry['Emb Price']}</p> | |
| # <p>π¨οΈ Screen Print Type: {entry['Print Type']}</p> | |
| # <p>π Print Size: {entry['Print Size']}</p> | |
| # <p>βοΈ Options: {entry['SP Options']}</p> | |
| # <p>πΈ Screen Print Total: {entry['SP Total Price']}</p> | |
| # <p>π° Total Net Cost: {entry['Total Net Cost']}</p> | |
| # <p>π Margin: {entry['Margin']}</p> | |
| # <p>π Unit Price: {entry['Unit Selling Price']}</p> | |
| # <p>π΅ Total 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: 'combo_embroidery_screenprint_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=120) | |