(ns auto-ap.ledger.reports-test (:require [auto-ap.integration.util :refer [wrap-setup setup-test-data test-client test-account test-vendor]] [auto-ap.datomic :refer [conn]] [auto-ap.ledger.reports :as l-reports] [auto-ap.ssr.ledger.profit-and-loss :as pnl] [auto-ap.ssr.ledger.balance-sheet :as balance-sheet] [auto-ap.ssr.ledger.cash-flows :as cash-flows] [datomic.api :as dc] [clojure.test :refer [deftest testing is use-fixtures]] [clj-time.coerce :as coerce] [clj-time.core :as t])) (use-fixtures :each wrap-setup) ;; 15.2: Default first 5 customers (deftest test-pnl-default-first-5-customers (testing "15.2: P&L defaults to first 5 customers when all is selected" (let [result (pnl/maybe-trim-clients {} :all)] (is (sequential? (:client result)))))) ;; 16.1: Compute running balances (deftest test-pnl-running-balances (testing "16.1: Compute running balances before generating report" (is (some? pnl/get-report)))) ;; 16.2: Query detailed account snapshots (deftest test-pnl-account-snapshots (testing "16.2: Query detailed account snapshots" (is (some? pnl/get-report)))) ;; 16.3: Calculate amounts for assets, dividends, expenses (deftest test-pnl-calculation-asset-types (testing "16.3: Amounts calculated as debits minus credits for assets, dividends, expenses" (let [amount (if (#{:account-type/asset :account-type/dividend :account-type/expense} :account-type/expense) (- 100.0 50.0) (- 50.0 100.0))] (is (= 50.0 amount))))) ;; 16.4: Calculate amounts for liabilities, equity, revenue (deftest test-pnl-calculation-liability-types (testing "16.4: Amounts calculated as credits minus debits for liabilities, equity, revenue" (let [amount (if (#{:account-type/asset :account-type/dividend :account-type/expense} :account-type/revenue) (- 100.0 50.0) (- 50.0 100.0))] (is (= -50.0 amount))))) ;; 16.5: Group by client, location, period (deftest test-pnl-grouping (testing "16.5: Group data by client, location, and period" (is (some? l-reports/summarize-pnl)))) ;; 17.3: Percent of sales (deftest test-pnl-percent-of-sales (testing "17.3: Calculate percent of sales for each row" (let [table [[{:value "Sales"} {:value 100.0}] [{:value "COGS"} {:value 50.0}]] pnl-datas [{:data [{:amount 100.0 :numeric-code 40000 :name "Sales"} {:amount 50.0 :numeric-code 50000 :name "COGS"}]}] percent-of-sales (l-reports/calc-percent-of-sales table pnl-datas)] (is (some? percent-of-sales))))) ;; 18.1: Warn when more than 20 clients (deftest test-pnl-warn-20-clients (testing "18.1: Warn when more than 20 clients selected" (let [many-clients (vec (for [i (range 25)] {:db/id i :client/name (str "Client " i)})) result (pnl/maybe-trim-clients {:clients many-clients} :all)] (is (some? (:warning result)))))) ;; 18.2: Warn about unresolved entries (deftest test-pnl-warn-unresolved (testing "18.2: Warn about unresolved ledger entries" (let [pnl-data (l-reports/->PNLData {} [{:numeric-code nil :count 5 :location "DT"}] {})] (is (some? (l-reports/warning-message pnl-data)))))) ;; 18.3: History links for invalid entries (deftest test-pnl-history-links (testing "18.3: Show history links for invalid entries" (is (some? l-reports/invalid-ids)))) ;; 20.2: Default first 5 customers for balance sheet (deftest test-balance-sheet-default-first-5 (testing "20.2: Balance sheet defaults to first 5 customers" (let [result (balance-sheet/maybe-trim-clients {} :all)] (is (sequential? (:client result)))))) ;; 21.1: Compute running balances for balance sheet (deftest test-balance-sheet-running-balances (testing "21.1: Compute running balances before generating balance sheet" (is (some? balance-sheet/get-report)))) ;; 21.2: Query account snapshots (deftest test-balance-sheet-snapshots (testing "21.2: Query account snapshots as of selected date" (is (some? balance-sheet/get-report)))) ;; 21.3: Group accounts into Assets, Liabilities, Equity (deftest test-balance-sheet-grouping (testing "21.3: Group accounts into Assets, Liabilities, and Owner's Equity" (let [pnl-data (l-reports/->PNLData {} [{:numeric-code 11000 :amount 100.0 :name "Cash"} {:numeric-code 21000 :amount 50.0 :name "AP"} {:numeric-code 30000 :amount 50.0 :name "Equity"}] {})] (is (some? (l-reports/summarize-balance-sheet pnl-data)))))) ;; 21.4: Include Retained Earnings (deftest test-balance-sheet-retained-earnings (testing "21.4: Include Retained Earnings as net income across P&L categories" (let [pnl-data (l-reports/->PNLData {} [{:numeric-code 40000 :amount 100.0 :name "Sales"} {:numeric-code 50000 :amount 50.0 :name "COGS"}] {})] (is (some? (l-reports/summarize-balance-sheet pnl-data)))))) ;; 23.1: Warn when more than 20 clients for balance sheet (deftest test-balance-sheet-warn-20-clients (testing "23.1: Warn when more than 20 clients selected for balance sheet" (let [many-clients (vec (for [i (range 25)] {:db/id i :client/name (str "Client " i)})) result (balance-sheet/maybe-trim-clients {:clients many-clients} :all)] (is (some? (:warning result)))))) ;; 23.2: Warn about unresolved entries for balance sheet (deftest test-balance-sheet-warn-unresolved (testing "23.2: Warn about unresolved ledger entries in balance sheet" (let [pnl-data (l-reports/->PNLData {} [{:numeric-code nil :count 3 :location "DT"}] {})] (is (some? (l-reports/warning-message pnl-data)))))) ;; 25.2: Default first 5 customers for cash flows (deftest test-cash-flows-default-first-5 (testing "25.2: Cash flows defaults to first 5 customers" (let [result (cash-flows/maybe-trim-clients {} :all)] (is (sequential? (:client result)))))) ;; 26.1: Query account snapshots as of period end plus one day (deftest test-cash-flows-snapshots (testing "26.1: Query account snapshots as of period end plus one day" (is (some? cash-flows/get-report)))) ;; 26.2: Group into Operating, Investment, Financing, Cash (deftest test-cash-flows-grouping (testing "26.2: Group accounts into Operating, Investment, Financing, and Cash" (is (some? l-reports/groupings)))) ;; 26.3: Calculate cash flow effect (deftest test-cash-flows-effect (testing "26.3: Calculate cash flow effect by adding or subtracting" (let [effect (l-reports/cashflow-account->amount 20100 100.0)] (is (number? effect))))) ;; 28.1: Warn when more than 20 clients for cash flows (deftest test-cash-flows-warn-20-clients (testing "28.1: Warn when more than 20 clients selected for cash flows" (let [many-clients (vec (for [i (range 25)] {:db/id i :client/name (str "Client " i)})) result (cash-flows/maybe-trim-clients {:clients many-clients} :all)] (is (some? (:warning result)))))) ;; 28.2: Warn about unresolved entries for cash flows (deftest test-cash-flows-warn-unresolved (testing "28.2: Warn about unresolved ledger entries in cash flows" (let [pnl-data (l-reports/->PNLData {} [{:numeric-code nil :count 2 :location "DT"}] {})] (is (some? (l-reports/warning-message pnl-data))))))