Spaces:
Running
Running
Update the Sales tab to adjust the data show in the tables
Browse files- streamlit_app.py +85 -8
streamlit_app.py
CHANGED
|
@@ -1559,7 +1559,10 @@ with tab_summary:
|
|
| 1559 |
# rest of the revenue is "Normal" (à la carte food +
|
| 1560 |
# beverage). Derive Normal as Revenue − Delivery so the
|
| 1561 |
# column lines up with the kpi_monthly Revenue total
|
| 1562 |
-
# that drives the other tiles.
|
|
|
|
|
|
|
|
|
|
| 1563 |
delivery_rows = (fi[fi["Type"] == "Delivery"]
|
| 1564 |
if "Type" in fi.columns else fi.iloc[0:0])
|
| 1565 |
_bucket_into(delivery_rows, "Delivery")
|
|
@@ -1567,9 +1570,6 @@ with tab_summary:
|
|
| 1567 |
m["Normal"] = (m["Revenue"] - m.get("Delivery", 0.0)).clip(lower=0)
|
| 1568 |
else:
|
| 1569 |
m["Normal"] = 0.0
|
| 1570 |
-
# Tiew Copper has no Premium / Party Pack channels.
|
| 1571 |
-
m["Premium"] = 0.0
|
| 1572 |
-
m["PartyPack"] = 0.0
|
| 1573 |
|
| 1574 |
else:
|
| 1575 |
# Group-level rows (Holding / CK / Conso) — no channel
|
|
@@ -1894,6 +1894,74 @@ with tab_summary:
|
|
| 1894 |
d_tail = [c for c in ["Rev_Per_Head"] if c in d.columns]
|
| 1895 |
d_round_cols: list[str] = []
|
| 1896 |
d_metric_cols: list[str] = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1897 |
|
| 1898 |
if restaurant_name == "Copper Buffet" and not fact_shift_items.empty:
|
| 1899 |
si_all = _summary_filter(fact_shift_items)
|
|
@@ -1981,10 +2049,11 @@ with tab_summary:
|
|
| 1981 |
d = d.drop(columns=["_Cap", "_TotCust2"])
|
| 1982 |
d_metric_cols.append("%Cap")
|
| 1983 |
|
| 1984 |
-
# Column order
|
| 1985 |
-
#
|
|
|
|
| 1986 |
metric_cols_d = [c for c in ["%Cap", "%Premium"] if c in d.columns]
|
| 1987 |
-
cols = d_base + metric_cols_d + d_tail + d_round_cols
|
| 1988 |
|
| 1989 |
disp = d[cols].copy()
|
| 1990 |
for c in ("Revenue", "Rev_Per_Head"):
|
|
@@ -1992,11 +2061,19 @@ with tab_summary:
|
|
| 1992 |
disp[c] = disp[c].map(fmt_money)
|
| 1993 |
if "Customers" in disp.columns:
|
| 1994 |
disp["Customers"] = disp["Customers"].map(fmt_num)
|
|
|
|
|
|
|
| 1995 |
for c in d_round_cols:
|
| 1996 |
disp[c] = disp[c].map(fmt_num)
|
| 1997 |
for c in metric_cols_d:
|
| 1998 |
disp[c] = disp[c].map(fmt_pct)
|
| 1999 |
-
disp = disp.rename(columns={
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2000 |
st.dataframe(
|
| 2001 |
disp,
|
| 2002 |
use_container_width=True, hide_index=True,
|
|
|
|
| 1559 |
# rest of the revenue is "Normal" (à la carte food +
|
| 1560 |
# beverage). Derive Normal as Revenue − Delivery so the
|
| 1561 |
# column lines up with the kpi_monthly Revenue total
|
| 1562 |
+
# that drives the other tiles. Premium / Party Pack
|
| 1563 |
+
# don't apply here — the columns are intentionally NOT
|
| 1564 |
+
# added so they're omitted from both the table and the
|
| 1565 |
+
# stacked-bar chart legend.
|
| 1566 |
delivery_rows = (fi[fi["Type"] == "Delivery"]
|
| 1567 |
if "Type" in fi.columns else fi.iloc[0:0])
|
| 1568 |
_bucket_into(delivery_rows, "Delivery")
|
|
|
|
| 1570 |
m["Normal"] = (m["Revenue"] - m.get("Delivery", 0.0)).clip(lower=0)
|
| 1571 |
else:
|
| 1572 |
m["Normal"] = 0.0
|
|
|
|
|
|
|
|
|
|
| 1573 |
|
| 1574 |
else:
|
| 1575 |
# Group-level rows (Holding / CK / Conso) — no channel
|
|
|
|
| 1894 |
d_tail = [c for c in ["Rev_Per_Head"] if c in d.columns]
|
| 1895 |
d_round_cols: list[str] = []
|
| 1896 |
d_metric_cols: list[str] = []
|
| 1897 |
+
d_channel_cols: list[str] = []
|
| 1898 |
+
|
| 1899 |
+
# ── Daily channel split — Normal / Premium / Delivery /
|
| 1900 |
+
# Party Pack per (Date, Branch). Same source rules as the
|
| 1901 |
+
# Monthly summary version (GrossRev + SVC; Copper Buffet
|
| 1902 |
+
# uses Type=Package + SubType; Tiew Copper uses
|
| 1903 |
+
# Type='Delivery' with Normal derived as Revenue −
|
| 1904 |
+
# Delivery). The result merges onto `d` so the daily
|
| 1905 |
+
# table renders the same column set as the monthly one.
|
| 1906 |
+
if not fact_items.empty:
|
| 1907 |
+
fi_d = fact_items.copy()
|
| 1908 |
+
if "Date" in fi_d.columns:
|
| 1909 |
+
fi_d["Date"] = pd.to_datetime(fi_d["Date"], errors="coerce")
|
| 1910 |
+
fi_d = fi_d.dropna(subset=["Date"])
|
| 1911 |
+
if "Restaurant" in fi_d.columns:
|
| 1912 |
+
fi_d = fi_d[fi_d["Restaurant"] == restaurant_name]
|
| 1913 |
+
if sel_branches and "Branch" in fi_d.columns:
|
| 1914 |
+
fi_d = fi_d[fi_d["Branch"].isin(sel_branches)]
|
| 1915 |
+
if date_from is not None and "Date" in fi_d.columns:
|
| 1916 |
+
fi_d = fi_d[fi_d["Date"] >= pd.to_datetime(date_from)]
|
| 1917 |
+
if date_to is not None and "Date" in fi_d.columns:
|
| 1918 |
+
fi_d = fi_d[fi_d["Date"] <= pd.to_datetime(date_to)]
|
| 1919 |
+
if not fi_d.empty:
|
| 1920 |
+
_g = pd.to_numeric(fi_d.get("GrossRev", 0), errors="coerce").fillna(0)
|
| 1921 |
+
_s = pd.to_numeric(fi_d.get("SVC", 0), errors="coerce").fillna(0)
|
| 1922 |
+
fi_d["_rev"] = _g + _s
|
| 1923 |
+
|
| 1924 |
+
def _bucket_into_daily(source: pd.DataFrame, dest_col: str) -> None:
|
| 1925 |
+
nonlocal d
|
| 1926 |
+
if source.empty:
|
| 1927 |
+
d[dest_col] = 0.0
|
| 1928 |
+
return
|
| 1929 |
+
agg = (
|
| 1930 |
+
source.groupby(["Date", "Branch"], as_index=False)["_rev"]
|
| 1931 |
+
.sum().rename(columns={"_rev": dest_col})
|
| 1932 |
+
)
|
| 1933 |
+
d = d.merge(agg, on=["Date", "Branch"], how="left")
|
| 1934 |
+
d[dest_col] = d[dest_col].fillna(0.0)
|
| 1935 |
+
|
| 1936 |
+
if restaurant_name == "Copper Buffet":
|
| 1937 |
+
fi_pkg_d = (fi_d[fi_d["Type"] == "Package"]
|
| 1938 |
+
if "Type" in fi_d.columns else fi_d.iloc[0:0])
|
| 1939 |
+
def _by_subtype_d(sub_value: str) -> pd.DataFrame:
|
| 1940 |
+
if "SubType" not in fi_pkg_d.columns:
|
| 1941 |
+
return fi_pkg_d.iloc[0:0]
|
| 1942 |
+
return fi_pkg_d[fi_pkg_d["SubType"] == sub_value]
|
| 1943 |
+
_bucket_into_daily(_by_subtype_d("Normal"), "Normal")
|
| 1944 |
+
_bucket_into_daily(_by_subtype_d("Premium"), "Premium")
|
| 1945 |
+
_bucket_into_daily(_by_subtype_d("Delivery"), "Delivery")
|
| 1946 |
+
_bucket_into_daily(_by_subtype_d("Party Pack"), "PartyPack")
|
| 1947 |
+
elif restaurant_name == "Tiew Copper":
|
| 1948 |
+
# Same restaurant-specific rules as the monthly
|
| 1949 |
+
# version above: only Normal + Delivery; Premium
|
| 1950 |
+
# and Party Pack columns are not added so they're
|
| 1951 |
+
# absent from the table and the chart legend.
|
| 1952 |
+
delivery_rows_d = (fi_d[fi_d["Type"] == "Delivery"]
|
| 1953 |
+
if "Type" in fi_d.columns else fi_d.iloc[0:0])
|
| 1954 |
+
_bucket_into_daily(delivery_rows_d, "Delivery")
|
| 1955 |
+
if "Revenue" in d.columns:
|
| 1956 |
+
d["Normal"] = (d["Revenue"] - d.get("Delivery", 0.0)).clip(lower=0)
|
| 1957 |
+
else:
|
| 1958 |
+
d["Normal"] = 0.0
|
| 1959 |
+
else:
|
| 1960 |
+
for c in ("Normal", "Premium", "Delivery", "PartyPack"):
|
| 1961 |
+
d[c] = 0.0
|
| 1962 |
+
d_channel_cols = [c for c in
|
| 1963 |
+
("Normal", "Premium", "Delivery", "PartyPack")
|
| 1964 |
+
if c in d.columns]
|
| 1965 |
|
| 1966 |
if restaurant_name == "Copper Buffet" and not fact_shift_items.empty:
|
| 1967 |
si_all = _summary_filter(fact_shift_items)
|
|
|
|
| 2049 |
d = d.drop(columns=["_Cap", "_TotCust2"])
|
| 2050 |
d_metric_cols.append("%Cap")
|
| 2051 |
|
| 2052 |
+
# Column order matches the Monthly summary table:
|
| 2053 |
+
# Date · Branch · Revenue · Customers · channels ·
|
| 2054 |
+
# %Cap · %Premium · Rev/Head · rounds.
|
| 2055 |
metric_cols_d = [c for c in ["%Cap", "%Premium"] if c in d.columns]
|
| 2056 |
+
cols = d_base + d_channel_cols + metric_cols_d + d_tail + d_round_cols
|
| 2057 |
|
| 2058 |
disp = d[cols].copy()
|
| 2059 |
for c in ("Revenue", "Rev_Per_Head"):
|
|
|
|
| 2061 |
disp[c] = disp[c].map(fmt_money)
|
| 2062 |
if "Customers" in disp.columns:
|
| 2063 |
disp["Customers"] = disp["Customers"].map(fmt_num)
|
| 2064 |
+
for c in d_channel_cols:
|
| 2065 |
+
disp[c] = disp[c].map(fmt_money)
|
| 2066 |
for c in d_round_cols:
|
| 2067 |
disp[c] = disp[c].map(fmt_num)
|
| 2068 |
for c in metric_cols_d:
|
| 2069 |
disp[c] = disp[c].map(fmt_pct)
|
| 2070 |
+
disp = disp.rename(columns={
|
| 2071 |
+
"Rev_Per_Head": "Rev / Head",
|
| 2072 |
+
"Normal": t("sm_col_normal"),
|
| 2073 |
+
"Premium": t("sm_col_premium"),
|
| 2074 |
+
"Delivery": t("sm_col_delivery"),
|
| 2075 |
+
"PartyPack": t("sm_col_partypack"),
|
| 2076 |
+
})
|
| 2077 |
st.dataframe(
|
| 2078 |
disp,
|
| 2079 |
use_container_width=True, hide_index=True,
|