progress on a shared pnl with pdf.

This commit is contained in:
2022-03-18 23:00:57 -07:00
parent 6e41ada061
commit f536d1ac5e
6 changed files with 1646 additions and 172 deletions

View File

@@ -1,20 +1,23 @@
(ns auto-ap.graphql.ledger
(:require [auto-ap.datomic :refer [audit-transact-batch remove-nils uri conn]]
[auto-ap.datomic.accounts :as a]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.ledger :as l]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.graphql.utils
:refer
[->graphql <-graphql assert-admin assert-can-see-client result->page]]
[auto-ap.parse.util :as parse]
[auto-ap.utils :refer [by dollars=]]
[clj-time.coerce :as coerce]
[clojure.tools.logging :as log]
[datomic.api :as d]
[unilog.context :as lc]
[mount.core :as mount]
[yang.scheduler :as scheduler]))
(:require
[auto-ap.datomic :refer [audit-transact-batch conn remove-nils uri]]
[auto-ap.datomic.accounts :as a]
[auto-ap.datomic.clients :as d-clients]
[auto-ap.datomic.ledger :as l]
[auto-ap.datomic.vendors :as d-vendors]
[auto-ap.graphql.utils
:refer [->graphql <-graphql assert-admin assert-can-see-client result->page]]
[auto-ap.parse.util :as parse]
[auto-ap.utils :refer [by dollars=]]
[auto-ap.pdf.ledger :refer [print-pnl]]
[clj-time.coerce :as coerce]
[clojure.tools.logging :as log]
[com.walmartlabs.lacinia.util :refer [attach-resolvers]]
[datomic.api :as d]
[mount.core :as mount]
[unilog.context :as lc]
[yang.scheduler :as scheduler]))
(mount/defstate running-balance-cache
:start (atom {}))
@@ -205,6 +208,34 @@
#(roll-up-until (lookup-account %) (all-ledger-entries %) (coerce/to-date end) (coerce/to-date start) )
client-ids)})))})))
(defn profit-and-loss-pdf [context args value]
(let [data (get-profit-and-loss context args value)
result (print-pnl args data)]
(->graphql {:report_url result}))
#_(let [client-id (:client_id args)
client-ids (or (some-> client-id vector)
(filter identity (:client_ids args)))
_ (when (not (seq client-ids))
(throw (ex-info "Please select a client." {:validation-error "Please select a client."})))
_ (doseq [client-id client-ids]
(assert-can-see-client (:id context) client-id))
all-ledger-entries (->> client-ids
(map (fn [client-id]
[client-id (full-ledger-for-client client-id)]))
(into {}))
lookup-account (->> client-ids
(map (fn [client-id]
[client-id (build-account-lookup client-id)]))
(into {}))]
(->graphql
{:periods
(->> (:periods args)
(mapv (fn [{:keys [start end]}]
{:accounts (mapcat
#(roll-up-until (lookup-account %) (all-ledger-entries %) (coerce/to-date end) (coerce/to-date start) )
client-ids)})))})))
(defn assoc-error [f]
(fn [entry]
@@ -490,3 +521,169 @@
:start (scheduler/every (* 15 60 1000) refresh-running-balance-cache)
:stop (scheduler/stop running-balance-cache-worker))
(def objects
{:balance_sheet_account
{:fields {:id {:type 'String}
:amount {:type 'String}
:location {:type 'String}
:client_id {:type :id}
:count {:type 'Int}
:numeric_code {:type 'Int}
:account_type {:type :account_type}
:name {:type 'String}}}
:profit_and_loss_pdf
{:fields {:report_url {:type 'String}}}
:balance_sheet
{:fields {:balance_sheet_accounts {:type '(list :balance_sheet_account)}
:comparable_balance_sheet_accounts {:type '(list :balance_sheet_account)}}}
:profit_and_loss_report_period
{:fields {:accounts {:type '(list :balance_sheet_account)}}}
:profit_and_loss_report
{:fields {:periods {:type '(list :profit_and_loss_report_period)}}}
:journal_entry_line
{:fields {:id {:type :id}
:account {:type :account}
:location {:type 'String}
:debit {:type 'String}
:credit {:type 'String}
:running_balance {:type :money}}}
:journal_entry
{:fields {:id {:type :id}
:source {:type 'String}
:external_id {:type 'String}
:original_entity {:type :id}
:amount {:type 'String}
:note {:type 'String}
:cleared_against {:type 'String}
:client {:type :client}
:vendor {:type :vendor}
:alternate_description {:type 'String}
:date {:type 'String}
:line_items {:type '(list :journal_entry_line)}}}
:ledger_page
{:fields {:journal_entries {:type '(list :journal_entry)}
:count {:type 'Int}
:total {:type 'Int}
:start {:type 'Int}
:end {:type 'Int}}}
:import_ledger_entry_result
{:fields {:external_id {:type 'String}
:error {:type 'String}
:status {:type 'String}}}
:import_ledger_result
{:fields {:successful {:type '(list :import_ledger_entry_result)}
:existing {:type '(list :import_ledger_entry_result)}
:ignored {:type '(list :import_ledger_entry_result)}
:errors {:type '(list :import_ledger_entry_result)}}}})
(def queries
{:balance_sheet {:type :balance_sheet
:args {:client_id {:type :id}
:include_comparison {:type 'Boolean}
:date {:type :iso_date}
:comparison_date {:type :iso_date}}
:resolve :get-balance-sheet}
:profit_and_loss {:type :profit_and_loss_report
:args {:client_id {:type :id}
:client_ids {:type '(list :id)}
:periods {:type '(list :date_range)}}
:resolve :get-profit-and-loss}
:profit_and_loss_pdf {:type :profit_and_loss_pdf
:args {:client_id {:type :id}
:client_ids {:type '(list :id)}
:periods {:type '(list :date_range)}}
:resolve :profit-and-loss-pdf}
:ledger_page {:type :ledger_page
:args {:filters {:type :ledger_filters}}
:resolve :get-ledger-page}})
(def mutations
{:import_ledger
{:type :import_ledger_result
:args {:entries {:type '(list :import_ledger_entry)}}
:resolve :mutation/import-ledger}
:delete_external_ledger
{:type :message
:args {:filters {:type :ledger_filters}
:ids {:type '(list :id)}}
:resolve :mutation/delete-external-ledger}})
(def input-objects
{:ledger_filters
{:fields {:client_id {:type :id}
:vendor_id {:type :id}
:account_id {:type :id}
:amount_lte {:type :money}
:amount_gte {:type :money}
:bank_account_id {:type :id}
:date_range {:type :date_range}
:location {:type 'String}
:from_numeric_code {:type 'Int}
:to_numeric_code {:type 'Int}
:start {:type 'Int}
:per_page {:type 'Int}
:only_external {:type 'Boolean}
:external_id_like {:type 'String}
:source {:type 'String}
:sort {:type '(list :sort_item)}}}
:import_ledger_line_item
{:fields {:account_identifier {:type 'String}
:location {:type 'String}
:debit {:type :money}
:credit {:type :money}}}
:import_ledger_entry
{:fields {:source {:type 'String}
:external_id {:type 'String}
:client_code {:type 'String}
:date {:type 'String}
:vendor_name {:type 'String}
:amount {:type :money}
:note {:type 'String}
:cleared_against {:type 'String}
:line_items {:type '(list :import_ledger_line_item)}}}
})
(def enums
{:payment_type {:values [{:enum-value :check}
{:enum-value :cash}
{:enum-value :debit}
{:enum-value :credit}]}
:payment_status {:values [{:enum-value :voided}
{:enum-value :pending}
{:enum-value :cleared}]}})
(def resolvers
{:get-ledger-page get-ledger-page
:get-balance-sheet get-balance-sheet
:get-profit-and-loss get-profit-and-loss
:profit-and-loss-pdf profit-and-loss-pdf
:mutation/delete-external-ledger delete-external-ledger
:mutation/import-ledger import-ledger})
(defn attach [schema]
(->
(merge-with merge schema
{:objects objects
:queries queries
:mutations mutations
:input-objects input-objects
:enums enums})
(attach-resolvers resolvers)))