Spaces:
Running
Running
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +10 -20
src/streamlit_app.py
CHANGED
|
@@ -268,22 +268,22 @@ STRICT POLICY RULES (apply exactly, do not deviate):
|
|
| 268 |
- In this JSON, output all values as strings. If a field is not present or cannot be determined with high confidence, output "" (empty string). Do not use null, 0, or placeholders.
|
| 269 |
2) Currency selection (multi-currency invoices)
|
| 270 |
- If multiple currencies are shown, ALWAYS choose the recipient/customer currency for all monetary fields in items and summary.
|
| 271 |
-
- Do NOT perform FX conversion. Select the column/figures that are explicitly in the recipient
|
| 272 |
- For "summary.currency", prefer the printed 3-letter code (e.g., USD, EUR, INR). If only an unambiguous symbol is present, map it (βΉβINR, β¬βEUR, $βUSD when clearly USD). If ambiguous, leave "".
|
| 273 |
-
3) Tax handling (no rounding of rates; don
|
| 274 |
- Do NOT round tax percentages. Use the original precision for any calculations; keep the printed formatting for "summary.tax_rate".
|
| 275 |
-
- If a TOTAL tax amount is explicitly printed on the invoice (e.g.,
|
| 276 |
a) If per-line tax amounts are printed, copy them directly.
|
| 277 |
-
b) If per-line tax amounts are not printed, allocate the printed TOTAL tax proportionally across line items by each line
|
| 278 |
- If NO total tax amount is printed but a tax rate is printed, compute per-line tax as: tax = (quantity * unit_price β discount) Γ (exact, unrounded tax rate). Then set "summary.tax_amount" = sum of per-line taxes.
|
| 279 |
- "items[].amount" is the pre-tax line amount AFTER discount. "items[].Line_total" = amount + tax.
|
| 280 |
4) Discounts
|
| 281 |
- If discounts are present (per-line or overall), compute tax on the discounted base: (quantity * unit_price β discount). Never compute tax on the undiscounted amount.
|
| 282 |
5) Due date calculation from payment terms
|
| 283 |
-
- Preserve the invoice
|
| 284 |
- If explicit due date is printed, use it as "due_date".
|
| 285 |
- If payment terms specify Net X (e.g., Net 30), set due_date = invoice_date + X days (same format as invoice_date).
|
| 286 |
-
- If terms say
|
| 287 |
- If both a printed due date and terms exist and they conflict, prefer the printed due date.
|
| 288 |
6) Items array
|
| 289 |
- Include every visible line item. Preserve multi-line descriptions using literal "\\n" where line breaks exist.
|
|
@@ -1125,17 +1125,7 @@ elif len(st.session_state.batch_results) > 0:
|
|
| 1125 |
with frame_right:
|
| 1126 |
st.subheader(f"Editable Invoice: {current['file_name']}")
|
| 1127 |
|
| 1128 |
-
#
|
| 1129 |
-
swap_cols = st.columns([1,1,2])
|
| 1130 |
-
with swap_cols[0]:
|
| 1131 |
-
if st.button("β Swap Sender β Recipient", key=f"swap_{selected_hash}"):
|
| 1132 |
-
sn = f"Sender Name_{selected_hash}"
|
| 1133 |
-
rn = f"Recipient Name_{selected_hash}"
|
| 1134 |
-
sa = f"Sender Address_{selected_hash}"
|
| 1135 |
-
ra = f"Recipient Address_{selected_hash}"
|
| 1136 |
-
st.session_state[sn], st.session_state[rn] = st.session_state[rn], st.session_state[sn]
|
| 1137 |
-
st.session_state[sa], st.session_state[ra] = st.session_state[ra], st.session_state[sa]
|
| 1138 |
-
st.rerun()
|
| 1139 |
|
| 1140 |
# ----------------- FORM START -----------------
|
| 1141 |
with st.form(key=f"edit_form_{selected_hash}", clear_on_submit=False):
|
|
@@ -1300,9 +1290,9 @@ elif len(st.session_state.batch_results) > 0:
|
|
| 1300 |
|
| 1301 |
st.session_state.batch_results[selected_hash]["edited_data"] = updated
|
| 1302 |
st.success(f"β
Saved: {current['file_name']} | Updated totals: Subtotal=${calculated_subtotal:,.2f}, Tax=${calculated_total_tax:,.2f}, Total=${calculated_total:,.2f}")
|
| 1303 |
-
st.rerun()
|
| 1304 |
|
| 1305 |
-
# Per-file CSV download (
|
| 1306 |
d_currency = st.session_state.get(f"Currency_{selected_hash}", 'USD')
|
| 1307 |
if d_currency == 'Other':
|
| 1308 |
d_currency = st.session_state.get(f"Currency_Custom_{selected_hash}", '')
|
|
@@ -1372,4 +1362,4 @@ else:
|
|
| 1372 |
with frame_left:
|
| 1373 |
st.caption("Ready when you are.")
|
| 1374 |
with frame_right:
|
| 1375 |
-
st.caption("Preview & editor will appear here after extraction.")
|
|
|
|
| 268 |
- In this JSON, output all values as strings. If a field is not present or cannot be determined with high confidence, output "" (empty string). Do not use null, 0, or placeholders.
|
| 269 |
2) Currency selection (multi-currency invoices)
|
| 270 |
- If multiple currencies are shown, ALWAYS choose the recipient/customer currency for all monetary fields in items and summary.
|
| 271 |
+
- Do NOT perform FX conversion. Select the column/figures that are explicitly in the recipient's currency.
|
| 272 |
- For "summary.currency", prefer the printed 3-letter code (e.g., USD, EUR, INR). If only an unambiguous symbol is present, map it (βΉβINR, β¬βEUR, $βUSD when clearly USD). If ambiguous, leave "".
|
| 273 |
+
3) Tax handling (no rounding of rates; don't recompute given totals)
|
| 274 |
- Do NOT round tax percentages. Use the original precision for any calculations; keep the printed formatting for "summary.tax_rate".
|
| 275 |
+
- If a TOTAL tax amount is explicitly printed on the invoice (e.g., "Tax", "VAT", "IGST", "Total Tax"), TREAT IT AS AUTHORITATIVE. Do NOT recompute a new total.
|
| 276 |
a) If per-line tax amounts are printed, copy them directly.
|
| 277 |
+
b) If per-line tax amounts are not printed, allocate the printed TOTAL tax proportionally across line items by each line's net amount (quantity * unit_price β discount). Use precise arithmetic; ensure the sum of allocated per-line taxes equals the printed TOTAL tax (adjust the last cent minimally if required).
|
| 278 |
- If NO total tax amount is printed but a tax rate is printed, compute per-line tax as: tax = (quantity * unit_price β discount) Γ (exact, unrounded tax rate). Then set "summary.tax_amount" = sum of per-line taxes.
|
| 279 |
- "items[].amount" is the pre-tax line amount AFTER discount. "items[].Line_total" = amount + tax.
|
| 280 |
4) Discounts
|
| 281 |
- If discounts are present (per-line or overall), compute tax on the discounted base: (quantity * unit_price β discount). Never compute tax on the undiscounted amount.
|
| 282 |
5) Due date calculation from payment terms
|
| 283 |
+
- Preserve the invoice's original date format for both "invoice_date" and "due_date".
|
| 284 |
- If explicit due date is printed, use it as "due_date".
|
| 285 |
- If payment terms specify Net X (e.g., Net 30), set due_date = invoice_date + X days (same format as invoice_date).
|
| 286 |
+
- If terms say "upon receipt", "upon publication", or equivalent, due_date = invoice_date.
|
| 287 |
- If both a printed due date and terms exist and they conflict, prefer the printed due date.
|
| 288 |
6) Items array
|
| 289 |
- Include every visible line item. Preserve multi-line descriptions using literal "\\n" where line breaks exist.
|
|
|
|
| 1125 |
with frame_right:
|
| 1126 |
st.subheader(f"Editable Invoice: {current['file_name']}")
|
| 1127 |
|
| 1128 |
+
# SWAP BUTTON REMOVED COMPLETELY
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1129 |
|
| 1130 |
# ----------------- FORM START -----------------
|
| 1131 |
with st.form(key=f"edit_form_{selected_hash}", clear_on_submit=False):
|
|
|
|
| 1290 |
|
| 1291 |
st.session_state.batch_results[selected_hash]["edited_data"] = updated
|
| 1292 |
st.success(f"β
Saved: {current['file_name']} | Updated totals: Subtotal=${calculated_subtotal:,.2f}, Tax=${calculated_total_tax:,.2f}, Total=${calculated_total:,.2f}")
|
| 1293 |
+
# REMOVED st.rerun() - This was causing the download button to disappear
|
| 1294 |
|
| 1295 |
+
# Per-file CSV download (MOVED OUTSIDE THE FORM - Always visible)
|
| 1296 |
d_currency = st.session_state.get(f"Currency_{selected_hash}", 'USD')
|
| 1297 |
if d_currency == 'Other':
|
| 1298 |
d_currency = st.session_state.get(f"Currency_Custom_{selected_hash}", '')
|
|
|
|
| 1362 |
with frame_left:
|
| 1363 |
st.caption("Ready when you are.")
|
| 1364 |
with frame_right:
|
| 1365 |
+
st.caption("Preview & editor will appear here after extraction.")
|