Feat/Complete Sales Summaries (#5)
## Summary Completes the automatic sales summary pipeline end-to-end: the `sales-summaries-v2` job now calculates aggregate totals, preserves manual adjustments, and automatically posts balanced journal entries to the ledger. ## What Changed **New Datomic transaction function** (`upsert-sales-summary-ledger`) - Transforms detailed `sales-summary-item`s into aggregated `journal-entry` lines grouped by account and ledger side - Handles the full upsert: posts a new journal entry for summaries with mapped accounts, or retracts the orphaned entry if items no longer qualify **Enhanced `sales-summaries-v2` job** - Calculates and stores 13 aggregate total attributes (card/cash/food-app/gift-card payments, refunds, fees, discounts, tax, tip, returns, unknown, net) - Preserves manual items (`manual? true`) during recalculation — only auto-calculated items are replaced **Ledger reconciliation** - `reconcile-ledger` now queries for sales summaries missing journal entries and repairs them via `:upsert-sales-summary-ledger`, alongside existing invoice and transaction repairs **Schema** - Added 13 `total-*` attributes on `sales-summary` (all `db.type/double`, no history) - Registered the new transaction function in `tx.clj` and `datomic.clj` **Admin UI cleanup** - Resolved "clientize" and HTMX `client-id` TODOs in the sales summaries admin page - `new-summary-item` now correctly passes `client-id` via `hx-vals` - Removed stale TODO comments and placeholder code ## Files Changed (8) | File | Purpose | |------|---------| | `iol_ion/.../upsert_sales_summary_ledger.clj` | New Datomic tx function | | `iol_ion/.../tx.clj` | Register new tx function | | `resources/schema.edn` | 13 new `total-*` attributes | | `src/.../datomic.clj` | Load new tx namespace | | `src/.../jobs/sales_summaries.clj` | Aggregate totals + manual item preservation | | `src/.../ledger.clj` | Sales summary repair in `reconcile-ledger` | | `src/.../ssr/admin/sales_summaries.clj` | UI TODO cleanup | | `docs/plans/...plan.md` | Implementation plan document | Co-authored-by: Bryce <bryce@integreatconsult.com> Reviewed-on: #5 Co-authored-by: Bryce <bryce@brycecovertoperations.com> Co-committed-by: Bryce <bryce@brycecovertoperations.com>
This commit was merged in pull request #5.
This commit is contained in:
@@ -35,27 +35,42 @@
|
||||
|
||||
|
||||
invoices-missing-ledger-entries (->> (dc/q {:find ['?t ]
|
||||
:in ['$ '?sd]
|
||||
:where ['[?t :invoice/date ?d]
|
||||
'[(>= ?d ?sd)]
|
||||
'(not [_ :journal-entry/original-entity ?t])
|
||||
'[?t :invoice/total ?amt]
|
||||
'[(not= 0.0 ?amt)]
|
||||
'(not [?t :invoice/status :invoice-status/voided])
|
||||
'(not [?t :invoice/import-status :import-status/pending])
|
||||
'(not [?t :invoice/exclude-from-ledger true])
|
||||
]}
|
||||
(dc/db conn) start-date)
|
||||
:in ['$ '?sd]
|
||||
:where ['[?t :invoice/date ?d]
|
||||
'[(>= ?d ?sd)]
|
||||
'(not [_ :journal-entry/original-entity ?t])
|
||||
'[?t :invoice/total ?amt]
|
||||
'[(not= 0.0 ?amt)]
|
||||
'(not [?t :invoice/status :invoice-status/voided])
|
||||
'(not [?t :invoice/import-status :import-status/pending])
|
||||
'(not [?t :invoice/exclude-from-ledger true])
|
||||
]}
|
||||
(dc/db conn) start-date)
|
||||
(map first)
|
||||
(mapv (fn [i]
|
||||
[:upsert-invoice {:db/id i}])))
|
||||
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries))]
|
||||
|
||||
sales-summaries-missing-ledger-entries (->> (dc/q {:find ['?ss ]
|
||||
:in ['$ '?sd]
|
||||
:where ['[?ss :sales-summary/date ?d]
|
||||
'[(>= ?d ?sd)]
|
||||
'(not [_ :journal-entry/original-entity ?ss])
|
||||
'[?ss :sales-summary/items ?item]
|
||||
'[?item :ledger-mapped/account]
|
||||
]}
|
||||
(dc/db conn) start-date)
|
||||
(map first)
|
||||
(mapv (fn [ss]
|
||||
[:upsert-sales-summary {:db/id ss}])))
|
||||
|
||||
repairs (vec (concat txes-missing-ledger-entries invoices-missing-ledger-entries sales-summaries-missing-ledger-entries))]
|
||||
(when (seq repairs)
|
||||
(mu/log ::ledger-repairs-needed
|
||||
:sample (take 3 repairs)
|
||||
:transaction-count (count txes-missing-ledger-entries)
|
||||
:invoice-count (count invoices-missing-ledger-entries))
|
||||
@(dc/transact conn repairs)))))
|
||||
(mu/log ::ledger-repairs-needed
|
||||
:sample (take 3 repairs)
|
||||
:transaction-count (count txes-missing-ledger-entries)
|
||||
:invoice-count (count invoices-missing-ledger-entries)
|
||||
:sales-summary-count (count sales-summaries-missing-ledger-entries))
|
||||
@(dc/transact conn repairs)))))
|
||||
|
||||
|
||||
(defn touch-transaction [e]
|
||||
|
||||
Reference in New Issue
Block a user