-
Notifications
You must be signed in to change notification settings - Fork 0
/
quote_approval.py
108 lines (81 loc) · 2.88 KB
/
quote_approval.py
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
import abstra.workflows as aw
import pandas as pd
from abstra.forms import reactive
price_markdown = """
## Total: {total}
"""
def price_string(cents: int) -> str:
return f"${cents / 100:,.2f}"
file_path = "mock_database.csv"
# open files
df = pd.read_csv(file_path)
products_database = df.to_dict(orient="records")
products_ids = set([str(product["id"]) for product in products_database])
updated_proposal = aw.get_data("updated_proposal")
dropdown_options = [{"label": "(Product Unavailable)", "value": "none"}]
for prod in products_database:
prod["id"] = str(prod["id"])
dropdown_options.append({"label": prod["name"], "value": prod["id"]})
# build form page
@reactive
def render(partial):
total = 0
for product in updated_proposal:
product_id = str(product["product_id"])
if product_id not in products_ids:
selected_prod = partial.read_dropdown(
f"{product['quantity']} x {product['product']}",
options=dropdown_options,
key=f"{product['id']}",
required=True,
initial_value="none",
)
else:
selected_prod = partial.read_dropdown(
f"{product['quantity']} x {product['product']}",
options=dropdown_options,
key=f"{product['id']}",
required=True,
initial_value=product_id,
)
if selected_prod == None:
selected_prod = product_id
in_stock_product = next(
(prod for prod in products_database if prod["id"] == selected_prod), None
)
if in_stock_product is not None:
total += int(in_stock_product["price"]) * int(product["quantity"])
partial.display_markdown(price_markdown.format(total=price_string(total)))
result = render.run()
# adjust final estimate
estimate = []
for product_id, in_stock_id in result.items():
product_in_stock = next(
(product for product in products_database if product["id"] == in_stock_id), None
)
product = next(
(product for product in updated_proposal if product["id"] == product_id), None
)
if product_in_stock is None:
estimate.append(
{
"id": product_id,
"product": product["product"],
"quantity": product["quantity"],
"price": "",
"database_match": "Unavailable",
"product_id": 0,
}
)
else:
estimate.append(
{
"id": product_id,
"product": product["product"],
"quantity": product["quantity"],
"price": product_in_stock["price"],
"database_match": product_in_stock["name"],
"product_id": product_in_stock["id"],
}
)
aw.set_data("estimate", estimate)