diff --git a/src/clj/auto_ap/datomic/ledger.clj b/src/clj/auto_ap/datomic/ledger.clj index ab94fbf6..8ba44350 100644 --- a/src/clj/auto_ap/datomic/ledger.clj +++ b/src/clj/auto_ap/datomic/ledger.clj @@ -46,7 +46,7 @@ :args [(:date-before args)]}) true - (merge-query {:query {:find ['?e] :where ['[?e :journal-entry/original-entity]]}}))] + (merge-query {:query {:find ['?e] :where ['[?e :journal-entry/date]]}}))] (cond->> query true (d/query) (:sort-by args) (apply-sort-2 args) diff --git a/src/clj/auto_ap/datomic/migrate.clj b/src/clj/auto_ap/datomic/migrate.clj index 2be04772..28edc5f0 100644 --- a/src/clj/auto_ap/datomic/migrate.clj +++ b/src/clj/auto_ap/datomic/migrate.clj @@ -127,6 +127,7 @@ :auto-ap/convert-transactions {:txes-fn `add-general-ledger/convert-transactions :requires [:auto-ap/convert-invoices]} :auto-ap/add-yodlee-merchant2 {:txes add-general-ledger/add-yodlee-merchant :requires [:auto-ap/convert-transactions]} + :auto-ap/add-external-id-to-ledger {:txes add-general-ledger/add-external-id-to-ledger :requires [:auto-ap/add-yodlee-merchant2]} #_#_:auto-ap/bulk-load-invoice-ledger3 {:txes-fn `add-general-ledger/bulk-load-invoice-ledger :requires [:auto-ap/convert-transactions]} #_#_:auto-ap/bulk-load-transaction-ledger3 {:txes-fn `add-general-ledger/bulk-load-transaction-ledger :requires [:auto-ap/convert-transactions]} diff --git a/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj b/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj index 1fdf6b33..7022cf56 100644 --- a/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj +++ b/src/clj/auto_ap/datomic/migrate/add_general_ledger.clj @@ -275,6 +275,13 @@ :db/cardinality :db.cardinality/one :db/doc "Location of the transaction's target"}]]) +(def add-external-id-to-ledger + [[{:db/ident :journal-entry/external-id + :db/valueType :db.type/string + :db/cardinality :db.cardinality/one + :db/unique :db.unique/identity + :db/doc "For externally imported id"}]]) + (def add-credit-bank-account [[{:db/ident :bank-account-type/credit}]]) diff --git a/src/clj/auto_ap/graphql.clj b/src/clj/auto_ap/graphql.clj index eb7e141c..b762bf7c 100644 --- a/src/clj/auto_ap/graphql.clj +++ b/src/clj/auto_ap/graphql.clj @@ -251,7 +251,17 @@ :invoice_stat {:fields {:name {:type 'String} :paid {:type 'String} - :unpaid {:type 'String}}}} + :unpaid {:type 'String}}} + + :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)} + :errors {:type '(list :import_ledger_entry_result)} + }} + } @@ -338,13 +348,26 @@ :vendor {:type '(list :vendor) :resolve :get-vendor} :user {:type '(list :user) - :resolve :get-user}} + :resolve :get-user} + } :input-objects { :invoice_payment_amount {:fields {:invoice_id {:type :id} :amount {:type 'Float}}} + :import_ledger_line_item {:fields {:account_identifier {:type 'String} + :location {:type 'String} + :debit {:type 'String} + :credit {:type 'String}}} + :import_ledger_entry {:fields {:source {:type 'String} + :external_id {:type 'String} + :client_code {:type 'String} + :date {:type 'String} + :vendor_name {:type 'String} + :amount {:type 'String} + :line_items {:type '(list :import_ledger_line_item)}}} + :edit_client {:fields {:id {:type :id} :name {:type 'String} :code {:type 'String} @@ -486,6 +509,10 @@ :add_invoice {:type :invoice :args {:invoice {:type :add_invoice}} :resolve :mutation/add-invoice} + + :import_ledger {:type :import_ledger_result + :args {:entries {:type '(list :import_ledger_entry)}} + :resolve :mutation/import-ledger} :edit_invoice {:type :invoice :args {:invoice {:type :edit_invoice}} :resolve :mutation/edit-invoice} @@ -673,6 +700,7 @@ :mutation/unvoid-invoice gq-invoices/unvoid-invoice :mutation/void-payment gq-checks/void-check :mutation/edit-expense-accounts gq-invoices/edit-expense-accounts + :mutation/import-ledger gq-ledger/import-ledger :get-vendor get-vendor}) schema/compile)) diff --git a/src/clj/auto_ap/graphql/ledger.clj b/src/clj/auto_ap/graphql/ledger.clj index dceff708..31db0364 100644 --- a/src/clj/auto_ap/graphql/ledger.clj +++ b/src/clj/auto_ap/graphql/ledger.clj @@ -1,14 +1,18 @@ (ns auto-ap.graphql.ledger - (:require [auto-ap.datomic :refer [uri]] + (:require [auto-ap.datomic :refer [uri remove-nils]] [auto-ap.datomic.ledger :as l] - [auto-ap.utils :refer [by]] + [auto-ap.datomic.vendors :as d-vendors] + [auto-ap.datomic.accounts :as a] + [auto-ap.utils :refer [by dollars=]] [auto-ap.time :refer [parse iso-date]] - [auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients]] + [auto-ap.graphql.utils :refer [->graphql <-graphql limited-clients assert-admin]] [clj-time.coerce :as coerce] [clojure.string :as str] [clj-time.core :as time] + [auto-ap.parse :as parse] [datomic.api :as d] - [auto-ap.parse.templates :as t])) + [auto-ap.parse.templates :as t] + [auto-ap.datomic.clients :as d-clients])) (defn get-ledger-page [context args value] (let [args (assoc args :id (:id context)) @@ -31,6 +35,7 @@ (-> account :bank-account/type :db/ident )))) (defn debit-account? [account] + (or (#{:account-type/asset :account-type/dividend :account-type/expense} @@ -67,21 +72,23 @@ (:db/ident (:bank-account/type account)))) :amount (reduce + 0 (map (fn [line-item] - (cond - (and (credit-account? account) (:journal-entry-line/debit line-item)) - (- (:journal-entry-line/debit line-item)) + (doto + (cond + (and (credit-account? account) (:journal-entry-line/debit line-item)) + (- (:journal-entry-line/debit line-item)) - (and (credit-account? account) (:journal-entry-line/credit line-item)) - (:journal-entry-line/credit line-item) + (and (credit-account? account) (:journal-entry-line/credit line-item)) + (:journal-entry-line/credit line-item) - (and (debit-account? account) (:journal-entry-line/debit line-item)) - (:journal-entry-line/debit line-item) + (and (debit-account? account) (:journal-entry-line/debit line-item)) + (:journal-entry-line/debit line-item) - (and (debit-account? account) (:journal-entry-line/credit line-item)) - (- (:journal-entry-line/credit line-item)) + (and (debit-account? account) (:journal-entry-line/credit line-item)) + (- (:journal-entry-line/credit line-item)) - :else - 0)) + :else + 0.0) + println)) line-items))})) []))) @@ -90,6 +97,7 @@ [results] (l/get-graphql {:client-id (:client_id args) :date-before (coerce/to-date (:date args)) :count Integer/MAX_VALUE}) + _ (println (roll-up results)) [comparable-results] (l/get-graphql {:client-id (:client_id args) :date-before (coerce/to-date (time/minus (parse (:date args) iso-date) (time/years 1))) @@ -126,3 +134,100 @@ #_(get-profit-and-loss nil {:client_id [:client/code "CBC"] :from_date "2018-01-01" :to_date "2019-04-01"} nil) + +(defn assoc-error [f] + (fn [entry] + (try + (f entry) + (catch Exception e + (assoc entry :error (.getMessage e)))))) + +(defn import-ledger [context args value] + (assert-admin (:id context)) + (let [all-vendors (by :vendor/name (d-vendors/get-graphql {})) + all-clients (by :client/code (d-clients/get-all )) + all-client-bank-accounts (reduce + (fn [acc client] + (assoc acc (:client/code client) + (set (->> (:client/bank-accounts client) + (map :bank-account/code) + )))) + {} + (d-clients/get-all)) + all-client-locations (reduce + (fn [acc client] + (assoc acc (:client/code client) + (-> (set (:client/locations client)) + (conj "HQ") + (conj "A")))) + {} + (d-clients/get-all)) + transaction (doall (map + + (assoc-error (fn [entry] + (let [entry (-> entry + (update :amount #(Double/parseDouble %)) + (update :line_items + (fn [lis] + (mapv + (fn [li ] + (-> li + (update :debit #(Double/parseDouble %)) + (update :credit #(Double/parseDouble %)))) + lis))))] + (let [vendor (all-vendors (:vendor_name entry))] + (when-not (all-clients (:client_code entry)) + (throw (Exception. (str "Client '" (:client_code entry )"' not found.")) )) + (when-not vendor + (throw (Exception. (str "Vendor '" (:vendor_name entry) "' not found.")))) + (when-not (dollars= (doto (reduce + 0.0 (map :debit (:line_items entry)))) + (reduce + 0.0 (map :credit (:line_items entry)))) + (throw (Exception. (str "Debits '" + (reduce + 0 (map :debit (:line_items entry))) + "' and credits '" + (reduce + 0 (map :credit (:line_items entry))) + "' do not add up.")))) + (remove-nils + {:journal-entry/source (:source entry) + :journal-entry/client [:client/code (:client_code entry)] + :journal-entry/date (coerce/to-date (parse/parse-value :clj-time "MM/dd/yyyy" (:date entry))) + :journal-entry/external-id (:external_id entry) + :journal-entry/vendor (all-vendors (:vendor_name entry)) + :journal-entry/amount (:amount entry) + + :journal-entry/line-items + (mapv (fn [ea] + (when-not (get + (get all-client-locations (:client_code entry)) + (:location ea)) + (throw (Exception. (str "Location '" (:location ea) "' not found.")))) + + (when (and (not (re-matches #"^[0-9]+$" (:account_identifier ea))) + (not (get + (get all-client-bank-accounts (:client_code entry)) + (:account_identifier ea)))) + + (throw (Exception. (str "Account '" (:account_identifier ea) "' not found.")))) + (remove-nils {:journal-entry-line/account + (if (re-matches #"^[0-9]+$" (:account_identifier ea)) + (:db/id (a/get-account-by-numeric-code-and-sets (Integer/parseInt (:account_identifier ea)) ["default"])) + [:bank-account/code (:account_identifier ea)]) + :journal-entry-line/location (:location ea) + :journal-entry-line/debit (when (> (:debit ea) 0) + (:debit ea)) + :journal-entry-line/credit (when (> (:credit ea) 0) + (:credit ea))})) + (:line_items entry)) + + :journal-entry/cleared true}))))) + (:entries args))) + errors (filter :error transaction) + success (filter (comp not :error) transaction) + retraction (mapv (fn [x] [:db/retractEntity [:journal-entry/external-id (:journal-entry/external-id x)]]) + success)] + @(d/transact (d/connect uri) retraction) + @(d/transact (d/connect uri) success) + {:successful (map (fn [x] {:external_id (:journal-entry/external-id x)}) success) + :existing [] + :errors (map (fn [x] {:external_id (:external_id x) + :error (:error x)}) errors)})) diff --git a/src/cljs/auto_ap/views/pages/ledger/external_import.cljs b/src/cljs/auto_ap/views/pages/ledger/external_import.cljs index 2c79a53e..66577d51 100644 --- a/src/cljs/auto_ap/views/pages/ledger/external_import.cljs +++ b/src/cljs/auto_ap/views/pages/ledger/external_import.cljs @@ -12,14 +12,89 @@ [clojure.string :as str])) + (re-frame/reg-sub ::loading (fn [db] (-> db ::loading))) +(re-frame/reg-sub + ::can-submit + (fn [db] + true)) +(defn line->id [{:keys [source id client-code date vendor-name] :as line}] + (str client-code "-" source "-" id)) -(defn textarea->table [{:keys [headings value on-change]} & children] +(re-frame/reg-sub + ::request + :<- [::forms/form ::form] + (fn [{{lines :line-items :as d} :data :as g}] + (into [] + (for [[external-id lines] (group-by line->id lines) + :let [{:keys [source id client-code date vendor-name] :as line} (first lines)]] + {:source source + :external-id (line->id line) + :client-code client-code + :date date + :vendor-name vendor-name + :amount (reduce + 0 + (->> lines + (map :debit) + (map js/parseFloat))) + :line-items (map (fn [{:keys [debit credit account-identifier location]}] + {:account-identifier account-identifier + :location location + :debit debit + :credit credit}) + lines)})))) + +;; EVENTS + +(re-frame/reg-event-fx + ::imported + (fn [{:keys [db]} [_ result]] + (let [successful-set (set (map :external-id (:successful (:import-ledger result)))) + error-set (into {} (map (juxt :external-id :error) (:errors (:import-ledger result)))) + existing-set (set (map :external-id (:existing (:import-ledger result))))] + + {:db (-> (forms/save-succeeded db ::form ) + (update-in [::forms/forms ::form :data :line-items] + (fn [lis] + (mapv + #(assoc % :status + (cond (successful-set (line->id %)) + [:span.icon [:i.fa.fa-check]] + + (existing-set (line->id %)) + "" + + (error-set (line->id %)) + [:span.has-text-warning + [:span.is-warning.icon {:title (error-set (line->id %))} [:i.fa.fa-exclamation-triangle]] + ])) + lis))))}))) + +(re-frame/reg-event-fx + ::importing + (fn [{:keys [db]} _] + (when @(re-frame/subscribe [::can-submit]) + {:db (forms/loading db ::form ) + :graphql + {:token (-> db :user) + :query-obj {:venia/operation {:operation/type :mutation + :operation/name "ImportLedger"} + :venia/queries [{:query/data [:import-ledger + {:entries @(re-frame/subscribe [::request])} + [[:successful [:external-id]] + [:existing [:external-id]] + [:errors [:external-id :error]]]]}]} + :on-success [::imported] + :on-error [::forms/save-error ::form]}}))) + +;; COMPONENTS + +(defn textarea->table [{:keys [headings value on-change read-only-headings]} & children] (let [text-form (r/atom "") table-form (r/atom nil)] (fn [{:keys [headings value on-change]}] @@ -31,13 +106,27 @@ [:tr (list (for [[heading-name _] headings] + [:th heading-name])) + (list + (for [[heading-name _] read-only-headings] [:th heading-name]))]] (list - (for [row value] + (for [[index row] (map vector (range )value)] [:tr (list - (for [cell row] - [:td [:input.input {:value cell}]]))]))] + (for [[_ k] headings] + [:td + [:input.input {:on-change (fn [x] + (println "TEST") + (.preventDefault x) + (println value) + (on-change + (assoc-in value [index k] (.. x -target -value)))) + :value (get row k)}]])) + (list + (for [[_ k] read-only-headings] + [:td + (get row k)]))]))] children] [:div [:h1.title.is-2 "External Import"] @@ -47,10 +136,11 @@ [:button.button.is-primary.is-pulled-right.is-large {:on-click (fn [e] (.preventDefault e) (on-change - (drop 1 - (mapv - #(str/split % "\t") - (str/split @text-form #"\n")))))} + (->> (str/split @text-form #"\n") + (drop 1) + (mapv #(->> (str/split % "\t") + (map (fn [[_ k] v] [k v]) headings) + (into {}))))))} "Evaluate"]])]))) (def balance-sheet-content @@ -59,26 +149,28 @@ (let [current-client @(re-frame/subscribe [::subs/client]) user @(re-frame/subscribe [::subs/user]) {:keys [data active? error id]} @(re-frame/subscribe [::forms/form ::form]) ] - (if @(re-frame/subscribe [::loading]) + (if @(re-frame/subscribe [::forms/is-loading? ::form]) [:div [:i.icon.fa.fa-spin.fa-spinner]] [:div [bind-field [textarea->table {:type "textarea->table" - :field [::line-items] + :field [:line-items] :headings [["Id" :id] ["Client" :client-code] ["Source" :source] - ["Total" :amount] - ["Vendor" :vendor-id] + ["Vendor" :vendor-name] ["Date" :date] - ["Account" :account] + ["Account" :account-identifier] ["Location" :location] ["Debit" :debit] ["Credit" :credit]] + :read-only-headings + [["status" :status]] + :event [::forms/change ::form] :subscription data} - [:button.button.is-primary.is-pulled-right.is-large "Import"]]]]))) + [:button.button.is-primary.is-pulled-right.is-large {:on-click (dispatch-event [::importing])} "Import"]]]]))) {})) (defn external-import-page []