diff --git a/src/clj/auto_ap/datomic/ledger.clj b/src/clj/auto_ap/datomic/ledger.clj index a12719ce..ab94fbf6 100644 --- a/src/clj/auto_ap/datomic/ledger.clj +++ b/src/clj/auto_ap/datomic/ledger.clj @@ -17,7 +17,7 @@ (keyword "transaction" sort-by))) (defn raw-graphql-ids [db args] - (println args) + (let [query (cond-> {:query {:find [] :in ['$ ] :where []} @@ -67,7 +67,6 @@ (defn get-graphql [args] (let [db (d/db (d/connect uri)) {ids-to-retrieve :ids matching-count :count} (raw-graphql-ids db args)] - [(->> (graphql-results ids-to-retrieve db args)) matching-count])) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index b86aa54e..3b786b55 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -1,6 +1,7 @@ (ns auto-ap.graphql.ledger (:require [auto-ap.datomic :refer [uri]] [auto-ap.datomic.ledger :as l] + [auto-ap.utils :refer [by]] [auto-ap.time :refer [parse iso-date]] [auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients]] [clj-time.coerce :as coerce] @@ -98,20 +99,27 @@ (defn get-profit-and-loss [context args value] (let [args (assoc args :id (:id context)) - [starting-results] (l/get-graphql {:client-id (:client_id args) - :date-before (coerce/to-date (:from_date args)) - :count Integer/MAX_VALUE}) - [ending-results] (l/get-graphql {:client-id (:client_id args) - :date-before (coerce/to-date (:to_date args)) - :count Integer/MAX_VALUE}) - - starting-accounts (roll-up starting-results) - ending-accounts (roll-up ending-results) - pnl starting-accounts - comparable-pnl ending-accounts] + pnl (fn [from-date to-date] + (let [[starting-results] (l/get-graphql {:client-id (:client_id args) + :date-before from-date + :count Integer/MAX_VALUE}) + [ending-results] (l/get-graphql {:client-id (:client_id args) + :date-before to-date + :count Integer/MAX_VALUE}) + starting-accounts (by :id (roll-up starting-results)) + ending-accounts (by :id (roll-up ending-results))] + (reduce-kv + (fn [results k v] + (conj results (update v :amount (fn [amt] + (- amt + (get-in starting-accounts [k :amount] 0)))))) + [] + ending-accounts)))] (->graphql - {:balance-sheet-accounts pnl - :comparable-balance-sheet-accounts comparable-pnl}))) + {:balance-sheet-accounts (pnl (coerce/to-date (:from_date args)) + (coerce/to-date (:to_date args))) + :comparable-balance-sheet-accounts (pnl (coerce/to-date (coerce/to-date (time/minus (coerce/to-date-time (:from_date args)) (time/years 1)))) + (coerce/to-date (coerce/to-date (time/minus (coerce/to-date-time (:to_date args)) (time/years 1)))))}))) #_(get-profit-and-loss nil {:client_id [:client/code "CBC"] :from_date "2018-01-01" diff --git a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs index e98034b8..1dd4cc26 100644 --- a/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/profit_and_loss.cljs @@ -35,6 +35,7 @@ (re-frame/reg-sub ::accounts (fn [db [_ type only-location]] + (println (::report db)) (->> db ::report :balance-sheet-accounts @@ -112,14 +113,56 @@ :to-date to :selected selected)]})) (def groupings - {:expense [["Expenses" 4000 4999] - ["Expenses 2" 5000 5999] - ["Expenses 3" 6000 6999] - ["Expenses 4" 7000 7999] - ["Expenses 5" 8000 8999]]}) + + {:expense [["1100 Cash and Bank Accounts" 1100 1199] + ["1200 Accounts Receivable" 1200 1299] + ["1300 Inventory" 1300 1399] + ["1400 Prepaid Expenses" 1400 1499] + ["1500 Property and Equipment" 1500 1599] + ["1600 Intangible Assets" 1600 1699] + ["2100 Accounts Payable" 2100 2399] + ["2400 Accrued Expenses" 2400 2499] + ["2500 Other Liabilities" 2500 2599] + ["2600 Split Accounts" 2600 2699] + ["2700 Current Portion of Long-Term Debt" 2700 2799] + ["2800 Notes Payable" 2800 2899] + + ["4100-4200 Food Sales" 4100 4199] + ["4400-4600 Alcohol Sales" 4400 4599] + ["4700 Merchandise Sales" 4700 4799] + ["4800 Other Operating Income" 4800 4899] + ["5100-5200 Food Costs" 5100 5199] + ["5400-5600 Alcohol Cost" 5400 5599] + ["5700 Merchandise Cost" 5700 5799] + ["5800 Other Operating Cost" 5800 5899] + ["5900 Paper Cost" 5900 5999] + ["6010 HQ Payroll" 6010 6029] + ["6030 Payroll Benefits and Taxes" 6030 6099] + ["6100 Management Payroll" 6100 6199] + ["6200 Staff Payroll" 6200 6299] + ["7100 Ops Related DirGMC Exp" 7100 7199] + ["7200 Customer Related DirGMC Exp" 7200 7299] + ["7300 Employee Related DirGMC Exp" 7300 7399] + ["7400 Building and Equipment Related DirGMC Exp" 7400 7399] + ["7500 Office/ Management Related DirGMC Exp" 7500 7599] + ["8100 Operational" 8100 8199] + ["8200 Occupancy Costs" 8200 8299] + ["8300 Utilities" 8300 8399] + ["8400 Equipment Rental" 8400 8499] + ["8500-8700 Taxes and Insurance" 8500 8699] + ["8800 Depreciation" 8800 8899] + ["9100 Promotion and Outreach" 9100 9199] + ["9200 Employee Morale and Training" 9200 9299] + ["9300 Operational" 9300 9499] + ["9500 Interest and Bank Expenses" 9500 9599] + ["9600 Depreciation" 9600 9699] + ["9700 Taxes" 9700 9799] + ["9800 Other Expenses" 9800 9899] + ["9900 Tax Only Expenses" 9900 9999]]}) (defn grouping [{:keys [header accounts comparable-accounts groupings]}] + (println accounts) (for [[grouping-name from to] groupings :let [matching-accounts (filter #(<= from (:numeric-code %) to)